aboutsummaryrefslogtreecommitdiffstats
path: root/ubuntu-online-accounts/cc-plugins/generate-plugins.py
blob: babe5433c61c53030421f2b40b0265c844310ab1 (plain) (blame)
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
339
340
341
342
#!/usr/bin/env python

# (name, CM, protocol, icon)
ALL = [
        ('AIM', 'haze', 'aim', 'aim'),
        ('GaduGadu', 'haze', 'gadugadu', 'gadugadu'),
        ('Groupwise', 'haze', 'groupwise', 'groupwise'),
        ('ICQ', 'haze', 'icq', 'icq'),
        ('IRC', 'idle', 'irc', 'irc'),
        ('Jabber', 'gabble', 'jabber', 'jabber'),
        ('Mxit', 'haze', 'mxit', 'mxit'),
        ('Myspace', 'haze', 'myspace', 'myspace'),
        ('SIP', 'sofiasip', 'sip', 'sip'),
        ('Salut', 'salut', 'local-xmpp', 'people-nearby'),
        ('Sametime', 'haze', 'sametime', 'sametime'),
        ('Yahoo Japan', 'haze', 'yahoojp', 'yahoo'),
        ('Yahoo!', 'haze', 'yahoo', 'yahoo'),
        ('Zephyr', 'haze', 'zephyr', 'zephyr'),
      ]

class Plugin:
    def __init__(self, name, cm, protocol, icon):
        self.name = name
        self.cm = cm
        self.protocol = protocol
        self.icon = icon

##### The plugin itself #####

def magic_replace(text, protocol):
    p = protocol.replace('-', '_')

    l = protocol.split('-')
    l = map(str.title, l)
    camel = ''.join(l)

    text = text.replace('$lower', p)
    text = text.replace('$UPPER', p.upper())
    text = text.replace('$Camel', camel)

    return text

def generate_plugin_header(p):
    # header
    f = open('empathy-accounts-plugin-%s.h' % p.protocol, 'w')

    tmp = '''/* # Generated using empathy/ubuntu-online-accounts/cc-plugins/generate-plugins.py
 * Do NOT edit manually */

/*
 * empathy-accounts-plugin-%s.h
 *
 * Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * 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
 */


#ifndef __EMPATHY_ACCOUNTS_PLUGIN_$UPPER_H__
#define __EMPATHY_ACCOUNTS_PLUGIN_$UPPER_H__

#include "empathy-accounts-plugin.h"

G_BEGIN_DECLS

typedef struct _EmpathyAccountsPlugin$Camel EmpathyAccountsPlugin$Camel;
typedef struct _EmpathyAccountsPlugin$CamelClass EmpathyAccountsPlugin$CamelClass;

struct _EmpathyAccountsPlugin$CamelClass
{
  /*<private>*/
  EmpathyAccountsPluginClass parent_class;
};

struct _EmpathyAccountsPlugin$Camel
{
  /*<private>*/
  EmpathyAccountsPlugin parent;
};

GType empathy_accounts_plugin_$lower_get_type (void);

/* TYPE MACROS */
#define EMPATHY_TYPE_ACCOUNTS_PLUGIN_$UPPER \\
  (empathy_accounts_plugin_$lower_get_type ())
#define EMPATHY_ACCOUNTS_PLUGIN_$UPPER(obj) \\
  (G_TYPE_CHECK_INSTANCE_CAST((obj), \\
    EMPATHY_TYPE_ACCOUNTS_PLUGIN_$UPPER, \\
    EmpathyAccountsPlugin$Camel))
#define EMPATHY_ACCOUNTS_PLUGIN_$UPPER_CLASS(klass) \\
  (G_TYPE_CHECK_CLASS_CAST((klass), \\
    EMPATHY_TYPE_ACCOUNTS_PLUGIN_$UPPER, \\
    EmpathyAccountsPlugin$CamelClass))
#define EMPATHY_IS_ACCOUNTS_PLUGIN_$UPPER(obj) \\
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), \\
    EMPATHY_TYPE_ACCOUNTS_PLUGIN_$UPPER))
#define EMPATHY_IS_ACCOUNTS_PLUGIN_$UPPER_CLASS(klass) \\
  (G_TYPE_CHECK_CLASS_TYPE((klass), \\
    EMPATHY_TYPE_ACCOUNTS_PLUGIN_$UPPER))
#define EMPATHY_ACCOUNTS_PLUGIN_$UPPER_GET_CLASS(obj) \\
  (G_TYPE_INSTANCE_GET_CLASS ((obj), \\
    EMPATHY_TYPE_ACCOUNTS_PLUGIN_$UPPER, \\
    EmpathyAccountsPlugin$CamelClass))

GType ap_module_get_object_type (void);

G_END_DECLS

#endif /* #ifndef __EMPATHY_ACCOUNTS_PLUGIN_$UPPER_H__*/''' % (p.protocol)

    f.write(magic_replace (tmp, p.protocol))

def generate_plugin_code(p):
    # header
    f = open('empathy-accounts-plugin-%s.c' % p.protocol, 'w')

    tmp = '''/* # Generated using empathy/ubuntu-online-accounts/cc-plugins/generate-plugins.py
 * Do NOT edit manually */

/*
 * empathy-accounts-plugin-%s.c
 *
 * Copyright (C) 2012 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * 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
 */

#include "config.h"

#include "empathy-accounts-plugin-%s.h"

G_DEFINE_TYPE (EmpathyAccountsPlugin$Camel, empathy_accounts_plugin_$lower,\\
        EMPATHY_TYPE_ACCOUNTS_PLUGIN)

static void
empathy_accounts_plugin_$lower_class_init (
    EmpathyAccountsPlugin$CamelClass *klass)
{
}

static void
empathy_accounts_plugin_$lower_init (EmpathyAccountsPlugin$Camel *self)
{
}

GType
ap_module_get_object_type (void)
{
  return EMPATHY_TYPE_ACCOUNTS_PLUGIN_$UPPER;
}''' % (p.protocol, p.protocol)

    f.write(magic_replace (tmp, p.protocol))

