~charlesk/libunity/unity-actions-api

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
/*
 * Copyright (C) 2011 Canonical, Ltd.
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * version 3.0 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3.0 for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * Authored by Neil Jagdish Patel <neil.patel@canonical.com>
 *
 */

using GLib;
using Dee;
using Unity.Protocol;

namespace Unity {

private class ScopeProxyRemote : GLib.Object, ScopeProxy
{
  public string dbus_name { get; construct; }
  public string dbus_path { get; construct; }

  public OptionsFilter sources { get; set; }
  public bool search_in_global { get; set; }
  public bool provides_personal_content { get; set; }

  public Dee.SerializableModel results_model {
    get { return _results_model; }
    set {
      _results_model = value as Dee.SharedModel;
    }
  }

  public Dee.SerializableModel global_results_model {
    get { return _global_results_model; }
    set {
      _global_results_model = value as Dee.SharedModel;
    }
  }

  public Dee.SerializableModel filters_model {
    get { return _filters_model; }
    set {
      _filters_model = value as Dee.SharedModel;
    }
  }

  public ViewType view_type {
    get { return _view_type; }
    // make sure we do dbus calls only on changes
    set { if (_view_type != value) set_view_type (value); }
  }

  private ViewType _view_type;
  private DBusConnection _bus;
  private uint _watcher;

  private ScopeService _service;

  private Dee.SharedModel? _results_model;
  private Dee.SharedModel? _global_results_model;
  private Dee.SharedModel? _filters_model;
  private uint _reconnection_id = 0;
  private bool synchronized = false;
  private int64 _last_scope_crash = 0;
  private uint _scope_crashes = 0;

  public ScopeProxyRemote (string dbus_name_, string dbus_path_)
  {
    Object (dbus_name:dbus_name_, dbus_path:dbus_path_);
  }

  construct
  {
    sources = new CheckOptionFilter ("sources", "Sources", null, true);
    try {
      _bus = Bus.get_sync (BusType.SESSION);
      _watcher = Bus.watch_name_on_connection (_bus,
                                               dbus_name,
                                               BusNameWatcherFlags.NONE,
                                               on_scope_appeared,
                                               on_scope_vanished);

      /* If it doesn't exist when we load up, start it */
      start_reconnection_timeout ();
    } catch (Error e) {
      critical (@"Unable to connect to session bus: $(e.message)");
    }
  }

  private async void connect_to_scope ()
  {
    try {
      _service = yield _bus.get_proxy (dbus_name, dbus_path);
      _service.changed.connect (on_changed);
      yield _service.info_request ();

    } catch (Error e) {
      warning (@"Unable to connect to Scope ($dbus_path @ $dbus_name): $(e.message)");
    }
  }
 
  private void on_scope_appeared (DBusConnection conn,
                                  string         name,
                                  string         name_owner)
  {
    Trace.log_object (this, "Scope appeared: %s", dbus_name);
    if (_reconnection_id != 0)
      Source.remove (_reconnection_id);
    connect_to_scope.begin ();
  }

  private void on_scope_vanished (DBusConnection conn,
                                  string         name)
  {
    Trace.log_object (this, "Scope vanished: %s", dbus_name);

    synchronized = false;
    search_in_global = false;
    provides_personal_content = false;
    sources = new CheckOptionFilter ("sources", "Sources", null, true);
    if (_results_model != null)
      _results_model.clear ();

    if (_global_results_model != null)
      _global_results_model.clear ();

    /* No need to clear the filters model, it's read-only for the scope and
     * it would just cause warnings from filters synchronizer */
    _filters_model = null;

    if (_service != null)
    {
      /* Here comes the protected-restarting logic - the scope will be
       * restarted unless it crashed more than 10 times during the past
       * 15 minutes */
      _scope_crashes++;
      var cur_time = get_monotonic_time ();
      var time_since_last_crash = cur_time - _last_scope_crash;

      if (time_since_last_crash >= 15*60000000) // 15 minutes
      {
        _last_scope_crash = cur_time;
        // reset crash counter, it's not that bad
        _scope_crashes = 1;
      }
      else if (_scope_crashes >= 10)
      {
        // more than 10 crashes in the past 15 minutes
        warning ("Scope %s is crashing too often, disabling it", dbus_name);
        return;
      }

      start_reconnection_timeout ();
    }
    else
    {
      start_reconnection_timeout ();
    }
  }

