#coding=utf-8
from gerrit_notify import GerritNotify
from gi.repository import GObject,  Gtk
from os.path import expanduser
from ConfigParser import SafeConfigParser

class TrayiconPlugin (GObject.Object):
    notify = None
    def do_activate (self, notify):
        self.notify = notify
        self.staticon = Gtk.StatusIcon.new_from_file(self.notify.icon)
        self.staticon.connect ("activate", self.trayicon_activate)
        self.staticon.connect ("popup_menu", self.trayicon_popup)
        self.staticon.set_tooltip_markup("<span font_size='small'>Gerrit Notifier</span>")
        self.staticon.set_visible (True)

    def trayicon_activate (self, widget, data = None):
        pass

    def trayicon_quit (self, widget, data = None):
        Gtk.main_quit()

    def trayicon_popup (self, widget, button, time, data = None):
        self.menu = Gtk.Menu ()
        incoming_menu = Gtk.Menu ()
        open_menu = Gtk.Menu ()
        menuitem_quit = Gtk.MenuItem ("Quit")
        menuitem_incoming = Gtk.MenuItem ("Incoming changes")
        menuitem_open = Gtk.MenuItem ("Open changes")

        menuitem_incoming.set_submenu(incoming_menu)
        menuitem_open.set_submenu(open_menu)

        for c in self.notify.incoming_changes():
            menutitem_change = Gtk.MenuItem (str(c))
            incoming_menu.append(menutitem_change)

        for c in self.notify.open_changes():
            menutitem_change = Gtk.MenuItem (str(c))
            open_menu.append(menutitem_change)

        self.menu.append(menuitem_incoming)
        self.menu.append(menuitem_open)


        menuitem_quit.connect ("activate", self.trayicon_quit)

        self.menu.append (menuitem_quit)

        self.menu.show_all ()
        self.menu.popup(None, None, lambda w,x: self.staticon.position_menu(self.menu, self.staticon), self.staticon, 3, time)

    def do_deactivate (self):
        self.staticon.set_visible (False)
        del self.staticon

def main():
    default_config = {
            'url': 'http://update-your-settings.net',
            'username': None,
            'password': None
            }
    config = SafeConfigParser(default_config)
    p = expanduser('~/.config/gerrit-notify/config')
    config.readfp(open(p))

    notify = GerritNotify(
            config.get('server', 'url'),
            config.get('server', 'username'),
            config.get('server', 'password')
            )
    TrayiconPlugin().do_activate(notify)
    Gtk.main()


@@ -776,63 +776,57 @@
 def 
-trayicon_popup (self, widget, button, time, data = None
+populate_menuitem_submenu(self, menuitem, changes
 ):%0A 
@@ -828,29 +828,24 @@
 s):%0A        
-self.
 menu = Gtk.M
@@ -863,49 +863,328 @@
     
-incoming_menu = Gtk.Menu ()%0A        open_
+menuitem.set_submenu(menu)%0A        if len(changes) == 0:%0A            menuitem.set_sensitive(False)%0A            return%0A%0A        for c in changes:%0A            menutitem_change = Gtk.MenuItem (str(c))%0A            menu.append(menutitem_change)%0A%0A    def trayicon_popup (self, widget, button, time, data = None):%0A        self.
 menu
@@ -1361,24 +1361,25 @@
 nges%22)%0A%0A
+%0A
         
 menuitem
@@ -1374,115 +1374,26 @@
     
-menuitem_incoming.set_submenu(incoming_menu)%0A        menuitem_open.set_submenu(open_menu)%0A%0A        for c in
+incoming_changes =
  sel
@@ -1411,33 +1411,32 @@
 coming_changes()
-:
 %0A            men
@@ -1432,146 +1432,125 @@
     
-    menutitem
+open
 _change
+s
  = 
-Gtk.MenuItem (str(c))%0A            incoming_menu.append
+self.notify.open_changes()%0A%0A        self.populate_menuitem_submenu
 (menu
-t
 item_
-change)%0A%0A        for c in self.notify.open
+incoming, incoming
 _changes
 ():%0A
@@ -1549,21 +1549,18 @@
 nges
-():%0A 
+)%0A
         
    m
@@ -1559,97 +1559,66 @@
     
-   menutitem_change = Gtk.MenuItem (str(c))%0A            open_menu.append
+self.populate_menuitem_submenu
 (menu
-t
 item_
+open, open_
 change
+s
 )%0A%0A 

Disable menuitems containing empty submenus