new upstream release 0.0.9
[ptask-pkg-ubuntu.git] / src / ui_newtask_diag.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
20 #include <string.h>
21
22 #include <gtk/gtk.h>
23
24 #include <log.h>
25 #include <tw.h>
26 #include <ui.h>
27
28 static const char *ui_get_priority(GtkComboBox *combo)
29 {
30         int prio;
31
32         prio = gtk_combo_box_get_active(combo);
33
34         switch (prio) {
35         case 3:
36                 return "H";
37         case 2:
38                 return "M";
39         case 1:
40                 return "L";
41         default:
42                 return "";
43         }
44 }
45
46 static void populate_project(GtkComboBoxText *w)
47 {
48         struct task **tasks;
49         struct project **all_prjs, **prjs;
50
51         tasks = tw_get_all_tasks("pending");
52
53         all_prjs = tw_get_projects(tasks);
54
55         prjs = all_prjs;
56         while (*prjs) {
57                 if (strcmp((*prjs)->name, "ALL"))
58                         gtk_combo_box_text_append_text(w, (*prjs)->name);
59
60                 prjs++;
61         }
62
63         tw_task_list_free(tasks);
64         tw_project_list_free(all_prjs);
65 }
66
67 void ui_newtask()
68 {
69         gint result;
70         static GtkDialog *diag;
71         GtkBuilder *builder;
72         GtkEntry *entry;
73         const char *desc, *prj, *prio;
74         GtkComboBox *combo;
75         GtkComboBoxText *w_prj;
76
77         log_debug("ui_newtask()");
78
79         builder = gtk_builder_new();
80         gtk_builder_add_from_file
81                 (builder,
82                  PACKAGE_DATA_DIR G_DIR_SEPARATOR_S "ptask.glade",
83                  NULL);
84         diag = GTK_DIALOG(gtk_builder_get_object(builder, "diag_tasknew"));
85         gtk_builder_connect_signals(builder, NULL);
86
87         w_prj = GTK_COMBO_BOX_TEXT(gtk_builder_get_object
88                                    (builder, "diag_tasknew_project"));
89         populate_project(w_prj);
90
91         result = gtk_dialog_run(diag);
92
93         if (result == GTK_RESPONSE_ACCEPT) {
94                 log_debug("ui_newtask(): ok");
95
96                 entry = GTK_ENTRY(gtk_builder_get_object
97                                   (builder, "diag_tasknew_description"));
98                 desc = gtk_entry_get_text(entry);
99
100                 w_prj = GTK_COMBO_BOX_TEXT(gtk_builder_get_object
101                                            (builder, "diag_tasknew_project"));
102                 prj = gtk_combo_box_text_get_active_text(w_prj);
103
104                 combo = GTK_COMBO_BOX(gtk_builder_get_object
105                                       (builder, "diag_tasknew_priority"));
106                 prio = ui_get_priority(combo);
107
108                 log_debug("ui_newtask(): description=%s project=%s priority=%d",
109                           desc,
110                           prj,
111                           prio);
112
113                 tw_add(desc, prj, prio);
114                 refresh();
115         } else {
116                 log_debug("ui_newtask(): cancel");
117         }
118
119         g_object_unref(G_OBJECT(builder));
120
121         gtk_widget_destroy(GTK_WIDGET(diag));
122 }