Debian Codegraph @

src/window.vala

raw
1 /* window.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 namespace Connections {
23 [GtkTemplate (ui = "/org/gnome/Connections/ui/window.ui")]
24 private class Window : Gtk.ApplicationWindow {
25 [GtkChild]
26 private unowned Topbar topbar;
27
28 [GtkChild]
29 private unowned Gtk.Stack stack;
30
31 [GtkChild]
32 private unowned Hdy.StatusPage empty_view;
33
34 [GtkChild]
35 public unowned CollectionView collection_view;
36
37 [GtkChild]
38 private unowned DisplayView display_view;
39
40 [GtkChild]
41 public unowned NotificationsBar notifications_bar;
42 public DialogsWindow dialogs_window;
43
44 public bool fullscreened {
45 get { return Gdk.WindowState.FULLSCREEN in get_window ().get_state (); }
46 set {
47 if (value)
48 fullscreen ();
49 else
50 unfullscreen ();
51 }
52 }
53 private uint configure_id;
54 private const uint configure_id_timeout = 100; // 100ms
55
56 public Window (Gtk.Application app) {
57 Object (application: app);
58
59 empty_view.icon_name = "%s-symbolic".printf (app.application_id);
60 stack.set_visible_child (empty_view);
61
62 bind_model (Application.application.model);
63 Application.application.model.items_changed.connect (items_changed);
64
65 dialogs_window = new DialogsWindow ();
66 dialogs_window.set_modal (true);
67 dialogs_window.set_transient_for (this);
68
69 try {
70 var style_provider = new Gtk.CssProvider ();
71 var file = File.new_for_uri ("resource:///org/gnome/Connections/ui/style.css");
72 style_provider.load_from_file (file);
73
74 Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (),
75 style_provider,
76 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
77 } catch (GLib.Error error) {
78 warning ("Failed to load CSS: %s", error.message);
79 }
80
81 topbar.search_button.bind_property ("active", collection_view.search_bar, "search_mode_enabled", BindingFlags.BIDIRECTIONAL);
82
83 if (app.application_id == "org.gnome.Connections.Devel") {
84 get_style_context ().add_class ("devel");
85 }
86
87 /* Restore window size */
88 var size = Application.application.settings.get_value ("window-size");
89 if (size.n_children () == 2) {
90 set_default_size ((int) size.get_child_value (0), (int) size.get_child_value (1));
91 }
92 }
93
94 public void bind_model (ListModel model) {
95 collection_view.bind_model (model);
96
97 model.items_changed.connect (() => {
98 topbar.search_button.visible = model.get_n_items () > 0;
99 });
100 }
101
102 public void items_changed () {
103 if (Application.application.model.get_n_items () > 0)
104 stack.set_visible_child (collection_view);
105 else
106 stack.set_visible_child (empty_view);
107 }
108
109 public void open_connection (Connection connection) {
110 stack.set_visible_child (display_view);
111 topbar.show_display_view (connection);
112
113 if (!connection.connected)
114 display_view.connect_to (connection);
115 else
116 display_view.switch_to (connection);
117 }
118
119 public void show_collection_view () {
120 stack.set_visible_child (collection_view);
121
122 topbar.show_collection_view ();
123 topbar.set_title (_("Connections"));
124 }
125
126 public void show_preferences_window (Connection connection) {
127 if (connection is VncConnection)
128 (new VncPreferencesWindow (connection)).present ();
129 else if (connection is RdpConnection)
130 (new RdpPreferencesWindow (connection)).present ();
131 #if HAS_SPICE
132 else if (connection is SpiceConnection)
133 (new SpicePreferencesWindow (connection)).present ();
134 #endif
135 else
136 debug ("Failed to launch preferences window for %s", connection.uri);
137 }
138
139 [GtkCallback]
140 private bool on_key_pressed (Gtk.Widget widget, Gdk.EventKey event) {
141 var default_modifiers = Gtk.accelerator_get_default_mod_mask ();
142
143 if (event.keyval == Gdk.Key.f &&
144 (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
145 collection_view.search_bar.search_mode_enabled = !collection_view.search_bar.search_mode_enabled;
146
147 return true;
148 } else if (event.keyval == Gdk.Key.F11) {
149 fullscreened = !fullscreened;
150
151 return true;
152 } else if (event.keyval == Gdk.Key.q &&
153 (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
154 Application.application.quit_app ();
155
156 return true;
157 } else if (event.keyval == Gdk.Key.F1) {
158 Application.application.activate_action ("help", null);
159
160 return true;
161 } else if (event.keyval == Gdk.Key.n &&
162 (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) {
163 topbar.assistant.popdown ();
164
165 return true;
166 }
167
168 if (stack.visible_child == collection_view)
169 return collection_view.search_bar.handle_event ((Gdk.Event) event);
170
171 return false;
172 }
173
174 [GtkCallback]
175 private bool on_delete_event () {
176 notifications_bar.dismiss ();
177
178 return false;
179 }
180
181 private void save_window_geometry () {
182 int width, height;
183
184 if (is_maximized)
185 return;
186
187 get_size (out width, out height);
188 Application.application.settings.set ("window-size", "(ii)", width, height);
189 }
190
191 [GtkCallback]
192 private bool on_configure_event () {
193 if (fullscreened) {
194 return false;
195 }
196
197 if (configure_id != 0) {
198 GLib.Source.remove (configure_id);
199 }
200
201 configure_id = Timeout.add (configure_id_timeout, () => {
202 configure_id = 0;
203 save_window_geometry ();
204
205 return false;
206 });
207
208 return false;
209 }
210 }
211 }