File: mate-tweak-helper

package info (click to toggle)
mate-tweak 22.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,276 kB
  • sloc: python: 1,518; sh: 76; makefile: 13
file content (226 lines) | stat: -rwxr-xr-x 8,511 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# Copyright (C) 2015-2022 Martin Wimpress <code@ubuntu-mate.org>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.

import os
import subprocess
import shutil
import sys
import tempfile
from gi.repository import Gio
from subprocess import PIPE


def autostop(filename):
    desktopfile = os.path.join('/','etc','xdg','autostart', filename)
    if os.path.exists(desktopfile):
        os.remove(desktopfile)


def install_layout(filename):
    if os.path.exists(os.path.join(tempfile.gettempdir(),filename)):
        shutil.copy2(os.path.join(tempfile.gettempdir(),filename),
                     os.path.join('/','usr','share','mate-panel','layouts',filename))
        os.remove(os.path.join(tempfile.gettempdir(),filename))
    else:
        # If a .dock hint is not found but is installed, remove it.
        if filename.endswith('dock') and os.path.exists(os.path.join('/','usr','share','mate-panel','layouts',filename)):
            os.remove(os.path.join('/','usr','share','mate-panel','layouts',filename))
        print('Unable to find ' + os.path.join(tempfile.gettempdir(),filename))


def delete_layout(filename):
    if 'tweak' in filename:
        layout = os.path.join('/','usr','share','mate-panel','layouts',filename)
        if os.path.exists(layout):
            os.remove(layout)
        else:
            print('Unable to find ' + layout)
    else:
        print('WARNING! I will only delete custom layouts. Skipping ' + layout)


def backup_layout(filename):
    VALID = {'toplevel': ('enable-buttons', 'expand', 'monitor', 'orientation', 'screen', 'size'),
             'launcher': ('object-type', 'launcher-location', 'menu-path', 'toplevel-id', 'position', 'panel-right-stick', 'relative-to-edge', 'locked'),
             'applet': ('object-type', 'applet-iid', 'toplevel-id', 'position', 'panel-right-stick', 'relative-to-edge', 'locked'),
             'drawer': ('object-type', 'attached-toplevel-id', 'toplevel-id', 'position', 'panel-right-stick', 'relative-to-edge', 'use-custom-icon'),
             'menu-bar': ('object-type', 'applet-iid', 'toplevel-id', 'position', 'panel-right-stick','relative-to-edge', 'locked'),
             'menu': ('object-type', 'toplevel-id', 'position', 'panel-right-stick', 'relative-to-edge', 'locked'),
             'action': ('object-type', 'action-type', 'position', 'toplevel-id', 'panel-right-stick', 'relative-to-edge', 'locked'),
             'separator': ('object-type', 'toplevel-id', 'position', 'panel-right-stick', 'relative-to-edge', 'locked')}

    schemas = {'panel': 'org.mate.panel',
               'object': 'org.mate.panel.object',
               'toplevel':'org.mate.panel.toplevel'}

    paths = {'object': '/org/mate/panel/objects/',
             'toplevel': '/org/mate/panel/toplevels/'}

    general_settings = Gio.Settings.new(schemas['panel'])

    toplevel_ids = general_settings['toplevel-id-list']
    object_ids = general_settings['object-id-list']

    layout = []

    for toplevel in toplevel_ids:
        settings = Gio.Settings.new_with_path(
            schemas['toplevel'],
            paths['toplevel'] + toplevel + '/')

        layout.append("[Toplevel %s]\n" % toplevel)

        for key in settings.keys():
            val = settings[key]
            if str(val) == "True" or str(val) == "False":
                val = str(val).lower()

            if key in VALID['toplevel']:
                layout.append("%s=%s\n" % (key, val))
        layout.append("\n")

    for obj in object_ids:
        settings = Gio.Settings.new_with_path(
            schemas['object'],
            paths['object'] + obj + '/')

        obj_toplevel = settings['toplevel-id']
        obj_type = settings['object-type']
        obj_name = str(obj)

        if not obj_toplevel in toplevel_ids:
            print("WARNING! object \"%s\" references unknown toplevel... skipped" % obj_name)
            continue

        layout.append("[Object %s]\n" % obj_name.lower())
        for key in settings.keys():
            if key in VALID[obj_type]:
                val = settings[key]
                if str(val) == "True" or str(val) == "False":
                    val = str(val).lower()

                layout.append("%s=%s\n" % (key, val))
        layout.append("\n")

    layout.extend(get_non_panel_settings())

    #print(layout)
    with open(os.path.join(tempfile.gettempdir(), filename + '.layout'), 'w') as f:
        f.writelines(layout)

    # Dump dconf panel
    dconf_process = subprocess.Popen(['dconf', 'dump', '/org/mate/panel/'], stdout=PIPE)
    dump = dconf_process.communicate()[0].decode("UTF-8")
    with open(os.path.join(tempfile.gettempdir(),filename + '.panel'), 'w') as f:
        f.writelines(dump)

