Replaced state cacher with gsettings

This commit is contained in:
Erik Reider 2022-06-19 14:01:12 +02:00
parent 8425afbfa8
commit aab565d723
9 changed files with 42 additions and 94 deletions

13
build-aux/meson/postinstall.py Executable file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
from os import environ, path
from subprocess import call
prefix = environ.get('MESON_INSTALL_PREFIX', '/usr/local')
datadir = path.join(prefix, 'share')
destdir = environ.get('DESTDIR', '')
# Package managers set this so we don't need to run
if not destdir:
print('Compiling GSettings schemas...')
call(['glib-compile-schemas', path.join(datadir, 'glib-2.0', 'schemas')])

10
data/meson.build Normal file
View File

@ -0,0 +1,10 @@
install_data('org.erikreider.swaync.gschema.xml',
install_dir: join_paths(get_option('datadir'), 'glib-2.0/schemas')
)
compile_schemas = find_program('glib-compile-schemas', required: false)
if compile_schemas.found()
test('Validate schema file', compile_schemas,
args: ['--strict', '--dry-run', meson.current_source_dir()]
)
endif

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="swaync">
<schema id="org.erikreider.swaync" path="/org/erikreider/swaync/">
<key name="dnd-state" type="b">
<default>false</default>
<summary>The current do not disturb state</summary>
<description>Whether notifications should be silent or not</description>
</key>
</schema>
</schemalist>

View File

@ -7,6 +7,9 @@ project('sway-notificaton-center', ['c', 'vala'],
add_project_arguments(['--enable-gobject-tracing'], language: 'vala')
add_project_arguments(['--enable-checking'], language: 'vala')
i18n = import('i18n')
subdir('data')
subdir('src')
datadir = get_option('datadir')

View File

@ -1,79 +0,0 @@
namespace SwayNotificationCenter {
public class Cacher {
private static Cacher _instance;
/** Get the static singleton */
public static unowned Cacher instance {
get {
if (_instance == null) _instance = new Cacher ();
return _instance;
}
}
private const string CACHE_FILE_STATE = "swaync-state.cache";
private const FileCreateFlags FILE_FLAGS = FileCreateFlags.PRIVATE
| FileCreateFlags.REPLACE_DESTINATION;
private string get_cache_path () {
string path = Path.build_filename (Environment.get_user_cache_dir (),
CACHE_FILE_STATE);
File file = File.new_for_path (path);
if (!file.query_exists ()) {
try {
file.create (FILE_FLAGS);
} catch (Error e) {
stderr.printf ("Error: %s\n", e.message);
}
}
return path;
}
public bool cache_state (StateCache state) {
string path = get_cache_path ();
Json.Node json = Json.gobject_serialize (state);
string data = Json.to_string (json, true);
try {
File file = File.new_for_path (path);
return file.replace_contents (
data.data,
null,
false,
FILE_FLAGS,
null);
} catch (Error e) {
stderr.printf ("Cache state write error: %s\n", e.message);
return false;
}
}
public StateCache get_state_cache () {
string path = get_cache_path ();
StateCache s = null;
try {
Json.Parser parser = new Json.Parser ();
parser.load_from_file (path);
Json.Node ? node = parser.get_root ();
if (node == null) {
throw new Json.ParserError.PARSE ("Node is null!");
}
StateCache model = Json.gobject_deserialize (
typeof (StateCache), node) as StateCache;
if (model == null) {
throw new Json.ParserError.UNKNOWN ("Json model is null!");
}
s = model;
} catch (Error e) {
stderr.printf (e.message + "\n");
}
return s ?? new StateCache ();
}
}
public class StateCache : Object {
public bool dnd_state { get; set; default = false; }
}
}

View File

@ -3,11 +3,15 @@ namespace SwayNotificationCenter {
static string ? style_path;
static string ? config_path;
static Settings self_settings;
public void main (string[] args) {
Gtk.init (ref args);
Hdy.init ();
Functions.init ();
self_settings = new Settings ("org.erikreider.swaync");
if (args.length > 0) {
for (uint i = 1; i < args.length; i++) {
string arg = args[i];

View File

@ -32,7 +32,6 @@ app_sources = [
'notification/notification.vala',
'controlCenter/controlCenter.vala',
'controlCenter/topAction/topAction.vala',
'cacher/cacher.vala',
'blankWindow/blankWindow.vala',
'functions.vala',
constants,

View File

@ -12,12 +12,8 @@ namespace SwayNotificationCenter {
public NotiDaemon (SwayncDaemon swaync_daemon) {
this.notify["dnd"].connect (() => on_dnd_toggle (dnd));
// Init from state cache
swaync_daemon.cache_state.bind_property ("dnd-state",
this,
"dnd",
BindingFlags.BIDIRECTIONAL
| BindingFlags.SYNC_CREATE);
// Init dnd from gsettings
self_settings.bind ("dnd-state", this, "dnd", SettingsBindFlags.DEFAULT);
this.noti_window = new NotificationWindow ();
this.control_center = new ControlCenter (swaync_daemon, this);

View File

@ -7,20 +7,12 @@ namespace SwayNotificationCenter {
[DBus (name = "org.erikreider.swaync.cc")]
public class SwayncDaemon : Object {
public StateCache cache_state;
public NotiDaemon noti_daemon;
private Array<BlankWindow> blank_windows = new Array<BlankWindow> ();
private unowned Gdk.Display ? display = Gdk.Display.get_default ();
public SwayncDaemon () {
this.cache_state = Cacher.instance.get_state_cache ();
this.cache_state.notify.connect ((x, r) => {
debug ("State changed: %s\n", r.name);
Cacher.instance.cache_state (cache_state);
});
// Init noti_daemon
this.noti_daemon = new NotiDaemon (this);
Bus.own_name (BusType.SESSION, "org.freedesktop.Notifications",