| 1 |
#!/usr/bin/python3 |
| 2 |
# -*- python -*- |
| 3 |
"""Migrate notification settings.""" |
| 4 |
|
| 5 |
import argparse |
| 6 |
import logging |
| 7 |
|
| 8 |
import gi |
| 9 |
from gi.repository import Gio |
| 10 |
|
| 11 |
logging.basicConfig(level=logging.INFO) |
| 12 |
|
| 13 |
|
| 14 |
def gschema_exists(schema): |
| 15 |
source = Gio.SettingsSchemaSource.get_default() |
| 16 |
if not source: |
| 17 |
return False |
| 18 |
|
| 19 |
return source.lookup(schema, True) != None |
| 20 |
|
| 21 |
def migrate_old_settings(): |
| 22 |
logging.debug(f"migrating old notification settings...") |
| 23 |
if not gschema_exists("com.ubuntu.notifications.hub"): |
| 24 |
logging.debug(f"skipping settings migration: schema does not exist") |
| 25 |
return |
| 26 |
|
| 27 |
old_settings = Gio.Settings.new("com.ubuntu.notifications.hub") |
| 28 |
blacklist = old_settings.get_value("blacklist").unpack() |
| 29 |
if not blacklist: |
| 30 |
logging.debug(f"skipping settings migration: setting does not exist") |
| 31 |
return |
| 32 |
|
| 33 |
old_settings.reset("blacklist") |
| 34 |
|
| 35 |
for app in blacklist: |
| 36 |
app_key = "/".join([app[0] if app[0] else "dpkg", app[1]]) |
| 37 |
settings_path = "/com/lomiri/NotificationSettings/{app_key}/" |
| 38 |
settings = Gio.Settings.new_with_path( |
| 39 |
"com.lomiri.notifications.settings", settings_path |
| 40 |
) |
| 41 |
settings.set_boolean("enable-notifications", False) |
| 42 |
logging.debug(f"migrating notification setting for {app_key}") |
| 43 |
|
| 44 |
|
| 45 |
if __name__ == "__main__": |
| 46 |
parser = argparse.ArgumentParser(description=__doc__) |
| 47 |
parser.add_argument("-D", "--debug", |
| 48 |
help="Enable debug logging", |
| 49 |
action="store_true", |
| 50 |
default=False) |
| 51 |
args = parser.parse_args() |
| 52 |
if args.debug: |
| 53 |
logging.getLogger().setLevel(logging.DEBUG) |
| 54 |
migrate_old_settings() |
| 55 |
logging.info("Notification settings were migrated successfully") |