#!/usr/bin/python
#
# wrapper for subscription Manager commandline tool.
#
# Copyright (c) 2010 Red Hat, Inc.
#
# Authors: Pradeep Kilambi
#
# This software is licensed to you under the GNU General Public License,
# version 2 (GPLv2). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv2
# along with this software; if not, see
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
#
# Red Hat trademarks are not licensed under GPLv2. No permission is
# granted to use or replicate Red Hat trademarks that are incorporated
# in this software or its documentation.
#


if __name__ != '__main__':
    raise ImportError("module cannot be imported")

import warnings
# this is a deprecation warning we see on rhel6, but the
# functionality doesn't seem to exist in public api yet
# Note that you do not see this on python2.7 systems as
# deprecationWarnings are ignored by default
warnings.filterwarnings(action="ignore",
                        category=DeprecationWarning,
                        message="^The dbus_bindings module is not public API and will go away soon.")

import sys
import os
import dbus
import dbus.service
import dbus.glib
import dbus.exceptions
import logging
import gettext
_ = gettext.gettext

try:
    import gtk
except RuntimeError, e:
    print _("Unable to open a display")
    sys.exit(1)

# Since the location of these def's keeps moving, hardcoded them here as per the spec
# see http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-names
REQUEST_NAME_REPLY_PRIMARY_OWNER = 1
REQUEST_NAME_REPLY_ALREADY_OWNER = 4

# To figure out if we got the bus name we wanted, and if not,
# why not, we need the results from request_name, and to
# see if it's return codes are REQUEST_NAME_REPLY_PRIMARY_OWNER
# or REQUEST_NAME_REPLY_ALREADY_OWNER.
#
# We have to support dbus-python ~ 0.7, from ~2005 for rhel5
# which require the use of "dbus.dbus_bindings".
# 'dbus_bindings' has been deprecated since ~2006  (~0.8.x)
# and were finally removed from the dbus-python bindings
# in rhel7. So for rhel5, we conditionally import dbus_bindings
# use bus_request_name, otherwise we use the newer bus.request_name


def _request_name_rhel5(bus, bus_name):
    return dbus.dbus_bindings.bus_request_name(bus.get_connection(), bus_name.get_name())


def _request_name(bus, bus_name):
    return bus.request_name(bus_name.get_name())


try:
    import dbus.dbus_bindings
    request_name = _request_name_rhel5
except ImportError, e:
    request_name = _request_name

gtk.gdk.threads_init()


def system_exit(code, msgs=None):
    "Exit with a code and optional message(s). Saved a few lines of code."

    if msgs:
        if type(msgs) not in [type([]), type(())]:
            msgs = (msgs, )
        for msg in msgs:
            sys.stderr.write(str(msg) + '\n')
    sys.exit(code)

BUS_NAME = "com.redhat.SubscriptionManagerGUI"
BUS_PATH = "/gui"

_LIBPATH = "/usr/share/rhsm"
# add to the path if need be
if _LIBPATH not in sys.path:
    sys.path.append(_LIBPATH)


# quick check to see if you are a super-user.
if os.getuid() != 0:
    sys.stderr.write('Error: must be root to execute\n')
    sys.exit(8)

try:
    # this has to be done first thing due to module level translated vars.
    from subscription_manager.i18n import configure_i18n
    configure_i18n(with_glade=True)

    from subscription_manager import logutil
    logutil.init_logger()

    from subscription_manager.injectioninit import init_dep_injection
    init_dep_injection()

    from subscription_manager.gui import managergui
    from subscription_manager.i18n_optparse import OptionParser, \
        WrappedIndentedHelpFormatter, USAGE
except ImportError, e:
    system_exit(2, "Unable to find Subscription Manager module.\n"
                  "Error: %s" % e)


class SubscriptionManagerService(dbus.service.Object):
    def __init__(self, window):
        self.window = window
        bus_name = dbus.service.BusName(BUS_NAME, bus=dbus.SessionBus())
        dbus.service.Object.__init__(self, bus_name, BUS_PATH)

    @dbus.service.method(dbus_interface=BUS_NAME)
    def show_window(self):
        self.window.present()


def already_running(bus):
    if bus is None:
        return False

    bus_name = dbus.service.BusName(BUS_NAME, bus=dbus.SessionBus())
    request_name_res = request_name(bus, bus_name)

    if bus and ((request_name_res != REQUEST_NAME_REPLY_PRIMARY_OWNER) and
       (request_name_res != REQUEST_NAME_REPLY_ALREADY_OWNER)):
        print _("subscription-manager-gui is already running")
        return True
    return False

if __name__ == '__main__':
    parser = OptionParser(usage=USAGE,
                          formatter=WrappedIndentedHelpFormatter())
    parser.add_option("--register", action='store_true',
                      help=_("launches the registration dialog on startup"))
    options, args = parser.parse_args(args=sys.argv)

    log = logging.getLogger("rhsm-app.subscription-manager-gui")

    try:
        bus = dbus.SessionBus()
    except dbus.exceptions.DBusException, e:
        log.debug("Enabled to connect to dbus SessionBus")
        log.exception(e)
        # Just ignore it if for some reason we can't find the session bus
        bus = None

    if already_running(bus):
        # Attempt to raise the running instance to the forefront
        try:
            remote_object = bus.get_object(BUS_NAME, BUS_PATH)
            remote_object.show_window(dbus_interface=BUS_NAME)
            log.debug("subscription-manager-gui already running, showing main window")
        except dbus.exceptions.DBusException, e:
            log.debug("Error attempting to show main window via dbus")
            log.debug("dbus remote_object with no show_window: %s" % remote_object)
            log.debug(e)
            # failed to raise the window, maybe we raced dbus?
            # fallback to opening a new window
        else:
            # we raised the existing window, we are done
            sys.exit()

    try:
        main = managergui.MainWindow(auto_launch_registration=options.register)

        # Hook into dbus service - only if it is available
        if bus:
            SubscriptionManagerService(main.main_window)

        # Exit the gtk loop when the window is closed
        main.main_window.connect('hide', gtk.main_quit)

        sys.exit(gtk.main() or 0)
    except SystemExit, e:
        # this is a non-exceptional exception thrown by Python 2.4, just
        # re-raise, bypassing handle_exception
        raise e
    except KeyboardInterrupt:
        system_exit(0, "\nUser interrupted process.")
    except Exception, e:
        log.exception(e)
        system_exit(1, e)
