#!/usr/bin/python

# Winki the Ripper - GTK frontend for mencoder, mplayer and lsdvd
# Copyright (C) 2004-2007 Velko Hristov
#
# 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 3 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""
This is the main entry point for winki.
The main() function creates an instance of the WinkiTheRipper class
and starts the gtk event loop.
"""

try:
    import pygtk
    pygtk.require('2.0')
except:
    print 'Sorry. Your PyGTK installation does not seem to be correct'
    raise SystemExit

try:
    import gtk
except:
    print 'Unsupported PyGTK version. Please consider an upgrade to PyGTK 2'
    raise SystemExit

if gtk.pygtk_version < (2,4,0):
    # TreeRowReference is available only in PyGTK 2.4 and higher
    # egg.trayicon too
    print 'PyGTK 2.4.0 or later is required for running winki'
    raise SystemExit

from os import nice
from winkirip.druid import Druid
from winkirip.project import Project
from gnome import program_init

sys_tray = True
try:
    from egg.trayicon import TrayIcon
except:
    sys_tray = False



class WinkiTheRipper:
    def __init__(self):
        self.__druid = None
        self.__project = None
        self.__tray_icon = None
        self.__tooltips = None
        return

    # -- get/set --

    def get_project(self):
        return self.__project

    def set_tray_icon(self, img_name):
        if not self.__tray_icon:
            return

        img = self.__tray_icon.get_child().get_child()
        img.set_from_icon_name(img_name, gtk.ICON_SIZE_SMALL_TOOLBAR)
        self.__tray_icon.show_all()
        return

    def set_tooltip(self, text):
        if not self.__tray_icon:
            return

        eventbox = self.__tray_icon.get_child()
        self.__tooltips.set_tip(eventbox, text)
        return

    # -- public interface --

    def init(self):
        progname = 'winki'
        progversion = '0.4.5'
        program_init(progname, progversion)

        self.__project = Project()
        self.__druid = Druid(self)

        self.__setup_trayicon()

        nice_value = self.__project.get_config().get_nice_value()
        nice(nice_value)

        print "%s %s" % (progname , progversion)
        return

    def end(self):
        gtk.main_quit()
        return 0

    def cleanup(self):
        self.end()
        return

    def has_tray(self):
        return (self.__tray_icon != None)

    # -- signal handlers --

    def on_tray_action(self, event_box, event):
        # left button click: show/hide application window
        if event.button == 1:
            self.__druid.on_show_hide()

        # right button click: show menu
        elif event.button == 3:
            self.__druid.on_show_tray_menu(event_box, event)
            #print event.x_root
            #print event.y_root
            #print event.get_coords()
            #print event.get_root_coords()
            #print event.get_screen()
            #print event.x
            #print event.y
        return True

    # -- helpers --

    def __setup_trayicon(self):
        if not sys_tray:
            return

        self.__tray_icon = TrayIcon("Winki")
        eventbox = gtk.EventBox()
        eventbox.set_events(gtk.gdk.BUTTON_PRESS_MASK)
        eventbox.connect("button-press-event", self.on_tray_action)
        self.__tray_icon.add(eventbox)
        eventbox.add(gtk.image_new_from_icon_name('winki', gtk.ICON_SIZE_SMALL_TOOLBAR))

        self.__tooltips = gtk.Tooltips()
        self.__tooltips.set_tip(eventbox, _('Winki the ripper'))

        self.__tray_icon.show_all()
        return


def main():
    app = WinkiTheRipper()
    app.init()

    gtk.main()
    return 0

main()

# vim:number:autoindent:shiftwidth=4:tabstop=4
