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