6411492d94692fe307409c877a71713e0c6eb1e7
[ptask.git] / src / main.c
1 /*
2  * Copyright (C) 2012-2013 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <getopt.h>
23 #include <sys/stat.h>
24
25 #include <json/json.h>
26
27 #include <glib/gi18n.h>
28
29 #include <config.h>
30
31 #include <log.h>
32 #include <note.h>
33 #include <tw.h>
34 #include <ui.h>
35 #include <ui_projecttree.h>
36 #include <ui_taskpanel.h>
37 #include <ui_tasktree.h>
38
39 static const char *program_name;
40 static struct task **tasks;
41 static GtkTreeView *w_treeview;
42 static GSettings *settings;
43
44 enum {
45         COL_ID,
46         COL_DESCRIPTION,
47         COL_PROJECT,
48         COL_UUID,
49         COL_PRIORITY
50 };
51
52 static struct option long_options[] = {
53         {"version", no_argument, 0, 'v'},
54         {"help", no_argument, 0, 'h'},
55         {"debug", required_argument, 0, 'd'},
56         {0, 0, 0, 0}
57 };
58
59 static void print_version()
60 {
61         printf("ptask %s\n", VERSION);
62         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
63                  "License GPLv2: GNU GPL version 2 or later "
64                  "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
65                  "This is free software: you are free to change and "
66                  " redistribute it.\n"
67                  "There is NO WARRANTY, to the extent permitted by law.\n"),
68                "2012-2013");
69 }
70
71 static void print_help()
72 {
73         printf(_("Usage: %s [OPTION]...\n"), program_name);
74
75         puts(_("ptask is a task management user interface based"
76                " on taskwarrior."));
77
78         puts("");
79         puts(_("Options:"));
80         puts(_("  -h, --help          display this help and exit\n"
81                "  -v, --version       display version information and exit"));
82
83         puts("");
84
85         puts(_("  -d, --debug=LEVEL   "
86                "set the debug level, integer between 0 and 3"));
87
88         puts("");
89
90         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
91         puts("");
92         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
93 }
94
95 static struct task *get_selected_task(GtkTreeView *treeview)
96 {
97         GtkTreePath *path;
98         GtkTreeViewColumn *cols;
99         struct task **tasks_cur;
100         GtkTreeIter iter;
101         GtkTreeModel *model;
102         GValue value = {0,};
103         const char *uuid;
104
105         log_debug("get_selected_task");
106
107         gtk_tree_view_get_cursor(treeview, &path, &cols);
108
109         if (path) {
110                 model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview));
111                 gtk_tree_model_get_iter(model, &iter, path);
112                 gtk_tree_model_get_value(model, &iter, COL_UUID, &value);
113
114                 uuid = g_value_get_string(&value);
115
116                 for (tasks_cur = tasks; *tasks_cur; tasks_cur++)
117                         if (!strcmp((*tasks_cur)->uuid, uuid))
118                                 return *tasks_cur;
119
120                 gtk_tree_path_free(path);
121         }
122
123         return NULL;
124 }
125
126 void refresh()
127 {
128         GtkWidget *dialog;
129         GtkTreeModel *model;
130         struct task **tasks_cur;
131         struct task *task;
132         int i;
133         GtkTreeIter iter;
134         const char *project;
135
136         log_fct_enter();
137         ui_taskpanel_update(NULL);
138
139         if (tasks)
140                 tw_task_list_free(tasks);
141
142         tasks = tw_get_all_tasks(ui_get_status_filter());
143
144         model = gtk_tree_view_get_model(GTK_TREE_VIEW(w_treeview));
145         gtk_list_store_clear(GTK_LIST_STORE(model));
146
147         if (tasks) {
148                 for (tasks_cur = tasks, i = 0; *tasks_cur; tasks_cur++, i++) {
149                         task = (*tasks_cur);
150
151                         gtk_list_store_append(GTK_LIST_STORE(model), &iter);
152
153                         if (task->project)
154                                 project = task->project;
155                         else
156                                 project = "";
157
158                         gtk_list_store_set(GTK_LIST_STORE(model),
159                                            &iter,
160                                            COL_ID, (*tasks_cur)->id,
161                                            COL_DESCRIPTION,
162                                            (*tasks_cur)->description,
163                                            COL_PROJECT, project,
164                                            COL_UUID, (*tasks_cur)->uuid,
165                                            COL_PRIORITY, (*tasks_cur)->priority,
166                                            -1);
167                 }
168                 ui_projecttree_update(tasks);
169         } else {
170                 dialog = gtk_message_dialog_new(NULL,
171                                                 GTK_DIALOG_DESTROY_WITH_PARENT,
172                                                 GTK_MESSAGE_ERROR,
173                                                 GTK_BUTTONS_CLOSE,
174                                                 _("Error loading tasks, verify "
175                                                   "that a supported version of "
176                                                   "taskwarrior is installed "));
177                 gtk_dialog_run(GTK_DIALOG(dialog));
178                 gtk_widget_destroy(dialog);
179         }
180         log_fct(__func__, "EXIT");
181 }
182
183 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
184 {
185         log_fct_enter();
186
187         ui_taskpanel_update(get_selected_task(treeview));
188
189         log_fct_exit();
190
191         return FALSE;
192 }
193
194 static void log_init()
195 {
196         char *home, *path, *dir;
197
198         home = getenv("HOME");
199
200         if (!home)
201                 return ;
202
203         dir = malloc(strlen(home)+1+strlen(".ptask")+1);
204         sprintf(dir, "%s/%s", home, ".ptask");
205         mkdir(dir, 0777);
206
207         path = malloc(strlen(dir)+1+strlen("log")+1);
208         sprintf(path, "%s/%s", dir, "log");
209
210         log_open(path);
211
212         free(dir);
213         free(path);
214 }
215
216 int main(int argc, char **argv)
217 {
218         GtkWindow *window;
219         GtkBuilder *builder;
220         int optc, cmdok, opti;
221
222         program_name = argv[0];
223
224         setlocale(LC_ALL, "");
225
226 #if ENABLE_NLS
227         bindtextdomain(PACKAGE, LOCALEDIR);
228         textdomain(PACKAGE);
229 #endif
230
231         cmdok = 1;
232         while ((optc = getopt_long(argc, argv, "vhd:", long_options,
233                                    &opti)) != -1) {
234                 switch (optc) {
235                 case 'h':
236                         print_help();
237                         exit(EXIT_SUCCESS);
238                 case 'v':
239                         print_version();
240                         exit(EXIT_SUCCESS);
241                 case 'd':
242                         log_level = atoi(optarg);
243                         log_info(_("Enables debug mode."));
244                         break;
245                 default:
246                         cmdok = 0;
247                         break;
248                 }
249         }
250
251         if (!cmdok || optind != argc) {
252                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
253                         program_name);
254                 exit(EXIT_FAILURE);
255         }
256
257         log_init();
258
259         gtk_init(NULL, NULL);
260
261         settings = g_settings_new("ptask");
262
263         builder = gtk_builder_new();
264         gtk_builder_add_from_file
265                 (builder,
266                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "ptask.glade",
267                  NULL);
268         window = create_window(builder, settings);
269
270         ui_taskpanel_init(builder);
271         ui_tasktree_init(builder);
272         ui_projecttree_init(builder);
273
274         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "tasktree"));
275
276         gtk_builder_connect_signals(builder, NULL);
277
278         g_signal_connect(w_treeview,
279                          "cursor-changed", (GCallback)cursor_changed_cbk,
280                          tasks);
281
282         g_object_unref(G_OBJECT(builder));
283
284         refresh();
285
286         gtk_widget_show_all(GTK_WIDGET(window));
287
288         gtk_main();
289
290         exit(EXIT_SUCCESS);
291 }