#!/usr/bin/env python

# Copyright (C) 2006 by Aiwota Programmer
# aiwotaprog@tetteke.tk
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import pygtk
pygtk.require('2.0')
import gtk
import gnome.ui
import gobject
import getopt
import sys
import dbus
import dbus.service
import os.path

from FukuiNoNamari import config
from FukuiNoNamari import thread_window
from FukuiNoNamari import board_window
from FukuiNoNamari import dbus_object
from FukuiNoNamari import uri_opener
from FukuiNoNamari import session
from FukuiNoNamari import bookmark_list

# where are these values ?
DBUS_NAME_FLAG_ALLOW_REPLACEMENT = 1
DBUS_NAME_FLAG_REPLACE_EXISTING = 2
DBUS_NAME_FLAG_DO_NOT_QUEUE = 4
DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER = 1
DBUS_REQUEST_NAME_REPLY_IN_QUEUE = 2
DBUS_REQUEST_NAME_REPLY_EXISTS = 3
DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER = 4

dbus_bus_name = "tk.tetteke.FukuiNoNamari"
dbus_object_path = "/tk/tetteke/FukuiNoNamari"
dbus_object.dbus_interface_name = "tk.tetteke.FukuiNoNamari"

def usage():
    print """usage: hage1 uri"""

def getoption():
    try:
        opts, args = getopt.getopt(
            sys.argv[1:], "h", ["help"])
    except getopt.GetoptError, msg:
        print msg
        usage()
        sys.exit(2)
    for option, value in opts:
        if option in ("-h", "--help"):
            usage()
            sys.exit()
    return args

    
if __name__ == "__main__":

    # get option
    uris = getoption()

    # check if an instance exists
    session_bus = dbus.SessionBus()
    dbus_proxy_object = session_bus.get_object(
        "org.freedesktop.DBus", "/org/freedesktop/DBus")
    dbus_iface = dbus.Interface(dbus_proxy_object, "org.freedesktop.DBus")
    req_name_ret = dbus_iface.RequestName(
        dbus_bus_name, DBUS_NAME_FLAG_DO_NOT_QUEUE)

    if req_name_ret == DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER or \
       req_name_ret == DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
        # the instance does not exist. start normally.

        bus_name = dbus.service.BusName(dbus_bus_name, bus=session_bus)
        obj = dbus_object.DBusFukuiNoNamariObject(bus_name, dbus_object_path)

        gnome.init(config.APPNAME, config.APPVERSION)
        gtk.gdk.threads_init()

        bookmark_path = os.path.join(config.get_config_dir_path(),
                                     "bookmarks.txt")
        bookmark_list.init(bookmark_path, "http://menu.2ch.net/bbsmenu.html")

        # open some windows
        if uris:
            from FukuiNoNamari.BbsType import bbs_type_judge_uri
            from FukuiNoNamari.BbsType import bbs_type_exception
            for uri in uris:
                try:
                    uri_opener.open_uri(uri)
                except bbs_type_exception.BbsTypeError, msg:
                    print msg

        session.start()

    else:
        # the instance exists. send message and exit.

        proxy_object = session_bus.get_object(dbus_bus_name, dbus_object_path)
        iface = dbus.Interface(proxy_object, dbus_object.dbus_interface_name)

        # open some windows
        if uris:
            from FukuiNoNamari.BbsType import bbs_type_judge_uri
            from FukuiNoNamari.BbsType import bbs_type_exception
            for uri in uris:
                try:
                    iface.open_uri(uri)
                except bbs_type_exception.BbsTypeError, msg:
                    print msg
