Re-arranging for packaging prep
[theonering] / src / telepathy-theonering
1 #!/usr/bin/env python
2
3 """
4 Telepathy-TheOneRing - Telepathy plugin for GoogleVoice
5 Copyright (C) 2009  Ed Page eopage AT byu DOT net
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 """
21
22 import os
23 import sys
24 import signal
25 import logging
26 import gobject
27
28 import dbus.glib
29 import telepathy.utils as telepathy_utils
30
31 import util.linux as linux_utils
32 import util.go_utils as gobject_utils
33 import constants
34 import connection_manager
35
36
37 IDLE_TIMEOUT = 5000
38
39
40 def run_theonering():
41         linux_utils.set_process_name(constants.__app_name__)
42
43         @gobject_utils.async
44         def quit():
45                 manager.quit()
46                 mainloop.quit()
47
48
49         if 'BUTTERFLY_PERSIST' not in os.environ:
50                 def timeout_cb():
51                         if len(manager._connections) == 0:
52                                 logging.info('No connection received - quitting')
53                                 quit()
54                         return False
55                 gobject.timeout_add(IDLE_TIMEOUT, timeout_cb)
56                 shutdown_callback = quit
57         else:
58                 shutdown_callback = None
59
60         signal.signal(signal.SIGTERM, quit)
61
62         try:
63                 manager = connection_manager.TheOneRingConnectionManager(shutdown_func=shutdown_callback)
64         except dbus.exceptions.NameExistsException:
65                 logging.warning('Failed to acquire bus name, connection manager already running?')
66                 sys.exit(1)
67
68         mainloop = gobject.MainLoop(is_running=True)
69
70         while mainloop.is_running():
71                 try:
72                         mainloop.run()
73                 except KeyboardInterrupt:
74                         quit()
75
76
77 if __name__ == '__main__':
78         telepathy_utils.debug_divert_messages(os.getenv('BUTTERFLY_LOGFILE'))
79         logging.basicConfig(level=logging.DEBUG)
80         run_theonering()