source: deluge/ui/gtkui/gtkui.py@ 2ac545d

2.0.x develop extjs4-port
Last change on this file since 2ac545d was 2ac545d, checked in by Andrew Resch <andrewresch@gmail.com>, 16 years ago

Clean-up signal handling since twisted.reactor handles it now

  • Property mode set to 100644
File size: 7.9 KB
Line 
1#
2# gtkui.py
3#
4# Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com>
5#
6# Deluge is free software.
7#
8# You may redistribute it and/or modify it under the terms of the
9# GNU General Public License, as published by the Free Software
10# Foundation; either version 3 of the License, or (at your option)
11# any later version.
12#
13# deluge is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16# See the GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with deluge. If not, write to:
20# The Free Software Foundation, Inc.,
21# 51 Franklin Street, Fifth Floor
22# Boston, MA 02110-1301, USA.
23#
24
25
26from deluge.log import LOG as log
27
28# Install the twisted reactor
29from twisted.internet import gtk2reactor
30reactor = gtk2reactor.install()
31
32import gobject
33import gettext
34import locale
35import pkg_resources
36import gtk, gtk.glade
37
38import deluge.component as component
39from deluge.ui.client import client
40from mainwindow import MainWindow
41from menubar import MenuBar
42from toolbar import ToolBar
43from torrentview import TorrentView
44from torrentdetails import TorrentDetails
45from sidebar import SideBar
46from filtertreeview import FilterTreeView
47from preferences import Preferences
48from systemtray import SystemTray
49from statusbar import StatusBar
50from connectionmanager import ConnectionManager
51from pluginmanager import PluginManager
52from ipcinterface import IPCInterface
53
54from queuedtorrents import QueuedTorrents
55from addtorrentdialog import AddTorrentDialog
56import deluge.configmanager
57import deluge.common
58
59DEFAULT_PREFS = {
60 "classic_mode": True,
61 "interactive_add": True,
62 "focus_add_dialog": True,
63 "enable_system_tray": True,
64 "close_to_tray": True,
65 "start_in_tray": False,
66 "lock_tray": False,
67 "tray_password": "",
68 "check_new_releases": True,
69 "default_load_path": None,
70 "window_maximized": False,
71 "window_x_pos": 0,
72 "window_y_pos": 0,
73 "window_width": 640,
74 "window_height": 480,
75 "window_pane_position": -1,
76 "tray_download_speed_list" : [5.0, 10.0, 30.0, 80.0, 300.0],
77 "tray_upload_speed_list" : [5.0, 10.0, 30.0, 80.0, 300.0],
78 "connection_limit_list": [50, 100, 200, 300, 500],
79 "enabled_plugins": [],
80 "show_connection_manager_on_start": True,
81 "autoconnect": False,
82 "autoconnect_host_id": None,
83 "autostart_localhost": False,
84 "autoadd_queued": False,
85 "autoadd_enable": False,
86 "autoadd_location": "",
87 "choose_directory_dialog_path": deluge.common.get_default_download_dir(),
88 "show_new_releases": True,
89 "signal_port": 40000,
90 "ntf_tray_blink": True,
91 "ntf_sound": False,
92 "ntf_sound_path": deluge.common.get_default_download_dir(),
93 "ntf_popup": False,
94 "ntf_email": False,
95 "ntf_email_add": "",
96 "ntf_username": "",
97 "ntf_pass": "",
98 "ntf_server": "",
99 "ntf_security": None,
100 "signal_port": 40000,
101 "show_sidebar": True,
102 "show_toolbar": True,
103 "show_statusbar": True,
104 "sidebar_show_zero": False,
105 "sidebar_show_trackers": True,
106 "sidebar_position": 170,
107 "show_rate_in_title": False
108}
109
110class GtkUI:
111 def __init__(self, args):
112
113 # Initialize gettext
114 try:
115 if hasattr(locale, "bindtextdomain"):
116 locale.bindtextdomain("deluge", pkg_resources.resource_filename("deluge", "i18n"))
117 if hasattr(locale, "textdomain"):
118 locale.textdomain("deluge")
119 gettext.bindtextdomain("deluge", pkg_resources.resource_filename("deluge", "i18n"))
120 gettext.textdomain("deluge")
121 gettext.install("deluge", pkg_resources.resource_filename("deluge", "i18n"))
122 gtk.glade.bindtextdomain("deluge", pkg_resources.resource_filename("deluge", "i18n"))
123 gtk.glade.textdomain("deluge")
124 except Exception, e:
125 log.error("Unable to initialize gettext/locale!")
126 log.exception(e)
127 # Setup signals
128 try:
129 import gnome.ui
130 self.gnome_client = gnome.ui.Client()
131 self.gnome_client.connect("die", self.shutdown)
132 except:
133 pass
134
135 # Twisted catches signals to terminate, so just have it call the shutdown
136 # method.
137 reactor.addSystemEventTrigger("after", "shutdown", self.shutdown)
138
139 if deluge.common.windows_check():
140 from win32api import SetConsoleCtrlHandler
141 from win32con import CTRL_CLOSE_EVENT
142 from win32con import CTRL_SHUTDOWN_EVENT
143 def win_handler(ctrl_type):
144 log.debug("ctrl_type: %s", ctrl_type)
145 if ctrl_type == CTRL_CLOSE_EVENT or ctrl_type == CTRL_SHUTDOWN_EVENT:
146 self.shutdown()
147 return 1
148 SetConsoleCtrlHandler(win_handler)
149
150 # Make sure gtkui.conf has at least the defaults set
151 self.config = deluge.configmanager.ConfigManager("gtkui.conf", DEFAULT_PREFS)
152
153 # We need to check on exit if it was started in classic mode to ensure we
154 # shutdown the daemon.
155 self.started_in_classic = self.config["classic_mode"]
156
157 # Start the IPC Interface before anything else.. Just in case we are
158 # already running.
159 self.queuedtorrents = QueuedTorrents()
160 self.ipcinterface = IPCInterface(args)
161
162 # Initialize gdk threading
163 gtk.gdk.threads_init()
164 gobject.threads_init()
165
166 # We make sure that the UI components start once we get a core URI
167 client.set_disconnect_callback(self.__on_disconnect)
168
169 # Initialize various components of the gtkui
170 self.mainwindow = MainWindow()
171 self.menubar = MenuBar()
172 self.toolbar = ToolBar()
173 self.torrentview = TorrentView()
174 self.torrentdetails = TorrentDetails()
175 self.sidebar = SideBar()
176 self.filtertreeview = FilterTreeView()
177 self.preferences = Preferences()
178 self.systemtray = SystemTray()
179 self.statusbar = StatusBar()
180 self.addtorrentdialog = AddTorrentDialog()
181
182 # Initalize the plugins
183 self.plugins = PluginManager()
184
185 # Show the connection manager
186 self.connectionmanager = ConnectionManager()
187
188 reactor.callWhenRunning(self._on_reactor_start)
189
190 # Start the gtk main loop
191 try:
192 gtk.gdk.threads_enter()
193 reactor.run()
194 gtk.gdk.threads_leave()
195 except KeyboardInterrupt:
196 self.shutdown()
197 else:
198 self.shutdown()
199
200 def shutdown(self, *args, **kwargs):
201 log.debug("gtkui shutting down..")
202
203 # Shutdown all components
204 component.shutdown()
205 if self.started_in_classic:
206 try:
207 client.daemon.shutdown()
208 except:
209 pass
210
211 # Make sure the config is saved.
212 self.config.save()
213
214 try:
215 gtk.main_quit()
216 except RuntimeError:
217 pass
218
219 def _on_reactor_start(self):
220 log.debug("_on_reactor_start")
221 if self.config["classic_mode"]:
222 client.start_classic_mode()
223 component.start()
224 return
225
226 # Autoconnect to a host
227 if self.config["autoconnect"]:
228 for host in self.connectionmanager.config["hosts"]:
229 if host[0] == self.config["autoconnect_host_id"]:
230 def on_connect(connector):
231 component.start()
232 client.connect(*host[1:]).addCallback(on_connect)
233
234 if self.config["show_connection_manager_on_start"]:
235 # XXX: We need to call a simulate() here, but this could be a bug in twisted
236 reactor.simulate()
237 self.connectionmanager.show()
238
239
240
241 def __on_disconnect(self):
242 """
243 Called when disconnected from the daemon. We basically just stop all
244 the components here.
245 """
246 component.stop()
Note: See TracBrowser for help on using the repository browser.