gget r36 - in trunk: data gget



Author: johans
Date: Mon Jul 21 12:01:32 2008
New Revision: 36
URL: http://svn.gnome.org/viewvc/gget?rev=36&view=rev

Log:
Made GGet single-instance using DBus.

Added:
   trunk/data/org.gnome.gget.service.in
Modified:
   trunk/data/   (props changed)
   trunk/data/Makefile.am
   trunk/data/gget.glade
   trunk/gget/AddDownloadDialog.py
   trunk/gget/DBusService.py
   trunk/gget/Download.py
   trunk/gget/Main.py
   trunk/gget/Utils.py

Modified: trunk/data/Makefile.am
==============================================================================
--- trunk/data/Makefile.am	(original)
+++ trunk/data/Makefile.am	Mon Jul 21 12:01:32 2008
@@ -1,5 +1,12 @@
 SUBDIRS = images
 
+servicedir       = $(datadir)/dbus-1/services
+service_in_files = org.gnome.gget.service.in
+service_DATA     = $(service_in_files:.service.in=.service)
+
+$(service_DATA): $(service_in_files) Makefile
+	@sed -e "s|\ bindir\@|$(bindir)|" $< > $@
+
 desktopdir       = $(datadir)/applications
 desktop_in_files = gget.desktop.in
 desktop_DATA     = $(desktop_in_files:.desktop.in=.desktop)
@@ -22,10 +29,12 @@
 endif
 
 DISTCLEANFILES =		\
+	$(service_DATA)		\
 	$(desktop_DATA)		\
 	$(schema_DATA)
 
 EXTRA_DIST =			\
+	$(service_in_files)	\
 	$(desktop_in_files)	\
 	$(glade_DATA)		\
 	$(schema_in_files)

Modified: trunk/data/gget.glade
==============================================================================
--- trunk/data/gget.glade	(original)
+++ trunk/data/gget.glade	Mon Jul 21 12:01:32 2008
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 <!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
-<!--Generated with glade3 3.4.5 on Fri Jul 18 16:23:42 2008 -->
+<!--Generated with glade3 3.4.5 on Mon Jul 21 00:07:25 2008 -->
 <glade-interface>
   <widget class="GtkWindow" id="main_window">
     <property name="width_request">800</property>
@@ -256,6 +256,7 @@
               <widget class="GtkToolButton" id="details_tool_button">
                 <property name="visible">True</property>
                 <property name="sensitive">False</property>
+                <property name="label" translatable="yes">Details</property>
                 <property name="stock_id">gtk-info</property>
               </widget>
               <packing>
@@ -346,7 +347,7 @@
                     <property name="visible">True</property>
                     <property name="xalign">0</property>
                     <property name="yalign">0</property>
-                    <property name="label" translatable="yes">Please enter the URL to the file you would like to download and select a folder to save it in.</property>
+                    <property name="label" translatable="yes">Please enter the URI to the file you would like to download and select a folder to save it in.</property>
                     <property name="wrap">True</property>
                   </widget>
                   <packing>
@@ -389,7 +390,7 @@
                       </packing>
                     </child>
                     <child>
-                      <widget class="GtkEntry" id="url_entry">
+                      <widget class="GtkEntry" id="uri_entry">
                         <property name="visible">True</property>
                         <property name="can_focus">True</property>
                       </widget>
@@ -402,9 +403,9 @@
                       <widget class="GtkLabel" id="label3">
                         <property name="visible">True</property>
                         <property name="xalign">1</property>
-                        <property name="label" translatable="yes">_URL:</property>
+                        <property name="label" translatable="yes">_URI:</property>
                         <property name="use_underline">True</property>
-                        <property name="mnemonic_widget">url_entry</property>
+                        <property name="mnemonic_widget">uri_entry</property>
                       </widget>
                       <packing>
                         <property name="x_options">GTK_FILL</property>
@@ -692,8 +693,8 @@
                                           <widget class="GtkFileChooserButton" id="default_folder_filechooserbutton">
                                             <property name="visible">True</property>
                                             <property name="sensitive">False</property>
-                                            <property name="action">GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER</property>
                                             <property name="local_only">False</property>
+                                            <property name="action">GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER</property>
                                             <property name="title" translatable="yes">Select download folder</property>
                                           </widget>
                                         </child>

Added: trunk/data/org.gnome.gget.service.in
==============================================================================
--- (empty file)
+++ trunk/data/org.gnome.gget.service.in	Mon Jul 21 12:01:32 2008
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.gnome.gget
+Exec= bindir@/gget

Modified: trunk/gget/AddDownloadDialog.py
==============================================================================
--- trunk/gget/AddDownloadDialog.py	(original)
+++ trunk/gget/AddDownloadDialog.py	Mon Jul 21 12:01:32 2008
@@ -21,6 +21,7 @@
 from gettext import gettext as _
 
 import gtk
+import gnomevfs
 
 import GUI
 import Utils
@@ -39,11 +40,11 @@
         self.owner_change_id = self.clipboard.connect("owner-change",
                 self.__clipboard_owner_change)
 
-        self.__valid_url = False
+        self.__valid_uri = False
         if uri != "":
