summaryrefslogtreecommitdiff
path: root/mate-conf/mate-conf-import
blob: 2517b88f04d5df5192d17afa2c69ac46c9bc73dc (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
#
# Copyright (c) 2013 Stefano Karapetsas
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser 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 Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
# USA.
#
# Authors: Stefano Karapetsas <stefano@karapetsas.com>

import glob
import optparse
import os
import sys

# to read MateConf files
import xml.etree.ElementTree as ElementTree


import gi
# to show dialogs
gi.require_version("Gtk", "2.0")
from gi.repository import Gtk
# to read convert files
from gi.repository import GLib
# to write GSettings
from gi.repository import Gio

# command options
parser = optparse.OptionParser()
parser.add_option("-d", "--debug", dest="debug", action="store_true", help="enable debug messages")
parser.add_option("-f", "--force", dest="force", action="store_true", help="force migration of settings")
(options, args) = parser.parse_args()

# print a message only if debug option is enabled
def debug_message(*argv):
    args = []
    for arg in argv:
        args.append(str(arg))
    if options.debug:
        print " ".join(args)

# get a mateconf string list
def mateconf_get_string_list(root, key):
    for child in root:
        if child.attrib['name'] == key:
            if child.attrib["type"] == "list":
                if child.attrib["ltype"] == "string":
                    if len(list(child)) > 0:
                        strv = []
                        for ch in child:
                            for li in ch:
                                strv.append(li.text)
                        return strv
                    else:
                        return []
    return None

def gsettings_id(id):
    return id.replace("_", "-")

mateconf_user_path = os.path.dirname(os.path.join(GLib.get_home_dir(), ".mateconf/"))
if not os.path.exists(mateconf_user_path):
    #FIXME save key in GSettings
    debug_message(mateconf_user_path, "not found!")
    sys.exit(0)

#FIXME check GSettings key
dialog = Gtk.MessageDialog (None,
                            Gtk.DialogFlags.MODAL,
                            Gtk.MessageType.QUESTION,
                            Gtk.ButtonsType.YES_NO,
                            "Migrate settings from MATE 1.4?")
res = dialog.run()
if res != Gtk.ResponseType.YES:
    dialog.destroy()
    sys.exit(0)
dialog.destroy()

# load .convert files list from MateConf folder
convert_files = glob.glob("/usr/share/MateConf/gsettings/*")

no_background = False
for convert_file in convert_files:

    # load .convert file
    keyfile = GLib.KeyFile.new()
    GLib.KeyFile.load_from_file(keyfile, convert_file, 0)
    groups = GLib.KeyFile.get_groups(keyfile)

    # iter GSettings schemas
    for group in groups[0]:

        # each group is a GSettings schema
        gsettings_schema = group
        keys = GLib.KeyFile.get_keys(keyfile, group);
        settings = Gio.Settings.new(gsettings_schema)

        # delay settings to save them only once
        settings.delay()

        # iter GSettings keys
        for gsettings_key in keys[0]:

            # get mateconf xml file,
            mateconf_fullkey = GLib.KeyFile.get_string(keyfile, group, gsettings_key)
            mateconf_path = os.path.dirname(os.path.join(mateconf_user_path, mateconf_fullkey[1:]))
            mateconf_file = os.path.join(mateconf_path, "%mateconf.xml")
            mateconf_key = os.path.basename(mateconf_fullkey)

            # if user has custom mateconf
            if os.path.exists(mateconf_file):

                # MateConf file could be empty
                try:
                    tree = ElementTree.parse(mateconf_file)
                    root = tree.getroot()
                except:
                    continue

                for child in root:
                    # if the key exists, the user has a custom value
                    if child.attrib['name'] == mateconf_key:

                        # boolean
                        if child.attrib["type"] == "bool":
                            if child.attrib["value"] == "true":
                                debug_message(gsettings_schema, gsettings_key, True)
                                settings.set_boolean(gsettings_key, True)
                            else:
                                debug_message(gsettings_schema, gsettings_key, False)
                                settings.set_boolean(gsettings_key, False)

                        # int
                        elif child.attrib["type"] == "int":
                            debug_message(gsettings_schema, gsettings_key, int(child.attrib["value"]))
                            settings.set_int(gsettings_key, int(child.attrib["value"]))

                        # string
                        elif child.attrib["type"] == "string":

                            value = child[0].text
                            # hack for background
                            if gsettings_schema == "org.mate.background" and gsettings_key == "picture-filename":
                                # check if the background exists before apply it
                                if not os.path.exists(value):
                                    if "/pixmaps/backgrounds/" in value:
                                        value = value.replace("/pixmaps/backgrounds/", "/backgrounds/")
                                        if not os.path.exists(value):
                                            no_background = True
                                            continue
                                    else:
                                        no_background = True
                                        continue
                            if no_background and gsettings_schema == "org.mate.background":
                                continue

                            debug_message(gsettings_schema, gsettings_key, value)
                            settings.set_string(gsettings_key, value)

                        # list
                        elif child.attrib["type"] == "list":

                            # list of strings
                            if child.attrib["ltype"] == "string":
                                
                                if len(list(child)) > 0:
                                    strv = []
                                    for ch in child:
                                        for li in ch:
                                            strv.append(li.text)
                                    debug_message(gsettings_schema, gsettings_key, strv)
                                    settings.set_strv(gsettings_key, strv)
                                else:
                                    debug_message(gsettings_schema, gsettings_key, "[]")
                                    settings.set_strv(gsettings_key, [])
                            else:
                                debug_message("Unknown list type", child.attrib["ltype"])

                        else:
                            debug_message("Unknown type", child.attrib["type"])

        settings.apply()

# ugly hack to migrate panel layout
mateconf_panel_file = os.path.join(mateconf_user_path, "apps/panel/general", "%mateconf.xml")
if os.path.exists(mateconf_panel_file):

    # load list of panels
    tree = ElementTree.parse(mateconf_panel_file)
    root = tree.getroot()

    # old lists
    toplevels = mateconf_get_string_list (root, "toplevel_id_list")
    objects = mateconf_get_string_list (root, "object_id_list")
    applets = mateconf_get_string_list (root, "applet_id_list")

    panel_settings = Gio.Settings.new("org.mate.panel")
    panel_settings.delay()

    # new lists (applets are together with objects in 1.6)
    new_toplevels = []
    new_objects = []

    # import objects
    for obj in objects:
        debug_message("object", obj)
        mateconf_obj_file = os.path.join(mateconf_user_path, "apps/panel/objects", obj, "%mateconf.xml")
        obj_tree = ElementTree.parse(mateconf_obj_file)
        obj_root = obj_tree.getroot()
        obj_id = gsettings_id(obj)
        obj_settings = Gio.Settings.new_with_path("org.mate.panel.object", "/org/mate/panel/objects/%s/" % obj_id)
        new_objects.append(obj_id)

        for child in obj_root:
            # toplevel-id
            if child.attrib["name"] == "toplevel_id":
                obj_settings.set_string("toplevel-id", gsettings_id(child[0].text))
            # object-type
            elif child.attrib["name"] == "object_type":
                obj_type = child[0].text
                # changed types in 1.6
                if obj_type == "launcher-object":
                    obj_type = "launcher"
                elif obj_type == "action-applet":
                    obj_type = "action"
                obj_settings.set_string("object-type", obj_type)
            # other settings
            elif "type" in child.attrib:
                name = gsettings_id(child.attrib["name"])
                # boolean
                if child.attrib["type"] == "bool":
                    if child.attrib["value"] == "true":
                        obj_settings.set_boolean(name, True)
                    else:
                        obj_settings.set_boolean(name, False)
                # int
                elif child.attrib["type"] == "int":
                    obj_settings.set_int(name, int(child.attrib["value"]))
                # string
                elif child.attrib["type"] == "string":
                    obj_settings.set_string(name, child[0].text)

    # import applets
    for app in applets:
        debug_message("applet", app)
        mateconf_app_file = os.path.join(mateconf_user_path, "apps/panel/applets", app, "%mateconf.xml")
        app_tree = ElementTree.parse(mateconf_app_file)
        app_root = app_tree.getroot()
        app_id = gsettings_id(app)
        app_settings = Gio.Settings.new_with_path("org.mate.panel.object", "/org/mate/panel/objects/%s/" % app_id)
        new_objects.append(app_id)

        for child in app_root:
            # toplevel-id
            if child.attrib["name"] == "toplevel_id":
                app_settings.set_string("toplevel-id", gsettings_id(child[0].text))
            # object-type
            elif child.attrib["name"] == "object_type":
                app_type = child[0].text
                if app_type == "external-applet":
                    app_type = "applet"
                app_settings.set_string("object-type", app_type)
            # applet-iid
            elif child.attrib["name"] == "applet_iid":
                applet_iid = child[0].text
                # changed iids in 1.6
                if applet_iid == "OAFIID:SensorsApplet":
                    applet_iid = "SensorsAppletFactory::SensorsApplet"
                elif applet_iid == "OAFIID:MATE_NetspeedApplet":
                    applet_iid = "NetspeedAppletFactory::NetspeedApplet"
                app_settings.set_string("applet-iid", applet_iid)
            # other settings
            elif "type" in child.attrib:
                name = gsettings_id(child.attrib["name"])
                # boolean
                if child.attrib["type"] == "bool":
                    if child.attrib["value"] == "true":
                        app_settings.set_boolean(name, True)
                    else:
                        app_settings.set_boolean(name, False)
                # int
                elif child.attrib["type"] == "int":
                    app_settings.set_int(name, int(child.attrib["value"]))
                # string
                elif child.attrib["type"] == "string":
                    app_settings.set_string(name, child[0].text)

    # import panels
    for toplevel in toplevels:
        debug_message("toplevel", toplevel)
        mateconf_toplevel_file = os.path.join(mateconf_user_path, "apps/panel/toplevels", toplevel, "%mateconf.xml")
        toplevel_tree = ElementTree.parse(mateconf_toplevel_file)
        toplevel_root = toplevel_tree.getroot()
        toplevel_id = gsettings_id(toplevel)
        toplevel_settings = Gio.Settings.new_with_path("org.mate.panel.toplevel", "/org/mate/panel/toplevels/%s/" % toplevel_id)
        new_toplevels.append(toplevel_id)

        for child in toplevel_root:
            if "type" in child.attrib:
                name = gsettings_id(child.attrib["name"])
                # boolean
                if child.attrib["type"] == "bool":
                    if child.attrib["value"] == "true":
                        toplevel_settings.set_boolean(name, True)
                    else:
                        toplevel_settings.set_boolean(name, False)
                # int
                elif child.attrib["type"] == "int":
                    toplevel_settings.set_int(name, int(child.attrib["value"]))
                # string
                elif child.attrib["type"] == "string":
                    toplevel_settings.set_string(name, child[0].text)

    # set new toplevels and objects
    panel_settings.set_strv("toplevel-id-list", new_toplevels)
    panel_settings.set_strv("object-id-list", new_objects)

    # apply settings!
    panel_settings.apply()

# success!
dialog = Gtk.MessageDialog (None,
                            Gtk.DialogFlags.MODAL,
                            Gtk.MessageType.INFO,
                            Gtk.ButtonsType.OK,
                            "MATE 1.4 settings migration completed!")
dialog.run()
dialog.destroy()