Commits

Damián Nohales committed e535920

Refactoring the status icon manager class.

  • Participants
  • Parent commits 1ef82dc

Comments (0)

Files changed (2)

File lib/SharkDown.py

         
         # Main window and widgets initialization
         self.window = builder.get_object('mainwindow')
+        self.window.connect("delete-event", self.window.hide_on_delete)
         self.mainmenu = builder.get_object('mainmenu')
         
         self.button_play = builder.get_object('button_play')
             self.play_first_song()
         opendlg.destroy()
 
-    def on_window_close(self, widget, data = None):
-        """
-        Closes the main window
-        """
-        self.window.hide_on_delete()
-        self.windowstate = 0
-        return True
-
     def check_for_updates(self):
         """
         Checks if a new version of the application is available
     def on_check_for_updates(self, widget, data = None):
         self.check_for_updates()
 
-    def on_statusicon_clicked(self, widget, data = None):
-        """
-        Executed on left clicking the statusicon
-        """
-        if(self.windowstate == 0):
-            self.window.show_all()
-            self.windowstate = 1
-        else:
-            self.window.hide_on_delete()
-            self.windowstate = 0
-            return True
-
     def on_quit_app(self, widget, data = None):
         """
         Confirmation dialog when exiting the application
         if song != None:
             self.is_buffering = True
 
-            play_stop_image = gtk.Image()
-            play_stop_image.set_from_stock(gtk.STOCK_MEDIA_STOP, gtk.ICON_SIZE_MENU)
-            self.staticon.change_playbutton(play_stop_image)
             self.staticon.change_status_playing()
-            
             self.button_play.set_image(self.widget_image_buffering)
             
             self.button_songinfo.set_sensitive(True)
                 self.player_state_timeout_id = None
 
             self.staticon.change_status_stopped()
-            play_stop_image = gtk.Image()
-            play_stop_image.set_from_stock(gtk.STOCK_MEDIA_PLAY, gtk.ICON_SIZE_MENU)
-            self.staticon.change_playbutton(play_stop_image)
-            
             self.button_play.set_image(self.widget_image_play)
             self.songinfo.set_markup("")
             

File lib/guihelpers.py

     AppIndicator/StatusIcon for gSharkDown
     """
 
-    def __init__(self, instance):
+    def __init__(self, app):
         """
