[rhythmbox] Use gio through introspection in python plugins (bug 645703)



commit b2b90815d9d1440289b1ed5e2f516eb8cd47e7d1
Author: Jonathan Matthew <jonathan d14n org>
Date:   Thu Mar 24 23:30:25 2011 +1000

    Use gio through introspection in python plugins (bug 645703)
    
    (except for jamendo and magnatune, for which more work is required
    in pygobject)

 plugins/artdisplay/artdisplay/CoverArtDatabase.py  |    3 +-
 .../artdisplay/artdisplay/LocalCoverArtSearch.py   |   35 +++++---
 plugins/artdisplay/artdisplay/__init__.py          |    5 +-
 plugins/context/context/ContextView.py             |   10 +--
 plugins/rb/Loader.py                               |   98 ++++++++++----------
 plugins/rbzeitgeist/rbzeitgeist/__init__.py        |    8 +-
 6 files changed, 83 insertions(+), 76 deletions(-)
---
diff --git a/plugins/artdisplay/artdisplay/CoverArtDatabase.py b/plugins/artdisplay/artdisplay/CoverArtDatabase.py
index 23bc6e0..a1d3e1f 100644
--- a/plugins/artdisplay/artdisplay/CoverArtDatabase.py
+++ b/plugins/artdisplay/artdisplay/CoverArtDatabase.py
@@ -29,7 +29,6 @@ import os
 import itertools
 import gobject
 import gi
-import gio
 
 import rb
 from gi.repository import GdkPixbuf
@@ -365,6 +364,8 @@ class CoverArtDatabase (object):
 				return l.get_pixbuf()
 			except gobject.GError, e:
 				print "error reading image: %s" % str(e)
+				import sys
+				sys.excepthook(*sys.exc_info())
 				pass
 
 		return None
diff --git a/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py b/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py
index 45fbcb3..921f5d0 100644
--- a/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py
+++ b/plugins/artdisplay/artdisplay/LocalCoverArtSearch.py
@@ -27,10 +27,11 @@
 
 import os
 import gobject
-import gio
+import glib
 import gi
 
 from gi.repository import RB
+from gi.repository import Gio
 
 IMAGE_NAMES = ["cover", "album", "albumart", ".folder", "folder"]
 ITEMS_PER_NOTIFICATION = 10
@@ -78,24 +79,28 @@ class LocalCoverArtSearch:
 				if ct is not None and ct.startswith("image/") and readable:
 					results.append(f.get_name())
 
-			fileenum.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data=(results, on_search_completed_cb, entry, args))
+			fileenum.next_files_async(ITEMS_PER_NOTIFICATION, glib.PRIORITY_DEFAULT, None, self._enum_dir_cb, (results, on_search_completed_cb, entry, args))
 		except Exception, e:
 			print "okay, probably done: %s" % e
+			import sys
+			sys.excepthook(*sys.exc_info())
 			on_search_completed_cb(self, entry, results, *args)
 
 
 	def _enum_children_cb(self, parent, result, (on_search_completed_cb, entry, args)):
 		try:
 			enumfiles = parent.enumerate_children_finish(result)
-			enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._enum_dir_cb, user_data=([], on_search_completed_cb, entry, args))
+			enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, glib.PRIORITY_DEFAULT, None, self._enum_dir_cb, ([], on_search_completed_cb, entry, args))
 		except Exception, e:
 			print "okay, probably done: %s" % e
+			import sys
+			sys.excepthook(*sys.exc_info())
 			on_search_completed_cb(self, entry, [], *args)
 
 
 	def search (self, db, entry, is_playing, on_search_completed_cb, *args):
 
-		self.file = gio.File(entry.get_playback_uri())
+		self.file = Gio.file_new_for_uri(entry.get_playback_uri())
 		if self.file.get_uri_scheme() in IGNORED_SCHEMES:
 			print 'not searching for local art for %s' % (self.file.get_uri())
 			on_search_completed_cb (self, entry, [], *args)
