| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- python -*- |
| 3 |
"""Populate notification settings from installed packages.""" |
| 4 |
|
| 5 |
import argparse |
| 6 |
import json |
| 7 |
import logging |
| 8 |
from pathlib import Path |
| 9 |
|
| 10 |
import xdg.BaseDirectory |
| 11 |
|
| 12 |
import gi |
| 13 |
gi.require_version("Click", "0.4") |
| 14 |
from gi.repository import GLib |
| 15 |
from gi.repository import Gio |
| 16 |
from gi.repository import Click |
| 17 |
|
| 18 |
|
| 19 |
def populate_settings_from_legacy_dpkg(): |
| 20 |
legacy_helpers_path = Path("/usr/lib/lomiri-push-service/legacy-helpers/") |
| 21 |
|
| 22 |
if not legacy_helpers_path.is_dir(): |
| 23 |
return [] |
| 24 |
|
| 25 |
dpkg_apps_keys = [] |
| 26 |
for f in legacy_helpers_path.iterdir(): |
| 27 |
if f.is_file(): |
| 28 |
dpkg_apps_keys.append("/".join(["dpkg", f.name, "0"])) |
| 29 |
logging.debug(f"adding notification setting for app {f.name}") |
| 30 |
return dpkg_apps_keys |
| 31 |
|
| 32 |
|
| 33 |
def reset_settings(path=None): |
| 34 |
if not path: |
| 35 |
return |
| 36 |
|
| 37 |
logging.debug( |
| 38 |
f'resetting notification setting for removed application "{path}"' |
| 39 |
) |
| 40 |
settings_path = f"/com/lomiri/NotificationSettings/{path}/" |
| 41 |
try: |
| 42 |
settings = Gio.Settings.new_with_path( |
| 43 |
"com.lomiri.notifications.settings", settings_path |
| 44 |
) |
| 45 |
for k in settings.list_keys(): |
| 46 |
settings.reset(k) |
| 47 |
except Exception as e: |
| 48 |
logging.warning(f"failed to reset settings: {e}") |
| 49 |
|
| 50 |
|
| 51 |
def reset_removed_applications(current_apps=None, new_apps=None): |
| 52 |
if not current_apps or not new_apps: |
| 53 |
return |
| 54 |
|
| 55 |
current_keys = ["/".join(k.split("/")[:2]) for k in current_apps] |
| 56 |
new_keys = ["/".join(k.split("/")[:2]) for k in new_apps] |
| 57 |
for settings_path in set(current_keys) - set(new_keys): |
| 58 |
reset_settings(settings_path) |
| 59 |
|
| 60 |
|
| 61 |
def populate_notifications_settings(hooks_path): |
| 62 |
logging.debug(f"populating notification settings...") |
| 63 |
if not hooks_path or not Path(hooks_path).is_dir(): |
| 64 |
return |
| 65 |
|
| 66 |
db = Click.DB.new() |
| 67 |
db.read() |
| 68 |
|
| 69 |
settings = Gio.Settings.new( |
| 70 |
"com.lomiri.notifications.settings.applications" |
| 71 |
) |
| 72 |
applications = GLib.VariantBuilder.new(GLib.VariantType.new("as")) |
| 73 |
|
| 74 |
current_applications_keys = settings.get_value("applications").unpack() |
| 75 |
|
| 76 |
applications_keys = populate_settings_from_legacy_dpkg() |
| 77 |
for hook in Path(hooks_path).iterdir(): |
| 78 |
pkg_name, helper_name, version = hook.stem.split("_") |
| 79 |
|
| 80 |
manifest = json.loads(db.get_manifest_as_string(pkg_name, version)) |
| 81 |
hooks = manifest.get("hooks", None) |
| 82 |
if not hooks: |
| 83 |
continue |
| 84 |
|
| 85 |
app_names = [h for h in hooks.keys() if "desktop" in hooks[h].keys()] |
| 86 |
|
| 87 |
for app_name in app_names: |
| 88 |
app_key = "/".join([pkg_name, app_name, version]) |
| 89 |
logging.debug(f"adding notification setting for app {app_key}") |
| 90 |
applications_keys.append(app_key) |
| 91 |
|
| 92 |
for app_key in applications_keys: |
| 93 |
applications.add_value(GLib.Variant.new_string(app_key)) |
| 94 |
|
| 95 |
settings.set_value("applications", applications.end()) |
| 96 |
|
| 97 |
reset_removed_applications(current_applications_keys, applications_keys) |
| 98 |
|
| 99 |
|
| 100 |
if __name__ == "__main__": |
| 101 |
xdg_data_home = xdg.BaseDirectory.xdg_data_home |
| 102 |
|
| 103 |
parser = argparse.ArgumentParser(description=__doc__) |
| 104 |
parser.add_argument("-D", "--debug", |
| 105 |
help="Enable debug logging", |
| 106 |
action="store_true", |
| 107 |
default=False) |
| 108 |
parser.add_argument("-d", "--data-home", |
| 109 |
help="The Path to the (xdg) data home", |
| 110 |
default=xdg_data_home) |
| 111 |
args = parser.parse_args() |
| 112 |
if args.debug: |
| 113 |
logging.getLogger().setLevel(logging.DEBUG) |
| 114 |
hooks_path = Path(args.data_home) / "lomiri-push-service" / "helpers" |
| 115 |
|
| 116 |
populate_notifications_settings(hooks_path) |