2025-01-29 13:24:36 +01:00
// Copyright (c) 2024-2025 Sophie Herold
2024-05-09 23:27:45 +02:00
//
// 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 <http://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
2024-05-09 01:30:01 +02:00
use adw ::prelude ::* ;
use adw ::subclass ::prelude ::* ;
use gtk ::CompositeTemplate ;
use crate ::deps ::* ;
2024-12-18 01:29:14 +01:00
use crate ::util ::gettext ::* ;
2024-12-18 17:12:17 +01:00
use crate ::util ::ErrorType ;
2024-05-09 01:30:01 +02:00
mod imp {
use super ::* ;
#[ derive(Debug, Default, CompositeTemplate) ]
#[ template(file = " error_details.ui " ) ]
pub struct LpErrorDetails {
#[ template_child ]
pub ( super ) message : TemplateChild < gtk ::TextView > ,
#[ template_child ]
pub ( super ) copy : TemplateChild < gtk ::Button > ,
#[ template_child ]
pub ( super ) report : TemplateChild < gtk ::Button > ,
2024-12-18 01:29:14 +01:00
#[ template_child ]
pub ( super ) preference_group : TemplateChild < adw ::PreferencesGroup > ,
2024-05-09 01:30:01 +02:00
}
#[ glib::object_subclass ]
impl ObjectSubclass for LpErrorDetails {
const NAME : & 'static str = " LpErrorDetails " ;
type Type = super ::LpErrorDetails ;
type ParentType = adw ::Dialog ;
fn class_init ( klass : & mut Self ::Class ) {
Self ::bind_template ( klass ) ;
}
fn instance_init ( obj : & glib ::subclass ::InitializingObject < Self > ) {
obj . init_template ( ) ;
}
}
impl ObjectImpl for LpErrorDetails {
fn constructed ( & self ) {
self . message . buffer ( ) . set_text ( " Some test text " ) ;
}
}
impl WidgetImpl for LpErrorDetails { }
impl AdwDialogImpl for LpErrorDetails { }
}
glib ::wrapper! {
pub struct LpErrorDetails ( ObjectSubclass < imp ::LpErrorDetails > )
2025-08-22 15:03:55 +02:00
@ extends adw ::Dialog , gtk ::Widget ,
@ implements gtk ::Buildable , gtk ::Accessible , gtk ::ConstraintTarget ;
2024-05-09 01:30:01 +02:00
}
impl LpErrorDetails {
2024-12-18 01:29:14 +01:00
pub fn new ( root : & impl IsA < gtk ::Widget > , text : & str , type_ : ErrorType ) -> Self {
2024-05-09 01:30:01 +02:00
let obj = glib ::Object ::new ::< Self > ( ) ;
let imp = obj . imp ( ) ;
imp . message . buffer ( ) . set_text ( text ) ;
2024-12-18 01:29:14 +01:00
if matches! ( type_ , ErrorType ::General ) {
2025-02-13 22:08:13 +01:00
imp . preference_group . set_description (
Some ( & gettext ( " The following error occurred. If you think this is an issue with the program, please include this information when you report the error. " ) ) ,
2024-12-18 01:29:14 +01:00
) ;
}
2024-06-18 00:37:38 +02:00
imp . copy . connect_clicked ( glib ::clone! (
#[ weak ]
obj ,
move | _ | {
2024-05-09 01:30:01 +02:00
let buffer = obj . imp ( ) . message . buffer ( ) ;
let ( start , end ) = buffer . bounds ( ) ;
obj . display ( )
. clipboard ( )
. set_text ( buffer . text ( & start , & end , true ) . as_str ( ) )
2024-06-18 00:37:38 +02:00
}
) ) ;
2024-05-09 01:30:01 +02:00
2024-12-18 01:29:14 +01:00
let issue_uri = match type_ {
ErrorType ::Loader = > " https://gitlab.gnome.org/GNOME/glycin/-/issues " ,
ErrorType ::General = > " https://gitlab.gnome.org/GNOME/loupe/-/issues " ,
} ;
2024-06-18 00:37:38 +02:00
imp . report . connect_clicked ( glib ::clone! (
#[ weak ]
obj ,
move | _ | {
2024-12-18 01:29:14 +01:00
gtk ::UriLauncher ::new ( issue_uri ) . launch (
2024-05-09 01:30:01 +02:00
obj . root ( ) . and_downcast_ref ::< gtk ::Window > ( ) ,
gio ::Cancellable ::NONE ,
| _ | { } ,
) ;
2024-06-18 00:37:38 +02:00
}
) ) ;
2024-05-09 01:30:01 +02:00
2024-12-18 17:12:17 +01:00
log ::debug! ( " Showing error details " ) ;
2024-06-14 17:46:33 +02:00
obj . present ( Some ( root ) ) ;
2024-05-09 01:30:01 +02:00
obj
}
}