~motoster/gwibber/facebook-offers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
/*
 * Copyright (C) 2011 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3, as published
 * by the Free Software Foundation.

 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranties of
 * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
 * PURPOSE.  See the GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by Ken VanDine <ken.vandine@canonical.com>
 */

using Dee;
using Gee;
using Config;
using Unity;
using Gwibber;

namespace UnityGwibber {

  /* DBus name for the place. Must match out .place file */
  const string BUS_NAME = "com.canonical.Unity.Lens.Gwibber";
  const string ICON_PATH = Config.DATADIR + "/icons/unity-icon-theme/places/svg/";

  /**
   * The Daemon class implements all of the logic for the place.
   *
   */
  public class Daemon : GLib.Object
  {
    private Unity.Lens lens;
    private Unity.Scope scope;
    private Unity.PreferencesManager preferences = Unity.PreferencesManager.get_default ();
    private Gwibber.Streams streams_service;
    private Gwibber.Service service;
    private Gwibber.Accounts accounts;
    private Gwibber.Utils utils;
    private Dee.Model? _model = null;
    private Dee.Model? _streams_model = null;
    private Dee.Filter _sort_filter;
    /* Keep track of the previous search, so we can determine when to
     * filter down the result set instead of rebuilding it */
    private unowned Dee.ModelIter _stream_iter_first = null;
    private unowned Dee.ModelIter _stream_iter_last = null;

    private Dee.Analyzer _analyzer;
    private Dee.Index _index;
    private Dee.ICUTermFilter _ascii_filter;
    private Ag.Manager _account_manager;
    private bool _has_accounts = false;

    construct
    {
      lens = new Unity.Lens("/com/canonical/unity/lens/gwibber", "gwibber");
      lens.search_in_global = false;
      lens.search_hint = _("Enter name or content you would like to search for");
      lens.visible = false;
      try
      {
        lens.export ();
      } catch (GLib.IOError e)
      {
        warning ("failed to export lens: %s", e.message);
      }


      // Check for accounts
      _account_manager = new Ag.Manager.for_service_type("microblogging");
      GLib.List<Ag.AccountService> accts = _account_manager.get_enabled_account_services();
      foreach (Ag.AccountService account_service in accts) {
        Ag.Account account = account_service.get_account();
        account.set_enabled (false);
        message ("ACCOUNT PROVIDER: %s", account.get_provider_name());
      }

      // We only want to trigger starting gwibber-service if there are accounts
      if (accts.length() > 0)
      {
        _has_accounts = true;
        setup ();
      }

      _account_manager.enabled_event.connect ((id) =>
      {
        accts = _account_manager.get_enabled_account_services();
        if (accts.length() > 0 && !_has_accounts)
        {
          _has_accounts = true;
          setup ();
        }
        else if (accts.length() == 0)
        {
          lens.visible = false;
          _has_accounts = false;
        }
      });
    }


    void setup ()
    {
      scope = new Unity.Scope ("/com/canonical/unity/scope/gwibber");
      scope.search_in_global = false;
      scope.preview_uri.connect (preview);

      lens.visible = true;

      lens.add_local_scope (scope);

      /* Listen for filter changes */
      scope.notify["active"].connect(() =>
      {
        if (scope.active)
        {
          scope.queue_search_changed (SearchType.DEFAULT);
        }
      });

      scope.generate_search_key.connect ((lens_search) =>
      {
        return lens_search.search_string.strip ();
      });
      scope.search_changed.connect ((lens_search, search_type, cancellable) =>
      {
        if (search_type == SearchType.DEFAULT)
          update_scope_search.begin (lens_search, cancellable);
        else
          update_global_search.begin (lens_search, cancellable);
      });

      lens.notify["active"].connect ((obj, pspec) => {
        if (lens.active && scope.active)
        {
          if (_stream_iter_first != _model.get_first_iter () || _stream_iter_last != _model.get_last_iter ())
          {
            if (scope.active)
            {
              scope.queue_search_changed (SearchType.DEFAULT);
            }
          }
        }
      });

      scope.filters_changed.connect (() =>
      {
        scope.queue_search_changed (SearchType.DEFAULT);
      });

      preferences.notify["remote-content-search"].connect ((obj, pspec) =>
      {
        scope.queue_search_changed (SearchType.DEFAULT);
      });

    }

