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