/
main.cpp
384 lines (334 loc) · 10.1 KB
/
main.cpp
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <stdio.h>
#include <sqlite3.h>
#include <iostream>
#include <getopt.h>
#include <string>
#include <vector>
#include <signal.h>
#include <curses.h>
#include "activity.h"
#include "Pdb.h"
#define PACKAGE "pomodoro"
#define VERSION "0.0.1"
#define BREAK 5*60
#define LONG_BREAK 30*60
#define POMODORO 25*60
#define ACTION_ADD 1
#define ACTION_LIST 2
#define ACTION_DELETE 3
void print_help();
void add_activity(std::string, int expectedPomodoros);
void list_all_activities();
void run_timer(int seconds, std::string msg = "", bool quit = true);
std::string secsToTime(int seconds);
void runActivity(std::string id);
void deleteActivity(std::string id);
void completeActivity(std::string id, bool complete);
void notify(std::string);
static void finish_curses_and_quit(int sig);
static void finish_curses(int sig);
// This is the callback function to display the select data in the table
static int callback(void *NotUsed, int argc, char **argv, char **szColName)
{
for(int i = 0; i < argc; i++)
{
std::cout << szColName[i] << " = " << argv[i] << std::endl;
}
std::cout << "\n";
return 0;
}
int main(int argc, char**argv)
{
int opt;
int expectedPomodoros = 0;
int action = 0;
std::string activityTitle;
/*
// no arguments given
*/
if(argc == 1)
{
fprintf(stderr, "This program needs arguments....\n\n");
print_help();
return 1;
}
while((opt = getopt(argc, argv, "hVp:a:lbBs:d:c:u:")) != -1)
{
switch(opt)
{
case 'h':
print_help();
return 0;
break;
case 'V':
printf("%s %s\n\n", PACKAGE, VERSION);
return 0;
break;
case 'l':
list_all_activities();
return 0;
break;
case 'p':
if(!optarg || strlen(optarg) < 1)
{
fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt);
return 1;
}
else
{
expectedPomodoros = atoi(optarg);
}
break;
case 'a':
printf("Output: %s\n", optarg);
if(!optarg || strlen(optarg) < 1)
{
fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt);
return 1;
}
else
{
action = ACTION_ADD;
activityTitle = std::string(optarg);
}
break;
case 's':
if(!optarg || strlen(optarg) < 1)
{
fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt);
return 1;
}
else
{
runActivity(std::string(optarg));
}
break;
case 'd':
if(!optarg || strlen(optarg) < 1)
{
fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt);
return 1;
}
else
{
deleteActivity(std::string(optarg));
return 0;
}
break;
case 'c':
if(!optarg || strlen(optarg) < 1)
{
fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt);
return 1;
}
else
{
completeActivity(std::string(optarg), true);
return 0;
}
break;
case 'u':
if(!optarg || strlen(optarg) < 1)
{
fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt);
return 1;
}
else
{
completeActivity(std::string(optarg), false);
return 0;
}
break;
case 'b':
run_timer(BREAK);
return 0;
break;
case 'B':
run_timer(LONG_BREAK);
return 0;
break;
case ':':
//fprintf(stderr, "%s: Error - Option '%c' needs a value\n\n", PACKAGE, opt);
print_help();
return 1;
break;
case '?':
//fprintf(stderr, "%s: Error - No such option: '%c'\n\n", PACKAGE, optopt);
print_help();
return 1;
}
}
switch(action)
{
case ACTION_ADD:
add_activity(activityTitle, expectedPomodoros);
break;
}
return 0;
}
void print_help()
{
printf("%s,%s simple pomodoro application\n", PACKAGE, VERSION);
printf("%s [-h] [-V] [-a Activity] \n\n", PACKAGE);
printf(" -h print this help and exit\n");
printf(" -V print version and exit\n\n");
printf(" -a adds 'Activity' as a new activity\n");
printf(" -p NUMBER If used with -a add an estimated number of pomodoros for the new task\n");
printf(" -l Lists all uncompleted activities\n");
printf(" -b Starts a short break\n");
printf(" -B Starts a long break\n");
printf(" -s ID Starts a pomodoro for the task with id: ID\n");
printf(" -d ID Deletes the task with id: ID\n");
printf(" -c ID Marks the task with id: ID as completed\n");
printf(" -u ID Marks the task with id: ID as uncompleted\n");
}
void add_activity(std::string title, int expectedPomodoros)
{
Pdb db;
Activity act(&db, title, expectedPomodoros);
act.persist();
std::cout << "ACTIVITY: " << act.toString() << "added." << std::endl;
}
void list_all_activities()
{
Pdb db;
std::vector<Activity> activities = db.getAllActivities();
printf("%-5s\t%-50s\t%-20s\t%-2s\t%-2s\n", "ID", "TITLE", "DATE", "E", "C");
printf("=====\t=================================================\t====================\t==\t==\n\n");
for(int i = 0; i < activities.size(); i++)
{
Activity act = activities.at(i);
printf("%5i\t%-50s\t%20s\t%-2i\t%-2i\n", act.getId(), act.getTitle().c_str(), act.getDateAsText().c_str(), act.getExpectedPomodoros(), act.getPomodoros());
//std::cout << activities.at(i).toString() << std::endl;
}
}
static void finish_curses_and_quit(int sig)
{
finish_curses(sig);
exit(0);
}
static void finish_curses(int sig)
{
endwin();
}
void run_timer(int seconds, std::string msg, bool quit)
{
std::string timer;
timer = secsToTime(seconds);
(void) signal(SIGINT, finish_curses_and_quit); /* arrange interrupts to terminate */
(void) initscr(); /* initialize the curses library */
keypad(stdscr, TRUE); /* enable keyboard mapping */
(void) nonl(); /* tell curses not to do NL->CR/NL on output */
(void) cbreak(); /* take input chars one at a time, no wait for \n */
(void) noecho(); /* don't echo input */
if (has_colors())
{
start_color();
/*
* Simple color assignment, often all we need.
*/
init_pair(COLOR_BLACK, COLOR_BLACK, COLOR_BLACK);
init_pair(COLOR_GREEN, COLOR_GREEN, COLOR_BLACK);
init_pair(COLOR_RED, COLOR_RED, COLOR_BLACK);
init_pair(COLOR_CYAN, COLOR_CYAN, COLOR_BLACK);
init_pair(COLOR_WHITE, COLOR_WHITE, COLOR_BLACK);
init_pair(COLOR_MAGENTA, COLOR_MAGENTA, COLOR_BLACK);
init_pair(COLOR_BLUE, COLOR_BLUE, COLOR_BLACK);
init_pair(COLOR_YELLOW, COLOR_YELLOW, COLOR_BLACK);
}
int row,col; /* to store the number of rows and *
* the number of colums of the screen */
initscr(); /* start the curses mode */
getmaxyx(stdscr,row,col); /* get the number of rows and columns */
mvprintw(row/2,(col-strlen(timer.c_str()))/2,"%s",timer.c_str());
/* print the message at the center of the screen */
mvprintw(row-1,0, msg.c_str());
//printw("Try resizing your window(if possible) and then run this program again");
refresh();
for (;;)
{
//int c = getch(); /* refresh, accept single keystroke of input */
/* process the command keystroke */
sleep(1);
if(--seconds == 0)
{
mvprintw(row/2,(col-strlen("TIME IS UP!"))/2,"%s","TIME IS UP!");
refresh();
system("play ring.wav > /dev/null 2>&1 &");
if(!quit)
{
return finish_curses(0);
}
else
{
finish_curses_and_quit(0);
}
}
mvprintw(row/2,(col-strlen(" "))/2,"%s"," ");
timer = secsToTime(seconds);
mvprintw(row/2,(col-strlen(timer.c_str()))/2,"%s",timer.c_str());
refresh();
}
if(!quit)
{
return finish_curses(0);
}
else
{
finish_curses_and_quit(0);
}
}
std::string secsToTime(int seconds)
{
std::stringstream ss;
if(seconds < 60)
{
ss << "0:" << seconds;
}
else
{
int mins = seconds / 60;
seconds = seconds - mins * 60;
ss << mins << ":" << seconds;
}
return ss.str();
}
void runActivity(std::string id)
{
Pdb db;
Activity act = db.getActivity(id);
if(act.getId() == 0)
{
//No activity found
std::cerr << "No activity found for id: " << id << std::endl;
exit(1);
}
run_timer(POMODORO, act.toString(), false);
act.addPomodoro();
act.persist();
notify(act.getTitle() + " finished!");
}
void deleteActivity(std::string id)
{
Pdb db;
db.deleteActivity(id);
std::cout << "Activity with id: " << id << " deleted." << std::endl;
}
void completeActivity(std::string id, bool complete)
{
Pdb db;
Activity act = db.getActivity(id);
if(act.getId() == 0)
{
//No activity found
std::cerr << "No activity found for id: " << id << std::endl;
exit(1);
}
act.complete(complete);
act.persist();
}
void notify(std::string msg)
{
std::string cmd = "notify-send \"Pomodoro\" \"" + msg + "\"";
system(cmd.c_str());
}