about summary refs log tree commit diff
path: root/src/ui/preferences_window.rs
blob: bd135dea6c048e3ba15cd0a6dc5db6fccab34c86 (plain) (blame)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Furtherance - Track your time without being tracked
// Copyright (C) 2022  Ricky Kresslein <rk@lakoliu.com>
//
// 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 <https://www.gnu.org/licenses/>.

use adw::prelude::*;
use adw::subclass::prelude::*;
use gettextrs::*;
use glib::clone;
use gtk::glib;
use gtk::CompositeTemplate;

use crate::settings_manager;
use crate::ui::FurtheranceWindow;
use crate::FurtheranceApplication;
use crate::database;

mod imp {
    use super::*;
    use glib::subclass;

    #[derive(Default, Debug, CompositeTemplate)]
    #[template(resource = "/com/lakoliu/Furtherance/gtk/preferences_window.ui")]
    pub struct FurPreferencesWindow {
        #[template_child]
        pub appearance_group: TemplateChild<adw::PreferencesGroup>,
        #[template_child]
        pub dark_theme_switch: TemplateChild<gtk::Switch>,

        #[template_child]
        pub idle_group: TemplateChild<adw::PreferencesGroup>,
        #[template_child]
        pub notify_of_idle_expander: TemplateChild<adw::ExpanderRow>,
        #[template_child]
        pub notify_of_idle_spin: TemplateChild<gtk::SpinButton>,

        #[template_child]
        pub task_group: TemplateChild<adw::PreferencesGroup>,
        #[template_child]
        pub limit_tasks_expander: TemplateChild<adw::ExpanderRow>,
        #[template_child]
        pub limit_days_spin: TemplateChild<gtk::SpinButton>,
        #[template_child]
        pub delete_confirmation_switch: TemplateChild<gtk::Switch>,
        #[template_child]
        pub show_seconds_switch: TemplateChild<gtk::Switch>,
        #[template_child]
        pub show_daily_sums_switch: TemplateChild<gtk::Switch>,
        #[template_child]
        pub show_tags_switch: TemplateChild<gtk::Switch>,

        #[template_child]
        pub timer_group: TemplateChild<adw::PreferencesGroup>,
        #[template_child]
        pub pomodoro_expander: TemplateChild<adw::ExpanderRow>,
        #[template_child]
        pub pomodoro_spin: TemplateChild<gtk::SpinButton>,

        #[template_child]
        pub autosave_expander: TemplateChild<adw::ExpanderRow>,
        #[template_child]
        pub autosave_spin: TemplateChild<gtk::SpinButton>,

        #[template_child]
        pub inclusive_total_switch: TemplateChild<gtk::Switch>,

        #[template_child]
        pub database_loc_row: TemplateChild<adw::ActionRow>,
        #[template_child]
        pub database_browse_btn: TemplateChild<gtk::Button>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for FurPreferencesWindow {
        const NAME: &'static str = "FurPreferencesWindow";
        type ParentType = adw::PreferencesWindow;
        type Type = super::FurPreferencesWindow;

        fn class_init(klass: &mut Self::Class) {
            Self::bind_template(klass);
        }

        fn instance_init(obj: &subclass::InitializingObject<Self>) {
            obj.init_template();
        }
    }

    impl ObjectImpl for FurPreferencesWindow {
        fn constructed(&self) {
            let window = FurtheranceWindow::default();
            let obj = self.obj();
            obj.set_transient_for(Some(&window));

            obj.setup_signals();
            obj.setup_widgets();

            self.parent_constructed();
        }
    }

    impl WidgetImpl for FurPreferencesWindow {}

    impl WindowImpl for FurPreferencesWindow {}

    impl AdwWindowImpl for FurPreferencesWindow {}

    impl PreferencesWindowImpl for FurPreferencesWindow {}
}

glib::wrapper! {
    pub struct FurPreferencesWindow(
        ObjectSubclass<imp::FurPreferencesWindow>)
        @extends gtk::Widget, gtk::Window, adw::Window, adw::PreferencesWindow;

}

impl FurPreferencesWindow {
    pub fn new() -> Self {
        glib::Object::new::<FurPreferencesWindow>()
    }

    fn setup_widgets(&self) {
        self.set_search_enabled(false);

        let imp = imp::FurPreferencesWindow::from_obj(self);

        let manager = adw::StyleManager::default();
        let support_darkmode = manager.system_supports_color_schemes();
        imp.appearance_group.set_visible(!support_darkmode);

        let db_dir = database::get_directory().to_string_lossy().to_string();
        imp.database_loc_row.set_subtitle(&db_dir);
    }

