Debian Codegraph @

debian/update-notification-settings

raw
1 #!/usr/bin/python3
2 #
3 # Copyright (C) 2014-2016 Canonical Ltd.
4 # Copyright (C) 2021 Ratchanan Srirattanamet <[email protected]>
5 # Copyright (C) 2022-2024 Guido Berhoerster <[email protected]>
6 #
7 # This program is free software: you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License version 3, as published
9 # by the Free Software Foundation.
10 # This program is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranties of
12 # MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
13 # PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
17 """Populate notification settings from installed packages."""
18
19 import argparse
20 import json
21 import logging
22 from pathlib import Path
23
24 import gi
25 from gi.repository import GLib
26 from gi.repository import Gio
27
28
29 def populate_settings_from_legacy_dpkg():
30 legacy_helpers_path = Path("/usr/lib/lomiri-push-service/legacy-helpers/")
31
32 if not legacy_helpers_path.is_dir():
33 return []
34
35 dpkg_apps_keys = []
36 for f in legacy_helpers_path.iterdir():
37 if f.is_file():
38 dpkg_apps_keys.append("/".join(["dpkg", f.name, "0"]))
39 logging.debug(f"adding notification setting for app {f.name}")
40 return dpkg_apps_keys
41
42
43 def reset_settings(path=None):
44 if not path:
45 return
46
47 logging.debug(
48 f'resetting notification setting for removed application "{path}"'
49 )
50 settings_path = f"/com/lomiri/NotificationSettings/{path}/"
51 try:
52 settings = Gio.Settings.new_with_path(
53 "com.lomiri.notifications.settings", settings_path
54 )
55 for k in settings.list_keys():
56 settings.reset(k)
57 except Exception as e:
58 logging.warning(f"failed to reset settings: {e}")
59
60
61 def reset_removed_applications(current_apps=None, new_apps=None):
62 if not current_apps or not new_apps:
63 return
64
65 current_keys = ["/".join(k.split("/")[:2]) for k in current_apps]
66 new_keys = ["/".join(k.split("/")[:2]) for k in new_apps]
67 for settings_path in set(current_keys) - set(new_keys):
68 reset_settings(settings_path)
69
70
71 def populate_notifications_settings():
72 logging.debug(f"populating notification settings...")
73
74 settings = Gio.Settings.new(
75 "com.lomiri.notifications.settings.applications"
76 )
77 applications = GLib.VariantBuilder.new(GLib.VariantType.new("as"))
78
79 current_applications_keys = settings.get_value("applications").unpack()
80
81 applications_keys = populate_settings_from_legacy_dpkg()
82 for app_key in applications_keys:
83 applications.add_value(GLib.Variant.new_string(app_key))
84
85 settings.set_value("applications", applications.end())
86
87 reset_removed_applications(current_applications_keys, applications_keys)
88
89
90 if __name__ == "__main__":
91 parser = argparse.ArgumentParser(description=__doc__)
92 parser.add_argument("-D", "--debug",
93 help="Enable debug logging",
94 action="store_true",
95 default=False)
96 args = parser.parse_args()
97 if args.debug:
98 logging.getLogger().setLevel(logging.DEBUG)
99
100 populate_notifications_settings()