~ubuntu-branches/ubuntu/lucid/docky/lucid-proposed

« back to all changes in this revision

Viewing changes to scripts/gtg_menus.py

  • Committer: Bazaar Package Importer
  • Author(s): Christopher James Halse Rogers
  • Date: 2010-02-17 15:10:07 UTC
  • Revision ID: james.westby@ubuntu.com-20100217151007-msxpd0lsj300ndde
Tags: upstream-2.0.0
ImportĀ upstreamĀ versionĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
#  
 
4
#  Copyright (C) 2010 Rico Tzschichholz
 
5
 
6
#  This program is free software: you can redistribute it and/or modify
 
7
#  it under the terms of the GNU General Public License as published by
 
8
#  the Free Software Foundation, either version 3 of the License, or
 
9
#  (at your option) any later version.
 
10
 
11
#  This program is distributed in the hope that it will be useful,
 
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
#  GNU General Public License for more details.
 
15
 
16
#  You should have received a copy of the GNU General Public License
 
17
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
#
 
19
 
 
20
#  Notes:
 
21
#  * Getting Things GNOME! 0.2 or higher is required for dbus support
 
22
 
 
23
import atexit
 
24
import gobject
 
25
import glib
 
26
import dbus
 
27
import dbus.glib
 
28
import sys
 
29
import os
 
30
 
 
31
try:
 
32
        from docky.docky import DockyItem, DockySink
 
33
        from signal import signal, SIGTERM
 
34
        from sys import exit
 
35
except ImportError, e:
 
36
        exit()
 
37
 
 
38
gtgbus = "org.GTG"
 
39
gtgpath = "/org/GTG"
 
40
gtgiface = "org.GTG"
 
41
 
 
42
class DockyGTGItem(DockyItem):
 
43
        def __init__(self, path):
 
44
                DockyItem.__init__(self, path)
 
45
                self.gtg = None
 
46
                
 
47
                self.bus.add_signal_receiver(self.name_owner_changed_cb,
 
48
                                             dbus_interface='org.freedesktop.DBus',
 
49
                                             signal_name='NameOwnerChanged')
 
50
                                             
 
51
                obj = self.bus.get_object ("org.freedesktop.DBus", "/org/freedesktop/DBus")
 
52
                self.bus_interface = dbus.Interface(obj, "org.freedesktop.DBus")
 
53
                
 
54
                self.bus_interface.ListNames (reply_handler=self.list_names_handler, error_handler=self.list_names_error_handler)
 
55
        
 
56
        def list_names_handler(self, names):
 
57
                if gtgbus in names:
 
58
                        self.init_gtg_objects()
 
59
                        self.set_menu_buttons()
 
60
 
 
61
        def list_names_error_handler(self, error):
 
62
                print "error getting bus names - %s" % str(error)
 
63
        
 
64
        def name_owner_changed_cb(self, name, old_owner, new_owner):
 
65
                if name == gtgbus:
 
66
                        if new_owner:
 
67
                                self.init_gtg_objects()
 
68
                        else:
 
69
                                self.gtg = None
 
70
                        self.set_menu_buttons()
 
71
        
 
72
        def init_gtg_objects(self):
 
73
                obj = self.bus.get_object(gtgbus, gtgpath)
 
74
                self.gtg = dbus.Interface(obj, gtgiface)
 
75
                
 
76
        def clear_menu_buttons(self):
 
77
                for k, v in self.id_map.iteritems():
 
78
                        try:
 
79
                                self.iface.RemoveItem(k)
 
80
                        except:
 
81
                                break;  
 
82
        
 
83
        def set_menu_buttons(self):
 
84
                self.clear_menu_buttons()
 
85
                
 
86
                if not self.gtg:
 
87
                        return
 
88
 
 
89
                self.add_menu_item ("New Task", "list-add", "NewTask", "Task Controls")
 
90
                self.add_menu_item ("Open Tasks List", "gtg", "OpenWindow", "Task Controls")
 
91
 
 
92
        def menu_pressed(self, menu_id):
 
93
                if not menu_id in self.id_map:
 
94
                        return  
 
95
                
 
96
                menu_id = self.id_map[menu_id]
 
97
                
 
98
                if menu_id == "NewTask":
 
99
                        self.gtg.open_new_task()
 
100
                elif menu_id == "OpenWindow":
 
101
                        self.gtg.show_task_browser()
 
102
                
 
103
        def add_menu_item(self, name, icon, ident, title):
 
104
                menu_id = self.iface.AddMenuItem(name, icon, title)
 
105
                self.id_map[menu_id] = ident
 
106
                
 
107
                
 
108
class DockyGTGSink(DockySink):
 
109
        def item_path_found(self, pathtoitem, item):
 
110
                if item.GetOwnsDesktopFile() and item.GetDesktopFile().endswith ("gtg.desktop"):
 
111
                        self.items[pathtoitem] = DockyGTGItem(pathtoitem)
 
112
 
 
113
 
 
114
dockysink = DockyGTGSink()
 
115
 
 
116
def cleanup ():
 
117
        dockysink.dispose ()
 
118
 
 
119
if __name__ == "__main__":
 
120
        mainloop = gobject.MainLoop(is_running=True)
 
121
 
 
122
        atexit.register (cleanup)
 
123
        signal(SIGTERM, lambda signum, stack_frame: exit(1))
 
124
 
 
125
        mainloop.run()