2 * Copyright (C) 2012-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 #include <json/json.h>
27 #include <glib/gi18n.h>
36 static const char *program_name;
37 static struct task **tasks;
38 static GtkTextView *w_note;
39 static GtkEntry *w_description;
40 static GtkEntry *w_project;
41 static GtkTreeView *w_treeview;
42 static GtkWidget *w_tasksave_btn;
43 static GtkWidget *w_taskdone_btn;
44 static GtkComboBox *w_status;
45 static GtkComboBox *w_priority;
46 static GSettings *settings;
56 static struct option long_options[] = {
57 {"version", no_argument, 0, 'v'},
58 {"help", no_argument, 0, 'h'},
59 {"debug", required_argument, 0, 'd'},
63 static void print_version()
65 printf("ptask %s\n", VERSION);
66 printf(_("Copyright (C) %s jeanfi@gmail.com\n"
67 "License GPLv2: GNU GPL version 2 or later "
68 "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
69 "This is free software: you are free to change and "
71 "There is NO WARRANTY, to the extent permitted by law.\n"),
75 static void print_help()
77 printf(_("Usage: %s [OPTION]...\n"), program_name);
79 puts(_("ptask is a task management user interface based"
84 puts(_(" -h, --help display this help and exit\n"
85 " -v, --version display version information and exit"));
89 puts(_(" -d, --debug=LEVEL "
90 "set the debug level, integer between 0 and 3"));
94 printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
96 printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
99 static struct task *get_selected_task(GtkTreeView *treeview)
102 GtkTreeViewColumn *cols;
103 struct task **tasks_cur;
109 log_debug("get_selected_task");
111 gtk_tree_view_get_cursor(treeview, &path, &cols);
114 model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
115 gtk_tree_model_get_iter(model, &iter, path);
116 gtk_tree_model_get_value(model, &iter, COL_UUID, &value);
118 uuid = g_value_get_string(&value);
120 for (tasks_cur = tasks; *tasks_cur; tasks_cur++)
121 if (!strcmp((*tasks_cur)->uuid, uuid))
124 gtk_tree_path_free(path);
130 static void clear_task_panel()
134 gtk_widget_set_sensitive(w_tasksave_btn, 0);
135 gtk_widget_set_sensitive(w_taskdone_btn, 0);
137 buf = gtk_text_view_get_buffer(w_note);
138 gtk_text_buffer_set_text(buf, "", 0);
139 gtk_widget_set_sensitive(GTK_WIDGET(w_note), 0);
141 gtk_entry_set_text(w_description, "");
142 gtk_widget_set_sensitive(GTK_WIDGET(w_description), 0);
144 gtk_entry_set_text(w_project, "");
145 gtk_widget_set_sensitive(GTK_WIDGET(w_project), 0);
147 gtk_combo_box_set_active(w_priority, 0);
148 gtk_widget_set_sensitive(GTK_WIDGET(w_priority), 0);
155 struct task **tasks_cur;
162 log_debug("refresh");
165 status = gtk_combo_box_get_active(w_status);
166 log_debug("status: %d", status);
169 tw_task_list_free(tasks);
173 tasks = tw_get_all_tasks("pending");
176 tasks = tw_get_all_tasks("completed");
179 tasks = tw_get_all_tasks("pending");
182 model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
183 gtk_list_store_clear(GTK_LIST_STORE(model));
186 for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
189 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
192 project = task->project;
196 gtk_list_store_set(GTK_LIST_STORE(model),
198 COL_ID, (*tasks_cur)->id,
200 (*tasks_cur)->description,
201 COL_PROJECT, project,
202 COL_UUID, (*tasks_cur)->uuid,
203 COL_PRIORITY, (*tasks_cur)->priority,
207 dialog = gtk_message_dialog_new(NULL,
208 GTK_DIALOG_DESTROY_WITH_PARENT,
211 _("Error loading tasks, verify "
212 "that a supported version of "
213 "taskwarrior is installed "));
214 gtk_dialog_run(GTK_DIALOG(dialog));
215 gtk_widget_destroy(dialog);
217 log_debug("refresh done");
220 int taskdone_clicked_cbk(GtkButton *btn, gpointer data)
224 task = get_selected_task(GTK_TREE_VIEW(w_treeview));
231 static int tasksave_clicked_cbk(GtkButton *btn, gpointer data)
236 GtkTextIter sIter, eIter;
240 task = get_selected_task(GTK_TREE_VIEW(w_treeview));
242 log_debug("tasksave_clicked_cbk %d", task->id);
244 buf = gtk_text_view_get_buffer(w_note);
246 gtk_text_buffer_get_iter_at_offset(buf, &sIter, 0);
247 gtk_text_buffer_get_iter_at_offset(buf, &eIter, -1);
248 txt = gtk_text_buffer_get_text(buf, &sIter, &eIter, TRUE);
250 log_debug("note=%s", txt);
252 if (!task->note || strcmp(txt, task->note))
253 note_put(task->uuid, txt);
255 ctxt = gtk_entry_get_text(w_description);
256 if (!task->description || strcmp(ctxt, task->description))
257 tw_modify_description(task->uuid, ctxt);
259 ctxt = gtk_entry_get_text(w_project);
260 if (!task->project || strcmp(ctxt, task->project))
261 tw_modify_project(task->uuid, ctxt);
263 priority = gtk_combo_box_get_active(w_priority);
264 log_debug("priority: %d", priority);
280 if (strcmp(task->priority, pri))
281 tw_modify_priority(task->uuid, pri);
288 int refresh_clicked_cbk(GtkButton *btn, gpointer data)
290 log_debug("refresh_clicked_cbk");
296 static int status_changed_cbk(GtkComboBox *w, gpointer data)
298 log_debug("status_changed_cbk");
304 static int priority_to_int(const char *str)
318 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
324 log_debug("cursor_changed_cbk");
326 task = get_selected_task(treeview);
330 buf = gtk_text_view_get_buffer(w_note);
332 gtk_text_buffer_set_text(buf,
336 gtk_text_buffer_set_text(buf, "", 0);
337 gtk_widget_set_sensitive(GTK_WIDGET(w_note), 1);
339 gtk_entry_set_text(w_description, task->description);
340 gtk_widget_set_sensitive(GTK_WIDGET(w_description), 1);
343 gtk_entry_set_text(w_project, task->project);
345 gtk_entry_set_text(w_project, "");
346 gtk_widget_set_sensitive(GTK_WIDGET(w_project), 1);
348 gtk_widget_set_sensitive(w_tasksave_btn, 1);
349 gtk_widget_set_sensitive(w_taskdone_btn, 1);
351 gtk_widget_set_sensitive(GTK_WIDGET(w_priority), 1);
352 priority = priority_to_int(task->priority);
353 gtk_combo_box_set_active(w_priority, priority);
355 log_debug("clear task widgets");
357 log_debug("clear task widgets done");
363 static gint priority_cmp(GtkTreeModel *model,
368 GValue v1 = {0,}, v2 = {0,};
369 const char *str1, *str2;
372 gtk_tree_model_get_value(model, a, COL_PRIORITY, &v1);
373 str1 = g_value_get_string(&v1);
374 i1 = priority_to_int(str1);
376 gtk_tree_model_get_value(model, b, COL_PRIORITY, &v2);
377 str2 = g_value_get_string(&v2);
378 i2 = priority_to_int(str2);
388 static void log_init()
390 char *home, *path, *dir;
392 home = getenv("HOME");
397 dir = malloc(strlen(home)+1+strlen(".ptask")+1);
398 sprintf(dir, "%s/%s", home, ".ptask");
401 path = malloc(strlen(dir)+1+strlen("log")+1);
402 sprintf(path, "%s/%s", dir, "log");
410 int main(int argc, char **argv)
416 int optc, cmdok, opti;
418 program_name = argv[0];
420 setlocale(LC_ALL, "");
423 bindtextdomain(PACKAGE, LOCALEDIR);
428 while ((optc = getopt_long(argc, argv, "vhd:", long_options,
438 log_level = atoi(optarg);
439 log_info(_("Enables debug mode."));
447 if (!cmdok || optind != argc) {
448 fprintf(stderr, _("Try `%s --help' for more information.\n"),
455 gtk_init(NULL, NULL);
457 settings = g_settings_new("ptask");
459 builder = gtk_builder_new();
460 gtk_builder_add_from_file
462 PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "ptask.glade",
464 window = create_window(builder, settings);
466 w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "treeview"));
467 model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
468 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(model),
474 w_note = GTK_TEXT_VIEW(gtk_builder_get_object(builder, "tasknote"));
476 w_description = GTK_ENTRY(gtk_builder_get_object(builder,
478 w_project = GTK_ENTRY(gtk_builder_get_object(builder, "taskproject"));
479 w_status = GTK_COMBO_BOX(gtk_builder_get_object(builder, "status"));
480 w_priority = GTK_COMBO_BOX(gtk_builder_get_object(builder,
483 gtk_builder_connect_signals(builder, NULL);
485 g_signal_connect(w_treeview,
486 "cursor-changed", (GCallback)cursor_changed_cbk,
488 g_signal_connect(w_status,
489 "changed", (GCallback)status_changed_cbk,
492 btn = GTK_WIDGET(gtk_builder_get_object(builder, "tasksave"));
493 g_signal_connect(btn,
494 "clicked", (GCallback)tasksave_clicked_cbk, tasks);
495 gtk_widget_set_sensitive(btn, 0);
496 w_tasksave_btn = btn;
498 w_taskdone_btn = GTK_WIDGET(gtk_builder_get_object(builder,
500 gtk_widget_set_sensitive(w_taskdone_btn, 0);
502 g_object_unref(G_OBJECT(builder));
506 gtk_widget_show_all(GTK_WIDGET(window));