    private void setup_gwibber ()
    {
      streams_service = new Gwibber.Streams();
      service = new Gwibber.Service();
      utils = new Gwibber.Utils();
      accounts = new Gwibber.Accounts();

      populate_categories ();
      populate_filters();

      _streams_model = streams_service.stream_model;
      Intl.setlocale(LocaleCategory.COLLATE, "C");
      _sort_filter = Dee.Filter.new_collator_desc (StreamModelColumn.TIMESTAMP);
      _model = new Dee.FilterModel (_streams_model, _sort_filter);

      _ascii_filter = new Dee.ICUTermFilter.ascii_folder ();
      _analyzer = new Dee.TextAnalyzer ();
      _analyzer.add_term_filter ((terms_in, terms_out) =>
      {
        for (uint i = 0; i < terms_in.num_terms (); i++)
        {
          unowned string term = terms_in.get_term (i);
          var folded = _ascii_filter.apply (term);
          terms_out.add_term (term);
          if (folded != term) terms_out.add_term (folded);
        }
      });
      var reader = Dee.ModelReader.new ((model, iter) =>
      {
        var sender_col = StreamModelColumn.SENDER;
        var msg_col = StreamModelColumn.MESSAGE;
        return "%s\n%s".printf (model.get_string (iter, sender_col), 
                                model.get_string (iter, msg_col));
      });
      _index = new Dee.TreeIndex (_model, _analyzer, reader);
    }

    private void populate_filters ()
    {
      var filters = new GLib.List<Unity.Filter> ();

      /* Stream filter */
      {
        var filter = new CheckOptionFilter ("stream", _("Stream"));

        filter.add_option ("messages", _("Messages"));
        filter.add_option ("replies", _("Replies"));
        filter.add_option ("images", _("Images"));
        filter.add_option ("videos", _("Videos"));
        filter.add_option ("links", _("Links"));
        filter.add_option ("private", _("Private"));
        filter.add_option ("public", _("Public"));

        filters.append (filter);
      }

      /* Account filter */
      {
        var filter = create_account_filter ();
        filters.append (filter);
      }

      lens.filters = filters;

      accounts.created.connect((source) => {
        /* FIXME: we need a way to add an option 
        var filter = scope.get_filter ("account_id") as CheckOptionFilter;
        filter.add_option (source.id, source.service + "/" + source.username);
        */
      });

      /* FIXME: we need a way to remove an option or remove and re-add the
       * filter on account deletion */
    }

    private Unity.CheckOptionFilter create_account_filter ()
    {
      GLib.Icon icon;
      GLib.File icon_file;
      var filter = new CheckOptionFilter ("account_id", _("Account"));
      foreach (var _acct in accounts.list ())
      {
        /* create a service icon, which isn't used yet as of libunity 4.24
           when it is used, we can drop _acct.service from the option name 
           and rely on the service icon and username to disambiguate accounts */
        icon_file = GLib.File.new_for_path (GLib.Path.build_filename (Config.PKGDATADIR + "/plugins/" + _acct.service + "/ui/icons/16x16/" + _acct.service + ".png"));
        icon = new GLib.FileIcon (icon_file);

        filter.add_option (_acct.id, _acct.service + "/" + _acct.username, icon);
      }
      return filter;
    }