-            self.url_entry.set_text(uri)
+            self.uri_entry.set_text(uri)
         else:
-            self.__set_url_from_clipboard(self.clipboard)
+            self.__set_uri_from_clipboard(self.clipboard)
 
         folder = Utils.get_folder_for_extension(uri)
         if not folder:
@@ -55,50 +56,50 @@
 
         self.dialog = xml.get_widget("add_dialog")
 
-        self.url_entry = xml.get_widget("url_entry")
+        self.uri_entry = xml.get_widget("uri_entry")
         self.download_filechooserbutton = xml.get_widget("download_filechooserbutton")
 
         self.add_button = xml.get_widget("add_add_button")
         self.cancel_button = xml.get_widget("add_cancel_button")
 
     def __connect_widgets(self):
-        self.url_entry.connect("changed", self.__url_entry_changed)
-        self.url_entry.connect("activate", self.__url_entry_activate)
+        self.uri_entry.connect("changed", self.__uri_entry_changed)
+        self.uri_entry.connect("activate", self.__uri_entry_activate)
 
         self.add_button.connect("clicked", self.__add_button_clicked)
         self.cancel_button.connect("clicked", self.__cancel_button_clicked)
 
-    def __url_entry_changed(self, entry):
-        url = entry.get_text()
-        if len(url) > 0 and (self.__valid_url or self.__is_valid_url(url)):
+    def __uri_entry_changed(self, entry):
+        uri = entry.get_text()
+        if len(uri) > 0 and (self.__valid_uri or self.__is_valid_uri(uri)):
             self.add_button.set_sensitive(True)
         else:
             self.add_button.set_sensitive(False)
 
     def __clipboard_owner_change(self, clipboard, event):
-        self.__set_url_from_clipboard(clipboard)
+        self.__set_uri_from_clipboard(clipboard)
 
-    def __set_url_from_clipboard(self, clipboard):
+    def __set_uri_from_clipboard(self, clipboard):
         if clipboard.wait_is_text_available():
-            url = clipboard.wait_for_text()
-            if url and self.__is_valid_url(url):
-                self.url_entry.set_text(url)
-
-    def __is_valid_url(self, url):
-        PROTOCOLS = ["http", "https", "ftp"]
-        for protocol in PROTOCOLS:
-            if url.startswith(protocol + "://"):
-                self.__valid_url = True
-                return True
-        self.__valid_url = False
+            uri = clipboard.wait_for_text()
+            if uri and self.__is_valid_uri(uri):
+                self.uri_entry.set_text(uri)
+
+    def __is_valid_uri(self, uri):
+        uri = gnomevfs.make_uri_from_shell_arg(uri)
+        scheme = gnomevfs.get_uri_scheme(uri)
+        if scheme in ["file", "http", "https", "ftp"]:
+            self.__valid_uri = True
+            return True
+        self.__valid_uri = False
         return False
 
-    def __url_entry_activate(self, entry):
+    def __uri_entry_activate(self, entry):
         self.add_button.clicked()
 
     def __add_button_clicked(self, button):
         download_list = DownloadList()
