#!/usr/bin/env python

#    gRefer is a Bibliographic Management System that uses Google Docs
#    as shared storage.
#
#    Copyright (C) 2011  NigelB
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

import dbus
from _dbus_glib_bindings import DBusGMainLoop
import os
from gRefer.plugins import register_plugin
from gRefer.process.constants import GREF_RDF_PROCESSOR
from gRefer.process.interfaces import GREF_RDF_Processor
from gRefer.conv.internal.rdf import ns
from gRefer.utils import fix_filename_for_system

dbus_loop = DBusGMainLoop(set_as_default=True)


import pygtk
pygtk.require("2.0")
import gtk
from gRefer.gdbus import DBusProvider

class Send_Title_To_Clipboard(GREF_RDF_Processor):

    def __init__(self, *args, **kwargs):
        GREF_RDF_Processor.__init__(self, *args, **kwargs)
        self.clip = gtk.Clipboard()

    def process(self, id, rdf_dict):
        self.clip.set_text(fix_filename_for_system(rdf_dict[id][ns.terms['title']][0]))

def run_systray():
    register_plugin("Send_Title_To_Clipboard", GREF_RDF_PROCESSOR, __name__, Send_Title_To_Clipboard.__name__)

    ICON_FILE = "/usr/share/pixmaps/grefer/icons/bulb.png"
    DEV_ICON_FILE = "icons/bulb.png"
    if not os.path.exists(ICON_FILE):
        if os.path.exists(DEV_ICON_FILE):
           ICON_FILE = DEV_ICON_FILE
        else:
            raise Exception("Count not find Icon.")

    def process_clipboard(*args, **kwargs):
        print a.process_clipboard()

    def editor(*args, **kwargs):pass

    def do_exit(*args, **kwargs):
        gtk.main_quit()

    def popm(icon, button, time):
        menu.popup(None, None, None, button, time, icon)
        menu.show()



    menu = gtk.Menu()
    items=[
        ["Process Clipboard",{"activate":process_clipboard}, None],
        ["Editor",{"activate":editor}, None],
        None,
        ["Quit", {"activate":do_exit}, gtk.STOCK_QUIT]
    ]

    for item in items:
        if not item:
            sep = gtk.SeparatorMenuItem()
            sep.show()
            menu.append(sep)
            continue

        if item[2] is None:
            mitem = gtk.MenuItem(item[0])
        else:
            mitem = gtk.ImageMenuItem(item[2])
            if item[0]: mitem.set_label(item[0])

        for fun in item[1]:
            mitem.connect(fun, item[1][fun])
            mitem.show()
        menu.append(mitem)

    si=gtk.StatusIcon()
    si.set_from_file(ICON_FILE)
    si.connect("popup-menu", popm)
    si.connect("activate", process_clipboard)

    bus = dbus.SessionBus(mainloop=dbus_loop)

    a = DBusProvider(session_bus=bus)
    gtk.main()
