File: SearchProvider_stub.cpp

package info (click to toggle)
pinot 1.23-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,608 kB
  • sloc: cpp: 41,870; makefile: 611; xml: 416; sh: 336
file content (313 lines) | stat: -rw-r--r-- 12,161 bytes parent folder | download | duplicates (4)
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
static const char interfaceXml0[] = R"XML_DELIMITER(<!DOCTYPE node PUBLIC
'-//freedesktop//DTD D-BUS Object Introspection 1.0//EN'
'http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd'>
<node>

  <!--
      org.gnome.Shell.SearchProvider2:
      @short_description: Search provider interface

      The interface used for integrating into GNOME Shell's search
      interface (version 2).
  -->
  <interface name="org.gnome.Shell.SearchProvider2">

    <!--
        GetInitialResultSet:
        @terms: Array of search terms, which the provider should treat as logical AND.
        @results: An array of result identifier strings representing items which match the given search terms. Identifiers must be unique within the provider's domain, but other than that may be chosen freely by the provider.

        Called when the user first begins a search.
    -->
    <method name="GetInitialResultSet">
      <arg type="as" name="terms" direction="in" />
      <arg type="as" name="results" direction="out" />
    </method>

    <!--
        GetSubsearchResultSet:
        @previous_results: Array of results previously returned by GetInitialResultSet().
        @terms: Array of updated search terms, which the provider should treat as logical AND.
        @results: An array of result identifier strings representing items which match the given search terms. Identifiers must be unique within the provider's domain, but other than that may be chosen freely by the provider.

        Called when a search is performed which is a "subsearch" of
        the previous search, e.g. the method may return less results, but
        not more or different results.

        This allows search providers to only search through the previous
        result set, rather than possibly performing a full re-query.
    -->
    <method name="GetSubsearchResultSet">
      <arg type="as" name="previous_results" direction="in" />
      <arg type="as" name="terms" direction="in" />
      <arg type="as" name="results" direction="out" />
    </method>

    <!--
        GetResultMetas:
        @identifiers: An array of result identifiers as returned by GetInitialResultSet() or GetSubsearchResultSet()
        @metas: A dictionary describing the given search result, containing a human-readable 'name' (string), along with the result identifier this meta is for, 'id' (string). Optionally, 'icon' (a serialized GIcon as obtained by g_icon_serialize) can be specified if the result can be better served with a thumbnail of the content (such as with images). 'gicon' (a serialized GIcon as obtained by g_icon_to_string) or 'icon-data' (raw image data as (iiibiiay) - width, height, rowstride, has-alpha, bits per sample, channels, data) are deprecated values that can also be used for that purpose. A 'description' field (string) may also be specified if more context would help the user find the desired result.

        Return an array of meta data used to display each given result
    -->
    <method name="GetResultMetas">
      <arg type="as" name="identifiers" direction="in" />
      <arg type="aa{sv}" name="metas" direction="out" />
    </method>

    <!--
        ActivateResult:
        @identifier: A result identifier as returned by GetInitialResultSet() or GetSubsearchResultSet()
        @terms: Array of search terms, which the provider should treat as logical AND.
        @timestamp: A timestamp of the user interaction that triggered this call

        Called when the users chooses a given result. The result should
        be displayed in the application associated with the corresponding
        provider. The provided search terms can be used to allow launching a full search in
        the application.
    -->
    <method name="ActivateResult">
      <arg type="s" name="identifier" direction="in" />
      <arg type="as" name="terms" direction="in" />
      <arg type="u" name="timestamp" direction="in" />
    </method>

    <!--
        LaunchSearch:
        @terms: Array of search terms, which the provider should treat as logical AND.
        @timestamp: A timestamp of the user interaction that triggered this call

        Asks the search provider to launch a full search in the application for the provided terms.
    -->
    <method name="LaunchSearch">
      <arg type="as" name="terms" direction="in" />
      <arg type="u" name="timestamp" direction="in" />
    </method>
  </interface>
</node>
)XML_DELIMITER";

#include "SearchProvider_stub.h"

