utils.py 9.67 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# This file is part of gnome-tweak-tool.
#
# Copyright (c) 2011 John Stowers
#
# gnome-tweak-tool 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.
#
# gnome-tweak-tool 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 gnome-tweak-tool.  If not, see <http://www.gnu.org/licenses/>.

import os.path
19
import logging
20 21
import tempfile
import shutil
22
import subprocess
23

24
import gtweak
25
from gtweak.gsettings import GSettingsSetting
26

27 28
from gi.repository import GLib

29 30 31 32 33 34 35 36 37 38 39 40 41
def singleton(cls):
    """
    Singleton decorator that works with GObject derived types. The 'recommended'
    python one - http://wiki.python.org/moin/PythonDecoratorLibrary#Singleton
    does not (interacts badly with GObjectMeta
    """
    instances = {}
    def getinstance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return getinstance

42
def make_combo_list_with_default(opts, default, title=True, default_text=None):
43 44 45 46 47 48 49 50 51
    """
    Turns a list of values into a list of value,name (where name is the
    display name a user will see in a combo box). If a value is opt is
    equal to that supplied in default the display name for that value is
    modified to "value <i>(default)</i>"

    @opts: a list of value
    @returns: a list of 2-tuples (value, name)
    """
52 53
    themes = []
    for t in opts:
54
        if t.lower() == "default" and t != default:
55 56
            #some themes etc are actually called default. Ick. Dont show them if they
            #are not the actual default value
57 58
            continue

59
        if title and len(t):
60 61 62 63 64
            name = t[0].upper() + t[1:]
        else:
            name = t

        if t == default:
65
            #indicates the default theme, e.g Adwaita (default)
John Stowers's avatar
John Stowers committed
66
            name = default_text or _("%s <i>(default)</i>") % name
67 68 69 70

        themes.append((t, name))
    return themes

71 72 73 74 75 76 77 78 79 80 81 82
def walk_directories(dirs, filter_func):
    valid = []
    try:
        for thdir in dirs:
            if os.path.isdir(thdir):
                for t in os.listdir(thdir):
                    if filter_func(os.path.join(thdir, t)):
                         valid.append(t)
    except:
        logging.critical("Error parsing directories", exc_info=True)
    return valid

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
def extract_zip_file(z, members_path, dest):
    """ returns (true_if_extracted_ok, true_if_updated) """
    tmp = tempfile.mkdtemp()
    tmpdest = os.path.join(tmp, members_path)

    ok = True
    updated = False
    try:
        if os.path.exists(dest):
            shutil.rmtree(dest)
            updated = True
        z.extractall(tmp)
        shutil.copytree(tmpdest, dest)
    except OSError:
        ok = False
        logging.warning("Error extracting zip", exc_info=True)

    if ok:
        logging.info("Extracted zip to %s, copied to %s" % (tmpdest, dest))

    return ok, updated

105 106 107 108 109 110 111 112
def execute_subprocess(cmd_then_args, block=True):
    p = subprocess.Popen(
            cmd_then_args,
            stdin=subprocess.PIPE, stdout=subprocess.PIPE, close_fds=True)
    if block:
        stdout, stderr = p.communicate()
        return stdout, stderr, p.returncode

113
class AutostartManager:
114 115 116
    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
117
        self._exec_cmd = exec_cmd
118
        self._extra_exec_args = " %s\n" % extra_exec_args
119

120 121 122 123 124 125
        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)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

        #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]

147
    def get_autostart_condition(self):
148
        for f in self._get_system_autostart_files():
149 150 151 152 153 154 155
            if os.path.exists(f):
                with open(f, 'r') as f:
                    for l in f.readlines():
                        if l.startswith("AutostartCondition="):
                            return l.split("=")[-1].strip()
        return None

156 157 158 159 160 161 162 163 164 165
    def uses_autostart_condition(self, autostart_type=None):
        asc = self.get_autostart_condition()
        try:
            if autostart_type:
                return asc.split(" ", 1)[0] == autostart_type
            else:
                return asc != None
        except:
            logging.warning("Testing for expected AutostartCondition failed: Got (%s)" % asc, exc_info=True)
            return False
166 167

    def is_start_at_login_enabled(self):
168 169
        if os.path.exists(self._user_autostart_file):
            #prefer user directories first
170 171 172
            #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
173
            return open(self._user_autostart_file).read().find("X-GNOME-Autostart-enabled=false") == -1
174
        else:
175 176 177 178 179
            #check the system directories
            for f in self._get_system_autostart_files():
                if os.path.exists(f):
                    return True
        return False
180 181 182

    def update_start_at_login(self, update):

183 184 185
        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)
186

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        asc = self.get_autostart_condition()
        if asc:
            #AutostartCondition=GNOME /desktop/gnome/interface/accessibility
            #AutostartCondition=GNOME3 if-session gnome-fallback
            #AutostartCondition=GSettings org.gnome.desktop.background show-desktop-icons
            method, value = asc.split(" ", 1)
            try:
                logging.info("Changing AutostartCondition %s -> %s" % (method, update))
                if method == "GSettings":
                    schema, key = value.split(" ")
                    GSettingsSetting(schema).set_boolean(key, update)
                else:
                    raise Exception("Method not supported")
            except:
                logging.warning("Autostart desktop file unsupported AutostartCondition (%s)" % asc, exc_info=True)

            return

205
        if update:
206
            if (not self._desktop_file) or (not os.path.exists(self._desktop_file)):
207
                logging.critical("Could not find desktop file: %s" % self._desktop_file)
208 209
                return

210
            logging.info("Adding autostart %s" % self._user_autostart_file)
211 212
            #copy the original file to the new file, but add the extra exec args
            old = open(self._desktop_file, "r")
213
            new = open(self._user_autostart_file, "w")
214 215 216

            for l in old.readlines():         
                if l.startswith("Exec="):
217 218 219 220 221
                    if self._exec_cmd:
                        new.write("Exec=%s\n" % self._exec_cmd)
                    else:
                        new.write(l[0:-1])
                        new.write(self._extra_exec_args)
222 223 224 225 226
                else:
                    new.write(l)

            old.close()
            new.close()
227 228 229

if __name__ == "__main__":
    gtweak.DATA_DIR = "/usr/share"
230
    gtweak.GSETTINGS_SCHEMA_DIR = "/usr/share/glib-2.0/schemas/"
231 232 233 234 235 236 237 238 239 240 241 242 243

    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()
244
    print d.desktop_filename, "autostartcondition is:", d.get_autostart_condition()
245
    print d.desktop_filename, "uses GSettings autostartcondition", d.uses_autostart_condition("GSettings")
246 247 248 249 250 251
    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()

252 253 254
    d = AutostartManager("nautilus.desktop", "nautilus-autostart.desktop")
    print d.desktop_filename, "uses autostartcondition", d.uses_autostart_condition()
    d.update_start_at_login(False)