#!/usr/bin/python

# Ubuntu Tweak - PyGTK based desktop configuration tool
#
# Copyright (C) 2007-2010 TualatriX <tualatrix@gmail.com>
#
# Ubuntu Tweak 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 2 of the License, or
# (at your option) any later version.
#
# Ubuntu Tweak 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 Ubuntu Tweak; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA

import sys
import dbus
import dbus.service
import dbus.mainloop.glib

from gi.repository import GObject, Notify

GObject.threads_init()
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
dbus.mainloop.glib.threads_init()

from ubuntutweak.gui import GuiBuilder
from ubuntutweak.policykit.dbusproxy import proxy
from ubuntutweak.gui.dialogs import InfoDialog

UTURL_DBUS_IFACE = 'com.ubuntu_tweak.UtUrl'
UTURL_DBUS_PATH = '/com/ubuntu_tweak/UtUrl'

class UtUrl(dbus.service.Object):
    to_add = []

    def __init__(self, session_bus):
        bus_name = dbus.service.BusName(UTURL_DBUS_IFACE, bus=session_bus)
        dbus.service.Object.__init__(self, bus_name, UTURL_DBUS_PATH)

        self.window = GuiBuilder('uturl.ui')
        self.window.confirmation_dialog.set_keep_above(True)
        self.window.header_label.set_markup('<big>Install additional software?</big>')

    @dbus.service.method(dbus_interface=UTURL_DBUS_IFACE,
                         in_signature='s', out_signature='')
    def add_package(self, package):
        self.to_add.append(package)
        self.show_notify('"%s" is adding to the install list.' % package)

    @dbus.service.method(dbus_interface=UTURL_DBUS_IFACE,
                         in_signature='s', out_signature='')
    def show_notify(self, message):
        notify = Notify.Notification()
        notify.update(summary=_('Ubuntu Tweak'),
                      body=message,
                      icon='ubuntu-tweak')
        notify.set_hint_string ("x-canonical-append", "");
        notify.show()

    def run(self):
        self.window.body_label.set_text('Do you want to install package "%s"?' % 
                                 ', '.join(self.to_add))
        self.window.confirmation_dialog.run()

    def destroy(self):
        self.window.confirmation_dialog.destroy()

if __name__ == "__main__":
    package = sys.argv[1].split(':')[1]

    installed = proxy.get_package_status(package)

    session_bus = dbus.SessionBus()

    if session_bus.request_name(UTURL_DBUS_IFACE) != \
            dbus.bus.REQUEST_NAME_REPLY_PRIMARY_OWNER:
        uturl = session_bus.get_object(UTURL_DBUS_IFACE, UTURL_DBUS_PATH)
        if installed:
            uturl.show_notify('"%s" is already installed.' % package)
        else:
            uturl.add_package(package)
    else:
        uturl = UtUrl(session_bus)

        if installed:
            dialog = InfoDialog('"%s" is already installed.' % package)
            dialog.launch()
        else:
            uturl.add_package(package)
            response = uturl.run()
            uturl.destroy()
