Пример #1
0
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)

    try:
        if appindicator:
            # Create indicator
            indicator = appindicator.Indicator('redshift',
                                               'redshift-status-on',
                                               appindicator.CATEGORY_APPLICATION_STATUS)
            indicator.set_status(appindicator.STATUS_ACTIVE)
        else:
            # Create status icon
            status_icon = gtk.StatusIcon()
            status_icon.set_from_icon_name('redshift-status-on')
            status_icon.set_tooltip('Redshift')

        def toggle_cb(widget, data=None):
            process.send_signal(signal.SIGUSR1)
            if appindicator:
                if indicator.get_icon() == 'redshift-status-on':
                    indicator.set_icon('redshift-status-off')
                else:
                    indicator.set_icon('redshift-status-on')
            else:
	            if status_icon.get_icon_name() == 'redshift-status-on':
                        status_icon.set_from_icon_name('redshift-status-off')
	            else:
                        status_icon.set_from_icon_name('redshift-status-on')

        def autostart_cb(widget, data=None):
            utils.set_autostart(widget.get_active())

        def destroy_cb(widget, data=None):
            if not appindicator:
                status_icon.set_visible(False)
            gtk.main_quit()
            return False

        # Create popup menu
        status_menu = gtk.Menu()

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

        autostart_item = gtk.CheckMenuItem(_('Autostart'))
        try:
            autostart_item.set_active(utils.get_autostart())
        except IOError as strerror:
            print strerror
            autostart_item.set_property('sensitive', False)
        else:
            autostart_item.connect('activate', autostart_cb)
        finally:
            status_menu.append(autostart_item)

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

        if appindicator:
            status_menu.show_all()

            # Set the menu
            indicator.set_menu(status_menu)
        else:
            def popup_menu_cb(widget, button, time, data=None):
                status_menu.show_all()
                status_menu.popup(None, None, gtk.status_icon_position_menu,
                                  button, time, status_icon)

            # Connect signals for status icon and show
            status_icon.connect('activate', toggle_cb)
            status_icon.connect('popup-menu', popup_menu_cb)
            status_icon.set_visible(True)

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

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

        # Run main loop
        gtk.main()

    except KeyboardInterrupt:
        # Ignore user interruption
        pass

    finally:
        # Always terminate redshift
        process.terminate()
        process.wait()
Пример #2
0
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)

    try:
        if appindicator:
            # Create indicator
            indicator = appindicator.Indicator('redshift',
                                               'redshift-status-on',
                                               appindicator.CATEGORY_APPLICATION_STATUS)
            indicator.set_status(appindicator.STATUS_ACTIVE)
        else:
            # Create status icon
            status_icon = gtk.StatusIcon()
            status_icon.set_from_icon_name('redshift-status-on')
            status_icon.set_tooltip('Redshift')

        def is_enabled():
            if appindicator:
                return indicator.get_icon() == 'redshift-status-on'
            else:
                return status_icon.get_icon_name() == 'redshift-status-on'

        def remove_suspend_timer():
            global SUSPEND_TIMER
            if SUSPEND_TIMER is not None:
                glib.source_remove(SUSPEND_TIMER)
                SUSPEND_TIMER = None

        def toggle_cb(widget, data=None):
            # If the user toggles redshift, we forget about the suspend timer.
            # Only then widget is not None.
            if widget:
                remove_suspend_timer()

            process.send_signal(signal.SIGUSR1)
            if appindicator:
                if indicator.get_icon() == 'redshift-status-on':
                    indicator.set_icon('redshift-status-off')
                else:
                    indicator.set_icon('redshift-status-on')
            else:
	            if status_icon.get_icon_name() == 'redshift-status-on':
                        status_icon.set_from_icon_name('redshift-status-off')
	            else:
                        status_icon.set_from_icon_name('redshift-status-on')

        def enable_cb():
            if is_enabled():
                return
            # Enable redshift
            toggle_cb(None)

        def suspend_cb(widget, minutes):
            if is_enabled():
                # Disable redshift
                toggle_cb(None)
            # If "suspend" is clicked while redshift is disabled, we reenable
            # it after the last selected timespan is over.
            remove_suspend_timer()
            # If redshift was already disabled we reenable it nonetheless.
            global SUSPEND_TIMER
            SUSPEND_TIMER = glib.timeout_add_seconds(minutes * 60, enable_cb)

        def autostart_cb(widget, data=None):
            utils.set_autostart(widget.get_active())

        def destroy_cb(widget, data=None):
            if not appindicator:
                status_icon.set_visible(False)
            gtk.main_quit()
            return False

        # Create popup menu
        status_menu = gtk.Menu()

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

        suspend_menu_item = gtk.MenuItem(_('Suspend for'))
        suspend_menu = gtk.Menu()
        for minutes, label in [(30, _('30 minutes')), (60, _('1 hour')),
                               (120, _('2 hours'))]:
            suspend_item = gtk.MenuItem(label)
            suspend_item.connect('activate', suspend_cb, minutes)
            suspend_menu.append(suspend_item)
        suspend_menu_item.set_submenu(suspend_menu)
        status_menu.append(suspend_menu_item)

        autostart_item = gtk.CheckMenuItem(_('Autostart'))
        try:
            autostart_item.set_active(utils.get_autostart())
        except IOError as strerror:
            print strerror
            autostart_item.set_property('sensitive', False)
        else:
            autostart_item.connect('activate', autostart_cb)
        finally:
            status_menu.append(autostart_item)

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

        if appindicator:
            status_menu.show_all()

            # Set the menu
            indicator.set_menu(status_menu)
        else:
            def popup_menu_cb(widget, button, time, data=None):
                status_menu.show_all()
                status_menu.popup(None, None, gtk.status_icon_position_menu,
                                  button, time, status_icon)

            # Connect signals for status icon and show
            status_icon.connect('activate', toggle_cb)
            status_icon.connect('popup-menu', popup_menu_cb)
            status_icon.set_visible(True)

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

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

        # Run main loop
        gtk.main()

    except KeyboardInterrupt:
        # Ignore user interruption
        pass

    finally:
        # Always terminate redshift
        process.terminate()
        process.wait()
