Debian Codegraph @

src/application.vala

raw
1 /* application.vala
2 *
3 * Copyright (C) Red Hat, Inc
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Felipe Borges <[email protected]>
19 *
20 */
21
22 using Config;
23
24 namespace Connections {
25 private class Application : Gtk.Application {
26 public static Application application;
27
28 public ListStore model;
29
30 private List<Connections.Window> windows;
31 public unowned Window main_window {
32 get { return (windows.length () > 0) ? windows.data : null; }
33 }
34
35 public GLib.Settings settings;
36 private bool first_run {
37 get { return settings.get_boolean ("first-run"); }
38 set { settings.set_boolean ("first-run", value); }
39 }
40
41 construct {
42 application_id = Config.APPLICATION_ID;
43 windows = new List<Connections.Window> ();
44 model = new GLib.ListStore (typeof (Connections.Connection));
45 }
46
47 public Application () {
48 application = this;
49
50 flags |= ApplicationFlags.HANDLES_COMMAND_LINE | ApplicationFlags.HANDLES_OPEN;
51
52 settings = new GLib.Settings ("org.gnome.Connections");
53
54 var action = new GLib.SimpleAction ("help", null);
55 action.activate.connect (show_help);
56 add_action (action);
57
58 action = new GLib.SimpleAction ("about", null);
59 action.activate.connect (show_about_dialog);
60 add_action (action);
61
62 action = new GLib.SimpleAction ("quit", null);
63 action.activate.connect (quit_app);
64 add_action (action);
65 }
66
67 private void show_help () {
68 try {
69 Gtk.show_uri_on_window (main_window,
70 "help:gnome-connections",
71 Gtk.get_current_event_time ());
72 } catch (GLib.Error error) {
73 warning ("Failed to display help: %s", error.message);
74 }
75 }
76
77 private void show_about_dialog () {
78 string[] authors = {
79 "Felipe Borges <[email protected]>"
80 };
81 string[] artists = {
82 "Jakub Steiner <[email protected]>"
83 };
84
85 Gtk.show_about_dialog (main_window,
86 "artists", artists,
87 "authors", authors,
88 "translator-credits", _("translator-credits"),
89 "comments", _("A remote desktop client for the GNOME desktop environment"),
90 "copyright", "\xc2\xa9 2020 Red Hat, Inc.",
91 "license-type", Gtk.License.GPL_3_0,
92 "program-name", _("Connections"),
93 "wrap-license", true,
94 "logo-icon-name", application_id,
95 "version", Config.VERSION);
96 }
97
98 public override void startup () {
99 base.startup ();
100
101 Environment.set_prgname (application_id);
102 Gtk.Window.set_default_icon_name (application_id);
103
104 Hdy.init ();
105 Hdy.StyleManager.get_default ().color_scheme = PREFER_DARK;
106 }
107
108 public override void activate () {
109 base.activate ();
110
111 if (main_window != null) {
112 main_window.present ();
113 return;
114 }
115
116 add_new_window ();
117
118 if (first_run) {
119 (new OnboardingDialog (main_window)).present ();
120
121 first_run = !first_run;
122 }
123
124 load_connections ();
125 }
126
127 public Window add_new_window () {
128 var window = new Connections.Window (this);
129
130 windows.append (window);
131 window.present ();
132
133 return window;
134 }
135
136 public void add_connection (string uri) {
137 var existing = Database.get_default ().find_connection_by_uri (uri);
138 if (existing != null) {
139 open_connection (existing);
140 return;
141 }
142
143 var connection = Database.get_default ().add_connection (uri);
144 if (connection == null)
145 return;
146
147 model.insert (0, connection);
148
149 open_connection (connection);
150 }
151
152 public void add_connection_from_file (string file_path) {
153 Connection? connection = null;
154 var mime_type = GLib.ContentType.guess(file_path, null, null);
155
156 switch (mime_type) {
157 case "application/x-vnc":
158 connection = new VncConnection.from_vnc_file (file_path);
159 break;
160 default:
161 warning ( _ ("Couldn’t open file of unknown mime type %s".printf (mime_type)));
162 break;
163 }
164
165 if (connection == null)
166 return;
167
168 model.insert (0, connection);
169 }
170
171 public void remove_connection (Connection connection) {
172 Notification.OKFunc undo = () => {
173 debug ("Connection deletion cancelled by user. Re-adding to view");
174 model.insert (0, connection);
175 };
176
177 Notification.DismissFunc really_remove = () => {
178 debug ("User did not cancel deletion. Deleting now...");
179 connection.disconnect_it ();
180 connection.delete_auth_credentials.begin ();
181 Database.get_default ().delete_connection (connection);
182 };
183
184 for (int i = 0; i < model.get_n_items (); i++) {
185 if ((model.get_item (i) as Connection) == connection) {
186 model.remove (i);
187
188 break;
189 }
190 }
191
192 var message = _("Connection to “%s” has been deleted").printf (connection.display_name != "" && connection.display_name != null ?
193 connection.display_name : connection.uri);
194 main_window.notifications_bar.display_for_action (message,
195 _("Undo"),
196 (owned) undo,
197 (owned) really_remove);
198 }
199
200 public void load_connections () {
201 var db = new Database ();
202 foreach (var connection in db.get_connections ()) {
203 model.append (connection);
204 }
205 }
206
207 public void open_connection (Connection connection) {
208 main_window.open_connection (connection);
209 }
210
211 public override void open(File[] files, string hint)
212 {
213 activate();
214
215 foreach (var file in files) {
216 add_connection_from_file(file.get_path());
217 }
218 }
219
220 public override void shutdown () {
221 base.shutdown ();
222
223 for (int idx = 0; idx < model.get_n_items (); idx++) {
224 var connection = (Connection)model.get_item (idx);
225
226 if (connection.connected)
227 connection.disconnect_it ();
228 }
229 }
230
231 public void quit_app () {
232 foreach (var window in windows)
233 window.hide ();
234
235 quit ();
236 }
237
238 static bool opt_fullscreen;
239 static string[] opt_uris;
240 static string opt_file_import_uri;
241 const OptionEntry[] options = {
242 { "", 0, 0, GLib.OptionArg.STRING_ARRAY, ref opt_uris, N_ ("URL to connect"), null },
243 { "file", 'F', 0, GLib.OptionArg.FILENAME, ref opt_file_import_uri, N_ ("Open .vnc or .rdp file at the given PATH"), N_ ("PATH") },
244 { "fullscreen", 'f', 0, GLib.OptionArg.NONE, ref opt_fullscreen, N_("Open in full screen"), null },
245 { null }
246 };
247 public override int command_line (GLib.ApplicationCommandLine cmdline) {
248 opt_uris = null;
249 opt_file_import_uri = null;
250 opt_fullscreen = false;
251
252 var parameter_string = _("A remote desktop client for the GNOME desktop environment");
253 var opt_context = new OptionContext ("— " + parameter_string);
254 opt_context.add_main_entries (options, null);
255
256 try {
257 string[] args1 = cmdline.get_arguments ();
258 unowned string[] args2 = args1;
259 opt_context.parse (ref args2);
260 } catch (GLib.OptionError error) {
261 cmdline.printerr ("%s\n", error.message);
262
263 return 1;
264 }
265
266 if (opt_uris != null && opt_uris.length > 1) {
267 cmdline.printerr (_("Too many command-line arguments specified.\n"));
268
269 return 1;
270 }
271 activate ();
272
273 if (opt_uris != null) {
274 var uri = opt_uris[0];
275
276 // TODO: throw a notification if this fails.
277 add_connection (uri);
278 }
279
280 if (opt_file_import_uri != null) {
281 add_connection_from_file (opt_file_import_uri);
282 }
283
284 main_window.fullscreened = opt_fullscreen;
285
286 return 0;
287 }
288 }
289 }