[gnome-tweak-tool] AutostartFile: add support to create a user autostart file



commit 3be4eba33c1eb55a3a2c2878c45364ee9d31be78
Author: Rui Matos <tiagomatos gmail com>
Date:   Sat Feb 15 17:08:48 2014 +0100

    AutostartFile: add support to create a user autostart file
    
    If no appinfo is provided on the constructor we enter a mode where
    we'll try to create the .desktop file in the user's autostart
    directory.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=715022

 gtweak/utils.py |   27 +++++++++++++++++++++++----
 1 files changed, 23 insertions(+), 4 deletions(-)
---
diff --git a/gtweak/utils.py b/gtweak/utils.py
index 3d20425..9907adc 100644
--- a/gtweak/utils.py
+++ b/gtweak/utils.py
@@ -136,8 +136,17 @@ class AutostartManager:
 
 class AutostartFile:
     def __init__(self, appinfo, autostart_desktop_filename="", exec_cmd="", extra_exec_args=""):
-        self._desktop_file = appinfo.get_filename()
-        self._autostart_desktop_filename = autostart_desktop_filename or os.path.basename(self._desktop_file)
+        if appinfo:
+            self._desktop_file = appinfo.get_filename()
+            self._autostart_desktop_filename = autostart_desktop_filename or 
os.path.basename(self._desktop_file)
+            self._create_file = False
+        elif autostart_desktop_filename:
+            self._desktop_file = None
+            self._autostart_desktop_filename = autostart_desktop_filename
+            self._create_file = True
+        else:
+            raise Exception("Need either an appinfo or a file name")
+
         self._exec_cmd = exec_cmd
         self._extra_exec_args = " %s\n" % extra_exec_args
 
@@ -150,9 +159,16 @@ class AutostartFile:
 
         self._user_autostart_file = os.path.join(user_autostart_dir, self._autostart_desktop_filename)
 
-        logging.debug("Found desktop file: %s" % self._desktop_file)
+        if self._desktop_file:
+            logging.debug("Found desktop file: %s" % self._desktop_file)
         logging.debug("User autostart desktop file: %s" % self._user_autostart_file)
 
+    def _create_user_autostart_file(self):
+        f = open(self._user_autostart_file, "w")
+        f.write("[Desktop Entry]\nType=Application\nName=%s\nExec=%s\n" %
+                (self._autostart_desktop_filename[0:-len('.desktop')], self._exec_cmd + 
self._extra_exec_args))
+        f.close()
+
     def is_start_at_login_enabled(self):
         if os.path.exists(self._user_autostart_file):
             #prefer user directories first
@@ -175,7 +191,10 @@ class AutostartFile:
 
         if update:
             if (not self._desktop_file) or (not os.path.exists(self._desktop_file)):
-                logging.critical("Could not find desktop file: %s" % self._desktop_file)
+                if self._create_file:
+                    self._create_user_autostart_file()
+                else:
+                    logging.critical("Could not find desktop file: %s" % self._desktop_file)
                 return
 
             logging.info("Adding autostart %s" % self._user_autostart_file)


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