Пример #3
0
    def __init__(self, args=[]):
        # Initialize state variables
        self._status = False
        self._temperature = 0
        self._period = 'Unknown'
        self._location = (0.0, 0.0)

        # Install TERM signal handler
        signal.signal(signal.SIGTERM, sigterm_handler)

        # Start redshift with arguments
        args.insert(0, os.path.join(defs.BINDIR, 'redshift'))
        if '-v' not in args:
            args.append('-v')

        self.start_child_process(args)

        if appindicator:
            # Create indicator
            self.indicator = appindicator.Indicator.new('redshift',
                                                        'redshift-status-on',
                                                        appindicator.IndicatorCategory.APPLICATION_STATUS)
            self.indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
        else:
            # Create status icon
            self.status_icon = Gtk.StatusIcon()
            self.status_icon.set_from_icon_name('redshift-status-on')
            self.status_icon.set_tooltip_text('Redshift')

        # Create popup menu
        self.status_menu = Gtk.Menu()

        # Add toggle action
        toggle_item = Gtk.MenuItem(_('Toggle'))
        toggle_item.connect('activate', self.toggle_cb)
        self.status_menu.append(toggle_item)

        # Add suspend menu
        suspend_menu_item = Gtk.MenuItem(_('Suspend for'))
        suspend_menu = Gtk.Menu()
        for minutes, label in [(30, _('30 minutes')),
                               (60, _('1 hour')),
                               (120, _('2 hours'))]:
            suspend_item = Gtk.MenuItem(label)
            suspend_item.connect('activate', self.suspend_cb, minutes)
            suspend_menu.append(suspend_item)
        suspend_menu_item.set_submenu(suspend_menu)
        self.status_menu.append(suspend_menu_item)

        # Add autostart option
        autostart_item = Gtk.CheckMenuItem(_('Autostart'))
        try:
            autostart_item.set_active(utils.get_autostart())
        except IOError as strerror:
            print strerror
            autostart_item.set_property('sensitive', False)
        else:
            autostart_item.connect('toggled', self.autostart_cb)
        finally:
            self.status_menu.append(autostart_item)

        # Add info action
        info_item = Gtk.MenuItem(_('Info'))
        info_item.connect('activate', self.show_info_cb)
        self.status_menu.append(info_item)

        # Add quit action
        quit_item = Gtk.ImageMenuItem(_('Quit'))
        quit_item.connect('activate', self.destroy_cb)
        self.status_menu.append(quit_item)

        # Create info dialog
        self.info_dialog = Gtk.Dialog(_('Info'),
                                      None, 0,
                                      (_('Close'), Gtk.ResponseType.CLOSE))
        self.info_dialog.set_resizable(False)
        self.info_dialog.set_property('border-width', 6)

        self.status_label = Gtk.Label()
        self.status_label.set_alignment(0.0, 0.5)
        self.status_label.set_padding(6, 6);
        self.info_dialog.get_content_area().pack_start(self.status_label, True, True, 0)
        self.status_label.show()

        self.location_label = Gtk.Label()
        self.location_label.set_alignment(0.0, 0.5)
        self.location_label.set_padding(6, 6);
        self.info_dialog.get_content_area().pack_start(self.location_label, True, True, 0)
        self.location_label.show()

        self.temperature_label = Gtk.Label()
        self.temperature_label.set_alignment(0.0, 0.5)
        self.temperature_label.set_padding(6, 6);
        self.info_dialog.get_content_area().pack_start(self.temperature_label, True, True, 0)
        self.temperature_label.show()

        self.period_label = Gtk.Label()
        self.period_label.set_alignment(0.0, 0.5)
        self.period_label.set_padding(6, 6);
        self.info_dialog.get_content_area().pack_start(self.period_label, True, True, 0)
        self.period_label.show()

        self.info_dialog.connect('response', self.response_info_cb)

        if appindicator:
            self.status_menu.show_all()

            # Set the menu
            self.indicator.set_menu(self.status_menu)
        else:
            # Connect signals for status icon and show
            self.status_icon.connect('activate', self.toggle_cb)
            self.status_icon.connect('popup-menu', self.popup_menu_cb)
            self.status_icon.set_visible(True)

        # Initialize suspend timer
        self.suspend_timer = None

        # Handle child input
        class InputBuffer(object):
            lines = []
            buf = ''
        self.input_buffer = InputBuffer()
        self.error_buffer = InputBuffer()

        # Set non blocking
        fcntl.fcntl(self.process[2], fcntl.F_SETFL,
                    fcntl.fcntl(self.process[2], fcntl.F_GETFL) | os.O_NONBLOCK)

        # Add watch on child process
        GLib.child_watch_add(self.process[0], self.child_cb)
        GLib.io_add_watch(self.process[2], GLib.IO_IN,
                          self.child_data_cb, (True, self.input_buffer))
        GLib.io_add_watch(self.process[3], GLib.IO_IN,
                          self.child_data_cb, (False, self.error_buffer))