    private void populate_categories ()
    {
      var categories = new GLib.List<Unity.Category> ();
      Icon icon;

      var icon_dir = File.new_for_path ("/usr/share/gwibber/ui/icons/hicolor/scalable/places/");

      icon = new FileIcon (icon_dir.get_child ("group-messages.svg"));
      var cat = new Unity.Category (_("Messages"), icon, Unity.CategoryRenderer.HORIZONTAL_TILE);
      categories.append (cat);

      icon = new FileIcon (icon_dir.get_child ("group-replies.svg"));
      cat =  new Unity.Category (_("Replies"), icon, Unity.CategoryRenderer.HORIZONTAL_TILE);
      categories.append (cat);

      icon = new FileIcon (icon_dir.get_child ("group-images.svg"));
      cat =  new Unity.Category (_("Images"), icon, Unity.CategoryRenderer.FLOW);
      categories.append (cat);

      icon = new FileIcon (icon_dir.get_child ("group-videos.svg"));
      cat =  new Unity.Category (_("Videos"), icon, Unity.CategoryRenderer.FLOW);
      categories.append (cat);

      icon = new FileIcon (icon_dir.get_child ("group-links.svg"));
      cat =  new Unity.Category (_("Links"), icon, Unity.CategoryRenderer.HORIZONTAL_TILE);
      categories.append (cat);

      icon = new FileIcon (icon_dir.get_child ("group-private.svg"));
      cat =  new Unity.Category (_("Private"), icon, Unity.CategoryRenderer.HORIZONTAL_TILE);
      categories.append (cat);

      icon = new FileIcon (icon_dir.get_child ("group-public.svg"));
      cat =  new Unity.Category (_("Public"), icon, Unity.CategoryRenderer.HORIZONTAL_TILE);
      categories.append (cat);

      lens.categories = categories;
    }


    private bool is_empty_search (LensSearch search)
    {
      return search.search_string.strip () == "";
    }

    private async void update_global_search (LensSearch search, Cancellable cancellable)
    {
      /**
       * only perform the request if the user has not disabled
       * online/commercial suggestions. That will hide the category as well.
       */
      if (preferences.remote_content_search != Unity.PreferencesManager.RemoteContent.ALL)
      {
        search.results_model.clear ();
        search.finished ();
        return;
      }

      var results_model = scope.global_results_model;

      // FIXME: no results for home screen of the dash?
      if (is_empty_search (search))
      {
        search.finished ();
        return;
      }

      update_results_model (results_model, search.search_string, null);

      search.finished ();
    }

    private async void update_scope_search  (LensSearch search, Cancellable cancellable)
    {
      /**
       * only perform the request if the user has not disabled
       * online/commercial suggestions. That will hide the category as well.
       */
      if (preferences.remote_content_search != Unity.PreferencesManager.RemoteContent.ALL)
      {
        search.results_model.clear ();
        search.finished ();
        return;
      }

      var results_model = search.results_model;

      update_results_model (results_model, search.search_string, null);
      debug ("%u results", results_model.get_n_rows ());
      search.finished ();
    }


