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