~ubuntu-desktop/unity-lens-files/ubuntu

« back to all changes in this revision

Viewing changes to src/daemon.vala

  • Committer: Didier Roche
  • Date: 2012-01-12 17:28:26 UTC
  • mfrom: (14.2.190 MASTER)
  • mto: This revision was merged to the branch mainline in revision 149.
  • Revision ID: didier.roche@canonical.com-20120112172826-az8arm5v0nxjgf4o
Tags: upstream-5.0.0
ImportĀ upstreamĀ versionĀ 5.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
46
46
     * filter down the result set instead of rebuilding it */
47
47
    private LensSearch? previous_search;
48
48
    
49
 
    private bool is_dirty;
50
 
    
51
49
    construct
52
50
    {
53
51
      prepare_type_templates();
66
64
      
67
65
      previous_search = null;
68
66
      
69
 
      is_dirty = true;
70
 
      
71
67
      /* Bring up Zeitgeist interfaces */
72
68
      log = new Zeitgeist.Log();
73
69
      index = new Zeitgeist.Index();
89
85
      urls = new UrlChecker ();
90
86
 
91
87
      /* Listen for filter changes */
92
 
      scope.filters_changed.connect (
93
 
        () => {          
94
 
          if (scope.active_search != null)
95
 
            {
96
 
              is_dirty = true;
97
 
              scope.notify_property ("active-search");
98
 
            }
99
 
        }
100
 
      );
 
88
      scope.filters_changed.connect (() => {
 
89
        scope.queue_search_changed (SearchType.DEFAULT);
 
90
      });
 
91
 
 
92
      scope.generate_search_key.connect ((lens_search) => {
 
93
        return lens_search.search_string.strip ();
 
94
      });
101
95
 
102
96
      /* Listen for changes to the lens entry search */
103
 
      scope.notify["active-search"].connect (
104
 
        (obj, pspec) => {
105
 
          var search = scope.active_search;
106
 
          
107
 
          if (!(Utils.search_has_really_changed (previous_search, search) || is_dirty))
108
 
            return;
109
 
          
110
 
          is_dirty = false;
111
 
          
112
 
          if (search_is_invalid (search))
113
 
            {
114
 
              update_without_search_async.begin(search);
115
 
            }
116
 
          else
117
 
            {
118
 
              update_search_async.begin (search);
119
 
             }
120
 
          previous_search = search;
121
 
        }
122
 
      );
123
 
 
124
 
      /* Listen for changes to the global search */
125
 
      scope.notify["active-global-search"].connect (
126
 
        (obj, pspec) => {
127
 
 
128
 
          var search = scope.active_global_search;
129
 
          
130
 
          if (search_is_invalid (search))
131
 
            return;
132
 
          
133
 
          if (!Utils.search_has_really_changed (previous_search, search))
134
 
            return;
135
 
          
136
 
          update_global_search_async.begin (search);
137
 
          previous_search = search;
138
 
        }
139
 
      );
140
 
     
 
97
      scope.search_changed.connect ((lens_search, search_type, cancellable) =>
 
98
      {
 
99
        dispatch_search.begin (lens_search, search_type, cancellable);
 
100
      });
 
101
 
141
102
      lens.export ();
142
103
    }
143
104
 
 
105
    private async void dispatch_search (LensSearch lens_search,
 
106
                                        SearchType search_type,
 
107
                                        Cancellable cancellable)
 
108
    {
 
109
      if (search_type == SearchType.GLOBAL)
 
110
      {
 
111
        yield update_global_search_async (lens_search, cancellable);
 
112
      }
 
113
      else
 
114
      {
 
115
        yield update_search_async (lens_search, cancellable);
 
116
      }
 
117
 
 
118
      // make sure we don't forget to emit this (if we didn't get cancelled)
 
119
      if (!cancellable.is_cancelled ()) lens_search.finished ();
 
120
    }
 
