1 | /* rgtaskswin.cc |
---|
2 | * |
---|
3 | * Copyright (c) 2004 Michael Vogt |
---|
4 | * |
---|
5 | * Author: Michael Vogt <mvo@debian.org> |
---|
6 | * |
---|
7 | * This program is free software; you can redistribute it and/or |
---|
8 | * modify it under the terms of the GNU General Public License as |
---|
9 | * published by the Free Software Foundation; either version 2 of the |
---|
10 | * License, or (at your option) any later version. |
---|
11 | * |
---|
12 | * This program is distributed in the hope that it will be useful, |
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | * GNU General Public License for more details. |
---|
16 | * |
---|
17 | * You should have received a copy of the GNU General Public License |
---|
18 | * along with this program; if not, write to the Free Software |
---|
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
20 | * USA |
---|
21 | */ |
---|
22 | |
---|
23 | #include <gtk/gtk.h> |
---|
24 | #include <cassert> |
---|
25 | #include <list> |
---|
26 | #include "config.h" |
---|
27 | #include "rgtaskswin.h" |
---|
28 | #include "rgmainwindow.h" |
---|
29 | #include "rguserdialog.h" |
---|
30 | #include "i18n.h" |
---|
31 | |
---|
32 | enum { |
---|
33 | TASK_CHECKBOX_COLUMN, |
---|
34 | TASK_SENSITIVE_COLUMN, |
---|
35 | TASK_NAME_COLUMN, |
---|
36 | TASK_DESCR_COLUMN, |
---|
37 | TASK_N_COLUMNS |
---|
38 | }; |
---|
39 | |
---|
40 | void RGTasksWin::cbButtonOkClicked(GtkWidget *self, void *data) |
---|
41 | { |
---|
42 | //cout << "cbButtonOkClicked(GtkWidget *self, void *data)"<<endl; |
---|
43 | RGTasksWin *me = (RGTasksWin*)data; |
---|
44 | |
---|
45 | GtkTreeIter iter; |
---|
46 | GtkTreeModel *model = GTK_TREE_MODEL(me->_store); |
---|
47 | if(!gtk_tree_model_get_iter_first(model, &iter)) { |
---|
48 | me->hide(); |
---|
49 | return; |
---|
50 | } |
---|
51 | |
---|
52 | me->setBusyCursor(true); |
---|
53 | |
---|
54 | // get selected tasks |
---|
55 | bool act=false; |
---|
56 | gboolean marked = FALSE; |
---|
57 | gboolean activatable = FALSE; |
---|
58 | gchar *taskname = NULL; |
---|
59 | string cmd = _config->Find("Synaptic::taskHelperProg", "/usr/bin/tasksel"); |
---|
60 | do { |
---|
61 | gtk_tree_model_get(model, &iter, |
---|
62 | TASK_CHECKBOX_COLUMN, &marked, |
---|
63 | TASK_SENSITIVE_COLUMN, &activatable, |
---|
64 | TASK_NAME_COLUMN, &taskname, |
---|
65 | -1); |
---|
66 | // only install if the state has changed |
---|
67 | if(marked && activatable) { |
---|
68 | //cout << "selected: " << taskname << endl; |
---|
69 | cmd += " --task-packages " + string(taskname); |
---|
70 | act=true; |
---|
71 | } |
---|
72 | } while(gtk_tree_model_iter_next(model, &iter)); |
---|
73 | |
---|
74 | // cout << "cmd: " << cmd << endl; |
---|
75 | |
---|
76 | vector<string> packages; |
---|
77 | // only act if at least one task was selected |
---|
78 | if(act) { |
---|
79 | char buf[255]; |
---|
80 | FILE *f = popen(cmd.c_str(), "r"); |
---|
81 | while(fgets(buf, 254, f) != NULL) { |
---|
82 | packages.push_back(string(g_strstrip(buf))); |
---|
83 | } |
---|
84 | pclose(f); |
---|
85 | |
---|
86 | #if 0 // some debug code |
---|
87 | cout << "got: " << endl; |
---|
88 | for(int i=0;i<packages.size();i++) { |
---|
89 | cout << packages[i] << endl; |
---|
90 | } |
---|
91 | #endif |
---|
92 | } |
---|
93 | |
---|
94 | me->setBusyCursor(false); |
---|
95 | me->hide(); |
---|
96 | |
---|
97 | me->_mainWin->selectToInstall(packages); |
---|
98 | |
---|
99 | } |
---|
100 | |
---|
101 | void RGTasksWin::cbButtonCancelClicked(GtkWidget *self, void *data) |
---|
102 | { |
---|
103 | //cout << "cbButtonCancelClicked(GtkWidget *self, void *data)"<<endl; |
---|
104 | RGTasksWin *me = (RGTasksWin*)data; |
---|
105 | me->hide(); |
---|
106 | } |
---|
107 | |
---|
108 | void RGTasksWin::cbButtonDetailsClicked(GtkWidget *self, void *data) |
---|
109 | { |
---|
110 | //cout << "cbButtonDetailsClicked(GtkWidget *self, void *data)"<<endl; |
---|
111 | RGTasksWin *me = (RGTasksWin*)data; |
---|
112 | |
---|
113 | me->setBusyCursor(true); |
---|
114 | |
---|
115 | // get selected task-name |
---|
116 | GtkTreeIter iter; |
---|
117 | GtkTreeSelection* selection; |
---|
118 | selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(me->_taskView)); |
---|
119 | if(!gtk_tree_selection_get_selected (selection, |
---|
120 | (GtkTreeModel**)(&me->_store), |
---|
121 | &iter)) { |
---|
122 | return; |
---|
123 | } |
---|
124 | gchar *str; |
---|
125 | gtk_tree_model_get(GTK_TREE_MODEL(me->_store), &iter, |
---|
126 | TASK_NAME_COLUMN, &str, -1); |
---|
127 | |
---|
128 | // ask tasksel about the selected task |
---|
129 | string cmd = _config->Find("Synaptic::taskHelperProg", "/usr/bin/tasksel") + " --task-desc " + string(str); |
---|
130 | string taskDescr; |
---|
131 | char buf[255]; |
---|
132 | FILE *f=popen(cmd.c_str(), "r"); |
---|
133 | while(fgets(buf, 254, f) != NULL) { |
---|
134 | taskDescr += string(buf); |
---|
135 | } |
---|
136 | |
---|
137 | // display the result in a nice dialog |
---|
138 | RGGladeUserDialog dia(me, "task_descr"); |
---|
139 | |
---|
140 | //TRANSLATORS: Title of the task window - %s is the task (e.g. "desktop" or "mail server") |
---|
141 | gchar *title = g_strdup_printf(_("Description %s"), str); |
---|
142 | dia.setTitle(title); |
---|
143 | |
---|
144 | GtkWidget *tv = glade_xml_get_widget(dia.getGladeXML(), |
---|
145 | "textview"); |
---|
146 | GtkTextBuffer *tb = gtk_text_view_get_buffer(GTK_TEXT_VIEW(tv)); |
---|
147 | gtk_text_buffer_set_text(tb, utf8(taskDescr.c_str()), -1); |
---|
148 | |
---|
149 | dia.run(); |
---|
150 | |
---|
151 | g_free(str); |
---|
152 | g_free(title); |
---|
153 | me->setBusyCursor(false); |
---|
154 | } |
---|
155 | |
---|
156 | |
---|
157 | void RGTasksWin::cell_toggled_callback (GtkCellRendererToggle *cell, |
---|
158 | gchar *path_string, |
---|
159 | gpointer user_data) |
---|
160 | { |
---|
161 | GtkTreeIter iter; |
---|
162 | gboolean res; |
---|
163 | |
---|
164 | RGTasksWin *me = (RGTasksWin *)user_data; |
---|
165 | |
---|
166 | static int selected = 0; |
---|
167 | |
---|
168 | GtkTreeModel *model = GTK_TREE_MODEL(me->_store); |
---|
169 | GtkTreePath* path = gtk_tree_path_new_from_string(path_string); |
---|
170 | gtk_tree_model_get_iter(model, &iter, path); |
---|
171 | gtk_tree_model_get(GTK_TREE_MODEL(model), &iter, |
---|
172 | TASK_CHECKBOX_COLUMN, &res, -1); |
---|
173 | gtk_list_store_set(GTK_LIST_STORE(model), &iter, |
---|
174 | TASK_CHECKBOX_COLUMN, !res, |
---|
175 | -1); |
---|
176 | |
---|
177 | if(!res) |
---|
178 | selected++; |
---|
179 | else |
---|
180 | selected--; |
---|
181 | |
---|
182 | if(selected > 0) |
---|
183 | gtk_widget_set_sensitive(glade_xml_get_widget(me->_gladeXML,"button_ok"), |
---|
184 | true); |
---|
185 | else |
---|
186 | gtk_widget_set_sensitive(glade_xml_get_widget(me->_gladeXML,"button_ok"), |
---|
187 | false); |
---|
188 | } |
---|
189 | |
---|
190 | void RGTasksWin::selection_changed_callback(GtkTreeSelection *selection, |
---|
191 | gpointer data) |
---|
192 | { |
---|
193 | RGTasksWin *me = (RGTasksWin*)data; |
---|
194 | |
---|
195 | GtkTreeIter iter; |
---|
196 | GtkTreeModel *model; |
---|
197 | bool sensitiv = false; |
---|
198 | |
---|
199 | if (gtk_tree_selection_get_selected (selection, &model, &iter)) |
---|
200 | sensitiv = true; |
---|
201 | |
---|
202 | gtk_widget_set_sensitive(me->_detailsButton, sensitiv); |
---|
203 | } |
---|
204 | |
---|
205 | |
---|
206 | RGTasksWin::RGTasksWin(RGWindow *parent) |
---|
207 | : RGGladeWindow(parent, "tasks") |
---|
208 | { |
---|
209 | _mainWin = (RGMainWindow *)parent; |
---|
210 | _detailsButton = glade_xml_get_widget(_gladeXML, "button_details"); |
---|
211 | assert(_detailsButton); |
---|
212 | |
---|
213 | GtkListStore *store= _store = gtk_list_store_new (TASK_N_COLUMNS, |
---|
214 | G_TYPE_BOOLEAN, |
---|
215 | G_TYPE_BOOLEAN, |
---|
216 | G_TYPE_STRING, |
---|
217 | G_TYPE_STRING); |
---|
218 | |
---|
219 | // fiel in tasks |
---|
220 | char buf[255]; |
---|
221 | gchar **strArray; |
---|
222 | string cmd = _config->Find("Synaptic::taskHelperProg", "/usr/bin/tasksel") + " --list-tasks"; |
---|
223 | FILE *f = popen(cmd.c_str(),"r"); |
---|
224 | |
---|
225 | while(fgets(buf,254,f) != NULL) { |
---|
226 | bool installed = false; |
---|
227 | |
---|
228 | strArray = g_strsplit(buf, "\t", 2); |
---|
229 | g_strstrip(strArray[1]); |
---|
230 | if(strArray[0][0] == 'i') |
---|
231 | installed = true; |
---|
232 | |
---|
233 | char *name = strArray[0]; |
---|
234 | name+=2; |
---|
235 | |
---|
236 | GtkTreeIter iter; |
---|
237 | gtk_list_store_append (store, &iter); |
---|
238 | // you can't uninstall a task for now from synaptic, we make |
---|
239 | // tasks that are already installed insensitive |
---|
240 | gtk_list_store_set (store, &iter, |
---|
241 | TASK_CHECKBOX_COLUMN, installed, |
---|
242 | TASK_SENSITIVE_COLUMN, !installed, |
---|
243 | TASK_NAME_COLUMN, name, |
---|
244 | TASK_DESCR_COLUMN, strArray[1], |
---|
245 | -1); |
---|
246 | g_strfreev(strArray); |
---|
247 | } |
---|
248 | pclose(f); |
---|
249 | GtkWidget *tree; |
---|
250 | GtkTreeSelection * select; |
---|
251 | |
---|
252 | tree = _taskView = glade_xml_get_widget(_gladeXML, "treeview_tasks"); |
---|
253 | select = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree)); |
---|
254 | gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE); |
---|
255 | g_signal_connect (G_OBJECT (select), "changed", |
---|
256 | G_CALLBACK (selection_changed_callback), |
---|
257 | this); |
---|
258 | GtkCellRenderer *renderer; |
---|
259 | GtkTreeViewColumn *column; |
---|
260 | renderer = gtk_cell_renderer_toggle_new (); |
---|
261 | g_object_set(renderer, "activatable", TRUE, NULL); |
---|
262 | g_signal_connect(renderer, "toggled", |
---|
263 | (GCallback) cell_toggled_callback, this); |
---|
264 | column = gtk_tree_view_column_new_with_attributes ("Install", |
---|
265 | renderer, |
---|
266 | "active", TASK_CHECKBOX_COLUMN, |
---|
267 | "activatable", TASK_SENSITIVE_COLUMN, |
---|
268 | NULL); |
---|
269 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); |
---|
270 | renderer = gtk_cell_renderer_text_new (); |
---|
271 | #if 0 |
---|
272 | column = gtk_tree_view_column_new_with_attributes ("Taskname", |
---|
273 | renderer, |
---|
274 | "text", TASK_NAME_COLUMN, |
---|
275 | NULL); |
---|
276 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); |
---|
277 | #endif |
---|
278 | column = gtk_tree_view_column_new_with_attributes ("Description", |
---|
279 | renderer, |
---|
280 | "text", TASK_DESCR_COLUMN, |
---|
281 | NULL); |
---|
282 | gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); |
---|
283 | |
---|
284 | |
---|
285 | gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store)); |
---|
286 | |
---|
287 | glade_xml_signal_connect_data(_gladeXML, "on_button_ok_clicked", |
---|
288 | (GCallback) cbButtonOkClicked, this); |
---|
289 | glade_xml_signal_connect_data(_gladeXML, "on_button_cancel_clicked", |
---|
290 | (GCallback) cbButtonCancelClicked, this); |
---|
291 | glade_xml_signal_connect_data(_gladeXML, "on_button_details_clicked", |
---|
292 | (GCallback) cbButtonDetailsClicked, this); |
---|
293 | |
---|
294 | }; |
---|
295 | |
---|
296 | |
---|