@@ -105,7 +110,7 @@ class LocalCoverArtSearch:
 
 		print 'searching for local art for %s' % (self.file.get_uri())
 		parent = self.file.get_parent()
-		enumfiles = parent.enumerate_children_async(attributes="standard::content-type,access::can-read,standard::name", callback = self._enum_children_cb, user_data=(on_search_completed_cb, entry, args))
+		enumfiles = parent.enumerate_children_async("standard::content-type,access::can-read,standard::name", 0, 0, None, self._enum_children_cb, (on_search_completed_cb, entry, args))
 
 	def search_next (self):
 		return False
@@ -162,7 +167,7 @@ class LocalCoverArtSearch:
 
 		file.replace_async(replace_cb, user_data=pixbuf())
 
-	def _save_dir_cb (self, enum, result, (db, entry, dir, pixbuf)):
+	def _save_dir_cb (self, enum, result, (db, entry, parent, pixbuf)):
 		artist, album = get_search_props(db, entry)
 
 		try:
@@ -179,7 +184,7 @@ class LocalCoverArtSearch:
 				if ct.startswith("image/") or ct.startswith("x-directory/"):
 					continue
 
-				uri = dir.resolve_relative_path(f.get_name()).get_uri()
+				uri = parent.resolve_relative_path(f.get_name()).get_uri()
 				u_entry = db.entry_lookup_by_location (uri)
 				if u_entry:
 					u_artist, u_album = get_search_props(db, u_entry)
@@ -191,17 +196,24 @@ class LocalCoverArtSearch:
 				print "Not saving local art; encountered unknown file (%s)" % uri
 				enum.close()
 				return
-	
-			enum.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._save_dir_cb, user_data=(db, entry, dir, pixbuf))
+
+			enum.next_files_async(ITEMS_PER_NOTIFICATION, glib.PRIORITY_DEFAULT, None, self._save_dir_cb, (db, entry, parent, pixbuf))
 		except Exception, e:
 			print "Error reading \"%s\": %s" % (dir, e)
 
+	def _save_enum_children_cb (self, parent, result, (db, entry, pixbuf)):
+		try:
+			enum = parent.enumerate_children_finish(result)
+			enum.next_files_async(ITEMS_PER_NOTIFICATION, glib.PRIORITY_DEFAULT, None, self._save_dir_cb, (db, entry, parent, pixbuf))
+		except Exception, e:
+			return
+
 	def save_pixbuf (self, db, entry, pixbuf):
 		uri = entry.get_playback_uri()
 		if uri is None or uri == '':
 			return
 
-		f = gio.File(uri)
+		f = Gio.file_new_for_uri(uri)
 		if f.get_uri_scheme() in IGNORED_SCHEMES:
 			print "not saving local art for %s" % uri
 			return
@@ -209,7 +221,6 @@ class LocalCoverArtSearch:
 		print 'checking whether to save local art for %s' % uri
 		parent = f.get_parent()
 		try:
-			enumfiles = parent.enumerate_children(attributes="standard::fast-content-type,access::can-read,standard::name")
-			enumfiles.next_files_async(ITEMS_PER_NOTIFICATION, callback = self._save_dir_cb, user_data=(db, entry, parent, pixbuf))
+			parent.enumerate_children_async("standard::content-type,access::can-read,standard::name", Gio.FileQueryInfoFlags.NONE, glib.PRIORITY_DEFAULT, None, self._save_enum_children_cb, (db, entry, pixbuf))
 		except Exception, e:
 			print "unable to scan directory %s: %s" % (parent.get_uri(), e)
diff --git a/plugins/artdisplay/artdisplay/__init__.py b/plugins/artdisplay/artdisplay/__init__.py
index d6709f1..5fc303a 100644
--- a/plugins/artdisplay/artdisplay/__init__.py
+++ b/plugins/artdisplay/artdisplay/__init__.py
@@ -25,7 +25,6 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
 import gobject
-import gio
 import gi
 
 from warnings import warn
@@ -33,7 +32,7 @@ from warnings import warn
 from CoverArtDatabase import CoverArtDatabase
 
 import rb
