/
Client.vala
181 lines (158 loc) · 6.54 KB
/
Client.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// -*- Mode: vala; indent-tabs-mode: nil; tab-width: 4 -*-
/*-
* Copyright (c) 2015 Adam Bieńkowski (https://launchpad.net/switchboard-plug-parental-controls)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Adam Bieńkowski <donadigos159@gmail.com>
*/
namespace PC.Client {
public class Client : Gtk.Application {
private Polkit.Permission? permission = null;
private static string? app_name;
private static string? auth;
private const GLib.OptionEntry[] OPTIONS = {
{
"authorize",
'a',
0,
OptionArg.STRING,
ref auth,
"Authorizes an application (format: username:action_id:arguments)",
null
},
{
"dialog",
'd',
0,
OptionArg.STRING,
ref app_name,
"Show an unavailable dialog for the specified application name",
null
},
{ null }
};
public static int main (string[] args) {
GLib.Intl.setlocale (LocaleCategory.ALL, "");
GLib.Intl.bindtextdomain (Constants.GETTEXT_PACKAGE, Constants.LOCALEDIR);
GLib.Intl.bind_textdomain_codeset (Constants.GETTEXT_PACKAGE, "UTF-8");
GLib.Intl.textdomain (Constants.GETTEXT_PACKAGE);
try {
var opt_context = new OptionContext (null);
opt_context.set_help_enabled (true);
opt_context.add_main_entries (OPTIONS, null);
opt_context.parse (ref args);
} catch (OptionError e) {
print ("error: %s\n", e.message);
print ("Run '%s --help' to see a full list of available command line options.\n", args[0]);
return 0;
}
var client = new Client ();
return client.run (args);
}
public override void activate () {
var api = Utils.get_api ();
if (app_name != null) {
var app_lock_dialog = new AppUnavailableDialog ();
app_lock_dialog.present ();
}
if (auth != null) {
string[] tokens = auth.split (":", 3);
if (tokens.length == 3) {
try {
string[] argv;
Shell.parse_argv (tokens[2], out argv);
on_authorize (tokens[0], tokens[1], argv);
} catch (ShellError e) {
print ("Failed to parse command line arguments: %s\n", tokens[2]);
}
} else {
print ("Usage: --authorize username:action_id:arguments");
return;
}
}
api.launch.connect (on_launch);
api.show_timeout.connect (on_show_timeout);
}
private void on_authorize (string username, string action_id, string[] _args) {
string[] argv = {};
foreach (unowned string arg in _args) {
argv += arg;
}
if (permission != null && permission.get_can_release ()) {
try {
permission.release ();
} catch (Error e) {
critical (e.message);
}
}
try {
var user = new Polkit.UnixUser.for_name (username);
var unixprocess = new Polkit.UnixProcess.for_owner (Posix.getpid (), 0, user.get_uid ());
permission = new Polkit.Permission.sync (action_id, unixprocess);
} catch (Error e) {
critical (e.message);
}
ulong signal_id = 0;
signal_id = permission.notify["allowed"].connect (() => {
permission.disconnect (signal_id);
unowned IParentalControls api = Utils.get_api ();
api.finish_app_authorization.begin (username, argv, (obj, res) => {
try {
api.finish_app_authorization.end (res);
} catch (Error e) {
warning (e.message);
}
});
});
permission.acquire_async.begin ();
}
private void on_launch (string[] args, bool incoming) {
if (!incoming) {
return;
}
try {
GLib.Process.spawn_async ("/",
args,
Environ.get (),
SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
null,
null);
} catch (Error e) {
warning ("%s\n", e.message);
}
Utils.get_api ().launch (args, false);
}
private void on_show_timeout (int hours, int minutes) {
string body = _("This computer will lock after %s.");
if (hours > 0) {
body = body.printf (ngettext ("%lu hour", "%lu hours", (ulong)hours).printf ((ulong)hours));
} else if (minutes > 0) {
body = body.printf (ngettext ("%lu minute", "%lu minutes", (ulong)minutes).printf ((ulong)minutes));
if (minutes < 10) {
body += "\n" + _("Make sure to close all applications before your computer will be locked.");
}
} else {
return;
}
var notification = new Notification (_("Time left"));
var icon = new ThemedIcon ("dialog-warning");
notification.set_icon (icon);
notification.set_body (body);
send_notification ("time-reminder", notification);
}
}
}