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