-from gi.repository import Gtk, Gdk, GdkPixbuf
+from gi.repository import Gtk, Gdk, GdkPixbuf, Gio
 from gi.repository import RB
 
 FADE_STEPS = 10
@@ -570,5 +569,5 @@ class ArtDisplayPlugin (RB.Plugin):
 		if self.art_widget.current_uri is None:
 			return
 
-		f = gio.File(self.art_widget.current_uri)
+		f = Gio.file_new_for_uri(self.art_widget.current_uri)
 		Gtk.show_uri(self.shell.props.window.get_screen(), f.get_uri(), event.time)
diff --git a/plugins/context/context/ContextView.py b/plugins/context/context/ContextView.py
index ac042ab..209d569 100644
--- a/plugins/context/context/ContextView.py
+++ b/plugins/context/context/ContextView.py
@@ -24,7 +24,7 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
-import gobject, gio
+import gobject
 import os
 
 import ArtistTab as at
@@ -33,7 +33,7 @@ import LyricsTab as lt
 import LinksTab as lit
 
 import rb
-from gi.repository import Gtk, Gdk, Pango
+from gi.repository import Gtk, Gdk, Pango, Gio
 from gi.repository import RB
 from gi.repository import WebKit
 
@@ -211,11 +211,7 @@ class ContextView (gobject.GObject):
         # open HTTP URIs externally.  this isn't a web browser.
         if request.get_uri().startswith('http'):
             print "opening uri %s" % request.get_uri()
-            appinfo = gio.app_info_get_default_for_uri_scheme("http")
-            try:
-                appinfo.launch_uris((request.get_uri(),))
-            except Exception, e:
-                print "failed: %s" % str(e)
+	    Gtk.show_uri(self.shell.props.window.get_screen(), request.get_uri(), Gdk.CURRENT_TIME)
 
             return 1        # WEBKIT_NAVIGATION_RESPONSE_IGNORE
         else:
diff --git a/plugins/rb/Loader.py b/plugins/rb/Loader.py
index 22854f5..872bb57 100644
--- a/plugins/rb/Loader.py
+++ b/plugins/rb/Loader.py
@@ -25,10 +25,8 @@
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
 import gobject
-import gi
-from gi.repository import Gdk
+from gi.repository import Gdk, Gio
 import sys
-import gio
 
 def callback_with_gdk_lock(callback, data, args):
 	Gdk.threads_enter()
@@ -37,21 +35,23 @@ def callback_with_gdk_lock(callback, data, args):
 		Gdk.threads_leave()
 		return v
 	except Exception, e:
-		print "Exception caught in loader callback: %s" % str(e)
 		sys.excepthook(*sys.exc_info())
 		Gdk.threads_leave()
 
 
 class Loader(object):
 	def __init__ (self):
-		self._cancel = gio.Cancellable()
+		self._cancel = Gio.Cancellable()
 
-	def _contents_cb (self, file, result):
+	def _contents_cb (self, file, result, data):
 		try:
-			(contents, length, etag) = file.load_contents_finish(result)
-			callback_with_gdk_lock(self.callback, contents, self.args)
+			(ok, contents, etag) = file.load_contents_finish(result)
+			if ok:
+				callback_with_gdk_lock(self.callback, contents, self.args)
+			else:
+				callback_with_gdk_lock(self.callback, None, self.args)
 		except Exception, e:
-			# somehow check if we just got cancelled
+			sys.excepthook(*sys.exc_info())
 			callback_with_gdk_lock(self.callback, None, self.args)
 
 	def get_url (self, url, callback, *args):
@@ -59,10 +59,10 @@ class Loader(object):
 		self.callback = callback
 		self.args = args
 		try:
-			file = gio.File(url)
-			file.load_contents_async(callback = self._contents_cb, cancellable=self._cancel)
+			file = Gio.file_new_for_uri(url)
+			file.load_contents_async(self._cancel, self._contents_cb, None)
 		except Exception, e:
