~ubuntu-branches/ubuntu/karmic/libtinymail/karmic

« back to all changes in this revision

Viewing changes to tests/shared/account-store.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Kowalik
  • Date: 2007-10-12 11:21:12 UTC
  • Revision ID: james.westby@ubuntu.com-20071012112112-fod9fs7yrooxjr7i
Tags: upstream-0.0.2
ImportĀ upstreamĀ versionĀ 0.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* tinymail - Tiny Mail
 
2
 * Copyright (C) 2006-2007 Philip Van Hoof <pvanhoof@gnome.org>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Lesser General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Lesser General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Lesser General Public
 
15
 * License along with self library; if not, write to the
 
16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
 * Boston, MA 02111-1307, USA.
 
18
 */
 
19
 
 
20
#include <sys/mman.h>
 
21
#include <camel/camel-session.h>
 
22
 
 
23
#include <config.h>
 
24
#include <glib/gi18n-lib.h>
 
25
 
 
26
#include <string.h>
 
27
#include <glib.h>
 
28
 
 
29
#include <tny-platform-factory.h>
 
30
#include "platfact.h"
 
31
 
 
32
#include <tny-account-store.h>
 
33
#include <tny-account.h>
 
34
#include <tny-camel-account.h>
 
35
 
 
36
#include <tny-store-account.h>
 
37
#include <tny-transport-account.h>
 
38
#include <tny-camel-store-account.h>
 
39
#include <tny-camel-transport-account.h>
 
40
#include <tny-device.h>
 
41
#include <tny-session-camel.h>
 
42
 
 
43
#include "device.h"
 
44
#include "account-store.h"
 
45
 
 
46
 
 
47
static GObjectClass *parent_class = NULL;
 
48
 
 
49
 
 
50
static gchar* 
 
51
per_account_get_pass_func (TnyAccount *account, const gchar *prompt, gboolean *cancel)
 
52
{
 
53
        if (strstr (tny_account_get_name (account), "SMTP"))
 
54
                return g_strdup ("unittest");
 
55
        else
 
56
                return g_strdup ("tnytest");
 
57
}
 
58
 
 
59
static void
 
60
per_account_forget_pass_func (TnyAccount *account)
 
61
{
 
62
        g_print ("Invalid test account (password incorrect)\n");
 
63
        return;
 
64
}
 
65
 
 
66
 
 
67
static gboolean
 
68
tny_test_account_store_alert (TnyAccountStore *self, TnyAccount *account, TnyAlertType type, gboolean question, const GError *error)
 
69
{
 
70
        return TRUE;
 
71
}
 
72
 
 
73
 
 
74
static const gchar*
 
75
tny_test_account_store_get_cache_dir (TnyAccountStore *self)
 
76
{
 
77
        TnyTestAccountStore *me = (TnyTestAccountStore*) self;
 
78
 
 
79
        if (me->cache_dir == NULL)
 
80
        {
 
81
                gint att=0;
 
82
                GDir *dir = NULL;
 
83
                do {
 
84
                        gchar *attempt = g_strdup_printf ("tinymail.%d", att);
 
85
                        gchar *str = g_build_filename (g_get_tmp_dir (), attempt, NULL);
 
86
                        g_free (attempt);
 
87
                        dir = g_dir_open (str, 0, NULL);
 
88
                        if (dir)
 
89
                        {
 
90
                                g_dir_close (dir);
 
91
                                g_free (str);
 
92
                        } else 
 
93
                                me->cache_dir = str;
 
94
                        att++;
 
95
                } while (dir != NULL);
 
96
        }
 
97
    
 
98
        return me->cache_dir;
 
99
}
 
100
 
 
101
 
 
102
static void
 
103
tny_test_account_store_get_accounts (TnyAccountStore *self, TnyList *list, TnyGetAccountsRequestType types)
 
104
{
 
105
        TnyTestAccountStore *me = (TnyTestAccountStore *) self;
 
106
        TnyAccount *account;
 
107
 
 
108
 
 
109
        /* Dear visitor of the SVN-web. This is indeed a fully functional and
 
110
           working IMAP account. This does not mean that you need to fuck it up */
 
111
 
 
112
        if (types == TNY_ACCOUNT_STORE_STORE_ACCOUNTS || types == TNY_ACCOUNT_STORE_BOTH)
 
113
        {
 
114
                account = TNY_ACCOUNT (tny_camel_store_account_new ());
 
115
 
 
116
                tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (account), me->session);
 
117
                camel_session_set_online ((CamelSession*)me->session, me->force_online); 
 
118
                tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (account), me->force_online, NULL, NULL);
 
119
 
 
120
                tny_account_set_proto (account, "imap");
 
121
                tny_account_set_name (account, "imap1.tinymail.org");
 
122
                tny_account_set_user (account, "tnytest");
 
123
                tny_account_set_hostname (account, "imap1.tinymail.org");
 
124
                tny_account_set_id (account, "tnytest@imap1.tinymail.org");
 
125
                tny_account_set_forget_pass_func (account, per_account_forget_pass_func);
 
126
                tny_account_set_pass_func (account, per_account_get_pass_func);
 
127
 
 
128
                tny_list_prepend (list, (GObject*)account);
 
129
                g_object_unref (G_OBJECT (account));
 
130
        }
 
131
 
 
132
        if (types == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS || types == TNY_ACCOUNT_STORE_BOTH)
 
133
        {
 
134
                account = TNY_ACCOUNT (tny_camel_transport_account_new ());
 
135
 
 
136
                tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (account), me->session);
 
137
                camel_session_set_online ((CamelSession*)me->session, me->force_online); 
 
138
                tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (account), me->force_online, NULL, NULL);
 
