r10026: Move registry header file to lib/registry
[bbaumbach/samba-autobuild/.git] / source4 / 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 "lib/registry/registry.h"
25 #include "lib/cmdline/popt_common.h"
26 #include "gtk/common/select.h"
27 #include "gtk/common/gtk-smb.h"
28
29 static GtkTreeStore *store_keys;
30 static GtkListStore *store_vals;
31 static GtkWidget *tree_keys;
32 static GtkWidget *tree_vals;
33 static GtkWidget *mainwin;
34 static GtkWidget *mnu_add_key, *mnu_set_value, *mnu_del_key, *mnu_del_value, *mnu_find;
35 static TALLOC_CTX *mem_ctx; /* FIXME: Split up */
36
37 static GtkWidget *save;
38 static 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                 uint32_t 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                                 NULL);
418
419         if(!W_ERROR_IS_OK(error)) {
420                 gtk_show_werror(mainwin, "Error while opening remote registry", error);
421                 gtk_widget_destroy(rpcwin);
422                 return;
423         }
424
425         tmp = g_strdup_printf("Registry Editor - Remote Registry at %s", gtk_rpc_binding_dialog_get_host(GTK_RPC_BINDING_DIALOG(rpcwin)));
426         gtk_window_set_title (GTK_WINDOW (mainwin), tmp);
427         g_free(tmp);
428
429         registry_load_root();
430
431
432         gtk_widget_destroy(rpcwin);
433 }
434
435
436 static void on_save_activate(GtkMenuItem *menuitem, gpointer user_data)
437 {
438         WERROR error = reg_save(registry, NULL);
439         if(!W_ERROR_IS_OK(error)) {
440                 gtk_show_werror(mainwin, "Error while saving", error);
441         }
442 }
443
444
445 static void on_save_as_activate(GtkMenuItem *menuitem, gpointer user_data)
446 {
447         gint result;
448         WERROR error;
449         GtkWidget *savefilewin = create_savefilewin();
450         result = gtk_dialog_run(GTK_DIALOG(savefilewin));
451         switch(result) {
452         case GTK_RESPONSE_OK:
453                 error = reg_save(registry, gtk_file_selection_get_filename(GTK_FILE_SELECTION(savefilewin)));
454                 if(!W_ERROR_IS_OK(error)) {
455                         gtk_show_werror(mainwin, "Error while saving as", error);
456                 }
457                 break;
458
459         default:
460                 break;
461
462         }
463         gtk_widget_destroy(savefilewin);
464 }
465
466
467 static void on_quit_activate(GtkMenuItem *menuitem, gpointer user_data)
468 {
469         gtk_main_quit();
470 }
471
472
473 static void on_delete_value_activate(GtkMenuItem *menuitem, gpointer user_data)
474 {
475         WERROR error;
476         GtkTreeIter iter;
477         const char *value;
478
479         if (!gtk_tree_selection_get_selected (gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_vals)), NULL, &iter)) {
480                 return;
481         }
482
483         gtk_tree_model_get(GTK_TREE_MODEL(store_vals), &iter, 0, &value, -1);
484         
485         error = reg_del_value(current_key, value);
486
487         if (!W_ERROR_IS_OK(error)) {
488                 gtk_show_werror(NULL, "Error while deleting value", error);
489                 return;
490         }
491 }
492
493 static void on_delete_key_activate(GtkMenuItem *menuitem, gpointer user_data)
494 {
495         WERROR error;
496         GtkTreeIter iter, parentiter;
497         struct registry_key *parent_key;
498
499         if (!gtk_tree_selection_get_selected (gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_keys)), NULL, &iter)) {
500                 return;
501         }
502
503         if (!gtk_tree_model_iter_parent(GTK_TREE_MODEL(store_keys), &parentiter, &iter)) {
504                 return;
505         }
506         
507         gtk_tree_model_get(GTK_TREE_MODEL(store_keys), &parentiter, 1, &parent_key, -1);
508         
509         error = reg_key_del(parent_key, current_key->name);
510
511         if (!W_ERROR_IS_OK(error)) {
512                 gtk_show_werror(NULL, "Error while deleting key", error);
513                 return;
514         }
515 }
516
517 static void on_add_key_activate(GtkMenuItem *menuitem, gpointer user_data)
518 {
519         GtkWidget *entry;
520         GtkDialog *addwin = GTK_DIALOG(create_NewKeyDialog(&entry));
521         gint result = gtk_dialog_run(addwin);
522
523         if (result == GTK_RESPONSE_OK)
524         {
525                 struct registry_key *newkey;
526                 WERROR error = reg_key_add_name(mem_ctx, current_key, gtk_entry_get_text(GTK_ENTRY(entry)), 0, NULL, &newkey);
527
528                 if (!W_ERROR_IS_OK(error)) {
529                         gtk_show_werror(NULL, "Error while adding key", error);
530                 }
531         }
532
533         gtk_widget_destroy(GTK_WIDGET(addwin));
534 }
535
536 static void on_value_activate(GtkTreeView *treeview, GtkTreePath *arg1,
537          GtkTreeViewColumn *arg2, gpointer user_data)
538 {
539         GtkWidget *entry_name, *entry_type, *entry_value;
540         GtkDialog *addwin = GTK_DIALOG(create_SetValueDialog(&entry_name, &entry_type, &entry_value));
541         GtkTreeIter iter;
542         struct registry_value *value;
543         gint result;
544
545         gtk_tree_model_get_iter(GTK_TREE_MODEL(store_vals), &iter, arg1);
546
547         gtk_tree_model_get(GTK_TREE_MODEL(store_vals), &iter, 3, &value, -1);
548
549         gtk_widget_set_sensitive(entry_name, FALSE);
550         gtk_entry_set_text(GTK_ENTRY(entry_name), value->name);
551         gtk_entry_set_text(GTK_ENTRY(entry_value), reg_val_data_string(mem_ctx, value));
552         gtk_combo_box_set_active(GTK_COMBO_BOX(entry_type), value->data_type);
553         
554         result = gtk_dialog_run(addwin);
555         if (result == GTK_RESPONSE_OK) 
556         {
557                 WERROR error;
558                 struct registry_value *val;
559                 
560                 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);
561                 
562                 error = reg_val_set(current_key, gtk_entry_get_text(GTK_ENTRY(entry_name)), val->data_type, val->data);
563
564                 if (!W_ERROR_IS_OK(error)) {
565                         gtk_show_werror(NULL, "Error while setting value", error);
566                 }
567         }
568         gtk_widget_destroy(GTK_WIDGET(addwin));
569 }
570
571 static void on_set_value_activate(GtkMenuItem *menuitem, gpointer user_data)
572 {
573         GtkWidget *entry_name, *entry_type, *entry_value;
574         GtkDialog *addwin = GTK_DIALOG(create_SetValueDialog(&entry_name, &entry_type, &entry_value));
575         gint result = gtk_dialog_run(addwin);
576         if (result == GTK_RESPONSE_OK) 
577         {
578                 WERROR error;
579                 struct registry_value *val;
580                 
581                 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);
582                 
583                 error = reg_val_set(current_key, gtk_entry_get_text(GTK_ENTRY(entry_name)), val->data_type, val->data);
584
585                 if (!W_ERROR_IS_OK(error)) {
586                         gtk_show_werror(NULL, "Error while setting value", error);
587                 }
588         }
589         gtk_widget_destroy(GTK_WIDGET(addwin));
590 }
591
592 static void on_find_activate(GtkMenuItem *menuitem, gpointer user_data)
593 {
594         GtkDialog *findwin = GTK_DIALOG(create_FindDialog());
595         /*gint result = gtk_dialog_run(findwin);
596         FIXME */
597         gtk_widget_destroy(GTK_WIDGET(findwin));
598 }
599
600 static void on_about_activate (GtkMenuItem *menuitem, gpointer user_data)
601 {
602     GtkDialog *aboutwin = GTK_DIALOG(create_gtk_samba_about_dialog("gregedit"));
603     gtk_dialog_run(aboutwin);
604     gtk_widget_destroy(GTK_WIDGET(aboutwin));
605 }
606
607 static gboolean on_key_activate(GtkTreeSelection *selection,
608                                              GtkTreeModel *model,
609                                              GtkTreePath *path,
610                                              gboolean path_currently_selected,
611                                              gpointer data)
612 {
613         int i;
614         struct registry_key *k;
615         struct registry_value *val;
616         WERROR error;
617         GtkTreeIter parent;
618
619         gtk_widget_set_sensitive(mnu_add_key, !path_currently_selected);
620         gtk_widget_set_sensitive(mnu_set_value, !path_currently_selected);
621         gtk_widget_set_sensitive(mnu_del_key, !path_currently_selected);
622         gtk_widget_set_sensitive(mnu_del_value, !path_currently_selected);
623         gtk_widget_set_sensitive(mnu_find, !path_currently_selected);
624
625         if(path_currently_selected) { 
626                 current_key = NULL; 
627                 return TRUE; 
628         }
629
630         gtk_tree_model_get_iter(GTK_TREE_MODEL(store_keys), &parent, path);
631         gtk_tree_model_get(GTK_TREE_MODEL(store_keys), &parent, 1, &k, -1);
632
633         current_key = k;
634
635         if (!k) return FALSE;
636
637         gtk_list_store_clear(store_vals);
638
639         for(i = 0; W_ERROR_IS_OK(error = reg_key_get_value_by_index(mem_ctx, k, i, &val)); i++) {
640                 GtkTreeIter iter;
641                 gtk_list_store_append(store_vals, &iter);
642                 gtk_list_store_set (store_vals,
643                                             &iter, 
644                                                 0,
645                                                 val->name,
646                                                 1,
647                                                 str_regtype(val->data_type),
648                                                 2,
649                                                 reg_val_data_string(mem_ctx, val),
650                                                 3, 
651                                                 val,
652                                                 -1);
653         }
654
655         if(!W_ERROR_EQUAL(error, WERR_NO_MORE_ITEMS)) {
656                  gtk_show_werror(mainwin, "Error while enumerating values",  error);
657                  return FALSE;
658         }
659         return TRUE;
660 }
661
662 static GtkWidget* create_mainwindow(void)
663 {
664         GtkWidget *vbox1;
665         GtkWidget *menubar;
666         GtkWidget *menu_file;
667         GtkWidget *menu_file_menu;
668         GtkWidget *open_nt4;
669         GtkWidget *open_ldb;
670         GtkWidget *open_w95;
671         GtkWidget *open_gconf;
672         GtkWidget *open_remote;
673         GtkWidget *open_local;
674         GtkWidget *separatormenuitem1;
675         GtkWidget *quit;
676         GtkWidget *men_key;
677         GtkWidget *men_key_menu;
678         GtkCellRenderer *renderer;
679         GtkTreeViewColumn *curcol;
680         GtkWidget *help;
681         GtkWidget *help_menu;
682         GtkWidget *about;
683         GtkWidget *hbox1;
684         GtkWidget *scrolledwindow1;
685         GtkWidget *scrolledwindow2;
686         GtkWidget *statusbar;
687         GtkAccelGroup *accel_group;
688
689         accel_group = gtk_accel_group_new ();
690
691         mainwin = gtk_window_new (GTK_WINDOW_TOPLEVEL);
692         gtk_window_set_title (GTK_WINDOW (mainwin), "Registry editor");
693         gtk_window_set_default_size (GTK_WINDOW (mainwin), 642, 562);
694
695         vbox1 = gtk_vbox_new (FALSE, 0);
696         gtk_container_add (GTK_CONTAINER (mainwin), vbox1);
697
698         menubar = gtk_menu_bar_new ();
699         gtk_box_pack_start (GTK_BOX (vbox1), menubar, FALSE, FALSE, 0);
700
701         menu_file = gtk_menu_item_new_with_mnemonic ("_File");
702         gtk_container_add (GTK_CONTAINER (menubar), menu_file);
703
704         menu_file_menu = gtk_menu_new ();
705         gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_file), menu_file_menu);
706
707         open_local = gtk_menu_item_new_with_mnemonic ("Open _Local");
708         gtk_container_add (GTK_CONTAINER (menu_file_menu), open_local);
709         g_signal_connect ((gpointer) open_local, "activate",
710                                                   G_CALLBACK (on_open_local_activate), NULL);
711
712         if(reg_has_backend("rpc")) {
713                 open_remote = gtk_menu_item_new_with_mnemonic ("Open _Remote");
714                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_remote);
715
716                 g_signal_connect ((gpointer) open_remote, "activate",
717                                                   G_CALLBACK (on_open_remote_activate),
718                                                   NULL);
719         }
720
721         separatormenuitem1 = gtk_menu_item_new ();
722         gtk_container_add (GTK_CONTAINER (menu_file_menu), separatormenuitem1);
723         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
724
725
726         if(reg_has_backend("nt4")) {
727                 open_nt4 = gtk_image_menu_item_new_with_mnemonic("Open _NT4 file");
728                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_nt4);
729
730                 g_signal_connect(open_nt4, "activate",
731                                  G_CALLBACK (on_open_file_activate),
732                                  discard_const_p(char, "nt4"));
733         }
734
735         if(reg_has_backend("w95")) {
736                 open_w95 = gtk_image_menu_item_new_with_mnemonic("Open Win_9x file");
737                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_w95);
738
739                 g_signal_connect (open_w95, "activate",
740                                   G_CALLBACK (on_open_file_activate),
741                                   discard_const_p(char, "w95"));
742         }
743
744         if(reg_has_backend("gconf")) {
745                 open_gconf = gtk_image_menu_item_new_with_mnemonic ("Open _GConf");
746                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_gconf);
747
748                 g_signal_connect ((gpointer) open_gconf, "activate",
749                                                   G_CALLBACK (on_open_gconf_activate),
750                                                   NULL);
751         }
752
753         if(reg_has_backend("ldb")) {
754                 open_ldb = gtk_image_menu_item_new_with_mnemonic("Open _LDB file");
755                 gtk_container_add (GTK_CONTAINER (menu_file_menu), open_ldb);
756
757                 g_signal_connect(open_ldb, "activate",
758                                  G_CALLBACK (on_open_file_activate),
759                                  discard_const_p(char, "ldb"));
760         }
761
762         separatormenuitem1 = gtk_menu_item_new ();
763         gtk_container_add (GTK_CONTAINER (menu_file_menu), separatormenuitem1);
764         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
765
766         save = gtk_image_menu_item_new_from_stock ("gtk-save", accel_group);
767         gtk_widget_set_sensitive( save, False );
768         gtk_container_add (GTK_CONTAINER (menu_file_menu), save);
769
770         save_as = gtk_image_menu_item_new_from_stock ("gtk-save-as", accel_group);
771         gtk_widget_set_sensitive( save_as, False );
772         gtk_container_add (GTK_CONTAINER (menu_file_menu), save_as);
773
774         separatormenuitem1 = gtk_menu_item_new ();
775         gtk_container_add (GTK_CONTAINER (menu_file_menu), separatormenuitem1);
776         gtk_widget_set_sensitive (separatormenuitem1, FALSE);
777
778         quit = gtk_image_menu_item_new_from_stock ("gtk-quit", accel_group);
779         gtk_container_add (GTK_CONTAINER (menu_file_menu), quit);
780
781         men_key = gtk_menu_item_new_with_mnemonic ("_Key");
782         gtk_container_add (GTK_CONTAINER (menubar), men_key);
783
784         men_key_menu = gtk_menu_new ();
785         gtk_menu_item_set_submenu (GTK_MENU_ITEM (men_key), men_key_menu);
786
787         mnu_add_key = gtk_image_menu_item_new_with_mnemonic("Add _Subkey");
788         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_add_key), gtk_image_new_from_stock ("gtk-add", GTK_ICON_SIZE_MENU));
789
790         gtk_widget_set_sensitive(mnu_add_key, False);
791         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_add_key);
792
793         mnu_set_value = gtk_image_menu_item_new_with_mnemonic("Set _Value");
794         gtk_widget_set_sensitive(mnu_set_value, False);
795         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_set_value), gtk_image_new_from_stock ("gtk-add", GTK_ICON_SIZE_MENU));
796         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_set_value);
797
798         mnu_find = gtk_image_menu_item_new_from_stock ("gtk-find", accel_group);
799         gtk_widget_set_sensitive(mnu_find, False);
800         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_find);
801
802         mnu_del_key = gtk_image_menu_item_new_with_mnemonic ("Delete Key"); 
803         gtk_widget_set_sensitive(mnu_del_key, False);
804         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_del_value), gtk_image_new_from_stock ("gtk-delete", GTK_ICON_SIZE_MENU));
805         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_del_key);
806
807         mnu_del_value = gtk_image_menu_item_new_with_mnemonic ("Delete Value"); 
808         gtk_widget_set_sensitive(mnu_del_value, False);
809         gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (mnu_del_value), gtk_image_new_from_stock ("gtk-delete", GTK_ICON_SIZE_MENU));
810         gtk_container_add (GTK_CONTAINER (men_key_menu), mnu_del_value);
811
812
813         help = gtk_menu_item_new_with_mnemonic ("_Help");
814         gtk_container_add (GTK_CONTAINER (menubar), help);
815
816         help_menu = gtk_menu_new ();
817         gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), help_menu);
818
819         about = gtk_menu_item_new_with_mnemonic ("_About");
820         gtk_container_add (GTK_CONTAINER (help_menu), about);
821
822         hbox1 = gtk_hbox_new (FALSE, 0);
823         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
824
825         scrolledwindow1 = gtk_scrolled_window_new (NULL, NULL);
826         gtk_box_pack_start (GTK_BOX (hbox1), scrolledwindow1, TRUE, TRUE, 0);
827
828         tree_keys = gtk_tree_view_new ();
829
830         /* Column names */
831         curcol = gtk_tree_view_column_new ();
832         gtk_tree_view_column_set_title(curcol, "Name");
833         renderer = gtk_cell_renderer_text_new();
834         gtk_tree_view_column_pack_start(curcol, renderer, True);
835
836         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_keys), curcol);
837
838         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
839         gtk_container_add (GTK_CONTAINER (scrolledwindow1), tree_keys);
840         store_keys = gtk_tree_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
841         gtk_tree_view_set_model(GTK_TREE_VIEW(tree_keys), GTK_TREE_MODEL(store_keys));
842         g_object_unref(store_keys);
843
844         gtk_tree_selection_set_select_function (gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_keys)), on_key_activate, NULL, NULL);
845
846         g_signal_connect ((gpointer) tree_keys, "row-expanded",
847                                           G_CALLBACK (expand_key),
848                                           NULL);
849
850
851         scrolledwindow2 = gtk_scrolled_window_new (NULL, NULL);
852         gtk_box_pack_start (GTK_BOX (hbox1), scrolledwindow2, TRUE, TRUE, 0);
853
854         tree_vals = gtk_tree_view_new ();
855         /* Column names */
856
857         curcol = gtk_tree_view_column_new ();
858         gtk_tree_view_column_set_title(curcol, "Name");
859         renderer = gtk_cell_renderer_text_new();
860         gtk_tree_view_column_pack_start(curcol, renderer, True);
861         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_vals), curcol);
862         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 0);
863
864         curcol = gtk_tree_view_column_new ();
865         gtk_tree_view_column_set_title(curcol, "Type");
866         renderer = gtk_cell_renderer_text_new();
867         gtk_tree_view_column_pack_start(curcol, renderer, True);
868         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_vals), curcol);
869         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 1);
870
871         curcol = gtk_tree_view_column_new ();
872         gtk_tree_view_column_set_title(curcol, "Value");
873         renderer = gtk_cell_renderer_text_new();
874         gtk_tree_view_column_pack_start(curcol, renderer, True);
875         gtk_tree_view_append_column(GTK_TREE_VIEW(tree_vals), curcol);
876         gtk_tree_view_column_add_attribute(curcol, renderer, "text", 2);
877
878
879         gtk_container_add (GTK_CONTAINER (scrolledwindow2), tree_vals);
880
881         store_vals = gtk_list_store_new(4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_POINTER);
882         gtk_tree_view_set_model(GTK_TREE_VIEW(tree_vals), GTK_TREE_MODEL(store_vals));
883         g_object_unref(store_vals);
884
885         statusbar = gtk_statusbar_new ();
886         gtk_box_pack_start (GTK_BOX (vbox1), statusbar, FALSE, FALSE, 0);
887         gtk_statusbar_set_has_resize_grip (GTK_STATUSBAR (statusbar), FALSE);
888
889         g_signal_connect ((gpointer) save, "activate",
890                                           G_CALLBACK (on_save_activate),
891                                           NULL);
892         g_signal_connect ((gpointer) save_as, "activate",
893                                           G_CALLBACK (on_save_as_activate),
894                                           NULL);
895         g_signal_connect ((gpointer) quit, "activate",
896                                           G_CALLBACK (on_quit_activate),
897                                           NULL);
898         g_signal_connect ((gpointer) mnu_add_key, "activate",
899                                           G_CALLBACK (on_add_key_activate),
900                                           NULL);
901         g_signal_connect ((gpointer) mnu_set_value, "activate",
902                                           G_CALLBACK (on_set_value_activate),
903                                           NULL);
904         g_signal_connect ((gpointer) mnu_find, "activate",
905                                           G_CALLBACK (on_find_activate),
906                                           NULL);
907         g_signal_connect ((gpointer) mnu_del_key, "activate",
908                                           G_CALLBACK (on_delete_key_activate),
909                                           NULL);
910         g_signal_connect ((gpointer) mnu_del_value, "activate",
911                                           G_CALLBACK (on_delete_value_activate),
912                                           NULL);
913         g_signal_connect ((gpointer) about, "activate",
914                                           G_CALLBACK (on_about_activate),
915                                           NULL);
916
917         g_signal_connect ((gpointer) tree_vals, "row-activated",
918                                           G_CALLBACK (on_value_activate),
919                                           NULL);
920
921
922         gtk_window_add_accel_group (GTK_WINDOW (mainwin), accel_group);
923
924         return mainwin;
925 }
926
927 static GtkWidget* create_openfilewin (void)
928 {
929         GtkWidget *openfilewin;
930         GtkWidget *ok_button;
931         GtkWidget *cancel_button;
932
933         openfilewin = gtk_file_selection_new ("Select File");
934         gtk_container_set_border_width (GTK_CONTAINER (openfilewin), 10);
935
936         ok_button = GTK_FILE_SELECTION (openfilewin)->ok_button;
937         GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
938
939         cancel_button = GTK_FILE_SELECTION (openfilewin)->cancel_button;
940         GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
941
942         return openfilewin;
943 }
944
945 static GtkWidget* create_savefilewin (void)
946 {
947         GtkWidget *savefilewin;
948         GtkWidget *ok_button;
949         GtkWidget *cancel_button;
950
951         savefilewin = gtk_file_selection_new ("Select File");
952         gtk_container_set_border_width (GTK_CONTAINER (savefilewin), 10);
953
954         ok_button = GTK_FILE_SELECTION (savefilewin)->ok_button;
955         GTK_WIDGET_SET_FLAGS (ok_button, GTK_CAN_DEFAULT);
956
957         cancel_button = GTK_FILE_SELECTION (savefilewin)->cancel_button;
958         GTK_WIDGET_SET_FLAGS (cancel_button, GTK_CAN_DEFAULT);
959
960         return savefilewin;
961 }
962
963 static int gregedit_load_defaults(void)
964 {
965         WERROR error = reg_open_local(&registry);
966         if(!W_ERROR_IS_OK(error)) {
967                 gtk_show_werror(mainwin, "Error while loading local registry", error);
968                 return -1;
969         }
970         registry_load_root();
971
972         return 0;
973 }
974
975 int main(int argc, char *argv[])
976 {
977         int ret;
978
979         gregedit_init_subsystems;
980         lp_load();
981         load_interfaces();
982         setup_logging(argv[0], DEBUG_STDERR);
983
984         mem_ctx = talloc_init("gregedit");
985
986         gtk_init(&argc, &argv);
987         mainwin = create_mainwindow();
988         gtk_widget_show_all(mainwin);
989
990         ret = gregedit_load_defaults();
991         if (ret != 0) goto failed;
992
993         ret = gtk_event_loop();
994
995 failed:
996         talloc_free(mem_ctx);
997         return ret;
998 }