~timo-jyrinki/unity-scope-mediascanner/manual_import_rtm_0.2+14.10.20141009-0ubuntu1

« back to all changes in this revision

Viewing changes to src/utils/resultforwarder.cpp

  • Committer: Timo Jyrinki
  • Date: 2014-10-29 14:28:38 UTC
  • Revision ID: timo.jyrinki@canonical.com-20141029142838-uveiptpvvs5yz0pe
Manual import of 0.2+14.10.20141009-0ubuntu1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2014 Canonical Ltd
3
 
 *
4
 
 * This program is free software: you can redistribute it and/or modify
5
 
 * it under the terms of the GNU General Public License version 3 as
6
 
 * published by the Free Software Foundation.
7
 
 *
8
 
 * This program is distributed in the hope that it will be useful,
9
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
 * GNU General Public License for more details.
12
 
 *
13
 
 * You should have received a copy of the GNU General Public License
14
 
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
 
 *
16
 
 * Authored by Jussi Pakkanen <jussi.pakkanen@canonical.com>
17
 
 *             Pawel Stolowski <pawel.stolowski@canonical.com>
18
 
 *
19
 
 */
20
 
 
21
 
#include "resultforwarder.h"
22
 
#include <unity/scopes/SearchReply.h>
23
 
 
24
 
using namespace unity::scopes;
25
 
 
26
 
void ResultForwarder::push(Category::SCPtr const& category) {
27
 
    upstream->register_category(category);
28
 
}
29
 
 
30
 
void ResultForwarder::push(CategorisedResult result) {
31
 
    {
32
 
        if (result_filter(result))
33
 
        {
34
 
            upstream->push(result);
35
 
        }
36
 
    }
37
 
    if (!ready_)
38
 
    {
39
 
        ready_ = notify_strategy_->is_ready(result);
40
 
        if (ready_)
41
 
        {
42
 
            notify_observers();
43
 
        }
44
 
    }
45
 
}
46
 
 
47
 
void ResultForwarder::finished(unity::scopes::CompletionDetails const& /*details*/) {
48
 
    if (!ready_)
49
 
    {
50
 
        ready_ = true;
51
 
        notify_observers();
52
 
    }
53
 
}
54
 
 
55
 
void ResultForwarder::notify_observers()
56
 
{
57
 
    for (auto o: observers_)
58
 
    {
59
 
        o->on_forwarder_ready(this);
60
 
    }
61
 
    observers_.clear();
62
 
}
63
 
 
64
 
void ResultForwarder::add_observer(std::shared_ptr<ResultForwarder> result_forwarder)
65
 
{
66
 
    if (result_forwarder.get() != this)
67
 
    {
68
 
        observers_.push_back(result_forwarder);
69
 
        result_forwarder->wait_for_.push_back(this);
70
 
    }
71
 
}
72
 
 
73
 
void ResultForwarder::on_forwarder_ready(ResultForwarder *fw)
74
 
{
75
 
    //
76
 
    // remove the forwarder that notified us from the wait_for_ list;
77
 
    wait_for_.remove_if([fw](ResultForwarder* r) -> bool { return r == fw; });
78
 
    if (wait_for_.size() == 0)
79
 
    {
80
 
        on_all_forwarders_ready();
81
 
    }
82
 
}
83
 
 
84
 
void ResultForwarder::on_all_forwarders_ready()
85
 
{
86
 
    // base impl does nothing
87
 
}