| 1 | package Web::Dash; | ||
| 2 | |||
| 3 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 2 | ▶ | use Plack::Request; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | ▶ | use Plack::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 2 | ▶ | use File::Find (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use Web::Dash::Lens; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | use Encode; | ||
| 10 | use Future::Q 0.012; | ||
| 11 | use AnyEvent::DBus 0.31; | ||
| 12 | use AnyEvent; | ||
| 13 | use JSON qw(to_json); | ||
| 14 | use Try::Tiny; | ||
| 15 | use Carp; | ||
| 16 | |||
| 17 | my $index_page_template = <<'EOD'; | ||
| 18 | <!DOCTYPE html> | ||
| 19 | <html> | ||
| 20 | <head> | ||
| 21 | <title>Web Dash</title> | ||
| 22 | <style type="text/css"> | ||
| 23 | body { | ||
| 24 | padding: 3px 5px; | ||
| 25 | color: #222; | ||
| 26 | font-size: small; | ||
| 27 | } | ||
| 28 | |||
| 29 | a, a:visited, a:active { | ||
| 30 | color: #302b95; | ||
| 31 | } | ||
| 32 | |||
| 33 | a:hover { | ||
| 34 | text-decoration: underline; | ||
| 35 | } | ||
| 36 | |||
| 37 | ul { | ||
| 38 | list-style-type: none; | ||
| 39 | padding: 0; | ||
| 40 | margin: 0; | ||
| 41 | } | ||
| 42 | |||
| 43 | li, .search-result-hint, .search-result-error { | ||
| 44 | padding: 3px 4px; | ||
| 45 | margin: 3px; | ||
| 46 | border-width: 0; | ||
| 47 | border-radius: 3px; | ||
| 48 | background-color: #f6f6f6; | ||
| 49 | } | ||
| 50 | |||
| 51 | .search-result-hint { | ||
| 52 | background-color: #dcffaf; | ||
| 53 | padding-left: 10px; | ||
| 54 | } | ||
| 55 | |||
| 56 | .search-result-error { | ||
| 57 | background-color: #ffafc2; | ||
| 58 | padding-left: 10px; | ||
| 59 | } | ||
| 60 | |||
| 61 | #lens-selector { | ||
| 62 | width: 200px; | ||
| 63 | float: left; | ||
| 64 | } | ||
| 65 | |||
| 66 | #results { | ||
| 67 | margin: 0 10px 0 210px; | ||
| 68 | } | ||
| 69 | |||
| 70 | .search-result { | ||
| 71 | padding-left: 10px; | ||
| 72 | overflow: auto; | ||
| 73 | } | ||
| 74 | |||
| 75 | .search-result-list-limited { | ||
| 76 | overflow: auto; | ||
| 77 | max-height: 200px; | ||
| 78 | } | ||
| 79 | |||
| 80 | .search-category { | ||
| 81 | font-size: normal; | ||
| 82 | margin: 5px 0px; | ||
| 83 | } | ||
| 84 | |||
| 85 | .search-category-num, .search-result-list-toggler { | ||
| 86 | margin-left: 8px; | ||
| 87 | font-size: small; | ||
| 88 | font-weight: normal; | ||
| 89 | } | ||
| 90 | |||
| 91 | .search-result-name { | ||
| 92 | margin: 0; | ||
| 93 | padding: 0 0 5px 0; | ||
| 94 | font-size: normal; | ||
| 95 | } | ||
| 96 | |||
| 97 | .search-result-icon { | ||
| 98 | display: block; | ||
| 99 | max-width: 80px; | ||
| 100 | max-height: 80px; | ||
| 101 | float: left; | ||
| 102 | margin-right: 5px; | ||
| 103 | } | ||
| 104 | |||
| 105 | .search-result-desc { | ||
| 106 | color: #505050; | ||
| 107 | font-size: small; | ||
| 108 | } | ||
| 109 | |||
| 110 | </style> | ||
| 111 | </head> | ||
| 112 | <body> | ||
| 113 | <div> | ||
| 114 | <input id="query" type="text" autofocus autocomplete="off" /> | ||
| 115 | <span id="spinner"></span> | ||
| 116 | <span id="results-num"></span> | ||
| 117 | </div> | ||
| 118 | <ul id="lens-selector"> | ||
| 119 | [%LENSES_LIST%] | ||
| 120 | </ul> | ||
| 121 | <div id="results"> | ||
| 122 | <div class="search-result-hint">Hint: You can change lens with arrow Up and Down keys.</div> | ||
| 123 | </div> | ||
| 124 | <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | ||
| 125 | <script type="text/javascript"> | ||
| 126 | $(function() { | ||
| 127 | var executeSearch = function(lens_name, query_string) { | ||
| 128 | return $.ajax({ | ||
| 129 | url: "search.json", | ||
| 130 | data: { lens: lens_name, q: query_string }, | ||
| 131 | dataType: "json", | ||
| 132 | type: 'GET', | ||
| 133 | }).then(null, function(jqxhr, text_status, error_thrown) { | ||
| 134 | return $.Deferred().reject("ajax error: " + text_status + ", " + error_thrown); | ||
| 135 | }); | ||
| 136 | }; | ||
| 137 | var SimpleSpinner = function(sel) { | ||
| 138 | this.sel = sel; | ||
| 139 | this.count = 0; | ||
| 140 | this.timer = null; | ||
| 141 | this.dot_pos = 0; | ||
| 142 | this.dot_length = 4; | ||
| 143 | this.full_length = 12; | ||
| 144 | this.interval_ms = 100; | ||
| 145 | }; | ||
| 146 | SimpleSpinner.prototype = { | ||
| 147 | _clear: function() { | ||
| 148 | $(this.sel).empty(); | ||
| 149 | }, | ||
| 150 | _render: function() { | ||
| 151 | var self = this; | ||
| 152 | var str = ""; | ||
| 153 | var i; | ||
| 154 | var dot_min = (self.dot_pos + self.dot_length) % self.full_length; | ||
| 155 | for(i = 0 ; i < self.full_length ; i++) { | ||
| 156 | if((self.dot_pos <= dot_min && i >= self.dot_pos && i < dot_min) | ||
| 157 | || (self.dot_pos > dot_min && (i >= self.dot_pos || i < dot_min) )) { | ||
| 158 | str += "."; | ||
| 159 | }else { | ||
| 160 | str += " "; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | $(self.sel).html(str); | ||
| 164 | }, | ||
| 165 | _set: function(new_count) { | ||
| 166 | var self = this; | ||
| 167 | self.count = new_count; | ||
| 168 | if(self.count <= 0) { | ||
| 169 | self.count = 0; | ||
| 170 | if(self.timer !== null) { | ||
| 171 | clearInterval(self.timer); | ||
| 172 | self.timer = null; | ||
| 173 | } | ||
| 174 | self._clear(); | ||
| 175 | } | ||
| 176 | if(self.count > 0 && self.timer === null) { | ||
| 177 | self.timer = setInterval(function() { | ||
| 178 | self.dot_pos = (self.dot_pos + 1) % self.full_length; | ||
| 179 | self._render(); | ||
| 180 | }, self.interval_ms) | ||
| 181 | } | ||
| 182 | }, | ||
| 183 | begin: function() { this._set(this.count + 1) }, | ||
| 184 | end: function() { this._set(this.count - 1) } | ||
| 185 | }; | ||
| 186 | var EventRegulator = function(wait, handler) { | ||
| 187 | this.wait_ms = wait; | ||
| 188 | this.handler = handler; | ||
| 189 | this.timeout_obj = null; | ||
| 190 | }; | ||
| 191 | EventRegulator.prototype = { | ||
| 192 | trigger: function(task) { | ||
| 193 | var self = this; | ||
| 194 | if(self.timeout_obj !== null) { | ||
| 195 | clearTimeout(self.timeout_obj); | ||
| 196 | } | ||
| 197 | self.timeout_obj = setTimeout(function() { | ||
| 198 | self.handler(task); | ||
| 199 | self.timeout_obj = null; | ||
| 200 | }, self.wait_ms); | ||
| 201 | }, | ||
| 202 | }; | ||
| 203 | |||
| 204 | var spinner = new SimpleSpinner('#spinner'); | ||
| 205 | |||
| 206 | var results_manager = { | ||
| 207 | LIMIT_THRESHOLD_ITEM_NUM: 5, | ||
| 208 | sel: '#results', | ||
| 209 | sel_num: '#results-num', | ||
| 210 | showError: function(error) { | ||
| 211 | $(this.sel_num).empty(); | ||
| 212 | var $results = $(this.sel); | ||
| 213 | $results.empty(); | ||
| 214 | $('<div class="search-result-error"></div>').text(error).appendTo($results); | ||
| 215 | }, | ||
| 216 | _createCategoryGroups: function(results) { | ||
| 217 | var groups = {}; | ||
| 218 | var groups_list = []; | ||
| 219 | $.each(results, function(i, result) { | ||
| 220 | if(result.name === "") return true; | ||
| 221 | if(!(result.category_index in groups)) { | ||
| 222 | groups[result.category_index] = []; | ||
| 223 | } | ||
| 224 | groups[result.category_index].push(result); | ||
| 225 | }); | ||
| 226 | $.each(groups, function(i, group) { | ||
| 227 | groups_list.push(group); | ||
| 228 | }); | ||
| 229 | return groups_list.sort(function(list_a, list_b) { | ||
| 230 | return list_a.length > list_b.length; | ||
| 231 | }); | ||
| 232 | }, | ||
| 233 | _setListLimit: function($category_header, is_limited) { | ||
| 234 | var $category_list = $category_header.next("ul"); | ||
| 235 | var $toggler = $category_header.find(".search-result-list-toggler"); | ||
| 236 | if($toggler.size() === 0) return; | ||
| 237 | if(is_limited) { | ||
| 238 | // toggle to limited | ||
| 239 | $category_list.addClass("search-result-list-limited"); | ||
| 240 | $toggler.text("Show more"); | ||
| 241 | }else { | ||
| 242 | // toggle to unlimited | ||
| 243 | $category_list.removeClass("search-result-list-limited"); | ||
| 244 | $toggler.text("Show less"); | ||
| 245 | } | ||
| 246 | }, | ||
| 247 | toggleListLimit: function($category_header) { | ||
| 248 | this._setListLimit($category_header, | ||
| 249 | !$category_header.next("ul").hasClass("search-result-list-limited")); | ||
| 250 | }, | ||
| 251 | show: function(results) { | ||
| 252 | var self = this; | ||
| 253 | var $results = $(self.sel); | ||
| 254 | var category_group_list = self._createCategoryGroups(results) | ||
| 255 | $(self.sel_num).text("total " + results.length + " result" + (results.length > 1 ? "s" : "")); | ||
| 256 | $results.empty(); | ||
| 257 | $.each(category_group_list, function(group_index, group) { | ||
| 258 | var $results_list = $('<ul></ul>'); | ||
| 259 | var $category = $('<h2 class="search-category"></h2>'); | ||
| 260 | $category.text(group[0].category.name); | ||
| 261 | $('<span class="search-category-num"></span>') | ||
| 262 | .text(group.length + (group.length > 1 ? " results" : " result")).appendTo($category); | ||
| 263 | if(group.length > self.LIMIT_THRESHOLD_ITEM_NUM) { | ||
| 264 | $('<a class="search-result-list-toggler" href="#"></a>').appendTo($category); | ||
| 265 | } | ||
| 266 | $category.appendTo($results); | ||
| 267 | $.each(group, function(j, result) { | ||
| 268 | var $li = $('<li class="search-result"></li>'); | ||
| 269 | var $name = $('<h3 class="search-result-name"></h3>'); | ||
| 270 | if(result.dnd_uri === "") { | ||
| 271 | $name.text(result.name); | ||
| 272 | }else { | ||
| 273 | $('<a></a>').attr('href', result.dnd_uri).text(result.name).appendTo($name); | ||
| 274 | } | ||
| 275 | $li.append($name); | ||
| 276 | if(result.icon_hint && result.icon_hint.match("^https?://")) { | ||
| 277 | $('<img />').attr('src', result.icon_hint).addClass('search-result-icon').appendTo($li); | ||
| 278 | } | ||
| 279 | if(result.comment !== "") { | ||
| 280 | $('<div class="search-result-desc"></div>').text(result.comment).appendTo($li); | ||
| 281 | } | ||
| 282 | $li.appendTo($results_list); | ||
| 283 | }); | ||
| 284 | $results_list.appendTo($results); | ||
| 285 | self._setListLimit($category, (group_index !== category_group_list.length - 1)); | ||
| 286 | }); | ||
| 287 | }, | ||
| 288 | }; | ||
| 289 | var lens_manager = { | ||
| 290 | lens_index: 0, | ||
| 291 | sel_lens_index: '#lens-selector', | ||
| 292 | on_change_listeners: [], | ||
| 293 | setLensIndex: function(new_index) { | ||
| 294 | var self = this; | ||
| 295 | var $radios = $(self.sel_lens_index).find('input'); | ||
| 296 | var changed = (self.lens_index != new_index); | ||
| 297 | self.lens_index = new_index % $radios.size(); | ||
| 298 | if(self.lens_index < 0) self.lens_index += $radios.size(); | ||
| 299 | |||
| 300 | $radios.removeAttr('checked'); | ||
| 301 | $radios.get(self.lens_index).checked = true; | ||
| 302 | if(changed) { | ||
| 303 | $.each(self.on_change_listeners, function(i, listener) { | ||
| 304 | listener(self); | ||
| 305 | }); | ||
| 306 | } | ||
| 307 | }, | ||
| 308 | getLensIndex: function() { | ||
| 309 | return this.lens_index; | ||
| 310 | }, | ||
| 311 | getCurrentLensName: function() { | ||
| 312 | var self = this; | ||
| 313 | return $(self.sel_lens_index).find('input').eq(self.lens_index).attr("value"); | ||
| 314 | }, | ||
| 315 | up: function() { this.setLensIndex(this.getLensIndex() - 1) }, | ||
| 316 | down: function() { this.setLensIndex(this.getLensIndex() + 1) }, | ||
| 317 | onChange: function(listener) { | ||
| 318 | var self = this; | ||
| 319 | self.on_change_listeners.push(listener); | ||
| 320 | }, | ||
| 321 | }; | ||
| 322 | lens_manager.setLensIndex(0); | ||
| 323 | |||
| 324 | var search_form = { | ||
| 325 | sel_query: '#query', | ||
| 326 | |||
| 327 | execute: function() { | ||
| 328 | var query_string = $(this.sel_query).val(); | ||
| 329 | if(query_string === "") { | ||
| 330 | return; | ||
| 331 | } | ||
| 332 | var lens_name = lens_manager.getCurrentLensName(); | ||
| 333 | spinner.begin(); | ||
| 334 | executeSearch(lens_name, query_string).then(function(result_object) { | ||
| 335 | if(result_object.error !== null) { | ||
| 336 | return $.Deferred().reject(result_object.error); | ||
| 337 | } | ||
| 338 | if(result_object.results.length === 0) { | ||
| 339 | return $.Deferred().reject("No result for query '" + query_string + "'"); | ||
| 340 | } | ||
| 341 | results_manager.show(result_object.results); | ||
| 342 | }).then(null, function(error) { | ||
| 343 | results_manager.showError(error); | ||
| 344 | return $.Deferred().resolve(); | ||
| 345 | }).then(function() { | ||
| 346 | spinner.end(); | ||
| 347 | }); | ||
| 348 | }, | ||
| 349 | }; | ||
| 350 | |||
| 351 | |||
| 352 | lens_manager.onChange(function() { search_form.execute() }); | ||
| 353 | var type_event_regulator = new EventRegulator(500, function() { | ||
| 354 | search_form.execute(); | ||
| 355 | }); | ||
| 356 | $('#query').on('input', function() { | ||
| 357 | type_event_regulator.trigger(); | ||
| 358 | }).on('keydown', function(e) { | ||
| 359 | switch(e.keyCode) { | ||
| 360 | case 38: | ||
| 361 | lens_manager.up(); | ||
| 362 | break; | ||
| 363 | case 40: | ||
| 364 | lens_manager.down(); | ||
| 365 | break; | ||
| 366 | } | ||
| 367 | }); | ||
| 368 | $('#lens-selector').on('click', 'input', function(e) { | ||
| 369 | lens_manager.setLensIndex($(this).data('lens-index')); | ||
| 370 | }); | ||
| 371 | $('#results').on('click', '.search-result-list-toggler', function(e) { | ||
| 372 | results_manager.toggleListLimit($(this).parent()); | ||
| 373 | return false; | ||
| 374 | }); | ||
| 375 | }); | ||
| 376 | </script> | ||
| 377 | </body> | ||
| 378 | </html> | ||
| 379 | EOD | ||
| 380 | |||
| 381 | sub _render_lenses_info { | ||
| 382 | my ($lenses_info_ref) = @_; | ||
| 383 | return join "", map { | ||
| 384 | my $lens = $lenses_info_ref->[$_]; | ||
| 385 | my $name = Plack::Util::encode_html($lens->{name}); | ||
| 386 | my $hint = Plack::Util::encode_html($lens->{hint}); | ||
| 387 | qq{<li><label><input type="radio" name="lens" value="$name" data-lens-index="$_" />$hint</label></li>\n}; | ||
| 388 | } 0 .. $#$lenses_info_ref; | ||
| 389 | } | ||
| 390 | |||
| 391 | sub new { | ||
| 392 | my ($class, %args) = @_; | ||
| 393 | my $self = bless { | ||
| 394 | lenses => [], | ||
| 395 | lens_for_service_name => {}, | ||
| 396 | cache_index_page => undef, | ||
| 397 | }, $class; | ||
| 398 | if(defined $args{lenses}) { | ||
| 399 | croak "lenses param must be an array-ref" if ref($args{lenses}) ne 'ARRAY'; | ||
| 400 | $self->{lenses} = $args{lenses}; | ||
| 401 | }else { | ||
| 402 | $self->_init_lenses(defined($args{lenses_dir}) ? $args{lenses_dir} : '/usr/share/unity/lenses'); | ||
| 403 | } | ||
| 404 | |||
| 405 | if(@{$self->{lenses}}) { | ||
| 406 | ## ** Wait for all the lenses to respond and recreate them. | ||
| 407 | ## ** This is because lenses can be unstable at first. | ||
| 408 | ## ** See xt/spawning-lens.t for detail. | ||
| 409 | my $cv = AnyEvent->condvar; | ||
| 410 | foreach my $lens (@{$self->{lenses}}) { | ||
| 411 | $cv->begin; | ||
| 412 | $lens->search_hint->then(sub { $cv->end }) | ||
| 413 | } | ||
| 414 | $cv->recv; | ||
| 415 | $self->_recreate_lenses(); | ||
| 416 | %{$self->{lens_for_service_name}} = map { $_->service_name => $_ } @{$self->{lenses}}; | ||
| 417 | } | ||
| 418 | |||
| 419 | return $self; | ||
| 420 | } | ||
| 421 | |||
| 422 | sub _init_lenses { | ||
| 423 | my ($self, @search_dirs) = @_; | ||
| 424 | File::Find::find(sub { | ||
| 425 | my $filepath = $File::Find::name; | ||
| 426 | return if $filepath !~ /\.lens$/; | ||
| 427 | push(@{$self->{lenses}}, Web::Dash::Lens->new(lens_file => $filepath)); | ||
| 428 | }, @search_dirs); | ||
| 429 | } | ||
| 430 | |||
| 431 | sub _recreate_lenses { | ||
| 432 | my ($self) = @_; | ||
| 433 | my @new_lenses = map { $_->clone } @{$self->{lenses}}; | ||
| 434 | $self->{lenses} = \@new_lenses; | ||
| 435 | } | ||
| 436 | |||
| 437 | sub _render_index { | ||
| 438 | my ($self, $req) = @_; | ||
| 439 | return sub { | ||
| 440 | my ($responder) = @_; | ||
| 441 | if(defined $self->{cache_index_page}) { | ||
| 442 | $responder->([ | ||
| 443 | 200, ['Content-Type', 'text/html; charset=utf8'], | ||
| 444 | [$self->{cache_index_page}] | ||
| 445 | ]); | ||
| 446 | return; | ||
| 447 | } | ||
| 448 | Future::Q->wait_all(map { $_->search_hint } @{$self->{lenses}})->then(sub { | ||
| 449 | my (@search_hints) = map { $_->get } @_; | ||
| 450 | my @lenses_info = map { | ||
| 451 | my $hint = $search_hints[$_]; | ||
| 452 | my $lens = $self->{lenses}[$_]; | ||
| 453 | +{hint => $hint, name => $lens->service_name}; | ||
| 454 | } (0 .. $#search_hints); | ||
| 455 | my $lenses_list = _render_lenses_info(\@lenses_info); | ||
| 456 | my $page = $index_page_template; | ||
| 457 | $page =~ s/\[%LENSES_LIST%\]/$lenses_list/; | ||
| 458 | $page = Encode::encode('utf8', $page); | ||
| 459 | $self->{cache_index_page} = $page; | ||
| 460 | $responder->([ | ||
| 461 | 200, ['Content-Type', 'text/html; charset=utf8'], | ||
| 462 | [$page] | ||
| 463 | ]); | ||
| 464 | })->catch(sub { | ||
| 465 | my $error = shift; | ||
| 466 | $responder->([ | ||
| 467 | 500, ['Content-Type', 'text/plain'], | ||
| 468 | [Encode::encode('utf8', $error)] | ||
| 469 | ]); | ||
| 470 | }); | ||
| 471 | }; | ||
| 472 | } | ||
| 473 | |||
| 474 | sub _json_response { | ||
| 475 | my ($response_object, $code) = @_; | ||
| 476 | if(!defined($code)) { | ||
| 477 | $code = $response_object->{error} ? 500 : 200; | ||
| 478 | } | ||
| 479 | return [ | ||
| 480 | $code, ['Content-Type', 'application/json; charset=utf8'], | ||
| 481 | [to_json($response_object, {ascii => 1})] | ||
| 482 | ]; | ||
| 483 | } | ||
| 484 | |||
| 485 | sub _render_search { | ||
| 486 | my ($self, $req) = @_; | ||
| 487 | return sub { | ||
| 488 | my $responder = shift; | ||
| 489 | my $lens_name = $req->query_parameters->{lens} || 0; | ||
| 490 | my $query_string = Encode::decode('utf8', scalar($req->query_parameters->{'q'}) || ''); | ||
| 491 | my $lens = $self->{lens_for_service_name}{$lens_name}; | ||
| 492 | Future::Q->try(sub { | ||
| 493 | if(not defined $lens) { | ||
| 494 | die "Unknown lens name: $lens_name\n"; | ||
| 495 | } | ||
| 496 | return $lens->search($query_string); | ||
| 497 | })->then(sub { | ||
| 498 | my @results = @_; | ||
| 499 | if(@results) { | ||
| 500 | return Future::Q->needs_all(map { $lens->category($_->{category_index}) } @results)->then(sub { | ||
| 501 | my (@categories) = @_; | ||
| 502 | foreach my $i (0 .. $#categories) { | ||
| 503 | $results[$i]{category} = $categories[$i]; | ||
| 504 | } | ||
| 505 | return @results; | ||
| 506 | })->catch(sub { | ||
| 507 | my $e = shift; | ||
| 508 | warn "WARN: $e"; | ||
| 509 | return @results; | ||
| 510 | }); | ||
| 511 | } | ||
| 512 | return @results; | ||
| 513 | })->then(sub { | ||
| 514 | $responder->(_json_response({error => undef, results => \@_}), 200); | ||
| 515 | })->catch(sub { | ||
| 516 | my $e = shift; | ||
| 517 | $responder->(_json_response({error => $e}, 500)); | ||
| 518 | }); | ||
| 519 | }; | ||
| 520 | } | ||
| 521 | |||
| 522 | sub to_app { | ||
| 523 | my $self = shift; | ||
| 524 | return sub { | ||
| 525 | my ($env) = @_; | ||
| 526 | my $req = Plack::Request->new($env); | ||
| 527 | if($req->path eq '/') { | ||
| 528 | return $self->_render_index($req); | ||
| 529 | }elsif($req->path eq '/search.json') { | ||
| 530 | return $self->_render_search($req); | ||
| 531 | }else { | ||
| 532 | return [404, ['Content-Type', 'text/plain'], ['Not Found']]; | ||
| 533 | } | ||
| 534 | }; | ||
| 535 | } | ||
| 536 | |||
| 537 | |||
| 538 | our $VERSION = '0.04'; | ||
| 539 | |||
| 540 | 1; | ||
| 541 | |||
| 542 | __END__ | ||
| 543 | |||
| 544 | =pod | ||
| 545 | |||
| 546 | =head1 NAME | ||
| 547 | |||
| 548 | Web::Dash - Unity Dash from Web browsers (experimental) | ||
| 549 | |||
| 550 | =head1 DESCRIPTION | ||
| 551 | |||
| 552 | L<Web::Dash> is a Web application version of Unity Dash. | ||
| 553 | Unity Dash is a powerful searching tool integrated in Unity desktop environment, | ||
| 554 | which is employed by Ubuntu Linux. | ||
| 555 | |||
| 556 | For detail of Unity, See L<https://wiki.ubuntu.com/Unity> | ||
| 557 | |||
| 558 | L<Web::Dash> acts like Unity Dash without the need of the whole Unity infrastructure. | ||
| 559 | All you need is some Lenses (searching agents) and your favorite Web browser, | ||
| 560 | and you can have the awesome searching power of Dash. | ||
| 561 | |||
| 562 | =head1 CAVEAT | ||
| 563 | |||
| 564 | =head2 This is an experimental application | ||
| 565 | |||
| 566 | L<Web::Dash> is quite an B<experimental> application. | ||
| 567 | |||
| 568 | It is not based on any official specification or documentation about Unity Dash or Unity Lens. | ||
| 569 | Instead, I analyzed the behavior of Unity Dash and Unity Lenses from outside, | ||
| 570 | and implemented what I guess was the correct usage of them. | ||
| 571 | |||
| 572 | I tested L<Web::Dash> in Ubuntu 12.04 and Xubuntu 12.04. | ||
| 573 | However it may not work as expected in other environments. | ||
| 574 | It is also possible for L<Web::Dash> to stop working in future versions of Unity or Ubuntu. | ||
| 575 | |||
| 576 | =head2 Privacy issues | ||
| 577 | |||
| 578 | Some Lenses are meant to search the local file system. | ||
| 579 | If you export those lenses to others, they are able to | ||
| 580 | see names of your files. | ||
| 581 | |||
| 582 | =head1 SCREENSHOTS | ||
| 583 | |||
| 584 | See L<https://github.com/debug-ito/Web-Dash/wiki/Screenshots> | ||
| 585 | |||
| 586 | =head1 TUTORIAL | ||
| 587 | |||
| 588 | =head2 Installation | ||
| 589 | |||
| 590 | To install L<Web::Dash>, first you need development tools and files for libexpat and libdbus. | ||
| 591 | |||
| 592 | $ sudo apt-get install build-essential pkg-config libexpat1-dev libdbus-1-dev | ||
| 593 | |||
| 594 | Then, type | ||
| 595 | |||
| 596 | $ sudo sh -c 'wget -O- http://cpanmin.us | perl - Web::Dash' | ||
| 597 | |||
| 598 | to install L<Web::Dash> from CPAN. | ||
| 599 | |||
| 600 | Of course, if you already have a CPAN client installed, you can type | ||
| 601 | |||
| 602 | $ sudo cpanm Web::Dash | ||
| 603 | |||
| 604 | |||
| 605 | =head2 Installing some lenses | ||
| 606 | |||
| 607 | Most Unity Lenses are provided as .deb packages named C<unity-lens-*>. | ||
| 608 | |||
| 609 | In Ubuntu, some Lens packages are served by "extra" repository. | ||
| 610 | Make sure the repository is enabled in your system. | ||
| 611 | |||
| 612 | $ grep extras /etc/apt/sources.list | ||
| 613 | deb http://extras.ubuntu.com/ubuntu precise main | ||
| 614 | deb-src http://extras.ubuntu.com/ubuntu precise main | ||
| 615 | |||
| 616 | Uncomment the deb lines if they are commented out. | ||
| 617 | |||
| 618 | To install Github lens, for example, type | ||
| 619 | |||
| 620 | $ sudo apt-get update | ||
| 621 | $ sudo apt-get install unity-lens-github | ||
| 622 | |||
| 623 | |||
| 624 | =head2 Start webdash | ||
| 625 | |||
| 626 | To start webdash, type | ||
| 627 | |||
| 628 | $ webdash | ||
| 629 | Twiggy: Accepting connections at http://127.0.0.1:5000/ | ||
| 630 | |||
| 631 | Access the URL with a Web browser. | ||
| 632 | |||
| 633 | |||
| 634 | =head2 Run webdash in Ubuntu Server | ||
| 635 | |||
| 636 | In a non-GUI environment, first you need to execute the following. | ||
| 637 | |||
| 638 | $ eval `dbus-launch --auto-syntax` | ||
| 639 | |||
| 640 | This will launch a DBus daemon for a session bus, | ||
| 641 | and set the environment variables necessary to access the bus. | ||
| 642 | |||
| 643 | After that, run C<webdash> as usual. | ||
| 644 | |||
| 645 | $ webdash | ||
| 646 | |||
| 647 | If you run C<webdash> at startup (e.g., in /etc/rc.local), | ||
| 648 | B<< make sure to specify C<LANG> environment variable. >> | ||
| 649 | Lens processes need the C<LANG> environment variable to be set. | ||
| 650 | |||
| 651 | |||
| 652 | =head1 WEB API | ||
| 653 | |||
| 654 | L<Web::Dash> has a Web API for searching. See L<Web::Dash::WebAPI>. | ||
| 655 | |||
| 656 | |||
| 657 | =head1 AS A MODULE | ||
| 658 | |||
| 659 | As a Perl module, L<Web::Dash> provides a class object that can generate a L<PSGI> application. | ||
| 660 | You can use L<Web::Dash> in your .psgi file to customize which Lenses to export. | ||
| 661 | |||
| 662 | =head1 SYNOPSIS | ||
| 663 | |||
| 664 | In your app.psgi file. | ||
| 665 | |||
| 666 | use Web::Dash; | ||
| 667 | |||
| 668 | Web::Dash->new(lenses_dir => '/your/personal/lenses/directory')->to_app; | ||
| 669 | |||
| 670 | Or, if you want to select lenses... | ||
| 671 | |||
| 672 | use Web::Dash; | ||
| 673 | use Web::Dash::Lens; | ||
| 674 | |||
| 675 | my @lenses; | ||
| 676 | foreach my $lens_file ( | ||
| 677 | 'extras-unity-lens-github', 'video' | ||
| 678 | ) { | ||
| 679 | push(@lenses, Web::Dash::Lens->new( | ||
| 680 | lens_file => "/usr/share/unity/lenses/$lens_file/$lens_file.lens" | ||
| 681 | )); | ||
| 682 | } | ||
| 683 | Web::Dash->new(lenses => \@lenses)->to_app; | ||
| 684 | |||
| 685 | |||
| 686 | =head1 CLASS METHODS | ||
| 687 | |||
| 688 | =head2 $dash = Web::Dash->new(%args) | ||
| 689 | |||
| 690 | The constructor. | ||
| 691 | |||
| 692 | Fields in C<%args> are: | ||
| 693 | |||
| 694 | =over | ||
| 695 | |||
| 696 | =item C<lenses_dir> => DIRECTORY_PATH (optional, default: '/usr/share/unity/lenses') | ||
| 697 | |||
| 698 | Specifies the root directory path under which it searches for lens files. | ||
| 699 | |||
| 700 | It loads *.lens files under this directory and creates L<Web::Dash::Lens> objects from them. | ||
| 701 | |||
| 702 | =item C<lenses> => ARRAYREF_OF_LENSES (optional) | ||
| 703 | |||
| 704 | Specifies an array-ref of L<Web::Dash::Lens> objects that you want to use with L<Web::Dash>. | ||
| 705 | |||
| 706 | If this option is specified, C<lenses_dir> option is ignored. | ||
| 707 | |||
| 708 | =back | ||
| 709 | |||
| 710 | =head1 OBJECT METHODS | ||
| 711 | |||
| 712 | =head2 $psgi_app = $dash->to_app() | ||
| 713 | |||
| 714 | Creates a L<PSGI> application from the C<$dash>. | ||
| 715 | |||
| 716 | Note that the PSGI application uses L<AnyEvent> for asynchronous responses. | ||
| 717 | Use L<AnyEvent>-compatible PSGI servers (like L<Twiggy>) to run the app. | ||
| 718 | |||
| 719 | =head1 SEE ALSO | ||
| 720 | |||
| 721 | =over | ||
| 722 | |||
| 723 | =item L<webdash> | ||
| 724 | |||
| 725 | Web::Dash daemon runner script. | ||
| 726 | |||
| 727 | =item L<Web::Dash::Lens> | ||
| 728 | |||
| 729 | An experimental Unity Lens object. | ||
| 730 | |||
| 731 | =back | ||
| 732 | |||
| 733 | =head1 AUTHOR | ||
| 734 | |||
| 735 | Toshio Ito C<< <toshioito [at] cpan.org> >> | ||
| 736 | |||
| 737 | =head1 REPOSITORY | ||
| 738 | |||
| 739 | L<https://github.com/debug-ito/Web-Dash> | ||
| 740 | |||
| 741 | =head1 LICENSE AND COPYRIGHT | ||
| 742 | |||
| 743 | Copyright 2013 Toshio Ito. | ||
| 744 | |||
| 745 | This program is free software; you can redistribute it and/or modify it | ||
| 746 | under the terms of the the Artistic License (2.0). You may obtain a | ||
| 747 | copy of the full license at: | ||
| 748 | |||
| 749 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||
| 750 | |||
| 751 | Any use, modification, and distribution of the Standard or Modified | ||
| 752 | Versions is governed by this Artistic License. By using, modifying or | ||
| 753 | distributing the Package, you accept this license. Do not use, modify, | ||
| 754 | or distribute the Package, if you do not accept this license. | ||
| 755 | |||
| 756 | If your Modified Version has been derived from a Modified Version made | ||
| 757 | by someone other than you, you are nevertheless required to ensure that | ||
| 758 | your Modified Version complies with the requirements of this license. | ||
| 759 | |||
| 760 | This license does not grant you the right to use any trademark, service | ||
| 761 | mark, tradename, or logo of the Copyright Holder. | ||
| 762 | |||
| 763 | This license includes the non-exclusive, worldwide, free-of-charge | ||
| 764 | patent license to make, have made, use, offer to sell, sell, import and | ||
| 765 | otherwise transfer the Package with respect to any patent claims | ||
| 766 | licensable by the Copyright Holder that are necessarily infringed by the | ||
| 767 | Package. If you institute patent litigation (including a cross-claim or | ||
| 768 | counterclaim) against any party alleging that the Package constitutes | ||
| 769 | direct or contributory patent infringement, then this Artistic License | ||
| 770 | to you shall terminate on the date that such litigation is filed. | ||
| 771 | |||
| 772 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||
| 773 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||
| 774 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||
| 775 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||
| 776 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||
| 777 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||
| 778 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||
| 779 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 780 | |||
| 781 | |||
| 782 | =cut | ||