~jamesh/unity-lens-friends/fix-icon-path

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
/*
 * 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 Friends;

namespace UnityFriends {

    /* DBus name for the place. Must match out .place file */
    const string BUS_NAME = "com.canonical.Unity.Scope.Friends";
    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.DeprecatedScope scope;
        private Unity.PreferencesManager preferences = Unity.PreferencesManager.get_default ();
        private unowned Dee.ResourceManager resources;
        private Dee.Model? _model = null;
        private Dee.SharedModel? _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;
        private HashMap<string,Variant> featureMap;
        private Variant empty_asv;

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

            featureMap = new HashMap<string,Variant> ();

            // Check for accounts
            _account_manager = new Ag.Manager.for_service_type ("microblogging");
            map_account_features ();

            _account_manager.enabled_event.connect ((id) =>
            {
                GLib.List<Ag.AccountService> accts = _account_manager.get_enabled_account_services ();
                if (accts.length () > 0 && !_has_accounts)
                {
                    _has_accounts = true;
                    setup ();
                }
                else if (accts.length () == 0)
                {
                    scope.visible = false;
                    _has_accounts = false;
                }
                else
                {
                    map_account_features ();
                }
            });
        }


        void setup ()
        {
            setup_friends ();

            scope.visible = true;
            scope.preview_uri.connect (preview);

            /* Listen for filter changes */
            scope.generate_search_key.connect ((lens_search) =>
            {
                return lens_search.search_string.strip ();
            });
            scope.search_changed.connect ((search, search_type, cancellable) =>
            {
                dispatch_search.begin (search, search_type, cancellable);
            });

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

        private void map_account_features ()
        {
            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);
                var _provider = account.get_provider_name ();
                if (!(featureMap.has_key(_provider)))
                {
                    var d = new Friends.Dispatcher ();
                    var builder = new VariantBuilder (VariantType.STRING_ARRAY);
                    foreach (var _feature in d.features (_provider))
                    {
                        builder.add ("s", _feature);
                    }
                    featureMap[_provider] = builder.end ();
                }
            }

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

        private void setup_friends ()
        {
            populate_categories ();
            populate_filters ();

            Intl.setlocale(LocaleCategory.COLLATE, "C");

            _streams_model = new Dee.SharedModel ("com.canonical.Friends.Streams");

            if (_streams_model is Dee.SharedModel)
            {
                    _streams_model.notify["synchronized"].connect (on_synchronized);
                    _streams_model.row_added.connect ((source) => {
                        scope.queue_search_changed (SearchType.DEFAULT);
                    });
                    _streams_model.row_removed.connect ((source) => {
                        scope.queue_search_changed (SearchType.DEFAULT);
                    });
                    _streams_model.row_changed.connect ((source) => {
                        scope.queue_search_changed (SearchType.DEFAULT);
                    });
            }
        }

        private void on_synchronized ()
        {
            debug ("on_synchronized");

            if (!_streams_model.is_synchronized ())
                return;

            debug ("%u ROWS", _streams_model.get_n_rows ());
            _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 Unity.FilterSet ();

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

                filter.add_option ("messages", _("Messages"));
                filter.add_option ("mentions", _("Mentions"));
                filter.add_option ("private", _("Private"));
                /* FIXME: Hide the filters for now, the streams don't exist
                filter.add_option ("images", _("Images"));
                filter.add_option ("videos", _("Videos"));
                filter.add_option ("links", _("Links"));
                filter.add_option ("public", _("Public"));
                */

                filters.add (filter);
            }

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

            scope.filters = filters;

            _account_manager.account_created.connect ((id) => {
                debug ("ACCOUNT ADDED");
                var _acct = _account_manager.get_account ((Ag.AccountId)id);
                var filter = scope.filters.get_filter_by_id ("account_id") as CheckOptionFilter;
                create_account_filter_option (filter, _acct);
                map_account_features ();
            });

            /* 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 ()
        {
            var filter = new CheckOptionFilter ("account_id", _("Account"));
            var accts = _account_manager.get_enabled_account_services ();
            if (accts.length () > 0)
            {
                foreach (var _acctservice in accts)
                {
                    // 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 
                    var _acct = _acctservice.get_account ();
                    create_account_filter_option (filter, _acct);
                }
            }
            return filter;
        }

        private void create_account_filter_option (CheckOptionFilter filter, Ag.Account _acct)
        {
            GLib.Icon icon;
            GLib.File icon_file;
            var _provider = _acct.get_provider_name ();
            var _name = _acct.get_display_name ();
            icon_file = GLib.File.new_for_path (GLib.Path.build_filename (Config.PKGDATADIR + "/plugins/" + _provider + "/ui/icons/16x16/" + _provider + ".png"));
            icon = new GLib.FileIcon (icon_file);

            filter.add_option (_acct.id.to_string(), _provider + "/" + _name, icon);
        }


        private void populate_categories ()
        {
            var categories = new Unity.CategorySet ();
            Icon icon;

            var icon_dir = File.new_for_path ("/usr/share/unity-lens-friends/ui/icons/hicolor/scalable/places/");

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

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

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

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

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

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

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

            scope.categories = categories;
        }


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

        private async void dispatch_search (DeprecatedScopeSearch search, SearchType search_type, GLib.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;
            }

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

            update_results_model (search, null);
            debug ("%u results", search.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 (DeprecatedScopeSearch search,
                                           Categories? category)
        {
            debug ("%u TOTAL ROWS", _model.get_n_rows ());
            unowned Dee.ModelIter iter, end;

            var results_model = search.results_model;
            var stream_ids = new Gee.ArrayList<string> ();
            var filter = search.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 = search.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_friends ();

            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.search_string), 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)
            {
                var _account = (uint)model.get_uint64 (iter, StreamModelColumn.ACCOUNT_ID);
                if (!(_account.to_string() in account_ids))
                    account_match = false;
            }

            var _stream = model.get_string (iter, StreamModelColumn.STREAM);
            if ("reply_to/" in _stream)
                stream_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 "mentions": 
                    if (model.get_bool (iter, StreamModelColumn.FROM_ME))
                        group = Categories.MESSAGES;
                    else
                        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 uri = model.get_string (iter, StreamModelColumn.URL);
            string _icon_uri = model.get_string (iter, StreamModelColumn.ICON_URI);
            if (stream_id == "images")
                _img_uri = model.get_string (iter, StreamModelColumn.LINK_PICTURE);
            else if (stream_id == "videos")
            {
                _img_uri = model.get_string (iter, StreamModelColumn.LINK_PICTURE);
                if (_img_uri.length < 1)
                    _img_uri = get_avatar_path (_icon_uri);
            }
            else
                _img_uri = get_avatar_path (_icon_uri);

            results_model.append (uri,
                                  _img_uri,
                                  group,
                                  ResultType.PERSONAL,
                                  "text/html",
                                  GLib.Markup.escape_text (_model.get_string(iter, StreamModelColumn.SENDER)),
                                  _model.get_string(iter, StreamModelColumn.MESSAGE),
                                  uri,
                                  empty_asv);
        }

        private string get_avatar_path (string uri)
        {
            var _avatar_cache_image = uri;
            /* FIXME
            var _avatar_cache_image = avatar_path (uri);
            if (_avatar_cache_image == null)
            {
                try
                {
                    _avatar_cache_image = 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 utils = new Friends.Utils ();
            var icon_dir = File.new_for_path (ICON_PATH);
            Icon icon;
            string retweet_str, like_str, _img_uri = null;

            if (_streams_model == null)
                setup_friends ();

            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_uint64 (iter, StreamModelColumn.LIKES);
                    var sender = model.get_string (iter, StreamModelColumn.SENDER);
                    var timestring = utils.create_time_string (model.get_string (iter, StreamModelColumn.TIMESTAMP));
                    var content = model.get_string (iter, StreamModelColumn.MESSAGE);
                    string title = timestring;
                    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.LINK_PICTURE);
                    else if (stream_id == "videos" && _img_uri != null && _img_uri.length < 1)
                        _img_uri = model.get_string (iter, StreamModelColumn.LINK_PICTURE);
                    if (_img_uri != null && _img_uri.length > 0)
                    {
                        Icon img = Icon.new_for_string (_img_uri);
                        preview.image = img;
                    }
                    break;
                }
                iter = model.next (iter);
            }

            uint _account_id = (uint)model.get_uint64(iter, StreamModelColumn.ACCOUNT_ID);
            debug ("account_id: %u", _account_id);
            var _account_service = model.get_string (iter, StreamModelColumn.PROTOCOL);
            var _status_id = model.get_string (iter, StreamModelColumn.MESSAGE_ID);
            var icon_str = icon_dir.get_child ("service-" + _account_service + ".svg");
            if (icon_str.query_exists ())
                icon = new FileIcon (icon_str);
            else
                icon = null;

            string[] _features = null;
            if (featureMap.has_key(_account_service))
            {
                _features = featureMap[_account_service].dup_strv ();
            }

            parse_comments (preview, _status_id);

            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 ("retweet" in _features)
            {
                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) => {
                    Idle.add (() => {
                        var dispatcher = new Friends.Dispatcher ();
                        dispatcher.retweet (_account_id, _status_id);
                        return false;
                    });
                    return new Unity.ActivationResponse (Unity.HandledType.SHOW_PREVIEW);
                });
            }
            
            bool from_me = model.get_bool (iter, StreamModelColumn.FROM_ME);
            // REPLY
            //if ("reply" in _features && !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 ("like" in _features && "unlike" in _features && !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) => {
                    Idle.add (() => {
                        var dispatcher = new Friends.Dispatcher ();
                        var ret = liked ? dispatcher.unlike (_account_id, _status_id) : dispatcher.like (_account_id, _status_id);
                        return false;
                    });
                    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 _message_id)
        {
            if (_message_id != null)
            {
                var utils = new Friends.Utils ();
                unowned Dee.ModelIter iter, end;
                iter = _model.get_first_iter ();
                end = _model.get_last_iter ();

                while (iter != end)
                {
                    var _stream = _model.get_string (iter, StreamModelColumn.STREAM).split ("/")[1];
                    if (_stream == _message_id)
                    {
                        debug ("stream: %s", _stream);
                        var text = _model.get_string (iter, StreamModelColumn.MESSAGE);
                        var name = GLib.Markup.escape_text (_model.get_string (iter, StreamModelColumn.SENDER));
                        var time_string = utils.create_time_string (_model.get_string (iter, StreamModelColumn.TIMESTAMP));
                        preview.add_comment (new Unity.SocialPreview.Comment (name, name, text, time_string));
                    }
                    iter = _model.next (iter);
                }
            }
        }

        private string? avatar_path(string url)
        {
            string _avatar_cache_image = Path.build_path (Path.DIR_SEPARATOR_S, Environment.get_user_cache_dir(), "friends/avatars", GLib.Checksum.compute_for_string (GLib.ChecksumType.SHA1, url));
            var file = File.new_for_path (_avatar_cache_image);
            if (file.query_exists ())
                return _avatar_cache_image;
            else
                return null;
        }


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