File Coverage

blib/lib/Wrangler.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Wrangler;
2              
3 1     1   55456 use strict;
  1         3  
  1         62  
4 1     1   6 use warnings;
  1         2  
  1         40  
5              
6 1     1   1769 use Wrangler::PubSub;
  1         3  
  1         34  
7 1     1   1031 use Wrangler::Config;
  1         3  
  1         38  
8 1     1   480 use Wrangler::PluginManager;
  1         3  
  1         27  
9 1     1   669 use Wrangler::FileSystem::Layers;
  1         4  
  1         40  
10 1     1   1625 use Wrangler::Wx::App;
  0            
  0            
11              
12             our $VERSION = 2.14;
13             our $log = 0;
14             our $wishlist;
15              
16             sub new {
17             my $class = shift;
18             my $self = bless {
19             appname => 'Wrangler',
20             version => $VERSION,
21             @_
22             }, $class;
23              
24             ## load config
25             Wrangler::Config::read();
26              
27             ## simple CLI switch parsing
28             if(@ARGV){
29             for(@ARGV){
30             if($_ eq '--debug'){
31             $self->{debug} = 1;
32             $log = 1; # $log is non-OO for efficiency
33             }elsif($_ =~ /^\/|^\\/){
34             $Wrangler::Config::env{CLI_ChangeDirectory} = $_;
35             }
36             }
37             }
38              
39             ## establish PluginManager
40             Wrangler::PluginManager::load_plugins($self);
41              
42             ## establish our virtual file-system
43             $self->{fs} = Wrangler::FileSystem::Layers->new();
44              
45             Wrangler::PubSub::subscribe('file.activated', \&OnActivated,__PACKAGE__);
46             Wrangler::PubSub::subscribe('main.menubar.toggle', sub {
47             $self->{main}->OnToggleMenuBar($_[0]);
48             },__PACKAGE__);
49             Wrangler::PubSub::subscribe('main.navbar.toggle', sub {
50             $self->{main}->OnToggleNavbar($_[0]);
51             },__PACKAGE__);
52             Wrangler::PubSub::subscribe('main.sidebar.toggle', sub {
53             $self->{main}->OnToggleSidebar($_[0]);
54             },__PACKAGE__);
55             Wrangler::PubSub::subscribe('main.statusbar.toggle', sub {
56             $self->{main}->OnToggleStatusBar($_[0]);
57             },__PACKAGE__);
58             Wrangler::PubSub::subscribe('main.formeditor.recreate', sub {
59             $self->{main}->OnReCreateFormEditor($_[0]);
60             },__PACKAGE__);
61             Wrangler::PubSub::publish('status.update', "Init complete");
62              
63             return $self;
64             }
65              
66             sub config {
67             Wrangler::Config::config(@_);
68             }
69              
70             sub debug {
71             print $_[0] . "\n" if $log;
72             }
73              
74             sub wishlist {
75             # use Data::Dumper;
76             # print 'Wrangler::wishlist:'.Dumper($wishlist);
77             return [ keys %$wishlist ];
78             }
79              
80             sub run {
81             my $self = shift;
82              
83             ## create the Wx App and its main window
84             my $app = Wrangler::Wx::App->new(); # Create the application object
85             $self->{main} = $app->create($self);
86             $app->MainLoop; # Start event processing
87             }
88              
89             sub OnActivated {
90             Wrangler::debug("Main:: received event 'file.activated': @_");
91             my ($path,$richlist_item) = @_;
92              
93             if($richlist_item->{'MIME::mediaType'}){
94             my $viewers = $Wrangler::Config::settings{'openwith'};
95             if( $viewers->{ $richlist_item->{'MIME::mediaType'} . '/*' } ){
96             Wrangler::debug(" mediaType ".$richlist_item->{'MIME::mediaType'} ." has association ". $viewers->{ $richlist_item->{'MIME::mediaType'} . '/*' } .", appending path $richlist_item->{'Filesystem::Path'}) ");
97             my $pid = ForkAndExec($viewers->{ $richlist_item->{'MIME::mediaType'} . '/*' }, $richlist_item->{'Filesystem::Path'} );
98             Wrangler::PubSub::publish('status.update',"Launched external viewer with pid $pid");
99             return;
100             }
101             }
102              
103             Wrangler::debug("No viewer associated or MIME::mediaType unknown. returning. ");
104             }
105              
106             sub ForkAndExec {
107             my @commands = @_;
108              
109             my $pid = fork();
110              
111             print STDERR "unable to fork: $!" unless defined($pid);
112             return undef unless defined($pid);
113              
114             # contains no pid when we're in the forked child
115             if(!$pid){
116             exec(@commands);
117             die "unable to exec: $!";
118             }
119              
120             return $pid if $pid;
121             }
122              
123             sub DESTROY {
124             Wrangler::debug("Wrangler::DESTROY");
125              
126             ## store config if it has changed
127             Wrangler::Config::write()
128             }
129              
130             1;
131              
132             __END__