script/wxregview.pl untested

filter: Statementstmt 0.0% 0 / 666 Branchbran 0.0% 0 / 118 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 89 Podpod 0.0% 0 / 53 totaltotal 0.0% 0 / 949 CC 60 SCAR 32.7
CC
60
Cov
0%
FindNext
182.0
ShowTimeline
56.0
FindMatchingItem
30.0
CRAP 26.3
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4  
5 binmode STDOUT, ':utf8';
6  
7 use Parse::Win32Registry 0.51;
8  
9  
10 package KeyTreeCtrl;
11  
12 use Wx qw(:everything);
13 use Wx::ArtProvider qw(:artid :clientid);
14 use Wx::Event qw(:everything);
15  
16 use base qw(Wx::TreeCtrl);
17  
18 sub new {
19 my ($class, $parent) = @_;
20  
21 my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxTR_DEFAULT_STYLE|wxBORDER_SUNKEN);
22 bless $self, $class;
23  
24 my $imagelist = Wx::ImageList->new(16, 16, 1);
25 $imagelist->Add(Wx::ArtProvider::GetIcon(wxART_FOLDER, wxART_MENU, [16, 16]));
26 $imagelist->Add(Wx::ArtProvider::GetIcon(wxART_NORMAL_FILE, wxART_MENU, [16, 16]));
27 $self->AssignImageList($imagelist);
28  
29 EVT_TREE_ITEM_EXPANDING($self, $self, \&OnTreeItemExpanding);
30  
31 return $self;
32 }
33  
34 sub Clear {
35 my ($self) = @_;
36  
37 $self->DeleteAllItems;
38 }
39  
40 sub SetRootKey {
41 my ($self, $root_key) = @_;
42  
43 my $name = $root_key->get_name;
44 $name =~ s/\0/[NUL]/g;
45 $name =~ s/\n/[LF]/g;
46 $name =~ s/\r/[CR]/g;
47  
48 my $root_item = $self->AddRoot($name, 0, -1);
49 $self->SetPlData($root_item, $root_key);
50  
51 $self->AddChildren($root_item, $root_key);
52 }
53  
54 sub AddChildren {
55 my ($self, $item, $key) = @_;
56  
57 my @subkeys = $key->get_list_of_subkeys;
58 foreach my $subkey (@subkeys) {
59 my $name = $subkey->get_name;
60 $name =~ s/\0/[NUL]/g;
61 $name =~ s/\n/[LF]/g;
62 $name =~ s/\r/[CR]/g;
63  
64 my $child_item = $self->AppendItem($item, $name, 0, -1);
65 $self->SetPlData($child_item, $subkey);
66 $self->SetItemHasChildren($child_item, 1);
67 }
68 return scalar @subkeys;
69 }
70  
71 sub OnTreeItemExpanding {
72 my ($self, $event) = @_;
73  
74 my $item = $event->GetItem;
75  
76 my ($child_item, $cookie) = $self->GetFirstChild($item);
77 if ($child_item->IsOk) {
78 return; # already populated
79 }
80  
81 my $key = $self->GetPlData($item);
82 if (!$self->AddChildren($item, $key)) {
83 $self->SetItemHasChildren($item, 0);
84 }
85 }
86  
87 sub FindMatchingItem {
88 my ($self, $key_name, $item) = @_;
89  
90 return if !$self->ItemHasChildren($item);
91  
92 # Make any virtual children real before proceeding
93 my ($child_item, $cookie) = $self->GetFirstChild($item);
94 if (!$child_item->IsOk) { # children still virtual
95 my $key = $self->GetPlData($item);
96 if (!$self->AddChildren($item, $key)) {
97 $self->SetItemHasChildren($item, 0);
98 }
99 }
100  
101 # Look through the children for a match
102 ($child_item, $cookie) = $self->GetFirstChild($item);
103 while ($child_item->IsOk) {
104 my $key = $self->GetPlData($child_item);
105  
106 if ($key_name eq $key->get_name) {
107 return $child_item; # found a match
108 }
109 ($child_item, $cookie) = $self->GetNextChild($item, $cookie);
110 }
111  
112 return; # no match
113 }
114  
115 sub GoToSubkey {
116 my ($self, $subkey_path) = @_;
117  
118 my $item = $self->GetRootItem;
119  
120 my @key_names = split(/\\/, $subkey_path, -1);
121  
122 # my @key_names = index($subkey_path, "\\") == -1
123 # ? ($subkey_path)
124 # : split(/\\/, $subkey_path, -1);
125  
126 # If the first method is chosen, it is possible to go to the root key,
127 # but a first-level subkey with no name will be inaccessible.
128 # This is because an empty string will produce an empty array,
129 # causing the following while loop to be skipped,
130 # leaving $item set to the root.
131  
132 # If the second method is chosen, it is possible to go to a first-level
133 # subkey with no name, but the root key will be inaccessible.
134 # This is because an array with at least one string in it is produced,
135 # causing the following while loop to be entered, and either
136 # the first-level subkey will be found and $item will be set to it,
137 # or it will not be found, and the subroutine will return
138 # without going to any item.
139  
140 while (@key_names) {
141 my $key_name = shift @key_names;
142  
143 $item = $self->FindMatchingItem($key_name, $item);
144  
145 if (!defined $item) {
146 return; # no match found
147 }
148 }
149  
150 # match found, in $item
151 $self->EnsureVisible($item);
152 $self->SelectItem($item);
153 }
154  
155 sub GetSelectedKey {
156 my ($self) = @_;
157  
158 my $item = $self->GetSelection;
159 if ($item->IsOk) {
160 my $key = $self->GetPlData($item);
161 return $key;
162 }
163 return;
164 }
165  
166  
167 package ValueListCtrl;
168  
169 use Wx qw(:everything);
170 use Wx::ArtProvider qw(:artid :clientid);
171  
172 use base qw(Wx::ListCtrl);
173  
174 sub new {
175 my ($class, $parent) = @_;
176  
177 my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxLC_REPORT|wxLC_SINGLE_SEL|wxBORDER_SUNKEN);
178 bless $self, $class;
179  
180 $self->InsertColumn(0, "Name", wxLIST_FORMAT_LEFT);
181 $self->InsertColumn(1, "Type", wxLIST_FORMAT_LEFT);
182 $self->InsertColumn(2, "Data", wxLIST_FORMAT_LEFT);
183  
184 $self->SetColumnWidth(0, 150);
185 $self->SetColumnWidth(1, 100);
186 $self->SetColumnWidth(2, 150);
187  
188 my $imagelist = Wx::ImageList->new(16, 16, 1);
189 $imagelist->Add(Wx::ArtProvider::GetIcon(wxART_NORMAL_FILE, wxART_MENU, [16, 16]));
190 $self->AssignImageList($imagelist, wxIMAGE_LIST_SMALL);
191  
192 return $self;
193 }
194  
195 sub SetKey {
196 my ($self, $key) = @_;
197  
198 return unless $key->can('get_list_of_values');
199  
200 my @values = $key->get_list_of_values;
201 $self->DeleteAllItems;
202 my $index = 0;
203 foreach my $value (@values) {
204 my $name = $value->get_name;
205 $name = "(Default)" if $name eq '';
206 $name =~ s/\0/[NUL]/g;
207 $name =~ s/\n/[LF]/g;
208 $name =~ s/\r/[CR]/g;
209  
210 my $type = $value->get_type_as_string;
211  
212 my $data = substr($value->get_data_as_string, 0, 200);
213 $data =~ s/\0/[NUL]/g;
214 $data =~ s/\n/[LF]/g;
215 $data =~ s/\r/[CR]/g;
216  
217 $index = $self->InsertImageStringItem($index+1, $name, 0);
218 $self->SetItem($index, 1, $type);
219 $self->SetItem($index, 2, $data);
220 }
221  
222 $self->{_key} = $key;
223 $self->{_values} = \@values;
224 }
225  
226 sub GetValue {
227 my ($self, $index) = @_;
228  
229 return $self->{_values}[$index];
230 }
231  
232 sub Clear {
233 my ($self) = @_;
234  
235 $self->DeleteAllItems;
236 $self->{_key} = undef;
237 $self->{_values} = undef;
238 }
239  
240 sub GoToValue {
241 my ($self, $value_name) = @_;
242  
243 for (my $index = 0; $index < @{$self->{_values}}; $index++) {
244 if ($value_name eq $self->{_values}[$index]->get_name) {
245 $self->EnsureVisible($index);
246 $self->SetItemState($index, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
247 }
248 }
249 }
250  
251  
252 package ViewFrame;
253  
254 use File::Basename;
255 use FindBin;
256 use Parse::Win32Registry qw(hexdump);
257 use Wx qw(:everything);
258 use Wx::DND; # required for copying to clipboard
259 use Wx::Event qw(:everything);
260  
261 use base qw(Wx::Frame);
262  
263 use constant ID_DUMP_KEYS => Wx::NewId;
264 use constant ID_FIND_NEXT => Wx::NewId;
265 use constant ID_TIMELINE => Wx::NewId;
266 use constant ID_SELECT_FONT => Wx::NewId;
267  
268 sub new {
269 my ($class, $parent) = @_;
270  
271 my $self = $class->SUPER::new($parent, -1, "Registry Viewer", wxDefaultPosition, [600, 400]);
272 bless $self, $class;
273  
274 $self->SetMinSize([600, 400]);
275  
276 my $menu1 = Wx::Menu->new;
277 $menu1->Append(wxID_OPEN, "&Open...\tCtrl+O");
278 $menu1->Append(wxID_CLOSE, "&Close\tCtrl+W");
279 $menu1->AppendSeparator;
280 $menu1->Append(wxID_EXIT, "E&xit\tAlt+F4");
281  
282 my $menu2 = Wx::Menu->new;
283 $menu2->Append(wxID_COPY, "&Copy Key Path\tCtrl+C");
284  
285 my $menu3 = Wx::Menu->new;
286 $menu3->Append(wxID_FIND, "&Find...\tCtrl+F");
287 $menu3->Append(ID_FIND_NEXT, "Find &Next\tF3");
288 $menu3->AppendSeparator;
289 $menu3->Append(ID_TIMELINE, "Show &Timeline...");
290  
291 my $menu4 = Wx::Menu->new;
292 $menu4->Append(ID_SELECT_FONT, "Select &Font...");
293  
294 my $menu5 = Wx::Menu->new;
295 $menu5->Append(wxID_ABOUT, "&About...");
296  
297 my $menubar = Wx::MenuBar->new;
298 $menubar->Append($menu1, "&File");
299 $menubar->Append($menu2, "&Edit");
300 $menubar->Append($menu3, "&Search");
301 $menubar->Append($menu4, "&View");
302 $menubar->Append($menu5, "&Help");
303  
304 $self->SetMenuBar($menubar);
305  
306 my $statusbar = Wx::StatusBar->new($self, -1);
307 $self->SetStatusBar($statusbar);
308  
309 EVT_MENU($self, wxID_OPEN, \&OnOpenFile);
310 EVT_MENU($self, wxID_CLOSE, \&OnCloseFile);
311 EVT_MENU($self, wxID_EXIT, \&OnQuit);
312 EVT_MENU($self, wxID_COPY, \&OnCopy);
313 EVT_MENU($self, wxID_FIND, \&OnFind);
314 EVT_MENU($self, ID_FIND_NEXT, \&FindNext);
315 EVT_MENU($self, ID_TIMELINE, \&ShowTimeline);
316 EVT_MENU($self, wxID_ABOUT, \&OnAbout);
317 EVT_MENU($self, ID_SELECT_FONT, \&OnSelectFont);
318  
319 my $hsplitter = Wx::SplitterWindow->new($self, -1, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER);
320  
321 my $tree = KeyTreeCtrl->new($hsplitter);
322  
323 my $vsplitter = Wx::SplitterWindow->new($hsplitter, -1, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER);
324  
325 $hsplitter->SplitVertically($tree, $vsplitter);
326 $hsplitter->SetMinimumPaneSize(10);
327  
328 my $list = ValueListCtrl->new($vsplitter);
329  
330 my $text = Wx::TextCtrl->new($vsplitter, -1, '', wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE|wxTE_DONTWRAP|wxTE_READONLY);
331 # Set a monospaced font
332 $text->SetFont(Wx::Font->new(10, wxMODERN, wxNORMAL, wxNORMAL));
333  
334 $vsplitter->SplitHorizontally($list, $text);
335 $vsplitter->SetMinimumPaneSize(10);
336  
337 $self->{_tree} = $tree;
338 $self->{_list} = $list;
339 $self->{_text} = $text;
340 $self->{_statusbar} = $statusbar;
341  
342 EVT_SPLITTER_DCLICK($self, $hsplitter, \&OnSplitterDClick);
343 EVT_SPLITTER_DCLICK($self, $vsplitter, \&OnSplitterDClick);
344  
345 EVT_TREE_SEL_CHANGED($self, $tree, \&OnKeyTreeSelChanged);
346 EVT_LIST_ITEM_SELECTED($self, $list, \&OnValueListItemSelected);
347  
348 $self->SetIcon(Wx::GetWxPerlIcon());
349  
350 my $accelerators = Wx::AcceleratorTable->new(
351 [wxACCEL_CTRL, ord('Q'), wxID_EXIT],
352 );
353 $self->SetAcceleratorTable($accelerators);
354  
355 my $filename = shift @ARGV;
356 if (defined $filename) {
357 $self->LoadFile($filename);
358 }
359  
360 return $self;
361 }
362  
363 sub OnSelectFont {
364 my ($self, $event) = @_;
365  
366 my $text = $self->{_text};
367 my $font = $text->GetFont;
368 $font = Wx::GetFontFromUser($self, $font);
369 if ($font->IsOk) {
370 $text->SetFont($font);
371 }
372 }
373  
374 sub OnSplitterDClick {
375 my ($self, $event) = @_;
376  
377 $event->Veto;
378 }
379  
380 sub ShowTimeline {
381 my ($self, $event) = @_;
382  
383 return if !defined $self->{_root_key};
384  
385 my $dialog = $self->{_timeline_dialog};
386 if (!defined $dialog) {
387 $dialog = $self->{_timeline_dialog} = TimelineDialog->new($self);
388 # OnKeyListItemSelected
389 EVT_LIST_ITEM_SELECTED($self, $dialog->{_list2}, sub {
390 my ($self, $event) = @_;
391 my $index = $event->GetIndex;
392 my $key = $dialog->{_list2}->GetKey($index);
393 if (defined $key) {
394 my $subkey_path = (split(/\\/, $key->get_path, 2))[1];
395 $self->{_tree}->GoToSubkey($subkey_path);
396 }
397 });
398 # OnKeyListItemActivated
399 EVT_LIST_ITEM_ACTIVATED($self, $dialog->{_list2}, sub {
400 my ($self, $event) = @_;
401 $self->Raise;
402 });
403 my $font = $self->{_tree}->GetFont;
404 $dialog->{_list1}->SetFont($font);
405 $dialog->{_list2}->SetFont($font);
406 }
407  
408 if (!defined $self->{_keys_by_time}) {
409 $self->BuildTimeline;
410 return if !defined $self->{_keys_by_time}; # build was cancelled
411 $dialog->SetTimeline($self->{_keys_by_time});
412 }
413  
414 if (scalar keys %{$self->{_keys_by_time}} == 0) {
415 my $dialog = Wx::MessageDialog->new($self,
416 'No keys have timestamps!', 'Timeline', wxICON_ERROR|wxOK);
417 $dialog->ShowModal;
418 $dialog->Destroy;
419 return;
420 }
421  
422 $dialog->Show;
423 $dialog->Raise;
424 $dialog->{_list1}->SetFocus;
425 }
426  
427 sub BuildTimeline {
428 my ($self) = @_;
429  
430 return if defined $self->{_keys_by_time};
431  
432 my $root_key = $self->{_root_key};
433  
434 return if !defined $root_key;
435  
436 my $subtree_iter = $root_key->get_subtree_iterator;
437  
438 my %keys_by_time = ();
439  
440 my $max = 0;
441 my $progress_dialog = Wx::ProgressDialog->new('Building Timeline',
442 'Ordering registry keys...', $max, $self,
443 wxPD_CAN_ABORT|wxPD_AUTO_HIDE);
444 $progress_dialog->Update;
445  
446 while (my $key = $subtree_iter->get_next) {
447 my $time = $key->get_timestamp;
448 push @{$keys_by_time{$time}}, $key if defined $time;
449  
450 if (!$progress_dialog->Update) {
451 # Cancelled!
452 $progress_dialog->Destroy;
453 return;
454 }
455 }
456  
457 $self->{_keys_by_time} = \%keys_by_time;
458  
459 $progress_dialog->Destroy;
460 }
461  
462 sub OnCopy {
463 my ($self, $event) = @_;
464  
465 my $key = $self->{_tree}->GetSelectedKey;
466 my $clip = '';
467 if (defined $key) {
468 $clip = $key->get_path;
469 }
470 wxTheClipboard->Open;
471 wxTheClipboard->SetData(Wx::TextDataObject->new($clip));
472 wxTheClipboard->Close;
473 }
474  
475 sub OnKeyTreeSelChanged {
476 my ($self, $event) = @_;
477  
478 my $item = $event->GetItem;
479 my $key = $self->{_tree}->GetPlData($item);
480  
481 $self->{_list}->SetKey($key);
482  
483 return if !$key->can('get_list_of_values');
484  
485 my $details = '';
486 if (defined $key->get_timestamp) {
487 $details .= "Timestamp: " . $key->get_timestamp_as_string . "\n";
488 }
489 my $class_name = $key->get_class_name;
490 if (defined $class_name) {
491 $class_name =~ s/\0/[NUL]/g;
492 $class_name =~ s/\n/[LF]/g;
493 $class_name =~ s/\r/[CR]/g;
494 $details .= "Class Name: $class_name\n";
495 }
496  
497 my $security = $key->get_security;
498 if (defined $security) {
499 my $sd = $security->get_security_descriptor;
500 $details .= $sd->as_stanza;
501 }
502  
503 $self->{_text}->ChangeValue($details);
504  
505 my $key_str = $key->as_string;
506 $key_str =~ s/\0/[NUL]/g;
507 $key_str =~ s/\n/[LF]/g;
508 $key_str =~ s/\r/[CR]/g;
509 $self->{_statusbar}->SetStatusText($key_str);
510 }
511  
512 sub OnValueListItemSelected {
513 my ($self, $event) = @_;
514  
515 my $value = $self->{_list}->GetValue($event->GetIndex);
516  
517 my $details = hexdump($value->get_raw_data);
518  
519 $self->{_text}->ChangeValue($details);
520 }
521  
522 sub OnAbout {
523 my ($self, $event) = @_;
524  
525 my $info = Wx::AboutDialogInfo->new;
526 $info->SetName($FindBin::Script);
527 $info->SetVersion($Parse::Win32Registry::VERSION);
528 $info->SetCopyright('Copyright (c) 2010-2012 James Macfarlane');
529 $info->SetDescription('wxWidgets Registry Viewer for the Parse::Win32Registry module');
530 Wx::AboutBox($info);
531 }
532  
533 sub FindNext {
534 my ($self) = @_;
535  
536 my $find_param = $self->{_find_param};
537 my $find_iter = $self->{_find_iter};
538 my $search_keys = $self->{_search_keys};
539 my $search_values = $self->{_search_values};
540  
541 return if !defined $find_param || $find_param eq '';
542 return if !defined $find_iter;
543  
544 my $start = time;
545  
546 my $max = 0;
547 my $progress_dialog;
548  
549 my $iter_finished = 1;
550  
551 while (my ($key, $value) = $find_iter->get_next) {
552  
553 my $key_name = $key->get_name;
554 my $key_path = $key->get_path;
555  
556 # strip root key name from path to get subkey path
557 my $subkey_path = (split(/\\/, $key_path, 2))[1];
558  
559 if (defined $value) { # check value for match
560 if ($search_values) {
561 my $value_name = $value->get_name;
562 if (index(lc $value_name, lc $find_param) >= 0) {
563 $self->{_tree}->GoToSubkey($subkey_path);
564 $self->{_list}->GoToValue($value_name);
565 $self->{_list}->SetFocus;
566 $self->SetFocus;
567 $iter_finished = 0;
568 last;
569 }
570 }
571 }
572 elsif ($search_keys) { # check key for match
573 if (index(lc $key_name, lc $find_param) >= 0) {
574 $self->{_tree}->GoToSubkey($subkey_path);
575 $self->{_tree}->SetFocus;
576 $self->SetFocus;
577 $iter_finished = 0;
578 last;
579 }
580 }
581  
582 if (defined $progress_dialog) {
583 if (!$progress_dialog->Update) {
584 # Cancelled!
585 $iter_finished = 0;
586 last;
587 }
588 }
589 else {
590 # display progress dialog if search is slow
591 if (time - $start >= 1) {
592 $progress_dialog = Wx::ProgressDialog->new('Find',
593 'Searching registry...', $max, $self,
594 wxPD_CAN_ABORT|wxPD_AUTO_HIDE);
595 }
596 }
597 }
598  
599 if (defined $progress_dialog) {
600 $progress_dialog->Destroy;
601 }
602  
603 if ($iter_finished) {
604 my $dialog = Wx::MessageDialog->new($self,
605 'Finished searching', 'Find', wxICON_EXCLAMATION|wxOK);
606 $dialog->ShowModal;
607 $dialog->Destroy;
608 }
609 }
610  
611 sub OnFind {
612 my ($self, $event) = @_;
613  
614 my $root_key = $self->{_root_key};
615 return if !defined $root_key;
616  
617 my $dialog = FindDialog->new($self);
618 $dialog->SetText($self->{_find_param});
619 $dialog->SetSearchKeys($self->{_search_keys});
620 $dialog->SetSearchValues($self->{_search_values});
621 $dialog->SetSearchSelected($self->{_search_selected});
622  
623 if ($dialog->ShowModal == wxID_OK) {
624 $self->{_find_param} = $dialog->GetText;
625  
626 $self->{_search_keys} = $dialog->GetSearchKeys;
627 $self->{_search_values} = $dialog->GetSearchValues;
628 if (!$self->{_search_keys} && !$self->{_search_values}) {
629 $self->{_search_keys} = $self->{_search_values} = 1;
630 }
631  
632 my $selected_key = $self->{_tree}->GetSelectedKey;
633 my $search_selected = $self->{_search_selected}
634 = $dialog->GetSearchSelected;
635  
636 $self->{_find_iter} = $search_selected
637 ? $selected_key->get_subtree_iterator
638 : $root_key->get_subtree_iterator;
639 $self->FindNext;
640 }
641 $dialog->Destroy;
642 }
643  
644 sub LoadFile {
645 my ($self, $filename) = @_;
646  
647 if (!-r $filename) {
648 my $dialog = Wx::MessageDialog->new($self,
649 "'$filename' cannot be read", 'Error', wxICON_ERROR|wxOK);
650 $dialog->ShowModal;
651 $dialog->Destroy;
652 return
653 }
654  
655 my $basename = basename($filename);
656  
657 my $registry = Parse::Win32Registry->new($filename);
658 if (!defined $registry) {
659 my $dialog = Wx::MessageDialog->new($self,
660 "'$basename' is not a registry file", 'Error', wxICON_ERROR|wxOK);
661 $dialog->ShowModal;
662 $dialog->Destroy;
663 return
664 }
665  
666 my $root_key = $registry->get_root_key;
667 if (!defined $registry) {
668 my $dialog = Wx::MessageDialog->new($self,
669 "'$basename' has no root key", 'Error', wxICON_ERROR|wxOK);
670 $dialog->ShowModal;
671 $dialog->Destroy;
672 return;
673 }
674  
675 # clear
676 $self->OnCloseFile;
677  
678 # set up
679 $self->{_root_key} = $root_key;
680 $self->{_tree}->SetRootKey($root_key);
681 $self->{_tree}->SetFocus;
682 $self->SetTitle("$basename - Registry Viewer");
683 }
684  
685 sub OnOpenFile {
686 my ($self, $event) = @_;
687  
688 my $dialog = Wx::FileDialog->new($self, 'Select Registry File', $self->{_directory} || '');
689  
690 if ($dialog->ShowModal != wxID_OK) {
691 return;
692 }
693  
694 my $filename = $dialog->GetPath;
695 $self->{_directory} = $dialog->GetDirectory;
696  
697 $self->LoadFile($filename);
698 }
699  
700 sub OnCloseFile {
701 my ($self, $event) = @_;
702 $self->{_tree}->Clear;
703 $self->{_list}->Clear;
704 $self->{_text}->Clear;
705 $self->{_statusbar}->SetStatusText('');
706 $self->{_root_key} = undef;
707 $self->{_find_iter} = undef;
708 $self->{_keys_by_time} = undef;
709 $self->SetTitle("Registry Viewer");
710 if (defined $self->{_timeline_dialog}) {
711 $self->{_timeline_dialog}->SetTimeline({});
712 $self->{_timeline_dialog}->Hide;
713 }
714 }
715  
716 sub OnQuit {
717 my ($self) = @_;
718  
719 $self->Close;
720 }
721  
722  
723 package FindDialog;
724  
725 use Wx qw(:everything);
726 use Wx::Event qw(:everything);
727  
728 use base qw(Wx::Dialog);
729  
730 sub new {
731 my ($class, $parent) = @_;
732  
733 my $self = $class->SUPER::new($parent, -1, "Find", wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE);
734 bless $self, $class;
735  
736 my $static = Wx::StaticText->new($self, -1, 'Enter text to &search for:');
737 my $text = Wx::TextCtrl->new($self, -1, '');
738 my $check1 = Wx::CheckBox->new($self, -1, 'Search &keys');
739 my $check2 = Wx::CheckBox->new($self, -1, 'Search &values');
740 my $radio = Wx::RadioBox->new($self, -1, 'Start searching', wxDefaultPosition, wxDefaultSize, ['from root key', 'from current key'], 1);
741  
742 my $sizer = Wx::BoxSizer->new(wxVERTICAL);
743 $sizer->Add($static, 0, wxEXPAND|wxALL, 5);
744 $sizer->Add($text, 0, wxEXPAND|wxALL, 5);
745 $sizer->Add($check1, 0, wxALL, 5);
746 $sizer->Add($check2, 0, wxALL, 5);
747 $sizer->Add($radio, 0, wxALL, 5);
748  
749 my $button_sizer = $self->CreateSeparatedButtonSizer(wxOK|wxCANCEL);
750  
751 $sizer->Add($button_sizer, 0, wxEXPAND|wxALL, 5);
752  
753 $self->SetSizer($sizer);
754  
755 $self->{_text} = $text;
756 $self->{_check1} = $check1;
757 $self->{_check2} = $check2;
758 $self->{_radio} = $radio;
759  
760 $self->Fit; # resize dialog to best fit child windows
761  
762 $self->{_text}->SetFocus;
763 $self->SetFocus;
764  
765 EVT_CHECKBOX($self, $check1, sub {
766 if (!$check1->GetValue && !$check2->GetValue) {
767 $check2->SetValue(1);
768 }
769 });
770 EVT_CHECKBOX($self, $check2, sub {
771 if (!$check1->GetValue && !$check2->GetValue) {
772 $check1->SetValue(1);
773 }
774 });
775  
776 return $self;
777 }
778  
779 sub GetSearchKeys {
780 my ($self) = @_;
781 return $self->{_check1}->GetValue;
782 }
783  
784 sub GetSearchValues {
785 my ($self) = @_;
786 return $self->{_check2}->GetValue;
787 }
788  
789 sub GetText {
790 my ($self) = @_;
791 return $self->{_text}->GetValue;
792 }
793  
794 sub GetSearchSelected {
795 my ($self) = @_;
796 return $self->{_radio}->GetSelection;
797 }
798  
799 sub SetSearchKeys {
800 my ($self, $state) = @_;
801 $state = 1 if !defined $state;
802 $self->{_check1}->SetValue($state);
803 }
804  
805 sub SetSearchValues {
806 my ($self, $state) = @_;
807 $state = 1 if !defined $state;
808 $self->{_check2}->SetValue($state);
809 }
810  
811 sub SetText {
812 my ($self, $value) = @_;
813 $value = '' if !defined $value;
814 $self->{_text}->ChangeValue($value);
815 $self->{_text}->SetSelection(-1, -1);
816 }
817  
818 sub SetSearchSelected {
819 my ($self, $n) = @_;
820 $n = 0 if !defined $n;
821 $self->{_radio}->SetSelection($n);
822 }
823  
824  
825 package TimeListCtrl;
826  
827 use Parse::Win32Registry qw(iso8601);
828 use Wx qw(:everything);
829  
830 use base qw(Wx::ListCtrl);
831  
832 sub new {
833 my ($class, $parent) = @_;
834  
835 my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [200, -1], wxLC_REPORT|wxLC_SINGLE_SEL|wxLC_VIRTUAL|wxBORDER_SUNKEN);
836 bless $self, $class;
837  
838 $self->InsertColumn(0, "Time");
839 $self->InsertColumn(1, "Count");
840  
841 $self->SetColumnWidth(0, 200);
842  
843 $self->{_times} = [];
844 $self->{_key_counts} = [];
845  
846 return $self;
847 }
848  
849 sub OnGetItemText {
850 my ($self, $index, $column) = @_;
851  
852 if ($column == 0) {
853 return iso8601($self->{_times}[$index]);
854 }
855 elsif ($column == 1) {
856 return $self->{_key_counts}[$index];
857 }
858 else {
859 return "?";
860 }
861 }
862  
863 sub SetTimes {
864 my ($self, $times, $key_counts) = @_;
865  
866 $self->{_times} = $times;
867 $self->{_key_counts} = $key_counts;
868 $self->SetItemCount(scalar @$times);
869 $self->Refresh;
870 $self->SetItemState(0, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
871 }
872  
873 sub GetTime {
874 my ($self, $index) = @_;
875  
876 return $self->{_times}[$index];
877 }
878  
879  
880 package KeyListCtrl;
881  
882 use Wx qw(:everything);
883 use Wx::ArtProvider qw(:artid :clientid);
884  
885 use base qw(Wx::ListCtrl);
886  
887 sub new {
888 my ($class, $parent) = @_;
889  
890 my $self = $class->SUPER::new($parent, -1, wxDefaultPosition, [200, -1], wxLC_REPORT|wxLC_SINGLE_SEL|wxLC_VIRTUAL|wxBORDER_SUNKEN);
891 bless $self, $class;
892  
893 my $imagelist = Wx::ImageList->new(16, 16, 1);
894 $imagelist->Add(Wx::ArtProvider::GetIcon(wxART_FOLDER, wxART_MENU, [16, 16]));
895 $self->AssignImageList($imagelist, wxIMAGE_LIST_SMALL);
896  
897 $self->InsertColumn(0, "Key");
898  
899 $self->SetColumnWidth(0, 280);
900  
901 return $self;
902 }
903  
904 sub OnGetItemText {
905 my ($self, $index, $column) = @_;
906  
907 my $key = $self->{_keys}[$index];
908 return if !defined $key;
909 if ($column == 0) {
910 my $key_path = $key->get_path;
911 $key_path =~ s/\0/[NUL]/g;
912 $key_path =~ s/\n/[LF]/g;
913 $key_path =~ s/\r/[CR]/g;
914 return $key_path;
915 }
916 else {
917 return "?";
918 }
919 }
920  
921 sub OnGetItemImage {
922 my ($self, $index) = @_;
923  
924 return 0;
925 }
926  
927 sub SetKeys {
928 my ($self, $keys) = @_;
929  
930 $self->{_keys} = $keys;
931 $self->SetItemCount(scalar @$keys);
932 $self->Refresh;
933 $self->SetItemState(0, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
934 }
935  
936 sub GetKey {
937 my ($self, $index) = @_;
938  
939 return $self->{_keys}[$index];
940 }
941  
942  
943 package TimelineDialog;
944  
945 use Wx qw(:everything);
946 use Wx::Event qw(:everything);
947  
948 use base qw(Wx::Frame);
949  
950 use constant ID_CLOSE_DIALOG => Wx::NewId;
951  
952 sub new {
953 my ($class, $parent) = @_;
954  
955 my $self = $class->SUPER::new($parent, -1, "Timeline", wxDefaultPosition, [600, 300]);
956 bless $self, $class;
957  
958 $self->SetMinSize([600, 300]);
959  
960 my $hsplitter = Wx::SplitterWindow->new($self, -1, wxDefaultPosition, wxDefaultSize, wxSP_NOBORDER);
961  
962 my $list1 = TimeListCtrl->new($hsplitter);
963  
964 my $list2 = KeyListCtrl->new($hsplitter);
965  
966 $hsplitter->SplitVertically($list1, $list2);
967 $hsplitter->SetMinimumPaneSize(10);
968  
969 $self->{_list1} = $list1;
970 $self->{_list2} = $list2;
971  
972 my $accelerators = Wx::AcceleratorTable->new(
973 [0, WXK_ESCAPE, ID_CLOSE_DIALOG],
974 [wxACCEL_CTRL, ord('W'), ID_CLOSE_DIALOG],
975 );
976 $self->SetAcceleratorTable($accelerators);
977  
978 EVT_MENU($self, ID_CLOSE_DIALOG, \&OnClose);
979  
980 EVT_SPLITTER_DCLICK($self, $hsplitter, \&OnSplitterDClick);
981  
982 EVT_LIST_ITEM_SELECTED($self, $list1, \&OnTimeListItemSelected);
983  
984 EVT_CLOSE($self, \&OnClose);
985  
986 $self->SetIcon(Wx::GetWxPerlIcon());
987  
988 return $self;
989 }
990  
991 sub OnSplitterDClick {
992 my ($self, $event) = @_;
993  
994 $event->Veto;
995 }
996  
997 sub OnTimeListItemSelected {
998 my ($self, $event) = @_;
999  
1000 my $index = $event->GetIndex;
1001 my $time = $self->{_list1}->GetTime($index);
1002 $self->{_list2}->SetKeys($self->{_keys_by_time}{$time});
1003 }
1004  
1005 sub OnKeyListItemActivated {
1006 my ($self, $event) = @_;
1007  
1008 $self->Close;
1009 }
1010  
1011 sub SetTimeline {
1012 my ($self, $keys_by_time) = @_;
1013  
1014 my @times = sort keys %$keys_by_time;
1015 my @key_counts = map { scalar @{$keys_by_time->{$_}} } @times;
1016  
1017 my $list1 = $self->{_list1};
1018 $list1->SetTimes(\@times, \@key_counts);
1019  
1020 my $list2 = $self->{_list2};
1021 $list2->SetKeys([]);
1022  
1023 $self->{_times} = \@times;
1024 $self->{_keys_by_time} = $keys_by_time;
1025 }
1026  
1027 sub OnClose {
1028 my ($self, $event) = @_;
1029  
1030 $self->Hide;
1031 }
1032  
1033  
1034 package ViewApp;
1035  
1036 use Wx qw(:everything);
1037  
1038 use base qw(Wx::App);
1039  
1040 sub OnInit {
1041 my ($self) = @_;
1042  
1043 my $frame = ViewFrame->new(undef);
1044 $frame->Show;
1045  
1046 return 1;
1047 }
1048  
1049  
1050 package main;
1051  
1052 my $app = ViewApp->new;
1053 $app->MainLoop;