Module gcal_calendar
[hide private]
[frames] | no frames]

Source Code for Module gcal_calendar

  1  ### BITPIM 
  2  ### 
  3  ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org> 
  4  ### 
  5  ### This program is free software; you can redistribute it and/or modify 
  6  ### it under the terms of the BitPim license as detailed in the LICENSE file. 
  7  ### 
  8  ### $Id: gcal_calendar.py 4375 2007-08-25 16:25:22Z djpham $ 
  9   
 10  "Deals with Google Calendar (gCalendar) import stuff" 
 11   
 12  # system modules 
 13  from __future__ import with_statement 
 14  import urllib2 
 15   
 16  # site modules 
 17  import wx 
 18   
 19  # local modules 
 20  import common_calendar 
 21  import database 
 22  import guihelper 
 23  import ical_calendar as ical 
 24  import vcal_calendar as vcal 
 25   
 26  module_debug=False 
 27   
 28  #------------------------------------------------------------------------------- 
29 -class ImportDataSource(common_calendar.ImportDataSource):
30 # how to define, and retrieve calendar import data source 31 message_str='Select a Google Calendar iCal URL' 32
33 - def browse(self, parent=None):
34 # how to select a source, default to select a file 35 if parent is None or not hasattr(parent, 'GetActiveDatabase'): 36 # need the database 37 return 38 with guihelper.WXDialogWrapper(SelectURLDialog(parent, self.message_str, 39 parent.GetActiveDatabase()), 40 True) as (dlg, retcode): 41 if retcode==wx.ID_OK: 42 self._source=dlg.GetPath()
43 44 #------------------------------------------------------------------------------- 45 URLDictKey='URLs' 46 URLDictName='gCalURL'
47 -class URLDataObject(database.basedataobject):
48 # object to store a list of URLs & names in the DB 49 _knownlistproperties=database.basedataobject._knownlistproperties.copy() 50 _knownlistproperties.update( { 'urls': [ 'url', 'name'] })
51 - def __init__(self, data=None):
52 if data: 53 self.update(data)
54 urlobjectfactory=database.dataobjectfactory(URLDataObject) 55 56 #-------------------------------------------------------------------------------
57 -class gCalendarServer(vcal.vCalendarFile):
58
59 - def _open(self, name):
60 return urllib2.urlopen(name)
61 62 #------------------------------------------------------------------------------- 63 parentclass=ical.iCalendarImportData
64 -class gCalendarImportData(parentclass):
65 _source_data_class=gCalendarServer
66 - def read(self, file_name=None, update_dlg=None):
67 try: 68 super(gCalendarImportData, self).read(file_name, update_dlg) 69 except urllib2.URLError: 70 raise IOError
71 72 #-------------------------------------------------------------------------------
73 -class gCalImportDialog(ical.iCalImportCalDialog):
74 _filetype_label='Google Calendar iCal URL:' 75 _data_type='Google Calendar' 76 _import_data_class=gCalendarImportData
77 - def __init__(self, parent, id, title):
78 self._db=parent.GetActiveDatabase() 79 super(gCalImportDialog, self).__init__(parent, id, title)
80
81 - def OnBrowseFolder(self, _):
82 with guihelper.WXDialogWrapper(SelectURLDialog(self, 'Select a Google Calendar iCal URL', self._db), 83 True) as (dlg, retcode): 84 if retcode==wx.ID_OK: 85 self.folderctrl.SetValue(dlg.GetPath())
86 87 #-------------------------------------------------------------------------------
88 -class SelectURLDialog(wx.Dialog):
89 - def __init__(self, parent, message, database):
90 super(SelectURLDialog, self).__init__(parent, -1, 'URL Selection') 91 self._db=database 92 self._data=[] 93 vbs=wx.BoxSizer(wx.VERTICAL) 94 vbs.Add(wx.StaticText(self, -1, message), 0, wx.EXPAND|wx.ALL, 5) 95 self._choices=wx.ListBox(self, -1, 96 style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_NEEDED_SB) 97 wx.EVT_LISTBOX_DCLICK(self, self._choices.GetId(), self.OnOK) 98 vbs.Add(self._choices, 0, wx.EXPAND|wx.ALL, 5) 99 vbs.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5) 100 hbs=self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL) 101 _btn=wx.Button(self, -1, 'New') 102 wx.EVT_BUTTON(self, _btn.GetId(), self.OnNew) 103 hbs.Add(_btn, 0, wx.EXPAND|wx.ALL, 5) 104 _btn=wx.Button(self, -1, 'Delete') 105 wx.EVT_BUTTON(self, _btn.GetId(), self.OnDel) 106 hbs.Add(_btn, 0, wx.EXPAND|wx.ALL, 5) 107 vbs.Add(hbs, 0, wx.EXPAND|wx.ALL, 5) 108 109 self._get_from_fs() 110 self.SetSizer(vbs) 111 self.SetAutoLayout(True) 112 vbs.Fit(self)
113
114 - def _get_from_fs(self):
115 # retrieve data from the DB 116 _db_data=self._db.getmajordictvalues(URLDictName, urlobjectfactory) 117 self.set(_db_data.get(URLDictKey, {}).get('urls', []))
118 - def _save_to_fs(self, data):
119 _dict={ URLDictKey: { 'urls': data } } 120 database.ensurerecordtype(_dict, urlobjectfactory) 121 self._db.savemajordict(URLDictName, _dict)
122 - def set(self, data):
123 self._data=data 124 self._choices.Clear() 125 for _item in self._data: 126 self._choices.Append(_item['name'], _item['url'])
127 - def OnDel(self, _):
128 _idx=self._choices.GetSelection() 129 if _idx==wx.NOT_FOUND: 130 return 131 self._choices.Delete(_idx) 132 del self._data[_idx] 133 self._save_to_fs(self._data)
134 - def OnNew(self, _):
135 with guihelper.WXDialogWrapper(NewURLDialog(self), 136 True) as (_dlg, retcode): 137 if retcode==wx.ID_OK: 138 _name, _url=_dlg.get() 139 self._choices.Append(_name, _url) 140 self._data.append({ 'name': _name, 141 'url': _url }) 142 self._save_to_fs(self._data)
143 - def OnOK(self, evt):
144 self.EndModal(wx.ID_OK)
145 - def GetPath(self):
146 _idx=self._choices.GetSelection() 147 if _idx==wx.NOT_FOUND: 148 return '' 149 return self._choices.GetClientData(_idx)
150 151 #-------------------------------------------------------------------------------
152 -class NewURLDialog(wx.Dialog):
153 - def __init__(self, parent):
154 super(NewURLDialog, self).__init__(parent, -1, 'New URL Entry') 155 vbs=wx.BoxSizer(wx.VERTICAL) 156 vbs.Add(wx.StaticText(self, -1, 'URL:'), 0, wx.EXPAND|wx.ALL, 5) 157 self._url=wx.TextCtrl(self, -1, '') 158 vbs.Add(self._url, 0, wx.EXPAND|wx.ALL, 5) 159 vbs.Add(wx.StaticText(self, -1, 'Name:'), 0, wx.EXPAND|wx.ALL, 5) 160 self._name=wx.TextCtrl(self, -1, '') 161 vbs.Add(self._name, 0, wx.EXPAND|wx.ALL, 5) 162 vbs.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5) 163 vbs.Add(self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL), 164 0, wx.EXPAND|wx.ALL, 5) 165 166 self.SetSizer(vbs) 167 self.SetAutoLayout(True) 168 vbs.Fit(self)
169
170 - def get(self):
171 return self._name.GetValue(), self._url.GetValue()
172