/* window.vala * * Copyright (C) Red Hat, Inc * * 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 * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Author: Felipe Borges * */ namespace Connections { [GtkTemplate (ui = "/org/gnome/Connections/ui/window.ui")] private class Window : Gtk.ApplicationWindow { [GtkChild] private unowned Topbar topbar; [GtkChild] private unowned Gtk.Stack stack; [GtkChild] private unowned Hdy.StatusPage empty_view; [GtkChild] public unowned CollectionView collection_view; [GtkChild] private unowned DisplayView display_view; [GtkChild] public unowned NotificationsBar notifications_bar; public DialogsWindow dialogs_window; public bool fullscreened { get { return Gdk.WindowState.FULLSCREEN in get_window ().get_state (); } set { if (value) fullscreen (); else unfullscreen (); } } private uint configure_id; private const uint configure_id_timeout = 100; // 100ms public Window (Gtk.Application app) { Object (application: app); empty_view.icon_name = "%s-symbolic".printf (app.application_id); stack.set_visible_child (empty_view); bind_model (Application.application.model); Application.application.model.items_changed.connect (items_changed); dialogs_window = new DialogsWindow (); dialogs_window.set_modal (true); dialogs_window.set_transient_for (this); try { var style_provider = new Gtk.CssProvider (); var file = File.new_for_uri ("resource:///org/gnome/Connections/ui/style.css"); style_provider.load_from_file (file); Gtk.StyleContext.add_provider_for_screen (Gdk.Screen.get_default (), style_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION); } catch (GLib.Error error) { warning ("Failed to load CSS: %s", error.message); } topbar.search_button.bind_property ("active", collection_view.search_bar, "search_mode_enabled", BindingFlags.BIDIRECTIONAL); if (app.application_id == "org.gnome.Connections.Devel") { get_style_context ().add_class ("devel"); } /* Restore window size */ var size = Application.application.settings.get_value ("window-size"); if (size.n_children () == 2) { set_default_size ((int) size.get_child_value (0), (int) size.get_child_value (1)); } } public void bind_model (ListModel model) { collection_view.bind_model (model); model.items_changed.connect (() => { topbar.search_button.visible = model.get_n_items () > 0; }); } public void items_changed () { if (Application.application.model.get_n_items () > 0) stack.set_visible_child (collection_view); else stack.set_visible_child (empty_view); } public void open_connection (Connection connection) { stack.set_visible_child (display_view); topbar.show_display_view (connection); if (!connection.connected) display_view.connect_to (connection); else display_view.switch_to (connection); } public void show_collection_view () { stack.set_visible_child (collection_view); topbar.show_collection_view (); topbar.set_title (_("Connections")); } public void show_preferences_window (Connection connection) { if (connection is VncConnection) (new VncPreferencesWindow (connection)).present (); else if (connection is RdpConnection) (new RdpPreferencesWindow (connection)).present (); #if HAS_SPICE else if (connection is SpiceConnection) (new SpicePreferencesWindow (connection)).present (); #endif else debug ("Failed to launch preferences window for %s", connection.uri); } [GtkCallback] private bool on_key_pressed (Gtk.Widget widget, Gdk.EventKey event) { var default_modifiers = Gtk.accelerator_get_default_mod_mask (); if (event.keyval == Gdk.Key.f && (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) { collection_view.search_bar.search_mode_enabled = !collection_view.search_bar.search_mode_enabled; return true; } else if (event.keyval == Gdk.Key.F11) { fullscreened = !fullscreened; return true; } else if (event.keyval == Gdk.Key.q && (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) { Application.application.quit_app (); return true; } else if (event.keyval == Gdk.Key.F1) { Application.application.activate_action ("help", null); return true; } else if (event.keyval == Gdk.Key.n && (event.state & default_modifiers) == Gdk.ModifierType.CONTROL_MASK) { topbar.assistant.popdown (); return true; } if (stack.visible_child == collection_view) return collection_view.search_bar.handle_event ((Gdk.Event) event); return false; } [GtkCallback] private bool on_delete_event () { notifications_bar.dismiss (); return false; } private void save_window_geometry () { int width, height; if (is_maximized) return; get_size (out width, out height); Application.application.settings.set ("window-size", "(ii)", width, height); } [GtkCallback] private bool on_configure_event () { if (fullscreened) { return false; } if (configure_id != 0) { GLib.Source.remove (configure_id); } configure_id = Timeout.add (configure_id_timeout, () => { configure_id = 0; save_window_geometry (); return false; }); return false; } } }