blib/lib/Web/Dash/Lens.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 SCAR 0.0
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Web/Dash/DeeModel.pm ↑ Summary
1 package Web::Dash::Lens;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Future::Q 0.012;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Net::DBus;
Subroutine BEGIN: called CC=1 SCAR=2.6
9 use Net::DBus::Reactor;
10 use Net::DBus::Annotation qw(dbus_call_noreply);
11 use Web::Dash::DeeModel;
12 use Web::Dash::Util qw(future_dbus_call);
13 use Encode;
14 use Async::Queue 0.02;
15 use utf8;
16  
17 my %SCHEMA_RESULTS = (
18 0 => 'uri',
19 1 => 'icon_hint',
20 2 => 'category_index',
21 3 => 'mimetype',
22 4 => 'name',
23 5 => 'comment',
24 6 => 'dnd_uri'
25 );
26  
27 my %SCHEMA_CATEGORIES = (
28 0 => 'name',
29 1 => 'icon_hint',
30 2 => 'renderer',
31 );
32  
33 sub new {
34 my ($class, %args) = @_;
35 my $self = bless {
36 reactor => $args{reactor} || Net::DBus::Reactor->main,
37 service_name => undef,
38 object_name => undef,
39 bus => undef,
40 bus_address => undef,
41 query_object => undef,
42 results_model_future => Future::Q->new,
43 search_hint_future => Future::Q->new,
44 categories_future => Future::Q->new,
45 request_queue => undef,
46 }, $class;
47 $self->_init_queue($args{concurrency});
48 $self->_init_bus(defined $args{bus_address} ? $args{bus_address} : ':session');
49 $self->_init_service(@args{qw(lens_file service_name object_name)});
50  
51 ## --- Procedure to connect to remote Lens service
52  
53 ## 1. Get hold of query_object
54 ## query_object is the main entry point to the Lens service in DBus.
55 ## Its service name and object name are normally obtained from .lens file.
56 ## query_object implements com.canonical.Unity.Lens interface.
57
58 $self->{query_object} =
59 $self->{bus}->get_service($self->{service_name})->get_object($self->{object_name}, 'com.canonical.Unity.Lens');
60 {
61 ## 2. Fetch Lens meta information
62 ## We then have to obtain meta information about the lens.
63 ## query_object broadcasts such information by "Changed" signal,
64 ## so we listen to it here. "Changed" signal is emitted when
65 ## "InfoRequest" method is called on the query_object.
66
67 weaken (my $self = $self); ## prevent memory leak
68 my $sigid; $sigid = $self->{query_object}->connect_to_signal('Changed', sub {
69 my ($result_arrayref) = @_;
70 my ($obj_name, $flag1, $flag2, $search_hint, $unknown,
71 $service_results, $service_global_results, $service_categories, $service_filters) = @$result_arrayref;
72  
73 ## 4. Obtain search_hint and some Dee Model objects
74 ## "Changed" signal conveys a number of values. I'm not able to
75 ## figure out all of their meanings. The forth value ($search_hint)
76 ## is a short description of the Lens.
77  
78 ## The last four values are DBus service names for Dee Model objects.
79 ## Lenses use these objects to export various data to DBus. Such data
80 ## include search results and categories of the results. A Dee Model
81 ## object's DBus object name is determined from the service name.
82 ## A Dee Model object is represented by Web::Dash::DeeModel class here.
83
84 $self->{query_object}->disconnect_from_signal('Changed', $sigid);
85 $self->{search_hint_future}->fulfill(Encode::decode('utf8', $search_hint));
86  
87 ## Results Model exports Search results. We will use the Model object
88 ## later when searching.
89 $self->{results_model_future}->fulfill(Web::Dash::DeeModel->new(
90 bus => $self->{bus},
91 service_name => $service_results,
92 schema => \%SCHEMA_RESULTS,
93 ));
94  
95 ## Categories Model exports meta information about categories
96 ## of search results. Here we cache the category information,
97 ## and throw away the Model object.
98 my $categories_model = Web::Dash::DeeModel->new(
99 bus => $self->{bus},
100 service_name => $service_categories,
101 schema => \%SCHEMA_CATEGORIES,
102 );
103 $categories_model->get()->then(sub {
104 $self->{categories_future}->fulfill(@_) if defined $self;
105 }, sub {
106 $self->{categories_future}->reject(@_) if defined $self;
107 });
108 });
109 }
110
111 ## 3. call "InfoRequest" method to make "Changed" signal fire.
112 $self->{query_object}->InfoRequest(dbus_call_noreply);
113 return $self;
114 }
115  
116 sub service_name { shift->{service_name} }
117 sub object_name { shift->{object_name} }
118  
119 sub _init_bus {
120 my ($self, $bus_address) = @_;
121 $self->{bus_address} = $bus_address;
122 if($bus_address eq ':session') {
123 $self->{bus} = Net::DBus->session;
124 }elsif($bus_address eq ':system') {
125 $self->{bus} = Net::DBus->system;
126 }else {
127 $self->{bus} = Net::DBus->new($bus_address);
128 }
129 }
130  
131 sub _remove_delims {
132 my ($str) = @_;
133 $str =~ s|^[^a-zA-Z0-9_\-\.\/]+||;
134 $str =~ s|[^a-zA-Z0-9_\-\.\/]+$||;
135 return $str;
136 }
137  
138 sub _init_service {
139 my ($self, $lens_file, $service_name, $object_name) = @_;
140 if(defined $lens_file) {
141 open my $file, "<", $lens_file or croak "Cannot read $lens_file: $!";
142 while(my $line = <$file>) {
143 chomp $line;
144 my ($key, $val) = split(/=/, $line);
145 next if not defined $val;
146 $key = _remove_delims($key);
147 $val = _remove_delims($val);
148 if($key eq 'DBusName') {
149 $self->{service_name} = $val;
150 }elsif($key eq 'DBusPath') {
151 $self->{object_name} = $val;
152 }
153 }
154 close $file;
155 }
156 $self->{service_name} = $service_name if defined $service_name;
157 $self->{object_name} = $object_name if defined $object_name;
158 if(!defined($self->{service_name}) || !defined($self->{object_name})) {
159 croak 'Specify either lens_file or combination of service_name and object_name in new()';
160 }
161 }
162  
163 sub _wait_on {
164 my ($self, $future) = @_;
165 my @result;
166 my $exception;
167 my $is_immediate = 1;
168 $future->then(sub {
169 @result = @_;
170 $self->{reactor}->shutdown if !$is_immediate;
171 }, sub {
172 $exception = shift;
173 $self->{reactor}->shutdown if !$is_immediate;
174 });
175 $is_immediate = 0;
176 $self->{reactor}->run if $future->is_pending;
177 die $exception if defined $exception;
178 return @result;
179 }
180  
181 sub search_hint {
182 my ($self) = @_;
183 return $self->{search_hint_future};
184 }
185  
186 sub search_hint_sync {
187 my ($self) = @_;
188 my ($desc) = $self->_wait_on($self->search_hint);
189 return $desc;
190 }
191  
192 sub _init_queue {
193 my ($self, $concurrency) = @_;
194  
195 ## --- Procedure of searching
196 ## Concurrency of this procedure is regulated by Async::Queue.
197
198 weaken $self; ## prevent memory leak
199 $self->{request_queue} = Async::Queue->new(
200 concurrency => $concurrency,
201 worker => sub {
202 my ($task, $queue_done) = @_;
203 my ($query_string, $final_future) = @$task;
204 $self->{results_model_future}->then(sub {
205 ## 1. Call "Search" method on query_object with search query.
206  
207 return future_dbus_call($self->{query_object}, "Search", $query_string, {});
208 })->then(sub {
209 ## 2. Obtain search results from Results Model object
210 ## The return value of "Search" method is NOT search results.
211 ## It contains a sequence number pointing to a state of the
212 ## Results Model object. We then obtain search results from the
213 ## Results Model object. However, the current sequence number of
214 ## the Results Model may be different from the one got from
215 ## query_object. That is possible when multiple processes are
216 ## making search queries concurrently. If that happens, the
217 ## obtained search result is discarded because it is not for
218 ## the query we made.
219  
220 my ($search_result) = @_;
221 my $exp_seqnum = $search_result->{'model-seqnum'};
222 my $results_model = $self->{results_model_future}->get;
223 return $results_model->get($exp_seqnum);
224 })->then(sub {
225 my (@results) = @_;
226 $final_future->fulfill(@results);
227 $queue_done->();
228 })->catch(sub {
229 $final_future->reject(@_);
230 $queue_done->();
231 });
232 }
233 );
234 }
235  
236 sub search {
237 my ($self, $query_string) = @_;
238 my $outer_future = Future::Q->new;
239 $self->{request_queue}->push([$query_string, $outer_future]);
240 return $outer_future;
241 }
242  
243 sub search_sync {
244 my ($self, $query_string) = @_;
245 return $self->_wait_on($self->search($query_string));
246 }
247  
248 sub clone {
249 my ($self) = @_;
250 return ref($self)->new(
251 service_name => $self->service_name,
252 object_name => $self->object_name,
253 reactor => $self->{reactor},
254 bus_address => $self->{bus_address},
255 concurrency => $self->{request_queue}->concurrency,
256 );
257 }
258  
259 sub category {
260 my ($self, $category_index) = @_;
261 return $self->{categories_future}->then(sub {
262 my @categories = @_;
263 if(not defined $categories[$category_index]) {
264 die "Invalid category_index: $category_index\n";
265 }
266 return $categories[$category_index];
267 });
268 }
269  
270 sub category_sync {
271 my ($self, $category_index) = @_;
272 my ($result) = $self->_wait_on($self->category($category_index));
273 return $result;
274 }
275  
276 our $VERSION = '0.031';
277  
278 1;
279  
280 __END__
281  
282 =pod
283  
284 =head1 NAME
285  
286 Web::Dash::Lens - An experimental Unity Lens object
287  
288 =head1 VERSION
289  
290 0.031
291  
292 =head1 SYNOPSIS
293  
294 use Web::Dash::Lens;
295 use utf8;
296 use Encode qw(encode);
297
298 sub show_results {
299 my (@results) = @_;
300 foreach my $result (@results) {
301 print "-----------\n";
302 print encode('utf8', "$result->{name}\n");
303 print encode('utf8', "$result->{description}\n");
304 print encode('utf8', "$result->{uri}\n");
305 }
306 print "=============\n";
307 }
308
309 my $lens = Web::Dash::Lens->new(lens_file => '/usr/share/unity/lenses/applications/applications.lens');
310
311
312 ## Synchronous query
313 my @search_results = $lens->search_sync("terminal");
314 show_results(@search_results);
315
316
317 ## Asynchronous query
318 use Future::Q;
319 use Net::DBus::Reactor;
320
321 $lens->search("terminal")->then(sub {
322 my @search_results = @_;
323 show_results(@search_results);
324 Net::DBus::Reactor->main->shutdown;
325 })->catch(sub {
326 my $e = shift;
327 warn "Error: $e";
328 Net::DBus::Reactor->main->shutdown;
329 });
330 Net::DBus::Reactor->main->run();
331  
332  
333 =head1 DESCRIPTION
334  
335 L<Web::Dash::Lens> is an object that represents a Unity Lens.
336 Note that this module is for using lenses, not for creating your own lenses.
337  
338 =head1 CAVEAT
339  
340 If you use L<AnyEvent::DBus>, do not use C<*_sync()> methods.
341 Instead you have to use asynchronous methods and explicit condition variables.
342  
343 my $cv = AnyEvent->condvar;
344 $lens->search_hint()->then(sub {
345 $cv->send(@_);
346 }, sub {
347 $cv->croak(shift);
348 });
349 my $search_hint = $cv->recv;
350  
351 This is because L<AnyEvent::DBus> replaces the DBus reactor
352 that is not completely compatible with the original L<Net::DBus::Reactor> objects.
353  
354 =head1 CLASS METHOD
355  
356 =head2 $lens = Web::Dash::Lens->new(%args)
357  
358 The constructor.
359  
360 Fields in C<%args> are:
361  
362 =over
363  
364 =item C<lens_file> => FILE_PATH (semi-optional)
365  
366 The file path to .lens file.
367 Usually you can find lens files installed under C</usr/share/unity/lenses/>.
368  
369 You must either specify C<lens_file> or combination of C<service_name> and C<object_name>.
370  
371 =item C<service_name> => DBUS_SERVICE_NAME (semi-optional)
372  
373 DBus service name of the lens.
374  
375 In a .lens file, the service name is specified by C<DBusName> field.
376  
377 =item C<object_name> => DBUS_OBJECT_NAME (semi-optional)
378  
379 DBus object name of the lens.
380  
381 In a .lens file, the object name is specified by C<DBusPath> field.
382  
383 =item C<reactor> => L<Net::DBus::Reactor> object (optional, default: C<< Net::DBus::Reactor->main >>)
384  
385 The L<Net::DBus::Reactor> object.
386 This object is needed for *_sync() methods.
387  
388 =item C<bus_address> => DBUS_BUS_ADDRESS (optional, default: ":session")
389  
390 The DBus bus address where this module searches for the lens service.
391  
392 If C<bus_address> is ":session", the session bus will be used.
393 If C<bus_address> is ":system", the system bus will be used.
394 Otherwise, C<bus_address> is passed to C<< Net::DBus->new() >> method.
395  
396 =item C<concurrency> => CONCURRENCY_NUM (optional, default: 1)
397  
398 The maximum number of asynchronous search queries that the lens handles simultaneously.
399  
400 If you call searching methods more than this value before any of the requests is complete,
401 the extra requests are queued in the lens and processed later.
402  
403 Setting C<concurrency> to 0 means there is no concurrency limit.
404  
405 =back
406  
407 =head1 OBJECT METHODS
408  
409 =head2 @results = $lens->search_sync($query_string)
410  
411 Makes a search with the C<$query_string> using the C<$lens>.
412 C<$query_string> must be a text string, not a binary (octet) string.
413  
414 In success, this method returns a list of search results (C<@results>).
415 Each element in C<@results> is a hash-ref containing the following key-value pairs.
416 All the string values are text strings, not binary (octet) strings.
417  
418 =over
419  
420 =item C<uri> => STR
421  
422 A URI of the result entry.
423 This URI is designed for Unity.
424 B<< Normal applications should refer to C<dnd_uri> below >>.
425  
426 =item C<icon_hint> => STR
427  
428 A string that specifies the icon of the result entry.
429  
430  
431 =item C<category_index> => INT
432  
433 The category index for the result entry.
434 You can obtain category information by C<category()> method.
435  
436 =item C<mimetype> => STR
437  
438 MIME type of the result entry.
439  
440 =item C<name> => STR
441  
442 The name of the result entry.
443  
444 =item C<comment> => STR
445  
446 One line description of the result entry.
447  
448 =item C<dnd_uri> => STR
449  
450 A URI of the result entry.
451 This URI takes a form that most applications can comprehend.
452 "dnd" stands for "Drag and Drop", I guess.
453  
454 =back
455  
456 In failure, this method throws an exception.
457  
458 See also: L<https://wiki.ubuntu.com/Unity/Lenses#Schema>
459  
460  
461 =head2 $future = $lens->search($query_string)
462  
463 The asynchronous version of C<search_sync()> method.
464  
465 Instead of returning the results, this method returns a L<Future::Q> object
466 that represents the search results obtained in future.
467  
468 In success, C<$future> will be fulfilled. You can obtain the list of search results by C<< $future->get >> method.
469  
470 In failure, C<$future> will be rejected. You can obtain the exception by C<< $future->failure >> method.
471  
472  
473 =head2 $search_hint = $lens->search_hint_sync()
474  
475 Returns the search hint of the C<$lens>. The search hint is a short description of the C<$lens>.
476 C<$search_hint> is a text string, not a binary (or octet) string.
477  
478 =head2 $future = $lens->search_hint()
479  
480 The asynchronous version of C<search_hint_sync()> method.
481  
482 Instead of returning the results, this method returns a L<Future::Q> object
483 that represents the search hint obtained in future.
484  
485 When done, C<$future> will be fulfilled. You can obtain the search hint by C<< $future->get >> method.
486  
487 =head2 $category_hashref = $lens->category_sync($category_index)
488  
489 Returns a hash-ref describing the category specified by C<$category_index>.
490  
491 C<$category_index> is an integer greater than or equal to zero.
492  
493 C<$category_hashref> is an hash-ref containing information about the category.
494 It has the following key-value pairs.
495 All the string values are text strings, not binary (octet) strings.
496  
497 =over
498  
499 =item C<name> => STR
500  
501 The name of the category.
502  
503 =item C<icon_hint> => STR
504  
505 A string that specifies the icon of the category.
506  
507 =item C<renderer> => STR
508  
509 A string that specifies how the results in this category should be rendered.
510  
511 =back
512  
513 If C<$category_index> is invalid, it throws an exception.
514  
515 =head2 $future = $lens->category($category_index)
516  
517 The asynchronous version of C<category_sync()> method.
518  
519 Instead of returning the category hash-ref, this method returns a L<Future::Q> object.
520  
521 In success, C<$future> will be fulfilled. You can obtain the category hash-ref by C<< $future->get >> method.
522  
523 In failure, C<$future> will be rejected. You can obtain the exception by C<< $future->failure >> method.
524  
525  
526 =head2 $service_name = $lens->service_name
527  
528 Returns the DBus service name of the C<$lens>.
529  
530 =head2 $object_name = $lens->object_name
531  
532 Returns the DBus object name of the C<$lens>.
533  
534 =head2 $new_lens = $lens->clone
535  
536 Returns the clone of the C<$lens>.
537  
538 =head1 IMPLEMENTATION
539  
540 For how L<Web::Dash::Lens> communicates with a Lens process via DBus,
541 read the source code of L<Web::Dash::Lens> and L<Web::Dash::DeeModel>.
542 I left some comments there.
543  
544 =head1 AUTHOR
545  
546 Toshio Ito C<< <toshioito [at] cpan.org> >>
547  
548 =cut
« blib/lib/Web/Dash/DeeModel.pm ↑ Summary