Commits

Vladimir Kolev committed 9fbdec8

Lyrics dialog is now in a thread, so the application will not hang.
Added new library for handling a lot of the gui stuff when in a thread.
Most of the messages should be moved here.

Comments (0)

Files changed (4)

data/lyrics_dialog.ui

   <object class="GtkDialog" id="dialog1">
     <property name="border_width">5</property>
     <property name="title" translatable="yes">Lyrics viewer</property>
+    <property name="modal">True</property>
     <property name="default_width">500</property>
     <property name="default_height">350</property>
     <property name="icon_name">audio-x-generic</property>
     <property name="type_hint">dialog</property>
+    <property name="skip_taskbar_hint">True</property>
+    <property name="skip_pager_hint">True</property>
     <child internal-child="vbox">
       <object class="GtkVBox" id="dialog-vbox1">
         <property name="visible">True</property>
 from lib.tfuncs import t_play
 from lib.tfuncs import t_search
 from lib.tfuncs import t_key_listener
-import lib.lyrdblib
+from lib.tfuncs import t_lyrics
+import lib.guihelpers
 import locale
 import gettext
 from gettext import gettext as _
                 pickle.dump(self.playlist_song(i), output)
             output.close()
         except:
-            self.show_error(_("Error saving the playlist"))
+            error = lib.guihelpers.ErrorMessage(None,
+                            _("Error saving the playlist"))
+            error.show_all()
 
 
     def save_as_playlist(self, widget, data=None):
         if select[1] != None:
             index = self.playlist.get_path(select[1])[0]
             song = self.playlist_song(index);
-            lyric = lib.lyrdblib.search(song['ArtistName'],
-                                         song['SongName'])
-            if "ERROR" in lyric:
-                self.show_error(lyric)
-            else:
-                builder = gtk.Builder()
-                builder.set_translation_domain(APP)
-                builder.add_from_file('%s/data/lyrics_dialog.ui' % BASEPATH)
-                lyrics = builder.get_object('dialog1')
-                songlabel = builder.get_object('label2')
-                songlabel.set_text(song['ArtistName'] + " - " + song['SongName'])
-                lyric_view = builder.get_object('textview1')
-                lyric_buff = lyric_view.get_buffer()
-                lyric_buff.set_text(lyric)
-                lyrics.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK)
-                lyrics.run()
-                lyrics.destroy()
+            t = t_lyrics('%s/data/lyrics_dialog.ui' % BASEPATH,
+                        song['ArtistName'], song['SongName'])
+            t.start()
+            #lyric = lib.lyrdblib.search(song['ArtistName'],
+            #                             song['SongName'])
 
     def show_about(self, widget, data=None):
         """
         element = self.downloads_view.get_parent().get_parent();
         element.show()
         self.downloads_view.set_visible(True)
-        
-    def show_error(self, error):
-        """
-        Displays an error message when error with groove.py
-        """
-        dialog = gtk.MessageDialog(None,
-                                  gtk.DIALOG_DESTROY_WITH_PARENT,
-                                  gtk.MESSAGE_ERROR,
-                                  gtk.BUTTONS_CLOSE, error)
-        dialog.run()
-        dialog.destroy()
 
     def get_stream_url(self):
         """

lib/guihelpers.py

+import gtk
+
+class ErrorMessage(gtk.MessageDialog):
+    """
+    Error Message helper. Displays an Error message whit given
+    text content. Used to be shown in Threads
+    """
+
+    def __init__(self, parent, message):
+        gtk.MessageDialog.__init__(self, parent,
+                            gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
+                            gtk.MESSAGE_ERROR, gtk.BUTTONS_OK, message)
+        self.set_default_response(gtk.RESPONSE_OK)
+        self.connect('response', self.handle_clicked)
+
+    def handle_clicked(self, *args):
+        """
+        Handler for the dedault dialog response.
+        """
+        self.destroy()
+
+
+class LyricsDialog:
+    """
+    Lyrics Dialog to be shown in a Thread. requieres the
+    .ui file, the name of the artist and song, and the
+    lyrics itself.
+    """
+
+    def __init__(self, _uifile, _artist, _song, _lyrics):
+        self.builder = gtk.Builder()
+        self.builder.add_from_file(_uifile)
+        self.lyrics = self.builder.get_object('dialog1')
+        self.songlabel = self.builder.get_object('label2')
+        self.lyrics_view = self.builder.get_object('textview1')
+        self.lyrics_buffer = self.lyrics_view.get_buffer()
+        self.lyrics.add_buttons(gtk.STOCK_OK, gtk.RESPONSE_OK)
+        self.lyrics.set_default_response(gtk.RESPONSE_OK)
+        self.lyrics.connect('response', self.handle_clicked)
+
+        self.songlabel.set_text(_artist + " - " + _song)
+        self.lyrics_buffer.set_text(_lyrics)
+
+    def show_all(self):
+        """
+        Wrap the default function of show_all for a dialog
+        """
+        self.lyrics.show_all()
+
+    def handle_clicked(self, *args):
+        """
+        Handler for the default dialog response
+        """
+        self.lyrics.destroy()
 import time
 import threading
 import groove
+import lyrdblib
+import guihelpers
 
 try:
     import pygst
         try:
             self.file = open(self.filename, "w")
         except IOError:
-            self.win.show_error(_("Failed to create '%s' for writing.") % self.filename)
+            error = guihelpers.ErrorMessage(None,
+                _("Failed to create '%s' for writing.") % self.filename)
+            error.show_all()
             raise
         
         self.song = _song
                                     item['AlbumName'],
                                     item['SongID'],
                                     item])
+
+class t_lyrics(threading.Thread):
+
+    def __init__(self, _uifile, _artist, _song):
+
+        threading.Thread.__init__(self)
+        self.ui = _uifile
+        self.artist = _artist
+        self.song = _song
+
+    def run(self):
+        lyrics = lyrdblib.search(self.artist, self.song)
+        if "ERROR:" in lyrics:
+            error = guihelpers.ErrorMessage(None,
+                lyrics)
+            error.show_all()
+        else:
+            lyrdiag = guihelpers.LyricsDialog(self.ui,
+                                              self.artist,
+                                              self.song,
+                                              lyrics)
+            lyrdiag.show_all()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.