Adding a new icon and bumping to 0.8.2
[theonering] / src / theonering.py
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 = 5
38
39
40 def run_theonering(persist):
41         linux_utils.set_process_name(constants.__app_name__)
42
43         try:
44                 os.makedirs(constants._data_path_)
45         except OSError, e:
46                 if e.errno != 17:
47                         raise
48
49         @gobject_utils.async
50         def on_quit():
51                 manager.quit()
52                 mainloop.quit()
53
54         def timeout_cb():
55                 if len(manager._connections) == 0:
56                         logging.info('No connection received - quitting')
57                         on_quit()
58                 return False
59
60         if persist:
61                 shutdown_callback = None
62         else:
63                 gobject_utils.timeout_add_seconds(IDLE_TIMEOUT, timeout_cb)
64                 shutdown_callback = on_quit
65
66         signal.signal(signal.SIGTERM, lambda: on_quit)
67
68         try:
69                 manager = connection_manager.TheOneRingConnectionManager(shutdown_func=shutdown_callback)
70         except dbus.exceptions.NameExistsException:
71                 logging.warning('Failed to acquire bus name, connection manager already running?')
72                 sys.exit(1)
73
74         mainloop = gobject.MainLoop(is_running=True)
75
76         while mainloop.is_running():
77                 try:
78                         mainloop.run()
79                 except KeyboardInterrupt:
80                         quit()
81
82
83 def main(logToFile):
84         try:
85                 os.makedirs(constants._data_path_)
86         except OSError, e:
87                 if e.errno != 17:
88                         raise
89
90         telepathy_utils.debug_divert_messages(os.getenv('THEONERING_LOGFILE'))
91         if logToFile:
92                 logging.basicConfig(
93                         level=logging.DEBUG,
94                         filename=constants._user_logpath_,
95                         format='(%(asctime)s) %(levelname)s:%(name)s:%(message)s',
96                         datefmt='%H:%M:%S',
97                 )
98         else:
99                 logging.basicConfig(
100                         level=logging.DEBUG,
101                         format='(%(asctime)s) %(levelname)s:%(name)s:%(message)s',
102                         datefmt='%H:%M:%S',
103                 )
104         logging.info("telepathy-theonering %s-%s" % (constants.__version__, constants.__build__))
105         logging.debug("OS: %s" % (os.uname()[0], ))
106         logging.debug("Kernel: %s (%s) for %s" % os.uname()[2:])
107         logging.debug("Hostname: %s" % os.uname()[1])
108
109         persist = 'THEONERING_PERSIST' in os.environ
110
111         try:
112                 run_theonering(persist)
113         finally:
114                 logging.shutdown()
115
116
117 if __name__ == "__main__":
118         main(False)