-			print "error getting contents of %s: %s" % (url, e)
+			sys.excepthook(*sys.exc_info())
 			callback(None, *args)
 
 	def cancel (self):
@@ -71,7 +71,7 @@ class Loader(object):
 
 class ChunkLoader(object):
 	def __init__ (self):
-		self._cancel = gio.Cancellable()
+		self._cancel = Gio.Cancellable()
 
 	def _callback(self, result):
 		return self.callback(result, self.total, *self.args)
@@ -91,48 +91,49 @@ class ChunkLoader(object):
 		self._callback_gdk(error)
 		return False
 
-	def _read_idle_cb(self, (stream, data)):
-		if (self._callback_gdk(data) is not False) and data:
-			stream.read_async (self.chunksize, self._read_cb, cancellable=self._cancel)
-		else:
-			# finished or cancelled by callback
-			stream.close()
-
-		return False
-
 	def _read_cb(self, stream, result):
 		try:
 			data = stream.read_finish(result)
-		except gio.Error, e:
-			print "error reading file %s: %s" % (self.uri, e.message)
+		except Exception, e:
+			print "error reading file %s" % (self.uri)
+			sys.excepthook(*sys.exc_info())
 			stream.close()
 			gobject.idle_add(self._error_idle_cb, e)
-		
-		# this is mostly here to hack around bug 575781
-		gobject.idle_add(self._read_idle_cb, (stream, data))
+
+		if (self._callback_gdk(data) is not False) and data:
+			stream.read_async (self.chunksize, glib.PRIORITY_DEFAULT, self._cancel, self._read_cb, None)
+		else:
+			# finished or cancelled by callback
+			stream.close()
 
 
 	def _open_cb(self, file, result):
 		try:
 			stream = file.read_finish(result)
-		except gio.Error, e:
-			print "error reading file %s: %s" % (self.uri, e.message)
+		except Exception, e:
+			print "error reading file %s" % (self.uri)
+			sys.excepthook(*sys.exc_info())
 			self._callback_gdk(e)
-		
-		stream.read_async(self.chunksize, self._read_cb, cancellable=self._cancel)
+
+		stream.read_async(self.chunksize, glib.PRIORITY_DEFAULT, self._cancel, self._read_cb, None)
 
 	def _info_cb(self, file, result):
 		try:
 			info = file.query_info_finish(result)
-			self.total = info.get_attribute_uint64(gio.FILE_ATTRIBUTE_STANDARD_SIZE)
+			self.total = info.get_attribute_uint64(Gio.FILE_ATTRIBUTE_STANDARD_SIZE)
 
-			file.read_async(self._open_cb, cancellable=self._cancel)
-		except gio.Error, e:
-			print "error checking size of source file %s: %s" % (self.uri, e.message)
+			file.read_async(glib.PRIORITY_DEFAULT, self._cancel, self._open_cb, None)
+		except Exception, e:
+			print "error checking size of source file %s" % (self.uri)
+			sys.excepthook(*sys.exc_info())
 			self._callback_gdk(e)
 
 
 	def get_url_chunks (self, uri, chunksize, want_size, callback, *args):
+		# this can't possibly work yet, we need to get annotations and
+		# other stuff for g_input_stream_read_async right first.
+		raise Exception("rb.ChunkLoader not implemented yet")
+
 		try:
 			self.uri = uri
 			self.chunksize = chunksize
@@ -142,10 +143,10 @@ class ChunkLoader(object):
 
 			file = gio.File(uri)
 			if want_size:
-				file.query_info_async(self._info_cb, gio.FILE_ATTRIBUTE_STANDARD_SIZE, cancellable=self._cancel)
+				file.query_info_async(Gio.FILE_ATTRIBUTE_STANDARD_SIZE, Gio.FileQueryInfoFlags.NONE, glib.PRIORITY_DEFAULT, self._cancel, self._info_cb, None)
 			else:
