~xavi-garcia-mena/unity-scopes-api/utils-qt

« back to all changes in this revision

Viewing changes to include/unity/scopes/qt/QResult.h

  • Committer: Xavi Garcia
  • Date: 2015-01-20 13:59:36 UTC
  • Revision ID: xavi.garcia.mena@canonical.com-20150120135936-cogxk8ok7g0gvo8g
Added Qt-bindings classes

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2015 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: Xavi Garcia <xavi.garcia.mena@canonical.com>
 
17
 */
 
18
 
 
19
#pragma once
 
20
 
 
21
#include <unity/util/DefinesPtrs.h>
 
22
 
 
23
#include <unity/scopes/ScopeProxyFwd.h>
 
24
#include <unity/scopes/Variant.h>
 
25
 
 
26
#include <unity/scopes/qt/QScopeVariant.h>
 
27
 
 
28
#include <QtCore/QString>
 
29
#include <QtCore/QVariant>
 
30
 
 
31
namespace unity
 
32
{
 
33
 
 
34
namespace scopes
 
35
{
 
36
 
 
37
class Variant;
 
38
class Result;
 
39
 
 
40
namespace qt
 
41
{
 
42
 
 
43
class QCategorisedResult;
 
44
 
 
45
namespace internal
 
46
{
 
47
class QResultImpl;
 
48
class QPreviewQueryBaseImpl;
 
49
}
 
50
 
 
51
/**
 
52
   \brief The attributes of a result returned by a Scope.
 
53
 
 
54
   The Result API provides convenience methods for some typical attributes (title,
 
55
   art), but scopes are free to add and use any custom attributes with `operator[]`.
 
56
   The only required attribute is 'uri' and it must not be empty before
 
57
   calling Reply::push().
 
58
*/
 
59
class QResult
 
60
{
 
61
public:
 
62
    /// @cond
 
63
    UNITY_DEFINES_PTRS(QResult);
 
64
    /// @endcond
 
65
 
 
66
    QResult();
 
67
    /**@name Copy and assignment
 
68
    Copy and assignment (move and non-move versions) have the usual value semantics.
 
69
    */
 
70
    //{@
 
71
    QResult(QResult const& other);
 
72
    QResult(QResult&&);
 
73
 
 
74
    QResult& operator=(QResult const& other);
 
75
    QResult& operator=(QResult&&);
 
76
    //@}
 
77
 
 
78
    /**
 
79
    Destructor.
 
80
    */
 
81
    virtual ~QResult();
 
82
 
 
83
    /**
 
84
    \brief Stores a Result inside this Result instance.
 
85
 
 
86
    This method is meant to be used by aggregator scopes which want to modify results they receive, but want
 
87
    to keep a copy of the original result so that they can be correctly handled by the original scopes
 
88
    who created them when it comes to activation or previews.
 
89
    Scopes middleware will automatically pass the correct inner stored result to the activation or preview request
 
90
    handler
 
91
    of a scope which created it.
 
92
    \param other The original result to store within this result.
 
93
    \param intercept_activation True if this scope should receive activation and preview requests.
 
94
    */
 
95
    void store(QResult const& other, bool intercept_activation = false);
 
96
 
 
97
    /**
 
98
    \brief Check if this Result instance has a stored result.
 
99
    \return True if there is a stored result
 
100
     */
 
101
    bool has_stored_result() const;
 
102
 
 
103
    /**
 
104
    \brief Get a stored result.
 
105
    \return stored result
 
106
    \throws unity::InvalidArgumentException if no result was stored in this Result instance.
 
107
    */
 
108
    QResult retrieve() const;
 
109
 
 
110
    /**
 
111
    \brief Set the "uri" attribute of this result.
 
112
    */
 
113
    void set_uri(QString const& uri);
 
114
 
 
115
    /**
 
116
    \brief Set the "title" attribute of this result.
 
117
 
 
118
    Equivalent to calling `result["title"] = title;`
 
119
    */
 
120
    void set_title(QString const& title);
 
121
    /**
 
122
    \brief Set the "art" attribute of this result.
 
123
 
 
124
    Equivalent to calling `result["art"] = image;`
 
125
    */
 
126
    void set_art(QString const& image);
 
127
 
 
128
    /**
 
129
    \brief Set the "dnd_uri" attribute of this result.
 
130
 
 
131
    Equivalent to calling `result["dnd_uri"] = dnd_uri;`
 
132
    */
 
133
    void set_dnd_uri(QString const& dnd_uri);
 
134
 
 
135
    /**
 
136
    \brief Indicates to the receiver that this scope should intercept activation requests for this result.
 
137
 
 
138
    By default, a scope receives preview requests for the results it creates, but does not receive activation
 
139
    requests (they are handled directly by the shell).
 
140
    Intercepting activation implies intercepting preview requests as well; this is important for scopes that
 
141
    forward results from other scopes and call set_intercept_activation() on these scopes.
 
142
    A scope that sets intercept activation flag for a result should re-implement ScopeBase::activate()
 
143
    and provide an implementation of ActivationQueryBase that handles the actual activation.
 
144
    If not called, the result will be activated directly by the Unity shell whithout involving the scope,
 
145
    assuming an appropriate URI schema handler is present on the system.
 
146
    */
 
147
    void set_intercept_activation();
 
148
 
 
149
    /**
 
150
    \brief Check if this result should be activated directly by the shell
 
151
           because the scope doesn't handle activation of this result.
 
152
    \return True if this result needs to be activated directly.
 
153
    */
 
154
    bool direct_activation() const;
 
155
 
 
156
    /**
 
157
    \brief Get the proxy of a scope that handles activation and preview of this result.
 
158
 
 
159
    The proxy is available only when receiving this result from a scope, otherwise
 
160
    this method throws LogicException. Activation requests should be sent to a scope
 
161
    returned by this method only if direct_activation() is false.
 
162
    \return The scope proxy.
 
163
    */
 
164
    ScopeProxy target_scope_proxy() const;
 
165
 
 
166
    /**
 
167
    \brief Returns reference of a Result attribute.
 
168
 
 
169
    This method can be used to read or initialize both standard ("uri", "title", "art", "dnd_uri")
 
170
    and custom metadata attributes. Referencing a non-existing attribute automatically creates
 
171
    it with a default value of Variant::Type::Null.
 
172
    \param key The name of the attribute.
 
173
    \return A reference to the attribute.
 
174
    \throws unity::Invalidargument if no attribute with the given name exists.
 
175
    */
 
176
    QScopeVariant& operator[](QString const& key);
 
177
 
 
178
    /**
 
179
    \brief Returns a const reference to a Result attribute.
 
180
 
 
181
    This method can be used for read-only access to both standard ("uri", "title", "art", "dnd_uri")
 
182
    and custom metadata attributes. Referencing a non-existing attribute throws unity::InvalidArgumentException.
 
183
    \param key The name of the attribute.
 
184
    \return A const reference to the attribute.
 
185
    \throws unity::Invalidargument if no attribute with the given name exists.
 
186
    */
 
187
    QVariant const& operator[](QString const& key) const;
 
188
 
 
189
    /**
 
190
    \brief Get the "uri" property of this Result.
 
191
 
 
192
    This method returns an empty string if this attribute is not of type Variant::Type::String (e.g. it was set with
 
193
    operator[]).
 
194
    \return The value of "uri" or the empty string.
 
195
     */
 
196
    QString uri() const noexcept;
 
197
 
 
198
    /**
 
199
    \brief Get the "title" property of this Result.
 
200
 
 
201
    This method returns an empty string if this attribute is not of type Variant::Type::String (e.g. it was set with
 
202
    operator[]).
 
203
    \return The value of "title" or the empty string.
 
204
    */
 
205
    QString title() const noexcept;
 
206
 
 
207
    /**
 
208
    \brief Get the "art" property of this Result.
 
209
 
 
210
    This method returns an empty string if this attribute is not of type Variant::Type::String (e.g. it was set with
 
211
    operator[]).
 
212
    \return The value of "art" or the empty string.
 
213
    */
 
214
    QString art() const noexcept;
 
215
 
 
216
    /**
 
217
    \brief Get the "dnd_uri" property of this Result.
 
218
 
 
219
    This method returns an empty string if this attribute is not of type Variant::Type::String type (e.g. it was set
 
220
    with operator[]).
 
221
    \return The value of "dnd_uri" or the empty string.
 
222
    */
 
223
    QString dnd_uri() const noexcept;
 
224
 
 
225
    /**
 
226
    \brief Check if this Result has an attribute.
 
227
    \param key The attribute name.
 
228
    \return True if the attribute is set.
 
229
    */
 
230
    bool contains(QString const& key) const;
 
231
 
 
232
    /**
 
233
    \brief Get the value of an attribute.
 
234
 
 
235
    \param key The attribute name.
 
236
    \return The attribute value.
 
237
    \throws unity::InvalidArgumentException if given attribute hasn't been set.
 
238
    */
 
239
    QVariant const& value(QString const& key) const;
 
240
 
 
241
    /**
 
242
    \brief Returns a dictionary of all attributes of this Result instance.
 
243
    \return All base attributes and custom attributes set with add_metadata().
 
244
    */
 
245
    VariantMap serialize() const;
 
246
 
 
247
protected:
 
248
    /// @cond
 
249
    explicit QResult(const VariantMap& variant_map);
 
250
    explicit QResult(internal::QResultImpl* impl);
 
251
    explicit QResult(unity::scopes::Result const& result);
 
252
 
 
253
private:
 
254
    std::unique_ptr<internal::QResultImpl> p;
 
255
 
 
256
    friend class internal::QResultImpl;
 
257
    friend class QCategorisedResult;
 
258
    friend class internal::QPreviewQueryBaseImpl;
 
259
    friend class QPreviewQueryBaseAPI;
 
260
    /// @endcond
 
261
};
 
262
 
 
263
}  // namespace qt
 
264
 
 
265
}  // namespace scopes
 
266
 
 
267
}  // namespace unity