[rhythmbox] Port lyrics plugin to GI



commit 03d2c2d6142a7e9e361070308885cacb902b36a1
Author: Jonathan Matthew <jonathan d14n org>
Date:   Sat Feb 5 16:05:43 2011 +1000

    Port lyrics plugin to GI
    
    Configuration doesn't work, but it doesn't really matter because
    none of the sites work anyway.

 plugins/lyrics/lyrics/LyricsConfigureDialog.py |   40 ++++++------
 plugins/lyrics/lyrics/LyricsParse.py           |   10 +--
 plugins/lyrics/lyrics/__init__.py              |   88 +++++++++++------------
 plugins/rb/__init__.py                         |   16 ++++
 4 files changed, 81 insertions(+), 73 deletions(-)
---
diff --git a/plugins/lyrics/lyrics/LyricsConfigureDialog.py b/plugins/lyrics/lyrics/LyricsConfigureDialog.py
index 1a5877d..487bf14 100644
--- a/plugins/lyrics/lyrics/LyricsConfigureDialog.py
+++ b/plugins/lyrics/lyrics/LyricsConfigureDialog.py
@@ -27,18 +27,20 @@
 
 from LyricsSites import lyrics_sites
 
-import gobject, gtk
-import gconf
+import gobject
 from os import system, path
 
+import rb
+from gi.repository import Gtk, GConf
+
 class LyricsConfigureDialog (object):
 	def __init__(self, builder_file, gconf_keys):
-		self.gconf = gconf.client_get_default()
+		self.gconf = GConf.Client.get_default()
 		self.gconf_keys = gconf_keys
 
-		builder = gtk.Builder()
+		builder = Gtk.Builder()
 		builder.add_from_file(builder_file)
-			
+
 		self.dialog = builder.get_object("preferences_dialog")
 
 		self.choose_button = builder.get_object("choose_button")
@@ -58,18 +60,18 @@ class LyricsConfigureDialog (object):
 		self.site_checks = {}
 		for s in lyrics_sites:
 			site_id = s['id']
-			checkbutton = gtk.CheckButton(label = s['name'])
+			checkbutton = Gtk.CheckButton(label = s['name'])
 			checkbutton.set_active(s['id'] in engines)
 			self.site_checks[site_id] = checkbutton
-			site_box.pack_start(checkbutton)
+			site_box.pack_start(checkbutton, True, True, 0)
 
 		site_box.show_all()
 
 	def dialog_response(self, dialog, response):
-		if response == gtk.RESPONSE_OK:
+		if response == Gtk.ResponseType.OK:
 			self.set_values()
 			self.dialog.hide()
-		elif response == gtk.RESPONSE_CANCEL or response == gtk.RESPONSE_DELETE_EVENT:
+		elif response == Gtk.ResponseType.CANCEL or response == Gtk.ResponseType.DELETE_EVENT:
 			self.dialog.hide()
 		else:
 			print "unexpected response type"
@@ -88,23 +90,24 @@ class LyricsConfigureDialog (object):
 		if len(self.path_display.get_text()) is not 0:
 			self.folder = self.path_display.get_text()
 
-		self.gconf.set_list(self.gconf_keys['engines'], gconf.VALUE_STRING, engines)
+		# XXX can't set gconf string lists; doesn't matter, none of the sites work anyway
+		# self.gconf.set_list(self.gconf_keys['engines'], GConf.ValueType.STRING, engines)
 		self.gconf.set_string(self.gconf_keys['folder'], self.folder)
 
 	def choose_callback(self, widget):
 		def response_handler(widget, response):
-			if response == gtk.RESPONSE_OK:
+			if response == Gtk.ResponseType.OK:
 				path = self.chooser.get_filename()
 				self.chooser.destroy()
 				self.path_display.set_text(path)
 			else:
 				self.chooser.destroy()
 
