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