~ubuntu-branches/ubuntu/oneiric/gnome-system-monitor/oneiric-proposed

« back to all changes in this revision

Viewing changes to src/favorites.c

  • Committer: Bazaar Package Importer
  • Author(s): Loic Minier
  • Date: 2005-08-26 18:38:24 UTC
  • Revision ID: james.westby@ubuntu.com-20050826183824-zh2978nxikpkfxyd
Tags: upstream-2.8.1
ImportĀ upstreamĀ versionĀ 2.8.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Procman - ability to show favorite processes blacklisted processes
 
2
 * For now the favorite processes will not be compiled in.
 
3
 * Copyright (C) 2001 Kevin Vandersloot
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU General Public License
 
7
 * as published by the Free Software Foundation; either version 2
 
8
 * of the License, or (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Library General Public
 
16
 * License along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 */
 
20
 
 
21
#ifdef HAVE_CONFIG_H
 
22
#  include <config.h>
 
23
#endif
 
24
 
 
25
#include "favorites.h"
 
26
#include "proctable.h"
 
27
 
 
28
static GtkWidget *blacklist_dialog = NULL;
 
29
static GtkWidget *proctree = NULL;
 
30
static gint initial_blacklist_num = 0; /* defined in order to prune off entries from config file */
 
31
 
 
32
void
 
33
add_to_blacklist (ProcData *procdata, gchar *name)
 
34
{
 
35
        gchar *process = g_strdup (name);
 
36
        procdata->blacklist = g_list_prepend (procdata->blacklist, process);
 
37
        procdata->blacklist_num++;
 
38
        
 
39
        if (blacklist_dialog) {
 
40
                GtkTreeModel *model;
 
41
                GtkTreeIter row;
 
42
                
 
43
                model = gtk_tree_view_get_model (GTK_TREE_VIEW (proctree));
 
44
                gtk_tree_store_insert (GTK_TREE_STORE (model), &row, NULL, 0);
 
45
                gtk_tree_store_set (GTK_TREE_STORE (model), &row, 0, name, -1);
 
46
        }
 
47
                
 
48
}
 
49
 
 
50
static GList *removed_processes = NULL;
 
51
 
 
52
static void
 
53
add_single_to_blacklist (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 
54
{
 
55
        ProcData *procdata = data;
 
56
        ProcInfo *info = NULL;
 
57
        
 
58
        gtk_tree_model_get (model, iter, COL_POINTER, &info, -1);
 
59
        g_return_if_fail (info);
 
60
        
 
61
        add_to_blacklist (procdata, info->name);
 
62
        
 
63
        if (info->visible) 
 
64
                removed_processes = g_list_prepend (removed_processes, info);
 
65
}
 
66
 
 
67
static void
 
68
remove_all_of_same_name_from_tree (ProcInfo *info, ProcData *procdata)
 
69
{
 
70
        GList *list = procdata->info;
 
71
        
 
72
        while (list) {
 
73
                ProcInfo *tmp = list->data;
 
74
                
 
75
                if (g_strcasecmp (info->name, tmp->name) == 0) 
 
76
                        remove_info_from_tree (tmp, procdata);
 
77
                        
 
78
                list = g_list_next (list);
 
79
        }
 
80
 
 
81
}
 
82
 
 
83
void
 
84
add_selected_to_blacklist (ProcData *procdata)
 
85
{
 
86
        if (!procdata->selection)
 
87
                return;
 
88
                
 
89
        gtk_tree_selection_selected_foreach (procdata->selection, 
 
90
                                             add_single_to_blacklist, procdata);
 
91
                                             
 
92
        while (removed_processes) {
 
93
                ProcInfo *info = removed_processes->data;
 
94
                remove_all_of_same_name_from_tree (info, procdata);
 
95
                
 
96
                removed_processes = g_list_next (removed_processes);
 
97
        }
 
98
}
 
99
 
 
100
void
 
101
remove_from_blacklist (ProcData *procdata, gchar *name)
 
102
{
 
103
        GList *list = procdata->blacklist;
 
104
        
 
105
        while (list) {
 
106
                if (!g_strcasecmp (list->data, name)) {
 
107
                        procdata->blacklist = g_list_remove (procdata->blacklist, list->data);
 
108
                        procdata->blacklist_num --;
 
109
                        return;
 
110
                }
 
111
                
 
112
                list = g_list_next (list);
 
113
        }
 
114
        
 
115
}
 
116
 
 
117
gboolean
 
118
is_process_blacklisted (ProcData *procdata, gchar *name)
 
119
{
 
120
        GList *list = procdata->blacklist;
 
121
        
 
122
        if (!list)
 
123
        {
 
124
                return FALSE;
 
125
        }
 
126
        
 
127
        while (list)
 
128
        {
 
129
                gchar *process = list->data;
 
130
                if (!g_strcasecmp (process, name))
 
131
                        return TRUE;
 
132
                
 
133
                list = g_list_next (list);
 
134
        }
 
135
        
 
136
        return FALSE;
 
137
 
 
138
}
 
139
 
 
140
void 
 
141
save_blacklist (ProcData *procdata, GConfClient *client)
 
142
{
 
143
 
 
144
        GList *list = procdata->blacklist;
 
145
        gint i = 0;
 
146
        
 
147
        while (list)
 
148
        {
 
149
                gchar *name = list->data;
 
150
                gchar *config = g_strdup_printf ("%s%d", "/apps/procman/process", i);
 
151
                gconf_client_set_string (client, config, name, NULL);
 
152
                g_free (config); 
 
153
                i++;
 
154
                
 
155
                list = g_list_next (list);
 
156
        }
 
157
        
 
158
        for (i = initial_blacklist_num; i >= procdata->blacklist_num; i--)
 
159
        {
 
160
                gchar *config = g_strdup_printf ("%s%d", "/apps/procman/process", i);
 
161
                gconf_client_unset (client, config, NULL);
 
162
                g_free (config);
 
163
        } 
 
164
 
 
165
}
 
166
 
 
167
void get_blacklist (ProcData *procdata, GConfClient *client)
 
168
{
 
169
        gint i = 0;
 
170
        gboolean done = FALSE;
 
171
        
 
172
        while (!done)
 
173
        {
 
174
                gchar *config = g_strdup_printf ("%s%d", "/apps/procman/process", i);
 
175
                gchar *process;
 
176
                
 
177
                process = gconf_client_get_string (client, config, NULL);
 
178
                g_free (config);
 
179
                if (process)
 
180
                {
 
181
                        add_to_blacklist (procdata, process);
 
182
                        g_free (process);
 
183
                }
 
184
                else
 
185
                        done = TRUE;
 
186
                i++;
 
187
        }
 
188
        
 
189
        procdata->blacklist_num = i - 1;
 
190
        initial_blacklist_num = i - 1;
 
191
 
 
192
}
 
193
 
 
194
static void 
 
195
fill_tree_with_info (ProcData *procdata, GtkWidget *tree)
 
196
{
 
197
        GList *blacklist = procdata->blacklist;
 
198
        GtkTreeModel *model;
 
199
        GtkTreeIter row;
 
200
        
 
201
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree));
 
202
        
 
203
        /* add the blacklist */
 
204
        while (blacklist)
 
205
        {
 
206
                gtk_tree_store_insert (GTK_TREE_STORE (model), &row, NULL, 0);
 
207
                gtk_tree_store_set (GTK_TREE_STORE (model), &row, 0, blacklist->data, -1);
 
208
                blacklist = g_list_next (blacklist);
 
209
        }
 
210
}
 