    /* Generic method to update a results model. We do it like this to minimize
     * code dup between updating the global- and the entry results model */
    private void update_results_model (Dee.Model results_model,
                                       string? search, Categories? category)
    {
      unowned Dee.ModelIter iter, end;

      var stream_ids = new Gee.ArrayList<string> ();
      var filter = scope.get_filter("stream") as CheckOptionFilter;
      if (filter.filtering)
      {
        foreach (Unity.FilterOption option in filter.options)
        {
          if (option.active)
          {
            stream_ids.add (option.id);
          }
        }
      }

      var account_ids = new Gee.ArrayList<string> ();
      filter = scope.get_filter("account_id") as CheckOptionFilter;
      if (filter.filtering)
      {
        foreach (Unity.FilterOption option in filter.options)
        {
          if (option.active)
          {
            account_ids.add (option.id);
          }
        }
      }

      results_model.clear ();

      if (_model == null)
        setup_gwibber ();

      iter = _model.get_first_iter ();
      end = _model.get_last_iter ();

      _stream_iter_first = _model.get_first_iter ();
      _stream_iter_last = end;
     
      var term_list = Object.new (typeof (Dee.TermList)) as Dee.TermList;
      // search only the folded terms, FIXME: is that a good idea?
      _analyzer.tokenize (_ascii_filter.apply (search), term_list);

      var matches = new Sequence<Dee.ModelIter> ();
      for (uint i = 0; i < term_list.num_terms (); i++)
      {
        // FIXME: use PREFIX search only for the last term?
        var result_set = _index.lookup (term_list.get_term (i),
                                        Dee.TermMatchFlag.PREFIX);
        bool first_pass = i == 0;
        CompareDataFunc<Dee.ModelIter> cmp_func = (a, b) =>
        {
          return a == b ? 0 : ((void*) a > (void*) b ? 1 : -1);
        };
        // intersect the results (cause we want to AND the terms)
        var remaining = new Sequence<Dee.ModelIter> ();
        foreach (var item in result_set)
        {
          if (first_pass)
            matches.insert_sorted (item, cmp_func);
          else if (matches.lookup (item, cmp_func) != null)
            remaining.insert_sorted (item, cmp_func);
        }
        if (!first_pass) matches = (owned) remaining;
        // final result set empty already?
        if (matches.get_begin_iter () == matches.get_end_iter ()) break;
      }

      matches.sort ((a, b) =>
      {
        var col = StreamModelColumn.TIMESTAMP;
        return _model.get_string (b, col).collate (_model.get_string (a, col));
      });

      var match_iter = matches.get_begin_iter ();
      var match_end_iter = matches.get_end_iter ();
      while (match_iter != match_end_iter)
      {
        iter = match_iter.get ();

        if (matches_filters (_model, iter, stream_ids, account_ids))
        {
          add_result (_model, iter, results_model);
        }

        match_iter = match_iter.next ();
      }

      if (term_list.num_terms () > 0) return;

      /* Go over the whole model if we had empty search */
      while (iter != end)
      {
        if (matches_filters (_model, iter, stream_ids, account_ids))
        {
          add_result (_model, iter, results_model);
        }
        iter = _model.next (iter);
      }

      //debug ("Results has %u rows", results_model.get_n_rows());
    }

    private bool matches_filters (Dee.Model model, Dee.ModelIter iter,
                                  Gee.List<string> stream_ids,
                                  Gee.List<string> account_ids)
    {
      bool stream_match = true;
      bool account_match = true;
      if (stream_ids.size > 0)
      {
        stream_match = model.get_string (iter, StreamModelColumn.STREAM) in stream_ids;
      }
      if (account_ids.size > 0)
      {
        string[] _accounts_array = (string[])model.get_value (iter, StreamModelColumn.ACCOUNTS);
        foreach (var a in _accounts_array)
        {
          string _account = a.split(":")[0];
          if (!(_account in account_ids))
            account_match = false;
        }
      }
      return account_match && stream_match;
    }

    private void add_result (Dee.Model model, Dee.ModelIter iter, Dee.Model results_model)
    {
      Categories group = Categories.MESSAGES;
      string _img_uri = null;

      unowned string stream_id = 
        model.get_string (iter, StreamModelColumn.STREAM);
      switch (stream_id)
      {
        case "messages": group = Categories.MESSAGES; break;
        case "replies": group = Categories.REPLIES; break;
        case "images": group = Categories.IMAGES; break;
        case "videos": group = Categories.VIDEOS; break;
        case "links": group = Categories.LINKS; break;
        case "private": group = Categories.PRIVATE; break;
        case "public": group = Categories.PUBLIC; break;
      }
      
      string _icon_uri = model.get_string (iter, StreamModelColumn.ICON_URI);
      if (stream_id == "images")
        _img_uri = model.get_string (iter, StreamModelColumn.IMG_SRC);
      else if (stream_id == "videos")
      {
        _img_uri = model.get_string (iter, StreamModelColumn.VIDEO_PIC);
        if (_img_uri.length < 1)
          _img_uri = get_avatar_path (_icon_uri);
      }
      else
        _img_uri = get_avatar_path (_icon_uri);

      results_model.append (model.get_string(iter, StreamModelColumn.URL),
                            _img_uri,
                            group,
                            "text/html",
                            _model.get_string(iter, StreamModelColumn.SENDER),
                            _model.get_string(iter, StreamModelColumn.MESSAGE));
    }

