summaryrefslogtreecommitdiff
path: root/telepathy-logger/conf.c
blob: f814b0b202f7e481fde9e324a11788c974f02558 (plain)
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
/*
 * Copyright (C) 2009-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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Authors: Cosimo Alfarano <cosimo.alfarano@collabora.co.uk>
 *          Danielle Madeley <danielle.madeley@collabora.co.uk>
 */

#include "config.h"
#include "conf-internal.h"

#include <glib.h>
#include <gio/gio.h>
#include <telepathy-glib/telepathy-glib.h>

#define DEBUG_FLAG TPL_DEBUG_CONF
#include <telepathy-logger/debug-internal.h>
#include <telepathy-logger/util-internal.h>

#define GET_PRIV(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), TPL_TYPE_CONF, TplConfPriv))

#define GSETTINGS_SCHEMA "org.freedesktop.Telepathy.Logger"
#define KEY_ENABLED "enabled"

G_DEFINE_TYPE (TplConf, _tpl_conf, G_TYPE_OBJECT)

static TplConf *conf_singleton = NULL;

typedef struct
{
  gboolean test_mode;
  gchar **ignore_list;
  GSettings *gsettings;
} TplConfPriv;


enum /* properties */
{
  PROP_0,
  PROP_GLOBALLY_ENABLED,
  PROP_IGNORE_LIST,
};


static void
_notify_globally_enable (GSettings *gsettings,
    const gchar *key,
    GObject *self)
{
  g_object_notify (self, "globally-enabled");
}


static void
tpl_conf_get_property (GObject *self,
    guint prop_id,
    GValue *value,
    GParamSpec *pspec)
{
  switch (prop_id)
    {
      case PROP_GLOBALLY_ENABLED:
        g_value_set_boolean (value,
            _tpl_conf_is_globally_enabled (TPL_CONF (self)));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
        break;
    }
}

static void
tpl_conf_set_property (GObject *self,
    guint prop_id,
    const GValue *value,
    GParamSpec *pspec)
{
  switch (prop_id)
    {
      case PROP_GLOBALLY_ENABLED:
        _tpl_conf_globally_enable (TPL_CONF (self),
            g_value_get_boolean (value));
        break;

      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
        break;
    }
}


static void
tpl_conf_finalize (GObject *obj)
{
  TplConfPriv *priv;

  priv = GET_PRIV (obj);

  g_strfreev (priv->ignore_list);
  priv->ignore_list = NULL;

  if (priv->gsettings != NULL)
    {
      g_object_unref (priv->gsettings);
      priv->gsettings = NULL;
    }

  G_OBJECT_CLASS (_tpl_conf_parent_class)->finalize (obj);
}


static GObject *
tpl_conf_constructor (GType type,
    guint n_props,
    GObjectConstructParam *props)
{
  GObject *retval;

  if (conf_singleton != NULL)
    {
      retval = g_object_ref (conf_singleton);
    }
  else
    {
      retval = G_OBJECT_CLASS (_tpl_conf_parent_class)->constructor (type,
          n_props, props);
      conf_singleton = TPL_CONF (retval);
      g_object_add_weak_pointer (retval, (gpointer *) &conf_singleton);
    }
  return retval;
}


