summaryrefslogtreecommitdiff
path: root/ytstenut/yts-factory.c
blob: 96036347153bb59d76330f660b910b7bc56fca88 (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
/*
 * Copyright © 2011 Intel Corp.
 *
 * 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 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/>.
 *
 * Authored by: Rob Staudinger <robsta@linux.intel.com>
 */
#include "config.h"

#include <stdlib.h>
#include <string.h>

#include "yts-factory.h"

#include "video-profile/yts-vp-player-adapter.h"
#include "video-profile/yts-vp-player-proxy.h"
#include "video-profile/yts-vp-transcript-adapter.h"
#include "video-profile/yts-vp-transcript-proxy.h"

G_DEFINE_ABSTRACT_TYPE (YtsFactory, yts_factory, G_TYPE_OBJECT)

typedef struct {
  char const *fqc_id;
  GType       adapter_gtype;
  GType       proxy_gtype;
} FactoryEntry;

static GArray *_table = NULL;

static int
_factory_entry_compare (const void *fe1,
                        const void *fe2)
{
  return strcmp (((FactoryEntry *) fe1)->fqc_id,
                 ((FactoryEntry *) fe2)->fqc_id);
}

static void
_dispose (GObject *object)
{
  G_OBJECT_CLASS (yts_factory_parent_class)->dispose (object);
}

static void
yts_factory_class_init (YtsFactoryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  FactoryEntry table[] = {
    { "org.freedesktop.ytstenut.VideoProfile.Player",
       yts_vp_player_adapter_get_type (),
       yts_vp_player_proxy_get_type () },
    { "org.freedesktop.ytstenut.VideoProfile.Transcript",
       yts_vp_transcript_adapter_get_type (),
       yts_vp_transcript_proxy_get_type () }
  };

  object_class->dispose = _dispose;

  if (NULL == _table) {
    _table = g_array_sized_new (false,
                                false,
                                sizeof (FactoryEntry),
                                G_N_ELEMENTS (table));
    memcpy (_table->data, table, sizeof (table));
    _table->len = G_N_ELEMENTS (table);
    g_array_sort (_table, _factory_entry_compare);
  }
}

static void
yts_factory_init (YtsFactory *self)
{
}

bool
yts_factory_has_fqc_id (YtsFactory const  *self,
                        char const        *fqc_id)
{
  FactoryEntry const *entry;
  FactoryEntry        needle;

  needle.fqc_id = fqc_id;
  entry = bsearch (&needle,
                   _table->data,
                   _table->len,
                   sizeof (FactoryEntry),
                   _factory_entry_compare);

  return (bool) entry;
}

GType
yts_factory_get_proxy_gtype_for_fqc_id (YtsFactory const  *self,
                                        char const        *fqc_id)
{
  FactoryEntry const *entry;
  FactoryEntry        needle;

  needle.fqc_id = fqc_id;
  entry = bsearch (&needle,
                   _table->data,
                   _table->len,
                   sizeof (FactoryEntry),
                   _factory_entry_compare);

  if (entry) {
    return entry->proxy_gtype;
  }

  return G_TYPE_INVALID;
}

GType
yts_factory_get_adapter_gtype_for_fqc_id (YtsFactory const  *self,
                                          char const        *fqc_id)
{
  FactoryEntry const *entry;
  FactoryEntry        needle;

  needle.fqc_id = fqc_id;
  entry = bsearch (&needle,
                   _table->data,
                   _table->len,
                   sizeof (FactoryEntry),
                   _factory_entry_compare);

  if (entry) {
    return entry->adapter_gtype;
  }

  return G_TYPE_INVALID;
}