[clocks] Alarms ics reader/writer



commit df8e8994e43b7b3525034ba2b47930353018b644
Author: Eslam Mostafa <cseslam gmail com>
Date:   Thu Jul 19 17:41:46 2012 +0200

    Alarms ics reader/writer
    
    fix a bug in alarms
    
    Alarms ics and UI impelmentation
    
    remove the ics file
    
    Signed-off-by: Seif Lotfy <seif lotfy com>

 alarm.py          |   45 ++++++++++++++++++++++++---------------
 alarms_handler.py |   39 ++++++++++++++++++++++++++++++++++
 clocks.py         |   48 +++++++++++++++++++++++++++++++++++++++--
 widgets.py        |   60 ++++++++++++++++++++++++++++++++++++++---------------
 4 files changed, 155 insertions(+), 37 deletions(-)
---
diff --git a/alarm.py b/alarm.py
index cb32bde..2366a64 100644
--- a/alarm.py
+++ b/alarm.py
@@ -1,32 +1,43 @@
-#WEEKDAYS:
-day1 = "day1"
-day2 = "day2"
-day3 = "day3"
-day4 = "day4"
-day5 = "day5"
-day6 = "day6"
-day7 = "day7"
+import datetime, vobject, time
 
 class AlarmItem:
     def __init__(self, name, time, repeat):
-        self.n = name
-        self.t = time
-        self.r = repeat
+        self.name = name
+        self.time = time
+        self.repeat = repeat
 
     def set_alarm_time(self, time):
-        self.t = time 
+        self.time = time 
         
     def get_alarm_time(self):
-        return self.t
+        return self.time
         
     def set_alarm_name(self, name):
-        self.n = name
+        self.name = name
     
     def get_alarm_name(self):
-        return self.n
+        return self.name
         
     def set_alarm_repeat(self, repeat):
-        self.r = repeat
+        self.repeat = repeat
 
     def get_alarm_repeat(self):
