about summary refs log tree commit diff
path: root/src/ui/task_row.rs
blob: f55aba4f3768a7b0567b580756d643a512431b97 (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
// 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 gtk::subclass::prelude::*;
use gtk::{glib, gio, prelude::*, CompositeTemplate};
use chrono::DateTime;
use std::sync::Mutex;
use once_cell::sync::Lazy;
use glib::clone;

use crate::database::Task;
use crate::ui::{FurTaskDetails, FurtheranceWindow};
use crate::settings_manager;


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

    #[derive(Debug, CompositeTemplate, Default)]
    #[template(resource = "/com/lakoliu/Furtherance/gtk/task_row.ui")]
    pub struct FurTaskRow {
        #[template_child]
        pub task_name_label: TemplateChild<gtk::Label>,
        #[template_child]
        pub total_time_label: TemplateChild<gtk::Label>,
        // pub tasks: Vec<database::Task>,
        pub tasks: Lazy<Mutex<Vec<Task>>>,
    }

    #[glib::object_subclass]
    impl ObjectSubclass for FurTaskRow {
        const NAME: &'static str = "FurTaskRow";
        type ParentType = gtk::ListBoxRow;
        type Type = super::FurTaskRow;

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

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

    impl ObjectImpl for FurTaskRow {
        fn constructed(&self, obj: &Self::Type) {
            obj.setup_signals();
            self.parent_constructed(obj);
        }
    }

    impl WidgetImpl for FurTaskRow {}

    impl ListBoxRowImpl for FurTaskRow {}
}

glib::wrapper! {
    pub struct FurTaskRow(
        ObjectSubclass<imp::FurTaskRow>)
        @extends gtk::Widget, gtk::ListBoxRow;
}

impl FurTaskRow {
    pub fn new() -> Self {
        /* This should take a model, object, or vec filled with the description
        details and fill out the labels based on those. */
        glib::Object::new(&[]).expect("Failed to create `FurTaskRow`.")
    }

    fn setup_signals(&self) {
        let open_details_action = gio::SimpleAction::new("open-details", None);

        open_details_action.connect_activate(clone!(@strong self as this => move |_, _| {
            let dialog = FurTaskDetails::new();
            dialog.setup_widgets(this.get_tasks());
            dialog.show();
        }));

        let actions = gio::SimpleActionGroup::new();
        self.insert_action_group("task-row", Some(&actions));
        actions.add_action(&open_details_action);
    }

    pub fn set_row_labels(&self, task_list: Vec<Task>) {
        let imp = imp::FurTaskRow::from_instance(&self);
        for task in task_list.clone() {
            imp.tasks.lock().unwrap().push(task);
        }
        let task_name_text = &task_list[0].task_name;
        imp.task_name_label.set_text(&imp.tasks.lock().unwrap()[0].task_name);

        // Create right-click gesture
        let gesture = gtk::GestureClick::new();
        gesture.set_button(gtk::gdk::ffi::GDK_BUTTON_SECONDARY as u32);
        gesture.connect_pressed(clone!(@strong task_name_text => move |gesture, _, _, _| {
            gesture.set_state(gtk::EventSequenceState::Claimed);
            let window = FurtheranceWindow::default();
            window.duplicate_task(task_name_text.to_string());
        }));

        self.add_controller(&gesture);

        // Add up all durations for task of said name to create total_time
        let mut total_time: i64 = 0;
        for task in &task_list {
            if task.task_name == task.task_name {
                let start_time = DateTime::parse_from_rfc3339(&task.start_time).unwrap();
                let stop_time = DateTime::parse_from_rfc3339(&task.stop_time).unwrap();

                let duration = stop_time - start_time;
                total_time += duration.num_seconds();
            }
        }
        // Format total time to readable string
        let h = total_time / 3600;
        let m = total_time % 3600 / 60;
        let s = total_time % 60;
        let mut total_time_str = format!("{:02}:{:02}:{:02}", h, m, s);

        if !settings_manager::get_bool("show-seconds") {
            total_time_str = format!("{:02}:{:02}", h, m);
        }

        imp.total_time_label.set_text(&total_time_str);
    }

    pub fn get_tasks(&self) -> Vec<Task> {
        let imp = imp::FurTaskRow::from_instance(&self);
        imp.tasks.lock().unwrap().to_vec()
    }
}