#!/usr/bin/python
#
# Copyright 2011 Alberto Milone <albertomilone@gmail.com>
#
# Author: Alberto Milone <albertomilone@gmail.com>
#
# This program is largely based on applet.py from the Hamster project.
#
# 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, see <http://www.gnu.org/licenses/>.
#

import gobject
import gtk
import appindicator
import gconf

import logging
import datetime as dt
import os.path

import pygtk
pygtk.require("2.0")

import dbus, dbus.service, dbus.mainloop.glib
import locale

from hamster.configuration import conf, runtime, dialogs

try:
    from hamster import stuff
    from hamster import i18n
except ImportError:
    from hamster.lib import stuff
    from hamster.lib import i18n

# controllers for other windows
from hamster import widgets
from hamster import idle

from hamster.applet import HamsterApplet

import pango

try:
    import wnck
except:
    logging.warning("Could not import wnck - workspace tracking will be disabled")
    wnck = None

try:
    import pynotify
    pynotify.init('Hamster Applet')
except:
    logging.warning("Could not import pynotify - notifications will be disabled")
    pynotify = None



class FakeApplet(object):
    '''Fake Applet class to trick HamsterApplet'''

    def __init__(self):
        pass

    def add(self, *args):
        pass

    def setup_menu_from_file(self, *args):
        pass


