1 /*
2  * gtkpasman
3  * Copyright (C) 2013 David Guglielmi
4  *
5  * gtk2ui.c
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
21 #include <gtk/gtk.h>
22 #include <gdk/gdkkeysyms.h>
23 #include <stdlib.h>
24 #include <gtkui.h>
25 
26 #ifdef HAVE_CONFIG_H
27   #include <config.h>
28 #endif
29 
30 /* FIXME: No include this */
31 #include "tree.h"
32 #include "passwd.h"
33 #include "gtkpasman_config.h"
34 
35 #ifdef DISABLE_GSETTINGS
36   #include "configread.h"
37 #else
38   #include "settings.h"
39 #endif
40 
41 #ifndef DISABLE_NLS
42   #include <glib/gi18n-lib.h>
43 #else
44   #define _(x) x
45 #endif
46 
47 /* Local used function */
48 GtkWidget* create_root_window (void);
49 GtkWidget* pasman_askpassword_widget_new (void);
50 
51 static void window_state_change (GtkWidget *, GdkEventWindowState *, gpointer);
52 static void select_file (void);
53 static void show_about_dialog (void);
54 static void pasman_window_swap_state (GtkWindow *);
55 static void create_gtkpasman_config (void);
56 static void change_passwd_entry_visibility (GtkToggleButton*, gpointer);
57 static void change_pass (GtkWidget *passtxt);
58 static void change_config (GtkWidget *dialog);
59 static void show_askpassword (void);
60 
61 /* FIXME: do not use global variable */
62 /* widgets to be used in several places */
63 
64 /* Setup dialog entry widget */
65 GtkWidget *passwd_file_entry;
66 GtkWidget *ssh_cmd_entry;
67 GtkWidget *ftp_cmd_entry;
68 GtkWidget *telnet_cmd_entry;
69 GtkWidget *http_cmd_entry;
70 GtkWidget *cssh_cmd_entry;
71 
72 void
init_gtk_ui(void)73 init_gtk_ui (void)
74 {
75 
76   int argc = 1;
77   const char* args[] = {"", "\0"};
78   char** argv = (char**)&args;
79 
80 #ifndef DISABLE_NLS
81   setlocale (LC_ALL, "");
82   bindtextdomain (PACKAGE, LOCALEDIR);
83   textdomain (PACKAGE);
84 #else
85   g_setenv ("LC_ALL", "C", TRUE);
86 #endif
87 
88   gtk_init (&argc, &argv);
89 
90 }
91 
92 void
start_gtk_ui(int initial_password)93 start_gtk_ui (int initial_password)
94 {
95 
96   GtkWidget *window;
97   GtkWidget *prompt_passwd;
98 
99   window = create_root_window ();
100 
101   /* Don't display passphrase dialog */
102   if (initial_password == 0)
103    {
104     prompt_passwd = g_object_get_data (G_OBJECT (window),\
105                       "pasman_prompt_passwd");
106     gtk_widget_hide (prompt_passwd);
107    }
108 
109   gtk_main ();
110 }
111 
112 /*
113  * create_root_window
114  *      create the list window and all the widgets
115  */
116 
117 GtkWidget*
create_root_window()118 create_root_window ()
119 {
120 
121   GtkWidget *window;
122   GtkWidget *vbox;
123   GtkWidget *hbox;
124   GtkWidget *button;
125   GtkWidget *menu;
126   GtkWidget *menu_bar;
127   GtkWidget *menu_item;
128   GtkWidget *tree_window;
129   GtkWidget *tree_view;
130   GtkWidget *label;
131   GtkWidget *entry;
132   GtkWidget *toolbar;
133   GtkWidget *clipboard;
134   GtkWidget *prompt_passwd;
135   GtkWidget *statusbar;
136   GtkToolItem *tool_item;
137   GtkStatusIcon *tray_icon;
138   GtkAccelGroup *accel_group;
139   GdkPixbuf *pixbuf;
140 
141   /* main window */
142   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
143   gtk_window_set_title (GTK_WINDOW (window), "GtkPasman");
144   gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
145 
146   gtk_window_set_default_size (GTK_WINDOW (window), -1, 350);
147 
148   pixbuf = gtk_widget_render_icon (window,\
149                                   GTK_STOCK_DIALOG_AUTHENTICATION,\
150                                   GTK_ICON_SIZE_BUTTON,\
151                                   NULL);
152 
153   gtk_window_set_default_icon (pixbuf);
154   g_object_unref (pixbuf);
155 
156   accel_group = gtk_accel_group_new ();
157   gtk_window_add_accel_group (GTK_WINDOW(window), accel_group);
158 
159   g_signal_connect_swapped (G_OBJECT (window), "destroy",
160 		      G_CALLBACK (gtk_main_quit), NULL);
161 
162   g_signal_connect (G_OBJECT (window), "window-state-event",\
163     G_CALLBACK (window_state_change), NULL);
164 
165   vbox = gtk_vbox_new (FALSE, 0);
166   gtk_container_add (GTK_CONTAINER (window), vbox);
167 
168   tree_view = pasman_tree_view_new ();
169 
170   /* menus */
171   menu_bar = gtk_menu_bar_new ();
172 
173     menu = gtk_menu_new ();
174 
175     menu_item = gtk_menu_item_new_with_mnemonic (_("_Open file"));
176     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
177     g_signal_connect (G_OBJECT (menu_item), "activate",\
178       G_CALLBACK (select_file), NULL);
179 
180     menu_item = gtk_image_menu_item_new_from_stock (\
181       GTK_STOCK_PREFERENCES, NULL);
182     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
183     g_signal_connect (G_OBJECT (menu_item), "activate",\
184       G_CALLBACK (create_gtkpasman_config), NULL);
185 
186     menu_item = gtk_separator_menu_item_new ();
187     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
188 
189     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_QUIT, NULL);
190     g_signal_connect (G_OBJECT (menu_item), "activate",\
191       G_CALLBACK (gtk_main_quit), NULL);
192     gtk_widget_add_accelerator (menu_item, "activate", accel_group,\
193       GDK_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
194     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
195 
196   menu_item = gtk_menu_item_new_with_mnemonic (_("_File"));
197   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
198   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), menu_item);
199 
200     menu = gtk_menu_new ();
201 
202     menu_item = gtk_menu_item_new_with_label (_("Copy command line"));
203     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
204 		  G_CALLBACK (button_copy_cmd_line), tree_view);
205     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
206 
207     menu_item = gtk_menu_item_new_with_label (_("Copy password"));
208     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
209 		  G_CALLBACK (button_copy_passwd), tree_view);
210     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
211 
212     menu_item = gtk_menu_item_new_with_label (_("Copy password & Execute"));
213     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
214 		  G_CALLBACK (button_copy_passwd_and_execute), tree_view);
215     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
216 
217     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_EXECUTE, NULL);
218     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
219 		  G_CALLBACK (button_execute), tree_view);
220     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
221 
222     menu_item = gtk_separator_menu_item_new ();
223     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
224 
225     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_GO_FORWARD, NULL);
226     g_signal_connect (G_OBJECT (menu_item), "activate",\
227       G_CALLBACK (find_next_in_tree_view), tree_view);
228     gtk_widget_add_accelerator (menu_item, "activate", accel_group,\
229       GDK_n, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
230     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
231 
232     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_GO_BACK, NULL);
233     g_signal_connect (G_OBJECT (menu_item), "activate",\
234       G_CALLBACK (find_prev_in_tree_view), tree_view);
235     gtk_widget_add_accelerator (menu_item, "activate", accel_group,\
236       GDK_p, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
237     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
238 
239   menu_item = gtk_menu_item_new_with_mnemonic (_("_Action"));
240   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
241   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), menu_item);
242 
243     menu = gtk_menu_new ();
244 
245     menu_item = gtk_menu_item_new_with_label (_("Collapse all"));
246     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
247     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
248       G_CALLBACK (gtk_tree_view_collapse_all), tree_view);
249 
250     menu_item = gtk_menu_item_new_with_label (_("Expand a little"));
251     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
252     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
253       G_CALLBACK (tree_expand_little), tree_view);
254 
255     menu_item = gtk_menu_item_new_with_label (_("Expand all"));
256     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
257     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
258       G_CALLBACK (gtk_tree_view_expand_all), tree_view);
259 
260     menu_item = gtk_menu_item_new_with_label (_("Unselect all"));
261     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
262     g_signal_connect_swapped (G_OBJECT (menu_item), "activate",\
263       G_CALLBACK (tree_unselectall), tree_view);
264 
265     menu_item = gtk_separator_menu_item_new ();
266     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
267 
268     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_REFRESH, NULL);
269     g_signal_connect (G_OBJECT (menu_item), "activate",\
270       G_CALLBACK (show_askpassword), NULL);
271     gtk_widget_add_accelerator (menu_item, "activate", accel_group,\
272       GDK_r, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);
273     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
274 
275   menu_item = gtk_menu_item_new_with_mnemonic (_("_View"));
276   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
277   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), menu_item);
278 
279     menu = gtk_menu_new ();
280     menu_item = gtk_image_menu_item_new_from_stock (GTK_STOCK_ABOUT, NULL);
281     g_signal_connect (G_OBJECT (menu_item), "activate",\
282       G_CALLBACK (show_about_dialog), NULL);
283     gtk_menu_shell_append (GTK_MENU_SHELL (menu), menu_item);
284 
285   menu_item = gtk_menu_item_new_with_mnemonic (_("_Help"));
286   gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
287   gtk_menu_shell_append (GTK_MENU_SHELL (menu_bar), menu_item);
288 
289   gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 0);
290 
291   prompt_passwd = pasman_askpassword_widget_new ();
292 
293   gtk_box_pack_start (GTK_BOX (vbox), prompt_passwd,\
294     FALSE, FALSE, 0);
295 
296   toolbar = gtk_toolbar_new ();
297 
298   gtk_toolbar_set_style (GTK_TOOLBAR (toolbar), GTK_TOOLBAR_BOTH);
299   gtk_toolbar_set_show_arrow (GTK_TOOLBAR (toolbar), FALSE);
300 
301   tool_item = gtk_tool_button_new_from_stock (GTK_STOCK_COPY);
302   gtk_tool_button_set_label (GTK_TOOL_BUTTON (tool_item), _("Copy cmd line"));
303   gtk_tool_item_set_tooltip_text (tool_item, _("Copy command line."));
304   g_signal_connect_swapped (G_OBJECT (tool_item), "clicked",\
305     G_CALLBACK (button_copy_cmd_line), tree_view);
306   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), tool_item, 0);
307 
308   tool_item = gtk_tool_button_new_from_stock (GTK_STOCK_COPY);
309   gtk_tool_button_set_label  (GTK_TOOL_BUTTON (tool_item), _("Copy passwd"));
310   gtk_tool_item_set_tooltip_text (tool_item, _("Copy password."));
311   g_signal_connect_swapped (G_OBJECT (tool_item), "clicked",\
312     G_CALLBACK (button_copy_passwd), tree_view);
313   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), tool_item, -1);
314 
315   tool_item = gtk_tool_button_new_from_stock (GTK_STOCK_COPY);
316   gtk_tool_button_set_label  (GTK_TOOL_BUTTON (tool_item),\
317     _("Copy & Execute"));
318   gtk_tool_item_set_tooltip_text (tool_item,\
319     _("Copy password and execute command line."));
320   g_signal_connect_swapped (G_OBJECT (tool_item), "clicked",\
321     G_CALLBACK (button_copy_passwd_and_execute), tree_view);
322   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), tool_item, -1);
323 
324   tool_item = gtk_tool_button_new_from_stock (GTK_STOCK_EXECUTE);
325   gtk_tool_item_set_tooltip_text (tool_item, _("Execute command line."));
326   g_signal_connect_swapped (G_OBJECT (tool_item), "clicked",\
327     G_CALLBACK (button_execute), tree_view);
328   gtk_toolbar_insert (GTK_TOOLBAR (toolbar), tool_item, -1);
329 
330   gtk_box_pack_start (GTK_BOX (vbox), toolbar, FALSE, FALSE, 0);
331 
332   /* scrollwindow */
333   tree_window = gtk_scrolled_window_new (NULL, NULL);
334 
335   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (tree_window),\
336     GTK_POLICY_NEVER,\
337     GTK_POLICY_AUTOMATIC);
338 
339   gtk_container_add (GTK_CONTAINER (tree_window), tree_view);
340   gtk_box_pack_start (GTK_BOX (vbox), tree_window, TRUE, TRUE, 0);
341 
342   /* search entry */
343   hbox = gtk_hbox_new (FALSE, 2);
344   gtk_container_set_border_width (GTK_CONTAINER (hbox), 2);
345 
346   label = gtk_label_new (_("Search : "));
347   gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
348   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
349 
350   entry = gtk_entry_new ();
351   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
352   gtk_entry_set_text (GTK_ENTRY (entry), _("enter your search here"));
353   g_signal_connect_swapped (G_OBJECT (entry), "changed",\
354 			     G_CALLBACK (search_in_list), entry);
355   g_signal_connect_swapped (G_OBJECT (entry), "activate",\
356 			     G_CALLBACK (search_in_list), entry);
357 
358   button = gtk_button_new ();
359   gtk_button_set_image (GTK_BUTTON (button),\
360     gtk_image_new_from_stock (GTK_STOCK_GO_FORWARD, GTK_ICON_SIZE_BUTTON));
361   g_signal_connect (G_OBJECT (button), "clicked",\
362 		      G_CALLBACK (find_next_in_tree_view), tree_view);
363   gtk_widget_set_tooltip_text (button, "Find forward.");
364   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
365 
366   button = gtk_button_new ();
367   gtk_button_set_image (GTK_BUTTON (button),\
368     gtk_image_new_from_stock (GTK_STOCK_GO_BACK, GTK_ICON_SIZE_BUTTON));
369   gtk_widget_set_tooltip_text (button, "Find back.");
370   g_signal_connect (G_OBJECT (button), "clicked",\
371 		      G_CALLBACK (find_prev_in_tree_view), tree_view);
372   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
373 
374   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
375 
376   statusbar = gtk_statusbar_new ();
377 
378   gtk_box_pack_start (GTK_BOX (vbox), statusbar, FALSE, FALSE, 0);
379 
380   /* show all */
381   gtk_widget_show_all (window);
382 
383   /* then add hidden entry used to have paste selections */
384   clipboard = gtk_entry_new ();
385   gtk_box_pack_start (GTK_BOX (vbox), clipboard, FALSE, TRUE, 0);
386   gtk_widget_show (clipboard);
387   gtk_widget_hide (clipboard);
388 
389   /* Gtk tray icon: the bcm request */
390   tray_icon = gtk_status_icon_new ();
391 
392   gtk_status_icon_set_from_icon_name (tray_icon,\
393     GTK_STOCK_DIALOG_AUTHENTICATION);
394   gtk_status_icon_set_tooltip_text (tray_icon, "GtkPasman");
395   gtk_status_icon_set_visible (tray_icon, TRUE);
396 
397   g_signal_connect_swapped (G_OBJECT(tray_icon), "activate",
398     G_CALLBACK (pasman_window_swap_state), window);
399 
400 /*
401   Set Global Widget
402 */
403   g_object_set_data (G_OBJECT (window), "pasman_search_entry", entry);
404   g_object_set_data (G_OBJECT (window), "pasman_clipboard", clipboard);
405   g_object_set_data (G_OBJECT (window), "pasman_prompt_passwd", prompt_passwd);
406   g_object_set_data (G_OBJECT (window), "pasman_tree_view", tree_view);
407 
408   return window;
409 
410 }
411 
412 /*
413  * About dialog.
414  *
415  */
416 static void
show_about_dialog(void)417 show_about_dialog (void)
418 {
419   GtkWidget *dialog;
420   GdkPixbuf *pixbuf;
421 
422   const gchar *authors [] = { "Cyril Bellot <jcpp@users.sourceforge.net>",\
423                         "David Guglielmi <dg@accelance.fr>",\
424                         NULL};
425 
426   dialog = gtk_about_dialog_new ();
427 
428   pixbuf = gtk_widget_render_icon (dialog,\
429                                   GTK_STOCK_DIALOG_AUTHENTICATION,\
430                                   GTK_ICON_SIZE_DIALOG,\
431                                   NULL);
432 
433   gtk_about_dialog_set_logo (GTK_ABOUT_DIALOG (dialog), pixbuf);
434 
435   g_object_unref (pixbuf);
436 
437   gtk_about_dialog_set_logo (GTK_ABOUT_DIALOG (dialog), pixbuf);
438 
439   gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (dialog), authors);
440 
441 #if (defined(PACKAGE_NAME) && defined(PACKAGE_VERSION))
442   gtk_about_dialog_set_program_name (GTK_ABOUT_DIALOG (dialog), PACKAGE_NAME);
443   gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (dialog), PACKAGE_VERSION);
444 #else
445   #error Unable to find PACKAGE_NAME or/and PACKAGE_VERSION, please define it !
446 #endif
447 
448   gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (dialog),\
449     "http://gtkpasman.sourceforge.net/");
450 
451   gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG (dialog),\
452     (_("Lightweight password manager based on GPG and GTK+.")));
453 
454   gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (dialog),\
455     ("Copyright \xc2\xa9 2013 Open Wide Outsourcing (ex .Accelance MSP)"));
456 
457   gtk_window_set_transient_for (GTK_WINDOW (dialog),\
458     GTK_WINDOW (gtk_window_list_toplevels ()->data));
459 
460   gtk_dialog_run (GTK_DIALOG (dialog));
461   gtk_widget_destroy (dialog);
462 
463 }
464 
465 /*
466  * create_gtkpasman_config
467  *      create the configuration dialog
468  */
469 void
create_gtkpasman_config()470 create_gtkpasman_config ()
471 {
472   GtkWidget *dialog;
473   GtkWidget *frame;
474   GtkWidget *label;
475   GtkWidget *hbox;
476   GtkWidget *button;
477   GtkWidget *content_area;
478   GtkWidget *table;
479   GtkWidget *alignment;
480 
481   /* main window (modal) */
482   dialog = gtk_dialog_new_with_buttons (_("Setup"),\
483     GTK_WINDOW (gtk_window_list_toplevels ()->data),\
484     GTK_DIALOG_MODAL,\
485     GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,\
486     GTK_STOCK_OK, GTK_RESPONSE_OK,\
487     NULL);
488   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
489   gtk_window_set_default_size (GTK_WINDOW (dialog), 320, -1);
490 
491   content_area  = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
492 
493   /* password file */
494   frame = gtk_frame_new (_("Password file database encrypted whith GnuPG:"));
495   gtk_box_pack_start (GTK_BOX (content_area),
496 		      frame, FALSE, FALSE, 2);
497 
498   hbox = gtk_hbox_new (FALSE, 0);
499   alignment = gtk_alignment_new (0, 0, 1, 1);
500   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 4, 4, 4, 4);
501 
502   gtk_container_add (GTK_CONTAINER (alignment), hbox);
503   gtk_container_add (GTK_CONTAINER (frame), alignment);
504 
505   label = gtk_label_new (_("Default file"));
506   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 2);
507 
508   passwd_file_entry = gtk_entry_new ();
509   gtk_entry_set_text (GTK_ENTRY (passwd_file_entry), passwdfile);
510   gtk_box_pack_start (GTK_BOX (hbox), passwd_file_entry, TRUE, TRUE, 4);
511 
512   button = gtk_button_new ();
513   gtk_button_set_image (GTK_BUTTON (button),\
514     gtk_image_new_from_stock (GTK_STOCK_OPEN, GTK_ICON_SIZE_BUTTON));
515   g_signal_connect_swapped (G_OBJECT (button), "clicked",\
516     G_CALLBACK (select_file), NULL);
517 
518   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 2);
519 
520   /* Command lines */
521   frame = gtk_frame_new (_("External commands to launch:"));
522   gtk_box_pack_start (GTK_BOX (content_area),
523 		      frame, FALSE, FALSE, 2);
524 
525   table = gtk_table_new (5, 2, FALSE);
526   gtk_table_set_col_spacings (GTK_TABLE (table), 6);
527 
528   alignment = gtk_alignment_new (0, 0, 1, 1);
529   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment), 8, 8, 4, 4);
530 
531   gtk_container_add (GTK_CONTAINER (alignment), table);
532   gtk_container_add (GTK_CONTAINER (frame), alignment);
533 
534   label = gtk_label_new (_("SSH command"));
535   alignment = gtk_alignment_new (1, 0.5, 0, 0);
536   gtk_container_add (GTK_CONTAINER (alignment), label);
537 
538     ssh_cmd_entry = gtk_entry_new ();
539     gtk_entry_set_text (GTK_ENTRY (ssh_cmd_entry), sshcmd);
540   gtk_table_attach (GTK_TABLE (table), alignment, 0, 1, 0, 1, \
541                     GTK_FILL, GTK_FILL, 0, 0);
542   gtk_table_attach_defaults (GTK_TABLE (table), ssh_cmd_entry, 1, 2, 0, 1);
543 
544   label = gtk_label_new (_("CSSH command"));
545   alignment = gtk_alignment_new (1, 0.5, 0, 0);
546   gtk_container_add (GTK_CONTAINER (alignment), label);
547 
548     cssh_cmd_entry = gtk_entry_new ();
549     gtk_entry_set_text (GTK_ENTRY (cssh_cmd_entry), csshcmd);
550   gtk_table_attach (GTK_TABLE (table), alignment, 0, 1, 1, 2, \
551                     GTK_FILL, GTK_FILL, 0, 0);
552   gtk_table_attach_defaults (GTK_TABLE (table), cssh_cmd_entry, 1, 2, 1, 2);
553 
554   label = gtk_label_new (_("FTP command"));
555   alignment = gtk_alignment_new (1, 0.5, 0, 0);
556   gtk_container_add (GTK_CONTAINER (alignment), label);
557 
558     ftp_cmd_entry = gtk_entry_new ();
559     gtk_entry_set_text (GTK_ENTRY (ftp_cmd_entry), ftpcmd);
560   gtk_table_attach (GTK_TABLE (table), alignment, 0, 1, 2, 3, \
561                     GTK_FILL, GTK_FILL, 0, 0);
562   gtk_table_attach_defaults (GTK_TABLE (table), ftp_cmd_entry, 1, 2, 2, 3);
563 
564   label = gtk_label_new (_("TELNET command"));
565   alignment = gtk_alignment_new (1, 0.5, 0, 0);
566   gtk_container_add (GTK_CONTAINER (alignment), label);
567 
568     telnet_cmd_entry = gtk_entry_new ();
569     gtk_entry_set_text (GTK_ENTRY (telnet_cmd_entry), telnetcmd);
570   gtk_table_attach (GTK_TABLE (table), alignment, 0, 1, 3, 4, \
571                     GTK_FILL, GTK_FILL, 0, 0);
572   gtk_table_attach_defaults (GTK_TABLE (table), telnet_cmd_entry, 1, 2, 3, 4);
573 
574   label = gtk_label_new (_("HTTP command"));
575   alignment = gtk_alignment_new (1, 0.5, 0, 0);
576   gtk_container_add (GTK_CONTAINER (alignment), label);
577 
578     http_cmd_entry = gtk_entry_new ();
579     gtk_entry_set_text (GTK_ENTRY (http_cmd_entry), httpcmd);
580   gtk_table_attach (GTK_TABLE (table), alignment, 0, 1, 4, 5, \
581                     GTK_FILL, GTK_FILL, 0, 0);
582   gtk_table_attach_defaults (GTK_TABLE (table), http_cmd_entry, 1, 2, 4, 5);
583 
584   gtk_widget_show_all (dialog);
585 
586   switch (gtk_dialog_run (GTK_DIALOG (dialog)))
587    {
588 
589      case GTK_RESPONSE_OK:
590        change_config (dialog);
591        break;
592        ;;
593 
594      default:
595        gtk_widget_destroy (dialog);
596    }
597 
598 }
599 
600 /*
601  * select_file
602  *      select a password file
603  */
604 static void
select_file(void)605 select_file (void)
606 {
607 
608   GtkWidget *file_chooser;
609 
610   file_chooser =  gtk_file_chooser_dialog_new (_("Select a new password file"),\
611     GTK_WINDOW (gtk_window_list_toplevels ()->data),
612     GTK_FILE_CHOOSER_ACTION_OPEN,
613     GTK_STOCK_CANCEL,
614     GTK_RESPONSE_CANCEL,
615     GTK_STOCK_OPEN,
616     GTK_RESPONSE_OK,
617     NULL);
618 
619  switch (gtk_dialog_run (GTK_DIALOG (file_chooser)))
620  {
621 
622   case GTK_RESPONSE_OK:
623 
624     g_free (passwdfile);
625     passwdfile = g_strdup (\
626       gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_chooser)));
627 
628     if (GTK_IS_ENTRY (passwd_file_entry))
629       gtk_entry_set_text (GTK_ENTRY (passwd_file_entry), passwdfile);
630 
631 #ifdef DISABLE_GSETTINGS
632     config_write ();
633 #else
634     write_settings ();
635 #endif
636 
637     show_askpassword ();
638     ;;
639   default:
640     break;
641   }
642 
643   gtk_widget_destroy (file_chooser);
644 
645 }
646 
647 GtkWidget*
pasman_askpassword_widget_new(void)648 pasman_askpassword_widget_new (void)
649 {
650 
651   GtkWidget *entry;
652   GtkWidget *button;
653   GtkWidget *hbox;
654   GtkWidget *vbox;
655 
656   vbox = gtk_vbox_new (FALSE, 2);
657   gtk_box_pack_start (GTK_BOX (vbox),\
658     gtk_label_new (_("Please enter your PGP Passphrase:")) , FALSE, FALSE, 2);
659 
660   gtk_container_set_border_width (GTK_CONTAINER (vbox), 2);
661 
662   hbox = gtk_hbox_new (FALSE, 2);
663   /* password entry */
664   entry = gtk_entry_new ();
665   gtk_entry_set_visibility (GTK_ENTRY (entry), FALSE);
666 
667   gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
668 
669   g_signal_connect (entry, "activate",\
670     G_CALLBACK (change_pass), NULL);
671 
672   g_signal_connect_swapped (entry, "activate",\
673     G_CALLBACK (gtk_widget_hide), vbox);
674 
675   button = gtk_toggle_button_new ();
676 
677   gtk_button_set_image (GTK_BUTTON (button),\
678     gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_MENU));
679 
680   g_signal_connect (button, "toggled",\
681     G_CALLBACK (change_passwd_entry_visibility), (gpointer)entry);
682 
683   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
684 
685   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
686 
687   hbox = gtk_hbox_new (FALSE, 2);
688   gtk_box_pack_start (GTK_BOX (hbox),\
689     gtk_alignment_new (0.0, 0.0, 0.0, 0.0), TRUE, TRUE, 0);
690 
691   button = gtk_button_new_from_stock (GTK_STOCK_OK);
692   g_signal_connect_swapped (button, "clicked",\
693     G_CALLBACK (change_pass), entry);
694   g_signal_connect_swapped (button, "clicked",\
695     G_CALLBACK (gtk_widget_hide), vbox);
696 
697   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
698 
699   button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
700   g_signal_connect_swapped (button, "clicked",\
701     G_CALLBACK (gtk_widget_hide), vbox);
702 
703   gtk_box_pack_start (GTK_BOX (hbox), button, FALSE, FALSE, 0);
704 
705   gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
706 
707   g_signal_connect_swapped (G_OBJECT (vbox), "show",\
708     G_CALLBACK (gtk_widget_grab_focus), entry);
709 
710   return vbox;
711 
712 }
713 
714 /*
715  * show_askpassword
716  *      create the asking passphrase window
717  */
718 void
show_askpassword(void)719 show_askpassword (void)
720 {
721 
722   GtkWidget *window;
723   GtkWidget *prompt_passwd;
724   GtkWidget *tree_view;
725   GtkTreeModel *tree_model;
726 
727   window = gtk_window_list_toplevels ()->data;
728   prompt_passwd = g_object_get_data (G_OBJECT (window), "pasman_prompt_passwd");
729 
730   gtk_widget_show (prompt_passwd);
731 
732   tree_view = g_object_get_data (G_OBJECT (window), "pasman_tree_view");
733   tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));
734 
735   emptytree (GTK_TREE_STORE (tree_model));
736 }
737 
738 /*
739  * Change password visibility state.
740  *
741  */
742 
743 static void
change_passwd_entry_visibility(GtkToggleButton * togglebutton,gpointer user_data)744 change_passwd_entry_visibility (GtkToggleButton *togglebutton,\
745   gpointer user_data)
746 {
747 
748   gtk_entry_set_visibility (GTK_ENTRY (user_data),\
749     gtk_toggle_button_get_active (togglebutton));
750 
751 }
752 
753 /*
754  *  Notification Area widget.
755  *
756  */
757 
758 void
window_state_change(GtkWidget * widget,GdkEventWindowState * event,gpointer user_data)759 window_state_change (GtkWidget *widget, GdkEventWindowState *event,\
760   gpointer user_data)
761 {
762 
763   gtk_window_set_skip_taskbar_hint (GTK_WINDOW (widget),\
764     (event->new_window_state == GDK_WINDOW_STATE_ICONIFIED));
765 
766 }
767 
768 void
pasman_window_swap_state(GtkWindow * window)769 pasman_window_swap_state (GtkWindow *window)
770 {
771 
772  GdkWindow *w;
773 
774  w = gtk_widget_get_window (GTK_WIDGET (window));
775 
776  if (gdk_window_get_state(w) == GDK_WINDOW_STATE_ICONIFIED)
777     gtk_window_deiconify (window);
778  else
779     gtk_window_iconify (window);
780 
781 }
782 
783 /*
784  *  Old function
785  *
786  */
787 
788 /* ###########################################################################*/
789 
790 /*
791  * change_pass
792  *      change the password and try to fill the tree
793  */
794 void
change_pass(GtkWidget * passtxt)795 change_pass (GtkWidget *passtxt)
796 {
797 
798   GtkWidget *window;
799   GtkWidget *entry;
800   GtkWidget *tree_view;
801   GtkTreeModel *tree_model;
802 
803   window = gtk_window_list_toplevels ()->data;
804 
805   /* read password from the entry widget */
806   globalpasswd = g_strdup (gtk_entry_get_text (GTK_ENTRY (passtxt)));
807 
808   gtk_entry_set_text (GTK_ENTRY (passtxt), "");
809 
810   tree_view = g_object_get_data (G_OBJECT (window), "pasman_tree_view");
811   tree_model = gtk_tree_view_get_model (GTK_TREE_VIEW (tree_view));
812 
813   /* empty tree */
814   emptytree (GTK_TREE_STORE (tree_model));
815 
816   /* read file and fill tree */
817   decrypted_passwds = filltree (GTK_TREE_STORE (tree_model));
818 
819   entry = g_object_get_data (G_OBJECT (window), "pasman_search_entry");
820 
821   gtk_widget_grab_focus (entry);
822 
823   /* No need to keep passphrase in memory now... */
824   g_free (globalpasswd);
825 }
826 
827 /*
828  * select_command
829  *      put command in the clipboard
830  */
831 void
select_command(gchar ** data)832 select_command (gchar **data)
833 {
834   gchar *txt;
835   GtkWidget *window;
836   GtkWidget *clipboard;
837 
838   window = gtk_window_list_toplevels ()->data;
839   clipboard = g_object_get_data (G_OBJECT (window), "pasman_clipboard");
840 
841 
842   if (data)
843    {
844     txt = data[1];
845     gtk_entry_set_text (GTK_ENTRY (clipboard), txt);
846     gtk_editable_select_region (GTK_EDITABLE (clipboard), 0, -1);
847     gtk_editable_copy_clipboard (GTK_EDITABLE (clipboard));
848   }
849 }
850 
851 /*
852  * select_passwd
853  *      put password in the clipboard
854  */
855 void
select_passwd(gchar ** data)856 select_passwd (gchar **data)
857 {
858   gchar *txt;
859 
860   GtkWidget *window;
861   GtkWidget *clipboard;
862 
863   window = gtk_window_list_toplevels ()->data;
864   clipboard = g_object_get_data (G_OBJECT (window), "pasman_clipboard");
865 
866   if (data != NULL) {
867     txt = data[0];
868     gtk_entry_set_text (GTK_ENTRY (clipboard), txt);
869     gtk_editable_select_region (GTK_EDITABLE (clipboard), 0, -1);
870     gtk_editable_copy_clipboard (GTK_EDITABLE (clipboard));
871   }
872 }
873 
874 /*
875  * select_passwd_and_exec_command
876  *      put password in the clipboard
877  *      execute command
878  */
879 void
select_passwd_and_exec_command(gchar ** data)880 select_passwd_and_exec_command (gchar **data)
881 {
882   gchar *txt;
883   int ret;
884 
885   select_passwd (data);
886   if (data != NULL) {
887     txt = data[1];
888     ret = system (txt);
889   }
890 
891   (void)ret;
892 
893 }
894 
895 /*
896  * exec_command
897  *      execute command
898  */
899 void
exec_command(gchar ** data)900 exec_command (gchar **data)
901 {
902   gchar *txt;
903   int ret;
904 
905   if (data != NULL) {
906     txt = data[1];
907     ret = system (txt);
908   }
909 
910   (void)ret;
911 
912 }
913 
914 /*
915  * exec_multiple
916  *      execute command whith multiple selection
917  */
918 void
exec_multiple(gchar * hostlist)919 exec_multiple (gchar *hostlist)
920 {
921   gchar c = ' ', *s;
922   gchar *cmd, *res, *tmp;
923   int ret;
924 
925   cmd = g_strdup (csshcmd);
926 
927   /* replace %u by values */
928   res = g_strdup ("");
929   tmp = g_strdup ("");
930   for (s = cmd; *s != '\0'; ++s) {
931     if (*s == '%' && *(s + 1) != '\0') {
932 	  if ((c = *(s + 1)) == 'h') {
933         tmp = g_strconcat (res, hostlist, NULL);
934       }
935 	  else {
936 	    tmp = g_strdup_printf ("%s%c", res, *s);
937 	  }
938       s++;
939     }
940     else {
941       tmp = g_strdup_printf ("%s%c", res, *s);
942     }
943     g_free (res);
944     res = g_strdup (tmp);
945     g_free (tmp);
946   }
947 
948   /* execute command */
949   ret = system(res);
950 
951   /* free resources */
952   g_free (cmd);
953   g_free (res);
954 
955   (void)ret;
956 }
957 
958 /*
959  * menu_copy_passwd
960  *      callback for menu
961  */
962 void
menu_copy_passwd(gpointer data)963 menu_copy_passwd (gpointer data)
964 {
965   if (decrypted_passwds)
966     select_passwd (data);
967 }
968 
969 /*
970  * menu_copy_passwd_and_execute
971  *      callback for menu
972  */
973 void
menu_copy_passwd_and_execute(gpointer data)974 menu_copy_passwd_and_execute (gpointer data)
975 {
976   if (decrypted_passwds)
977     select_passwd_and_exec_command (data);
978 }
979 
980 
981 
982 /*
983  * change_config
984  *      store the values from the configuration window
985  */
986 void
change_config(GtkWidget * dialog)987 change_config (GtkWidget *dialog)
988 {
989 
990   g_free (passwdfile);
991   g_free (sshcmd);
992   g_free (ftpcmd);
993   g_free (telnetcmd);
994   g_free (httpcmd);
995   g_free (csshcmd);
996 
997   passwdfile = g_strdup (gtk_entry_get_text (GTK_ENTRY (passwd_file_entry)));
998   sshcmd = g_strdup (gtk_entry_get_text (GTK_ENTRY (ssh_cmd_entry)));
999   ftpcmd = g_strdup (gtk_entry_get_text (GTK_ENTRY (ftp_cmd_entry)));
1000   telnetcmd = g_strdup (gtk_entry_get_text (GTK_ENTRY (telnet_cmd_entry)));
1001   httpcmd = g_strdup (gtk_entry_get_text (GTK_ENTRY (http_cmd_entry)));
1002   csshcmd = g_strdup (gtk_entry_get_text (GTK_ENTRY (cssh_cmd_entry)));
1003   gtk_widget_destroy (dialog);
1004   gtk_widget_show (gtk_window_list_toplevels ()->data);
1005 #ifdef DISABLE_GSETTINGS
1006   config_write ();
1007 #else
1008   write_settings ();
1009 #endif
1010 
1011 }
1012 
1013 /* ###########################################################################*/
1014 
1015