From 07ec131ba713ebf5e49a9c57a3d2cda9cd9489ad Mon Sep 17 00:00:00 2001 From: Francesco Marella Date: Sun, 6 Jun 2010 21:00:03 +0200 Subject: Add a feature to toggle autostart at login A launcher is placed in Applications -> Utility. The user toggles autostart through the user interface. The 'xdg' module is required. --- src/gtk-redshift/utils.py | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/gtk-redshift/utils.py (limited to 'src/gtk-redshift/utils.py') diff --git a/src/gtk-redshift/utils.py b/src/gtk-redshift/utils.py new file mode 100644 index 0000000..5c63a02 --- /dev/null +++ b/src/gtk-redshift/utils.py @@ -0,0 +1,57 @@ +# utils.py -- utility functions 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 . + +# Copyright (c) 2010 Francesco Marella + +import os +from xdg import BaseDirectory as base +from xdg import DesktopEntry as desktop + +REDSHIFT_DESKTOP = 'gtk-redshift.desktop' + + +def get_autostart(): + AUTOSTART_KEY = "X-GNOME-Autostart-enabled" + autostart_dir = base.save_config_path("autostart") + autostart_file = os.path.join(autostart_dir, REDSHIFT_DESKTOP) + if not os.path.exists(autostart_file): + return False + else: + dfile = desktop.DesktopEntry(autostart_file) + if dfile.get(AUTOSTART_KEY) == 'false': + return False + else: + return True + +def set_autostart(active): + AUTOSTART_KEY = "X-GNOME-Autostart-enabled" + autostart_dir = base.save_config_path("autostart") + autostart_file = os.path.join(autostart_dir, REDSHIFT_DESKTOP) + if not os.path.exists(autostart_file): + desktop_files = list(base.load_data_paths("applications", + REDSHIFT_DESKTOP)) + if not desktop_files: + print "Error: Installed redshift desktop file not found!" + return + desktop_file_path = desktop_files[0] + # Read installed file and modify it + dfile = desktop.DesktopEntry(desktop_file_path) + else: + dfile = desktop.DesktopEntry(autostart_file) + activestr = str(bool(active)).lower() + # print "Setting autostart to %s" % activestr + dfile.set(AUTOSTART_KEY, activestr) + dfile.write(filename=autostart_file) -- cgit v1.2.3-70-g09d2 From c9b0abf61022d55866bba65302708dfda65a57d1 Mon Sep 17 00:00:00 2001 From: Francesco Marella Date: Tue, 22 Jun 2010 12:46:45 +0200 Subject: Disable autostart menu item when the desktop file can't be found --- src/gtk-redshift/rsappindicator.py | 12 +++++++++--- src/gtk-redshift/statusicon.py | 12 +++++++++--- src/gtk-redshift/utils.py | 4 ++-- 3 files changed, 20 insertions(+), 8 deletions(-) (limited to 'src/gtk-redshift/utils.py') diff --git a/src/gtk-redshift/rsappindicator.py b/src/gtk-redshift/rsappindicator.py index 15a2dee..23f1ac8 100644 --- a/src/gtk-redshift/rsappindicator.py +++ b/src/gtk-redshift/rsappindicator.py @@ -76,9 +76,15 @@ def run(): status_menu.append(toggle_item) autostart_item = gtk.CheckMenuItem(_('Autostart')) - autostart_item.set_active(utils.get_autostart()) - autostart_item.connect('activate', autostart_cb) - status_menu.append(autostart_item) + 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) diff --git a/src/gtk-redshift/statusicon.py b/src/gtk-redshift/statusicon.py index 24f02da..8019ad4 100644 --- a/src/gtk-redshift/statusicon.py +++ b/src/gtk-redshift/statusicon.py @@ -67,9 +67,15 @@ def run(): status_menu.append(toggle_item) autostart_item = gtk.CheckMenuItem(_('Autostart')) - autostart_item.set_active(utils.get_autostart()) - autostart_item.connect('activate', autostart_cb) - status_menu.append(autostart_item) + 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) diff --git a/src/gtk-redshift/utils.py b/src/gtk-redshift/utils.py index 5c63a02..0344406 100644 --- a/src/gtk-redshift/utils.py +++ b/src/gtk-redshift/utils.py @@ -28,7 +28,7 @@ def get_autostart(): autostart_dir = base.save_config_path("autostart") autostart_file = os.path.join(autostart_dir, REDSHIFT_DESKTOP) if not os.path.exists(autostart_file): - return False + raise IOError("Installed redshift desktop file not found!") else: dfile = desktop.DesktopEntry(autostart_file) if dfile.get(AUTOSTART_KEY) == 'false': @@ -44,7 +44,7 @@ def set_autostart(active): desktop_files = list(base.load_data_paths("applications", REDSHIFT_DESKTOP)) if not desktop_files: - print "Error: Installed redshift desktop file not found!" + raise IOError("Installed redshift desktop file not found!") return desktop_file_path = desktop_files[0] # Read installed file and modify it -- cgit v1.2.3-70-g09d2 From 846284943baabadc4dc539894cacb00bf1f96169 Mon Sep 17 00:00:00 2001 From: Francesco Marella Date: Tue, 22 Jun 2010 16:14:39 +0200 Subject: utils.py: copy the desktop file in autostart folder --- src/gtk-redshift/utils.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/gtk-redshift/utils.py') diff --git a/src/gtk-redshift/utils.py b/src/gtk-redshift/utils.py index 0344406..93e0195 100644 --- a/src/gtk-redshift/utils.py +++ b/src/gtk-redshift/utils.py @@ -28,7 +28,16 @@ def get_autostart(): autostart_dir = base.save_config_path("autostart") autostart_file = os.path.join(autostart_dir, REDSHIFT_DESKTOP) if not os.path.exists(autostart_file): - raise IOError("Installed redshift desktop file not found!") + desktop_files = list(base.load_data_paths("applications", + REDSHIFT_DESKTOP)) + if not desktop_files: + raise IOError("Installed redshift desktop file not found!") + desktop_file_path = desktop_files[0] + # Read installed file and modify it + dfile = desktop.DesktopEntry(desktop_file_path) + dfile.set(AUTOSTART_KEY, "false") + dfile.write(filename=autostart_file) + return False else: dfile = desktop.DesktopEntry(autostart_file) if dfile.get(AUTOSTART_KEY) == 'false': -- cgit v1.2.3-70-g09d2