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., 51 Franklin Street, Fifth Floor,
17
 * Boston, MA 02110-1301, USA.
18
 */
19
20
#include <stdlib.h>
21
#include <string.h>
22
23
#include <glib.h>
24
25
#include <gtk/gtk.h>
26
27
#include <tny-list.h>
28
#include <tny-iterator.h>
29
#include <tny-simple-list.h>
30
#include <tny-account-store.h>
31
#include <tny-store-account.h>
32
33
#include "platfact.h"
34
#include <tny-platform-factory.h>
35
#include <tny-send-queue.h>
36
37
#include <tny-camel-send-queue.h>
38
#include <tny-camel-mem-stream.h>
39
40
#include <account-store.h>
41
42
static gchar *cachedir=NULL;
43
static gboolean online=FALSE, mainloop=TRUE;
44
45
static const GOptionEntry options[] = 
46
{
47
	{ "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
48
		"Cache directory", NULL },
49
	{ "online", 'o', 0, G_OPTION_ARG_NONE, &online,
50
		"Online or offline", NULL },
51
	{ "mainloop", 'm', 0, G_OPTION_ARG_NONE, &mainloop,
52
		"Use the Gtk+ mainloop", NULL },
53
54
	{ NULL }
55
};
56
57
58
59
static gboolean
60
time_s_up (gpointer data)
61
{
62
	exit (0);
63
	return FALSE;
64
}
65
66
static gboolean
67
dance (gpointer data)
68
{
69
	return FALSE;
70
}
71
72
#define TEST_STRING "This is a test E-mail"
73
#define HTML_PRE "<html><body><b>"
74
#define HTML_POST "</b></body></html>"
75
76
77
static TnyMsg*
78
create_test_msg (TnyPlatformFactory *platfact)
79
{
80
	TnyMsg *retval = tny_platform_factory_new_msg (platfact);
81
	TnyHeader *header = tny_msg_get_header (retval);
82
83
	TnyStream *plain_stream = tny_camel_mem_stream_new ();
84
	TnyStream *html_stream = tny_camel_mem_stream_new ();
85
	TnyMimePart *plain_body = tny_platform_factory_new_mime_part (platfact);
86
	TnyMimePart *html_body = tny_platform_factory_new_mime_part (platfact);
87
88
	tny_header_set_subject (header, TEST_STRING);
89
	tny_header_set_from (header, "tinymailunittest@mail.tinymail.org");
90
	tny_header_set_to (header, "spam@pvanhoof.be");
91
92
	g_object_unref (G_OBJECT (header));
93
94
	tny_stream_write (plain_stream, TEST_STRING, strlen (TEST_STRING));
95
	tny_stream_reset (plain_stream);
96
97
	tny_stream_write (html_stream, HTML_PRE TEST_STRING HTML_POST, 
98
		strlen (HTML_PRE TEST_STRING HTML_POST));
99
	tny_stream_reset (html_stream);
100
101
	tny_mime_part_construct (plain_body, plain_stream, "text/plain; charset=utf-8", "7bit"); 
102
	tny_mime_part_construct (html_body, html_stream, "text/html; charset=utf-8", "7bit"); 
103
104
	tny_mime_part_add_part (TNY_MIME_PART (retval), html_body);
105
	tny_mime_part_add_part (TNY_MIME_PART (retval), plain_body);
106
107
	g_object_unref (G_OBJECT (plain_stream));
108
	g_object_unref (G_OBJECT (html_stream));
109
110
	g_object_unref (G_OBJECT (plain_body));
111
	g_object_unref (G_OBJECT (html_body));
112
113
	return retval;
114
}
115
116
#if 0
117
static void
118
on_message_sent (TnySendQueue *queue, TnyMsg *msg, guint nth, guint total)
119
{
120
	TnyHeader *header;
121
122
	header = tny_msg_get_header (msg);
123
	g_print ("Message \"%s\" got sent", tny_header_get_subject (header));
124
	g_object_unref (G_OBJECT (header));
125
}
126
#endif
127
128
129
int 
130
main (int argc, char **argv)
131
{
132
	GOptionContext *context;
133
	TnyAccountStore *account_store;
134
	TnyList *accounts;
135
	TnyStoreAccount *account;
136
	TnyIterator *iter;
137
	TnySendQueue *queue;
138
	TnyMsg *msg;
139
	TnyPlatformFactory *platfact;
140
141
	free (malloc (10));
142
143
	g_type_init ();
144
145
	platfact = tny_test_platform_factory_get_instance ();
146
147
	context = g_option_context_new ("- The tinymail functional tester");
148
		g_option_context_add_main_entries (context, options, "tinymail");
149
	g_option_context_parse (context, &argc, &argv, NULL);
150
151
	account_store = tny_test_account_store_new (online, cachedir);
152
153
	if (cachedir)
154
		g_print ("Using %s as cache directory\n", cachedir);
155
156
	g_option_context_free (context);
157
158
	accounts = tny_simple_list_new ();
159
160
	tny_account_store_get_accounts (account_store, accounts, 
161
		TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS);
162
	/*g_object_unref (G_OBJECT (account_store));*/
163
164
	iter = tny_list_create_iterator (accounts);
165
	account = (TnyStoreAccount*) tny_iterator_get_current (iter);
166
167
	msg = create_test_msg (platfact);
168
169
	queue = tny_camel_send_queue_new (TNY_CAMEL_TRANSPORT_ACCOUNT (account));
170
	tny_send_queue_add (queue, msg, NULL);
171
172
	if (mainloop)
173
	{
174
		g_print ("Using the Gtk+ mainloop (will wait 4 seconds in the loop)\n");
175
		g_timeout_add (1, dance, account);	    
176
		g_timeout_add (1000 * 4, time_s_up, NULL);
177
		gtk_main ();
178
	} else {
179
		g_print ("Not using a mainloop (will sleep 4 seconds)\n");
180
		dance (account);
181
		sleep (4);
182
	}
183
184
	g_object_unref (G_OBJECT (account));
185
	g_object_unref (G_OBJECT (iter));
186
	g_object_unref (G_OBJECT (accounts));
187
	g_object_unref (G_OBJECT (platfact));
188
189
	return 0;
190
}