| 1 |
// Fragments - connection.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 gio::prelude::*; |
| 20 |
use glib::Properties; |
| 21 |
use gtk::subclass::prelude::*; |
| 22 |
use gtk::{gio, glib}; |
| 23 |
use uuid::Uuid; |
| 24 |
|
| 25 |
use crate::i18n::i18n; |
| 26 |
|
| 27 |
static FRAGMENTS_UUID: &str = "00000000-0000-0000-0000-000000000000"; |
| 28 |
|
| 29 |
mod imp { |
| 30 |
use super::*; |
| 31 |
|
| 32 |
#[derive(Debug, Default, Properties)] |
| 33 |
#[properties(wrapper_type = super::FrgConnection)] |
| 34 |
pub struct FrgConnection { |
| 35 |
#[property(get, set, construct_only)] |
| 36 |
title: OnceCell<String>, |
| 37 |
#[property(get, set, construct_only)] |
| 38 |
address: OnceCell<String>, |
| 39 |
#[property(get, set, construct_only)] |
| 40 |
uuid: OnceCell<String>, |
| 41 |
#[property(get = Self::is_fragments, set)] |
| 42 |
is_fragments: Cell<bool>, |
| 43 |
} |
| 44 |
|
| 45 |
impl FrgConnection { |
| 46 |
pub fn is_fragments(&self) -> bool { |
| 47 |
self.obj().uuid() == FRAGMENTS_UUID |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[glib::object_subclass] |
| 52 |
impl ObjectSubclass for FrgConnection { |
| 53 |
const NAME: &'static str = "FrgConnection"; |
| 54 |
type Type = super::FrgConnection; |
| 55 |
} |
| 56 |
|
| 57 |
#[glib::derived_properties] |
| 58 |
impl ObjectImpl for FrgConnection { |
| 59 |
fn constructed(&self) { |
| 60 |
self.parent_constructed(); |
| 61 |
|
| 62 |
let obj = self.obj(); |
| 63 |
let is_fragments = obj.uuid() == FRAGMENTS_UUID; |
| 64 |
self.is_fragments.set(is_fragments); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
glib::wrapper! { |
| 70 |
pub struct FrgConnection(ObjectSubclass<imp::FrgConnection>); |
| 71 |
} |
| 72 |
|
| 73 |
impl FrgConnection { |
| 74 |
pub fn new(title: &str, address: &str) -> Self { |
| 75 |
let uuid = Uuid::new_v4().to_string(); |
| 76 |
glib::Object::builder() |
| 77 |
.property("title", title) |
| 78 |
.property("address", address) |
| 79 |
.property("uuid", uuid) |
| 80 |
.build() |
| 81 |
} |
| 82 |
|
| 83 |
pub fn new_with_uuid(title: &str, address: &str, uuid: &str) -> Self { |
| 84 |
glib::Object::builder() |
| 85 |
.property("title", title) |
| 86 |
.property("address", address) |
| 87 |
.property("uuid", uuid) |
| 88 |
.build() |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
impl Default for FrgConnection { |
| 93 |
fn default() -> Self { |
| 94 |
Self::new_with_uuid( |
| 95 |
&i18n("Local Fragments session"), |
| 96 |
"http://127.0.0.1:9091/transmission/rpc", |
| 97 |
FRAGMENTS_UUID, |
| 98 |
) |
| 99 |
} |
| 100 |
} |