    fn setup_signals(&self) {
        let imp = imp::FurPreferencesWindow::from_obj(self);

        settings_manager::bind_property("dark-mode", &*imp.dark_theme_switch, "active");

        settings_manager::bind_property(
            "notify-of-idle",
            &*imp.notify_of_idle_expander,
            "enable-expansion",
        );

        settings_manager::bind_property("idle-time", &*imp.notify_of_idle_spin, "value");

        settings_manager::bind_property(
            "limit-tasks",
            &*imp.limit_tasks_expander,
            "enable-expansion",
        );

        settings_manager::bind_property("limit-days", &*imp.limit_days_spin, "value");

        settings_manager::bind_property(
            "delete-confirmation",
            &*imp.delete_confirmation_switch,
            "active",
        );

        settings_manager::bind_property("show-seconds", &*imp.show_seconds_switch, "active");

        settings_manager::bind_property("show-daily-sums", &*imp.show_daily_sums_switch, "active");

        settings_manager::bind_property("show-tags", &*imp.show_tags_switch, "active");

        settings_manager::bind_property("pomodoro", &*imp.pomodoro_expander, "enable-expansion");

        settings_manager::bind_property("pomodoro-time", &*imp.pomodoro_spin, "value");

        settings_manager::bind_property("autosave", &*imp.autosave_expander, "enable-expansion");

        settings_manager::bind_property("autosave-time", &*imp.autosave_spin, "value");

        settings_manager::bind_property("inclusive-total", &*imp.inclusive_total_switch, "active");

        imp.dark_theme_switch.connect_active_notify(move |_| {
            let app = FurtheranceApplication::default();
            app.update_light_dark();
        });

        imp.limit_tasks_expander
            .connect_enable_expansion_notify(move |_| {
                let window = FurtheranceWindow::default();
                window.reset_history_box();
            });

        imp.limit_days_spin.connect_value_changed(move |_| {
            let window = FurtheranceWindow::default();
            window.reset_history_box();
        });

        imp.show_seconds_switch.connect_active_notify(move |_| {
            let window = FurtheranceWindow::default();
            window.reset_history_box();
        });

        imp.show_daily_sums_switch.connect_active_notify(move |_| {
            let window = FurtheranceWindow::default();
            window.reset_history_box();
        });

        imp.show_tags_switch.connect_active_notify(move |_| {
            let window = FurtheranceWindow::default();
            window.reset_history_box();
        });

        imp.pomodoro_expander
            .connect_enable_expansion_notify(move |_| {
                let window = FurtheranceWindow::default();
                window.refresh_timer();
        });

        imp.pomodoro_spin.connect_value_changed(move |new_val| {
            settings_manager::set_int("pomodoro-time", new_val.value() as i32);
            let window = FurtheranceWindow::default();
            window.refresh_timer();
        });

        imp.inclusive_total_switch.connect_active_notify(move |_| {
            let window = FurtheranceWindow::default();
            window.reset_history_box();
        });

        imp.database_browse_btn.connect_clicked(clone!(@weak self as this => move |_| {
            let window = FurtheranceApplication::default().active_window().unwrap();
            let dialog = gtk::FileChooserDialog::new(
                Some(&gettext("Backup Database")),
                Some(&window),
                gtk::FileChooserAction::Save,
                &[
                    (&gettext("Cancel"), gtk::ResponseType::Reject),
                    (&gettext("Save"), gtk::ResponseType::Accept),
                ]
            );
            dialog.set_modal(true);

            // Set a filter to show only SQLite files
            let filter = gtk::FileFilter::new();
            gtk::FileFilter::set_name(&filter, Some("*.db"));
            filter.add_mime_type("application/x-sqlite3");
            dialog.add_filter(&filter);
            dialog.set_current_name("furtherance.db");

            dialog.connect_response(
                clone!(@strong dialog, @weak this as this2 => move |filechooser, resp| {
                    if resp == gtk::ResponseType::Accept {
                        if let Some(path) = filechooser.file().and_then(|file| file.path()) {
                            let path = &path.to_string_lossy();
                            let _bkup = database::backup_db(path.to_string());

                            let settings = settings_manager::get_settings();
                            let _ = settings.set_string("database-loc", &path.to_string());

                            let imp2 = imp::FurPreferencesWindow::from_obj(&this2);
                            imp2.database_loc_row.set_subtitle(&path.to_string());

                            let window = FurtheranceWindow::default();
                            window.reset_history_box();
                        }
                        dialog.close();
                    } else {
                        dialog.close();
                    }
                }),
            );

            dialog.show();

        }));
    }
}