def get_non_panel_settings():
    layout = []

    layout.extend(get_maximus_undecorate())
    layout.extend(get_window_control_layout())

    return layout

def get_maximus_undecorate():
    VALID = {'mate-maximus-undecorate': 'undecorate'}

    schemas = {'mate-maximus-undecorate': 'org.mate.maximus'}

    paths = {'mate-maximus-undecorate': '/org/mate/maximus/'}

    layout = []
    layout.append('[Customsetting maximusdecoration]\n')
    layout.extend(collect_simple_settings(VALID, schemas, paths, False))

    # We needed a way to determine if the user chose a different
    # maximus undecorated setting as opposed to the setting not
    # being present (old version of mate-tweak or just not in
    # dconf)
    if any('mate-maximus-undecorate' in entry for entry in layout):
        layout.append('mate-maximus-recorded=True\n')
    else:
        layout.append('mate-maximus-recorded=False\n')

    layout.append('\n')
    return layout

def get_window_control_layout():
    VALID = {'mate-general': 'button-layout',
             'mate-interface': 'gtk-decoration-layout',
             'gnome-wm-preferences': 'button-layout'}

    schemas = {'mate-general': 'org.mate.Marco.general',
               'mate-interface': 'org.mate.interface',
               'gnome-wm-preferences': 'org.gnome.desktop.wm.preferences'}

    paths = {'mate-general': '/org/mate/Macro/general/',
             'mate-interface': '/org/mate/interface/',
             'gnome-wm-preferences': '/org/gnome/desktop/wm/preferences/'}

    layout = []
    layout.append('[Customsetting windowcontrollayout]\n')
    layout.extend(collect_simple_settings(VALID, schemas, paths, True))

    return layout

def collect_simple_settings(valid_keys, schemas, paths, append_final_newline):
    layout = []
    for setting in schemas.keys():
        if Gio.SettingsSchemaSource.get_default().lookup(schemas[setting], True) == None:
            continue

        settings = Gio.Settings.new(schemas[setting])

        for key in settings.keys():
            if key == valid_keys[setting]:
                val = settings[key]
                layout.append(f"{setting}={val}\n")

    if append_final_newline:
        layout.append('\n')
    return layout


def backup_dock(filename):
    with open(os.path.join(tempfile.gettempdir(), filename + '.dock'), 'w') as f:
        f.writelines('plank')

if __name__ == '__main__':
    if len(sys.argv) == 3:
        action = sys.argv[1]
        filename = sys.argv[2]
        if action == 'autostop':
            autostop(filename)
        elif action == 'backup':
            backup_layout(filename)
        elif action == 'dock':
            backup_dock(filename)
        elif action == 'delete':
            delete_layout(filename + '.dock')
            delete_layout(filename + '.layout')
            delete_layout(filename + '.panel')
        elif action == 'install':
            install_layout(filename + '.dock')
            install_layout(filename + '.layout')
            install_layout(filename + '.panel')
    else:
        print("ERROR! No action supplied.")
        sys.exit(1)