Skip to content

Commit

Permalink
Gtk.Application (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
danirabbit authored Dec 7, 2023
1 parent 9d92e99 commit ea4e121
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 128 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ add_global_arguments('-DGETTEXT_PACKAGE="@0@"'.format(meson.project_name()), lan

executable(
meson.project_name(),
'src/Application.vala',
'src/Agent.vala',
'src/PolkitDialog.vala',
'src/Interfaces.vala',
'src/Utils.vala',
dependencies: [
dependency('glib-2.0'),
dependency('gobject-2.0'),
Expand Down
4 changes: 2 additions & 2 deletions po/POTFILES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
src/Agent.vala
src/PolkitDialog.vala
src/Application.vala
src/Interfaces.vala
src/Utils.vala
src/PolkitDialog.vala
58 changes: 0 additions & 58 deletions src/Agent.vala
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@

namespace Ag {
public class Agent : PolkitAgent.Listener {
public Agent () {
register_with_session.begin ((obj, res)=> {
bool success = register_with_session.end (res);
if (!success) {
warning ("Failed to register with Session manager");
}
});
}

public override async bool initiate_authentication (string action_id, string message, string icon_name,
Polkit.Details details, string cookie, GLib.List<Polkit.Identity> identities, GLib.Cancellable? cancellable) throws Polkit.Error {
if (identities == null) {
Expand All @@ -54,54 +45,5 @@ namespace Ag {

return true;
}

private async bool register_with_session () {
var sclient = yield Utils.register_with_session ("io.elementary.pantheon-agent-polkit");
if (sclient == null) {
return false;
}

sclient.query_end_session.connect ((flags) => session_respond (sclient, flags));
sclient.end_session.connect ((flags) => session_respond (sclient, flags));
sclient.stop.connect (session_stop);

return true;
}

private void session_respond (SessionClient sclient, uint flags) {
try {
sclient.end_session_response (true, "");
} catch (Error e) {
warning ("Unable to respond to session manager: %s", e.message);
}
}

private void session_stop () {
Gtk.main_quit ();
}
}

public static int main (string[] args) {
Gtk.init (ref args);

var agent = new Agent ();
int pid = Posix.getpid ();

Polkit.Subject? subject = null;
try {
subject = new Polkit.UnixSession.for_process_sync (pid, null);
} catch (Error e) {
critical ("Unable to initiate Polkit: %s", e.message);
return 1;
}

try {
PolkitAgent.register_listener (agent, subject, null);
} catch (Error e) {
return 1;
}

Gtk.main ();
return 0;
}
}
42 changes: 42 additions & 0 deletions src/Application.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* SPDX-FileCopyrightText: 2023 elementary, Inc. (https://elementary.io)
*/

public class Ag.Application : Gtk.Application {
public Application () {
Object (
application_id: "io.elementary.PolkitAgent",
flags: GLib.ApplicationFlags.IS_SERVICE,
register_session: true
);
}

protected override void startup () {
base.startup ();

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

granite_settings.notify["prefers-color-scheme"].connect (() =>
gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == DARK
);

gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == DARK;

var agent = new Agent ();
try {
var subject = new Polkit.UnixSession.for_process_sync (Posix.getpid (), null);
agent.register (NONE, subject, resource_base_path, null);
} catch (Error e) {
critical ("Unable to initiate Polkit: %s", e.message);
quit ();
}

hold ();
}

public static int main (string[] args) {
return new Ag.Application ().run (args);
}
}
9 changes: 0 additions & 9 deletions src/PolkitDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,6 @@ public class Ag.PolkitDialog : Granite.MessageDialog {

update_idents ();
select_session ();

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

gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;

granite_settings.notify["prefers-color-scheme"].connect (() => {
gtk_settings.gtk_application_prefer_dark_theme = granite_settings.prefers_color_scheme == Granite.Settings.ColorScheme.DARK;
});
}

public override void show () {
Expand Down
58 changes: 0 additions & 58 deletions src/Utils.vala

This file was deleted.

0 comments on commit ea4e121

Please sign in to comment.