-		buttons = (gtk.STOCK_CLOSE, gtk.RESPONSE_CLOSE,
-				gtk.STOCK_OK, gtk.RESPONSE_OK)
-		self.chooser = gtk.FileChooserDialog(title=_("Choose lyrics folder..."),
+		buttons = (Gtk.STOCK_CLOSE, Gtk.ResponseTypeCLOSE,
+				Gtk.STOCK_OK, Gtk.ResponseType.OK)
+		self.chooser = Gtk.FileChooserDialog(title=_("Choose lyrics folder..."),
 					parent=None,
-					action=gtk.FILE_CHOOSER_ACTION_SELECT_FOLDER,
+					action=Gtk.FileChooserAction.SELECT_FOLDER,
 					buttons=buttons)
 		self.chooser.connect("response", response_handler)
 		self.chooser.set_modal(True)
@@ -116,15 +119,12 @@ class LyricsConfigureDialog (object):
 	
 	def get_prefs (self):
 		try:
-			engines = gconf.client_get_default().get_list(self.gconf_keys['engines'], gconf.VALUE_STRING)
-			if engines is None:
-				engines = []
+			engines = rb.get_gconf_string_list(self.gconf_keys['engines'])
 		except gobject.GError, e:
 			print e
 			engines = []
-		folder = gconf.client_get_default().get_string(self.gconf_keys['folder'])
+		folder = GConf.Client.get_default().get_string(self.gconf_keys['folder'])
 
 		print "lyric engines: " + str (engines)
 		print "lyric folder: " + folder
 		return (engines, folder)
-
diff --git a/plugins/lyrics/lyrics/LyricsParse.py b/plugins/lyrics/lyrics/LyricsParse.py
index 1636b72..a17a785 100644
--- a/plugins/lyrics/lyrics/LyricsParse.py
+++ b/plugins/lyrics/lyrics/LyricsParse.py
@@ -24,12 +24,12 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 
-
 import urllib
 import re
 import gobject
-import gconf
+
 import rb
+from gi.repository import GConf
 
 from LyricsSites import lyrics_sites
 
@@ -39,9 +39,7 @@ class Parser (object):
 		self.artist = artist
 
 		try:
-			self.engines = gconf.client_get_default().get_list(gconf_keys['engines'], gconf.VALUE_STRING)
-			if self.engines is None:
-				self.engines = []
+			self.engines = rb.get_gconf_string_list(gconf_keys['engines'])
 		except gobject.GError, e:
 			print e
 			self.engines = []
@@ -68,5 +66,3 @@ class Parser (object):
 
 	def get_lyrics(self, callback, *data):
 		rb.Coroutine (self.searcher, callback, *data).begin ()
-
-
diff --git a/plugins/lyrics/lyrics/__init__.py b/plugins/lyrics/lyrics/__init__.py
index 49f4372..2baee09 100644
--- a/plugins/lyrics/lyrics/__init__.py
+++ b/plugins/lyrics/lyrics/__init__.py
@@ -25,14 +25,12 @@
 # You should have received a copy of the GNU General Public License
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
-#
-# TODO:
-# - check that the lyrics returned even remotely match the request?
 
 import os, re
-import gtk
-import gconf
-import rhythmdb, rb
+
+import rb
+from gi.repository import Gtk, GConf
+from gi.repository import RB
 
 import LyricsParse
 from LyricsConfigureDialog import LyricsConfigureDialog
@@ -56,21 +54,20 @@ gconf_keys = {	'engines' : '/apps/rhythmbox/plugins/lyrics/engines',
 		'folder': '/apps/rhythmbox/plugins/lyrics/folder'
 	     }
 
-
 def create_lyrics_view():
-	tview = gtk.TextView()
-	tview.set_wrap_mode(gtk.WRAP_WORD)
+	tview = Gtk.TextView()
+	tview.set_wrap_mode(Gtk.WrapMode.WORD)
 	tview.set_editable(False)
 	tview.set_left_margin(6)
 
 	tview.set_size_request (0, 0)
-	sw = gtk.ScrolledWindow()
+	sw = Gtk.ScrolledWindow()
 	sw.add(tview)
-	sw.set_policy(gtk.POLICY_NEVER, gtk.POLICY_AUTOMATIC)
-	sw.set_shadow_type(gtk.SHADOW_IN)
+	sw.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC)
+	sw.set_shadow_type(Gtk.ShadowType.IN)
 
