File: appindicator.py

package info (click to toggle)
timekpr-next 0.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,440 kB
  • sloc: python: 7,938; sh: 98; xml: 39; makefile: 6
file content (126 lines) | stat: -rw-r--r-- 4,753 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""
Created on Aug 28, 2018

@author: mjasnik
"""

# import
import gi
import os
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

# timekpr imports
from timekpr.common.constants import constants as cons
from timekpr.common.log import log
from timekpr.client.interface.ui.notificationarea import timekprNotificationArea
from timekpr.common.constants import messages as msg

# indicator stuff
try:
    # try to load ayatanaappindicator (fully compatible with appindicator, I hope it stays that way)
    gi.require_version("AyatanaAppIndicator3", "0.1")
    from gi.repository import AyatanaAppIndicator3 as AppIndicator

    # if successful, mark it so
    _USE_INDICATOR = True
except (ImportError, ValueError):
    try:
        # try to load appindicator
        gi.require_version("AppIndicator3", "0.1")
        from gi.repository import AppIndicator3 as AppIndicator

        # if successful, mark it so
        _USE_INDICATOR = True
    except (ImportError, ValueError):
        # no indictor
        _USE_INDICATOR = False
        pass


class timekprIndicator(timekprNotificationArea):
    """Support appindicator"""

    def __init__(self, pUserName, pUserNameFull, pTimekprClientConfig):
        """Init all required stuff for indicator"""

        log.log(cons.TK_LOG_LEVEL_INFO, "start initTimekprIndicator")

        # only if this is supported
        if self.isSupported():
            # init parent as well
            super().__init__(pUserName, pUserNameFull, pTimekprClientConfig)

            # this is our icon
            self._indicator = None

        log.log(cons.TK_LOG_LEVEL_INFO, "finish initTimekprIndicator")

    def isSupported(self):
        """Get whether appindicator is supported"""
        global _USE_INDICATOR
        # returns whether we can use appindicator
        return _USE_INDICATOR

    def initTimekprIcon(self):
        """Initialize timekpr indicator"""
        log.log(cons.TK_LOG_LEVEL_INFO, "start initTimekprIndicatorIcon")

        # init indicator itself (icon will be set later)
        self._indicator = AppIndicator.Indicator.new("indicator-timekpr", os.path.join(self._timekprClientConfig.getTimekprSharedDir(), "icons", cons.TK_PRIO_CONF["client-logo"][cons.TK_ICON_STAT]), AppIndicator.IndicatorCategory.APPLICATION_STATUS)
        self._indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE)

        # define empty menu
        self._timekprMenu = Gtk.Menu()

        # add menu items
        self._timekprMenuItemTimeLeft = Gtk.MenuItem(msg.getTranslation("TK_MSG_MENU_TIME_LEFT"))
        self._timekprMenu.append(self._timekprMenuItemTimeLeft)
        self._timekprMenu.append(Gtk.SeparatorMenuItem())
        self._timekprMenuItemProperties = Gtk.MenuItem(msg.getTranslation("TK_MSG_MENU_CONFIGURATION"))
        self._timekprMenu.append(self._timekprMenuItemProperties)
        self._timekprMenu.append(Gtk.SeparatorMenuItem())
        self._timekprMenuItemAbout = Gtk.MenuItem(msg.getTranslation("TK_MSG_MENU_ABOUT"))
        self._timekprMenu.append(self._timekprMenuItemAbout)

        # enable all
        self._timekprMenu.show_all()

        # connect signal to code
        self._timekprMenuItemTimeLeft.connect("activate", super().invokeTimekprTimeLeft)
        self._timekprMenuItemProperties.connect("activate", super().invokeTimekprUserProperties)
        self._timekprMenuItemAbout.connect("activate", super().invokeTimekprAbout)

        # set menu to indicator
        self._indicator.set_menu(self._timekprMenu)

        # initial config
        self.setTimeLeft("", None, 0)

        log.log(cons.TK_LOG_LEVEL_INFO, "finish initTimekprIndicatorIcon")

    def setTimeLeft(self, pPriority, pTimeLeft, pTimeNotLimited, pPlayTimeLeft=None):
        """Set time left in the indicator"""
        # make strings to set
        timeLeftStr, icon = super().formatTimeLeft(pPriority, pTimeLeft, pTimeNotLimited, pPlayTimeLeft)

        # if we have smth to set
        if timeLeftStr is not None:
            # set time left (this works with indicator in unity and gnome)
            self._indicator.set_label(timeLeftStr, "")

            # set time left (this works with indicator in kde5)
            self._indicator.set_title(timeLeftStr)

        # if we have smth to set
        if icon is not None:
            # set up the icon
            self._indicator.set_icon(icon)

    def getTrayIconEnabled(self):
        """Get whether tray icon is enabled"""
        return self._indicator.get_status() == AppIndicator.IndicatorStatus.ACTIVE

    def setTrayIconEnabled(self, pEnabled):
        """Set whether tray icon is enabled"""
        self._indicator.set_status(AppIndicator.IndicatorStatus.ACTIVE if pEnabled else AppIndicator.IndicatorStatus.PASSIVE)