File Coverage

blib/lib/RecentInfo/Manager.pm
Criterion Covered Total %
statement 62 70 88.5
branch 7 12 58.3
condition 4 5 80.0
subroutine 9 10 90.0
pod 4 5 80.0
total 86 102 84.3


line stmt bran cond sub pod time code
1             package RecentInfo::Manager 0.04;
2 2     2   182142 use 5.020;
  2         9  
3 2     2   1080 use experimental 'signatures', 'postderef';
  2         5112  
  2         12  
4              
5 2     2   417 use Exporter 'import';
  2         3  
  2         73  
6 2     2   1223 use Module::Load;
  2         2744  
  2         14  
7             our @EXPORT_OK = (qw(add_recent_file remove_recent_file recent_files));
8              
9             =head1 NAME
10              
11             RecentInfo::Manager - manage recent documents
12              
13             =head1 SYNOPSIS
14              
15             use RecentInfo::Manager 'add_recent_file';
16             add_recent_file('output.pdf');
17              
18             # oo interface
19             my $mgr = RecentInfo::Manager->new();
20             $mgr->load();
21             $mgr->add('output.pdf');
22             $mgr->save;
23              
24             =head1 FUNCTIONS
25              
26             =head2 C<< add_recent_file $filename, $file_options >>
27              
28             add_recent_file( 'output.pdf', { mime_type => 'application/pdf' } );
29              
30             Adds C as a recently used (or created) file for the current
31             application. If the MIME filetype is not given, it is inferred from
32             the filename.
33              
34             =cut
35              
36 2     2 1 101940 sub add_recent_file($filename, $file_options={}, $options={}) {
  2         6  
  2         6  
  2         4  
  2         4  
37 2         15 my $mgr = RecentInfo::Manager->new(%$options);
38              
39 2 100       128 if( ! ref $filename ) {
40 1         2 $filename = [ [$filename => $file_options] ];
41             }
42              
43             my @files = map {
44 2 100       8 ! ref $_ ? [$_ => $file_options] : $_
  3         15  
45             } $filename->@*;
46              
47 2         5 for my $f (@files) {
48 3         15 $mgr->add( $f->@* );
49             };
50 2         8 $mgr->save();
51             };
52              
53             =head2 C<< remove_recent_file $filename >>
54              
55             remove_recent_file( 'oops.xls' );
56              
57             Removes the given file from the list of recently accessed files.
58              
59             =cut
60              
61 1     1 1 100868 sub remove_recent_file($filename, $options={}) {
  1         2  
  1         3  
  1         3  
62 1         8 my $mgr = RecentInfo::Manager->new(%$options);
63              
64 1 50       29 if( ! ref $filename ) {
65 0         0 $filename = [ $filename ];
66             }
67              
68 1         4 my @files = $filename->@*;
69              
70 1         3 for my $f (@files) {
71 2         8 $mgr->remove( $f );
72             };
73 1         4 $mgr->save();
74             };
75              
76 0     0 0 0 sub mime_match( $type, $pattern ) {
  0         0  
  0         0  
  0         0  
77 0         0 $pattern =~ s/\*/.*/g;
78 0         0 $type =~ /$pattern/
79             }
80              
81             =head2 C<< recent_files $options >>
82              
83             my @entries = recent_files( { mime_type => 'application/pdf' });
84              
85             Returns a list of filenames of the recently accessed files.
86             In the options hash, you can pass in the following keys:
87              
88             =over 4
89              
90             =item B - search for the given MIME type. C<*> is a wildcard.
91              
92             =item B - search for the given application name.
93              
94             =back
95              
96             =cut
97              
98 6     6 1 386023 sub recent_files($recent_options=undef, $options={}) {
  6         36  
  6         16  
  6         12  
99 6         72 my $mgr = RecentInfo::Manager->new(%$options);
100 6   100     1404 $recent_options //= {
101             app => $mgr->app,
102             };
103              
104 6         30 my $appname = $recent_options->{app};
105 6         17 my $mimetype = $recent_options->{mime_type};
106 2         12 my @res = map { $_->to_native } grep {
107 6 0       128 defined $appname ? grep { $_->name eq $appname } $_->applications->@*
  4 50       187  
  4         28  
108             : defined $mimetype ? mime_match( $_->mime_type, $mimetype )
109             : 1
110             } $mgr->entries->@*;
111              
112             return @res
113 6         13534 };
114              
115             =head1 METHODS
116              
117             The module also acts as a factory for OS-specific implementations.
118              
119             =head2 C<< ->new >>
120              
121             my $mgr = RecentInfo::Manager->new();
122             $mgr->load();
123             $mgr->add('output.pdf');
124             $mgr->save;
125              
126             =cut
127              
128             our $implementation;
129 9     9 1 21 sub new($factoryclass, @args) {
  9         25  
  9         27  
  9         14  
130 9   66     37 $implementation //= $factoryclass->_best_implementation();
131              
132             # return a new instance
133 9         409 $implementation->new(@args);
134             }
135              
136 1     1   2 sub _best_implementation( $class, @candidates ) {
  1         2  
  1         1  
  1         1  
137 1         2 my $impl;
138 1 50       4 if( $^O =~ /cygwin|MSWin32/ ) {
139 0         0 $impl = 'RecentInfo::Manager::Windows';
140             } else {
141 1         1 $impl = 'RecentInfo::Manager::XBEL';
142             }
143 1         6 load $impl;
144 1         15 return $impl;
145             };
146              
147             1;
148              
149             =head1 SEE ALSO
150              
151             L - recent documents for old MacOS
152              
153             =head1 REPOSITORY
154              
155             The public repository of this module is
156             L.
157              
158             =head1 SUPPORT
159              
160             The public support forum of this module is L.
161              
162             =head1 BUG TRACKER
163              
164             Please report bugs in this module via Github
165             at L
166              
167             =head1 AUTHOR
168              
169             Max Maischein C
170              
171             =head1 COPYRIGHT (c)
172              
173             Copyright 2024-2024 by Max Maischein C.
174              
175             =head1 LICENSE
176              
177             This module is released under the same terms as Perl itself.
178              
179             =cut
180