-	vbox = gtk.VBox(spacing=12)
-	vbox.pack_start(sw, expand=True)
+	vbox = Gtk.VBox(spacing=12)
+	vbox.pack_start(sw, True, True, 0)
 	
 	return (vbox, tview.get_buffer(), tview)
 
@@ -108,8 +105,8 @@ def get_artist_and_title(db, entry):
 	if stream_song_title is not None:
 		(artist, title) = extract_artist_and_title(stream_song_title)
 	else:
-		artist = db.entry_get(entry, rhythmdb.PROP_ARTIST)
-		title = db.entry_get(entry, rhythmdb.PROP_TITLE)
+		artist = entry.get_string(RB.RhythmDBPropType.ARTIST)
+		title = entry.get_string(RB.RhythmDBPropType.TITLE)
 	return (artist, title)
 
 def extract_artist_and_title(stream_song_title):
@@ -128,9 +125,9 @@ def extract_artist_and_title(stream_song_title):
 	return (artist, title)
 	
 def build_cache_path(artist, title):
-	folder = gconf.client_get_default().get_string(gconf_keys['folder'])
+	folder = GConf.Client.get_default().get_string(gconf_keys['folder'])
 	if folder is None or folder == "":
-		folder = os.path.join(rb.user_cache_dir(), "lyrics")
+		folder = os.path.join(RB.user_cache_dir(), "lyrics")
 
 	lyrics_folder = os.path.expanduser (folder)
 	if not os.path.exists (lyrics_folder):
@@ -226,23 +223,23 @@ class LyricPane(object):
 			self.get_lyrics()
 	   
 
-		self.edit = gtk.ToggleButton(_("_Edit"))
+		self.edit = Gtk.ToggleButton(label=_("_Edit"))
 		self.edit.connect('toggled', edit_callback)
-		self.discard = gtk.Button(_("_Search again"))
+		self.discard = Gtk.Button(label=_("_Search again"))
 		self.discard.connect('clicked', discard_callback)
-		self.clear = gtk.Button(stock=gtk.STOCK_CLEAR)
+		self.clear = Gtk.Button.new_from_stock(Gtk.STOCK_CLEAR)
 		self.clear.connect('clicked', clear_callback)
-		self.hbox = gtk.HButtonBox()
+		self.hbox = Gtk.HButtonBox()
 		self.hbox.set_spacing (6)
-		self.hbox.set_layout(gtk.BUTTONBOX_END)
+		self.hbox.set_layout(Gtk.ButtonBoxStyle.END)
 		self.hbox.add(self.edit)
 		self.hbox.add(self.clear)
 		self.hbox.add(self.discard)
-		self.hbox.set_child_secondary (self.clear, is_secondary=True)
+		self.hbox.set_child_secondary (self.clear, True)
 
 		(self.view, self.buffer, self.tview) = create_lyrics_view()
 
-		self.view.pack_start(self.hbox, expand=False, fill=False, padding=6)
+		self.view.pack_start(self.hbox, False, False, 6)
 		self.view.set_spacing(2)
 	
 		self.view.show_all()
@@ -280,33 +277,33 @@ class LyricPane(object):
 		self.get_lyrics()
 
 	def __got_lyrics(self, text):
-		self.buffer.set_text(text)
+		self.buffer.set_text(str(text), -1)
 
 	def get_lyrics(self):
 		if self.entry is None:
 			return
 
-		self.buffer.set_text(_("Searching for lyrics..."));
+		self.buffer.set_text(_("Searching for lyrics..."), -1);
 		lyrics_grabber = LyricGrabber(self.db, self.entry)
 		lyrics_grabber.search_lyrics(self.__got_lyrics)
 
 
-class LyricWindow (gtk.Window):
+class LyricWindow (Gtk.Window):
 
 	def __init__(self, shell):
-		gtk.Window.__init__(self)
+		Gtk.Window.__init__(self)
 		self.shell = shell
 		self.set_border_width(12)
 
-		close = gtk.Button(stock=gtk.STOCK_CLOSE)
+		close = Gtk.Button.new_from_stock(Gtk.STOCK_CLOSE)
 		close.connect('clicked', lambda w: self.destroy())
 	
 		(lyrics_view, buffer, tview) = create_lyrics_view()
 		self.buffer = buffer
