#!/usr/bin/python
# -*- coding: utf-8 -*-
### BEGIN LICENSE
# Copyright (C) 2010 Leszek Lesner leszek@zevenos.com
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, 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/>.
### END LICENSE

import sys
import os
import gtk

import gettext
from gettext import gettext as _
gettext.textdomain('pde-control-center')

# optional Launchpad integration
# this shouldn't crash if not found as it is simply used for bug reporting
try:
    import LaunchpadIntegration
    launchpad_available = True
except:
    launchpad_available = False

# Add project root directory (enable symlink, and trunk execution).
PROJECT_ROOT_DIRECTORY = os.path.abspath(
    os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0]))))

if (os.path.exists(os.path.join(PROJECT_ROOT_DIRECTORY, 'pde_control_center'))
    and PROJECT_ROOT_DIRECTORY not in sys.path):
    sys.path.insert(0, PROJECT_ROOT_DIRECTORY)
    os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses

#from pde_control_center import (
#    AboutPDEControlCenterDialog, PreferencesPDEControlCenterDialog)
from pde_control_center.helpers import get_builder


class PDEControlCenterWindow(gtk.Window):
    __gtype_name__ = "PDEControlCenterWindow"
    
    # To construct a new instance of this method, the following notable 
    # methods are called in this order:
    # __new__(cls)
    # __init__(self)
    # finish_initializing(self, builder)
    # __init__(self)
    #
    # For this reason, it's recommended you leave __init__ empty and put
    # your inialization code in finish_intializing
    
    def __new__(cls):
        """Special static method that's automatically called by Python when 
        constructing a new instance of this class.
        
        Returns a fully instantiated PDEControlCenterWindow object.
        """
        builder = get_builder('PDEControlCenterWindow')
        new_object = builder.get_object("pde_control_center_window")
        new_object.finish_initializing(builder)
        return new_object

    def finish_initializing(self, builder):
        """Called while initializing this instance in __new__

        finish_initalizing should be called after parsing the UI definition
        and creating a PDEControlCenterWindow object with it in order to finish
        initializing the start of the new PDEControlCenterWindow instance.
        
        Put your initilization code in here and leave __init__ undefined.
        """
        # Get a reference to the builder and set up the signals.
        self.builder = builder
        self.builder.connect_signals(self)

        global launchpad_available
        if launchpad_available:
            # see https://wiki.ubuntu.com/UbuntuDevelopment/Internationalisation/Coding for more information
            # about LaunchpadIntegration
            helpmenu = self.builder.get_object('helpMenu')
            if helpmenu:
                LaunchpadIntegration.set_sourcepackagename('pde-control-center')
                LaunchpadIntegration.add_items(helpmenu, 0, False, True)
            else:
                launchpad_available = False

        # Uncomment the following code to read in preferences at start up.
        #dlg = PreferencesPDEControlCenterDialog.NewPreferencesPDEControlCenterDialog()
        #self.preferences = dlg.get_preferences()

        # Code for other initialization actions should be added here.

    def about(self, widget, data=None):
        """Display the about box for pde-control-center."""
        about = AboutPDEControlCenterDialog.AboutPDEControlCenterDialog()
        response = about.run()
        about.destroy()

    def preferences(self, widget, data=None):
        """Display the preferences window for pde-control-center."""
        prefs = PreferencesPDEControlCenterDialog.PreferencesPDEControlCenterDialog()
        response = prefs.run()
        if response == gtk.RESPONSE_OK:
            # Make any updates based on changed preferences here.
            pass
        prefs.destroy()

    def on_panelevent_button_press_event(self,widget, data=None):
        """Open up panel configuration"""
        os.popen("xfpanel-switch &")

    def on_lookevent_button_press_event(self,widget, data=None):
        """Open up the look and feel configuration"""
        os.popen("lxappearance &")

    def on_compizconfig_button_press_event(self,widget, data=None):
        """Open up the look and feel configuration"""
        os.popen("ccsm &")

    def on_languageevent_button_press_event(self,widget, data=None):
        """Open up the language preferences"""
        os.popen("gnome-language-selector -n &")

    def on_backgroundevent_button_press_event(self,widget, data=None):
        """Open up the background preferences here"""
        os.popen("pcmanfm --desktop-pref &")

    def on_openboxevent_button_press_event(self,widget, data=None):
        """Open up openbox configuration here"""
        os.popen("obconf &")

    def on_printerevent_button_press_event(self,widget, data=None):
        """Open up printer configuration here"""
        os.popen("system-config-printer &")

    def on_screensaverevent_button_press_event(self,widget, data=None):
        """Open up screensaver settings here"""
        os.popen("xscreensaver-demo &")

    def on_automountevent_button_press_event(self,widget, data=None):
        """Open up automount settings here"""
        os.popen("pcmanfm --show-pref=3 &")

    def on_monitorevent_button_press_event(self,widget, data=None):
        """Open up Monitor / Screen preferences here"""
        os.popen("lxrandr &")

    def on_soundevent_button_press_event(self,widget, data=None):
        """Open up sound mixer here"""
        os.popen("pavucontrol &")

    def on_keyboardevent_button_press_event(self,widget, data=None):
        """Open up keyboard and mouse settings here"""
        os.popen("lxinput &")

    def on_driverevent_button_press_event(self,widget, data=None):
        """Open up jockey gtk here"""
        os.popen("pearldrivers &")

    def on_networkevent_button_press_event(self,widget, data=None):
        """Open up network manager configuration here"""
        os.popen("nm-connection-editor &")

    def on_shareevent_button_press_event(self,widget, data=None):
        """Open up shares-admin here"""
        os.popen("system-config-samba &")

    def on_proxyevent_button_press_event(self,widget, data=None):
        """Open up proxy settings here"""
        os.popen("lxproxy &")

    def on_usersevent_button_press_event(self,widget, data=None):
        """Open up users-admin here"""
        os.popen("pearl-usermanager &")

    def on_timeevent_button_press_event(self,widget, data=None):
        """Open up Time Admin here"""
        os.popen("time-admin &")

    def on_autotartevent_button_press_event(self,widget, data=None):
        """Open up lxsession editor here"""
        os.popen("lxsession-edit &")

    def on_updateevent_button_press_event(self,widget, data=None):
        """Open up update-manager here"""
        os.popen("pearlupdate &")

    def quit(self, widget, data=None):
        """Signal handler for closing the PDEControlCenterWindow."""
        self.destroy()

    def on_destroy(self, widget, data=None):
        """Called when the PDEControlCenterWindow is closed."""
        # Clean up code for saving application state should be added here.
        gtk.main_quit()

if __name__ == "__main__":
    # Support for command line options.
    import logging
    import optparse
    parser = optparse.OptionParser(version="%prog %ver")
    parser.add_option(
        "-v", "--verbose", action="store_true", dest="verbose",
        help=_("Show debug messages"))
    (options, args) = parser.parse_args()

    # Set the logging level to show debug messages.
    if options.verbose:
        logging.basicConfig(level=logging.DEBUG)
        logging.debug('logging enabled')

    # Run the application.
    window = PDEControlCenterWindow()
    window.show()
    gtk.main()
