| 1 |
// Fragments - connection_row.rs |
| •2 |
// Copyright (C) 2022-2025 Felix Häcker <[email protected]> |
| 3 |
// |
| 4 |
// This program is free software: you can redistribute it and/or modify |
| 5 |
// it under the terms of the GNU General Public License as published by |
| 6 |
// the Free Software Foundation, either version 3 of the License, or |
| 7 |
// (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
use std::cell::{Cell, OnceCell}; |
| 18 |
|
| 19 |
use glib::{subclass, Properties}; |
| 20 |
use gtk::prelude::*; |
| 21 |
use gtk::subclass::prelude::*; |
| 22 |
use gtk::{glib, CompositeTemplate}; |
| 23 |
|
| 24 |
use crate::app::FrgApplication; |
| 25 |
use crate::backend::{secret_store, FrgConnection, FrgConnectionManager}; |
| 26 |
use crate::config; |
| 27 |
use crate::i18n::i18n; |
| 28 |
|
| 29 |
mod imp { |
| 30 |
use super::*; |
| 31 |
|
| 32 |
#[derive(Default, Debug, CompositeTemplate, Properties)] |
| 33 |
#[template(resource = "/de/haeckerfelix/Fragments/gtk/connection_row.ui")] |
| 34 |
#[properties(wrapper_type = super::FrgConnectionRow)] |
| 35 |
pub struct FrgConnectionRow { |
| 36 |
#[template_child] |
| 37 |
pub icon: TemplateChild<gtk::Image>, |
| 38 |
#[template_child] |
| 39 |
pub title: TemplateChild<gtk::Label>, |
| 40 |
#[template_child] |
| 41 |
pub subtitle: TemplateChild<gtk::Label>, |
| 42 |
#[template_child] |
| 43 |
pub checkmark: TemplateChild<gtk::Image>, |
| 44 |
#[template_child] |
| 45 |
pub remove_button: TemplateChild<gtk::Button>, |
| 46 |
|
| 47 |
#[property(get, set, construct_only)] |
| 48 |
pub connection: OnceCell<FrgConnection>, |
| 49 |
#[property(get, set)] |
| 50 |
pub selected: Cell<bool>, |
| 51 |
} |
| 52 |
|
| 53 |
#[glib::object_subclass] |
| 54 |
impl ObjectSubclass for FrgConnectionRow { |
| 55 |
const NAME: &'static str = "FrgConnectionRow"; |
| 56 |
type ParentType = gtk::ListBoxRow; |
| 57 |
type Type = super::FrgConnectionRow; |
| 58 |
|
| 59 |
fn class_init(klass: &mut Self::Class) { |
| 60 |
klass.bind_template(); |
| 61 |
klass.bind_template_callbacks(); |
| 62 |
} |
| 63 |
|
| 64 |
fn instance_init(obj: &subclass::InitializingObject<Self>) { |
| 65 |
obj.init_template(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
#[glib::derived_properties] |
| 70 |
impl ObjectImpl for FrgConnectionRow { |
| 71 |
fn constructed(&self) { |
| 72 |
self.parent_constructed(); |
| 73 |
let con = self.obj().connection(); |
| 74 |
|
| 75 |
self.title.set_label(&con.title()); |
| 76 |
self.subtitle.set_label(&con.address()); |
| 77 |
|
| 78 |
if con.is_fragments() { |
| 79 |
let icon_name = if config::PROFILE == "development" { |
| 80 |
"de.haeckerfelix.Fragments.Devel-symbolic" |
| 81 |
} else { |
| 82 |
"de.haeckerfelix.Fragments-symbolic" |
| 83 |
}; |
| 84 |
self.icon.set_icon_name(Some(icon_name)); |
| 85 |
self.subtitle.set_label(&i18n("This computer")); |
| 86 |
self.remove_button.set_visible(false); |
| 87 |
} else { |
| 88 |
self.icon.set_icon_name(Some("network-server-symbolic")); |
| 89 |
} |
| 90 |
|
| 91 |
self.obj() |
| 92 |
.bind_property("selected", &self.checkmark.get(), "visible") |
| 93 |
.build(); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
impl WidgetImpl for FrgConnectionRow {} |
| 98 |
|
| 99 |
impl ListBoxRowImpl for FrgConnectionRow {} |
| 100 |
|
| 101 |
#[gtk::template_callbacks] |
| 102 |
impl FrgConnectionRow { |
| 103 |
#[template_callback] |
| 104 |
async fn remove_button_clicked(&self) { |
| 105 |
// Remove stored secrets |
| 106 |
if let Err(e) = secret_store::remove(&self.obj().connection()).await { |
| 107 |
error!("Unable to remove secret: {}", e); |
| 108 |
} |
| 109 |
|
| 110 |
// Connect with default connection |
| 111 |
let app = FrgApplication::default(); |
| 112 |
app.activate_action( |
| 113 |
"set-connection", |
| 114 |
Some(&FrgConnection::default().uuid().to_variant()), |
| 115 |
); |
| 116 |
|
| 117 |
let connections = FrgConnectionManager::default().connections(); |
| 118 |
let pos = connections |
| 119 |
.find(&self.obj().connection()) |
| 120 |
.expect("Unable to find connection to remove"); |
| 121 |
connections.remove(pos); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
glib::wrapper! { |
| 127 |
pub struct FrgConnectionRow( |
| 128 |
ObjectSubclass<imp::FrgConnectionRow>) |
| •129 |
@extends gtk::Widget, gtk::ListBoxRow, |
| 130 |
@implements gtk::Actionable, gtk::Accessible, gtk::Buildable, gtk::ConstraintTarget; |
| 131 |
} |
| 132 |
|
| 133 |
impl FrgConnectionRow { |
| 134 |
pub fn new(connection: &FrgConnection) -> Self { |
| 135 |
glib::Object::builder() |
| 136 |
.property("connection", connection) |
| 137 |
.build() |
| 138 |
} |
| 139 |
} |