summaryrefslogtreecommitdiff
path: root/timer-applet/src/timer-applet
blob: cd550fbb34d6bb550272819b2e3a29f73b597d6e (plain)
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
#!/usr/bin/env python
# Copyright (C) 2010 Kenny Meyer <knny.myer@gmail.com>
# Copyright (C) 2008 Jimmy Do <jimmydo@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 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 General Public License for more details.
#
# You should have received a copy of the GNU 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

from os import path
import gettext
import locale
import sys
import gtk
import mateapplet
from timerapplet import config
from timerapplet.controllers import GlobalController, TimerApplet, TimerService, TimerManagerService
from timerapplet.core import AppletMateConfWrapper, Timer

from dbus.mainloop.glib import DBusGMainLoop
DBusGMainLoop(set_as_default=True)

DBUS_BUS_NAME = 'net.launchpad.timerapplet.TimerApplet'
DBUS_OBJ_NAMESPACE = '/net/launchpad/timerapplet/TimerApplet'

gettext.bindtextdomain(config.GETTEXT_PACKAGE, config.LOCALE_DIR)
gettext.bind_textdomain_codeset(config.GETTEXT_PACKAGE, 'UTF-8')
gettext.textdomain(config.GETTEXT_PACKAGE)
locale.bindtextdomain(config.GETTEXT_PACKAGE, config.LOCALE_DIR)
locale.bind_textdomain_codeset(config.GETTEXT_PACKAGE, 'UTF-8')
locale.textdomain(config.GETTEXT_PACKAGE)

global_controller = GlobalController()
timer_manager_obj_path = path.join(DBUS_OBJ_NAMESPACE, 'TimerManager')
print 'Timer Manager D-Bus object path: %s' % timer_manager_obj_path

timer_manager = None
try:
    timer_manager = TimerManagerService(DBUS_BUS_NAME, timer_manager_obj_path)
except Exception, err:
    print 'ERROR: Could not start TimerManagerService. D-Bus support will not be available. Error message: %s' % err

def check_dependencies():
    # Check for optional dependencies
    try:
        import dbus # >= 0.80
    except ImportError, err:
        print 'Missing optional dependency: %s' % err

    # Check for required dependencies
    try:
        import gobject # >= 2.12
        import gtk # >= 2.10, includes pango
        import gtk.glade # >= 2.10
        import mateconf # >= 2.18
        import mate # >= 2.18, includes matecomponent.ui
        import mateapplet # >= 2.18, included in python-mate2-desktop
        import pynotify # >= 0.1.1
    except ImportError, err:
        dialog = gtk.MessageDialog(type=gtk.MESSAGE_ERROR,
                                    buttons=gtk.BUTTONS_CLOSE,
                                    message_format='%s\n\nPlease install required dependencies.' % err)
        dialog.run()
        dialog.destroy()
        sys.exit(1)

def get_timer_id(mateconf_wrapper):
    path_components = mateconf_wrapper.get_base_path().split('/')
    
    # Use the second component from the end, which should usually be 'applet_*',
    # where '*' is some integer assigned by the system.
    # It could also be 'timer-applet' if we're running in standalone mode.
    # D-Bus doesn't like hyphens in object paths, so we have to replace them
    # with underscores.
    return path_components[-2].replace('-', '_')

def applet_factory(applet, iid):
    check_dependencies()

    timer = Timer()
    mateconf_wrapper = AppletMateConfWrapper(applet,
                                       '/schemas/apps/timer-applet/prefs',
                                       '/apps/timer-applet/prefs')
    timer_id = get_timer_id(mateconf_wrapper)
    print 'Timer ID: %s' % timer_id
    
    if timer_manager is not None:
        timer_manager.register_timer_id(timer_id)
        applet.connect('destroy', lambda sender: timer_manager.unregister_timer_id(timer_id))

    TimerApplet(global_controller.get_presets_store(),
                global_controller.get_manage_presets_dialog(),
                applet,
                timer,
                mateconf_wrapper)
    
    timer_obj_path = path.join(DBUS_OBJ_NAMESPACE, 'Timers', timer_id)
    print 'Timer D-Bus object path: %s' % timer_obj_path
    
    try:
        TimerService(DBUS_BUS_NAME, timer_obj_path, timer)
    except Exception, err:
        print 'ERROR: Could not start TimerService. D-Bus support will not be available. Error message: %s' % err
    
    return True

if __name__ == '__main__':
    windowed_mode = (len(sys.argv) > 1 and sys.argv[1] == '-w')
    
    if windowed_mode:
        win = gtk.Window()
        win.set_title('Timer Applet')
        applet = mateapplet.Applet()
        applet_factory(applet, None)
        applet.reparent(win)
        
        applet.connect('destroy', gtk.main_quit)
        win.show()
        
        gtk.main()
    else:
        mateapplet.matecomponent_factory(
            'OAFIID:TimerApplet_Factory',
            mateapplet.Applet.__gtype__,
            config.PACKAGE,
            config.VERSION,
            applet_factory)