summaryrefslogtreecommitdiff
path: root/telepathy-logger/channel-factory.c
blob: ce52b899cc8a070b6976432c3f9bef7be5b03d5a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2009 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>
 */

#include "config.h"
#include "channel-factory.h"

#include <telepathy-glib/util.h>

#include <telepathy-logger/channel-text.h>
#include <telepathy-logger/util.h>

#define DEBUG_FLAG TPL_DEBUG_CHANNEL
#include <telepathy-logger/debug.h>

static GHashTable *channel_table = NULL;

void
tpl_channel_factory_init (void)
{
  g_return_if_fail (channel_table == NULL);

  channel_table = g_hash_table_new_full (g_str_hash,
      (GEqualFunc) g_str_equal, g_free, NULL);
}


void
tpl_channel_factory_add (const gchar *type,
    TplChannelConstructor constructor)
{
  gchar *key;

  g_return_if_fail (!TPL_STR_EMPTY (type));
  g_return_if_fail (constructor != NULL);
  g_return_if_fail (channel_table != NULL);

  key = g_strdup (type);

  if (g_hash_table_lookup (channel_table, type) != NULL)
    {
      g_warning ("Type %s already mapped. replacing constructor.", type);
      g_hash_table_replace (channel_table, key, constructor);
    }
  else
    g_hash_table_insert (channel_table, key, constructor);
}


TplChannelConstructor
tpl_channel_factory_lookup (const gchar *type)
{
  g_return_val_if_fail (!TPL_STR_EMPTY (type), NULL);
  g_return_val_if_fail (channel_table != NULL, NULL);

  return g_hash_table_lookup (channel_table, type);
}

void
tpl_channel_factory_deinit (void)
{
  g_return_if_fail (channel_table != NULL);

  g_hash_table_unref (channel_table);
  channel_table = NULL;
}

TplChannel *
tpl_channel_factory_build (const gchar *channel_type,
    TpConnection *conn,
    const gchar *object_path,
    GHashTable *tp_chan_props,
    TpAccount *tp_acc,
    GError **error)
{
  TplChannelConstructor chan_constructor;

  g_return_val_if_fail (channel_table != NULL, NULL);

  chan_constructor = tpl_channel_factory_lookup (channel_type);
  if (chan_constructor == NULL)
    {
      g_set_error (error, TPL_CHANNEL_FACTORY_ERROR,
          TPL_CHANNEL_FACTORY_ERROR_CHANNEL_TYPE_NOT_HANDLED,
          "%s: channel type not handled by this logger", channel_type);
      return NULL;
    }

  return chan_constructor (conn, object_path, tp_chan_props, tp_acc, error);
}