added -f option to force usage of an unsupported version of taskwarrior
[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 <locale.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <getopt.h>
25 #include <sys/stat.h>
26
27 #include <json.h>
28
29 #include <glib/gi18n.h>
30
31 #include <config.h>
32
33 #include <log.h>
34 #include <note.h>
35 #include <settings.h>
36 #include <tw.h>
37 #include <ui.h>
38 #include <ui_projecttree.h>
39 #include <ui_taskpanel.h>
40 #include <ui_tasktree.h>
41
42 static const char *program_name;
43 static struct task **tasks;
44
45 static struct option long_options[] = {
46         {"version", no_argument, 0, 'v'},
47         {"help", no_argument, 0, 'h'},
48         {"force-unsupported-taskwarrior", no_argument, 0, 'f'},
49         {"debug", required_argument, 0, 'd'},
50         {0, 0, 0, 0}
51 };
52
53 static void print_version()
54 {
55         printf("ptask %s\n", VERSION);
56         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
57                  "License GPLv2: GNU GPL version 2 or later "
58                  "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
59                  "This is free software: you are free to change and "
60                  " redistribute it.\n"
61                  "There is NO WARRANTY, to the extent permitted by law.\n"),
62                "2012-2013");
63 }
64
65 static void print_help()
66 {
67         printf(_("Usage: %s [OPTION]...\n"), program_name);
68
69         puts(_("ptask is a task management user interface based"
70                " on taskwarrior."));
71
72         puts("");
73         puts(_("Options:"));
74         puts(_("  -h, --help          display this help and exit\n"
75                "  -v, --version       display version information and exit"));
76
77         puts("");
78         puts(_("  -f, --force-unsupported-taskwarrior force usage of an"
79                " unsupported version of taskwarrior"));
80         puts(_("  -d, --debug=LEVEL   "
81                "set the debug level, integer between 0 and 3"));
82
83         puts("");
84
85         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
86         puts("");
87         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
88 }
89
90 void refresh()
91 {
92         GtkWidget *dialog;
93         const char *current_prj, *current_uuid;
94         struct task **old_tasks;
95
96         log_fct_enter();
97         ui_taskpanel_update(NULL);
98
99         if (tasks) {
100                 old_tasks = tasks;
101                 current_prj = ui_projecttree_get_project();
102                 current_uuid = ui_tasktree_get_task_uuid();
103                 ui_tasktree_update(NULL);
104         } else {
105                 old_tasks = NULL;
106                 current_prj = NULL;
107                 current_uuid = NULL;
108         }
109
110         tasks = tw_get_all_tasks(ui_get_status_filter());
111
112         if (tasks) {
113                 ui_projecttree_update(tasks);
114                 ui_tasktree_update(tasks);
115                 if (current_uuid)
116                         ui_tasktree_set_selected_task(current_uuid);
117         } else {
118                 dialog = gtk_message_dialog_new(NULL,
119                                                 GTK_DIALOG_DESTROY_WITH_PARENT,
120                                                 GTK_MESSAGE_ERROR,
121                                                 GTK_BUTTONS_CLOSE,
122                                                 _("Error loading tasks, verify "
123                                                   "that a supported version of "
124                                                   "taskwarrior is installed."));
125                 gtk_dialog_run(GTK_DIALOG(dialog));
126                 gtk_widget_destroy(dialog);
127         }
128
129         if (old_tasks)
130                 tw_task_list_free(old_tasks);
131
132         log_fct_exit();
133 }
134
135 static void log_init()
136 {
137         char *home, *path, *dir;
138
139         home = getenv("HOME");
140
141         if (!home)
142                 return ;
143
144         dir = malloc(strlen(home)+1+strlen(".ptask")+1);
145         sprintf(dir, "%s/%s", home, ".ptask");
146         mkdir(dir, 0777);
147
148         path = malloc(strlen(dir)+1+strlen("log")+1);
149         sprintf(path, "%s/%s", dir, "log");
150
151         log_open(path);
152
153         free(dir);
154         free(path);
155 }
156
157 int main(int argc, char **argv)
158 {
159         GtkWindow *window;
160         GtkBuilder *builder;
161         int optc, cmdok, opti, ret;
162         GError *err;
163         gchar *msg;
164         GtkMessageDialog *diag;
165
166         program_name = argv[0];
167
168         setlocale(LC_ALL, "");
169
170 #if ENABLE_NLS
171         bindtextdomain(PACKAGE, LOCALEDIR);
172         textdomain(PACKAGE);
173 #endif
174
175         cmdok = 1;
176         while ((optc = getopt_long(argc, argv, "vhfd:", long_options,
177                                    &opti)) != -1) {
178                 switch (optc) {
179                 case 'h':
180                         print_help();
181                         exit(EXIT_SUCCESS);
182                 case 'v':
183                         print_version();
184                         exit(EXIT_SUCCESS);
185                 case 'd':
186                         log_level = atoi(optarg);
187                         log_info(_("Enables debug mode."));
188                         break;
189                 case 'f':
190                         log_info(_("Force usage of an unsupported version of "
191                                    "taskwarrior."));
192                         tw_enable_check_version(0);
193                         break;
194                 default:
195                         cmdok = 0;
196                         break;
197                 }
198         }
199
200         if (!cmdok || optind != argc) {
201                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
202                         program_name);
203                 exit(EXIT_FAILURE);
204         }
205
206         log_init();
207
208         gtk_init(NULL, NULL);
209
210         settings_init();
211
212         builder = gtk_builder_new();
213         err = NULL;
214         ret = gtk_builder_add_from_file
215                 (builder,
216                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "ptask.glade",
217                  &err);
218
219         if (!ret) {
220                 msg = g_strdup_printf(_("Failed to load UI: %s"),
221                                       err->message);
222                 log_err(msg);
223
224                 diag = GTK_MESSAGE_DIALOG(gtk_message_dialog_new
225                                           (NULL,
226                                            GTK_DIALOG_DESTROY_WITH_PARENT,
227                                            GTK_MESSAGE_ERROR,
228                                            GTK_BUTTONS_CLOSE,
229                                            msg,
230                                            NULL));
231
232                 gtk_dialog_run(GTK_DIALOG(diag));
233
234                 g_free(msg);
235
236                 exit(EXIT_FAILURE);
237         }
238
239         window = create_window(builder);
240
241         gtk_builder_connect_signals(builder, NULL);
242
243         g_object_unref(G_OBJECT(builder));
244
245         refresh();
246
247         gtk_widget_show_all(GTK_WIDGET(window));
248
249         gtk_main();
250
251         exit(EXIT_SUCCESS);
252 }