Skip to content

Commit

Permalink
show the dialog only when required
Browse files Browse the repository at this point in the history
  • Loading branch information
Marukesu committed May 28, 2023
1 parent a1d271a commit 2a2c4fe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 17 deletions.
48 changes: 33 additions & 15 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,17 @@ namespace Maya {
}

protected override void activate () {
new Calendar.TodayEventMonitor ();
if (run_in_background) {
run_in_background = false;
new Calendar.TodayEventMonitor ();
hold ();

ask_for_background.begin ((obj, res) => {
if (!ask_for_background.end (res)) {
release ();
}
});

return;
}

Expand Down Expand Up @@ -101,8 +108,6 @@ namespace Maya {
});
}

ask_for_background ();

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

Expand Down Expand Up @@ -168,21 +173,34 @@ namespace Maya {
Calendar.EventStore.get_default ().delete_trashed_calendars ();
}

private void ask_for_background () {
var portal = Portal.Background.get ();
public async bool ask_for_background () {
string handle = active_window != null ? yield ((MainWindow) active_window).export () : "";
string[] cmd = { "io.elementary.calendar", "--background" };
bool can_background = true;

window.export.begin ((obj, res) => {
var options = new HashTable<string, Variant> (str_hash, str_equal);
options["handle_token"] = Portal.generate_token ();
options["reason"] = _("Calendar wants to initialize with the session");
options["commandline"] = cmd;
options["dbus-activatable"] = false;
options["autostart"] = true;
var options = new HashTable<string, Variant> (str_hash, str_equal);
options["handle_token"] = Portal.generate_token ();
options["reason"] = _("Calendar wants to run on background and initialize with the session for events notifications");
options["commandline"] = cmd;
options["autostart"] = true;
options["dbus-activatable"] = false;

// TODO: handle response
portal.request_background (window.export.end (res), options);
});
try {
ulong response_id = 0;

var request = Portal.Background.get ().request_background (handle, options);
response_id = request.response.connect ((res) => {
request.disconnect (response_id);
can_background = (res == 0);
ask_for_background.callback ();
});
yield;
} catch (Error e) {
warning ("cannot ask for background access: %s", e.message);
can_background = false;
}

return can_background;
}

public static DateTime get_selected_datetime () {
Expand Down
15 changes: 14 additions & 1 deletion src/MainWindow.vala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/*-
* Copyright (c) 2011-2021 elementary, Inc. (https://elementary.io)
* Copyright (c) 2011-2022 elementary, Inc. (https://elementary.io)
*
* 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
Expand Down Expand Up @@ -184,6 +184,19 @@ public class Maya.MainWindow : Hdy.ApplicationWindow {
return base.configure_event (event);
}

public override bool delete_event (Gdk.EventAny event) {
((Application) application).ask_for_background.begin ((obj, res) => {
unowned var app = (Application) obj;
if (app.ask_for_background.end (res)) {
hide ();
} else {
destroy ();
}
});

return Gdk.EVENT_STOP;
}

public async string export () {
var window = get_window ();

Expand Down
16 changes: 15 additions & 1 deletion src/Services/Portal.vala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ namespace Portal {
);
}

[DBus (name = "org.freedesktop.portal.Request")]
interface Request : Object {
public signal void response (uint response, HashTable<string, Variant> results);
public abstract void close () throws IOError, DBusError;
}

[DBus (name = "org.freedesktop.portal.Background")]
interface Background : Object {
public abstract uint version { get; }
Expand All @@ -42,6 +48,14 @@ namespace Portal {
return background;
}

public abstract ObjectPath request_background (string window_handle, HashTable<string, Variant> options) throws IOError, DBusError;
[DBus (visible = false)]
public Request request_background (string window_handle, HashTable<string, Variant> options) throws IOError, DBusError {
var connection = GLib.Application.get_default ().get_dbus_connection ();
var path = _request_background (window_handle, options);
return connection.get_proxy_sync (DBUS_DESKTOP_NAME, path);
}

[DBus (name = "RequestBackground")]
public abstract ObjectPath _request_background (string window_handle, HashTable<string, Variant> options) throws IOError, DBusError;
}
}
1 change: 1 addition & 0 deletions src/TodayEventMonitor.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

[SingleInstance]
public class Calendar.TodayEventMonitor : GLib.Object {
private Gee.HashMultiMap<ECal.Component, string> event_uids;

Expand Down

0 comments on commit 2a2c4fe

Please sign in to comment.