Fix #565 wait for the deluged process to start to prevent defunct

processes
This commit is contained in:
Andrew Resch 2008-10-24 07:39:14 +00:00
parent 1aa33421e9
commit dc0a2ce52e
1 changed files with 61 additions and 62 deletions

View File

@ -2,19 +2,19 @@
# connectionmanager.py # connectionmanager.py
# #
# Copyright (C) 2007 Andrew Resch ('andar') <andrewresch@gmail.com> # Copyright (C) 2007 Andrew Resch ('andar') <andrewresch@gmail.com>
# #
# Deluge is free software. # Deluge is free software.
# #
# You may redistribute it and/or modify it under the terms of the # You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software # GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) # Foundation; either version 3 of the License, or (at your option)
# any later version. # any later version.
# #
# deluge is distributed in the hope that it will be useful, # deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details. # See the GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with deluge. If not, write to: # along with deluge. If not, write to:
# The Free Software Foundation, Inc., # The Free Software Foundation, Inc.,
@ -70,9 +70,9 @@ class ConnectionManager(component.Component):
component.Component.__init__(self, "ConnectionManager") component.Component.__init__(self, "ConnectionManager")
# Get the glade file for the connection manager # Get the glade file for the connection manager
self.glade = gtk.glade.XML( self.glade = gtk.glade.XML(
pkg_resources.resource_filename("deluge.ui.gtkui", pkg_resources.resource_filename("deluge.ui.gtkui",
"glade/connection_manager.glade")) "glade/connection_manager.glade"))
self.window = component.get("MainWindow") self.window = component.get("MainWindow")
self.config = ConfigManager("hostlist.conf", DEFAULT_CONFIG) self.config = ConfigManager("hostlist.conf", DEFAULT_CONFIG)
self.gtkui_config = ConfigManager("gtkui.conf") self.gtkui_config = ConfigManager("gtkui.conf")
@ -81,19 +81,19 @@ class ConnectionManager(component.Component):
self.connection_manager.set_transient_for(self.window.window) self.connection_manager.set_transient_for(self.window.window)
self.hostlist = self.glade.get_widget("hostlist") self.hostlist = self.glade.get_widget("hostlist")
self.connection_manager.set_icon(common.get_logo(32)) self.connection_manager.set_icon(common.get_logo(32))
self.glade.get_widget("image1").set_from_pixbuf(common.get_logo(32)) self.glade.get_widget("image1").set_from_pixbuf(common.get_logo(32))
self.liststore = gtk.ListStore(gtk.gdk.Pixbuf, str, int) self.liststore = gtk.ListStore(gtk.gdk.Pixbuf, str, int)
# Holds the online status of hosts # Holds the online status of hosts
self.online_status = {} self.online_status = {}
# Fill in hosts from config file # Fill in hosts from config file
for host in self.config["hosts"]: for host in self.config["hosts"]:
row = self.liststore.append() row = self.liststore.append()
self.liststore.set_value(row, HOSTLIST_COL_URI, host) self.liststore.set_value(row, HOSTLIST_COL_URI, host)
# Setup host list treeview # Setup host list treeview
self.hostlist.set_model(self.liststore) self.hostlist.set_model(self.liststore)
render = gtk.CellRendererPixbuf() render = gtk.CellRendererPixbuf()
@ -103,7 +103,7 @@ class ConnectionManager(component.Component):
render = gtk.CellRendererText() render = gtk.CellRendererText()
column = gtk.TreeViewColumn("Host", render, text=HOSTLIST_COL_URI) column = gtk.TreeViewColumn("Host", render, text=HOSTLIST_COL_URI)
self.hostlist.append_column(column) self.hostlist.append_column(column)
self.glade.signal_autoconnect({ self.glade.signal_autoconnect({
"on_button_addhost_clicked": self.on_button_addhost_clicked, "on_button_addhost_clicked": self.on_button_addhost_clicked,
"on_button_removehost_clicked": self.on_button_removehost_clicked, "on_button_removehost_clicked": self.on_button_removehost_clicked,
@ -115,22 +115,22 @@ class ConnectionManager(component.Component):
"on_chk_autostart_toggled": self.on_chk_autostart_toggled, "on_chk_autostart_toggled": self.on_chk_autostart_toggled,
"on_chk_donotshow_toggled": self.on_chk_donotshow_toggled "on_chk_donotshow_toggled": self.on_chk_donotshow_toggled
}) })
self.connection_manager.connect("delete-event", self.on_delete_event) self.connection_manager.connect("delete-event", self.on_delete_event)
# Connect to the 'changed' event of TreeViewSelection to get selection # Connect to the 'changed' event of TreeViewSelection to get selection
# changes. # changes.
self.hostlist.get_selection().connect("changed", self.hostlist.get_selection().connect("changed",
self.on_selection_changed) self.on_selection_changed)
self.hostlist.connect("row-activated", self._on_row_activated) self.hostlist.connect("row-activated", self._on_row_activated)
# If classic mode is set, we just start up a localhost daemon and connect to it # If classic mode is set, we just start up a localhost daemon and connect to it
if self.gtkui_config["classic_mode"]: if self.gtkui_config["classic_mode"]:
uri = "http://localhost:58846" uri = "http://localhost:58846"
if deluge.common.windows_check(): if deluge.common.windows_check():
win32api.WinExec("deluged -p 58846") win32api.WinExec("deluged -p 58846")
else: else:
subprocess.Popen(["deluged", "-p 58846"]) subprocess.call(["deluged", "-p 58846"])
time.sleep(0.1) time.sleep(0.1)
# We need to wait for the host to start before connecting # We need to wait for the host to start before connecting
while not self.test_online_status(uri): while not self.test_online_status(uri):
@ -142,7 +142,7 @@ class ConnectionManager(component.Component):
# This controls the timer, if it's set to false the update timer will stop. # This controls the timer, if it's set to false the update timer will stop.
self._do_update = True self._do_update = True
self._update_list() self._update_list()
# Auto connect to a host if applicable # Auto connect to a host if applicable
if self.gtkui_config["autoconnect"] and \ if self.gtkui_config["autoconnect"] and \
self.gtkui_config["autoconnect_host_uri"] != None: self.gtkui_config["autoconnect_host_uri"] != None:
@ -165,19 +165,19 @@ class ConnectionManager(component.Component):
if deluge.common.windows_check(): if deluge.common.windows_check():
win32api.WinExec("deluged -p %s" % port) win32api.WinExec("deluged -p %s" % port)
else: else:
subprocess.Popen(["deluged", "-p %s" % port]) subprocess.call(["deluged", "-p %s" % port])
# We need to wait for the host to start before connecting # We need to wait for the host to start before connecting
while not self.test_online_status(uri): while not self.test_online_status(uri):
time.sleep(0.01) time.sleep(0.01)
client.set_core_uri(uri) client.set_core_uri(uri)
self.hide() self.hide()
def start(self): def start(self):
if self.gtkui_config["autoconnect"]: if self.gtkui_config["autoconnect"]:
# We need to update the autoconnect_host_uri on connection to host # We need to update the autoconnect_host_uri on connection to host
# start() gets called whenever we get a new connection to a host # start() gets called whenever we get a new connection to a host
self.gtkui_config["autoconnect_host_uri"] = client.get_core_uri() self.gtkui_config["autoconnect_host_uri"] = client.get_core_uri()
def show(self): def show(self):
# Set the checkbuttons according to config # Set the checkbuttons according to config
self.glade.get_widget("chk_autoconnect").set_active( self.glade.get_widget("chk_autoconnect").set_active(
@ -186,13 +186,13 @@ class ConnectionManager(component.Component):
self.gtkui_config["autostart_localhost"]) self.gtkui_config["autostart_localhost"])
self.glade.get_widget("chk_donotshow").set_active( self.glade.get_widget("chk_donotshow").set_active(
not self.gtkui_config["show_connection_manager_on_start"]) not self.gtkui_config["show_connection_manager_on_start"])
# Setup timer to update host status # Setup timer to update host status
self._update_timer = gobject.timeout_add(1000, self._update_list) self._update_timer = gobject.timeout_add(1000, self._update_list)
self._update_list() self._update_list()
self._update_list() self._update_list()
self.connection_manager.show_all() self.connection_manager.show_all()
def hide(self): def hide(self):
self.connection_manager.hide() self.connection_manager.hide()
self._do_update = False self._do_update = False
@ -213,31 +213,31 @@ class ConnectionManager(component.Component):
online = self.online_status[uri] online = self.online_status[uri]
except: except:
online = False online = False
if online: if online:
image = gtk.STOCK_YES image = gtk.STOCK_YES
online = HOSTLIST_STATUS.index("Online") online = HOSTLIST_STATUS.index("Online")
else: else:
image = gtk.STOCK_NO image = gtk.STOCK_NO
online = HOSTLIST_STATUS.index("Offline") online = HOSTLIST_STATUS.index("Offline")
if uri == current_uri: if uri == current_uri:
# We are connected to this host, so lets display the connected # We are connected to this host, so lets display the connected
# icon. # icon.
image = gtk.STOCK_CONNECT image = gtk.STOCK_CONNECT
online = HOSTLIST_STATUS.index("Connected") online = HOSTLIST_STATUS.index("Connected")
pixbuf = self.connection_manager.render_icon( pixbuf = self.connection_manager.render_icon(
image, gtk.ICON_SIZE_MENU) image, gtk.ICON_SIZE_MENU)
model.set_value(row, HOSTLIST_COL_PIXBUF, pixbuf) model.set_value(row, HOSTLIST_COL_PIXBUF, pixbuf)
model.set_value(row, HOSTLIST_COL_STATUS, online) model.set_value(row, HOSTLIST_COL_STATUS, online)
current_uri = client.get_core_uri() current_uri = client.get_core_uri()
self.liststore.foreach(update_row) self.liststore.foreach(update_row)
# Update the buttons # Update the buttons
self.update_buttons() self.update_buttons()
# See if there is any row selected # See if there is any row selected
paths = self.hostlist.get_selection().get_selected_rows()[1] paths = self.hostlist.get_selection().get_selected_rows()[1]
if len(paths) < 1: if len(paths) < 1:
@ -246,7 +246,7 @@ class ConnectionManager(component.Component):
# Then select the first row # Then select the first row
self.hostlist.get_selection().select_iter(self.liststore.get_iter_first()) self.hostlist.get_selection().select_iter(self.liststore.get_iter_first())
return self._do_update return self._do_update
def update_buttons(self): def update_buttons(self):
"""Updates the buttons based on selection""" """Updates the buttons based on selection"""
if self.liststore.iter_n_children(None) < 1: if self.liststore.iter_n_children(None) < 1:
@ -260,7 +260,7 @@ class ConnectionManager(component.Component):
"_Start Daemon") "_Start Daemon")
self.glade.get_widget("label_startdaemon").set_use_underline( self.glade.get_widget("label_startdaemon").set_use_underline(
True) True)
# Get the selected row's URI # Get the selected row's URI
paths = self.hostlist.get_selection().get_selected_rows()[1] paths = self.hostlist.get_selection().get_selected_rows()[1]
# If nothing is selected, just return # If nothing is selected, just return
@ -269,12 +269,12 @@ class ConnectionManager(component.Component):
row = self.liststore.get_iter(paths[0]) row = self.liststore.get_iter(paths[0])
uri = self.liststore.get_value(row, HOSTLIST_COL_URI) uri = self.liststore.get_value(row, HOSTLIST_COL_URI)
status = self.liststore.get_value(row, HOSTLIST_COL_STATUS) status = self.liststore.get_value(row, HOSTLIST_COL_STATUS)
# Check to see if a localhost is selected # Check to see if a localhost is selected
localhost = False localhost = False
if uri.split(":")[0] == "localhost" or uri.split(":")[0] == "127.0.0.1": if uri.split(":")[0] == "localhost" or uri.split(":")[0] == "127.0.0.1":
localhost = True localhost = True
# Make actual URI string # Make actual URI string
uri = "http://" + uri uri = "http://" + uri
@ -282,7 +282,7 @@ class ConnectionManager(component.Component):
self.glade.get_widget("button_startdaemon").set_sensitive(True) self.glade.get_widget("button_startdaemon").set_sensitive(True)
self.glade.get_widget("button_connect").set_sensitive(True) self.glade.get_widget("button_connect").set_sensitive(True)
self.glade.get_widget("button_removehost").set_sensitive(True) self.glade.get_widget("button_removehost").set_sensitive(True)
# See if this is the currently connected URI # See if this is the currently connected URI
if status == HOSTLIST_STATUS.index("Connected"): if status == HOSTLIST_STATUS.index("Connected"):
# Display a disconnect button if we're connected to this host # Display a disconnect button if we're connected to this host
@ -300,7 +300,7 @@ class ConnectionManager(component.Component):
gtk.STOCK_STOP, gtk.ICON_SIZE_MENU) gtk.STOCK_STOP, gtk.ICON_SIZE_MENU)
self.glade.get_widget("label_startdaemon").set_text( self.glade.get_widget("label_startdaemon").set_text(
"_Stop Daemon") "_Stop Daemon")
# Update the start daemon button if the selected host is localhost # Update the start daemon button if the selected host is localhost
if localhost and status == HOSTLIST_STATUS.index("Offline"): if localhost and status == HOSTLIST_STATUS.index("Offline"):
# The localhost is not online # The localhost is not online
@ -308,25 +308,25 @@ class ConnectionManager(component.Component):
gtk.STOCK_EXECUTE, gtk.ICON_SIZE_MENU) gtk.STOCK_EXECUTE, gtk.ICON_SIZE_MENU)
self.glade.get_widget("label_startdaemon").set_text( self.glade.get_widget("label_startdaemon").set_text(
"_Start Daemon") "_Start Daemon")
if not localhost: if not localhost:
# An offline host # An offline host
self.glade.get_widget("button_startdaemon").set_sensitive(False) self.glade.get_widget("button_startdaemon").set_sensitive(False)
# Make sure label is displayed correctly using mnemonics # Make sure label is displayed correctly using mnemonics
self.glade.get_widget("label_startdaemon").set_use_underline( self.glade.get_widget("label_startdaemon").set_use_underline(
True) True)
def save(self): def save(self):
"""Save the current host list to file""" """Save the current host list to file"""
def append_row(model=None, path=None, row=None, columns=None): def append_row(model=None, path=None, row=None, columns=None):
hostlist.append(model.get_value(row, HOSTLIST_COL_URI)) hostlist.append(model.get_value(row, HOSTLIST_COL_URI))
hostlist = [] hostlist = []
self.liststore.foreach(append_row, hostlist) self.liststore.foreach(append_row, hostlist)
self.config["hosts"] = hostlist self.config["hosts"] = hostlist
self.config.save() self.config.save()
def test_online_status(self, uri): def test_online_status(self, uri):
"""Tests the status of URI.. Returns True or False depending on status. """Tests the status of URI.. Returns True or False depending on status.
""" """
@ -341,12 +341,12 @@ class ConnectionManager(component.Component):
del host del host
self.online_status[uri] = online self.online_status[uri] = online
return online return online
## Callbacks ## Callbacks
def on_delete_event(self, widget, event): def on_delete_event(self, widget, event):
self.hide() self.hide()
return True return True
def on_button_addhost_clicked(self, widget): def on_button_addhost_clicked(self, widget):
log.debug("on_button_addhost_clicked") log.debug("on_button_addhost_clicked")
dialog = self.glade.get_widget("addhost_dialog") dialog = self.glade.get_widget("addhost_dialog")
@ -356,11 +356,11 @@ class ConnectionManager(component.Component):
response = dialog.run() response = dialog.run()
if response == 1: if response == 1:
# We add the host # We add the host
self.add_host(hostname_entry.get_text(), self.add_host(hostname_entry.get_text(),
port_spinbutton.get_value_as_int()) port_spinbutton.get_value_as_int())
dialog.hide() dialog.hide()
def add_host(self, hostname, port): def add_host(self, hostname, port):
"""Adds the host to the list""" """Adds the host to the list"""
if hostname.startswith("http://"): if hostname.startswith("http://"):
@ -369,8 +369,8 @@ class ConnectionManager(component.Component):
# Check to make sure the hostname is at least 1 character long # Check to make sure the hostname is at least 1 character long
if len(hostname) < 1: if len(hostname) < 1:
return return
# Get the port and concatenate the hostname string # Get the port and concatenate the hostname string
hostname = hostname + ":" + str(port) hostname = hostname + ":" + str(port)
# Check to see if there is already an entry for this host and return # Check to see if there is already an entry for this host and return
@ -382,7 +382,7 @@ class ConnectionManager(component.Component):
self.liststore.foreach(each_row, None) self.liststore.foreach(each_row, None)
if hostname in self.hosts_liststore: if hostname in self.hosts_liststore:
return return
# Host isn't in the list, so lets add it # Host isn't in the list, so lets add it
row = self.liststore.append() row = self.liststore.append()
self.liststore.set_value(row, HOSTLIST_COL_URI, hostname) self.liststore.set_value(row, HOSTLIST_COL_URI, hostname)
@ -390,20 +390,20 @@ class ConnectionManager(component.Component):
self.save() self.save()
# Update the status of the hosts # Update the status of the hosts
self._update_list() self._update_list()
def on_button_removehost_clicked(self, widget): def on_button_removehost_clicked(self, widget):
log.debug("on_button_removehost_clicked") log.debug("on_button_removehost_clicked")
# Get the selected rows # Get the selected rows
paths = self.hostlist.get_selection().get_selected_rows()[1] paths = self.hostlist.get_selection().get_selected_rows()[1]
for path in paths: for path in paths:
self.liststore.remove(self.liststore.get_iter(path)) self.liststore.remove(self.liststore.get_iter(path))
# Update the hostlist # Update the hostlist
self._update_list() self._update_list()
# Save the host list # Save the host list
self.save() self.save()
def on_button_startdaemon_clicked(self, widget): def on_button_startdaemon_clicked(self, widget):
log.debug("on_button_startdaemon_clicked") log.debug("on_button_startdaemon_clicked")
if self.liststore.iter_n_children(None) < 1: if self.liststore.iter_n_children(None) < 1:
@ -412,7 +412,7 @@ class ConnectionManager(component.Component):
# ..and start the daemon. # ..and start the daemon.
self.start_localhost(58846) self.start_localhost(58846)
return return
paths = self.hostlist.get_selection().get_selected_rows()[1] paths = self.hostlist.get_selection().get_selected_rows()[1]
if len(paths) < 1: if len(paths) < 1:
return return
@ -431,7 +431,7 @@ class ConnectionManager(component.Component):
self.update() self.update()
elif HOSTLIST_STATUS[status] == "Offline": elif HOSTLIST_STATUS[status] == "Offline":
self.start_localhost(port) self.start_localhost(port)
def start_localhost(self, port): def start_localhost(self, port):
"""Starts a localhost daemon""" """Starts a localhost daemon"""
port = str(port) port = str(port)
@ -440,7 +440,7 @@ class ConnectionManager(component.Component):
if deluge.common.windows_check(): if deluge.common.windows_check():
win32api.WinExec("deluged -p %s" % port) win32api.WinExec("deluged -p %s" % port)
else: else:
subprocess.Popen(["deluged", "-p %s" % port]) subprocess.call(["deluged", "-p %s" % port])
def on_button_close_clicked(self, widget): def on_button_close_clicked(self, widget):
log.debug("on_button_close_clicked") log.debug("on_button_close_clicked")
@ -458,7 +458,7 @@ class ConnectionManager(component.Component):
port = uri.split(":")[1] port = uri.split(":")[1]
if uri.split(":")[0] == "localhost": if uri.split(":")[0] == "localhost":
localhost = True localhost = True
uri = "http://" + uri uri = "http://" + uri
if status == HOSTLIST_STATUS.index("Connected"): if status == HOSTLIST_STATUS.index("Connected"):
# Stop all the components first. # Stop all the components first.
@ -467,7 +467,7 @@ class ConnectionManager(component.Component):
client.set_core_uri(None) client.set_core_uri(None)
self._update_list() self._update_list()
return return
# Test the host to see if it is online or not. We don't use the status # Test the host to see if it is online or not. We don't use the status
# column information because it can be up to 5 seconds out of sync. # column information because it can be up to 5 seconds out of sync.
if not self.test_online_status(uri): if not self.test_online_status(uri):
@ -477,14 +477,14 @@ class ConnectionManager(component.Component):
self.start_localhost(port) self.start_localhost(port)
# We need to wait for the host to start before connecting # We need to wait for the host to start before connecting
while not self.test_online_status(uri): while not self.test_online_status(uri):
time.sleep(0.01) time.sleep(0.01)
client.set_core_uri(uri) client.set_core_uri(uri)
self._update_list() self._update_list()
self.hide() self.hide()
# Update the list to show proper status # Update the list to show proper status
self._update_list() self._update_list()
return return
# Status is OK, so lets change to this host # Status is OK, so lets change to this host
@ -509,11 +509,10 @@ class ConnectionManager(component.Component):
log.debug("on_chk_donotshow_toggled") log.debug("on_chk_donotshow_toggled")
value = widget.get_active() value = widget.get_active()
self.gtkui_config["show_connection_manager_on_start"] = not value self.gtkui_config["show_connection_manager_on_start"] = not value
def on_selection_changed(self, treeselection): def on_selection_changed(self, treeselection):
log.debug("on_selection_changed") log.debug("on_selection_changed")
self.update_buttons() self.update_buttons()
def _on_row_activated(self, tree, path, view_column): def _on_row_activated(self, tree, path, view_column):
self.on_button_connect_clicked(self.glade.get_widget("button_connect")) self.on_button_connect_clicked(self.glade.get_widget("button_connect"))