    private string get_avatar_path (string uri)
    {
      var _avatar_cache_image = utils.avatar_path (uri);
      if (_avatar_cache_image == null)
      {
        try
        {
          _avatar_cache_image = service.avatar_path (uri);
        } catch (GLib.Error e)
        {
        }
        if (_avatar_cache_image == null)
          _avatar_cache_image = uri;
      }

      return _avatar_cache_image;
    }

    public Unity.Preview preview (string uri)
    {
      debug ("Previewing: %s",  uri);
      Unity.SocialPreview preview = null;
      var icon_dir = File.new_for_path (ICON_PATH);
      Icon icon;
      string retweet_str, like_str, _img_uri = null;

      if (_streams_model == null)
        setup_gwibber ();

      var model = _streams_model;
      unowned Dee.ModelIter iter, end;
      iter = model.get_first_iter ();
      end = model.get_last_iter ();

      while (iter != end)
      {
        var url = model.get_string (iter, StreamModelColumn.URL);
        if (url == uri)
        {
          debug ("Found %s", url);
          var icon_uri = model.get_string (iter, StreamModelColumn.ICON_URI);
          if ("_normal." in icon_uri)
          {
            //If it looks like a twitter avatar, try to get the original
            var ss = icon_uri.split("_normal.");
            icon_uri = ss[0] + "." + ss[1];
          }
          var avatar = Icon.new_for_string (icon_uri);

          //var avatar = new FileIcon (File.new_for_path (get_avatar_path (icon_uri)));
          var likes = model.get_double (iter, StreamModelColumn.LIKES);
          var sender = model.get_string (iter, StreamModelColumn.SENDER);
          var sender_nick = model.get_string (iter, StreamModelColumn.SENDER_NICK);
          var timestring = model.get_string (iter, StreamModelColumn.TIMESTRING);
          if (sender_nick.length > 0)
            sender_nick = "@" + sender_nick;
          else
            sender_nick = sender;
          var content = model.get_string (iter, StreamModelColumn.MESSAGE);
          string title = sender_nick + " " + timestring;
          var comments_json = model.get_string (iter, StreamModelColumn.COMMENTS);

          preview = new Unity.SocialPreview (sender, title, content, avatar);
          // work around bug 1058198
          preview.title = sender;
          preview.subtitle = title;
          // end work around
          preview.add_info (new InfoHint ("likes", _("Favorites"), null, likes.to_string()));
          var stream_id = model.get_string (iter, StreamModelColumn.STREAM);
          if (stream_id == "images")
            _img_uri = model.get_string (iter, StreamModelColumn.IMG_SRC);
          else if (stream_id == "videos" && _img_uri != null && _img_uri.length < 1)
            _img_uri = model.get_string (iter, StreamModelColumn.VIDEO_PIC);
          if (_img_uri != null && _img_uri.length > 0)
          {
            Icon img = Icon.new_for_string (_img_uri);
            preview.image = img;
          }
          parse_comments (preview, comments_json);
          break;
        }
        iter = model.next (iter);
      }

      string[] _accounts_array = (string[])model.get_value (iter, StreamModelColumn.ACCOUNTS);
      string[] _seen = null;
      foreach (var a in _accounts_array)
      {
        var _account_id = a.split(":")[0];
        if (_account_id in _seen)
          continue;
        _seen += _account_id;
        var _account_service = a.split(":")[1];
        var _status_id = a.split(":")[2];
        var icon_str = icon_dir.get_child ("service-" + _account_service + ".svg");
        if (icon_str.query_exists())
          icon = new FileIcon (icon_str);
        else
          icon = null;

        var view_action = new Unity.PreviewAction ("view", _("View"), icon);
        preview.add_action (view_action);

        view_action.activated.connect ((source) => {
          try
          {
            if (GLib.AppInfo.launch_default_for_uri (uri, null))
            {
              return new Unity.ActivationResponse(Unity.HandledType.HIDE_DASH);
            }
          }
          catch (GLib.Error e)
          {
            warning ("Failed to launch default application for uri '%s': %s", uri, e.message);
          }
          return new Unity.ActivationResponse(Unity.HandledType.NOT_HANDLED);
        });

        if (_account_service == "twitter" || _account_service == "identica" || 
            _account_service == "statusnet" || _account_service == "sina" ||
            _account_service == "sohu") 
        {
          if (_account_service == "twitter")
            retweet_str = _("Retweet");
          else 
            retweet_str = _("Share");
          var retweet_action = new Unity.PreviewAction ("retweet", retweet_str, icon);
          preview.add_action (retweet_action);
          retweet_action.activated.connect ((source) => {
            service.retweet(_status_id, _account_id);
            return new Unity.ActivationResponse(Unity.HandledType.SHOW_PREVIEW);
          });
        }
        
        bool from_me = model.get_bool (iter, StreamModelColumn.FROM_ME);
        /* REPLY
        if ((_account_service != "flicker" && _account_service != "foursquare" && _account_service != "digg") && !from_me)
        {
          var reply_action = new Unity.PreviewAction ("reply", _("Reply"), icon);
          preview.add_action (reply_action);
          reply_action.activated.connect ((source) => {
            return new Unity.ActivationResponse(Unity.HandledType.HIDE_DASH);
          });
        }
        */

        bool liked = model.get_bool (iter, StreamModelColumn.LIKED);
        if ((_account_service != "flicker" && _account_service != "foursquare" && _account_service != "digg") && !from_me)
        {
          like_str = liked ? _("Unlike") : _("Like");
          var like_action = new Unity.PreviewAction ("like", like_str, icon);
          preview.add_action (like_action);
          like_action.activated.connect ((source) => {
            var ret = liked ? service.unlike (_status_id, _account_id) : service.like (_status_id, _account_id);
            if (ret)
            {
              model.set_value (iter, StreamModelColumn.LIKED, !liked);
            }
            Unity.Preview new_preview = this.preview (uri);
            return new Unity.ActivationResponse.with_preview(new_preview);
          });

        }
      }

      return preview;
    }