template<class T>
inline T specialGetter(Glib::Variant<T> variant)
{
    return variant.get();
}

template<>
inline std::string specialGetter(Glib::Variant<std::string> variant)
{
    // String is not guaranteed to be null-terminated, so don't use ::get()
    gsize n_elem;
    gsize elem_size = sizeof(char);
    char* data = (char*)g_variant_get_fixed_array(variant.gobj(), &n_elem, elem_size);

    return std::string(data, n_elem);
}

org::gnome::Shell::SearchProvider2Stub::SearchProvider2Stub():
    m_interfaceName("org.gnome.Shell.SearchProvider2")
{
}

org::gnome::Shell::SearchProvider2Stub::~SearchProvider2Stub()
{
    unregister_object();
}

guint org::gnome::Shell::SearchProvider2Stub::register_object(
    const Glib::RefPtr<Gio::DBus::Connection> &connection,
    const Glib::ustring &object_path)
{
    if (!introspection_data) {
        try {
            introspection_data = Gio::DBus::NodeInfo::create_for_xml(interfaceXml0);
        } catch(const Glib::Error& ex) {
            g_warning("Unable to create introspection data for %s: %s", object_path.c_str(), ex.what().c_str());
            return 0;
        }
    }

    Gio::DBus::InterfaceVTable *interface_vtable =
        new Gio::DBus::InterfaceVTable(
            sigc::mem_fun(this, &SearchProvider2Stub::on_method_call),
            sigc::mem_fun(this, &SearchProvider2Stub::on_interface_get_property),
            sigc::mem_fun(this, &SearchProvider2Stub::on_interface_set_property));

    guint registration_id;
    try {
        registration_id = connection->register_object(object_path,
            introspection_data->lookup_interface("org.gnome.Shell.SearchProvider2"),
            *interface_vtable);
    } catch(const Glib::Error &ex) {
        g_warning("Registration of object %s failed: %s", object_path.c_str(), ex.what().c_str());
        return 0;
    }

    m_registered_objects.emplace_back(RegisteredObject {
        registration_id,
        connection,
        object_path
    });

    return registration_id;
}

void org::gnome::Shell::SearchProvider2Stub::unregister_object()
{
    for (const RegisteredObject &obj: m_registered_objects) {
        obj.connection->unregister_object(obj.id);
    }
    m_registered_objects.clear();
}

void org::gnome::Shell::SearchProvider2Stub::on_method_call(
    const Glib::RefPtr<Gio::DBus::Connection> &/* connection */,
    const Glib::ustring &/* sender */,
    const Glib::ustring &/* object_path */,
    const Glib::ustring &/* interface_name */,
    const Glib::ustring &method_name,
    const Glib::VariantContainerBase &parameters,
    const Glib::RefPtr<Gio::DBus::MethodInvocation> &invocation)
{
    static_cast<void>(method_name); // maybe unused
    static_cast<void>(parameters); // maybe unused
    static_cast<void>(invocation); // maybe unused

    if (method_name.compare("GetInitialResultSet") == 0) {
        Glib::Variant<std::vector<Glib::ustring>> base_terms;
        parameters.get_child(base_terms, 0);
        std::vector<Glib::ustring> p_terms = specialGetter(base_terms);

        MethodInvocation methodInvocation(invocation);
        GetInitialResultSet(
            (p_terms),
            methodInvocation);
    }

    if (method_name.compare("GetSubsearchResultSet") == 0) {
        Glib::Variant<std::vector<Glib::ustring>> base_previous_results;
        parameters.get_child(base_previous_results, 0);
        std::vector<Glib::ustring> p_previous_results = specialGetter(base_previous_results);

        Glib::Variant<std::vector<Glib::ustring>> base_terms;
        parameters.get_child(base_terms, 1);
        std::vector<Glib::ustring> p_terms = specialGetter(base_terms);

        MethodInvocation methodInvocation(invocation);
        GetSubsearchResultSet(
            (p_previous_results),
            (p_terms),
            methodInvocation);
    }

    if (method_name.compare("GetResultMetas") == 0) {
        Glib::Variant<std::vector<Glib::ustring>> base_identifiers;
        parameters.get_child(base_identifiers, 0);
        std::vector<Glib::ustring> p_identifiers = specialGetter(base_identifiers);

        MethodInvocation methodInvocation(invocation);
        GetResultMetas(
            (p_identifiers),
            methodInvocation);
    }

    if (method_name.compare("ActivateResult") == 0) {
        Glib::Variant<Glib::ustring> base_identifier;
        parameters.get_child(base_identifier, 0);
        Glib::ustring p_identifier = specialGetter(base_identifier);

        Glib::Variant<std::vector<Glib::ustring>> base_terms;
        parameters.get_child(base_terms, 1);
        std::vector<Glib::ustring> p_terms = specialGetter(base_terms);

        Glib::Variant<guint32> base_timestamp;
        parameters.get_child(base_timestamp, 2);
        guint32 p_timestamp = specialGetter(base_timestamp);

        MethodInvocation methodInvocation(invocation);
        ActivateResult(
            (p_identifier),
            (p_terms),
            (p_timestamp),
            methodInvocation);
    }

    if (method_name.compare("LaunchSearch") == 0) {
        Glib::Variant<std::vector<Glib::ustring>> base_terms;
        parameters.get_child(base_terms, 0);
        std::vector<Glib::ustring> p_terms = specialGetter(base_terms);

        Glib::Variant<guint32> base_timestamp;
        parameters.get_child(base_timestamp, 1);
        guint32 p_timestamp = specialGetter(base_timestamp);

        MethodInvocation methodInvocation(invocation);
        LaunchSearch(
            (p_terms),
            (p_timestamp),
            methodInvocation);
    }

}