211
 
 
212
 
 
213
static GtkWidget *
 
214
create_tree (ProcData *procdata)
 
215
{
 
216
        GtkWidget *scrolled;
 
217
        GtkWidget *tree;
 
218
        GtkTreeStore *model;
 
219
        GtkTreeViewColumn *column;
 
220
        GtkTreeSelection *selection;
 
221
        GtkCellRenderer *cell_renderer;
 
222
                
 
223
        scrolled = gtk_scrolled_window_new (NULL, NULL);
 
224
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
 
225
                                        GTK_POLICY_AUTOMATIC,
 
226
                                        GTK_POLICY_AUTOMATIC);
 
227
        
 
228
        model = gtk_tree_store_new (1, G_TYPE_STRING);
 
229
        
 
230
        tree = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));
 
231
        g_object_unref (G_OBJECT (model));
 
232
        
 
233
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (tree));
 
234
        gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
 
235
        
 
236
        cell_renderer = gtk_cell_renderer_text_new ();
 
237
        column = gtk_tree_view_column_new_with_attributes ("hello",
 
238
                                                           cell_renderer,
 
239
                                                           "text", 0,
 
240
                                                           NULL);
 
241
        gtk_tree_view_column_set_sort_column_id (column, 0);
 
242
                                                   
 
243
        gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column);
 