    private void parse_comments (Unity.SocialPreview preview, string _comments)
    {
      if (_comments != null)
      {
        string cname = "";
        string ctext = "";
        string time_string = "";

        var parser = new Json.Parser();
        try
        {
          parser.load_from_data(_comments, -1);
        }
        catch (Error e)
        {
        }
        unowned Json.Node comments_node = null;
        comments_node = parser.get_root();
        if (comments_node != null)
        {
          Json.Object comments_obj = comments_node.get_object ();
          if (comments_obj != null)
          {
            if (comments_obj.has_member ("comments"))
            {
              Json.Array comments = comments_obj.get_array_member ("comments");
              if (comments.get_length() > 0)
              {
                for(int i = 0; i < comments.get_length(); i++)
                {
                  var obj = comments.get_element(i).get_object();
                  if (obj != null)
                  {
                    if (obj.has_member ("text"))
                    {
                      ctext = obj.get_string_member ("text");
                      if (ctext != null)
                        ctext = GLib.Markup.escape_text (ctext);
                    }
                    if (obj.has_member ("time"))
                    {
                      var ctime = obj.get_int_member ("time");
                      time_string = utils.generate_time_string ((uint)ctime);
                      debug ("ctime: %s", time_string);
                    }
                    if (obj.has_member ("sender"))
                    {
                      var _sender_obj = obj.get_object_member ("sender");
                      if (_sender_obj != null)
                      {
                        if (_sender_obj.has_member ("name"))
                        {
                          cname = _sender_obj.get_string_member ("name");
                          if (cname != null)
                            cname = GLib.Markup.escape_text (cname);
                        }
                      }
                    }
                    preview.add_comment (new Unity.SocialPreview.Comment (cname, cname, ctext, time_string));
                  }
                }
              }  
            }  
          }
        }
      }
    }

  } /* End Daemon class */
} /* end Gwibber namespace */