~loneowais/gmailwatcher/quickly_trunk

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
#!/usr/bin/env python

import gi, sys
from gi.repository import Gtk, GObject, Gio, Unity, Notify
import dbus, dbus.service, dbus.glib, gettext
from dbus.mainloop.glib import DBusGMainLoop

from gettext import gettext as _
gettext.textdomain('gmailwatcher')
Notify.init('gmailwatcher')

from lib import preferences


class MainApp():
    def __init__(self):
        self.builder = Gtk.Builder()
        self.builder.add_from_file('data/ui/MainApp.glade')
        self.builder.connect_signals(self)

        self.main_window = self.builder.get_object('mainwindow')
        self.prefs = preferences.new_preferences_dialog()
        
        self.launcher_entry = Unity.LauncherEntry.get_for_desktop_file('gmailwatcher.desktop')
 
       
    def start(self):
        self.main_window.show_all()


    def new_mail(self,account,label,notifications):
        notif = Notify.Notification.new('gmail',account,"You've got %s new emails in %s"%(len(notifications),label))
        notif.show()
        
    #Gtk Event Callbacks
    def on_preferences_clicked(self,widget,data=None):
        self.prefs.show()

    def on_quit_clicked(self,widget,data=None):
        self.main_window.destroy()

    def on_mainwindow_delete_event(self,widget,data=None):
        self.main_window.hide()
        return True

    def on_mainwindow_destroy(self,widget,data=None):
        service.remove_from_connection()
        loop.quit()



class GmailWatcherService(dbus.service.Object):
    def __init__(self, app):
        self.app = app
        bus_name = dbus.service.BusName('org.owaislone.GmailWatcher', bus = dbus.SessionBus())
        dbus.service.Object.__init__(self, bus_name, '/org/owaislone/GmailWatcher')

    @dbus.service.method(dbus_interface='org.owaislone.GmailWatcher')
    def show_window(self):
        self.app.main_window.show_all()
        self.app.main_window.present()
    
    @dbus.service.method(dbus_interface='org.owaislone.GmailWatcher')
    def new_mail(self,account,label,notifications):
        print account, label, len(notifications)
        print self.app.new_mail(account,label,notifications)
    
    @dbus.service.method(dbus_interface='org.owaislone.GmailWatcher')
    def register_worker(self,mail):
        return ["loneowais@gmail.com","XXXX"]
        
    @dbus.service.method(dbus_interface='org.owaislone.GmailWatcher')
    def continue_watching(self,username):
        return True

if __name__ == '__main__':

    DBusGMainLoop(set_as_default=True)
    bus = dbus.SessionBus()
    if bus.request_name("org.owaislone.GmailWatcher") != dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER:
        print _("application already running")
        gmailwatcher_dbus = bus.get_object("org.owaislone.GmailWatcher", "/org/owaislone/GmailWatcher")
        method = gmailwatcher_dbus.get_dbus_method("show_window")
        method()
    else:
        loop = GObject.MainLoop()
        mainapp = MainApp()
        service = GmailWatcherService(app=mainapp)
        mainapp.start()
        loop.run()