-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcal.py
executable file
·367 lines (298 loc) · 10.8 KB
/
cal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/python cal.py
# Calendar, Graphical calendar applet with novel interface
#
# cal.py
#
# Copyright (c) 2010, Brandon Lewis <[email protected]>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
import gtk
import pango
import gobject
import datetime
from gettext import gettext as _
from schedule import Schedule, Event
from command import UndoStack, MenuCommand, Command
from calendarwidget import CalendarInfo
from weekview import WeekView
from monthview import MonthView
import recurrence
import settings
import parser
import os
import time
#TODO: event alarms
#TODO: sync with google calendar
#TODO: sync with facebook calendar
#TODO: sync with evolution ??
#TODO: go to next {week, month}
#TODO: make calendar view "roll" so that sunday is always on the left
#TODO: do something sane when events overlap
#TODO: specify working day (hide unused hours)
#TODO: tomboy integration
#TODO: implement month view
#TODO: remove the selection marquee, replace with a 'temporary event'
# clicking creates an event with a default duration
# clicking and dragging creates an event over the specified area (min 15 min)
#TODO: exceptions to recurrences
#TODO: mousewheel scrolling / zooming
class SelectRecurrence(Command):
def __init__(self, app, text):
self.app = app
self.selected = app.info.selected
self.old = app.info.selection_recurrence
if text:
self.new = parser.parse(text)
else:
self.new = None
self.do()
def do(self):
self.app.info.selection_recurrence = self.new
self.app.info.selected = None
def undo(self):
self.app.info.selected = self.selected
self.app.info.selection_recurrence = self.old
class SetEventRecurrence(Command):
def __init__(self, app, text):
self.app = app
self.selected = self.app.info.selected
self.event = self.app.weekview.timed.get_occurence_event(self.selected)
self.old = self.event.recurrence
self.new = parser.parse(text)
self.do()
def do(self):
self.event.recurrence = self.new
def undo(self):
self.event.recurrence = self.old
class NewEvent(MenuCommand):
label = _("New Event")
tooltip = _("Add a new event")
stockid = gtk.STOCK_ADD
@classmethod
def can_do(cls, app):
return app.info.selection_recurrence != None
def configure(self):
self.selection = self.app.info.selection_recurrence
self.event = Event(self.selection, "New Event")
def do(self):
self.app.model.add_event(self.event)
self.app.info.selection_recurrence = None
self.app.weekview.queue_draw()
return True
def undo(self):
self.app.model.del_event(self.event)
self.app.info.selection_recurrence = self.selection
self.app.info.selected = None
return True
class DelEvent(MenuCommand):
label = _("Delete Event")
tooltip = _("Delete selected events")
stockid = gtk.STOCK_DELETE
@classmethod
def can_do(cls, app):
return app.info.selected != None
def configure(self):
self.selected = self.app.info.selected
self.event = self.app.weekview.timed.occurrences[self.selected][0]
def do(self):
self.app.model.del_event(self.event)
self.app.weekview.timed.select_occurrence(None)
return True
def undo(self):
self.app.model.add_event(self.event)
self.app.weekview.timed.select_occurrence(self.selected)
return True
class GoToToday(MenuCommand):
label = _("Today")
tooltip = _("Scroll to current day")
@classmethod
def can_do(cls, app):
return True
def configure(self):
self.today = datetime.datetime.today().toordinal()
self.date = self.app.info.date
def do(self):
self.app.info.date = self.today
def undo(self):
self.ap.schedule.date = self.date
class GoToSelected(MenuCommand):
label = _("Selected")
tooltip = _("Scroll to the currently-selected item")
@classmethod
def can_do(cls, app):
return bool(app.info.selected)
def configure(self):
self.selected = self.app.info.selected
self.date = self.app.info.date
def do(self):
self.app.info.date = self.selected[1]
def undo(self):
self.app.info.date = self.date
class ZoomIn(MenuCommand):
label = _("Zoom In")
stockid = gtk.STOCK_ZOOM_IN
undoable = False
def do(self):
self.app.weekview.zoom_in()
class ZoomOut(MenuCommand):
label = _("Zoom Out")
stockid = gtk.STOCK_ZOOM_OUT
undoable = False
def do(self):
self.app.weekview.zoom_out()
class SwitchViews(MenuCommand):
label = _("Month View")
undoable = False
def do(self):
self.app.switch_views()
class App(object):
ui = """
<ui>
<toolbar name="upperToolbar">
<toolitem action="Undo"/>
<toolitem action="Redo"/>
<separator />
<toolitem action="NewEvent"/>
<toolitem action="DelEvent"/>
<separator />
<toolitem action="Back"/>
<toolitem action="Forward"/>
<toolitem action="GoToToday"/>
<toolitem action="GoToSelected"/>
<separator />
<toolitem action="SwitchViews"/>
</toolbar>
<toolbar name="lowerToolbar">
<toolitem action="ZoomIn"/>
<toolitem action="ZoomOut"/>
</toolbar>
</ui>"""
def __init__(self):
self.undo = UndoStack()
self.history = UndoStack(
gtk.Action("Back", None, None, gtk.STOCK_GO_BACK),
gtk.Action("Forward", None, None, gtk.STOCK_GO_FORWARD))
w = gtk.Window()
w.connect("destroy", gtk.main_quit)
vbox = gtk.VBox()
self.model = Schedule("schedule.csv")
self.info = CalendarInfo(self.model)
self.weekview = WeekView(self.info, self.undo, self.history)
self.monthview = MonthView(self.info, self.undo, self.history)
uiman = gtk.UIManager ()
actiongroup = MenuCommand.create_action_group(self)
actiongroup.add_action(self.undo.undo_action)
actiongroup.add_action(self.undo.redo_action)
actiongroup.add_action(self.history.undo_action)
actiongroup.add_action(self.history.redo_action)
self.undo.undo_action.connect("activate", self.update_actions)
self.undo.redo_action.connect("activate", self.update_actions)
uiman.insert_action_group(actiongroup)
uiman.add_ui_from_string(self.ui)
toolbar = uiman.get_widget("/upperToolbar")
vbox.pack_start (toolbar, False, False)
vbox.pack_start(self.weekview, True, True)
vbox.pack_start(self.monthview, True, True)
toolbar = uiman.get_widget("/lowerToolbar")
self.selection_buffer = gtk.TextBuffer()
self.selection_buffer.create_tag("error",
underline=pango.UNDERLINE_SINGLE)
self.selection_entry = gtk.TextView(self.selection_buffer)
self.pack_toolbar_widget(toolbar, self.selection_entry)
vbox.pack_end (toolbar, False, False)
w.add(vbox)
w.show()
vbox.show()
self.monthview.hide()
self.window = w
self.weekview.connect("notify::selected", self.update_actions)
self.info.connect("selection-recurrence-changed", self.update_actions)
self.selection_entry.connect("key-press-event", self.selection_entry_key_press_cb)
def pack_toolbar_widget(self, toolbar, widget):
toolitem = gtk.ToolItem()
toolitem.add(widget)
toolitem.set_expand(True)
widget.show()
toolitem.show()
toolbar.add(toolitem)
dont_update_entry = False
def selection_entry_key_press_cb(self, entry, event):
if event.keyval == gtk.keysyms.Return:
self._parse_text()
return True
return False
def _parse_text(self):
self.dont_update_entry = True
self.dirty = False
b = self.selection_buffer
text = b.get_text(b.get_start_iter(), b.get_end_iter())
try:
if not (self.info.selected is None):
self.undo.commit(SetEventRecurrence(self, text))
else:
self.undo.commit(SelectRecurrence(self, text))
b.remove_all_tags(b.get_start_iter(), b.get_end_iter())
except parser.ParseError, e:
self.selection_entry_error(e.position)
self.dont_update_entry = False
return False
def selection_entry_error(self, pos=None):
b = self.selection_buffer
if not pos is None:
b.remove_all_tags(b.get_start_iter(), b.get_end_iter())
b.apply_tag_by_name("error",
b.get_iter_at_offset(pos),
b.get_end_iter())
def update_actions(self, *unused):
MenuCommand.update_actions(self)
if self.dont_update_entry:
return
if not (self.info.selection_recurrence is None):
text = self.info.selection_recurrence.toEnglish()
elif not (self.info.selected is None):
text = self.weekview.timed.get_occurence_event(
self.info.selected).recurrence.toEnglish()
else:
text = ""
self.selection_buffer.set_text(text)
def run(self):
self.load()
gtk.main()
self.save()
gobject.timeout_add(60000, self.save)
def quit(self):
self.window.destroy()
gtk.main_quit()
path = os.path.expanduser("~/.calendar_data")
def load(self):
self.model.load(self.path)
def save(self):
self.model.save(self.path)
return True
def switch_views(self):
if self.weekview.props.visible:
self.weekview.hide()
self.monthview.show()
else:
self.weekview.show()
self.monthview.hide()
def do_command(self, unused_action, command):
cmd = command(self)
cmd.configure()
if cmd.do():
self.undo.commit(cmd)
if __name__ == "__main__":
App().run()