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