PyXR

c:\projects\bitpim\src \ gcal_calendar.py



0001 ### BITPIM
0002 ###
0003 ### Copyright (C) 2006 Joe Pham <djpham@bitpim.org>
0004 ###
0005 ### This program is free software; you can redistribute it and/or modify
0006 ### it under the terms of the BitPim license as detailed in the LICENSE file.
0007 ###
0008 ### $Id: gcal_calendar.py 4375 2007-08-25 16:25:22Z djpham $
0009 
0010 "Deals with Google Calendar (gCalendar) import stuff"
0011 
0012 # system modules
0013 from __future__ import with_statement
0014 import urllib2
0015 
0016 # site modules
0017 import wx
0018 
0019 # local modules
0020 import common_calendar
0021 import database
0022 import guihelper
0023 import ical_calendar as ical
0024 import vcal_calendar as vcal
0025 
0026 module_debug=False
0027 
0028 #-------------------------------------------------------------------------------
0029 class ImportDataSource(common_calendar.ImportDataSource):
0030     # how to define, and retrieve calendar import data source
0031     message_str='Select a Google Calendar iCal URL'
0032 
0033     def browse(self, parent=None):
0034         # how to select a source, default to select a file
0035         if parent is None or not hasattr(parent, 'GetActiveDatabase'):
0036             # need the database
0037             return
0038         with guihelper.WXDialogWrapper(SelectURLDialog(parent, self.message_str,
0039                                                        parent.GetActiveDatabase()),
0040                                        True) as (dlg, retcode):
0041             if retcode==wx.ID_OK:
0042                 self._source=dlg.GetPath()
0043 
0044 #-------------------------------------------------------------------------------
0045 URLDictKey='URLs'
0046 URLDictName='gCalURL'
0047 class URLDataObject(database.basedataobject):
0048     # object to store a list of URLs & names in the DB
0049     _knownlistproperties=database.basedataobject._knownlistproperties.copy()
0050     _knownlistproperties.update( { 'urls': [ 'url', 'name'] })
0051     def __init__(self, data=None):
0052         if data:
0053             self.update(data)
0054 urlobjectfactory=database.dataobjectfactory(URLDataObject)
0055 
0056 #-------------------------------------------------------------------------------
0057 class gCalendarServer(vcal.vCalendarFile):
0058 
0059     def _open(self, name):
0060         return urllib2.urlopen(name)
0061 
0062 #-------------------------------------------------------------------------------
0063 parentclass=ical.iCalendarImportData
0064 class gCalendarImportData(parentclass):
0065     _source_data_class=gCalendarServer
0066     def read(self, file_name=None, update_dlg=None):
0067         try:
0068             super(gCalendarImportData, self).read(file_name, update_dlg)
0069         except urllib2.URLError:
0070             raise IOError
0071 
0072 #-------------------------------------------------------------------------------
0073 class gCalImportDialog(ical.iCalImportCalDialog):
0074     _filetype_label='Google Calendar iCal URL:'
0075     _data_type='Google Calendar'
0076     _import_data_class=gCalendarImportData
0077     def __init__(self, parent, id, title):
0078         self._db=parent.GetActiveDatabase()
0079         super(gCalImportDialog, self).__init__(parent, id, title)
0080 
0081     def OnBrowseFolder(self, _):
0082         with guihelper.WXDialogWrapper(SelectURLDialog(self, 'Select a Google Calendar iCal URL', self._db),
0083                                        True) as (dlg, retcode):
0084             if retcode==wx.ID_OK:
0085                 self.folderctrl.SetValue(dlg.GetPath())
0086 
0087 #-------------------------------------------------------------------------------
0088 class SelectURLDialog(wx.Dialog):
0089     def __init__(self, parent, message, database):
0090         super(SelectURLDialog, self).__init__(parent, -1, 'URL Selection')
0091         self._db=database
0092         self._data=[]
0093         vbs=wx.BoxSizer(wx.VERTICAL)
0094         vbs.Add(wx.StaticText(self, -1, message), 0, wx.EXPAND|wx.ALL, 5)
0095         self._choices=wx.ListBox(self, -1,
0096                                  style=wx.LB_SINGLE|wx.LB_HSCROLL|wx.LB_NEEDED_SB)
0097         wx.EVT_LISTBOX_DCLICK(self, self._choices.GetId(), self.OnOK)
0098         vbs.Add(self._choices, 0, wx.EXPAND|wx.ALL, 5)
0099         vbs.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5)
0100         hbs=self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL)
0101         _btn=wx.Button(self, -1, 'New')
0102         wx.EVT_BUTTON(self, _btn.GetId(), self.OnNew)
0103         hbs.Add(_btn, 0, wx.EXPAND|wx.ALL, 5)
0104         _btn=wx.Button(self, -1, 'Delete')
0105         wx.EVT_BUTTON(self, _btn.GetId(), self.OnDel)
0106         hbs.Add(_btn, 0, wx.EXPAND|wx.ALL, 5)
0107         vbs.Add(hbs, 0, wx.EXPAND|wx.ALL, 5)
0108 
0109         self._get_from_fs()
0110         self.SetSizer(vbs)
0111         self.SetAutoLayout(True)
0112         vbs.Fit(self)
0113 
0114     def _get_from_fs(self):
0115         # retrieve data from the DB
0116         _db_data=self._db.getmajordictvalues(URLDictName, urlobjectfactory)
0117         self.set(_db_data.get(URLDictKey, {}).get('urls', []))
0118     def _save_to_fs(self, data):
0119         _dict={ URLDictKey: { 'urls': data } }
0120         database.ensurerecordtype(_dict, urlobjectfactory)
0121         self._db.savemajordict(URLDictName, _dict)
0122     def set(self, data):
0123         self._data=data
0124         self._choices.Clear()
0125         for _item in self._data:
0126             self._choices.Append(_item['name'], _item['url'])
0127     def OnDel(self, _):
0128         _idx=self._choices.GetSelection()
0129         if _idx==wx.NOT_FOUND:
0130             return
0131         self._choices.Delete(_idx)
0132         del self._data[_idx]
0133         self._save_to_fs(self._data)
0134     def OnNew(self, _):
0135         with guihelper.WXDialogWrapper(NewURLDialog(self),
0136                                        True) as (_dlg, retcode):
0137             if retcode==wx.ID_OK:
0138                 _name, _url=_dlg.get()
0139                 self._choices.Append(_name, _url)
0140                 self._data.append({ 'name': _name,
0141                                     'url': _url })
0142                 self._save_to_fs(self._data)
0143     def OnOK(self, evt):
0144         self.EndModal(wx.ID_OK)
0145     def GetPath(self):
0146         _idx=self._choices.GetSelection()
0147         if _idx==wx.NOT_FOUND:
0148             return ''
0149         return self._choices.GetClientData(_idx)
0150 
0151 #-------------------------------------------------------------------------------
0152 class NewURLDialog(wx.Dialog):
0153     def __init__(self, parent):
0154         super(NewURLDialog, self).__init__(parent, -1, 'New URL Entry')
0155         vbs=wx.BoxSizer(wx.VERTICAL)
0156         vbs.Add(wx.StaticText(self, -1, 'URL:'), 0, wx.EXPAND|wx.ALL, 5)
0157         self._url=wx.TextCtrl(self, -1, '')
0158         vbs.Add(self._url, 0, wx.EXPAND|wx.ALL, 5)
0159         vbs.Add(wx.StaticText(self, -1, 'Name:'), 0, wx.EXPAND|wx.ALL, 5)
0160         self._name=wx.TextCtrl(self, -1, '')
0161         vbs.Add(self._name, 0, wx.EXPAND|wx.ALL, 5)
0162         vbs.Add(wx.StaticLine(self), 0, wx.EXPAND|wx.ALL, 5)
0163         vbs.Add(self.CreateStdDialogButtonSizer(wx.OK|wx.CANCEL),
0164                 0, wx.EXPAND|wx.ALL, 5)
0165         
0166         self.SetSizer(vbs)
0167         self.SetAutoLayout(True)
0168         vbs.Fit(self)
0169 
0170     def get(self):
0171         return self._name.GetValue(), self._url.GetValue()
0172 

Generated by PyXR 0.9.4