Пример #4
0
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)

    try:
        if appindicator:
            # Create indicator
            indicator = appindicator.Indicator(
                'redshift', 'redshift-status-on',
                appindicator.CATEGORY_APPLICATION_STATUS)
            indicator.set_status(appindicator.STATUS_ACTIVE)
        else:
            # Create status icon
            status_icon = gtk.StatusIcon()
            status_icon.set_from_icon_name('redshift-status-on')
            status_icon.set_tooltip('Redshift')

        def is_enabled():
            if appindicator:
                return indicator.get_icon() == 'redshift-status-on'
            else:
                return status_icon.get_icon_name() == 'redshift-status-on'

        def remove_suspend_timer():
            global SUSPEND_TIMER
            if SUSPEND_TIMER is not None:
                glib.source_remove(SUSPEND_TIMER)
                SUSPEND_TIMER = None

        def toggle_cb(widget, data=None):
            # If the user toggles redshift, we forget about the suspend timer.
            # Only then widget is not None.
            if widget:
                remove_suspend_timer()

            process.send_signal(signal.SIGUSR1)
            if appindicator:
                if indicator.get_icon() == 'redshift-status-on':
                    indicator.set_icon('redshift-status-off')
                else:
                    indicator.set_icon('redshift-status-on')
            else:
                if status_icon.get_icon_name() == 'redshift-status-on':
                    status_icon.set_from_icon_name('redshift-status-off')
                else:
                    status_icon.set_from_icon_name('redshift-status-on')

        def enable_cb():
            if is_enabled():
                return
            # Enable redshift
            toggle_cb(None)

        def suspend_cb(widget, minutes):
            if is_enabled():
                # Disable redshift
                toggle_cb(None)
            # If "suspend" is clicked while redshift is disabled, we reenable
            # it after the last selected timespan is over.
            remove_suspend_timer()
            # If redshift was already disabled we reenable it nonetheless.
            global SUSPEND_TIMER
            SUSPEND_TIMER = glib.timeout_add_seconds(minutes * 60, enable_cb)

        def autostart_cb(widget, data=None):
            utils.set_autostart(widget.get_active())

        def destroy_cb(widget, data=None):
            if not appindicator:
                status_icon.set_visible(False)
            gtk.main_quit()
            return False

        # Create popup menu
        status_menu = gtk.Menu()

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

        suspend_menu_item = gtk.MenuItem(_('Suspend for'))
        suspend_menu = gtk.Menu()
        for minutes, label in [(30, _('30 minutes')), (60, _('1 hour')),
                               (120, _('2 hours'))]:
            suspend_item = gtk.MenuItem(label)
            suspend_item.connect('activate', suspend_cb, minutes)
            suspend_menu.append(suspend_item)
        suspend_menu_item.set_submenu(suspend_menu)
        status_menu.append(suspend_menu_item)

        autostart_item = gtk.CheckMenuItem(_('Autostart'))
        try:
            autostart_item.set_active(utils.get_autostart())
        except IOError as strerror:
            print strerror
            autostart_item.set_property('sensitive', False)
        else:
            autostart_item.connect('activate', autostart_cb)
        finally:
            status_menu.append(autostart_item)

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

        if appindicator:
            status_menu.show_all()

            # Set the menu
            indicator.set_menu(status_menu)
        else:

            def popup_menu_cb(widget, button, time, data=None):
                status_menu.show_all()
                status_menu.popup(None, None, gtk.status_icon_position_menu,
                                  button, time, status_icon)

            # Connect signals for status icon and show
            status_icon.connect('activate', toggle_cb)
            status_icon.connect('popup-menu', popup_menu_cb)
            status_icon.set_visible(True)

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

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

        # Run main loop
        gtk.main()

    except KeyboardInterrupt:
        # Ignore user interruption
        pass

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