244
        
 
245
        gtk_container_add (GTK_CONTAINER (scrolled), tree);
 
246
        
 
247
        gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
 
248
                                              0,
 
249
                                              GTK_SORT_ASCENDING);
 
250
        
 
251
        gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (tree), FALSE);
 
252
                
 
253
        fill_tree_with_info (procdata, tree);
 
254
 
 
255
        proctree = tree;
 
256
        
 
257
        return scrolled;
 
258
 
 
259
}
 
260
 
 
261
static GList *removed_iters = NULL;
 
262
 
 
263
static void
 
264
insert_all_of_same_name_from_tree (gchar *name, ProcData *procdata)
 
265
{
 
266
        GList *list = procdata->info;
 
267
        
 
268
        while (list) {
 
269
                ProcInfo *tmp = list->data;
 
270
                
 
271
                if (g_strcasecmp (name, tmp->name) == 0) 
 
272
                        insert_info_to_tree (tmp, procdata);
 
273
                        
 
274
                list = g_list_next (list);
 
275
        }
 
276
 
 
277
}
 
278
 
 
279
static void
 
280
remove_item (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, gpointer data)
 
281
{
 
282
        ProcData *procdata = data;
 
283
        GtkTreeIter *iter_copy;
 
284
        gchar *process = NULL;
 
285
        
 
286
        gtk_tree_model_get (model, iter, 0, &process, -1);
 
287
        
 
288
        if (process) {
 
289
                remove_from_blacklist (procdata, process);
 
290
                insert_all_of_same_name_from_tree (process, procdata);                  
 
291
        }
 
292
                
 
293
        iter_copy = gtk_tree_iter_copy (iter);  
 
294
        removed_iters = g_list_prepend (removed_iters, iter_copy);
 
295
}
 
296
 
 
297
static void
 
298
remove_button_clicked (GtkButton *button, gpointer data)
 
299
{
 
300
        ProcData *procdata = data;
 
301
        GtkTreeModel *model;
 
302
        GtkTreeSelection *selection = NULL;
 
303
        
 
304
        model = gtk_tree_view_get_model (GTK_TREE_VIEW (proctree));
 
305
        selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (proctree));
 
306
        
 
307
        gtk_tree_selection_selected_foreach (selection, remove_item, procdata); 
 
308
        
 
309
        while (removed_iters) {
 
310
                GtkTreeIter *iter = removed_iters->data;
 
311
                
 
312
                gtk_tree_store_remove (GTK_TREE_STORE (model), iter);
 
313
                gtk_tree_iter_free (iter);
 
314
                
 
315
                removed_iters = g_list_next (removed_iters);
 
316
        }
 
317
        
 
318
}
 
319
 
 
320
static void
 
321
close_blacklist_dialog (GtkDialog *dialog, gint id, gpointer data)
 
322
{
 
323
        gtk_widget_destroy (blacklist_dialog);
 
324
        
 
325
        blacklist_dialog = NULL;
 
326
}
 
327
 
 
328
void create_blacklist_dialog (ProcData *procdata)
 
329
{
 
330
        GtkWidget *main_vbox, *vbox;
 
331
        GtkWidget *inner_vbox;
 
332
        GtkWidget *hbox;
 
333
        GtkWidget *button;
 
334
        GtkWidget *scrolled;
 
335
        GtkWidget *label;
 
336
        GtkWidget *dialog;
 
337
        GtkWidget *align;
 
338
        GtkWidget *icon;
 
339
        gchar *message;
 
340
        
 
341
 
 
342
        if (procdata->blacklist_num == 0 )
 
343
        {
 
344
                message = g_strdup_printf(_("No processes are currently hidden."));
 
345
                dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
 
346
                                                 GTK_MESSAGE_INFO, GTK_BUTTONS_OK,
 
347
                                                 "%s", message); 
 
348
                gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
 
349
                gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
 
350
                gtk_dialog_run (GTK_DIALOG (dialog));
 
351
                gtk_widget_destroy (dialog);
 
352
                g_free (message);
 
353
        }
 
