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
|
// 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::{PreferencesGroupExt, PreferencesPageExt};
use adw::subclass::prelude::*;
use chrono::{DateTime, Duration, Local};
use chrono_locale::LocaleDate;
use gettextrs::*;
use gtk::glib;
use std::env;
use crate::database::{self, SortOrder, TaskSort};
use crate::settings_manager;
use crate::ui::FurTasksGroup;
mod imp {
use super::*;
use glib::subclass;
use gtk::CompositeTemplate;
use std::cell::RefCell;
#[derive(Default, Debug, CompositeTemplate)]
#[template(resource = "/com/lakoliu/Furtherance/gtk/tasks_page.ui")]
pub struct FurTasksPage {
pub all_groups: RefCell<Vec<FurTasksGroup>>,
}
#[glib::object_subclass]
impl ObjectSubclass for FurTasksPage {
const NAME: &'static str = "FurTasksPage";
type ParentType = adw::PreferencesPage;
type Type = super::FurTasksPage;
fn class_init(klass: &mut Self::Class) {
Self::bind_template(klass);
}
fn instance_init(obj: &subclass::InitializingObject<Self>) {
obj.init_template();
}
}
impl ObjectImpl for FurTasksPage {
fn constructed(&self) {
let obj = self.obj();
obj.setup_widgets();
self.parent_constructed();
}
}
impl WidgetImpl for FurTasksPage {}
impl PreferencesPageImpl for FurTasksPage {}
}
glib::wrapper! {
pub struct FurTasksPage(
ObjectSubclass<imp::FurTasksPage>)
@extends gtk::Widget, adw::PreferencesPage;
}
impl FurTasksPage {
fn setup_widgets(&self) {
self.build_task_list();
}
pub fn clear_task_list(&self) {
let imp = imp::FurTasksPage::from_obj(&self);
for group in &*imp.all_groups.borrow() {
self.remove(group);
}
imp.all_groups.borrow_mut().clear();
}
pub fn build_task_list(&self) {
let imp = imp::FurTasksPage::from_obj(&self);
// Get user's locale for date formatting
let locale_env = env::var("LANG");
let user_locale: String;
if let Err(_) = locale_env {
user_locale = "en_US".to_string();
} else {
let locale_env = locale_env.unwrap();
let split_locale: Vec<&str> = locale_env.split(".").collect();
user_locale = split_locale[0].to_string();
}
let tasks_list = database::retrieve(TaskSort::StartTime, SortOrder::Descending).unwrap();
let mut uniq_date_list: Vec<String> = Vec::new();
let mut same_date_list: Vec<database::Task> = Vec::new();
let mut tasks_sorted_by_day: Vec<Vec<database::Task>> = Vec::new();
// Go through tasks list and look at all dates
let mut i: u32 = 1;
let len = tasks_list.len() as u32;
for task in tasks_list {
let task_clone = task.clone();
let date = DateTime::parse_from_rfc3339(&task.start_time).unwrap();
let date = date.formatl("%h %e", &user_locale).to_string();
if !uniq_date_list.contains(&date) {
// if same_date_list is empty, push "date" to it
// if it is not empty, push it to a vec of vecs, and then clear it
// and push "date" to it
if same_date_list.is_empty() {
same_date_list.push(task_clone);
} else {
tasks_sorted_by_day.push(same_date_list.clone());
same_date_list.clear();
same_date_list.push(task_clone);
}
uniq_date_list.push(date);
} else {
// otherwise push the task to the list of others with the same date
same_date_list.push(task_clone);
}
// If this is the last iteration, push the list of objects to sorted_by_day
if i == len {
tasks_sorted_by_day.push(same_date_list.clone());
}
i += 1;
if settings_manager::get_bool("limit-tasks") {
if uniq_date_list.len() > settings_manager::get_int("limit-days") as usize {
if same_date_list.len() > 0 && i != len {
tasks_sorted_by_day.push(same_date_list.clone());
}
uniq_date_list.pop();
break;
}
}
}
// Create FurTasksGroups for all unique days
let now = Local::now();
let yesterday = now - Duration::days(1);
let yesterday = yesterday.formatl("%h %e", &user_locale).to_string();
let today = now.formatl("%h %e", &user_locale).to_string();
for i in 0..uniq_date_list.len() {
let group = FurTasksGroup::new();
if uniq_date_list[i] == today {
group.set_title(&gettext("Today"));
} else if uniq_date_list[i] == yesterday {
group.set_title(&gettext("Yesterday"));
} else {
group.set_title(&uniq_date_list[i]);
}
self.add(&group);
group.add_task_model(tasks_sorted_by_day[i].clone());
// Set total time for each day
if settings_manager::get_bool("show-daily-sums") {
let day_total_time = group.get_total_day_time();
// Format total time to readable string
let h = day_total_time / 3600;
let m = day_total_time % 3600 / 60;
let s = day_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);
}
group.set_description(Some(&total_time_str));
}
imp.all_groups.borrow_mut().push(group);
}
}
}
|