#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 ()

        menuitem_quit = Gtk.MenuItem ("Quit")
        for c in self.notify.open_changes():
            menutitem_change = Gtk.MenuItem (str(c))
            self.menu.append(menutitem_change)

        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()


@@ -866,54 +866,337 @@
  ()%0A
-%0A        menuitem_quit = Gtk.MenuItem (%22Quit%22)
+        incoming_menu = Gtk.Menu ()%0A        open_menu = Gtk.Menu ()%0A        menuitem_quit = Gtk.MenuItem (%22Quit%22)%0A        menuitem_incoming = Gtk.MenuItem (%22Incoming changes%22)%0A        menuitem_open = Gtk.MenuItem (%22Open changes%22)%0A%0A        menuitem_incoming.set_submenu(incoming_menu)%0A        menuitem_open.set_submenu(open_menu)%0A
 %0A   
@@ -1221,20 +1221,24 @@
 .notify.
-open
+incoming
 _changes
@@ -1310,42 +1310,278 @@
     
-self.menu.append(menutitem_change)
+incoming_menu.append(menutitem_change)%0A%0A        for c in self.notify.open_changes():%0A            menutitem_change = Gtk.MenuItem (str(c))%0A            open_menu.append(menutitem_change)%0A%0A        self.menu.append(menuitem_incoming)%0A        self.menu.append(menuitem_open)%0A
 %0A%0A  

Tidy up the menu