tpf-logger.vala 6.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (C) 2010 Collabora Ltd.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * 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 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/>.
 *
 * Authors:
 *       Philip Withnall <philip.withnall@collabora.co.uk>
 */

using GLib;
using Gee;
23
using TelepathyGLib;
24
25
26
27
using Folks;

private struct AccountFavourites
{
28
  ObjectPath account_path;
29
30
31
32
  string[] ids;
}

[DBus (name = "org.freedesktop.Telepathy.Logger.DRAFT")]
33
private interface LoggerIface : Object
34
35
{
  public abstract async AccountFavourites[] get_favourite_contacts ()
36
      throws GLib.Error;
37
  public abstract async void add_favourite_contact (
38
      ObjectPath account_path, string id) throws GLib.Error;
39
  public abstract async void remove_favourite_contact (
40
      ObjectPath account_path, string id) throws GLib.Error;
41
42

  public abstract signal void favourite_contacts_changed (
43
      ObjectPath account_path, string[] added, string[] removed);
44
45
}

46
47
48
49
50
51
52
/* See: https://mail.gnome.org/archives/vala-list/2011-June/msg00008.html */
[Compact]
private class DelegateWrapper
{
  public SourceFunc cb;
}

53
54
internal class Logger : GLib.Object
{
55
  private static DBusConnection _dbus_conn;
56
  private static LoggerIface _logger;
57
58
  private static DelegateWrapper[] _prepare_waiters = null;

Travis Reitter's avatar
Travis Reitter committed
59
  private uint _logger_watch_id;
60

61
  public signal void invalidated ();
62
63
64
  public signal void favourite_contacts_changed (string[] added,
      string[] removed);

65
66
67
68
  /**
   * D-Bus object path of the {@link TelepathyGLib.Account} to watch for
   * favourite contacts.
   *
Travis Reitter's avatar
Travis Reitter committed
69
   * @since 0.6.6
70
71
72
   */
  public string account_path { get; construct; }

73
  public Logger (string account_path)
74
    {
75
      Object (account_path: account_path);
76
77
    }

Travis Reitter's avatar
Travis Reitter committed
78
79
  ~Logger ()
    {
80
81
82
83
84
      /* Can only be 0 if prepare() hasn't been called. */
      if (this._logger_watch_id > 0)
        {
          Bus.unwatch_name (this._logger_watch_id);
        }
Travis Reitter's avatar
Travis Reitter committed
85
86
    }

87
  public async void prepare () throws GLib.Error
88
    {
89
      if (Logger._logger == null && Logger._prepare_waiters == null)
90
        {
91
92
93
94
95
96
97
          /* If this is the first call to prepare(), start some async calls. We
           * then yield to the main thread. Any subsequent calls to prepare()
           * will have their continuations added to the _prepare_waiters list,
           * and will be signalled once the first call returns.
           * See: https://bugzilla.gnome.org/show_bug.cgi?id=677633 */
          Logger._prepare_waiters = new DelegateWrapper[0];

98
          /* Create a logger proxy for favourites support */
Travis Reitter's avatar
Travis Reitter committed
99
          var dbus_conn = yield Bus.get (BusType.SESSION);
100
          Logger._logger = yield dbus_conn.get_proxy<LoggerIface> (
101
              "org.freedesktop.Telepathy.Logger",
Travis Reitter's avatar
Travis Reitter committed
102
              "/org/freedesktop/Telepathy/Logger");
103

104
          if (Logger._logger != null)
105
            {
106
              Logger._dbus_conn = dbus_conn;
107
108
            }

109
110
111
          /* Wake up any waiters. */
          foreach (unowned DelegateWrapper wrapper in Logger._prepare_waiters)
            {
112
              Idle.add (wrapper.cb);
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
            }

          Logger._prepare_waiters = null;
        }
      else if (Logger._logger == null && Logger._prepare_waiters != null)
        {
          /* Yield until the first ongoing prepare() call finishes. */
          var wrapper = new DelegateWrapper ();
          wrapper.cb = prepare.callback;
          Logger._prepare_waiters += (owned) wrapper;
          yield;
        }

      /* Failure? */
      if (Logger._logger == null)
        {
          this.invalidated ();
          return;
131
132
        }

133
134
135
136
      this._logger_watch_id = Bus.watch_name_on_connection (Logger._dbus_conn,
          "org.freedesktop.Telepathy.Logger", BusNameWatcherFlags.NONE,
          null, this._logger_vanished);

137
      Logger._logger.favourite_contacts_changed.connect ((ap, a, r) =>
138
        {
139
          if (ap != this._account_path)
140
141
142
143
144
145
            return;

          this.favourite_contacts_changed (a, r);
        });
    }

Travis Reitter's avatar
Travis Reitter committed
146
147
148
  private void _logger_vanished (DBusConnection conn, string name)
    {
      /* The logger has vanished on the bus, so it and we are no longer valid */
149
      Logger._logger = null;
150
      Logger._dbus_conn = null;
Travis Reitter's avatar
Travis Reitter committed
151
152
153
      this.invalidated ();
    }

154
  public async string[] get_favourite_contacts () throws GLib.Error
155
    {
156
      /* Invalidated */
157
      if (Logger._logger == null)
158
159
        return {};

Travis Reitter's avatar
Travis Reitter committed
160
161
      /* Use an intermediate, since this._logger could disappear before this
       * async function finishes */
162
      var logger = Logger._logger;
Travis Reitter's avatar
Travis Reitter committed
163
      AccountFavourites[] favs = yield logger.get_favourite_contacts ();
164
165
166
167

      foreach (AccountFavourites account in favs)
        {
          /* We only want the favourites from this account */
168
          if (account.account_path == this._account_path)
169
170
171
172
173
174
            return account.ids;
        }

      return {};
    }

175
  public async void add_favourite_contact (string id) throws GLib.Error
176
    {
177
      /* Invalidated */
178
      if (Logger._logger == null)
179
180
        return;

Travis Reitter's avatar
Travis Reitter committed
181
182
      /* Use an intermediate, since this._logger could disappear before this
       * async function finishes */
183
      var logger = Logger._logger;
Travis Reitter's avatar
Travis Reitter committed
184
      yield logger.add_favourite_contact (
185
          new ObjectPath (this._account_path), id);
186
187
    }

188
  public async void remove_favourite_contact (string id) throws GLib.Error
189
    {
190
      /* Invalidated */
191
      if (Logger._logger == null)
192
193
        return;

Travis Reitter's avatar
Travis Reitter committed
194
195
      /* Use an intermediate, since this._logger could disappear before this
       * async function finishes */
196
      var logger = Logger._logger;
Travis Reitter's avatar
Travis Reitter committed
197
      yield logger.remove_favourite_contact (
198
          new ObjectPath (this._account_path), id);
199
200
    }
}