-				file.read_async(self._open_cb, cancellable=self._cancel)
-		except gio.Error, e:
+				file.read_async(glib.PRIORITY_DEFAULT, self._cancel, self._open_cb, None)
+		except Exception, e:
 			print "error reading file %s: %s" % (uri, e.message)
 			self._callback(e)
 
@@ -155,16 +156,15 @@ class ChunkLoader(object):
 
 class UpdateCheck(object):
 	def __init__ (self):
-		self._cancel = gio.Cancellable()
+		self._cancel = Gio.Cancellable()
 
-	def _file_info_cb (self, file, result):
+	def _file_info_cb (self, file, result, data):
 		try:
 			rfi = file.query_info_finish(result)
-
-			remote_mod = rfi.get_attribute_uint64(gio.FILE_ATTRIBUTE_TIME_MODIFIED)
+			remote_mod = rfi.get_attribute_uint64(Gio.FILE_ATTRIBUTE_TIME_MODIFIED)
 			callback_with_gdk_lock(self.callback, remote_mod != self.local_mod, self.args)
 		except Exception, e:
-			print "error checking for update: %s" % e
+			sys.excepthook(*sys.exc_info())
 			callback_with_gdk_lock(self.callback, False, self.args)
 
 	def check_for_update (self, local, remote, callback, *args):
@@ -174,14 +174,14 @@ class UpdateCheck(object):
 		self.args = args
 
 		try:
-			lf = gio.File(local)
-			lfi = lf.query_info(gio.FILE_ATTRIBUTE_TIME_MODIFIED)
-			self.local_mod = lfi.get_attribute_uint64(gio.FILE_ATTRIBUTE_TIME_MODIFIED)
+			lf = Gio.file_new_for_commandline_arg(local)
+			lfi = lf.query_info(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, None)
+			self.local_mod = lfi.get_attribute_uint64(Gio.FILE_ATTRIBUTE_TIME_MODIFIED)
 
-			rf = gio.File(remote)
-			rf.query_info_async(self._file_info_cb, gio.FILE_ATTRIBUTE_TIME_MODIFIED, cancellable=self._cancel)
+			rf = Gio.file_new_for_uri(remote)
+			rf.query_info_async(Gio.FILE_ATTRIBUTE_TIME_MODIFIED, Gio.FileQueryInfoFlags.NONE, glib.PRIORITY_DEFAULT, self._cancel, self._file_info_cb, None)
 		except Exception, e:
-			print "error checking for update: %s" % e
+			sys.excepthook(*sys.exc_info())
 			self.callback(True, *self.args)
 
 	def cancel (self):
diff --git a/plugins/rbzeitgeist/rbzeitgeist/__init__.py b/plugins/rbzeitgeist/rbzeitgeist/__init__.py
index 8bf2e7b..c1caafc 100644
--- a/plugins/rbzeitgeist/rbzeitgeist/__init__.py
+++ b/plugins/rbzeitgeist/rbzeitgeist/__init__.py
@@ -28,9 +28,9 @@
 
 import time
 import gobject
-import gio
 
 from gi.repository import RB
+from gi.repository import Gio
 
 from zeitgeist.client import ZeitgeistClient
 from zeitgeist.datamodel import Event, Subject, Interpretation, Manifestation
@@ -118,7 +118,7 @@ class ZeitgeistPlugin(RB.Plugin):
         else:
             manifest = Manifestation.SCHEDULED_ACTIVITY
 
-        def file_info_complete(obj, res, user_data = None):
+        def file_info_complete(obj, res, user_data):
             try:
                 fi = obj.query_info_finish(res)
             except:
@@ -143,8 +143,8 @@ class ZeitgeistPlugin(RB.Plugin):
             )
             IFACE.insert_event(event)
 
-        f = gio.File(song["location"])
-        f.query_info_async(gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, file_info_complete)
+        f = Gio.file_new_for_uri(song["location"])
+        f.query_info_async(Gio.FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, Gio.FileQueryInfoFlags.NONE, glib.PRIORITY_DEFAULT, None, file_info_complete)
 
     def deactivate(self, shell):
         print "Unloading Zeitgeist plugin..."



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