121
 
144
122
    private void populate_filters ()
145
123
    {
146
124
      var filters = new GLib.List<Unity.Filter> ();
324
302
      return s;
325
303
    }
326
304
    
327
 
    private async void update_global_search_async (LensSearch search)
 
305
    private async void update_global_search_async (LensSearch search,
 
306
                                                   Cancellable cancellable)
328
307
    {
329
 
      var results_model = scope.global_results_model;
 
308
      var results_model = search.results_model;
330
309
 
331
310
      if (search_is_invalid (search))
332
311
        {
334
313
          return;
335
314
        }
336
315
      
337
 
      /* Prevent concurrent searches and concurrent updates of our models,
338
 
       * by preventing any notify signals from propagating to us.
339
 
       * Important: Remeber to thaw the notifys again! */
340
 
      scope.freeze_notify ();
341
 
      
342
316
      var search_string = prepare_search_string (search);
343
317
 
344
318
      var templates = new PtrArray.sized(1);
353
327
                                          0,
354
328
                                          20,
355
329
                                          ResultType.MOST_RECENT_SUBJECTS,
356
 
                                          null);
 
330
                                          cancellable);
357
331
 
358
332
        timer.stop ();
359
333
        debug ("Found %u/%u global results for search '%s' in %fms",
379
353
        Unity.FilesLens.append_events_sorted (results, results_model,
380
354
                                              min_size, max_size, false);
381
355
 
 
356
      } catch (IOError.CANCELLED ioe) {
 
357
        return;
382
358
      } catch (GLib.Error e) {
383
359
        warning ("Error performing global search '%s': %s",
384
360
                 search.search_string, e.message);
385
361
      }
386
 
      
387
 
      /* Allow new searches once we enter an idle again.
388
 
       * We don't do it directly from here as that could mean we start
389
 
       * changing the model even before we had flushed out current changes
390
 
       */
391
 
      Idle.add (() => {
392
 
        scope.thaw_notify ();
393
 
        return false;
394
 
      });
395
 
      
396
 
      search.finished ();
397
362
    }
398
363
    
399
 
    private async void update_search_async  (LensSearch search)
 
364
    private async void update_search_async (LensSearch search,
 
365
                                            Cancellable cancellable)
400
366
    {
401
367
      if (search_is_invalid (search))
402
368
        {
403
 
          update_without_search_async.begin (search);
 
369
          yield update_without_search_async (search, cancellable);
404
370
          return;
405
371
        }
406
372
 
407
 
      var results_model = scope.results_model;
 
373
      var results_model = search.results_model;
408
374
 
409
 
      /* Prevent concurrent searches and concurrent updates of our models,
410
 
       * by preventing any notify signals from propagating to us.
411
 
       * Important: Remeber to thaw the notifys again! */
412
 
      scope.freeze_notify ();
413
 
      
414
375
      var search_string = prepare_search_string (search);
415
376
 
416
377
      string type_id = get_current_type ();
423
384
      var templates = new PtrArray.sized(1);
424
385
      templates.add (type_templates.lookup (type_id));
425
386
 
426
 
 
427
387
      try {
428
388
        /* Get results ranked by recency */
429
389
        var timer = new Timer ();
433
393
                                          0,
434
394
                                          50,
435
395
                                          result_type,
436
 
                                          null);
 
396
                                          cancellable);
437
397
 
438
398
        timer.stop ();
439
399
        debug ("Found %u/%u results for search '%s' in %fms",
460
420
                                              min_size, max_size,
461
421
                                              origin_grouping);
462
422
 
463
 
        yield update_downloads_async (results_model, search.search_string);
464
 
        
 
423
        yield update_downloads_async (results_model, cancellable,
 
424
                                      search.search_string);
 
425
 
 
426
      } catch (IOError.CANCELLED ioe) {
 
427
        return;
465
428
      } catch (GLib.Error e) {
466
429
        warning ("Error performing global search '%s': %s",
467
430
                 search.search_string, e.message);
468
431
      }