void org::gnome::Shell::SearchProvider2Stub::on_interface_get_property(
    Glib::VariantBase &property,
    const Glib::RefPtr<Gio::DBus::Connection> &/* connection */,
    const Glib::ustring &/* sender */,
    const Glib::ustring &/* object_path */,
    const Glib::ustring &/* interface_name */,
    const Glib::ustring &property_name)
{
    static_cast<void>(property); // maybe unused
    static_cast<void>(property_name); // maybe unused

}

bool org::gnome::Shell::SearchProvider2Stub::on_interface_set_property(
    const Glib::RefPtr<Gio::DBus::Connection> &/* connection */,
    const Glib::ustring &/* sender */,
    const Glib::ustring &/* object_path */,
    const Glib::ustring &/* interface_name */,
    const Glib::ustring &property_name,
    const Glib::VariantBase &value)
{
    static_cast<void>(property_name); // maybe unused
    static_cast<void>(value); // maybe unused

    return true;
}


bool org::gnome::Shell::SearchProvider2Stub::emitSignal(
    const std::string &propName,
    Glib::VariantBase &value)
{
    std::map<Glib::ustring, Glib::VariantBase> changedProps;
    std::vector<Glib::ustring> changedPropsNoValue;

    changedProps[propName] = value;

    Glib::Variant<std::map<Glib::ustring, Glib::VariantBase>> changedPropsVar =
        Glib::Variant<std::map<Glib::ustring, Glib::VariantBase>>::create(changedProps);
    Glib::Variant<std::vector<Glib::ustring>> changedPropsNoValueVar =
        Glib::Variant<std::vector<Glib::ustring>>::create(changedPropsNoValue);
    std::vector<Glib::VariantBase> ps;
    ps.push_back(Glib::Variant<Glib::ustring>::create(m_interfaceName));
    ps.push_back(changedPropsVar);
    ps.push_back(changedPropsNoValueVar);
    Glib::VariantContainerBase propertiesChangedVariant =
        Glib::Variant<std::vector<Glib::VariantBase>>::create_tuple(ps);

    for (const RegisteredObject &obj: m_registered_objects) {
        obj.connection->emit_signal(
            obj.object_path,
            "org.freedesktop.DBus.Properties",
            "PropertiesChanged",
            Glib::ustring(),
            propertiesChangedVariant);
    }

    return true;
}