  private void start_reconnection_timeout ()
  {
    if (_reconnection_id != 0)
      Source.remove (_reconnection_id);

    _reconnection_id = Timeout.add_seconds (2, () =>
    {
      if (_service == null)
        connect_to_scope ();
      else if ((_service as DBusProxy).g_name_owner == null)
        _service.info_request (); // ping the service to autostart it

      _reconnection_id = 0;
      return false;
    });
  }

  /*
   * Implementation of the ScopeService interface
   */
  public async ActivationReplyRaw activate (string uri, ActionType action_type,
                                            HashTable<string, Variant> hints)
  {
    if (synchronized)
    {
      try {
        var raw = yield _service.activate (uri, (uint) action_type, hints);
        return raw;
      } catch (Error e) {
        warning (@"Unable to search scope ($dbus_path): $(e.message)");
      }
    }
    return ActivationReplyRaw ();
  }

  public async HashTable<string, Variant> update_preview_property (
      string uri, HashTable<string, Variant> values) throws IOError
  {
    if (synchronized)
    {
      try {
        var raw = yield _service.update_preview_property (uri, values);
        return raw;
      } catch (Error e) {
        warning (@"Unable to update_preview_property ($dbus_path): $(e.message)");
      }
    }

    return new HashTable<string, Variant> (str_hash, str_equal);
  }

  public async HashTable<string, Variant> search (
      string search_string, HashTable<string, Variant> hints)
  {
    if (synchronized)
    {
      try {
        var ht = yield _service.search (search_string, hints);
        return ht;
      } catch (ScopeError.SEARCH_CANCELLED scope_error) {
        // that's fine
      } catch (Error e) {
        warning (@"Unable to search scope ($dbus_path - '$search_string'): $(e.message)");
      }
    }

    return new HashTable<string, Variant> (str_hash, str_equal);
  }

  public async HashTable<string, Variant> global_search (
      string search_string, HashTable<string, Variant> hints)
  {
    if (synchronized)
    {
      try {
        var result = yield _service.global_search (search_string, hints);
        return result;
      } catch (ScopeError.SEARCH_CANCELLED scope_error) {
        // that's fine
      } catch (Error e) {
        warning (@"Unable to global_search scope ($dbus_path - '$search_string'): $(e.message)");
      }
    }

    return new HashTable<string, Variant> (str_hash, str_equal);
  }

  public async void set_view_type (ViewType view_type)
  {
    _view_type = view_type;
    if (synchronized)
    {
      try {
        // FIXME: no need to set HOME_VIEW if !search_in_global
        yield _service.set_view_type (view_type);
      } catch (Error e) {
        warning (@"Unable to set_active ($dbus_path): $(e.message)");
      }
    }
  }

  public async void set_active_sources (string[] sources)
  {
    if (synchronized)
    {
      try {
        yield _service.set_active_sources (sources);
      } catch (Error e) {
        warning (@"Unable to set_sources ($dbus_path): $(e.message)");
      }
    }
  }
  
  /* This is where we figure out the diff between states */
  public void on_changed (ScopeInfo scope_info)
  {
    if (dbus_path != scope_info.dbus_path)
    {
      warning (@"Unable to handle Scope changed signal: dbus_path mismatch. Expected $dbus_path got $(scope_info.dbus_path)");
      return;
    }
    Trace.log_object (this, "Processing changed signal for %s", dbus_path);
    search_in_global = scope_info.search_in_global;

    if (scope_info.hints.contains ("provides-personal-content"))
    {
      provides_personal_content = scope_info.hints["provides-personal-content"].get_boolean ();
    }
    else
    {
      provides_personal_content = false;
    }

    if (results_model == null ||
        _results_model.get_swarm_name () != scope_info.results_model_name)
    {
      results_model = new Dee.SharedModel (scope_info.results_model_name);
      results_model.set_schema ("s", "s", "u", "s", "s", "s", "s");
    }

    if (global_results_model == null ||
        _global_results_model.get_swarm_name () != scope_info.global_results_model_name)
    {
      global_results_model = new Dee.SharedModel (scope_info.global_results_model_name);
      global_results_model.set_schema ("s", "s", "u", "s", "s", "s", "s");
    }

    if (filters_model == null ||
        _filters_model.get_swarm_name () != scope_info.filters_model_name)
    {
      filters_model = new Dee.SharedModel (scope_info.filters_model_name);
      filters_model.set_schema ("s", "s", "s", "s", "a{sv}", "b", "b", "b");
    }

    /* extract the sources */
    sources.update (scope_info.sources);
    this.notify_property ("sources");

    if (!synchronized)
    {
      synchronized = true;
      set_view_type (_view_type);
      // FIXME: make sure active_sources are set, dispatch last (missed) search
    }
  }
}

} /* Namespace */