1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// MyCitadel desktop wallet: bitcoin & RGB wallet based on GTK framework.
//
// Written in 2022 by
//     Dr. Maxim Orlovsky <orlovsky@pandoraprime.ch>
//
// Copyright (C) 2022 by Pandora Prime SA, Switzerland.
//
// This software is distributed without any warranty. You should have received
// a copy of the AGPL-3.0 License along with this software. If not, see
// <https://www.gnu.org/licenses/agpl-3.0-standalone.html>.

pub mod about;
pub mod devices;
pub mod launch;
pub mod psbt;
pub mod settings;
pub mod wallet;

pub const APP_ICON: &[u8] = include_bytes!("../../res/applogo-big.png");
pub const APP_ICON_TOOL: &[u8] = include_bytes!("../../res/applogo.png");

use std::path::PathBuf;

use gtk::prelude::*;
use gtk::{
    ButtonsType, DialogFlags, FileChooserAction, FileChooserDialog, FileFilter, MessageDialog,
    MessageType, ResponseType,
};

pub trait NotificationBoxExt {
    fn notification_box(&self) -> &gtk::Box;
    fn main_dialog(&self) -> &gtk::Dialog;
    fn main_action_button(&self) -> &gtk::Button;
    fn notification_image(&self) -> &gtk::Image;
    fn notification_label(&self) -> &gtk::Label;

    fn show_notification(&self) { self.notification_box().show_all(); }
    fn show_error(&self, msg: &str) {
        self.main_dialog()
            .set_response_sensitive(ResponseType::Ok, false);
        self.main_action_button().set_sensitive(false);
        self.notification_image()
            .set_icon_name(Some("dialog-error-symbolic"));
        self.notification_label().set_label(msg);
        self.notification_box().show_all();
    }
    fn show_info(&self, msg: &str) {
        self.main_dialog()
            .set_response_sensitive(ResponseType::Ok, true);
        self.main_action_button().set_sensitive(true);
        self.notification_image()
            .set_icon_name(Some("dialog-information-symbolic"));
        self.notification_label().set_label(msg);
        self.notification_box().show_all();
    }
    fn show_warning(&self, msg: &str) {
        self.main_dialog()
            .set_response_sensitive(ResponseType::Ok, true);
        self.main_action_button().set_sensitive(true);
        self.notification_image()
            .set_icon_name(Some("dialog-warning-symbolic"));
        self.notification_label().set_label(msg);
        self.notification_box().show_all();
    }
    fn hide_message(&self) {
        self.main_dialog()
            .set_response_sensitive(ResponseType::Ok, true);
        self.main_action_button().set_sensitive(true);
        self.notification_box().hide()
    }
}

pub fn msg_dlg(
    parent: &impl IsA<gtk::Window>,
    ty: MessageType,
    title: &str,
    message: &str,
    details: Option<&str>,
) {
    let err_dlg = MessageDialog::new(
        Some(parent),
        DialogFlags::all(),
        MessageType::Error,
        ButtonsType::Close,
        message,
    );
    err_dlg.set_title(title);
    err_dlg.set_message_type(ty);
    err_dlg.set_secondary_text(details);
    err_dlg.run();
    err_dlg.close();
}

pub fn error_dlg(
    parent: &impl IsA<gtk::Window>,
    title: &str,
    message: &str,
    details: Option<&str>,
) {
    msg_dlg(parent, MessageType::Error, title, message, details);
}

pub fn file_dlg(
    parent: Option<&impl IsA<gtk::Window>>,
    title: &str,
    action: FileChooserAction,
    type_name: &str,
    mask: &str,
    default_name: Option<&str>,
) -> Option<PathBuf> {
    let button = match action {
        FileChooserAction::Open => "Open",
        FileChooserAction::Save => "Save",
        FileChooserAction::SelectFolder => "Select",
        FileChooserAction::CreateFolder => "Create",
        _ => unimplemented!(),
    };

    let file_dlg =
        FileChooserDialog::with_buttons(Some(title), parent, action, &[(button, ResponseType::Ok)]);
    file_dlg.set_default_response(ResponseType::Ok);
    file_dlg.set_do_overwrite_confirmation(action == FileChooserAction::Save);
    if let Some(name) = default_name {
        file_dlg.set_current_name(name);
    }

    let filter = FileFilter::new();
    filter.add_pattern(mask);
    filter.set_name(Some(type_name));
    file_dlg.add_filter(&filter);
    file_dlg.set_filter(&filter);

    let resp = file_dlg.run();
    let path = file_dlg.filename();
    file_dlg.hide();
    file_dlg.close();
    if resp != ResponseType::Ok {
        return None;
    }
    path
}

pub fn file_open_dlg(
    parent: Option<&gtk::ApplicationWindow>,
    title: &str,
    type_name: &str,
    mask: &str,
) -> Option<PathBuf> {
    file_dlg(
        parent,
        title,
        FileChooserAction::Open,
        type_name,
        mask,
        None,
    )
}

pub fn file_save_dlg(
    parent: Option<&gtk::ApplicationWindow>,
    title: &str,
    type_name: &str,
    mask: &str,
) -> Option<PathBuf> {
    file_dlg(
        parent,
        title,
        FileChooserAction::Save,
        type_name,
        mask,
        None,
    )
}

pub fn file_create_dlg(
    parent: Option<&gtk::ApplicationWindow>,
    title: &str,
    type_name: &str,
    mask: &str,
    default_name: &str,
) -> Option<PathBuf> {
    file_dlg(
        parent,
        title,
        FileChooserAction::Save,
        type_name,
        mask,
        Some(default_name),
    )
}