354
        
 
355
        else
 
356
        {
 
357
 
 
358
                if (blacklist_dialog)
 
359
                {
 
360
                        gdk_window_raise(blacklist_dialog->window);
 
361
                        return;
 
362
                }
 
363
 
 
364
                blacklist_dialog = gtk_dialog_new_with_buttons (_("Manage Hidden Processes"), 
 
365
                                                                NULL,
 
366
                                                                GTK_DIALOG_DESTROY_WITH_PARENT,
 
367
                                                                GTK_STOCK_CLOSE, 
 
368
                                                                GTK_RESPONSE_CLOSE,
 
369
                                                                NULL);
 
370
                gtk_window_set_resizable (GTK_WINDOW (blacklist_dialog), TRUE);
 
371
                gtk_window_set_default_size (GTK_WINDOW (blacklist_dialog), 320, 375);
 
372
                gtk_container_set_border_width (GTK_CONTAINER (blacklist_dialog), 5);
 
373
                gtk_dialog_set_has_separator (GTK_DIALOG (blacklist_dialog), FALSE);
 
374
                
 
375
                vbox = GTK_DIALOG (blacklist_dialog)->vbox;
 
376
                gtk_box_set_spacing (GTK_BOX (vbox), 2);
 
377
                
 
378
                main_vbox = gtk_vbox_new (FALSE, 12);
 
379
                gtk_box_pack_start (GTK_BOX (vbox), main_vbox, TRUE, TRUE, 0);
 
380
                gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 5);
 
381
                
 
382
                inner_vbox = gtk_vbox_new (FALSE, 6);
 
383
                gtk_box_pack_start (GTK_BOX (main_vbox), inner_vbox, TRUE, TRUE, 0);
 
384
                
 
385
                hbox = gtk_hbox_new (FALSE, 0);
 
386
                gtk_box_pack_start (GTK_BOX (inner_vbox), hbox, FALSE, FALSE, 0);
 
387
                
 
388
                label = gtk_label_new_with_mnemonic (_("_Hidden processes:"));
 
389
                gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
390
        
 
391
                scrolled = create_tree (procdata);
 
392
                gtk_box_pack_start (GTK_BOX (inner_vbox), scrolled, TRUE, TRUE, 0);
 
393
                gtk_label_set_mnemonic_widget (GTK_LABEL (label), proctree);
 
394
        
 
395
                hbox = gtk_hbox_new (FALSE, 0);
 
396
                gtk_box_pack_end (GTK_BOX (inner_vbox), hbox, FALSE, FALSE, 0);
 
397
        
 
398
                button = gtk_button_new ();
 
399
                gtk_box_pack_end (GTK_BOX (hbox), button, FALSE, FALSE, 0);
 
400
                
 
401
                align = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
 
402
                gtk_container_add (GTK_CONTAINER (button), align);
 
403
                
 
404
                hbox = gtk_hbox_new (FALSE, 2);
 
405
                gtk_container_add (GTK_CONTAINER (align), hbox);
 
406
 
 
407
                icon = gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_BUTTON);
 
408
                gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
 
409
 
 
410
                label = gtk_label_new_with_mnemonic (_("_Remove From List"));
 
411
                gtk_label_set_mnemonic_widget (GTK_LABEL (label), button);
 
412
                gtk_box_pack_end (GTK_BOX (hbox), label, FALSE, FALSE, 0);
 
413
        
 
414
                g_signal_connect (G_OBJECT (button), "clicked",
 
415
                                  G_CALLBACK (remove_button_clicked), procdata);
 
416
                g_signal_connect (G_OBJECT (blacklist_dialog), "response",
 
417
                                  G_CALLBACK (close_blacklist_dialog), procdata);
 
418
  
 
419
                message = g_strconcat("<small><i><b>", _("Note:"), "</b> ", 
 
420
                    _("These are the processes you have chosen to hide. You can reshow a process by removing it from this list."),
 
421
                    "</i></small>", NULL); 
 
422
                label = gtk_label_new (_(message));
 
423
                g_free (message);
 
424
                
 
425
                gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
 
426
                gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
 
427
                gtk_box_pack_start (GTK_BOX (main_vbox), label, FALSE, FALSE, 0);
 
428
        
 
429
                gtk_widget_show_all (blacklist_dialog);
 
430
        }
 
431
        
 
432
}