keep focus on task after refresh
[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 void refresh()
96 {
97         GtkWidget *dialog;
98         const char *current_prj, *current_uuid;
99         struct task **old_tasks;
100
101         log_fct_enter();
102         ui_taskpanel_update(NULL);
103
104         if (tasks) {
105                 old_tasks = tasks;
106                 current_prj = ui_projecttree_get_project();
107                 current_uuid = ui_tasktree_get_task_uuid();
108                 ui_tasktree_update(NULL, NULL, NULL);
109         } else {
110                 old_tasks = NULL;
111                 current_prj = NULL;
112                 current_uuid = NULL;
113         }
114
115         tasks = tw_get_all_tasks(ui_get_status_filter());
116
117         if (tasks) {
118                 ui_projecttree_update(tasks);
119                 ui_tasktree_update(tasks, current_prj, current_uuid);
120         } else {
121                 dialog = gtk_message_dialog_new(NULL,
122                                                 GTK_DIALOG_DESTROY_WITH_PARENT,
123                                                 GTK_MESSAGE_ERROR,
124                                                 GTK_BUTTONS_CLOSE,
125                                                 _("Error loading tasks, verify "
126                                                   "that a supported version of "
127                                                   "taskwarrior is installed "));
128                 gtk_dialog_run(GTK_DIALOG(dialog));
129                 gtk_widget_destroy(dialog);
130         }
131
132         if (old_tasks)
133                 tw_task_list_free(old_tasks);
134
135         log_fct(__func__, "EXIT");
136 }
137
138 static int cursor_changed_cbk(GtkTreeView *treeview, gpointer data)
139 {
140         log_fct_enter();
141
142         ui_taskpanel_update(ui_tasktree_get_selected_task());
143
144         log_fct_exit();
145
146         return FALSE;
147 }
148
149 static void log_init()
150 {
151         char *home, *path, *dir;
152
153         home = getenv("HOME");
154
155         if (!home)
156                 return ;
157
158         dir = malloc(strlen(home)+1+strlen(".ptask")+1);
159         sprintf(dir, "%s/%s", home, ".ptask");
160         mkdir(dir, 0777);
161
162         path = malloc(strlen(dir)+1+strlen("log")+1);
163         sprintf(path, "%s/%s", dir, "log");
164
165         log_open(path);
166
167         free(dir);
168         free(path);
169 }
170
171 int main(int argc, char **argv)
172 {
173         GtkWindow *window;
174         GtkBuilder *builder;
175         int optc, cmdok, opti;
176
177         program_name = argv[0];
178
179         setlocale(LC_ALL, "");
180
181 #if ENABLE_NLS
182         bindtextdomain(PACKAGE, LOCALEDIR);
183         textdomain(PACKAGE);
184 #endif
185
186         cmdok = 1;
187         while ((optc = getopt_long(argc, argv, "vhd:", long_options,
188                                    &opti)) != -1) {
189                 switch (optc) {
190                 case 'h':
191                         print_help();
192                         exit(EXIT_SUCCESS);
193                 case 'v':
194                         print_version();
195                         exit(EXIT_SUCCESS);
196                 case 'd':
197                         log_level = atoi(optarg);
198                         log_info(_("Enables debug mode."));
199                         break;
200                 default:
201                         cmdok = 0;
202                         break;
203                 }
204         }
205
206         if (!cmdok || optind != argc) {
207                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
208                         program_name);
209                 exit(EXIT_FAILURE);
210         }
211
212         log_init();
213
214         gtk_init(NULL, NULL);
215
216         settings = g_settings_new("ptask");
217
218         builder = gtk_builder_new();
219         gtk_builder_add_from_file
220                 (builder,
221                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "ptask.glade",
222                  NULL);
223         window = create_window(builder, settings);
224
225         ui_taskpanel_init(builder);
226         ui_tasktree_init(builder);
227         ui_projecttree_init(builder);
228
229         w_treeview = GTK_TREE_VIEW(gtk_builder_get_object(builder, "tasktree"));
230
231         gtk_builder_connect_signals(builder, NULL);
232
233         g_signal_connect(w_treeview,
234                          "cursor-changed", (GCallback)cursor_changed_cbk,
235                          tasks);
236
237         g_object_unref(G_OBJECT(builder));
238
239         refresh();
240
241         gtk_widget_show_all(GTK_WIDGET(window));
242
243         gtk_main();
244
245         exit(EXIT_SUCCESS);
246 }