Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Daemon: Notify, even if app is closed #51

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/LoginManager.vala → daemon/DBusService.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020 Marco Betschart (https://marco.betschart.name)
* Copyright (c) 2021 Marco Betschart (https://marco.betschart.name)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
Expand All @@ -19,7 +19,7 @@
* Authored by: Marco Betschart <[email protected]
*/

[DBus (name = "org.freedesktop.login1.Manager")]
interface LoginManager : GLib.Object {
public signal void prepare_for_sleep (bool start);
}
[DBus (name = "com.github.marbetschar.TimeLimit")]
public class TimeLimit.DBusService : Object {
public string alert_datetime_iso8601 { get; set; default = ""; }
}
126 changes: 126 additions & 0 deletions daemon/Daemon.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* Copyright (c) 2021 Marco Betschart (https://marco.betschart.name)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Marco Betschart <[email protected]
*/

namespace TimeLimit {
private static bool has_debug;

const OptionEntry[] OPTIONS = {
{ "debug", 'd', 0, OptionArg.NONE, out has_debug,
N_("Print debug information"), null},
{ null }
};

public class Daemon : GLib.Application {

private uint timelimit_dbus_service_registration_id = 0;
private uint alert_timeout = 0;

private DBusService? timelimit_dbus_service = null;

public Daemon () {
Object (
application_id: "com.github.marbetschar.time-limit-daemon",
flags: ApplicationFlags.FLAGS_NONE
);
}

construct {
timelimit_dbus_service = new DBusService ();
timelimit_dbus_service.notify["alert-datetime-iso8601"].connect (on_timelimit_alert_datetime_changed);

Bus.own_name (
BusType.SESSION, "com.github.marbetschar.TimeLimit",
BusNameOwnerFlags.NONE,
(connection, name) => {
debug ("Aquired DBus connection named '%s'", name);
try {
timelimit_dbus_service_registration_id = connection.register_object ("/com/github/marbetschar/timelimit", timelimit_dbus_service);
} catch (GLib.Error e) {
critical ("Error while aquiring DBus connection named '%s': %s", name, e.message);
}
},
() => {},
(connection, name) => {
if (timelimit_dbus_service_registration_id != 0) {
connection.unregister_object (timelimit_dbus_service_registration_id);
timelimit_dbus_service_registration_id = 0;

}
critical ("Could not aquire DBus connection named '%s', or the connection was closed.", name);
}
);
}

private void on_timelimit_alert_datetime_changed () {
if (alert_timeout > 0) {
debug ("on_timelimit_alert_datetime_changed: Removing scheduled notification.");
GLib.Source.remove (alert_timeout);
alert_timeout = 0;
}

var now = new GLib.DateTime.now_local ();
var alert_datetime = now;
if (timelimit_dbus_service.alert_datetime_iso8601 != "") {
alert_datetime = new GLib.DateTime.from_iso8601 (timelimit_dbus_service.alert_datetime_iso8601, null);
}

var seconds_remaining = alert_datetime.difference (now) / 1000000;
if (seconds_remaining > 0) {
debug ("on_timelimit_alert_datetime_changed: Schedule notification for: %s", timelimit_dbus_service.alert_datetime_iso8601);

alert_timeout = GLib.Timeout.add_seconds ((uint) seconds_remaining, () => {
var notification = new Notification (_("It's time!"));
notification.set_body (_("Your time limit is over."));
notification.set_priority (NotificationPriority.URGENT);

send_notification ("com.github.marbetschar.time-limit", notification);

return GLib.Source.REMOVE;
});
}
}

protected override void activate () {
Gtk.main ();
}
}

public static int main (string[] args) {
OptionContext context = new OptionContext ("");
context.add_main_entries (OPTIONS, null);

try {
context.parse (ref args);
} catch (OptionError e) {
error (e.message);
}

Granite.Services.Logger.initialize ("TimeLimit");
Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.WARN;

if (has_debug) {
Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.DEBUG;
}

var app = new Daemon ();
return app.run (args);
}
}
17 changes: 17 additions & 0 deletions daemon/meson.build
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
daemon_deps = [
gio_dep,
glib_dep,
granite_dep,
m_dep
]

executable(
meson.project_name()+'-daemon',
[
'Daemon.vala',
'DBusService.vala'
],
dependencies: daemon_deps,
install: true,
install_dir: join_paths(get_option('prefix'), get_option('libexecdir'))
)
13 changes: 13 additions & 0 deletions data/com.github.marbetschar.time-limit-daemon.desktop.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Desktop Entry]
Name=Time Limit
Comment=Notify when time is up
Exec=@EXEC_PATH@
Icon=com.github.marbetschar.time-limit
Terminal=false
Type=Application
Categories=System;
OnlyShowIn=Pantheon;
NoDisplay=true
X-GNOME-AutoRestart=true
X-GNOME-Autostart-Phase=Applications
X-GNOME-UsesNotifications=true
9 changes: 9 additions & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ foreach i : icon_sizes
)
endforeach

desktop_config = configuration_data()
desktop_config.set('EXEC_PATH', libexecdir+'-daemon')
configure_file (
input: meson.project_name()+'-daemon' + '.desktop.in',
output: meson.project_name()+'-daemon' + '.desktop',
configuration: desktop_config,
install_dir: join_paths(get_option('datadir'), 'applications')
)

i18n.merge_file(
input: meson.project_name() + '.desktop.in',
output: meson.project_name() + '.desktop',
Expand Down
15 changes: 12 additions & 3 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,27 @@ add_project_arguments([
language: 'c'
)

libexecdir = join_paths(get_option('prefix'), get_option('libexecdir'), meson.project_name())

asresources = gnome.compile_resources(
'as-resources',
'data/' + meson.project_name() + '.gresource.xml',
source_dir: 'data',
c_name: 'as'
)

gio_dep = dependency('gio-2.0')
glib_dep = dependency('glib-2.0')
granite_dep = dependency('granite', version: '>=0.5')
m_dep = meson.get_compiler('c').find_library('m', required : false)

dependencies = [
dependency('glib-2.0'),
gio_dep,
glib_dep,
granite_dep,
dependency('gtk+-3.0'),
dependency('granite', version: '>=0.5'),
dependency('libhandy-1', version: '>=0.83.0'),
meson.get_compiler('c').find_library('m', required : false)
m_dep
]

subdir('src')
Expand All @@ -36,6 +44,7 @@ executable(
)

subdir('data')
subdir('daemon')
subdir('po')

meson.add_install_script('meson/post_install.py')
11 changes: 11 additions & 0 deletions po/POTFILES
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
daemon/Daemon.vala
daemon/DBusService.vala
src/Widgets/Clock.vala
src/Widgets/Face.vala
src/Widgets/Labels.vala
src/Widgets/ProgressArrow.vala
src/Widgets/ProgressBar.vala
src/Widgets/ProgressIndicator.vala
src/Application.vala
src/DBusService.vala
src/MainWindow.vala
src/Uitl.vala
6 changes: 1 addition & 5 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Authored by: Marco Betschart <[email protected]
*/

public class Timer.Application : Gtk.Application {
public class TimeLimit.Application : Gtk.Application {
public static GLib.Settings settings;

public Application () {
Expand Down Expand Up @@ -72,10 +72,6 @@ public class Timer.Application : Gtk.Application {
}
});

main_window.send_notification.connect ((notification) => {
send_notification("com.github.marbetschar.time-limit", notification);
});

var granite_settings = Granite.Settings.get_default ();
var gtk_settings = Gtk.Settings.get_default ();

Expand Down
25 changes: 25 additions & 0 deletions src/DBusService.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2021 Marco Betschart (https://marco.betschart.name)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Marco Betschart <[email protected]
*/

[DBus (name = "com.github.marbetschar.TimeLimit")]
public interface TimeLimit.DBusService : Object {
public abstract string alert_datetime_iso8601 { owned get; set; }
}
13 changes: 6 additions & 7 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
* Authored by: Marco Betschart <[email protected]
*/

public class Timer.MainWindow : Hdy.ApplicationWindow {
public class TimeLimit.MainWindow : Hdy.ApplicationWindow {

public signal void send_notification (Notification notification);
private uint configure_id;

public MainWindow (Gtk.Application application) {
Expand Down Expand Up @@ -51,7 +50,7 @@ public class Timer.MainWindow : Hdy.ApplicationWindow {

header.get_style_context ().add_class (Gtk.STYLE_CLASS_FLAT);

var clock = new Timer.Widgets.Clock (header);
var clock = new TimeLimit.Widgets.Clock (header);
add (clock);

key_release_event.connect ((event) => {
Expand All @@ -77,17 +76,17 @@ public class Timer.MainWindow : Hdy.ApplicationWindow {
configure_id = 0;

if (is_maximized) {
Timer.Application.settings.set_boolean ("window-maximized", true);
TimeLimit.Application.settings.set_boolean ("window-maximized", true);
} else {
Timer.Application.settings.set_boolean ("window-maximized", false);
TimeLimit.Application.settings.set_boolean ("window-maximized", false);

Gdk.Rectangle rect;
get_allocation (out rect);
Timer.Application.settings.set ("window-size", "(ii)", rect.width, rect.height);
TimeLimit.Application.settings.set ("window-size", "(ii)", rect.width, rect.height);

int root_x, root_y;
get_position (out root_x, out root_y);
Timer.Application.settings.set ("window-position", "(ii)", root_x, root_y);
TimeLimit.Application.settings.set ("window-position", "(ii)", root_x, root_y);
}

return Source.REMOVE;
Expand Down
2 changes: 1 addition & 1 deletion src/Util.vala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Authored by: Marco Betschart <[email protected]
*/

namespace Timer.Util {
namespace TimeLimit.Util {

public double truncating_remainder (double x, double y) {
var q = Math.trunc (x / y);
Expand Down
Loading