r6600: Rework of the GTK credentials system; the credentials information is
[samba.git] / source / gtk / tools / gregedit.c
1 /* 
2    Unix SMB/CIFS implementation.
3    GTK+ registry frontend
4    
5    Copyright (C) Jelmer Vernooij 2004-2005
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "dynconfig.h"
24 #include "registry.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "gtk/common/select.h"
27 #include "gtk/common/gtk-smb.h"
28
29 GtkTreeStore *store_keys;
30 GtkListStore *store_vals;
31 GtkWidget *tree_keys;
32 GtkWidget *tree_vals;
33 GtkWidget *mainwin;
34 GtkWidget *mnu_add_key, *mnu_set_value, *mnu_del_key, *mnu_del_value, *mnu_find;
35 TALLOC_CTX *mem_ctx; /* FIXME: Split up */
36
37 GtkWidget *save;
38 GtkWidget *save_as;
39 static GtkWidget* create_openfilewin (void);
40 static GtkWidget* create_savefilewin (void);
41 struct registry_context *registry = NULL;
42 struct registry_key *current_key = NULL;
43
44 static GtkWidget* create_FindDialog (void)
45 {
46   GtkWidget *FindDialog;
47   GtkWidget *dialog_vbox2;
48   GtkWidget *vbox1;
49   GtkWidget *hbox1;
50   GtkWidget *label6;
51   GtkWidget *entry_pattern;
52   GtkWidget *frame3;
53   GtkWidget *alignment3;
54   GtkWidget *vbox2;
55   GtkWidget *checkbutton1;
56   GtkWidget *checkbutton2;
57   GtkWidget *checkbutton3;
58   GtkWidget *label7;
59   GtkWidget *dialog_action_area2;
60   GtkWidget *cancelbutton2;
61   GtkWidget *okbutton2;
62
63   FindDialog = gtk_dialog_new ();
64   gtk_window_set_title (GTK_WINDOW (FindDialog), "Find Key or Value");
65   gtk_window_set_resizable (GTK_WINDOW (FindDialog), FALSE);
66   gtk_window_set_type_hint (GTK_WINDOW (FindDialog), GDK_WINDOW_TYPE_HINT_DIALOG);
67
68   dialog_vbox2 = GTK_DIALOG (FindDialog)->vbox;
69
70   vbox1 = gtk_vbox_new (FALSE, 0);
71   gtk_box_pack_start (GTK_BOX (dialog_vbox2), vbox1, TRUE, TRUE, 0);
72
73   hbox1 = gtk_hbox_new (FALSE, 0);
74   gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
75
76   label6 = gtk_label_new ("Find String");
77   gtk_box_pack_start (GTK_BOX (hbox1), label6, FALSE, FALSE, 0);
78
79   entry_pattern = gtk_entry_new ();
80   gtk_box_pack_start (GTK_BOX (hbox1), entry_pattern, TRUE, TRUE, 0);
81
82   frame3 = gtk_frame_new (NULL);
83   gtk_box_pack_start (GTK_BOX (vbox1), frame3, TRUE, TRUE, 0);
84   gtk_frame_set_shadow_type (GTK_FRAME (frame3), GTK_SHADOW_NONE);
85
86   alignment3 = gtk_alignment_new (0.5, 0.5, 1, 1);
87   gtk_container_add (GTK_CONTAINER (frame3), alignment3);
88   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment3), 0, 0, 12, 0);
89
90   vbox2 = gtk_vbox_new (FALSE, 0);
91   gtk_container_add (GTK_CONTAINER (alignment3), vbox2);
92
93   checkbutton1 = gtk_check_button_new_with_mnemonic ("_Key Names");
94   gtk_box_pack_start (GTK_BOX (vbox2), checkbutton1, FALSE, FALSE, 0);
95
96   checkbutton2 = gtk_check_button_new_with_mnemonic ("_Value Names");
97   gtk_box_pack_start (GTK_BOX (vbox2), checkbutton2, FALSE, FALSE, 0);
98
99   checkbutton3 = gtk_check_button_new_with_mnemonic ("Value _Data");
100   gtk_box_pack_start (GTK_BOX (vbox2), checkbutton3, FALSE, FALSE, 0);
101
102   label7 = gtk_label_new ("<b>Search in</b>");
103   gtk_frame_set_label_widget (GTK_FRAME (frame3), label7);
104   gtk_label_set_use_markup (GTK_LABEL (label7), TRUE);
105
106   dialog_action_area2 = GTK_DIALOG (FindDialog)->action_area;
107   gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area2), GTK_BUTTONBOX_END);
108
109   cancelbutton2 = gtk_button_new_from_stock ("gtk-cancel");
110   gtk_dialog_add_action_widget (GTK_DIALOG (FindDialog), cancelbutton2, GTK_RESPONSE_CANCEL);
111   GTK_WIDGET_SET_FLAGS (cancelbutton2, GTK_CAN_DEFAULT);
112
113   okbutton2 = gtk_button_new_from_stock ("gtk-ok");
114   gtk_dialog_add_action_widget (GTK_DIALOG (FindDialog), okbutton2, GTK_RESPONSE_OK);
115   GTK_WIDGET_SET_FLAGS (okbutton2, GTK_CAN_DEFAULT);
116
117   gtk_widget_show_all (dialog_vbox2);
118
119   return FindDialog;
120 }
121
122 static GtkWidget* create_SetValueDialog (GtkWidget **entry_name, GtkWidget **entry_type, GtkWidget **entry_data)
123 {
124   GtkWidget *SetValueDialog;
125   GtkWidget *dialog_vbox1;
126   GtkWidget *table1;
127   GtkWidget *label3;
128   GtkWidget *label4;
129   GtkWidget *label5;
130   GtkWidget *entry_value_name;
131   GtkWidget *value_data;
132   GtkWidget *combo_data_type;
133   GtkWidget *dialog_action_area1;
134   GtkWidget *cancelbutton1;
135   GtkWidget *okbutton1;
136
137   SetValueDialog = gtk_dialog_new ();
138   gtk_window_set_title (GTK_WINDOW (SetValueDialog), "Set Registry Value");
139   gtk_window_set_position (GTK_WINDOW (SetValueDialog), GTK_WIN_POS_CENTER);
140   gtk_window_set_resizable (GTK_WINDOW (SetValueDialog), FALSE);
141   gtk_window_set_type_hint (GTK_WINDOW (SetValueDialog), GDK_WINDOW_TYPE_HINT_DIALOG);
142
143   dialog_vbox1 = GTK_DIALOG (SetValueDialog)->vbox;
144
145   table1 = gtk_table_new (3, 2, FALSE);
146   gtk_box_pack_start (GTK_BOX (dialog_vbox1), table1, TRUE, TRUE, 0);
147
148   label3 = gtk_label_new ("Value name:");
149   gtk_table_attach (GTK_TABLE (table1), label3, 0, 1, 0, 1,
150                     (GtkAttachOptions) (GTK_FILL),
151                     (GtkAttachOptions) (0), 0, 0);
152   gtk_misc_set_alignment (GTK_MISC (label3), 0, 0.5);
153
154   label4 = gtk_label_new ("Data Type:");
155   gtk_table_attach (GTK_TABLE (table1), label4, 0, 1, 1, 2,
156                     (GtkAttachOptions) (GTK_FILL),
157                     (GtkAttachOptions) (0), 0, 0);
158   gtk_misc_set_alignment (GTK_MISC (label4), 0, 0.5);
159
160   label5 = gtk_label_new ("Data:");
161   gtk_table_attach (GTK_TABLE (table1), label5, 0, 1, 2, 3,
162                     (GtkAttachOptions) (GTK_FILL),
163                     (GtkAttachOptions) (0), 0, 0);
164   gtk_misc_set_alignment (GTK_MISC (label5), 0, 0.5);
165
166   *entry_name = entry_value_name = gtk_entry_new ();
167   gtk_table_attach (GTK_TABLE (table1), entry_value_name, 1, 2, 0, 1,
168                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
169                     (GtkAttachOptions) (0), 0, 0);
170
171   *entry_data = value_data = gtk_entry_new ();
172   gtk_table_attach (GTK_TABLE (table1), value_data, 1, 2, 2, 3,
173                     (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
174                     (GtkAttachOptions) (0), 0, 0);
175
176   *entry_type = combo_data_type = gtk_combo_box_new_text ();
177
178   gtk_combo_box_append_text(GTK_COMBO_BOX(combo_data_type), "REG_NONE");
179   gtk_combo_box_append_text(GTK_COMBO_BOX(combo_data_type), "REG_SZ");
180   gtk_combo_box_append_text(GTK_COMBO_BOX(combo_data_type), "REG_EXPAND_SZ");
181   gtk_combo_box_append_text(GTK_COMBO_BOX(combo_data_type), "REG_BINARY");
182   gtk_combo_box_append_text(GTK_COMBO_BOX(combo_data_type), "REG_DWORD_LE");
183   gtk_combo_box_append_text(GTK_COMBO_BOX(combo_data_type), "REG_DWORD_BE");
184   
185   gtk_table_attach (GTK_TABLE (table1), combo_data_type, 1, 2, 1, 2,
186                     (GtkAttachOptions) (GTK_FILL),
187                     (GtkAttachOptions) (GTK_FILL), 0, 0);
188
189   dialog_action_area1 = GTK_DIALOG (SetValueDialog)->action_area;
190   gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area1), GTK_BUTTONBOX_END);
191
192   cancelbutton1 = gtk_button_new_from_stock ("gtk-cancel");
193   gtk_dialog_add_action_widget (GTK_DIALOG (SetValueDialog), cancelbutton1, GTK_RESPONSE_CANCEL);
194   GTK_WIDGET_SET_FLAGS (cancelbutton1, GTK_CAN_DEFAULT);
195
196   okbutton1 = gtk_button_new_from_stock ("gtk-ok");
197   gtk_dialog_add_action_widget (GTK_DIALOG (SetValueDialog), okbutton1, GTK_RESPONSE_OK);
198   GTK_WIDGET_SET_FLAGS (okbutton1, GTK_CAN_DEFAULT);
199
200   gtk_widget_show_all (dialog_vbox1);
201
202   return SetValueDialog;
203 }
204
205 static GtkWidget* create_NewKeyDialog (GtkWidget **name_entry)
206 {
207   GtkWidget *NewKeyDialog;
208   GtkWidget *dialog_vbox2;
209   GtkWidget *hbox1;
210   GtkWidget *label6;
211   GtkWidget *entry_key_name;
212   GtkWidget *dialog_action_area2;
213   GtkWidget *cancelbutton2;
214   GtkWidget *okbutton2;
215
216   NewKeyDialog = gtk_dialog_new ();
217   gtk_window_set_title (GTK_WINDOW (NewKeyDialog), "New Registry Key");
218   gtk_window_set_position (GTK_WINDOW (NewKeyDialog), GTK_WIN_POS_CENTER);
219   gtk_window_set_resizable (GTK_WINDOW (NewKeyDialog), FALSE);
220   gtk_window_set_type_hint (GTK_WINDOW (NewKeyDialog), GDK_WINDOW_TYPE_HINT_DIALOG);
221
222   dialog_vbox2 = GTK_DIALOG (NewKeyDialog)->vbox;
223
224   hbox1 = gtk_hbox_new (FALSE, 0);
225   gtk_box_pack_start (GTK_BOX (dialog_vbox2), hbox1, TRUE, TRUE, 0);
226
227   label6 = gtk_label_new ("Name:");
228   gtk_box_pack_start (GTK_BOX (hbox1), label6, FALSE, FALSE, 0);
229
230   entry_key_name = gtk_entry_new ();
231   gtk_box_pack_start (GTK_BOX (hbox1), entry_key_name, TRUE, TRUE, 0);
232
233   dialog_action_area2 = GTK_DIALOG (NewKeyDialog)->action_area;
234   gtk_button_box_set_layout (GTK_BUTTON_BOX (dialog_action_area2), GTK_BUTTONBOX_END);
235
236   *name_entry = entry_key_name;
237
238   cancelbutton2 = gtk_button_new_from_stock ("gtk-cancel");
239   gtk_dialog_add_action_widget (GTK_DIALOG (NewKeyDialog), cancelbutton2, GTK_RESPONSE_CANCEL);
240   GTK_WIDGET_SET_FLAGS (cancelbutton2, GTK_CAN_DEFAULT);
241
242   okbutton2 = gtk_button_new_from_stock ("gtk-ok");
243   gtk_dialog_add_action_widget (GTK_DIALOG (NewKeyDialog), okbutton2, GTK_RESPONSE_OK);
244   GTK_WIDGET_SET_FLAGS (okbutton2, GTK_CAN_DEFAULT);
245
246   gtk_widget_show_all (dialog_vbox2);
247
248   return NewKeyDialog;
249 }
250
251 static void expand_key(GtkTreeView *treeview, GtkTreeIter *parent, GtkTreePath *arg2)
252 {
253         GtkTreeIter firstiter, iter, tmpiter;
254         struct registry_key *k, *sub;
255         char *name;
256         WERROR error;
257         int i;
258
259         gtk_tree_model_iter_children(GTK_TREE_MODEL(store_keys), &firstiter, parent);
260
261         /* See if this row has ever had a name gtk_tree_store_set()'ed to it.
262            If not, read the directory contents */
263         gtk_tree_model_get(GTK_TREE_MODEL(store_keys), &firstiter, 0, &name, -1);
264
265         if(name) return;
266
267         gtk_tree_model_get(GTK_TREE_MODEL(store_keys), parent, 1, &k, -1);
268
269         g_assert(k);
270         
271         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_subkey_by_index(mem_ctx, k, i, &sub)); i++) {
272                 int count;
273                 /* Replace the blank child with the first directory entry
274            You may be tempted to remove the blank child node and then 
275            append a new one.  Don't.  If you remove the blank child 
276            node GTK gets confused and won't expand the parent row. */
277
278                 if(i == 0) {
279                         iter = firstiter;
280                 } else {
281                         gtk_tree_store_append(store_keys, &iter, parent);
282                 }
283                 gtk_tree_store_set (store_keys,
284                                             &iter, 
285                                                 0,
286                                                 sub->name,
287                                                 1, 
288                                                 sub,
289                                                 -1);
290                 
291                 if(W_ERROR_IS_OK(reg_key_num_subkeys(sub, &count)) && count > 0) 
292                         gtk_tree_store_append(store_keys, &tmpiter, &iter);
293         }
294
295         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) { 
296                 gtk_show_werror(mainwin, "While enumerating subkeys", error);
297         }
298 }
299
300 static void registry_load_hive(struct registry_key *root)
301 {
302         GtkTreeIter iter, tmpiter;
303         gtk_list_store_clear(store_vals);
304         /* Add the root */
305         gtk_tree_store_append(store_keys, &iter, NULL);
306         gtk_tree_store_set (store_keys,
307                                     &iter, 
308                                         0,
309                                         root->name?root->name:"",
310                                         1,
311                                         root,
312                                         -1);
313
314         gtk_tree_store_append(store_keys, &tmpiter, &iter);
315
316         gtk_widget_set_sensitive( save, True );
317         gtk_widget_set_sensitive( save_as, True );
318 }
319
320 static void registry_load_root(void) 
321 {
322         struct registry_key *root;
323         uint32_t i = 0;
324         if(!registry) return;
325
326         gtk_list_store_clear(store_vals);
327         gtk_tree_store_clear(store_keys);
328
329         for(i = HKEY_CLASSES_ROOT; i <= HKEY_PERFORMANCE_NLSTEXT; i++) 
330         {
331                 if (!W_ERROR_IS_OK(reg_get_predefined_key(registry, i, &root))) { continue; }
332
333                 registry_load_hive(root);
334         }
335 }
336
337 static void on_open_file_activate (GtkMenuItem *menuitem, gpointer user_data)
338 {
339         GtkWidget *openfilewin;
340         gint result;
341         char *filename, *tmp;
342         struct registry_key *root;
343         WERROR error;
344
345         openfilewin = create_openfilewin();
346
347         result = gtk_dialog_run(GTK_DIALOG(openfilewin));
348
349         switch(result) {
350         case GTK_RESPONSE_OK:
351                 filename = strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(openfilewin)));
352                 error = reg_open_hive(NULL, user_data, filename, NULL, &root);
353                 if(!W_ERROR_IS_OK(error)) {
354                         gtk_show_werror(mainwin, "Error while opening hive", error);
355                         break;
356                 }
357
358                 tmp = g_strdup_printf("Registry Editor - %s", filename);
359                 gtk_window_set_title (GTK_WINDOW (mainwin), tmp);
360                 g_free(tmp);
361                 gtk_tree_store_clear(store_keys);
362                 registry_load_hive(root);
363                 break;
364         default:
365                 break;
366         }
367
368         gtk_widget_destroy(openfilewin);
369 }
370
371 static void on_open_gconf_activate(GtkMenuItem *menuitem, gpointer user_data)
372 {
373         struct registry_key *root;
374         WERROR error = reg_open_hive(NULL, "gconf", NULL, NULL, &root);
375         if(!W_ERROR_IS_OK(error)) {
376                 gtk_show_werror(mainwin, "Error while opening GConf", error);
377                 return;
378         }
379
380         gtk_window_set_title (GTK_WINDOW (mainwin), "Registry Editor - GConf");
381
382         gtk_tree_store_clear(store_keys);
383         registry_load_hive(root);
384 }
385
386 static void on_open_local_activate(GtkMenuItem *menuitem, gpointer user_data)
387 {
388         WERROR error = reg_open_local(&registry);
389         if(!W_ERROR_IS_OK(error)) {
390                 gtk_show_werror(mainwin, "Error while opening local registry", error);
391                 return;
392         }
393         registry_load_root();
394 }
395
396 static void on_open_remote_activate(GtkMenuItem *menuitem, gpointer user_data)
397 {
398         char *tmp;
399         GtkWidget *rpcwin = GTK_WIDGET(gtk_rpc_binding_dialog_new(NULL));
400         gint result = gtk_dialog_run(GTK_DIALOG(rpcwin));
401         WERROR error;
402         struct cli_credentials *creds;
403         
404         if(result != GTK_RESPONSE_ACCEPT)
405         {
406                 gtk_widget_destroy(rpcwin);
407                 return;
408         }
409
410         creds = cli_credentials_init(mem_ctx);
411         cli_credentials_guess(creds);
412         cli_credentials_set_gtk_callbacks(creds);
413
414         error = reg_open_remote(&registry, 
415                         creds,
416                         gtk_rpc_binding_dialog_get_binding_string(GTK_RPC_BINDING_DIALOG(rpcwin), mem_ctx));
417
418         if(!W_ERROR_IS_OK(error)) {
419                 gtk_show_werror(mainwin, "Error while opening remote registry", error);
420                 gtk_widget_destroy(rpcwin);
421                 return;
422         }
423
424         tmp = g_strdup_printf("Registry Editor - Remote Registry at %s", gtk_rpc_binding_dialog_get_host(GTK_RPC_BINDING_DIALOG(rpcwin)));
425         gtk_window_set_title (GTK_WINDOW (mainwin), tmp);
426         g_free(tmp);
427
428         registry_load_root();
429
430
431         gtk_widget_destroy(rpcwin);
432 }
433
434
435 static void on_save_activate(GtkMenuItem *menuitem, gpointer user_data)
436 {
437         WERROR error = reg_save(registry, NULL);
438         if(!W_ERROR_IS_OK(error)) {
439                 gtk_show_werror(mainwin, "Error while saving", error);
440         }
441 }
442
443
444 static void on_save_as_activate(GtkMenuItem *menuitem, gpointer user_data)
445 {
446         gint result;
447         WERROR error;
448         GtkWidget *savefilewin = create_savefilewin();
449         result = gtk_dialog_run(GTK_DIALOG(savefilewin));
450         switch(result) {
451         case GTK_RESPONSE_OK:
452                 error = reg_save(registry, gtk_file_selection_get_filename(GTK_FILE_SELECTION(savefilewin)));
453                 if(!W_ERROR_IS_OK(error)) {
454                         gtk_show_werror(mainwin, "Error while saving as", error);
455                 }
456                 break;
457
458         default:
459                 break;
460
461         }
462         gtk_widget_destroy(savefilewin);
463 }
464
465
466 static void on_quit_activate(GtkMenuItem *menuitem, gpointer user_data)
467 {
468         gtk_main_quit();
469 }
470
471
472 static void on_delete_value_activate(GtkMenuItem *menuitem, gpointer user_data)
473 {
474         WERROR error;
475         GtkTreeIter iter;
476         const char *value;
477
478         if (!gtk_tree_selection_get_selected (gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_vals)), NULL, &iter)) {
479                 return;
480         }
481
482         gtk_tree_model_get(GTK_TREE_MODEL(store_vals), &iter, 0, &value, -1);
483         
484         error = reg_del_value(current_key, value);
485
486         if (!W_ERROR_IS_OK(error)) {
487                 gtk_show_werror(NULL, "Error while deleting value", error);
488                 return;
489         }
490 }
491
492 static void on_delete_key_activate(GtkMenuItem *menuitem, gpointer user_data)
493 {
494         WERROR error;
495         GtkTreeIter iter, parentiter;
496         struct registry_key *parent_key;
497
498         if (!gtk_tree_selection_get_selected (gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_keys)), NULL, &iter)) {
499                 return;
500         }
501
502         if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_keys), &parentiter, &iter)) {
503                 return;
504         }
505         
506         gtk_tree_model_get(GTK_TREE_MODEL(store_keys), &parentiter, 1, &parent_key, -1);
507         
508         error = reg_key_del(parent_key, current_key->name);
509
510         if (!W_ERROR_IS_OK(error)) {
511                 gtk_show_werror(NULL, "Error while deleting key", error);
512                 return;
513         }
514 }
515
516 static void on_add_key_activate(GtkMenuItem *menuitem, gpointer user_data)
517 {
518         GtkWidget *entry;
519         GtkDialog *addwin = GTK_DIALOG(create_NewKeyDialog(&entry));
520         gint result = gtk_dialog_run(addwin);
521
522         if (result == GTK_RESPONSE_OK)
523         {
524                 struct registry_key *newkey;
525                 WERROR error = reg_key_add_name(mem_ctx, current_key, gtk_entry_get_text(GTK_ENTRY(entry)), 0, NULL, &newkey);
526
527                 if (!W_ERROR_IS_OK(error)) {
528                         gtk_show_werror(NULL, "Error while adding key", error);
529                 }
530         }
531
532         gtk_widget_destroy(GTK_WIDGET(addwin));
533 }
534
535 static void on_value_activate(GtkTreeView *treeview, GtkTreePath *arg1,
536          GtkTreeViewColumn *arg2, gpointer user_data)
537 {
538         GtkWidget *entry_name, *entry_type, *entry_value;
539         GtkDialog *addwin = GTK_DIALOG(create_SetValueDialog(&entry_name, &entry_type, &entry_value));
540         GtkTreeIter iter;
541         struct registry_value *value;
542         gint result;
543
544         gtk_tree_model_get_iter(GTK_TREE_MODEL(store_vals), &iter, arg1);
545
546         gtk_tree_model_get(GTK_TREE_MODEL(store_vals), &iter, 3, &value, -1);
547
548         gtk_widget_set_sensitive(entry_name, FALSE);
549         gtk_entry_set_text(GTK_ENTRY(entry_name), value->name);
550         gtk_entry_set_text(GTK_ENTRY(entry_value), reg_val_data_string(mem_ctx, value));
551         gtk_combo_box_set_active(GTK_COMBO_BOX(entry_type), value->data_type);
552         
553         result = gtk_dialog_run(addwin);
554         if (result == GTK_RESPONSE_OK) 
555         {
556                 WERROR error;
557                 struct registry_value *val;
558                 
559                 reg_string_to_val(mem_ctx,str_regtype(gtk_combo_box_get_active(GTK_COMBO_BOX(entry_type))), gtk_entry_get_text(GTK_ENTRY(entry_value)), &val);
560                 
561                 error = reg_val_set(current_key, gtk_entry_get_text(GTK_ENTRY(entry_name)), val->data_type, val->data_blk, val->data_len);
562
563                 if (!W_ERROR_IS_OK(error)) {
564                         gtk_show_werror(NULL, "Error while setting value", error);
565                 }
566         }
567         gtk_widget_destroy(GTK_WIDGET(addwin));
568 }
569
570 static void on_set_value_activate(GtkMenuItem *menuitem, gpointer user_data)
571 {
572         GtkWidget *entry_name, *entry_type, *entry_value;
573         GtkDialog *addwin = GTK_DIALOG(create_SetValueDialog(&entry_name, &entry_type, &entry_value));
574         gint result = gtk_dialog_run(addwin);
575         if (result == GTK_RESPONSE_OK) 
576         {
577                 WERROR error;
578                 struct registry_value *val;
579                 
580                 reg_string_to_val(mem_ctx,str_regtype(gtk_combo_box_get_active(GTK_COMBO_BOX(entry_type))), gtk_entry_get_text(GTK_ENTRY(entry_value)), &val);
581                 
582                 error = reg_val_set(current_key, gtk_entry_get_text(GTK_ENTRY(entry_name)), val->data_type, val->data_blk, val->data_len);
583
584                 if (!W_ERROR_IS_OK(error)) {
585                         gtk_show_werror(NULL, "Error while setting value", error);
586                 }
587         }
588         gtk_widget_destroy(GTK_WIDGET(addwin));
589 }
590
591 static void on_find_activate(GtkMenuItem *menuitem, gpointer user_data)
592 {
593         GtkDialog *findwin = GTK_DIALOG(create_FindDialog());
594         /*gint result = gtk_dialog_run(findwin);
595         FIXME */
596         gtk_widget_destroy(GTK_WIDGET(findwin));
597 }
598
599 static void on_about_activate (GtkMenuItem *menuitem, gpointer user_data)
600 {
601     GtkDialog *aboutwin = GTK_DIALOG(create_gtk_samba_about_dialog("gregedit"));
602     gtk_dialog_run(aboutwin);
603     gtk_widget_destroy(GTK_WIDGET(aboutwin));
604 }
605
606 static gboolean on_key_activate(GtkTreeSelection *selection,
607                                              GtkTreeModel *model,
608                                              GtkTreePath *path,
609                                              gboolean path_currently_selected,
610                                              gpointer data)
611 {
612         int i;
613         struct registry_key *k;
614         struct registry_value *val;
615         WERROR error;
616         GtkTreeIter parent;
617
618         gtk_widget_set_sensitive(mnu_add_key, !path_currently_selected);
619         gtk_widget_set_sensitive(mnu_set_value, !path_currently_selected);
620         gtk_widget_set_sensitive(mnu_del_key, !path_currently_selected);
621         gtk_widget_set_sensitive(mnu_del_value, !path_currently_selected);
622         gtk_widget_set_sensitive(mnu_find, !path_currently_selected);
623
624         if(path_currently_selected) { 
625                 current_key = NULL; 
626                 return TRUE; 
627         }
628
629         gtk_tree_model_get_iter(GTK_TREE_MODEL(store_keys), &parent, path);
630         gtk_tree_model_get(GTK_TREE_MODEL(store_keys), &parent, 1, &k, -1);
631
632         current_key = k;
633
634         if (!k) return FALSE;
635
636         gtk_list_store_clear(store_vals);
637
638         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, k, i, &val)); i++) {
639                 GtkTreeIter iter;
640                 gtk_list_store_append(store_vals, &iter);
641                 gtk_list_store_set (store_vals,
642                                             &iter, 
643                                                 0,
644                                                 val->name,
645                                                 1,
646                                                 str_regtype(val->data_type),
647                                                 2,
648                                                 reg_val_data_string(mem_ctx, val),
649                                                 3, 
650                                                 val,
651                                                 -1);
652         }
653
654         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
655                  gtk_show_werror(mainwin, "Error while enumerating values",  error);
656                  return FALSE;
657         }
658         return TRUE;
659 }
660
661 static GtkWidget* create_mainwindow(void)
662 {
663         GtkWidget *vbox1;
664         GtkWidget *menubar;
665         GtkWidget *menu_file;
666         GtkWidget *menu_file_menu;
667         GtkWidget *open_nt4;
668         GtkWidget *open_ldb;
669         GtkWidget *open_w95;
670         GtkWidget *open_gconf;
671         GtkWidget *open_remote;
672         GtkWidget *open_local;
673         GtkWidget *separatormenuitem1;
674         GtkWidget *quit;
675         GtkWidget *men_key;
676         GtkWidget *men_key_menu;
677         GtkCellRenderer *renderer;
678         GtkTreeViewColumn *curcol;
679         GtkWidget *help;
680         GtkWidget *help_menu;
681         GtkWidget *about;
682         GtkWidget *hbox1;
683         GtkWidget *scrolledwindow1;
684         GtkWidget *scrolledwindow2;
685         GtkWidget *statusbar;
686         GtkAccelGroup *accel_group;
687
688         accel_group = gtk_accel_group_new ();
689
690         mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
691         gtk_window_set_title (GTK_WINDOW (mainwin), "Registry editor");
692         gtk_window_set_default_size (GTK_WINDOW (mainwin), 642, 562);
693
694         vbox1 = gtk_vbox_new (FALSE, 0);
695         gtk_container_add (GTK_CONTAINER (mainwin), vbox1);
696
697         menubar = gtk_menu_bar_new ();
698         gtk_box_pack_start (GTK_BOX (vbox1), menubar, FALSE, FALSE, 0);
699
700         menu_file = gtk_menu_item_new_with_mnemonic ("_File");
701         gtk_container_add (GTK_CONTAINER (menubar), menu_file);
702
703         menu_file_menu = gtk_menu_new ();
704         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_file), menu_file_menu);
705
706         open_local = gtk_menu_item_new_with_mnemonic ("Open _Local");
707         gtk_container_add (GTK_CONTAINER (menu_file_menu), open_local);
708         g_signal_connect ((gpointer) open_local, "activate",
709                                                   G_CALLBACK (on_open_local_activate), NULL);
710
711         if(reg_has_backend("rpc")) {
712                 open_remote = gtk_menu_item_new_with_mnemonic ("Open _Remote");
713                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_remote);
714
715                 g_signal_connect ((gpointer) open_remote, "activate",
716                                                   G_CALLBACK (on_open_remote_activate),
717                                                   NULL);
718         }
719
720         separatormenuitem1 = gtk_menu_item_new ();
721         gtk_container_add (GTK_CONTAINER (menu_file_menu), separatormenuitem1);
722         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
723
724
725         if(reg_has_backend("nt4")) {
726                 open_nt4 = gtk_image_menu_item_new_with_mnemonic("Open _NT4 file");
727                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_nt4);
728
729                 g_signal_connect(open_nt4, "activate",
730                                  G_CALLBACK (on_open_file_activate),
731                                  discard_const_p(char, "nt4"));
732         }
733
734         if(reg_has_backend("w95")) {
735                 open_w95 = gtk_image_menu_item_new_with_mnemonic("Open Win_9x file");
736                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_w95);
737
738                 g_signal_connect (open_w95, "activate",
739                                   G_CALLBACK (on_open_file_activate),
740                                   discard_const_p(char, "w95"));
741         }
742
743         if(reg_has_backend("gconf")) {
744                 open_gconf = gtk_image_menu_item_new_with_mnemonic ("Open _GConf");
745                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_gconf);
746
747                 g_signal_connect ((gpointer) open_gconf, "activate",
748                                                   G_CALLBACK (on_open_gconf_activate),
749                                                   NULL);
750         }
751
752         if(reg_has_backend("ldb")) {
753                 open_ldb = gtk_image_menu_item_new_with_mnemonic("Open _LDB file");
754                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_ldb);
755
756                 g_signal_connect(open_ldb, "activate",
757                                  G_CALLBACK (on_open_file_activate),
758                                  discard_const_p(char, "ldb"));
759         }
760
761         separatormenuitem1 = gtk_menu_item_new ();
762         gtk_container_add (GTK_CONTAINER (menu_file_menu), separatormenuitem1);
763         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
764
765         save = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
766         gtk_widget_set_sensitive( save, False );
767         gtk_container_add (GTK_CONTAINER (menu_file_menu), save);
768
769         save_as = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
770         gtk_widget_set_sensitive( save_as, False );
771         gtk_container_add (GTK_CONTAINER (menu_file_menu), save_as);
772
773         separatormenuitem1 = gtk_menu_item_new ();
774         gtk_container_add (GTK_CONTAINER (menu_file_menu), separatormenuitem1);
775         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
776
777         quit = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
778         gtk_container_add (GTK_CONTAINER (menu_file_menu), quit);
779
780         men_key = gtk_menu_item_new_with_mnemonic ("_Key");
781         gtk_container_add (GTK_CONTAINER (menubar), men_key);
782
783         men_key_menu = gtk_menu_new ();
784         gtk_menu_item_set_submenu (GTK_MENU_ITEM (men_key), men_key_menu);
785
786         mnu_add_key = gtk_image_menu_item_new_with_mnemonic("Add _Subkey");
787         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_add_key), gtk_image_new_from_stock ("gtk-add", GTK_ICON_SIZE_MENU));
788
789         gtk_widget_set_sensitive(mnu_add_key, False);
790         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_add_key);
791
792         mnu_set_value = gtk_image_menu_item_new_with_mnemonic("Set _Value");
793         gtk_widget_set_sensitive(mnu_set_value, False);
794         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_set_value), gtk_image_new_from_stock ("gtk-add", GTK_ICON_SIZE_MENU));
795         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_set_value);
796
797         mnu_find = gtk_image_menu_item_new_from_stock ("gtk-find", accel_group);
798         gtk_widget_set_sensitive(mnu_find, False);
799         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_find);
800
801         mnu_del_key = gtk_image_menu_item_new_with_mnemonic ("Delete Key"); 
802         gtk_widget_set_sensitive(mnu_del_key, False);
803         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_del_value), gtk_image_new_from_stock ("gtk-delete", GTK_ICON_SIZE_MENU));
804         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_del_key);
805
806         mnu_del_value = gtk_image_menu_item_new_with_mnemonic ("Delete Value"); 
807         gtk_widget_set_sensitive(mnu_del_value, False);
808         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_del_value), gtk_image_new_from_stock ("gtk-delete", GTK_ICON_SIZE_MENU));
809         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_del_value);
810
811
812         help = gtk_menu_item_new_with_mnemonic ("_Help");
813         gtk_container_add (GTK_CONTAINER (menubar), help);
814
815         help_menu = gtk_menu_new ();
816         gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), help_menu);
817
818         about = gtk_menu_item_new_with_mnemonic ("_About");
819         gtk_container_add (GTK_CONTAINER (help_menu), about);
820
821         hbox1 = gtk_hbox_new (FALSE, 0);
822         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
823
824         scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
825         gtk_box_pack_start (GTK_BOX (hbox1), scrolledwindow1, TRUE, TRUE, 0);
826
827         tree_keys = gtk_tree_view_new ();
828
829         /* Column names */
830         curcol = gtk_tree_view_column_new ();
831         gtk_tree_view_column_set_title(curcol, "Name");
832         renderer = gtk_cell_renderer_text_new();
833         gtk_tree_view_column_pack_start(curcol, renderer, True);
834
835         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_keys), curcol);
836
837         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
838         gtk_container_add (GTK_CONTAINER (scrolledwindow1), tree_keys);
839         store_keys = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
840         gtk_tree_view_set_model(GTK_TREE_VIEW(tree_keys), GTK_TREE_MODEL(store_keys));
841         g_object_unref(store_keys);
842
843         gtk_tree_selection_set_select_function (gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_keys)), on_key_activate, NULL, NULL);
844
845         g_signal_connect ((gpointer) tree_keys, "row-expanded",
846                                           G_CALLBACK (expand_key),
847                                           NULL);
848
849
850         scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
851         gtk_box_pack_start (GTK_BOX (hbox1), scrolledwindow2, TRUE, TRUE, 0);
852
853         tree_vals = gtk_tree_view_new ();
854         /* Column names */
855
856         curcol = gtk_tree_view_column_new ();
857         gtk_tree_view_column_set_title(curcol, "Name");
858         renderer = gtk_cell_renderer_text_new();
859         gtk_tree_view_column_pack_start(curcol, renderer, True);
860         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_vals), curcol);
861         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
862
863         curcol = gtk_tree_view_column_new ();
864         gtk_tree_view_column_set_title(curcol, "Type");
865         renderer = gtk_cell_renderer_text_new();
866         gtk_tree_view_column_pack_start(curcol, renderer, True);
867         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_vals), curcol);
868         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 1);
869
870         curcol = gtk_tree_view_column_new ();
871         gtk_tree_view_column_set_title(curcol, "Value");
872         renderer = gtk_cell_renderer_text_new();
873         gtk_tree_view_column_pack_start(curcol, renderer, True);
874         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_vals), curcol);
875         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 2);
876
877
878         gtk_container_add (GTK_CONTAINER (scrolledwindow2), tree_vals);
879
880         store_vals = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
881         gtk_tree_view_set_model(GTK_TREE_VIEW(tree_vals), GTK_TREE_MODEL(store_vals));
882         g_object_unref(store_vals);
883
884         statusbar = gtk_statusbar_new ();
885         gtk_box_pack_start (GTK_BOX (vbox1), statusbar, FALSE, FALSE, 0);
886         gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR (statusbar), FALSE);
887
888         g_signal_connect ((gpointer) save, "activate",
889                                           G_CALLBACK (on_save_activate),
890                                           NULL);
891         g_signal_connect ((gpointer) save_as, "activate",
892                                           G_CALLBACK (on_save_as_activate),
893                                           NULL);
894         g_signal_connect ((gpointer) quit, "activate",
895                                           G_CALLBACK (on_quit_activate),
896                                           NULL);
897         g_signal_connect ((gpointer) mnu_add_key, "activate",
898                                           G_CALLBACK (on_add_key_activate),
899                                           NULL);
900         g_signal_connect ((gpointer) mnu_set_value, "activate",
901                                           G_CALLBACK (on_set_value_activate),
902                                           NULL);
903         g_signal_connect ((gpointer) mnu_find, "activate",
904                                           G_CALLBACK (on_find_activate),
905                                           NULL);
906         g_signal_connect ((gpointer) mnu_del_key, "activate",
907                                           G_CALLBACK (on_delete_key_activate),
908                                           NULL);
909         g_signal_connect ((gpointer) mnu_del_value, "activate",
910                                           G_CALLBACK (on_delete_value_activate),
911                                           NULL);
912         g_signal_connect ((gpointer) about, "activate",
913                                           G_CALLBACK (on_about_activate),
914                                           NULL);
915
916         g_signal_connect ((gpointer) tree_vals, "row-activated",
917                                           G_CALLBACK (on_value_activate),
918                                           NULL);
919
920
921         gtk_window_add_accel_group (GTK_WINDOW (mainwin), accel_group);
922
923         return mainwin;
924 }
925
926 static GtkWidget* create_openfilewin (void)
927 {
928         GtkWidget *openfilewin;
929         GtkWidget *ok_button;
930         GtkWidget *cancel_button;
931
932         openfilewin = gtk_file_selection_new ("Select File");
933         gtk_container_set_border_width (GTK_CONTAINER (openfilewin), 10);
934
935         ok_button = GTK_FILE_SELECTION (openfilewin)->ok_button;
936         GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
937
938         cancel_button = GTK_FILE_SELECTION (openfilewin)->cancel_button;
939         GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
940
941         return openfilewin;
942 }
943
944 static GtkWidget* create_savefilewin (void)
945 {
946         GtkWidget *savefilewin;
947         GtkWidget *ok_button;
948         GtkWidget *cancel_button;
949
950         savefilewin = gtk_file_selection_new ("Select File");
951         gtk_container_set_border_width (GTK_CONTAINER (savefilewin), 10);
952
953         ok_button = GTK_FILE_SELECTION (savefilewin)->ok_button;
954         GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
955
956         cancel_button = GTK_FILE_SELECTION (savefilewin)->cancel_button;
957         GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
958
959         return savefilewin;
960 }
961
962 static int gregedit_load_defaults(void)
963 {
964         WERROR error = reg_open_local(&registry);
965         if(!W_ERROR_IS_OK(error)) {
966                 gtk_show_werror(mainwin, "Error while loading local registry", error);
967                 return -1;
968         }
969         registry_load_root();
970
971         return 0;
972 }
973
974 int main(int argc, char *argv[])
975 {
976         int ret;
977
978         gregedit_init_subsystems;
979         lp_load(dyn_CONFIGFILE,True,False,False);
980         load_interfaces();
981         setup_logging(argv[0], DEBUG_STDERR);
982
983         mem_ctx = talloc_init("gregedit");
984
985         gtk_init(&argc, &argv);
986         mainwin = create_mainwindow();
987         gtk_widget_show_all(mainwin);
988
989         ret = gregedit_load_defaults();
990         if (ret != 0) goto failed;
991
992         ret = gtk_event_loop();
993
994 failed:
995         talloc_free(mem_ctx);
996         return ret;
997 }