-        download_list.add_download(self.url_entry.get_text(),
+        download_list.add_download(self.uri_entry.get_text(),
                 self.download_filechooserbutton.get_current_folder())
 
         self.clipboard.disconnect(self.owner_change_id)

Modified: trunk/gget/DBusService.py
==============================================================================
--- trunk/gget/DBusService.py	(original)
+++ trunk/gget/DBusService.py	Mon Jul 21 12:01:32 2008
@@ -18,7 +18,13 @@
 # along with gget; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
+import os
+
 import GUI
+import Utils
+import AddDownloadDialog
+from Configuration import Configuration
+from gget import NAME
 
 try:
     import dbus
@@ -47,18 +53,38 @@
         return DBusService.instance
 
     def __init(self, *args):
+        self.download_list = args[0]
+
+    def register(self):
+        """Tries to registers the DBus service and its exposed objects. Returns
+        True if sucessful (i.e. service not already running) else False."""
         dbus_loop = DBusGMainLoop()
         self.session_bus = dbus.SessionBus(mainloop=dbus_loop)
 
+        try:
+            Utils.take_dbus_name(SERVICE, bus=self.session_bus)
+        except Utils.DBusNameExistsException, e:
+            obj = self.session_bus.get_object(SERVICE, OBJECT_PATH)
+            self.gget_object = dbus.Interface(obj, INTERFACE)
+            Utils.debug_print("The %s DBus service (%s) is already registered on the session bus." % (NAME, SERVICE))
+            return False
+
+        Utils.debug_print("The %s DBus service (%s) was sucessfully registered on the session bus." % (NAME, SERVICE))
+
+        self.__register_objects()
+        return True
+
+    def __register_objects(self):
+        """Registers the GGet DBus service object."""
         bus_name = dbus.service.BusName(SERVICE, bus=self.session_bus)
-        self.gget_object = GGetObject(bus_name, args[0])
+        self.gget_object = GGetObject(bus_name, self.download_list)
 
 class GGetObject(dbus.service.Object):
     def __init__(self, bus_name, download_list):
         dbus.service.Object.__init__(self, bus_name, OBJECT_PATH)
         self.download_list = download_list
+        self.config = Configuration()
 
-        self.download_list = download_list
         self.download_list.connect("download-added", self.__download_added)
         self.download_list.connect("download-removed", self.__download_removed)
 
@@ -70,18 +96,26 @@
 
     @dbus.service.signal(INTERFACE, signature='ss')
     def DownloadAdded(self, uri, path):
+        Utils.debug_print("Emitted DBus DownloadAdded signal.")
         pass
 
     @dbus.service.signal(INTERFACE, signature='s')
     def DownloadRemoved(self, uri):
+        Utils.debug_print("Emitted DBus DownloadRemoved signal.")
         pass
 
     @dbus.service.signal(INTERFACE, signature='s')
     def DownloadStatusChanged(self, status):
+        Utils.debug_print("Emitted DBus DownloadStatusChanged signal.")
         pass
 
     @dbus.service.method(INTERFACE, in_signature='ss', out_signature='')
-    def AddDownload(self, uri, path):
-        self.download_list.add_download(uri, path)
+    def AddDownload(self, uri, path=os.getcwd()):
+        Utils.debug_print("DBus AddDownload method was invoked.")
+        if self.config.ask_for_location:
+            add = AddDownloadDialog.AddDownloadDialog(uri)
+            add.dialog.show()
+        else:
+            self.download_list.add_download(uri, path)
 
 # vim: set sw=4 et sts=4 tw=79 fo+=l:

Modified: trunk/gget/Download.py
==============================================================================
--- trunk/gget/Download.py	(original)
+++ trunk/gget/Download.py	Mon Jul 21 12:01:32 2008
@@ -42,16 +42,15 @@
 COMPLETED = 4
 ERROR = 5
 
-STATUS_STRINGS = { CONNECTING: "Connecting",
-        DOWNLOADING: "Downloading",
-        CANCELED: "Canceled",
-        PAUSED: "Paused",
-        COMPLETED: "Completed",
-        ERROR: "Error"}
+STATUS_STRINGS = {CONNECTING:  "Connecting",
+                  DOWNLOADING: "Downloading",
+                  CANCELED:    "Canceled",
+                  PAUSED:      "Paused",
+                  COMPLETED:   "Completed",
+                  ERROR:       "Error"}
 
 class Download(gobject.GObject):
-    __gsignals__ = {"update":   (gobject.SIGNAL_RUN_LAST, None, (int, int,
-                                 int)),
+    __gsignals__ = {"update": (gobject.SIGNAL_RUN_LAST, None, (int, int, int)),
                     "bitrate": (gobject.SIGNAL_RUN_LAST, None, (float,)),
                     "status-changed": (gobject.SIGNAL_RUN_LAST, None, (int,))}
 
@@ -60,7 +59,7 @@
         self.config = Configuration()
         self.dbus_service = DBusService()
 
-        self.uri = uri
+        self.uri = gnomevfs.make_uri_from_shell_arg(uri)
         self.file_name = os.path.basename(self.uri)
 
         self.path = path

Modified: trunk/gget/Main.py
==============================================================================
--- trunk/gget/Main.py	(original)
+++ trunk/gget/Main.py	Mon Jul 21 12:01:32 2008
@@ -21,6 +21,7 @@
 import getopt
 import gettext
 import locale
+import os
 import sys
 from gettext import gettext as _
 
@@ -68,6 +69,10 @@
     download_manager = DownloadManager()
 
     dbus_service = DBusService(download_list)
+    if not dbus_service.register():
+        for uri in args:
+            dbus_service.gget_object.AddDownload(uri, os.getcwd())
+        return 0
 
     main_window = MainWindow(config, download_list)
     if config.show_main_window:

Modified: trunk/gget/Utils.py
==============================================================================
--- trunk/gget/Utils.py	(original)
+++ trunk/gget/Utils.py	Mon Jul 21 12:01:32 2008
@@ -22,6 +22,9 @@
 import os.path
 import time
 
+import dbus
+import dbus.glib
+
 from Configuration import Configuration
 
 def get_readable_size(bits):
@@ -60,4 +63,23 @@
         if level and (not element.tail or not element.tail.strip()):
             element.tail = i
 
+def take_dbus_name(name, replace=False, on_name_lost=None, bus=None):
+    target_bus = bus or dbus.Bus()
+    proxy = bus_proxy(bus=target_bus)
+    flags = 1 | 4 # allow replacement | do not queue
+    if replace:
+        flags = flags | 2 # replace existing
+    if not proxy.RequestName(name, dbus.UInt32(flags)) in (1,4):
+        raise DBusNameExistsException("Couldn't get D-BUS name %s: Name exists")
+    if on_name_lost:
+        proxy.connect_to_signal('NameLost', on_name_lost)
+
+def bus_proxy(bus=None):
+    target_bus = bus or dbus.Bus()
+    return target_bus.get_object('org.freedesktop.DBus',
+            '/org/freedesktop/DBus')
+
+class DBusNameExistsException(Exception):
+    pass
+
 # vim: set sw=4 et sts=4 tw=79 fo+=l:



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]