469
 
      
470
 
      /* Allow new searches once we enter an idle again.
471
 
       * We don't do it directly from here as that could mean we start
472
 
       * changing the model even before we had flushed out current changes
473
 
       */
474
 
      Idle.add (() => {
475
 
        scope.thaw_notify ();
476
 
        return false;
477
 
      });
478
 
      
479
 
      search.finished ();
480
432
    }
481
433
 
482
434
    private string get_current_type ()
574
526
      }
575
527
    }
576
528
 
577
 
    private async void update_without_search_async (LensSearch search)
 
529
    private async void update_without_search_async (LensSearch search,
 
530
                                                    Cancellable cancellable)
578
531
    {
579
 
      var results_model = scope.results_model;
 
532
      var results_model = search.results_model;
580
533
 
581
 
      /* Prevent concurrent searches and concurrent updates of our models,
582
 
       * by preventing any notify signals from propagating to us.
583
 
       * Important: Remeber to thaw the notifys again! */
584
 
      scope.freeze_notify ();
585
 
      
586
534
      string type_id = get_current_type ();
587
535
 
588
536
      bool origin_grouping = type_id == "folders";
602
550
                                             Zeitgeist.StorageState.ANY,
603
551
                                             100,
604
552
                                             result_type,
605
 
                                             null);
 
553
                                             cancellable);
606
554
 
607
555
        timer.stop ();
608
556
        debug ("Found %u/%u no search results in %fms",
623
571
                                              min_size, max_size,
624
572
                                              origin_grouping);
625
573
 
626
 
        yield update_downloads_async (results_model);
 
574
        yield update_downloads_async (results_model, cancellable);
627
575
 
 
576
      } catch (IOError.CANCELLED ioe) {
 
577
        return;
628
578
      } catch (GLib.Error e) {
629
579
        warning ("Error performing empty search: %s",
630
580
                 e.message);
631
581
      }
632
 
      
633
 
      /* Allow new searches once we enter an idle again.
634
 
       * We don't do it directly from here as that could mean we start
635
 
       * changing the model even before we had flushed out current changes
636
 
       */
637
 
      Idle.add (() => {
638
 
        scope.thaw_notify ();
639
 
        return false;
640
 
      });
641
 
      
642
 
      search.finished ();
643
582
    }
644
583
 
645
584
    private void append_bookmarks (GLib.List<Bookmark> bookmarks,
655
594
    }
656
595
 
657
596
    private async void update_downloads_async (Dee.Model results_model,
658
 
                                               string? name_filter = null)
 
597
        Cancellable cancellable, string? name_filter = null) throws IOError
659
598
    {
660
599
      // FIXME: Store the Downloads folder and update on changes
661
600
      unowned string download_path =
673
612
                 download_path, e.message);
674
613
        return;
675
614
      }
 
615
 
 
616
      if (cancellable.is_cancelled ())
 
617
        throw new IOError.CANCELLED ("Search was cancelled");
676
618
      
677
619
      /* Sort files by mtime, we do an ugly nested ternary
678
620
       * to avoid potential long/int overflow */
726
668
    {
727
669
      /* Emitting notify here will make us recheck if the search results
728
670
       * need update. In the negative case this is a noop */
729
 
      is_dirty = true;
730
 
      scope.notify_property ("active-search");
 
671
      // FIXME: no it isn't a noop
 
672
      scope.queue_search_changed (SearchType.DEFAULT);
731
673
    }
732
 
    
733
 
  }  
 
674
  }
734
675
 
735
676
  private const string ATTR_HIDDEN = FILE_ATTRIBUTE_STANDARD_IS_HIDDEN;
736
677
  private const string ATTR_SIZE_AND_HIDDEN = FILE_ATTRIBUTE_STANDARD_SIZE +