~jonas-drange/ubuntu-system-settings/filepicker

« back to all changes in this revision

Viewing changes to debian/ubuntu-system-settings-keyboard-accountsservice-to-gsettings.py

  • Committer: Jonas G. Drange
  • Date: 2016-10-14 13:59:02 UTC
  • mfrom: (1673.1.55 ubuntu-system-settings)
  • Revision ID: jonas.drange@canonical.com-20161014135902-9xufvqt524iaurit
sync trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
# -*- coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
 
3
#
 
4
# Copyright (C) 2016 Canonical
 
5
#
 
6
# This program is free software; you can redistribute it and/or modify it under
 
7
# the terms of the GNU General Public License as published by the Free Software
 
8
# Foundation; version 3.
 
9
#
 
10
# This program is distributed in the hope that it will be useful, but WITHOUTa
 
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
12
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
13
# details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along with
 
16
# this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
 
 
19
# Copy ubuntu-system-settings keyboard panel settings from AccountsService to
 
20
# GSettings.  This might seem backwards (AS has the more modern location),
 
21
# and early on in Ubuntu Touch days, we did indeed only store the values in
 
22
# AccountsService.  But once we started using indicator-keyboard, which read
 
23
# from GSettings, it was easier to copy the values to both places rather than
 
24
# update the indicator and all other legacy consumers of the GSettings key in
 
25
# Ubuntu.  So we do a one-time sync here if we have values in AccountsService
 
26
# but not GSettings.  Along with a change to our code to write to both places
 
27
# when the user adjust settings.
 
28
 
 
29
 
 
30
import dbus
 
31
import os
 
32
import sys
 
33
 
 
34
from gi.repository import Gio
 
35
from gi.repository import GLib
 
36
 
 
37
 
 
38
system_bus = dbus.SystemBus()
 
39
manager_proxy = system_bus.get_object('org.freedesktop.Accounts',
 
40
                                      '/org/freedesktop/Accounts')
 
41
object_path = manager_proxy.FindUserById(
 
42
    os.getuid(), dbus_interface='org.freedesktop.Accounts'
 
43
)
 
44
user_proxy = system_bus.get_object('org.freedesktop.Accounts', object_path)
 
45
 
 
46
gsettings = Gio.Settings(schema="org.gnome.desktop.input-sources")
 
47
 
 
48
 
 
49
def get_accountsservice():
 
50
    try:
 
51
        return user_proxy.Get("org.freedesktop.Accounts.User", "InputSources",
 
52
                              dbus_interface=dbus.PROPERTIES_IFACE)
 
53
    except dbus.exceptions.DBusException as e:
 
54
        print("Couldn't get InputSources: %s" % (e), file=sys.stderr)
 
55
        return []
 
56
 
 
57
 
 
58
def has_gsettings():
 
59
    return bool(gsettings.get_value("sources"))
 
60
 
 
61
 
 
62
def set_gsettings(as_value):
 
63
    # AS stores a list of maps, GSettings stores a list of tuples.
 
64
    builder = GLib.VariantBuilder.new(GLib.VariantType.new("a(ss)"))
 
65
    for as_map in as_value:
 
66
        for as_key in as_map:
 
67
            gkey = GLib.Variant.new_string(as_key)
 
68
            gvalue = GLib.Variant.new_string(as_map[as_key])
 
69
            gtuple = GLib.Variant.new_tuple(gkey, gvalue)
 
70
            builder.add_value(gtuple)
 
71
    gsettings.set_value("sources", builder.end())
 
72
 
 
73
 
 
74
as_value = get_accountsservice()
 
75
if as_value and not has_gsettings():
 
76
    set_gsettings(as_value)