#!/usr/bin/python3
# -*- coding: UTF-8 -*-

# 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# Authors: Quinn Storm (quinn@beryl-project.org)
#          Patrick Niklaus (patrick.niklaus@student.kit.edu)
# Copyright (C) 2007 Quinn Storm

from optparse import OptionParser
import gi
gi.require_version('Pango', '1.0')
gi.require_version('PangoCairo', '1.0')
gi.require_version('Rsvg', '2.0')
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gio, Gtk, Gdk
if Gtk.check_version(3, 0, 0) is None:
    gi.require_version('GdkX11', '3.0')
    from gi.repository import GdkX11
import os
import sys
import signal
import compizconfig
import ccm
from ccm.Utils import GlobalUpdater
from ccm.Constants import Version, CurrentScreenNum

signal.signal(signal.SIGINT, signal.SIG_DFL)

def activate_event(application, mainWinArgs):
    if Gtk.check_version(3, 6, 0) is None:
        prevWin = application.get_active_window()
    else:
        try:
            prevWin = application.__mainWin
        except AttributeError:
            prevWin = None

    if prevWin:
        prevWin.present()
        return

    mainWin = ccm.MainWin(*mainWinArgs)
    if Gtk.check_version(3, 6, 0) is None:
        application.add_window(mainWin)
    else:
        application.__mainWin = mainWin
    idle = ccm.IdleSettingsParser(context, mainWin)
    mainWin.show_all()
    if Gtk.check_version(3, 6, 0) is not None:
        Gtk.main()

plugin   = None
category = None
parser   = OptionParser()
parser.add_option("-p", "--plugin", dest = "plugin",
                  help = "Directly jump to the page of PLUGIN",
                  metavar = "PLUGIN")
parser.add_option("-c", "--category", dest = "category",
                  help = "Directly jump to CATEGORY",
                  metavar = "CATEGORY")
parser.add_option("-v", "--version", dest = "version",
                  action = "store_true",
                  help = "Version")
(options, args) = parser.parse_args()
if options.version:
    print("CCSM " + Version)
    sys.exit(0)
if options.plugin:
    plugin = options.plugin
if options.category:
    category = options.category

# Compiz 0.9.x and Compiz 0.8.x compatibility.
try:
    context = compizconfig.Context(CurrentScreenNum)
except (AttributeError, TypeError):
    context = compizconfig.Context(ccm.getScreens())
GlobalUpdater.SetContext(context)

if Gtk.check_version(3, 6, 0) is None:
    application = Gtk.Application(application_id="org.compiz.ccsm",
                                  flags=Gio.ApplicationFlags.FLAGS_NONE)
else:
    application = Gio.Application(application_id="org.compiz.ccsm",
                                  flags=Gio.ApplicationFlags.FLAGS_NONE)
application.connect("activate", activate_event, (context, plugin, category))
application.run(sys.argv)