139
 
 
140
                tny_account_set_proto (account, "something");
 
141
                tny_account_set_name (account, "SMTP unit test account");
 
142
                tny_account_set_id (account, "unique_smtp");
 
143
                tny_account_set_url_string (account, "smtp://tinymailunittest;auth=PLAIN@mail.tinymail.org:2222/;use_ssl=wrapped");
 
144
 
 
145
                tny_account_set_forget_pass_func (account, per_account_forget_pass_func);
 
146
                tny_account_set_pass_func (account, per_account_get_pass_func);
 
147
 
 
148
                tny_list_prepend (list, (GObject*)account);
 
149
                g_object_unref (G_OBJECT (account));
 
150
        }
 
151
 
 
152
        if (me->force_online)
 
153
                tny_device_force_online (me->device);
 
154
        else
 
155
                tny_device_force_offline (me->device);
 
156
 
 
157
        return;
 
158
}
 
159
 
 
160
 
 
161
 
 
162
TnyAccountStore*
 
163
tny_test_account_store_new (gboolean force_online, const gchar *cachedir)
 
164
{
 
165
        TnyTestAccountStore *self = g_object_new (TNY_TYPE_TEST_ACCOUNT_STORE, NULL);
 
166
 
 
167
        if (cachedir)
 
168
        {
 
169
                if (self->cache_dir)
 
170
                        g_free (self->cache_dir);
 
171
                self->cache_dir = g_strdup (cachedir);
 
172
        }
 
173
 
 
174
        self->session = tny_session_camel_new (TNY_ACCOUNT_STORE (self));
 
175
 
 
176
        self->force_online = force_online;
 
177
 
 
178
 
 
179
        return TNY_ACCOUNT_STORE (self);
 
180
}
 
181
 
 
182
 
 
183
static void
 
184
tny_test_account_store_instance_init (GTypeInstance *instance, gpointer g_class)
 
185
{
 
186
        TnyTestAccountStore *self = (TnyTestAccountStore *)instance;
 
187
        TnyPlatformFactory *platfact = tny_test_platform_factory_get_instance ();
 
188
 
 
189
        self->device = tny_platform_factory_new_device (platfact);
 
190
        g_object_unref (G_OBJECT (platfact));
 
191
 
 
192
        return;
 
193
}
 
194
 
 
195
 
 
196
static void
 
197
tny_test_account_store_finalize (GObject *object)
 
198
{
 
199
        TnyTestAccountStore *me = (TnyTestAccountStore*) object;
 
200
    
 
201
        if (me->cache_dir)
 
202
                g_free (me->cache_dir);
 
203
 
 
204
        g_object_unref (me->device);
 
205
    
 
206
        (*parent_class->finalize) (object);
 
207
 
 
208
        return;
 
209
}
 
210
 
 
211
 
 
212
static void 
 
213
tny_test_account_store_class_init (TnyTestAccountStoreClass *class)
 
214
{
 
215
        GObjectClass *object_class;
 
216
    
 
217
        parent_class = g_type_class_peek_parent (class);
 
218
        object_class = (GObjectClass*) class;
 
219
 
 
220
        object_class->finalize = tny_test_account_store_finalize;
 
221
 
 
222
        return;
 
223
}
 
224
 
 
225
 
 
226
static TnyDevice*
 
227
tny_test_account_store_get_device (TnyAccountStore *self)
 
228
{
 
229
        TnyTestAccountStore *me =  (TnyTestAccountStore*) self;
 
230
 
 
231
        return g_object_ref (G_OBJECT (me->device));
 
232
}
 
233
 
 
234
 
 
235
static void
 
236
tny_account_store_init (gpointer g, gpointer iface_data)
 
237
{
 
238
        TnyAccountStoreIface *klass = (TnyAccountStoreIface *)g;
 
239
 
 
240
        klass->get_accounts_func = tny_test_account_store_get_accounts;
 
241
        klass->get_cache_dir_func = tny_test_account_store_get_cache_dir;
 
242
        klass->alert_func = tny_test_account_store_alert;
 
243
        klass->get_device_func = tny_test_account_store_get_device;
 
244
    
 
245
        return;
 
246
}
 
247
 
 
248
 
 
249
GType 
 
250
tny_test_account_store_get_type (void)
 
251
{
 
252
        static GType type = 0;
 
253
 
 
254
        if (G_UNLIKELY(type == 0))
 
255
        {
 
256
                static const GTypeInfo info = 
 
257
                {
 
258
                  sizeof (TnyTestAccountStoreClass),
 
259
                  NULL,   /* base_init */
 
260
                  NULL,   /* base_finalize */
 
261
                  (GClassInitFunc) tny_test_account_store_class_init,   /* class_init */
 
262
                  NULL,   /* class_finalize */
 
263
                  NULL,   /* class_data */
 
264
                  sizeof (TnyTestAccountStore),
 
265
                  0,      /* n_preallocs */
 
266
                  tny_test_account_store_instance_init    /* instance_init */
 
267
                };
 
268
 
 
269
                static const GInterfaceInfo tny_account_store_info = 
 
270
                {
 
271
                  (GInterfaceInitFunc) tny_account_store_init, /* interface_init */
 
272
                  NULL,         /* interface_finalize */
 
273
                  NULL          /* interface_data */
 
274
                };
 
275
 
 
276
                type = g_type_register_static (G_TYPE_OBJECT,
 
277
                        "TnyTestAccountStore",
 
278
                        &info, 0);
 
279
 
 
280
                g_type_add_interface_static (type, TNY_TYPE_ACCOUNT_STORE, 
 
281
                        &tny_account_store_info);
 
282
        }
 
283
 
 
284
        return type;
 
285
}