Added initialization of project list at startup

This commit is contained in:
Patrick Lipka 2021-12-16 16:34:06 +01:00
parent 183717c956
commit 416bcbde0a
4 changed files with 43 additions and 13 deletions

View File

@ -104,7 +104,7 @@ void ProjectList::save(std::string file_name){
of.close(); of.close();
} }
void ProjectList::load(std::string file_name){ void ProjectList::load(std::string file_name, bool ignore_worktimes){
std::ifstream inf(file_name, std::ios::binary); std::ifstream inf(file_name, std::ios::binary);
int active; int active;
if (!inf){ if (!inf){
@ -142,7 +142,7 @@ void ProjectList::load(std::string file_name){
Task task(task_name); Task task(task_name);
int work_time=0; int work_time=0;
inf.read((char*) &(work_time), sizeof(int)); inf.read((char*) &(work_time), sizeof(int));
task.add_time(work_time); if(!ignore_worktimes) task.add_time(work_time);
proj.add_task(task); proj.add_task(task);
} }
// needed to get the active_task pointers right _after_ adding tasks to projetcs // needed to get the active_task pointers right _after_ adding tasks to projetcs

View File

@ -33,7 +33,7 @@ class ProjectList{
Project *active_project; Project *active_project;
void set_active_project(int id); void set_active_project(int id);
void save(std::string file_name); void save(std::string file_name);
void load(std::string file_name); void load(std::string file_name, bool ignore_worktimes=false);
Project *find_project_by_name(std::string proj_name); Project *find_project_by_name(std::string proj_name);
int find_project_id_by_name(std::string proj_name); int find_project_id_by_name(std::string proj_name);
}; };

View File

@ -27,6 +27,27 @@ std::string get_date(){
return date_str; return date_str;
} }
std::string get_last_date(){
time_t t = time(NULL);
tm *date = localtime(&t);
int month = date->tm_mon;
int year = date->tm_year+1900;
if(month == 0){
month = 12;
year--;
}
std::string month_str;
if (month < 10){
month_str = "0"+std::to_string(month);
}else{
month_str = std::to_string(month);
}
std::string date_str = std::to_string(year)+"-"+month_str;
return date_str;
}
void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir){ void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir){
std::ifstream f(file_name); std::ifstream f(file_name);
if (!f){ if (!f){
@ -62,20 +83,28 @@ int main(){
config_file = prefix+"/etc/tt.conf"; config_file = prefix+"/etc/tt.conf";
parse_config_file(config_file, &user_name, &tracking_dir); parse_config_file(config_file, &user_name, &tracking_dir);
// TEST: set up test list ProjectList proj_list(get_date());
ProjectList proj_list("dec");
Project proj("Test Project"); // if available: load project list for current month, else: start with list from last month
Task task1("Task 1"),task2("Task 2"),task3("Task 3"); std::string proj_file = tracking_dir+"/"+get_date();
proj.add_task(task1); std::string last_proj_file = tracking_dir+"/"+get_last_date();
proj.add_task(task2); std::ifstream f(proj_file);
proj.add_task(task3); std::ifstream lf(last_proj_file);
proj_list.add_project(proj); if(f){
// proj_list.load(get_date()); f.close();
lf.close();
proj_list.load(proj_file);
}else if (lf){
f.close();
lf.close();
// don't load work times from previous month
proj_list.load(last_proj_file,true);
}
init_autocomplete(&proj_list); init_autocomplete(&proj_list);
// use GNU readline for auto completion and history when parsing command input // use GNU readline for auto completion and history when parsing command input
while(1) { while(1) {
rl_attempted_completion_function = tt_name_completion; rl_attempted_completion_function = tt_name_completion;
char *buffer = readline("tt> "); char *buffer = readline("tt> ");
if (buffer){ if (buffer){

View File

@ -9,5 +9,6 @@ extern std::string tracking_dir;
extern std::string config_file; extern std::string config_file;
std::string get_date(); std::string get_date();
std::string get_last_date();
void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir); void parse_config_file(std::string file_name, std::string *user_name, std::string *tracking_dir);
#endif #endif