[gnome-tweak-tool] Improve autostart handling



commit 17731965f4e530761c8db14d20b305b0e9a102d0
Author: John Stowers <john stowers gmail com>
Date:   Wed May 25 12:04:35 2011 +1200

    Improve autostart handling
    
    	* Dont ever copy autostart files into ~/.config/autostart
    	  if the corresponding system autostart file has an
    	  AutostartCondition stanza
    	* This should prevent autostarting nautilus twice when using
    	  Nautilus >= 3.1.x
    	* Improve logic to support user installed desktop files
    	* Why isnt this complex autostart handling not in GLib
    	  or GApplication or something...

 gtweak/tweaks/tweak_nautilus.py |    4 +-
 gtweak/utils.py                 |   89 ++++++++++++++++++++++++++++++++-------
 2 files changed, 77 insertions(+), 16 deletions(-)
---
diff --git a/gtweak/tweaks/tweak_nautilus.py b/gtweak/tweaks/tweak_nautilus.py
index 7ed3670..3c46187 100644
--- a/gtweak/tweaks/tweak_nautilus.py
+++ b/gtweak/tweaks/tweak_nautilus.py
@@ -31,7 +31,9 @@ class DesktopIconTweak(GSettingsSwitchTweak):
 
         #when the user enables nautilus to draw the desktop icons, set nautilus
         #to autostart
-        self.nautilus = AutostartManager(gtweak.DATA_DIR, "nautilus.desktop", "nautilus -n")
+        self.nautilus = AutostartManager("nautilus.desktop",
+                            autostart_desktop_filename="nautilus-autostart.desktop",
+                            exec_cmd="nautilus -n")
         self.settings.connect('changed::'+self.key_name, self._on_setting_changed)
 
     def _on_setting_changed(self, setting, key):
diff --git a/gtweak/utils.py b/gtweak/utils.py
index 8c52df7..64542db 100644
--- a/gtweak/utils.py
+++ b/gtweak/utils.py
@@ -20,6 +20,8 @@ import logging
 import tempfile
 import shutil
 
+import gtweak
+
 from gi.repository import GLib
 
 def walk_directories(dirs, filter_func):
@@ -57,45 +59,78 @@ def extract_zip_file(z, members_path, dest):
     return ok, updated
 
 class AutostartManager:
-    def __init__(self, DATA_DIR, desktop_filename, exec_cmd="", extra_exec_args=""):
-        self._desktop_filename = desktop_filename
-        self._desktop_file = os.path.join(DATA_DIR, "applications", desktop_filename)
+    def __init__(self, desktop_filename, autostart_desktop_filename="", exec_cmd="", extra_exec_args=""):
+        self.desktop_filename = desktop_filename
+        self._autostart_desktop_filename = autostart_desktop_filename or desktop_filename
         self._exec_cmd = exec_cmd
         self._extra_exec_args = " %s\n" % extra_exec_args
-        
+
         user_autostart_dir = os.path.join(GLib.get_user_config_dir(), "autostart")
         if not os.path.isdir(user_autostart_dir):
             try:
                 os.makedirs(user_autostart_dir)
             except:
                 logging.critical("Could not create autostart dir: %s" % user_autostart_dir)
-        self._autostart_file = os.path.join(user_autostart_dir, desktop_filename)
+
+        #find the desktop file
+        self._desktop_file = ""
+        for f in self._get_desktop_files():
+            if os.path.exists(f):
+                self._desktop_file = f
+        self._user_autostart_file = os.path.join(user_autostart_dir, self._autostart_desktop_filename)
+
+        logging.debug("Found desktop file: %s" % self._desktop_file)
+        logging.debug("User autostart desktop file: %s" % self._user_autostart_file)
+
+    def _get_system_autostart_files(self):
+        return [
+            os.path.join(d, "autostart", self._autostart_desktop_filename)
+                for d in GLib.get_system_config_dirs()]
+
+    def _get_desktop_files(self):
+        dirs = [gtweak.DATA_DIR, GLib.get_user_data_dir()]
+        dirs.extend(GLib.get_system_data_dirs())
+        return [os.path.join(d, "applications", self.desktop_filename) for d in dirs]
+
+    def uses_autostart_condition(self):
+        for f in self._get_system_autostart_files():
+            if os.path.exists(f) and open(f).read().find("AutostartCondition=") != -1:
+                return True
+        return False
 
     def is_start_at_login_enabled(self):
-        if os.path.exists(self._autostart_file):
+        if os.path.exists(self._user_autostart_file):
+            #prefer user directories first
             #if it contains X-GNOME-Autostart-enabled=false then it has
             #has been disabled by the user in the session applet, otherwise
             #it is enabled
-            return open(self._autostart_file).read().find("X-GNOME-Autostart-enabled=false") == -1
+            return open(self._user_autostart_file).read().find("X-GNOME-Autostart-enabled=false") == -1
         else:
-            return False
+            #check the system directories
+            for f in self._get_system_autostart_files():
+                if os.path.exists(f):
+                    return True
+        return False
 
     def update_start_at_login(self, update):
-        logging.debug("Updating autostart %s -> %s" % (self._desktop_filename, update))
 
-        if os.path.exists(self._autostart_file):
-            logging.info("Removing autostart %s" % self._autostart_file)
-            os.remove(self._autostart_file)
+        if os.path.exists(self._user_autostart_file):
+            logging.info("Removing user autostart file %s" % self._user_autostart_file)
+            os.remove(self._user_autostart_file)
 
         if update:
-            if not os.path.exists(self._desktop_file):
+            if (not self._desktop_file) or (not os.path.exists(self._desktop_file)):
                 logging.critical("Could not find desktop file: %s" % self._desktop_file)
                 return
 
-            logging.info("Adding autostart %s" % self._autostart_file)
+            if self.uses_autostart_condition():
+                logging.warning("Autostart desktop file uses AutostartCondition. Skipping")
+                return
+
+            logging.info("Adding autostart %s" % self._user_autostart_file)
             #copy the original file to the new file, but add the extra exec args
             old = open(self._desktop_file, "r")
-            new = open(self._autostart_file, "w")
+            new = open(self._user_autostart_file, "w")
 
             for l in old.readlines():         
                 if l.startswith("Exec="):
@@ -109,3 +144,27 @@ class AutostartManager:
 
             old.close()
             new.close()
+
+if __name__ == "__main__":
+    gtweak.DATA_DIR = "/usr/share"
+
+    logging.basicConfig(format="%(levelname)-8s: %(message)s", level=logging.DEBUG)
+
+    d = AutostartManager("matlab.desktop")
+    print d.desktop_filename, "uses autostartcondition", d.uses_autostart_condition()
+    print d.desktop_filename, "autostarts", d.is_start_at_login_enabled()
+    d.update_start_at_login(True)
+    print d.desktop_filename, "autostarts", d.is_start_at_login_enabled()
+    d.update_start_at_login(False)
+    print d.desktop_filename, "autostarts", d.is_start_at_login_enabled()
+
+    d = AutostartManager("orca.desktop", "orca-autostart.desktop")
+    print d.desktop_filename, "uses autostartcondition", d.uses_autostart_condition()
+    print d.desktop_filename, "autostarts", d.is_start_at_login_enabled()
+    d.update_start_at_login(True)
+    print d.desktop_filename, "autostarts", d.is_start_at_login_enabled()
+
+    d = AutostartManager("dropbox.desktop")
+    print d.desktop_filename, "uses autostartcondition", d.uses_autostart_condition()
+    print d.desktop_filename, "autostarts", d.is_start_at_login_enabled()
+



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]