static void
_tpl_conf_class_init (TplConfClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = tpl_conf_get_property;
  object_class->set_property = tpl_conf_set_property;
  object_class->finalize = tpl_conf_finalize;
  object_class->constructor = tpl_conf_constructor;

  g_object_class_install_property (object_class, PROP_GLOBALLY_ENABLED,
      g_param_spec_boolean ("globally-enabled",
        "Globally Enabled",
        "TRUE if logging is enabled (may still be disabled for specific users)",
        TRUE,
        G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_IGNORE_LIST,
      g_param_spec_pointer ("ignore-list",
        "Ignore List",
        "List of TplEntities with which not to log conversations.",
        G_PARAM_READWRITE));

  g_type_class_add_private (object_class, sizeof (TplConfPriv));
}


static void
_tpl_conf_init (TplConf *self)
{
  TplConfPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (self,
      TPL_TYPE_CONF, TplConfPriv);

  if (g_getenv ("TPL_TEST_MODE") != NULL)
    {
      priv->test_mode = TRUE;
    }
  else
    {
      priv->gsettings = g_settings_new (GSETTINGS_SCHEMA);

      g_signal_connect (priv->gsettings, "changed::" KEY_ENABLED,
          G_CALLBACK (_notify_globally_enable), self);
    }

  priv->ignore_list = NULL;
}


/**
 * _tpl_conf_dup:
 *
 * Convenience function to obtain a TPL Configuration object, which is a
 * singleton.
 *
 * Returns: a TplConf signleton instance with its reference counter
 * incremented. Remember to unref the counter.
 */
TplConf *
_tpl_conf_dup (void)
{
  return g_object_new (TPL_TYPE_CONF, NULL);
}


/**
 * _tpl_conf_is_globally_enabled:
 * @self: a TplConf instance
 *
 * Whether TPL is globally enabled or not. If it's not globally enabled, no
 * signals will be logged at all.
 * To enable/disable a single account use _tpl_conf_set_accounts_ignorelist()
 *
 * Returns: %TRUE if TPL logging is globally enabled, otherwise returns
 * %FALSE.
 */
gboolean
_tpl_conf_is_globally_enabled (TplConf *self)
{
  g_return_val_if_fail (TPL_IS_CONF (self), FALSE);

  if (GET_PRIV (self)->test_mode)
    return TRUE;
  else
    return g_settings_get_boolean (GET_PRIV (self)->gsettings, KEY_ENABLED);
}


/**
 * _tpl_conf_globally_enable:
 * @self: a TplConf instance
 * @enable: wether to globally enable or globally disable logging.
 *
 * Globally enables or disables logging for TPL. If it's globally disabled, no
 * signals will be logged at all.
 * Note that this will change the global TPL configuration, affecting all the
 * TPL instances, including the TPL logging process and all the clients using
 * libtelepathy-logger.
 */
void
_tpl_conf_globally_enable (TplConf *self,
    gboolean enable)
{
  g_return_if_fail (TPL_IS_CONF (self));

  if (GET_PRIV (self)->test_mode)
    return;

  g_settings_set_boolean (GET_PRIV (self)->gsettings,
      KEY_ENABLED, enable);
}

/**
 * _tpl_conf_set_accounts_ignorelist:
 * @self: a TplConf instance
 * @newlist: a NULL-terminated list of account/entity IDs that should not be logged
 */
void
_tpl_conf_set_ignorelist (TplConf *self,
    const gchar **newlist)
{
  TplConfPriv *priv;

  g_return_if_fail (TPL_IS_CONF (self));

  priv = GET_PRIV (self);

  if (!priv->test_mode) {
    g_settings_set_strv (GET_PRIV (self)->gsettings, "ignorelist", newlist);
  }

  g_strfreev (priv->ignore_list);
  priv->ignore_list = g_strdupv ((gchar **) newlist);

  g_object_notify (G_OBJECT (self), "ignore-list");
}

/**
 * _tpl_conf_get_accounts_ignorelist:
 * @self: a TplConf instance
 *
 * Provides list of IDs in "account_id/entity_id" format. Events from or to
 * this entities should not be logged.
 *
 * Return value: (transfer-full) a newly allocated NULL-terminated list of contact IDs.
 * The list is owned by the @self and should not be freed.
 */
const gchar **
_tpl_conf_get_ignorelist (TplConf *self)
{
  TplConfPriv *priv;

  g_return_val_if_fail (TPL_IS_CONF (self), NULL);

  priv = GET_PRIV (self);

  if ((priv->ignore_list == NULL) && (!priv->test_mode)) {
    priv->ignore_list = g_settings_get_strv (priv->gsettings, "ignorelist");
  }

  return (const gchar **) priv->ignore_list;
}