aboutsummaryrefslogtreecommitdiffstats
path: root/src/gtk-redshift/rsappindicator.py
blob: 0caebc170df6c5da70fa027905741f0937022c9f (plain) (blame)
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
#!/usr/bin/env python
# rsappindicator.py -- Application Panel Indicator source
# This file is part of Redshift.

# Redshift 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.

# Redshift 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 Redshift.  If not, see <http://www.gnu.org/licenses/>.

# Copyright (c) 2010  Jon Lund Steffensen <jonlst@gmail.com>


import sys, os
import subprocess, signal
import gettext

import pygtk
pygtk.require("2.0")

import gtk, glib
import appindicator

import defs


def run():
    # Internationalisation
    gettext.bindtextdomain('redshift', defs.LOCALEDIR)
    gettext.textdomain('redshift')
    _ = gettext.gettext

    # Start redshift with arguments from the command line
    args = sys.argv[1:]
    args.insert(0, os.path.join(defs.BINDIR, 'redshift'))
    process = subprocess.Popen(args)

    # Create status icon
    indicator = appindicator.Indicator ("redshift",
                          "redshift",
                          appindicator.CATEGORY_APPLICATION_STATUS)
    indicator.set_status (appindicator.STATUS_ACTIVE)

    def toggle_cb(widget, data=None):
        if indicator.get_icon() == 'redshift':
            indicator.set_icon('redshift-idle')
        else:
            indicator.set_icon('redshift')
        process.send_signal(signal.SIGUSR1)

    def destroy_cb(widget, data=None):
        gtk.main_quit()
        return False

    # Create popup menu
    status_menu = gtk.Menu()

    toggle_item = gtk.ImageMenuItem(_('Toggle'))
    toggle_item.connect('activate', toggle_cb)
    status_menu.append(toggle_item)

    quit_item = gtk.ImageMenuItem(gtk.STOCK_QUIT)
    quit_item.connect('activate', destroy_cb)
    status_menu.append(quit_item)

    status_menu.show_all()

    # Set the menu
    indicator.set_menu(status_menu)

    def child_cb(pid, cond, data=None):
        sys.exit(-1)

    # Add watch on child process
    glib.child_watch_add(process.pid, child_cb)

    try:
        # Run main loop
        gtk.main()

    except KeyboardInterrupt:
        # Ignore user interruption
        pass

    finally:
        # Always terminate redshift
        process.terminate()
        process.wait()