-		bbox = gtk.HButtonBox()
-		bbox.set_layout(gtk.BUTTONBOX_END)
-		bbox.pack_start(close)
-		lyrics_view.pack_start(bbox, expand=False)
+		bbox = Gtk.HButtonBox()
+		bbox.set_layout(Gtk.ButtonBoxStyle.END)
+		bbox.pack_start(close, True, True, 0)
+		lyrics_view.pack_start(bbox, False, False, 0)
 
 		sp = shell.get_player ()
 		self.ppc_id = sp.connect('playing-song-property-changed', self.playing_property_changed)
@@ -318,7 +315,7 @@ class LyricWindow (gtk.Window):
 	def destroy(self):
 		sp = self.shell.get_player ()
 		sp.disconnect (self.ppc_id)
-		gtk.Window.destroy(self)
+		Gtk.Window.destroy(self)
 
 	def playing_property_changed(self, player, uri, prop, old_val, new_val):
 		if (prop == STREAM_SONG_TITLE):
@@ -326,7 +323,7 @@ class LyricWindow (gtk.Window):
 
 	
 	def __got_lyrics(self, text):
-		self.buffer.set_text (text)
+		self.buffer.set_text (text, -1)
 
 	def update_song_lyrics(self, entry):
 		db = self.shell.props.db
@@ -336,20 +333,20 @@ class LyricWindow (gtk.Window):
 		lyrics_grabber.search_lyrics(self.__got_lyrics)
 
 
-class LyricsDisplayPlugin(rb.Plugin):
+class LyricsDisplayPlugin(RB.Plugin):
 
 	def __init__ (self):
-		rb.Plugin.__init__ (self)
+		RB.Plugin.__init__ (self)
 		self.window = None
 
 	def activate (self, shell):
 		self.shell = shell
-		self.action = gtk.Action ('ViewSongLyrics', _('Song L_yrics'),
-					  _('Display lyrics for the playing song'),
-					  'rb-song-lyrics')
+		self.action = Gtk.Action (name='ViewSongLyrics', label=_('Song L_yrics'),
+					  tooltip=_('Display lyrics for the playing song'),
+					  stock_id='rb-song-lyrics')
 		self.activate_id = self.action.connect ('activate', self.show_song_lyrics, shell)
 		
-		self.action_group = gtk.ActionGroup ('SongLyricsPluginActions')
+		self.action_group = Gtk.ActionGroup (name='SongLyricsPluginActions')
 		self.action_group.add_action_with_accel (self.action, "<control>L")
 		
 		uim = shell.get_ui_manager ()
@@ -386,10 +383,9 @@ class LyricsDisplayPlugin(rb.Plugin):
 			self.window.destroy ()
 			self.window = None
 
-	def create_configure_dialog(self, dialog=None):
-		if not dialog:
-			builder_file = self.find_file("lyrics-prefs.ui")
-			dialog = LyricsConfigureDialog (builder_file, gconf_keys).get_dialog()
+	def create_configure_dialog(self):
+		builder_file = self.find_file("lyrics-prefs.ui")
+		dialog = LyricsConfigureDialog (builder_file, gconf_keys).get_dialog()
 		dialog.present()
 		return dialog
 
diff --git a/plugins/rb/__init__.py b/plugins/rb/__init__.py
index 92893f0..3a4c550 100644
--- a/plugins/rb/__init__.py
+++ b/plugins/rb/__init__.py
@@ -33,6 +33,7 @@ import time
 import thread
 
 from gi.repository import RB
+from gi.repository import GConf
 
 # rb classes
 from Loader import Loader
@@ -61,6 +62,21 @@ def append_plugin_source_path(theme, iconpath):
 		theme.append_search_path(icondir)
 
 
+def get_gconf_string_list(key):
+	gconf = GConf.Client().get_default()
+	l = gconf.get_without_default(key)
+	if l is None or \
+	    l.type != GConf.ValueType.LIST or \
+	    l.get_list_type() != GConf.ValueType.STRING:
+		return []
+	sl = []
+	for e in l.get_list():
+		sl.append(e.get_string())
+
+	return sl
+
+
+
 class _rbdebugfile:
 	def __init__(self, fn):
 		self.fn = fn



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