-	return self.r
+        return self.repeat
+    
+    def get_vobject(self):                
+        alarm = vobject.newFromBehavior('vevent')            
+        alarm.add('title').value = self.name        
+        alarm.add('dtstart').value = datetime.datetime.utcnow()        
+        t =  datetime.datetime.utcfromtimestamp(self.time)
+        alarm.add('dtend').value = t
+        alarm.add('rrule').value = 'FREQ=WEEKLY;BYDAY=%s' % ','.join(self.repeat)
+        #alarm.add('enddate').value =       
+        #alarm.add('repeat').value = '4' #self.repeat
+        #alarm.add('duration').value = '15M'        
+        #date = datetime.date(datetime.date.today())
+        #print date
+        #alarm.add('trigger')#.value = datetime.datetime.utcnow() #fromtimestamp(self.time)
+        #datetime.datetime.combine(datetime.date.today(), self.time) #Convert self.time to datetime
+        alarm.add('action').value = 'audio'
+        alarm.add('attach').value = '/usr/share/sounds/gnome/default/alerts/glass.ogg'
+        return alarm        
diff --git a/alarms_handler.py b/alarms_handler.py
new file mode 100644
index 0000000..677a0bb
--- /dev/null
+++ b/alarms_handler.py
@@ -0,0 +1,39 @@
+import vobject, os
+
+class AlarmsHandler():
+    def __init__(self):
+        self.ics_file = 'alarms.ics'
+        
+    def add_alarm(self, vobj):
+        ics = open(self.ics_file, 'r+')        
+        content = ics.read()        
+        ics.seek(0)
+        vcal = vobject.readOne(content)                  
+        vcal.add(vobj)        
+        ics.write(vcal.serialize())      
+        ics.close()
+                
+    def load_alarms(self):                
+        alarms = [] 
+        if os.path.exists(self.ics_file):
+            ics = open(self.ics_file, 'r')        
+            ics.seek(0)            
+            vcal = vobject.readOne(ics.read())          
+            for item in vcal.components():            
+                alarms.append(item)                    
+            ics.close()                           
+        else:
+            self.generate_ics_file()
+        return alarms
+        
+    def generate_ics_file(self):
+        vcal = vobject.iCalendar()
+        ics = open('alarms.ics', 'w')      
+        ics.write(vcal.serialize())
+        ics.close()  
+        
+        
+    def delete_alarm(self, smth_special_about_alarm):
+        ics = open('alarms.ics', 'r+')
+        data = ics.read()
+        ics.close()
diff --git a/clocks.py b/clocks.py
index 00569bf..81d1746 100644
--- a/clocks.py
+++ b/clocks.py
@@ -18,10 +18,11 @@
  Author: Seif Lotfy <seif lotfy collabora co uk>
 """
 
-from gi.repository import Gtk, GObject, Gio, Gdk, Gst, Notify
+from gi.repository import Gtk, GObject, Gio, Gdk, Gst, Notify, cairo
 from gi.repository.GdkPixbuf import Pixbuf
 
-from widgets import NewWorldClockDialog, DigitalClock, NewAlarmDialog
+from widgets import NewWorldClockDialog, DigitalClock, NewAlarmDialog, AlarmWidget
+from alarms_handler import AlarmsHandler
 from storage import worldclockstorage
 
 from datetime import datetime, timedelta
@@ -163,10 +164,51 @@ class World (Clock):
 class Alarm (Clock):
     def __init__ (self):
         Clock.__init__ (self, "Alarm", True)
+        
+        self.liststore = liststore = Gtk.ListStore(Pixbuf, str, GObject.TYPE_PYOBJECT)
+        self.iconview = iconview = Gtk.IconView.new()
+        
+        iconview.set_model(liststore)
+        iconview.set_spacing(3)
+        iconview.set_pixbuf_column(0)        
+        iconview.get_style_context ().add_class ('grey-bg')
+
+        renderer_text = Gtk.CellRendererText()
+        renderer_text.set_alignment (0.5, 0.5)                
+        iconview.pack_start(renderer_text, True)                
+        iconview.add_attribute(renderer_text, "markup", 1)        
+
+        scrolledwindow = Gtk.ScrolledWindow()
+        scrolledwindow.add(iconview)
+        self.add(scrolledwindow)
+        
+        self.alarms = []
+        self.load_alarms()
+        self.show_all()
+        
+    def load_alarms(self):
+        ah = AlarmsHandler()
+        alarms = ah.load_alarms()        
+        for alarm in alarms:         
+            name = alarm.title.value        
+            trigger = alarm.dtend.value                      
+            d = AlarmWidget(trigger)                        
+            view_iter = self.liststore.append([d.drawing.pixbuf, "<b>" + name + "</b>", d])
+            d.set_iter(self.liststore, view_iter)          
+        self.show_all()
+      
+    def add_alarm(self, alarm):
+        ah = AlarmsHandler()
+        ah.add_alarm(alarm.get_vobject())
+        d = AlarmWidget(datetime.utcfromtimestamp(alarm.time))            
+        view_iter = self.liststore.append([d.drawing.pixbuf, "<b>" + alarm.get_alarm_name() + "</b>", d])
+        d.set_iter(self.liststore, view_iter)
+        self.show_all()
 
     def open_new_dialog(self):
         parent = self.get_parent ().get_parent ().get_parent ()
-        window = NewAlarmDialog (parent)
+        window = NewAlarmDialog (parent)        
+        window.connect("add-alarm", lambda w, l: self.add_alarm(l))
         window.show_all ()
 
 class Stopwatch (Clock):
diff --git a/widgets.py b/widgets.py
index cea29cc..8eb74ea 100644
--- a/widgets.py
+++ b/widgets.py
@@ -88,7 +88,7 @@ class NewWorldClockDialog (Gtk.Dialog):
         return self.location
 
 class DigitalClock ():
-    def __init__(self, location):
+    def __init__(self, location):              
         self.location = location.location
         self.id = location.id
         self.timezone = self.location.get_timezone()
@@ -100,7 +100,7 @@ class DigitalClock ():
         self.list_store = None
 
         self.drawing = DigitalClockDrawing ()
-        self.standalone = DigitalClockStandalone (self.location)
+        self.standalone = DigitalClockStandalone (self.location)        
         self.update ()
         GObject.timeout_add(1000, self.update)
 
@@ -157,6 +157,27 @@ class DigitalClock ():
     def get_standalone_widget (self):
         return self.standalone
 
+class AlarmWidget():
+    def __init__(self, time_given):              
+        t = time_given.strftime("%I:%M %p")        
+        self.drawing = DigitalClockDrawing ()
+        isDay = self.get_is_day(t)
+        if isDay == True:
+            img = "data/cities/day.png"
+        else:
+            img = "data/cities/night.png"
+        self.drawing.render(t, img, isDay)
+        
+    def get_is_day(self, t):
+        if t[6:8] == 'AM':
+          return True
+        else:
+          return False
+        
+    def set_iter (self, list_store, view_iter):
+        self.view_iter = view_iter
+        self.list_store = list_store
+      
 class DigitalClockStandalone (Gtk.VBox):
     def __init__ (self, location):
         Gtk.VBox.__init__ (self, False)
@@ -240,9 +261,7 @@ class DigitalClockStandalone (Gtk.VBox):
             self.sunrise_time_label.set_markup (sunrise_markup)
             self.sunset_time_label.set_markup (sunset_markup)
         self.systemClockFormat = systemClockFormat
-
-
-
+        
 class DigitalClockDrawing (Gtk.DrawingArea):
     width = 160
     height = 160
@@ -256,6 +275,7 @@ class DigitalClockDrawing (Gtk.DrawingArea):
         self.pixbuf = None
         self.surface = None
         self.show_all()
+        
 
     def render(self, text, img, isDay):
         print "updating"
@@ -302,6 +322,9 @@ class DigitalClockDrawing (Gtk.DrawingArea):
         return self.pixbuf
 
 class NewAlarmDialog (Gtk.Dialog):
+    
+    __gsignals__ = {'add-alarm': (GObject.SignalFlags.RUN_LAST,
+                    None, (GObject.TYPE_PYOBJECT,))}
 
     def __init__(self, parent):
         Gtk.Dialog.__init__(self, "New Alarm", parent)
@@ -309,6 +332,7 @@ class NewAlarmDialog (Gtk.Dialog):
         self.parent = parent
         self.set_transient_for(parent)
         self.set_modal(True)
+        self.repeat_days = []
 
         table1 = Gtk.Table(4, 5, False) 
         table1.set_row_spacings(9)
@@ -350,7 +374,7 @@ class NewAlarmDialog (Gtk.Dialog):
         
         table1.attach(name, 0, 1, 1, 2)
         table1.attach(repeat, 0, 1, 2, 3)
-        table1.attach(sound, 0, 1, 3, 4)
+        #table1.attach(sound, 0, 1, 3, 4)
         
         self.entry = entry = Gtk.Entry ()
         entry.set_text("New Alarm")
@@ -385,39 +409,41 @@ class NewAlarmDialog (Gtk.Dialog):
         table1.attach(box, 1, 4, 2, 3) 
        
         soundbox = Gtk.ComboBox ()
-        table1.attach(soundbox, 1, 3, 3, 4)
+        #table1.attach(soundbox, 1, 3, 3, 4)
       
     def on_response(self, widget, id):
         if id == 0:
             self.destroy ()
         if id == 1:
             name = self.entry.get_text()  #Perfect
-            time = self.hourselect.get_value_as_int() * 60 * 60 +     self.minuteselect.get_value_as_int() * 60
-            repeat = True
-            new_alarm = AlarmItem(name, time, repeat)
+            time = self.hourselect.get_value_as_int() * 60 * 60 + self.minuteselect.get_value_as_int() * 60            
+            repeat = self.repeat_days
+            new_alarm = AlarmItem(name, time, repeat)            
+            self.emit('add-alarm', new_alarm)
+            self.destroy ()
         else:
             pass
 
     def on_d1_clicked(self, buttond1):
-        d1 = day1
+        self.repeat_days.append('SU')
                   
     def on_d2_clicked(self, buttond2):
-        d2 = day2
+        self.repeat_days.append('MO')
     
     def on_d3_clicked(self, buttond3):
-        d3 = day3
+        self.repeat_days.append('TU')
     
     def on_d4_clicked(self, buttond4):
-        pass
+        self.repeat_days.append('WE')
     
     def on_d5_clicked(self, buttond5):
-        pass
+        self.repeat_days.append('TH')
     
     def on_d6_clicked(self, buttond6):
-        pass
+        self.repeat_days.append('FR')
     
     def on_d7_clicked(self, buttond7):
-        pass
+        self.repeat_days.append('SA')
 
 """
 if text.startswith("0"):



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]