-        instance parameter is the main windows class for gSharkDown,
+        app parameter is the main windows class for gSharkDown,
         so we can access the gSharkDown methods.
         """
+        self.app = app
+        self.menu = gtk.Menu()
+        self.program_changing_show_window = False
+        
+        self.show_window = gtk.CheckMenuItem(_("Show gSharkDown"))
+        self.show_window.connect("toggled", self.on_statusicon_clicked)
+        
+        about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
+        about.connect("activate", self.app.on_show_about)
+        prefs = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
+        prefs.connect("activate", self.app.on_open_preferences)
+        self.playbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
+        self.playbut.connect("activate", self.app.on_play_selected)
+        prebut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PREVIOUS)
+        prebut.connect("activate", self.app.on_play_previous)
+        ffbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_NEXT)
+        ffbut.connect("activate", self.app.on_play_next)
+
+        updimg = gtk.Image()
+        updimg.set_from_icon_name('system-software-update',
+                                 gtk.ICON_SIZE_MENU)
+        updbut = gtk.ImageMenuItem(_("Check for update"))
+        updbut.set_image(updimg)
+        updbut.connect("activate", self.app.on_check_for_updates)
+        quiter = gtk.ImageMenuItem(gtk.STOCK_QUIT)
+        quiter.connect("activate", self.app.on_quit_app)
+        
+        self.menu.append(self.show_window)
+        self.menu.append(gtk.SeparatorMenuItem())
+        self.menu.append(prebut)
+        self.menu.append(self.playbut)
+        self.menu.append(ffbut)
+        self.menu.append(gtk.SeparatorMenuItem())
+        self.menu.append(prefs)
+        self.menu.append(updbut)
+        self.menu.append(about)
+        self.menu.append(quiter)
+        
         if env().HAVE_INDICATOR == True:
-            #if os.path.exists('/usr/share/pixmaps/gsharkdown'):
-            #    icons_path = '/usr/share/pixmaps/gsharkdown'
-            #else:
-            #    icons_path = "%s/_pixmaps/gsharkdown" % env().BASEPATH
-            #print icons_path
             self.ind = appindicator.Indicator("gsharkdown-client",
                                    "gsharkdown_16",
                                     appindicator.CATEGORY_APPLICATION_STATUS)
             self.ind.set_attention_icon("gsharkdown_16_playing")
             self.ind.set_icon("gsharkdown_16")
 
-            # Create the menu for the appindicator
-            menu = gtk.Menu()
-
-            # New menu entry to handle show/hide main window
-            show_window = gtk.CheckMenuItem(_("Hide gSharkDown"))
-            show_window.connect("toggled", instance.on_statusicon_clicked)
-
-            about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
-            about.connect("activate", instance.on_show_about)
-            prefs = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
-            prefs.connect("activate", instance.on_open_preferences)
-            self.playbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
-            self.playbut.connect("activate", instance.on_play_selected)
-            prebut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PREVIOUS)
-            prebut.connect("activate", instance.on_play_previous)
-            ffbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_NEXT)
-            ffbut.connect("activate", instance.on_play_next)
-
-            updimg = gtk.Image()
-            updimg.set_from_icon_name('system-software-update',
-                                     gtk.ICON_SIZE_MENU)
-            updbut = gtk.ImageMenuItem(_("Check for update"))
-            updbut.set_image(updimg)
-            updbut.connect("activate", instance.on_check_for_updates)
-            quiter = gtk.ImageMenuItem(gtk.STOCK_QUIT)
-            quiter.connect("activate", instance.on_quit_app)
-
-            menu.append(show_window)
-            menu.append(gtk.SeparatorMenuItem())
-            menu.append(prebut)
-            menu.append(self.playbut)
-            menu.append(ffbut)
-            menu.append(gtk.SeparatorMenuItem())
-            menu.append(about)
-            menu.append(updbut)
-            menu.append(prefs)
-            menu.append(gtk.SeparatorMenuItem())
-            menu.append(quiter)
-            menu.show_all()
-
-            self.ind.set_menu(menu)
+            self.menu.show_all()
+            self.ind.set_menu(self.menu)
         else:
-            self.instance = instance
             self.staticon = gtk.StatusIcon()
             self.staticon.set_from_file("%s/data/gsharkdown_16.png" % env().BASEPATH)
             self.staticon.connect("popup-menu", self.right_click_event)
             self.staticon.connect("activate", self.on_statusicon_clicked)
-            self.staticon.set_tooltip(_("gSharkDown :: Player and Downloader"))
+            self.staticon.set_tooltip(_("gSharkDown"))
+            
+        app.window.connect("hide", self.on_window_state_changed)
+        app.window.connect("show", self.on_window_state_changed)
 
     def right_click_event(self, icon, button, time):
         '''
         Handler for the right click event on the StatIcon
         Not usable for the AppIndicator
         '''
-        menu = gtk.Menu()
-
-        about = gtk.ImageMenuItem(gtk.STOCK_ABOUT)
-        about.connect("activate", self.instance.on_show_about)
-        prefs = gtk.ImageMenuItem(gtk.STOCK_PREFERENCES)
-        prefs.connect("activate", self.instance.on_open_preferences)
-        playbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PLAY)
-        playbut.connect("activate", self.instance.on_play_selected)
-        prebut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_PREVIOUS)
-        prebut.connect("activate", self.instance.on_play_previous)
-        ffbut = gtk.ImageMenuItem(gtk.STOCK_MEDIA_NEXT)
-        ffbut.connect("activate", self.instance.on_play_next)
-
-        updimg = gtk.Image()
-        updimg.set_from_icon_name('system-software-update',
-                                 gtk.ICON_SIZE_MENU)
-        updbut = gtk.ImageMenuItem(_("Check for update"))
-        updbut.set_image(updimg)
-        updbut.connect("activate", self.instance.on_check_for_updates)
-        quiter = gtk.ImageMenuItem(gtk.STOCK_QUIT)
-        quiter.connect("activate", self.instance.on_quit_app)
-
-        menu.append(prebut)
-        menu.append(playbut)
-        menu.append(ffbut)
-        menu.append(gtk.SeparatorMenuItem())
-        menu.append(about)
-        menu.append(updbut)
-        menu.append(prefs)
-        menu.append(gtk.SeparatorMenuItem())
-        menu.append(quiter)
-        menu.show_all()
-
-        menu.popup(None, None, gtk.status_icon_position_menu,
+        self.menu.show_all()
+        self.menu.popup(None, None, gtk.status_icon_position_menu,
                   button, time, icon)
 
     def on_statusicon_clicked(self, widget):
-        """
-        Handler for a click event on the StatIcon
-        Not usable for the AppIndicator
-        """
-        return self.instance.on_statusicon_clicked(widget)
+        if self.program_changing_show_window == False:
+            if self.app.window.get_visible():
+                self.app.window.hide()
+            else:
+                self.app.window.present()
+    
+    def on_window_state_changed(self, widget):
+        self.program_changing_show_window = True
+        self.show_window.set_active( self.app.window.get_visible() )
+        self.program_changing_show_window = False
 
     def change_status_playing(self):
         # Method to change the gSharkDown icon when playing
         if env().HAVE_INDICATOR == True:
             self.ind.set_status(appindicator.STATUS_ATTENTION)
         else:
-            self.staticon.set_from_file("%s/data/gsharkdown_16.png" % env().BASEPATH)
+            self.staticon.set_from_file("%s/data/gsharkdown_16_playing.png" % env().BASEPATH)
+        
+        self.__change_playbutton_content(gtk.STOCK_MEDIA_STOP, _("Stop"))
 
     def change_status_stopped(self):
         # Method to change the gSharkDown icon to default
         if env().HAVE_INDICATOR == True:
             self.ind.set_status(appindicator.STATUS_ACTIVE)
         else:
-            self.staticon.set_from_file("%s/data/gsharkdown_16_playing.png" % env().BASEPATH)
-
-    def change_playbutton(self, image):
-        if env().HAVE_INDICATOR == True:
-            self.playbut.set_image(image)
+            self.staticon.set_from_file("%s/data/gsharkdown_16.png" % env().BASEPATH)
+            
+        self.__change_playbutton_content(gtk.STOCK_MEDIA_PLAY, _("Play"))
+        
+    def __change_playbutton_content(self, icon_id, label):
+        image = gtk.Image()
+        image.set_from_stock(icon_id, gtk.ICON_SIZE_MENU)
+        self.playbut.set_image(image)
+        self.playbut.set_label(label)
+