def generate_plugins(plugins):
    '''empathy-accounts-plugin-$protocol.[ch]'''
    for p in plugins:
        generate_plugin_header(p)
        generate_plugin_code(p)

def generate_build_block(p):
    la = 'lib%s_la' % p.protocol.replace('-', '_')

    output = '''%s_SOURCES = \\
    empathy-accounts-plugin.c \\
    empathy-accounts-plugin.h \\
    empathy-accounts-plugin-%s.c \\
    empathy-accounts-plugin-%s.h \\
    empathy-accounts-plugin-widget.c \\
    empathy-accounts-plugin-widget.h
%s_LDFLAGS = -module -avoid-version
%s_LIBADD = \\
    $(UOA_LIBS)                 \\
    $(top_builddir)/libempathy/libempathy.la \\
    $(top_builddir)/libempathy-gtk/libempathy-gtk.la
''' % (la, p.protocol, p.protocol, la, la)

    return output

def generate_makefile_am(plugins):
    '''Generate Makefile.am'''
    libs = []
    build_blocks = []

    for p in plugins:
        name = '    lib%s.la' % p.protocol
        libs.append(name)

        build_blocks.append(generate_build_block(p))

    f = open('Makefile.am', 'w')

    f.write(
'''# Generated using empathy/ubuntu-online-accounts/cc-plugins/generate-plugins.py
# Do NOT edit manually
SUBDIRS = providers services app-plugin

plugindir = $(ACCOUNTS_PROVIDER_PLUGIN_DIR)

INCLUDES =                  \\
    -I$(top_builddir)           \\
    -I$(top_srcdir)             \\
    -DLOCALEDIR=\\""$(datadir)/locale"\\"   \\
    $(UOA_CFLAGS)               \\
    $(WARN_CFLAGS)              \\
    $(ERROR_CFLAGS)             \\
    $(DISABLE_DEPRECATED)           \\
    $(EMPATHY_CFLAGS)

plugin_LTLIBRARIES = \\
%s \\
    $(NULL)

%s''' % ('\\\n'.join(libs), '\n\n'.join(build_blocks)))

##### Providers #####

def generate_provider_file(p):
    f = open('providers/%s.provider' % p.protocol, 'w')

    f.write(
'''<?xml version="1.0" encoding="UTF-8" ?>
<!-- Generated using empathy/ubuntu-online-accounts/cc-plugins/generate-plugins.py
     Do NOT edit manually -->
<provider id="%s">
  <name>%s</name>
  <icon>%s</icon>
</provider>
''' % (p.protocol, p.name, p.icon))

def generate_providers(plugins):
    '''generate providers/*.provider files and providers/Makefile.am'''

    providers = []
    for p in plugins:
        providers.append('  %s.provider' % p.protocol)

        generate_provider_file(p)

    # providers/Makefile.am
    f = open('providers/Makefile.am', 'w')
    f.write(
'''# Generated using empathy/ubuntu-online-accounts/cc-plugins/generate-plugins.py
# Do NOT edit manually
providersdir = $(ACCOUNTS_PROVIDER_FILES_DIR)

providers_DATA = \\
%s \\
    $(NULL)

EXTRA_DIST = $(providers_DATA)
''' % ('\\\n'.join(providers)))

##### Services #####

def generate_service_file(p):
    f = open('services/%s-im.service' % p.protocol, 'w')

    f.write(
'''<?xml version="1.0" encoding="UTF-8" ?>
<!-- Generated using empathy/ubuntu-online-accounts/cc-plugins/generate-plugins.py
     Do NOT edit manually -->
<service id="%s-im">
  <type>IM</type>
  <name>%s</name>
  <icon>%s</icon>
  <provider>%s</provider>

  <!-- default settings (account settings have precedence over these) -->
  <template>
    <group name="telepathy">
      <setting name="manager">%s</setting>
      <setting name="protocol">%s</setting>
    </group>
    <group name="auth">
      <setting name="method">password</setting>
      <setting name="mechanism">password</setting>
    </group>
  </template>

</service>
''' % (p.protocol, p.name, p.icon, p.protocol, p.cm, p.protocol))

def generate_services(plugins):
    '''generate services/*-im.service files and services/Makefile.am'''

    services = []
    for p in plugins:
        services.append('   %s-im.service' % p.protocol)

        generate_service_file(p)

    # providers/Makefile.am
    f = open('services/Makefile.am', 'w')
    f.write(
'''# Generated using empathy/ubuntu-online-accounts/cc-plugins/generate-plugins.py
# Do NOT edit manually
servicesdir = $(ACCOUNTS_SERVICE_FILES_DIR)

services_DATA = \\
%s \\
    $(NULL)

EXTRA_DIST = $(services_DATA)
''' % ('\\\n'.join(services)))

def generate_all():
    plugins = []

    for name, cm, protocol, icon in ALL:
        plugins.append(Plugin(name, cm, protocol, icon))

    generate_plugins(plugins)
    generate_makefile_am(plugins)
    generate_providers(plugins)
    generate_services(plugins)

if __name__ == '__main__':
    generate_all()