class HamsterIndicator(HamsterApplet):
    BASE_KEY = "/apps/hamster-indicator"
    def __init__(self, applet=None):
        # Gconf settings
        self._settings = gconf.client_get_default()
        self._settings.add_dir(self.BASE_KEY, gconf.CLIENT_PRELOAD_NONE)
        # Key to enable/disable icon glow
        self._icon_glow_path = os.path.join(self.BASE_KEY, "icon_glow")
        self._use_icon_glow = self._settings.get_bool(self._icon_glow_path)
        self._settings.notify_add(self._icon_glow_path, self._on_icon_glow_changed)

        # Key to show/hide the indicator label
        self._show_label_path = os.path.join(self.BASE_KEY, "show_label")
        self._show_label = self._settings.get_bool(self._show_label_path)
        self._settings.notify_add(self._show_label_path, self._on_show_label_changed)

        # Key to set the length of indicator label
        self._label_length_path = os.path.join(self.BASE_KEY, "label_length")
        self._label_length = self._settings.get_int(self._label_length_path)
        self._settings.notify_add(self._label_length_path, self._on_label_length_changed)

        self._activity_as_attribute = None
        # Create a fake applet since HamsterApplet requires one
        applet = FakeApplet()

        self.indicator = appindicator.Indicator ("hamster-applet",
                                  "hamster-applet-inactive",
                                  appindicator.CATEGORY_SYSTEM_SERVICES)

        self.indicator.set_status (appindicator.STATUS_ACTIVE)
        # Set the attention icon as per the icon_glow gconf key
        self._set_attention_icon()

        # Initialise the activity label with "No Activity"
        self.indicator.set_label(self._get_no_activity_label())

        self.activity, self.duration = None, None

        self.menu = gtk.Menu()
        self.activity_item = gtk.MenuItem("")
        self.menu.append(self.activity_item)
        # this is where you would connect your menu item up with a function:
        self.activity_item.connect("activate", self.on_new_activity_activated, None)
        self.activity_label = self.activity_item.get_child()
        self.activity_label.connect('style-set', self.on_label_style_set)

        # show the items
        self.activity_item.show()

        self.stop_activity_item = gtk.MenuItem(_(u"Sto_p Tracking"))
        self.menu.append(self.stop_activity_item)
        # this is where you would connect your menu item up with a function:
        self.stop_activity_item.connect("activate", self.on_stop_activity_activated, None)
        # show the items
        self.stop_activity_item.show()

        self.append_separator(self.menu)

        self.earlier_activity_item = gtk.MenuItem(_(u"Add earlier activity"))
        self.menu.append(self.earlier_activity_item)
        # this is where you would connect your menu item up with a function:
        self.earlier_activity_item.connect("activate", self.on_earlier_activity_activated, None)
        # show the items
        self.earlier_activity_item.show()

        self.overview_show_item = gtk.MenuItem(_(u"Show _Overview"))
        self.menu.append(self.overview_show_item)
        # this is where you would connect your menu item up with a function:
        self.overview_show_item.connect("activate", self.on_overview_show_activated, None)
        # show the items
        self.overview_show_item.show()

        self.append_separator(self.menu)

        self.preferences_show_item = gtk.MenuItem(_(u"Preferences"))
        self.menu.append(self.preferences_show_item)
        # this is where you would connect your menu item up with a function:
        self.preferences_show_item.connect("activate", self.on_show_preferences_activated, None)
        # show the items
        self.preferences_show_item.show()

        self.append_separator(self.menu)

        self.quit_item = gtk.MenuItem(_(u"_Quit"))
        self.menu.append(self.quit_item)
        # this is where you would connect your menu item up with a function:
        self.quit_item.connect("activate", gtk.main_quit, None)
        # show the items
        self.quit_item.show()


        # Call constructor after the gtk.Menu is ready
        super(HamsterIndicator, self).__init__(applet)

        # Hide the panel button since it's not supported
        self.button.hide()

        self.window.set_title(_(u"Time Tracker"))

        # Add a window decoration
        self.window.set_decorated(True)

        # Place the window near the mouse cursor
        self.window.set_position(gtk.WIN_POS_MOUSE)

        # Do not skip the taskbar
        self.window.set_skip_taskbar_hint(False)

        # Do not skip the pager
        self.window.set_skip_pager_hint(False)


    def on_new_activity_activated(self, *args):
        self.show_dialog()

    def on_stop_activity_activated(self, *args):
        runtime.storage.stop_tracking()
        self.last_activity = None

    def on_show_preferences_activated(self, *args):
        dialogs.prefs.show(self.indicator)

    def on_overview_show_activated(self, *args):
        dialogs.overview.show(self.indicator)

    def on_earlier_activity_activated(self, *args):
        dialogs.edit.show(self.indicator)

    def refresh_menu(self):
        '''Update the menu so that the new activity text is visible'''
        self.indicator.set_menu(self.menu)

    def reformat_label(self):
        '''This adds a method which belongs to hamster.applet.PanelButton'''
        label = self.activity
        if self.duration:
            label = "%s %s" % (self.activity, self.duration)
        label = '<span gravity="south">%s</span>' % label
        if self.activity_label:
            self.activity_label.set_markup("") #clear - seems to fix the warning
            self.activity_label.set_markup(label)

    def on_label_style_set(self, widget, something):
        self.reformat_label()

    def append_separator(self, menu):
        '''Add separator to the menu'''
        separator = gtk.SeparatorMenuItem()
        separator.show()
        menu.append(separator)

    def update_label(self):
        '''Override for menu items sensitivity and to update the menu'''
        if self.last_activity:
            # Let's see if activity is an attribute and cache the result.
            # This is only required for backwards compatibility
            if self._activity_as_attribute == None:
                self._activity_as_attribute = hasattr(self.last_activity,
                                                      'activity')
            if self._activity_as_attribute:
                start_time = self.last_activity.start_time
                end_time = self.last_activity.end_time
                last_activity_name = self.last_activity.activity
            else:
                start_time = self.last_activity['start_time']
                end_time = self.last_activity['end_time']
                last_activity_name = self.last_activity['name']

        if self.last_activity and end_time is None:
            self._set_activity_status(1)
            delta = dt.datetime.now() - start_time
            duration = delta.seconds /  60
            label = "%s %s" % (last_activity_name,
                               stuff.format_duration(duration, False))
            self.set_activity_text(last_activity_name,
                                 stuff.format_duration(duration, False))
            indicator_label = "%s %s" % (self._clamp_text(self.activity,
                                         length=self._label_length,
                                         with_ellipsis=False),
                                         self.duration)
        else:
            self._set_activity_status(0)
            label = "%s" % _(u"New activity")
            self.set_activity_text(label, None)
            indicator_label = self._get_no_activity_label()

        # Update the indicator label, if needed
        if self._show_label:
            self.indicator.set_label(indicator_label)
        else:
            self.indicator.set_label("")

        # Update the menu or the new activity text won't show up
        self.refresh_menu()

    def _clamp_text(self, text, length=25, with_ellipsis=True, is_indicator=False):
        text = stuff.escape_pango(text)
        if len(text) > length:  #ellipsize at some random length
            if with_ellipsis:
                if is_indicator:
                    text = "%s%s" % (text[:length], "...")
                else:
                    text = "%s%s" % (text[:length], "&#8230;")
            else:
                text = "%s" % (text[:length])
        return text

    def _set_activity_status(self, is_active):
        if is_active:
            # There's an active task
            self.indicator.set_status (appindicator.STATUS_ATTENTION)
        else:
            # There's no active task
            self.indicator.set_status (appindicator.STATUS_ACTIVE)
        self.stop_activity_item.set_sensitive(is_active)

    def _set_attention_icon(self):
        '''Set the attention icon as per the gconf key'''
        if self._use_icon_glow:
            self.indicator.set_attention_icon('hamster-applet-active')
        else:
            self.indicator.set_attention_icon('hamster-applet-inactive')

    def _on_icon_glow_changed(self, client, connection_id, entry, *args):
        self._use_icon_glow = self._settings.get_bool(self.BASE_KEY + "/icon_glow")
        self._set_attention_icon()

    def _on_show_label_changed(self, client, connection_id, entry, *args):
        '''Hide or show the indicator's label'''
        self._show_label = self._settings.get_bool(self._show_label_path)
        if self._show_label:
            self.update_label()
        else:
            self.indicator.set_label("")

    def _on_label_length_changed(self, client, connection_id, entry, *args):
        '''Resize the indicator's label'''
        self._label_length = self._settings.get_int(self._label_length_path)
        if self._show_label:
            self.update_label()

    def _get_no_activity_label(self):
        '''Get the indicator label set to "No activity"'''
        return self._clamp_text(_(u"No activity"),
                                length=self._label_length,
                                with_ellipsis=False)

    def position_popup(self):
        '''Override the superclass method and do nothing'''
        pass

    def on_window_size_request(self, *args):
        '''Override the superclass method and do nothing'''
        pass

    def set_last_activity(self):
        '''Override to change the Stop button sensitivity'''
        #self._set_activity_status(self.last_activity != None)
        super(HamsterIndicator, self).set_last_activity()

    def on_stop_tracking_clicked(self, widget):
        '''Override to make the Stop button insensitive'''
        self._set_activity_status(0)
        super(HamsterIndicator, self).on_stop_tracking_clicked(widget)

    def on_switch_activity_clicked(self, widget):
        '''Override to make the Stop button sensitive'''
        self._set_activity_status(1)
        super(HamsterIndicator, self).on_switch_activity_clicked(widget)

    def set_activity_text(self, activity, duration):
        '''This adds a method which belongs to hamster.applet.PanelButton'''
#        activity = stuff.escape_pango(activity)
#        if len(activity) > 25:  #ellipsize at some random length
#            activity = "%s%s" % (activity[:25], "&#8230;")
        activity = self._clamp_text(activity)

        self.activity = activity
        self.duration = duration
        self.reformat_label()

    def show_dialog(self, is_active=True):
        """Show new task window"""
        self.button.set_active(is_active)

        if is_active == False:
            self.window.hide()
            return True

#        # doing unstick / stick here, because sometimes while switching
#        # between workplaces window still manages to disappear
#        self.window.unstick()
#        self.window.stick() #show on all desktops

        self.new_name.set_text("");
        self.new_tags.set_text("");
        gobject.idle_add(self._delayed_display)



if __name__ == "__main__":
    i18n.setup_i18n()
    hamster_indicator = HamsterIndicator()
#    ind.set_menu(hamster_indicator.menu)

    gtk.main()
