| 1 | #!/usr/local/bin/perl | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | use Glib ':constants'; | ||
| 6 | use Gtk2 -init; | ||
| 7 | |||
| 8 | my $window_width = 600; | ||
| 9 | my $window_height = 400; | ||
| 10 | |||
| 11 | use File::Basename; | ||
| 12 | use File::Spec; | ||
| 13 | use Parse::Win32Registry 0.51 qw( make_multiple_subtree_iterator | ||
| 14 | make_multiple_subkey_iterator | ||
| 15 | make_multiple_value_iterator | ||
| 16 | compare_multiple_keys | ||
| 17 | compare_multiple_values | ||
| 18 | hexdump ); | ||
| 19 | |||
| 20 | binmode(STDOUT, ':utf8'); | ||
| 21 | |||
| 22 | my $script_name = basename $0; | ||
| 23 | |||
| 24 | ### REGISTRY FILE STORE | ||
| 25 | |||
| 26 | use constant { | ||
| 27 | REGCOL_FILENAME => 0, | ||
| 28 | REGCOL_EMBEDDED_FILENAME => 1, | ||
| 29 | REGCOL_TIMESTAMP => 2, | ||
| 30 | REGCOL_DIRECTORY => 3, | ||
| 31 | REGCOL_REGISTRY => 4, | ||
| 32 | }; | ||
| 33 | |||
| 34 | my $registry_store = Gtk2::ListStore->new( | ||
| 35 | 'Glib::String', 'Glib::String', 'Glib::String', 'Glib::String', | ||
| 36 | 'Glib::Scalar', | ||
| 37 | ); | ||
| 38 | |||
| 39 | ### TREE STORE | ||
| 40 | |||
| 41 | use constant { | ||
| 42 | TREECOL_NAME => 0, | ||
| 43 | TREECOL_CHANGES => 1, | ||
| 44 | TREECOL_KEYS => 2, | ||
| 45 | TREECOL_VALUES => 3, | ||
| 46 | TREECOL_ICON => 4, | ||
| 47 | }; | ||
| 48 | |||
| 49 | my $tree_store = Gtk2::TreeStore->new( | ||
| 50 | 'Glib::String', 'Glib::Scalar', 'Glib::Scalar', 'Glib::Scalar', | ||
| 51 | 'Glib::String', | ||
| 52 | ); | ||
| 53 | |||
| 54 | my $tree_view = Gtk2::TreeView->new($tree_store); | ||
| 55 | |||
| 56 | my $icon_cell = Gtk2::CellRendererPixbuf->new; | ||
| 57 | my $name_cell = Gtk2::CellRendererText->new; | ||
| 58 | my $tree_column0 = Gtk2::TreeViewColumn->new; | ||
| 59 | $tree_column0->set_title('Name'); | ||
| 60 | $tree_column0->pack_start($icon_cell, FALSE); | ||
| 61 | $tree_column0->pack_start($name_cell, TRUE); | ||
| 62 | $tree_column0->set_attributes($icon_cell, | ||
| 63 | 'stock-id', TREECOL_ICON); | ||
| 64 | $tree_column0->set_attributes($name_cell, | ||
| 65 | 'text', TREECOL_NAME); | ||
| 66 | $tree_view->append_column($tree_column0); | ||
| 67 | $tree_column0->set_resizable(TRUE); | ||
| 68 | |||
| 69 | $tree_view->set_rules_hint(TRUE); | ||
| 70 | |||
| 71 | # row-expanded when row is expanded (e.g. after user clicks on arrow) | ||
| 72 | $tree_view->signal_connect('row-expanded' => \&expand_row); | ||
| 73 | $tree_view->signal_connect('row-collapsed' => \&collapse_row); | ||
| 74 | # row-activated when user double clicks on row | ||
| 75 | $tree_view->signal_connect('row-activated' => \&activate_row); | ||
| 76 | |||
| 77 | my $tree_selection = $tree_view->get_selection; | ||
| 78 | $tree_selection->set_mode('browse'); | ||
| 79 | $tree_selection->signal_connect('changed' => \&tree_item_selected); | ||
| 80 | |||
| 81 | my $scrolled_tree_view = Gtk2::ScrolledWindow->new; | ||
| 82 | $scrolled_tree_view->set_policy('automatic', 'automatic'); | ||
| 83 | $scrolled_tree_view->set_shadow_type('in'); | ||
| 84 | $scrolled_tree_view->add($tree_view); | ||
| 85 | |||
| 86 | ### LIST STORE | ||
| 87 | |||
| 88 | use constant { | ||
| 89 | LISTCOL_FILENUM => 0, | ||
| 90 | LISTCOL_CHANGE => 1, | ||
| 91 | LISTCOL_ITEM_STRING => 2, | ||
| 92 | LISTCOL_ITEM => 3, | ||
| 93 | LISTCOL_ICON => 4, | ||
| 94 | }; | ||
| 95 | |||
| 96 | my $list_store = Gtk2::ListStore->new( | ||
| 97 | 'Glib::String', 'Glib::String', 'Glib::String', 'Glib::Scalar', | ||
| 98 | 'Glib::String', | ||
| 99 | ); | ||
| 100 | |||
| 101 | my $list_view = Gtk2::TreeView->new($list_store); | ||
| 102 | |||
| 103 | my $list_cell0 = Gtk2::CellRendererText->new; | ||
| 104 | my $list_column0 = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 105 | '', $list_cell0, | ||
| 106 | 'text', LISTCOL_FILENUM); | ||
| 107 | $list_view->append_column($list_column0); | ||
| 108 | |||
| 109 | my $list_cell2 = Gtk2::CellRendererText->new; | ||
| 110 | my $list_column2 = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 111 | 'Change', $list_cell2, | ||
| 112 | 'text', LISTCOL_CHANGE); | ||
| 113 | $list_view->append_column($list_column2); | ||
| 114 | $list_column2->set_resizable(TRUE); | ||
| 115 | |||
| 116 | my $list_icon_cell = Gtk2::CellRendererPixbuf->new; | ||
| 117 | my $list_item_cell = Gtk2::CellRendererText->new; | ||
| 118 | my $list_column3 = Gtk2::TreeViewColumn->new; | ||
| 119 | $list_column3->pack_start($list_icon_cell, FALSE); | ||
| 120 | $list_column3->pack_start($list_item_cell, TRUE); | ||
| 121 | $list_column3->set_attributes($list_icon_cell, | ||
| 122 | 'stock-id', LISTCOL_ICON); | ||
| 123 | $list_column3->set_attributes($list_item_cell, | ||
| 124 | 'text', LISTCOL_ITEM_STRING); | ||
| 125 | $list_view->append_column($list_column3); | ||
| 126 | $list_column3->set_resizable(TRUE); | ||
| 127 | $list_item_cell->set('ellipsize', 'end'); | ||
| 128 | |||
| 129 | $list_view->set_rules_hint(TRUE); | ||
| 130 | $list_view->set_headers_visible(FALSE); | ||
| 131 | |||
| 132 | my $list_selection = $list_view->get_selection; | ||
| 133 | $list_selection->set_mode('browse'); | ||
| 134 | $list_selection->signal_connect('changed' => \&list_item_selected); | ||
| 135 | |||
| 136 | my $scrolled_list_view = Gtk2::ScrolledWindow->new; | ||
| 137 | $scrolled_list_view->set_policy('automatic', 'automatic'); | ||
| 138 | $scrolled_list_view->set_shadow_type('in'); | ||
| 139 | $scrolled_list_view->add($list_view); | ||
| 140 | |||
| 141 | ### TEXT VIEW | ||
| 142 | |||
| 143 | my $text_view = Gtk2::TextView->new; | ||
| 144 | $text_view->set_editable(FALSE); | ||
| 145 | $text_view->modify_font(Gtk2::Pango::FontDescription->from_string('monospace')); | ||
| 146 | |||
| 147 | my $text_buffer = $text_view->get_buffer; | ||
| 148 | |||
| 149 | my $scrolled_text_view = Gtk2::ScrolledWindow->new; | ||
| 150 | $scrolled_text_view->set_policy('automatic', 'automatic'); | ||
| 151 | $scrolled_text_view->set_shadow_type('in'); | ||
| 152 | $scrolled_text_view->add($text_view); | ||
| 153 | |||
| 154 | ### VPANED | ||
| 155 | |||
| 156 | my $vpaned2 = Gtk2::VPaned->new; | ||
| 157 | $vpaned2->pack1($scrolled_list_view, FALSE, FALSE); | ||
| 158 | $vpaned2->pack2($scrolled_text_view, FALSE, FALSE); | ||
| 159 | |||
| 160 | ### VPANED | ||
| 161 | |||
| 162 | my $vpaned1 = Gtk2::VPaned->new; | ||
| 163 | $vpaned1->pack1($scrolled_tree_view, FALSE, FALSE); | ||
| 164 | $vpaned1->pack2($vpaned2, FALSE, FALSE); | ||
| 165 | |||
| 166 | ### UIMANAGER | ||
| 167 | |||
| 168 | my $uimanager = Gtk2::UIManager->new; | ||
| 169 | |||
| 170 | my @actions = ( | ||
| 171 | # name, stock id, label | ||
| 172 | ['FileMenu', undef, '_File'], | ||
| 173 | ['EditMenu', undef, '_Edit'], | ||
| 174 | ['SearchMenu', undef, '_Search'], | ||
| 175 | ['ViewMenu', undef, '_View'], | ||
| 176 | ['HelpMenu', undef, '_Help'], | ||
| 177 | # name, stock-id, label, accelerator, tooltip, callback | ||
| 178 | ['Open', 'gtk-open', '_Select Files...', '<control>O', undef, \&open_files], | ||
| 179 | ['Close', 'gtk-close', '_Close Files', '<control>W', undef, \&close_files], | ||
| 180 | ['Quit', 'gtk-quit', '_Quit', '<control>Q', undef, \&quit], | ||
| 181 | ['Copy', 'gtk-copy', '_Copy Path', '<control>C', undef, \©_path], | ||
| 182 | ['Find', 'gtk-find', '_Find...', '<control>F', undef, \&find], | ||
| 183 | ['FindNext', undef, 'Find _Next', '<control>G', undef, \&find_next], | ||
| 184 | ['FindNext2', undef, 'Find Next', 'F3', undef, \&find_next], | ||
| 185 | ['FindChange', 'gtk-find-and-replace', 'Find _Change...', '<control>N', undef, \&find_change], | ||
| 186 | ['FindNextChange', undef, 'Find N_ext Change', '<control>M', undef, \&find_next_change], | ||
| 187 | ['FindNextChange2', undef, 'Find Next Change', 'F4', undef, \&find_next_change], | ||
| 188 | ['About', 'gtk-about', '_About...', undef, undef, \&about], | ||
| 189 | ); | ||
| 190 | |||
| 191 | my $default_actions = Gtk2::ActionGroup->new('actions'); | ||
| 192 | $default_actions->add_actions(\@actions, undef); | ||
| 193 | |||
| 194 | my @toggle_actions = ( | ||
| 195 | # name, stock id, label, accelerator, tooltip, callback, active | ||
| 196 | ['ShowDetail', 'gtk-edit', 'Show _Detail', '<control>D', undef, \&toggle_item_detail, TRUE], | ||
| 197 | ); | ||
| 198 | $default_actions->add_toggle_actions(\@toggle_actions, undef); | ||
| 199 | |||
| 200 | $uimanager->insert_action_group($default_actions, 0); | ||
| 201 | |||
| 202 | my $ui_info = <<END_OF_UI; | ||
| 203 | <ui> | ||
| 204 | <menubar name='MenuBar'> | ||
| 205 | <menu action='FileMenu'> | ||
| 206 | <menuitem action='Open'/> | ||
| 207 | <menuitem action='Close'/> | ||
| 208 | <separator/> | ||
| 209 | <menuitem action='Quit'/> | ||
| 210 | </menu> | ||
| 211 | <menu action='EditMenu'> | ||
| 212 | <menuitem action='Copy'/> | ||
| 213 | </menu> | ||
| 214 | <menu action='SearchMenu'> | ||
| 215 | <menuitem action='Find'/> | ||
| 216 | <menuitem action='FindNext'/> | ||
| 217 | <separator/> | ||
| 218 | <menuitem action='FindChange'/> | ||
| 219 | <menuitem action='FindNextChange'/> | ||
| 220 | </menu> | ||
| 221 | <menu action='ViewMenu'> | ||
| 222 | <menuitem action='ShowDetail'/> | ||
| 223 | <separator/> | ||
| 224 | </menu> | ||
| 225 | <menu action='HelpMenu'> | ||
| 226 | <menuitem action='About'/> | ||
| 227 | </menu> | ||
| 228 | </menubar> | ||
| 229 | <accelerator action='FindNext2'/> | ||
| 230 | <accelerator action='FindNextChange2'/> | ||
| 231 | </ui> | ||
| 232 | END_OF_UI | ||
| 233 | |||
| 234 | $uimanager->add_ui_from_string($ui_info); | ||
| 235 | |||
| 236 | my $menubar = $uimanager->get_widget('/MenuBar'); | ||
| 237 | |||
| 238 | ### STATUSBAR | ||
| 239 | |||
| 240 | my $statusbar = Gtk2::Statusbar->new; | ||
| 241 | |||
| 242 | ### VBOX | ||
| 243 | |||
| 244 | my $main_vbox = Gtk2::VBox->new(FALSE, 0); | ||
| 245 | $main_vbox->pack_start($menubar, FALSE, FALSE, 0); | ||
| 246 | $main_vbox->pack_start($vpaned1, TRUE, TRUE, 0); | ||
| 247 | $main_vbox->pack_start($statusbar, FALSE, FALSE, 0); | ||
| 248 | |||
| 249 | ### WINDOW | ||
| 250 | |||
| 251 | my $window = Gtk2::Window->new; | ||
| 252 | $window->set_default_size($window_width, $window_height); | ||
| 253 | $window->set_position('center'); | ||
| 254 | $window->signal_connect(destroy => sub { Gtk2->main_quit }); | ||
| 255 | $window->add($main_vbox); | ||
| 256 | $window->add_accel_group($uimanager->get_accel_group); | ||
| 257 | $window->set_title($script_name); | ||
| 258 | $window->show_all; | ||
| 259 | |||
| 260 | ############################################################################### | ||
| 261 | |||
| 262 | sub build_open_files_dialog { | ||
| 263 | my $registry_view = Gtk2::TreeView->new($registry_store); | ||
| 264 | $registry_view->set_reorderable(TRUE); | ||
| 265 | |||
| 266 | my $registry_column0 = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 267 | 'Filename', Gtk2::CellRendererText->new, | ||
| 268 | 'text', REGCOL_FILENAME); | ||
| 269 | $registry_view->append_column($registry_column0); | ||
| 270 | $registry_column0->set_resizable(TRUE); | ||
| 271 | |||
| 272 | my $registry_column1 = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 273 | 'Embedded Filename', Gtk2::CellRendererText->new, | ||
| 274 | 'text', REGCOL_EMBEDDED_FILENAME); | ||
| 275 | $registry_view->append_column($registry_column1); | ||
| 276 | $registry_column1->set_resizable(TRUE); | ||
| 277 | |||
| 278 | my $registry_column2 = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 279 | 'Embedded Timestamp', Gtk2::CellRendererText->new, | ||
| 280 | 'text', REGCOL_TIMESTAMP); | ||
| 281 | $registry_view->append_column($registry_column2); | ||
| 282 | $registry_column2->set_resizable(TRUE); | ||
| 283 | |||
| 284 | my $registry_column3 = Gtk2::TreeViewColumn->new_with_attributes( | ||
| 285 | 'Directory', Gtk2::CellRendererText->new, | ||
| 286 | 'text', REGCOL_DIRECTORY); | ||
| 287 | $registry_view->append_column($registry_column3); | ||
| 288 | $registry_column3->set_resizable(TRUE); | ||
| 289 | |||
| 290 | my $scrolled_registry_view = Gtk2::ScrolledWindow->new; | ||
| 291 | $scrolled_registry_view->set_policy('automatic', 'automatic'); | ||
| 292 | $scrolled_registry_view->set_shadow_type('in'); | ||
| 293 | $scrolled_registry_view->add($registry_view); | ||
| 294 | |||
| 295 | my $selection = $registry_view->get_selection; | ||
| 296 | $selection->set_mode('multiple'); | ||
| 297 | |||
| 298 | my $label = Gtk2::Label->new; | ||
| 299 | $label->set_markup('<i>Drag files to reorder them</i>'); | ||
| 300 | |||
| 301 | my $dialog = Gtk2::Dialog->new('Select Registry Files', $window, 'modal', | ||
| 302 | 'gtk-clear' => 70, | ||
| 303 | 'gtk-add' => 60, | ||
| 304 | 'gtk-remove' => 50, | ||
| 305 | 'gtk-cancel' => 'cancel', | ||
| 306 | 'gtk-ok' => 'ok', | ||
| 307 | ); | ||
| 308 | $dialog->set_size_request($window_width * 1, $window_height * 0.8); | ||
| 309 | $dialog->vbox->pack_start($scrolled_registry_view, TRUE, TRUE, 0); | ||
| 310 | $dialog->vbox->pack_start($label, FALSE, FALSE, 5); | ||
| 311 | $dialog->set_default_response('ok'); | ||
| 312 | |||
| 313 | $dialog->signal_connect(delete_event => sub { | ||
| 314 | $dialog->hide; | ||
| 315 | return TRUE; | ||
| 316 | }); | ||
| 317 | $dialog->signal_connect(response => sub { | ||
| 318 | my ($dialog, $response) = @_; | ||
| 319 | if ($response eq '70') { | ||
| 320 | $registry_store->clear; | ||
| 321 | } | ||
| 322 | elsif ($response eq '60') { | ||
| 323 | my @filenames = choose_files(); | ||
| 324 | foreach my $filename (@filenames) { | ||
| 325 | my ($name, $path) = fileparse($filename); | ||
| 326 | if (my $registry = Parse::Win32Registry->new($filename)) { | ||
| 327 | if (my $root_key = $registry->get_root_key) { | ||
| 328 | add_registry($registry); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | else { | ||
| 332 | show_message('error', "'$name' is not a registry file."); | ||
| 333 | } | ||
| 334 | } | ||
| 335 | } | ||
| 336 | elsif ($response eq '50') { | ||
| 337 | my $selection = $registry_view->get_selection; | ||
| 338 | my @paths = $selection->get_selected_rows; | ||
| 339 | my @iters = map { $registry_store->get_iter($_) } @paths; | ||
| 340 | foreach my $iter (@iters) { | ||
| 341 | $registry_store->remove($iter); | ||
| 342 | } | ||
| 343 | } | ||
| 344 | elsif ($response eq 'ok') { | ||
| 345 | $dialog->hide; | ||
| 346 | compare_files(); | ||
| 347 | } | ||
| 348 | else { | ||
| 349 | $dialog->hide; | ||
| 350 | } | ||
| 351 | }); | ||
| 352 | |||
| 353 | return $dialog; | ||
| 354 | } | ||
| 355 | |||
| 356 | my $open_files_dialog = build_open_files_dialog; | ||
| 357 | |||
| 358 | ######################## GLOBAL SETUP | ||
| 359 | |||
| 360 | my @registries = (); | ||
| 361 | my @root_keys = (); | ||
| 362 | |||
| 363 | my $last_dir; | ||
| 364 | |||
| 365 | my $search_keys = TRUE; | ||
| 366 | my $search_values = TRUE; | ||
| 367 | my $search_selected = 0; | ||
| 368 | my $find_param = ''; | ||
| 369 | my $find_iter; | ||
| 370 | my $change_iter; | ||
| 371 | |||
| 372 | if (@ARGV) { | ||
| 373 | my @filenames = (); | ||
| 374 | while (my $filename = shift) { | ||
| 375 | push @filenames, $filename if -r $filename; | ||
| 376 | } | ||
| 377 | @filenames = map { File::Spec->rel2abs($_) } @filenames; | ||
| 378 | foreach my $filename (@filenames) { | ||
| 379 | if (my $registry = Parse::Win32Registry->new($filename)) { | ||
| 380 | if (my $root_key = $registry->get_root_key) { | ||
| 381 | add_registry($registry); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | } | ||
| 385 | } | ||
| 386 | compare_files(); | ||
| 387 | |||
| 388 | Gtk2->main; | ||
| 389 | |||
| 390 | ############################################################################### | ||
| 391 | |||
| 392 | sub expand_row { | ||
| 393 | my ($view, $iter, $path) = @_; | ||
| 394 | my $model = $view->get_model; | ||
| 395 | |||
| 396 | # check that this is a key | ||
| 397 | my $icon = $model->get($iter, TREECOL_ICON); | ||
| 398 | if ($icon eq 'gtk-file') { | ||
| 399 | return; | ||
| 400 | } | ||
| 401 | |||
| 402 | my $keys = $model->get($iter, TREECOL_KEYS); | ||
| 403 | my $first_child_iter = $model->iter_nth_child($iter, 0); | ||
| 404 | # add children if not already present | ||
| 405 | if (!defined $model->get($first_child_iter, 0)) { | ||
| 406 | add_children($keys, $model, $iter); | ||
| 407 | $model->remove($first_child_iter); | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | sub collapse_row { | ||
| 412 | my ($view, $iter, $path) = @_; | ||
| 413 | } | ||
| 414 | |||
| 415 | sub activate_row { | ||
| 416 | my ($view, $path, $column) = @_; | ||
| 417 | if ($view->row_expanded($path)) { | ||
| 418 | $view->collapse_row($path); | ||
| 419 | } | ||
| 420 | else { | ||
| 421 | $view->expand_row($path, FALSE); | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | sub toggle_item_detail { | ||
| 426 | my ($toggle_action) = @_; | ||
| 427 | if ($toggle_action->get_active) { | ||
| 428 | $scrolled_text_view->show; | ||
| 429 | } | ||
| 430 | else { | ||
| 431 | $scrolled_text_view->hide; | ||
| 432 | } | ||
| 433 | } | ||
| 434 | |||
| 435 | sub tree_item_selected { | ||
| 436 | my ($tree_selection) = @_; | ||
| 437 | |||
| 438 | my ($model, $iter) = $tree_selection->get_selected; | ||
| 439 | if (!defined $model || !defined $iter) { | ||
| 440 | return; | ||
| 441 | } | ||
| 442 | |||
| 443 | my $changes = $model->get($iter, TREECOL_CHANGES); | ||
| 444 | my $keys = $model->get($iter, TREECOL_KEYS); | ||
| 445 | my $values = $model->get($iter, TREECOL_VALUES); | ||
| 446 | |||
| 447 | $list_store->clear; | ||
| 448 | $text_buffer->set_text(''); | ||
| 449 | |||
| 450 | my $batch_size = @root_keys; | ||
| 451 | |||
| 452 | if (defined $changes) { | ||
| 453 | for (my $num = 0; $num < $batch_size; $num++) { | ||
| 454 | my $item_as_string = ''; | ||
| 455 | my $item = ''; | ||
| 456 | my $icon = ''; | ||
| 457 | if (defined $values) { # values | ||
| 458 | my $key = $keys->[$num]; | ||
| 459 | my $value = $values->[$num]; | ||
| 460 | if (defined $value) { | ||
| 461 | $item_as_string = $value->as_string; | ||
| 462 | $item = $value; | ||
| 463 | $icon = 'gtk-file'; | ||
| 464 | } | ||
| 465 | } | ||
| 466 | else { # keys | ||
| 467 | my $key = $keys->[$num]; | ||
| 468 | if (defined $key) { | ||
| 469 | $item_as_string = $key->as_string; | ||
| 470 | $item = $key; | ||
| 471 | $icon = 'gtk-directory'; | ||
| 472 | } | ||
| 473 | } | ||
| 474 | $item_as_string = substr($item_as_string, 0, 500); | ||
| 475 | $item_as_string =~ s/\0/[NUL]/g; | ||
| 476 | my $iter = $list_store->append; | ||
| 477 | $list_store->set($iter, | ||
| 478 | LISTCOL_FILENUM, "[$num]", | ||
| 479 | LISTCOL_CHANGE, $changes->[$num], | ||
| 480 | LISTCOL_ITEM_STRING, $item_as_string, | ||
| 481 | LISTCOL_ITEM, $item, | ||
| 482 | LISTCOL_ICON, $icon); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | else { | ||
| 486 | my $item_as_string = ''; | ||
| 487 | my $item = ''; | ||
| 488 | my $icon = ''; | ||
| 489 | if (defined $values) { # values | ||
| 490 | my $any_value = (grep { defined } @$values)[0]; | ||
| 491 | $item_as_string = $any_value->as_string; | ||
| 492 | $item = $any_value; | ||
| 493 | $icon = 'gtk-file'; | ||
| 494 | } | ||
| 495 | else { # keys | ||
| 496 | my $any_key = (grep { defined } @$keys)[0]; | ||
| 497 | $item_as_string = $any_key->as_string; | ||
| 498 | $item = $any_key; | ||
| 499 | $icon = 'gtk-directory'; | ||
| 500 | } | ||
| 501 | $item_as_string = substr($item_as_string, 0, 500); | ||
| 502 | $item_as_string =~ s/\0/[NUL]/g; | ||
| 503 | my $iter = $list_store->append; | ||
| 504 | $list_store->set($iter, | ||
| 505 | LISTCOL_FILENUM, "[*]", | ||
| 506 | LISTCOL_CHANGE, "", | ||
| 507 | LISTCOL_ITEM_STRING, $item_as_string, | ||
| 508 | LISTCOL_ITEM, $item, | ||
| 509 | LISTCOL_ICON, $icon); | ||
| 510 | } | ||
| 511 | |||
| 512 | |||
| 513 | my $status = ''; | ||
| 514 | my $any_key = (grep { defined } @$keys)[0]; | ||
| 515 | my $key_path = $any_key->get_path; | ||
| 516 | if (defined $values) { | ||
| 517 | my $any_value = (grep { defined } @$values)[0]; | ||
| 518 | my $name = $any_value->get_name; | ||
| 519 | $name = "(Default)" if $name eq ''; | ||
| 520 | $status = "$key_path, $name"; | ||
| 521 | } | ||
| 522 | else { | ||
| 523 | $status = $key_path; | ||
| 524 | } | ||
| 525 | $status =~ s/\0/[NUL]/g; | ||
| 526 | $statusbar->pop(0); | ||
| 527 | $statusbar->push(0, $status); | ||
| 528 | } | ||
| 529 | |||
| 530 | sub list_item_selected { | ||
| 531 | my ($list_selection) = @_; | ||
| 532 | |||
| 533 | my ($model, $iter) = $list_selection->get_selected; | ||
| 534 | if (!defined $model || !defined $iter) { | ||
| 535 | return; | ||
| 536 | } | ||
| 537 | |||
| 538 | my $item = $model->get($iter, LISTCOL_ITEM); | ||
| 539 | my $icon = $model->get($iter, LISTCOL_ICON); | ||
| 540 | # there will be no item/icon for deleted items | ||
| 541 | |||
| 542 | my $str = ''; | ||
| 543 | if (defined $item) { | ||
| 544 | if ($icon eq 'gtk-file') { # item is a value | ||
| 545 | $str .= hexdump($item->get_raw_data); | ||
| 546 | } | ||
| 547 | elsif ($icon eq 'gtk-directory') { # item is a key | ||
| 548 | my $security = $item->get_security; | ||
| 549 | if (defined $security) { | ||
| 550 | my $sd = $security->get_security_descriptor; | ||
| 551 | $str .= $sd->as_stanza; | ||
| 552 | } | ||
| 553 | } | ||
| 554 | } | ||
| 555 | $text_buffer->set_text($str); | ||
| 556 | } | ||
| 557 | |||
| 558 | sub compare_files { | ||
| 559 | close_files(); # will clear @root_keys | ||
| 560 | |||
| 561 | # Set up global variables: @registries, @root_keys | ||
| 562 | @registries = (); | ||
| 563 | my $iter = $registry_store->get_iter_first; | ||
| 564 | while (defined $iter) { | ||
| 565 | my $registry = $registry_store->get($iter, REGCOL_REGISTRY); | ||
| 566 | push @registries, $registry; | ||
| 567 | $iter = $registry_store->iter_next($iter); | ||
| 568 | } | ||
| 569 | @root_keys = map { $_->get_root_key } @registries; | ||
| 570 | |||
| 571 | if (@registries > 0) { | ||
| 572 | my $filename = $registries[0]->get_filename; | ||
| 573 | my $basename = basename($filename); | ||
| 574 | $basename .= ',...' if @registries > 1; | ||
| 575 | $window->set_title("$basename - $script_name"); | ||
| 576 | } | ||
| 577 | else { | ||
| 578 | $window->set_title($script_name); | ||
| 579 | } | ||
| 580 | |||
| 581 | my $batch_size = @root_keys; | ||
| 582 | |||
| 583 | # Create columns with a custom function to display changes | ||
| 584 | for (my $num = 0; $num < $batch_size; $num++) { | ||
| 585 | $tree_view->insert_column_with_data_func( | ||
| 586 | $num + 1, | ||
| 587 | "[$num]", | ||
| 588 | Gtk2::CellRendererText->new, | ||
| 589 | sub { | ||
| 590 | my ($column, $cell, $model, $iter, $num) = @_; | ||
| 591 | my $changes = $model->get($iter, TREECOL_CHANGES); | ||
| 592 | if (defined $changes) { | ||
| 593 | my $diff = substr($changes->[$num], 0, 1); | ||
| 594 | $cell->set('text', $diff || "\x{00bb}"); | ||
| 595 | } | ||
| 596 | else { | ||
| 597 | $cell->set('text', "\x{00b7}"); | ||
| 598 | } | ||
| 599 | }, | ||
| 600 | $num, # additional data is passed to callback | ||
| 601 | ); | ||
| 602 | } | ||
| 603 | |||
| 604 | add_root(\@root_keys, $tree_store, undef); | ||
| 605 | } | ||
| 606 | |||
| 607 | sub add_root { | ||
| 608 | my ($items, $model, $parent_iter) = @_; | ||
| 609 | |||
| 610 | my @root_keys = @$items; | ||
| 611 | |||
| 612 | return if @root_keys == 0; | ||
| 613 | |||
| 614 | my $any_root_key = (grep { defined } @root_keys)[0]; | ||
| 615 | my $key_name = $any_root_key->get_name; | ||
| 616 | $key_name =~ s/\0/[NUL]/g; | ||
| 617 | |||
| 618 | my @changes = compare_multiple_keys(@root_keys); | ||
| 619 | my $num_changes = grep { $_ } @changes; | ||
| 620 | |||
| 621 | my $iter = $model->append($parent_iter); | ||
| 622 | if ($num_changes > 0) { | ||
| 623 | $model->set($iter, | ||
| 624 | TREECOL_NAME, $key_name, | ||
| 625 | TREECOL_CHANGES, \@changes, | ||
| 626 | TREECOL_KEYS, \@root_keys, | ||
| 627 | TREECOL_ICON, 'gtk-directory'); | ||
| 628 | } | ||
| 629 | else { | ||
| 630 | $model->set($iter, | ||
| 631 | TREECOL_NAME, $key_name, | ||
| 632 | #TREECOL_CHANGES, \@changes, | ||
| 633 | TREECOL_KEYS, \@root_keys, | ||
| 634 | TREECOL_ICON, 'gtk-directory'); | ||
| 635 | } | ||
| 636 | my $dummy = $model->append($iter); # placeholder for children | ||
| 637 | } | ||
| 638 | |||
| 639 | sub add_children { | ||
| 640 | my ($keys, $model, $parent_iter) = @_; | ||
| 641 | |||
| 642 | my @keys = @$keys; | ||
| 643 | |||
| 644 | my $subkeys_iter = make_multiple_subkey_iterator(@keys); | ||
| 645 | |||
| 646 | while (defined(my $subkeys = $subkeys_iter->get_next)) { | ||
| 647 | my @changes = compare_multiple_keys(@$subkeys); | ||
| 648 | my $num_changes = grep { $_ } @changes; | ||
| 649 | # insert a 'blank' change for missing subkeys | ||
| 650 | for (my $i = 0; $i < @changes; $i++) { | ||
| 651 | if ($changes[$i] eq '' && !defined $subkeys->[$i]) { | ||
| 652 | $changes[$i] = ' '; | ||
| 653 | } | ||
| 654 | } | ||
| 655 | |||
| 656 | my $any_subkey = (grep { defined } @$subkeys)[0]; | ||
| 657 | my $key_name = $any_subkey->get_name; | ||
| 658 | $key_name =~ s/\0/[NUL]/g; | ||
| 659 | |||
| 660 | my $iter = $model->append($parent_iter); | ||
| 661 | |||
| 662 | if ($num_changes > 0) { | ||
| 663 | $model->set($iter, | ||
| 664 | TREECOL_NAME, $key_name, | ||
| 665 | TREECOL_CHANGES, \@changes, | ||
| 666 | TREECOL_KEYS, $subkeys, | ||
| 667 | TREECOL_ICON, 'gtk-directory'); | ||
| 668 | } | ||
| 669 | else { | ||
| 670 | $model->set($iter, | ||
| 671 | TREECOL_NAME, $key_name, | ||
| 672 | #TREECOL_CHANGES, \@changes, | ||
| 673 | TREECOL_KEYS, $subkeys, | ||
| 674 | TREECOL_ICON, 'gtk-directory'); | ||
| 675 | } | ||
| 676 | my $dummy = $model->append($iter); # placeholder for children | ||
| 677 | } | ||
| 678 | |||
| 679 | my $values_iter = make_multiple_value_iterator(@keys); | ||
| 680 | |||
| 681 | while (defined(my $values = $values_iter->get_next)) { | ||
| 682 | my @changes = compare_multiple_values(@$values); | ||
| 683 | my $num_changes = grep { $_ } @changes; | ||
| 684 | # insert a 'blank' change for missing values | ||
| 685 | for (my $i = 0; $i < @changes; $i++) { | ||
| 686 | if ($changes[$i] eq '' && !defined $values->[$i]) { | ||
| 687 | $changes[$i] = ' '; | ||
| 688 | } | ||
| 689 | } | ||
| 690 | |||
| 691 | my $any_value = (grep { defined } @$values)[0]; | ||
| 692 | my $value_name = $any_value->get_name; | ||
| 693 | $value_name = "(Default)" if $value_name eq ''; | ||
| 694 | $value_name =~ s/\0/[NUL]/g; | ||
| 695 | |||
| 696 | my $iter = $model->append($parent_iter); | ||
| 697 | |||
| 698 | if ($num_changes > 0) { | ||
| 699 | $model->set($iter, | ||
| 700 | TREECOL_NAME, $value_name, | ||
| 701 | TREECOL_CHANGES, \@changes, | ||
| 702 | TREECOL_KEYS, $keys, | ||
| 703 | TREECOL_VALUES, $values, | ||
| 704 | TREECOL_ICON, 'gtk-file'); | ||
| 705 | } | ||
| 706 | else { | ||
| 707 | $model->set($iter, | ||
| 708 | TREECOL_NAME, $value_name, | ||
| 709 | #TREECOL_CHANGES, \@changes, | ||
| 710 | TREECOL_KEYS, $keys, | ||
| 711 | TREECOL_VALUES, $values, | ||
| 712 | TREECOL_ICON, 'gtk-file'); | ||
| 713 | } | ||
| 714 | } | ||
| 715 | } | ||
| 716 | |||
| 717 | sub add_registry { | ||
| 718 | my $registry = shift; | ||
| 719 | |||
| 720 | my $filename = $registry->get_filename; | ||
| 721 | |||
| 722 | my $embedded_filename = $registry->get_embedded_filename; | ||
| 723 | $embedded_filename = '' if !defined $embedded_filename; | ||
| 724 | |||
| 725 | my $timestamp = $registry->get_timestamp; | ||
| 726 | $timestamp = defined $timestamp ? $registry->get_timestamp_as_string : ''; | ||
| 727 | |||
| 728 | my $iter = $registry_store->append; | ||
| 729 | $registry_store->set($iter, | ||
| 730 | REGCOL_FILENAME, basename($filename), | ||
| 731 | REGCOL_EMBEDDED_FILENAME, $embedded_filename, | ||
| 732 | REGCOL_TIMESTAMP, $timestamp, | ||
| 733 | REGCOL_DIRECTORY, dirname($filename), | ||
| 734 | REGCOL_REGISTRY, $registry, | ||
| 735 | ); | ||
| 736 | } | ||
| 737 | |||
| 738 | sub choose_files { | ||
| 739 | my $file_chooser = Gtk2::FileChooserDialog->new( | ||
| 740 | 'Select Registry File(s)', | ||
| 741 | undef, | ||
| 742 | 'open', | ||
| 743 | 'gtk-cancel' => 'cancel', | ||
| 744 | 'gtk-ok' => 'ok', | ||
| 745 | ); | ||
| 746 | $file_chooser->set_select_multiple(TRUE); | ||
| 747 | if (defined $last_dir) { | ||
| 748 | $file_chooser->set_current_folder($last_dir); | ||
| 749 | } | ||
| 750 | my @filenames = (); | ||
| 751 | my $response = $file_chooser->run; | ||
| 752 | if ($response eq 'ok') { | ||
| 753 | @filenames = $file_chooser->get_filenames; | ||
| 754 | } | ||
| 755 | $last_dir = $file_chooser->get_current_folder; | ||
| 756 | $file_chooser->destroy; | ||
| 757 | return @filenames; | ||
| 758 | } | ||
| 759 | |||
| 760 | sub open_files { | ||
| 761 | # refresh $registry_store with contents of @registries... | ||
| 762 | $registry_store->clear; | ||
| 763 | foreach my $registry (@registries) { | ||
| 764 | add_registry($registry); | ||
| 765 | } | ||
| 766 | |||
| 767 | $open_files_dialog->show_all; | ||
| 768 | } | ||
| 769 | |||
| 770 | sub close_files { | ||
| 771 | @root_keys = (); | ||
| 772 | # @registries is not cleared to retain currently selected files | ||
| 773 | |||
| 774 | $find_param = ''; | ||
| 775 | $find_iter = undef; | ||
| 776 | $change_iter = undef; | ||
| 777 | |||
| 778 | $tree_store->clear; | ||
| 779 | $list_store->clear; | ||
| 780 | $text_buffer->set_text(''); | ||
| 781 | $statusbar->pop(0); | ||
| 782 | |||
| 783 | my @columns = $tree_view->get_columns; | ||
| 784 | shift @columns; | ||
| 785 | foreach my $column (@columns) { | ||
| 786 | $tree_view->remove_column($column); | ||
| 787 | } | ||
| 788 | } | ||
| 789 | |||
| 790 | sub quit { | ||
| 791 | $window->destroy; | ||
| 792 | } | ||
| 793 | |||
| 794 | sub about { | ||
| 795 | Gtk2->show_about_dialog(undef, | ||
| 796 | 'program-name' => $script_name, | ||
| 797 | 'version' => $Parse::Win32Registry::VERSION, | ||
| 798 | 'copyright' => 'Copyright (c) 2008-2012 James Macfarlane', | ||
| 799 | 'comments' => 'GTK2 Registry Compare for the Parse::Win32Registry module', | ||
| 800 | ); | ||
| 801 | } | ||
| 802 | |||
| 803 | sub show_message { | ||
| 804 | my $type = shift; | ||
| 805 | my $message = shift; | ||
| 806 | |||
| 807 | my $dialog = Gtk2::MessageDialog->new( | ||
| 808 | $window, | ||
| 809 | 'destroy-with-parent', | ||
| 810 | $type, | ||
| 811 | 'ok', | ||
| 812 | $message, | ||
| 813 | ); | ||
| 814 | $dialog->set_title(ucfirst $type); | ||
| 815 | $dialog->run; | ||
| 816 | $dialog->destroy; | ||
| 817 | } | ||
| 818 | |||
| 819 | sub get_location { | ||
| 820 | my ($model, $iter) = $tree_selection->get_selected; | ||
| 821 | if (defined $model && defined $iter) { | ||
| 822 | my $keys = $model->get($iter, TREECOL_KEYS); | ||
| 823 | my $values = $model->get($iter, TREECOL_VALUES); | ||
| 824 | return ($keys, $values); | ||
| 825 | } | ||
| 826 | else { | ||
| 827 | return (); | ||
| 828 | } | ||
| 829 | } | ||
| 830 | |||
| 831 | sub copy_path { | ||
| 832 | my ($keys, $values) = get_location; | ||
| 833 | my $clip = ''; | ||
| 834 | if (defined $keys) { | ||
| 835 | my $any_key = (grep { defined } @$keys)[0]; | ||
| 836 | |||
| 837 | if (defined $values) { # only values | ||
| 838 | my $any_value = (grep { defined } @$values)[0]; | ||
| 839 | $clip = $any_key->get_path . ", " . $any_value->get_name; | ||
| 840 | } | ||
| 841 | else { | ||
| 842 | $clip = $any_key->get_path; | ||
| 843 | } | ||
| 844 | } | ||
| 845 | my $clipboard = Gtk2::Clipboard->get(Gtk2::Gdk->SELECTION_CLIPBOARD); | ||
| 846 | $clipboard->set_text($clip); | ||
| 847 | } | ||
| 848 | |||
| 849 | sub find_matching_child_iter { | ||
| 850 | my ($iter, $name, $icon) = @_; | ||
| 851 | |||
| 852 | return if !defined $iter; | ||
| 853 | |||
| 854 | my $child_iter = $tree_store->iter_nth_child($iter, 0); | ||
| 855 | if (!defined $child_iter) { | ||
| 856 | return; | ||
| 857 | } | ||
| 858 | |||
| 859 | # Make sure children are real | ||
| 860 | if (!defined $tree_store->get($child_iter, 0)) { | ||
| 861 | my $keys = $tree_store->get($iter, TREECOL_KEYS); | ||
| 862 | add_children($keys, $tree_store, $iter); | ||
| 863 | $tree_store->remove($child_iter); # remove dummy items | ||
| 864 | $child_iter = $tree_store->iter_nth_child($iter, 0); # refetch items | ||
| 865 | } | ||
| 866 | |||
| 867 | while (defined $child_iter) { | ||
| 868 | my $child_icon = $tree_store->get($child_iter, TREECOL_ICON); | ||
| 869 | |||
| 870 | if ($icon eq 'gtk-directory') { | ||
| 871 | my $child_keys = $tree_store->get($child_iter, TREECOL_KEYS); | ||
| 872 | my $any_child_key = (grep { defined } @$child_keys)[0]; | ||
| 873 | if ($any_child_key->get_name eq $name) { | ||
| 874 | return $child_iter; # match found | ||
| 875 | } | ||
| 876 | } | ||
| 877 | else { | ||
| 878 | my $child_values = $tree_store->get($child_iter, TREECOL_VALUES); | ||
| 879 | if (defined $child_values) { | ||
| 880 | my $any_child_value = (grep { defined } @$child_values)[0]; | ||
| 881 | if ($any_child_value->get_name eq $name) { | ||
| 882 | return $child_iter; # match found | ||
| 883 | } | ||
| 884 | } | ||
| 885 | } | ||
| 886 | |||
| 887 | $child_iter = $tree_store->iter_next($child_iter); | ||
| 888 | } | ||
| 889 | return; # no match found | ||
| 890 | } | ||
| 891 | |||
| 892 | sub go_to_subkey_and_value { | ||
| 893 | my $subkey_path = shift; | ||
| 894 | my $value_name = shift; | ||
| 895 | |||
| 896 | my @path_components = index($subkey_path, "\\") == -1 | ||
| 897 | ? ($subkey_path) | ||
| 898 | : split(/\\/, $subkey_path, -1); | ||
| 899 | |||
| 900 | my $iter = $tree_store->get_iter_first; | ||
| 901 | return if !defined $iter; # no registry loaded | ||
| 902 | |||
| 903 | while (defined(my $subkey_name = shift @path_components)) { | ||
| 904 | my $keys = $tree_store->get($iter, TREECOL_KEYS); | ||
| 905 | if (@$keys == 0) { | ||
| 906 | return; | ||
| 907 | } | ||
| 908 | |||
| 909 | $iter = find_matching_child_iter($iter, $subkey_name, 'gtk-directory'); | ||
| 910 | if (!defined $iter) { | ||
| 911 | return; # no matching child iter | ||
| 912 | } | ||
| 913 | |||
| 914 | if (@path_components == 0) { | ||
| 915 | # Look for a value if a value name has been supplied | ||
| 916 | if (defined $value_name) { | ||
| 917 | $iter = find_matching_child_iter($iter, $value_name, 'gtk-file'); | ||
| 918 | if (!defined $iter) { | ||
| 919 | return; # no matching child iter | ||
| 920 | } | ||
| 921 | } | ||
| 922 | my $parent_iter = $tree_store->iter_parent($iter); | ||
| 923 | my $parent_path = $tree_store->get_path($parent_iter); | ||
| 924 | $tree_view->expand_to_path($parent_path); | ||
| 925 | my $tree_path = $tree_store->get_path($iter); | ||
| 926 | $tree_view->scroll_to_cell($tree_path); | ||
| 927 | $tree_view->set_cursor($tree_path); | ||
| 928 | $window->set_focus($tree_view); | ||
| 929 | return; # match found | ||
| 930 | } | ||
| 931 | } | ||
| 932 | } | ||
| 933 | |||
| 934 | sub get_search_message { | ||
| 935 | my $message; | ||
| 936 | if ($search_keys && $search_values) { | ||
| 937 | $message = "Searching registry keys and values..."; | ||
| 938 | } | ||
| 939 | elsif ($search_keys) { | ||
| 940 | $message = "Searching registry keys..."; | ||
| 941 | } | ||
| 942 | elsif ($search_values) { | ||
| 943 | $message = "Searching registry values..."; | ||
| 944 | } | ||
| 945 | return $message; | ||
| 946 | } | ||
| 947 | |||
| 948 | sub find_next { | ||
| 949 | if (!defined $find_param || !defined $find_iter) { | ||
| 950 | return; | ||
| 951 | } | ||
| 952 | |||
| 953 | my $label = Gtk2::Label->new; | ||
| 954 | $label->set_text(get_search_message); | ||
| 955 | my $dialog = Gtk2::Dialog->new('Find', | ||
| 956 | $window, | ||
| 957 | 'modal', | ||
| 958 | 'gtk-cancel' => 'cancel', | ||
| 959 | ); | ||
| 960 | $dialog->vbox->pack_start($label, TRUE, TRUE, 5); | ||
| 961 | $dialog->set_default_response('cancel'); | ||
| 962 | $dialog->show_all; | ||
| 963 | |||
| 964 | my $id = Glib::Idle->add(sub { | ||
| 965 | my ($keys, $values) = $find_iter->get_next; | ||
| 966 | |||
| 967 | if (!defined $keys) { | ||
| 968 | $dialog->response('ok'); | ||
| 969 | return FALSE; # stop searching | ||
| 970 | } | ||
| 971 | |||
| 972 | # Obtain the name and path from the first defined key | ||
| 973 | my $any_key = (grep { defined } @$keys)[0]; | ||
| 974 | my $subkey_path = (split(/\\/, $any_key->get_path, 2))[1]; | ||
| 975 | if (!defined $subkey_path) { | ||
| 976 | return TRUE; | ||
| 977 | } | ||
| 978 | |||
| 979 | # Check values (if defined) for a match | ||
| 980 | if (defined $values) { | ||
| 981 | if ($search_values) { | ||
| 982 | my $any_value = (grep { defined } @$values)[0]; | ||
| 983 | my $value_name = $any_value->get_name; | ||
| 984 | if (index(lc $value_name, lc $find_param) >= 0) { | ||
| 985 | go_to_subkey_and_value($subkey_path, $value_name); | ||
| 986 | $dialog->response(50); | ||
| 987 | return FALSE; # stop searching | ||
| 988 | } | ||
| 989 | } | ||
| 990 | return TRUE; # continue searching | ||
| 991 | } | ||
| 992 | |||
| 993 | # Check keys for a match | ||
| 994 | if ($search_keys) { | ||
| 995 | my $key_name = $any_key->get_name; | ||
| 996 | if (index(lc $key_name, lc $find_param) >= 0) { | ||
| 997 | go_to_subkey_and_value($subkey_path); | ||
| 998 | $dialog->response(50); | ||
| 999 | return FALSE; # stop searching | ||
| 1000 | } | ||
| 1001 | } | ||
| 1002 | return TRUE; # continue searching | ||
| 1003 | }); | ||
| 1004 | |||
| 1005 | my $response = $dialog->run; | ||
| 1006 | $dialog->destroy; | ||
| 1007 | |||
| 1008 | if ($response eq 'cancel' || $response eq 'delete-event') { | ||
| 1009 | Glib::Source->remove($id); | ||
| 1010 | } | ||
| 1011 | elsif ($response eq 'ok') { | ||
| 1012 | show_message('info', 'Finished searching.'); | ||
| 1013 | } | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | sub find { | ||
| 1017 | return if @root_keys == 0; | ||
| 1018 | |||
| 1019 | my ($selected_keys, $selected_values) = get_location; | ||
| 1020 | |||
| 1021 | my $label = Gtk2::Label->new('Enter text to search for:'); | ||
| 1022 | $label->set_alignment(0, 0); | ||
| 1023 | my $entry = Gtk2::Entry->new; | ||
| 1024 | $entry->set_text($find_param); | ||
| 1025 | $entry->set_activates_default(TRUE); | ||
| 1026 | my $check1 = Gtk2::CheckButton->new('Search _keys'); | ||
| 1027 | $check1->set_active($search_keys); | ||
| 1028 | my $check2 = Gtk2::CheckButton->new('Search _values'); | ||
| 1029 | $check2->set_active($search_values); | ||
| 1030 | $check1->signal_connect(toggled => sub { | ||
| 1031 | if (!$check1->get_active && !$check2->get_active) { | ||
| 1032 | $check2->set_active(TRUE); | ||
| 1033 | } | ||
| 1034 | }); | ||
| 1035 | $check2->signal_connect(toggled => sub { | ||
| 1036 | if (!$check1->get_active && !$check2->get_active) { | ||
| 1037 | $check1->set_active(TRUE); | ||
| 1038 | } | ||
| 1039 | }); | ||
| 1040 | my $frame = Gtk2::Frame->new('Start searching'); | ||
| 1041 | my $vbox = Gtk2::VBox->new(FALSE, 0); | ||
| 1042 | $frame->add($vbox); | ||
| 1043 | my $radio1 = Gtk2::RadioButton->new(undef, 'from _root key'); | ||
| 1044 | my $radio2 = Gtk2::RadioButton->new($radio1, 'from c_urrent key'); | ||
| 1045 | if (!defined $selected_keys) { | ||
| 1046 | $radio2->set_sensitive(FALSE); | ||
| 1047 | } | ||
| 1048 | elsif ($search_selected) { | ||
| 1049 | $radio2->set_active(TRUE); | ||
| 1050 | } | ||
| 1051 | $vbox->pack_start($radio1, TRUE, TRUE, 0); | ||
| 1052 | $vbox->pack_start($radio2, TRUE, TRUE, 0); | ||
| 1053 | |||
| 1054 | my $dialog = Gtk2::Dialog->new('Find', | ||
| 1055 | $window, | ||
| 1056 | 'modal', | ||
| 1057 | 'gtk-cancel' => 'cancel', | ||
| 1058 | 'gtk-ok' => 'ok', | ||
| 1059 | ); | ||
| 1060 | $dialog->vbox->set_spacing(5); | ||
| 1061 | $dialog->vbox->pack_start($label, FALSE, TRUE, 0); | ||
| 1062 | $dialog->vbox->pack_start($entry, FALSE, TRUE, 0); | ||
| 1063 | $dialog->vbox->pack_start($check1, FALSE, TRUE, 0); | ||
| 1064 | $dialog->vbox->pack_start($check2, FALSE, TRUE, 0); | ||
| 1065 | $dialog->vbox->pack_start($frame, FALSE, TRUE, 0); | ||
| 1066 | $dialog->set_default_response('ok'); | ||
| 1067 | $dialog->show_all; | ||
| 1068 | |||
| 1069 | my $response = $dialog->run; | ||
| 1070 | if ($response eq 'ok' && @root_keys > 0) { | ||
| 1071 | $search_keys = $check1->get_active; | ||
| 1072 | $search_values = $check2->get_active; | ||
| 1073 | $search_selected = $radio2->get_active; | ||
| 1074 | $find_param = $entry->get_text; | ||
| 1075 | $dialog->destroy; | ||
| 1076 | $find_iter = undef; | ||
| 1077 | if ($find_param ne '') { | ||
| 1078 | $find_iter = $search_selected | ||
| 1079 | ? make_multiple_subtree_iterator(@$selected_keys) | ||
| 1080 | : make_multiple_subtree_iterator(@root_keys); | ||
| 1081 | find_next; | ||
| 1082 | } | ||
| 1083 | } | ||
| 1084 | else { | ||
| 1085 | $dialog->destroy; | ||
| 1086 | } | ||
| 1087 | } | ||
| 1088 | |||
| 1089 | sub find_next_change { | ||
| 1090 | if (!defined $change_iter) { | ||
| 1091 | return; | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | my $label = Gtk2::Label->new; | ||
| 1095 | $label->set_text(get_search_message); | ||
| 1096 | my $dialog = Gtk2::Dialog->new('Find Change', | ||
| 1097 | $window, | ||
| 1098 | 'modal', | ||
| 1099 | 'gtk-cancel' => 'cancel', | ||
| 1100 | ); | ||
| 1101 | $dialog->vbox->pack_start($label, TRUE, TRUE, 5); | ||
| 1102 | $dialog->set_default_response('cancel'); | ||
| 1103 | $dialog->show_all; | ||
| 1104 | |||
| 1105 | my $id = Glib::Idle->add(sub { | ||
| 1106 | my ($keys, $values) = $change_iter->get_next; | ||
| 1107 | |||
| 1108 | if (!defined $keys) { | ||
| 1109 | $dialog->response('ok'); | ||
| 1110 | return FALSE; # stop searching | ||
| 1111 | } | ||
| 1112 | |||
| 1113 | # Obtain the name and path from the first defined key | ||
| 1114 | my $any_key = (grep { defined } @$keys)[0]; | ||
| 1115 | my $subkey_path = (split(/\\/, $any_key->get_path, 2))[1]; | ||
| 1116 | if (!defined $subkey_path) { | ||
| 1117 | return TRUE; | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | # Check values (if defined) for changes | ||
| 1121 | if (defined $values) { | ||
| 1122 | if ($search_values) { | ||
| 1123 | my $any_value = (grep { defined } @$values)[0]; | ||
| 1124 | my $value_name = $any_value->get_name; | ||
| 1125 | my @changes = compare_multiple_values(@$values); | ||
| 1126 | my $num_changes = grep { $_ } @changes; | ||
| 1127 | if ($num_changes > 0) { | ||
| 1128 | go_to_subkey_and_value($subkey_path, $value_name); | ||
| 1129 | $dialog->response(50); | ||
| 1130 | return FALSE; # stop searching | ||
| 1131 | } | ||
| 1132 | } | ||
| 1133 | return TRUE; # continue searching | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | if ($search_keys) { | ||
| 1137 | my $key_name = $any_key->get_name; | ||
| 1138 | my @changes = compare_multiple_keys(@$keys); | ||
| 1139 | my $num_changes = grep { $_ } @changes; | ||
| 1140 | if ($num_changes > 0) { | ||
| 1141 | go_to_subkey_and_value($subkey_path); | ||
| 1142 | $dialog->response(50); | ||
| 1143 | return FALSE; # stop searching | ||
| 1144 | } | ||
| 1145 | } | ||
| 1146 | return TRUE; # continue searching | ||
| 1147 | }); | ||
| 1148 | |||
| 1149 | my $response = $dialog->run; | ||
| 1150 | $dialog->destroy; | ||
| 1151 | |||
| 1152 | if ($response eq 'cancel' || $response eq 'delete-event') { | ||
| 1153 | Glib::Source->remove($id); | ||
| 1154 | } | ||
| 1155 | elsif ($response eq 'ok') { | ||
| 1156 | show_message('info', 'Finished searching.'); | ||
| 1157 | } | ||
| 1158 | } | ||
| 1159 | |||
| 1160 | sub find_change { | ||
| 1161 | return if @root_keys == 0; | ||
| 1162 | |||
| 1163 | my ($selected_keys, $selected_values) = get_location; | ||
| 1164 | |||
| 1165 | my $label = Gtk2::Label->new('Search for a change:'); | ||
| 1166 | $label->set_alignment(0, 0); | ||
| 1167 | my $check1 = Gtk2::CheckButton->new('Search _keys'); | ||
| 1168 | $check1->set_active($search_keys); | ||
| 1169 | my $check2 = Gtk2::CheckButton->new('Search _values'); | ||
| 1170 | $check2->set_active($search_values); | ||
| 1171 | $check1->signal_connect(toggled => sub { | ||
| 1172 | if (!$check1->get_active && !$check2->get_active) { | ||
| 1173 | $check2->set_active(TRUE); | ||
| 1174 | } | ||
| 1175 | }); | ||
| 1176 | $check2->signal_connect(toggled => sub { | ||
| 1177 | if (!$check1->get_active && !$check2->get_active) { | ||
| 1178 | $check1->set_active(TRUE); | ||
| 1179 | } | ||
| 1180 | }); | ||
| 1181 | my $frame = Gtk2::Frame->new('Start searching'); | ||
| 1182 | my $vbox = Gtk2::VBox->new(FALSE, 0); | ||
| 1183 | $frame->add($vbox); | ||
| 1184 | my $radio1 = Gtk2::RadioButton->new(undef, 'from _root key'); | ||
| 1185 | my $radio2 = Gtk2::RadioButton->new($radio1, 'from c_urrent key'); | ||
| 1186 | if (!defined $selected_keys) { | ||
| 1187 | $radio2->set_sensitive(FALSE); | ||
| 1188 | } | ||
| 1189 | elsif ($search_selected) { | ||
| 1190 | $radio2->set_active(TRUE); | ||
| 1191 | } | ||
| 1192 | $vbox->pack_start($radio1, TRUE, TRUE, 0); | ||
| 1193 | $vbox->pack_start($radio2, TRUE, TRUE, 0); | ||
| 1194 | |||
| 1195 | my $dialog = Gtk2::Dialog->new('Find', | ||
| 1196 | $window, | ||
| 1197 | 'modal', | ||
| 1198 | 'gtk-cancel' => 'cancel', | ||
| 1199 | 'gtk-ok' => 'ok', | ||
| 1200 | ); | ||
| 1201 | $dialog->vbox->set_spacing(5); | ||
| 1202 | $dialog->vbox->pack_start($label, FALSE, TRUE, 0); | ||
| 1203 | $dialog->vbox->pack_start($check1, FALSE, TRUE, 0); | ||
| 1204 | $dialog->vbox->pack_start($check2, FALSE, TRUE, 0); | ||
| 1205 | $dialog->vbox->pack_start($frame, FALSE, TRUE, 0); | ||
| 1206 | $dialog->set_default_response('ok'); | ||
| 1207 | $dialog->show_all; | ||
| 1208 | |||
| 1209 | my $response = $dialog->run; | ||
| 1210 | if ($response eq 'ok') { | ||
| 1211 | $search_keys = $check1->get_active; | ||
| 1212 | $search_values = $check2->get_active; | ||
| 1213 | $search_selected = $radio2->get_active; | ||
| 1214 | $dialog->destroy; | ||
| 1215 | $change_iter = $search_selected | ||
| 1216 | ? make_multiple_subtree_iterator(@$selected_keys) | ||
| 1217 | : make_multiple_subtree_iterator(@root_keys); | ||
| 1218 | $change_iter->get_next; # skip the starting key | ||
| 1219 | find_next_change; | ||
| 1220 | } | ||
| 1221 | else { | ||
| 1222 | $dialog->destroy; | ||
| 1223 | } | ||
| 1224 | } |