~ubuntu-branches/ubuntu/trusty/unity-scopes-api/trusty-proposed

« back to all changes in this revision

Viewing changes to src/scopes/internal/DepartmentImpl.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Pawel Stolowski
  • Date: 2014-02-11 17:55:05 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20140211175505-av3z0612p7o8w54b
Tags: 0.3.1+14.04.20140211.2-0ubuntu1
[ Pawel Stolowski ]
* [ Pawel Stolowski ]
* Added preliminary API for filters and departments.
* Changes to preview action activation API: support for widget id.
* Changes to Annotation API.
* Return ScopeProxy from Result::target_scope_proxy (replaces activation_scope_name).

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 Lesser 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 Lesser General Public License for more details.
 
12
 *
 
13
 * You should have received a copy of the GNU Lesser General Public License
 
14
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
15
 *
 
16
 * Authored by: Pawel Stolowski <pawel.stolowski@canonical.com>
 
17
*/
 
18
 
 
19
#include <unity/scopes/internal/DepartmentImpl.h>
 
20
#include <unity/UnityExceptions.h>
 
21
#include <unity/scopes/internal/QueryImpl.h>
 
22
#include <sstream>
 
23
 
 
24
namespace unity
 
25
{
 
26
 
 
27
namespace scopes
 
28
{
 
29
 
 
30
namespace internal
 
31
{
 
32
DepartmentImpl::DepartmentImpl(Query const& query, std::string const& label)
 
33
    : query_(query),
 
34
      label_(label)
 
35
{
 
36
}
 
37
 
 
38
DepartmentImpl::DepartmentImpl(std::string const& department_id, Query const& query, std::string const& label)
 
39
    : query_(query),
 
40
      label_(label)
 
41
{
 
42
    query_.set_department_id(department_id);
 
43
}
 
44
 
 
45
DepartmentImpl::DepartmentImpl(std::string const& department_id, Query const& query, std::string const& label, DepartmentList const& subdepartments)
 
46
    : query_(query),
 
47
      label_(label),
 
48
      departments_(subdepartments)
 
49
{
 
50
    query_.set_department_id(department_id);
 
51
}
 
52
 
 
53
void DepartmentImpl::set_subdepartments(DepartmentList const& departments)
 
54
{
 
55
    departments_ = departments;
 
56
}
 
57
 
 
58
std::string DepartmentImpl::id() const
 
59
{
 
60
    return query_.department_id();
 
61
}
 
62
 
 
63
std::string DepartmentImpl::label() const
 
64
{
 
65
    return label_;
 
66
}
 
67
 
 
68
Query DepartmentImpl::query() const
 
69
{
 
70
    return query_;
 
71
}
 
72
 
 
73
DepartmentList DepartmentImpl::subdepartments() const
 
74
{
 
75
    return departments_;
 
76
}
 
77
 
 
78
VariantMap DepartmentImpl::serialize() const
 
79
{
 
80
    VariantMap vm;
 
81
    vm["label"] = label_;
 
82
    vm["query"] = query_.serialize();
 
83
 
 
84
    // sub-departments are optional
 
85
    if (departments_.size())
 
86
    {
 
87
        VariantArray subdeparr;
 
88
        for (auto const& dep: departments_)
 
89
        {
 
90
            subdeparr.push_back(Variant(dep.serialize()));
 
91
        }
 
92
 
 
93
        vm["departments"] = Variant(subdeparr);
 
94
    }
 
95
    return vm;
 
96
}
 
97
 
 
98
Department DepartmentImpl::create(VariantMap const& var)
 
99
{
 
100
    auto it = var.find("label");
 
101
    if (it == var.end())
 
102
    {
 
103
        throw unity::InvalidArgumentException("DepartmentImpl::create(): missing 'label'");
 
104
    }
 
105
    auto label = it->second.get_string();
 
106
    it = var.find("query");
 
107
    if (it == var.end())
 
108
    {
 
109
        throw unity::InvalidArgumentException("DepartmentImpl::create(): missing 'query'");
 
110
    }
 
111
    auto query = QueryImpl::create(it->second.get_dict());
 
112
 
 
113
    Department department(query, label);
 
114
 
 
115
    it = var.find("departments");
 
116
    if (it != var.end())
 
117
    {
 
118
        DepartmentList subdeps;
 
119
        for (auto const dep: it->second.get_array())
 
120
        {
 
121
            subdeps.push_back(create(dep.get_dict()));
 
122
        }
 
123
        department.set_subdepartments(subdeps);
 
124
    }
 
125
    return department;
 
126
}
 
127
 
 
128
void DepartmentImpl::validate_departments(DepartmentList const& departments, std::unordered_set<std::string>& lookup)
 
129
{
 
130
    for (auto const& dep: departments)
 
131
    {
 
132
        if (lookup.find(dep.id()) != lookup.end())
 
133
        {
 
134
            std::stringstream str;
 
135
            str << "DepartmentImpl::validate_departments(): Duplicate department: '" << dep.id() << "'";
 
136
            throw unity::LogicException(str.str());
 
137
        }
 
138
        lookup.insert(dep.id());
 
139
        validate_departments(dep.subdepartments(), lookup);
 
140
    }
 
141
}
 
142
 
 
143
void DepartmentImpl::validate_departments(DepartmentList const& departments, std::string const &current_department_id)
 
144
{
 
145
    if (departments.size() == 0)
 
146
    {
 
147
        throw unity::LogicException("DepartmentImpl::validate_departments(): empty departments list");
 
148
    }
 
149
 
 
150
    // don't allow duplicated department ids. ensure at current_department_id matches one of the departments (if non-empty).
 
151
    std::unordered_set<std::string> lookup;
 
152
    validate_departments(departments, lookup);
 
153
    if (!current_department_id.empty())
 
154
    {
 
155
        if (lookup.find(current_department_id) == lookup.end())
 
156
        {
 
157
            std::stringstream str;
 
158
            str << "DepartmentImpl::validate_departments(): current department '" << current_department_id << "' doesn't match any of the departments";
 
159
            throw unity::LogicException(str.str());
 
160
        }
 
161
    }
 
162
}
 
163
 
 
164
} // namespace internal
 
165
 
 
166
} // namespace scopes
 
167
 
 
168
} // namespace unity