1
/* tinymail - Tiny Mail
2
 * Copyright (C) 2006-2007 Sergio Villar Senin <svillar@igalia.com>
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 <glib.h>
21
22
#include <gtk/gtk.h>
23
24
#include <tny-iterator.h>
25
#include <tny-simple-list.h>
26
#include <tny-account-store.h>
27
#include <tny-store-account.h>
28
#include <tny-folder.h>
29
#include <tny-folder-store.h>
30
#include <tny-folder-store-query.h>
31
32
#include <account-store.h>
33
34
static gchar *cachedir=NULL;
35
static gboolean online=FALSE, mainloop=FALSE;
36
37
static void
38
recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
39
{
40
	TnyIterator *iter;
41
	TnyList *folders = tny_simple_list_new ();
42
43
	tny_folder_store_get_folders (store, folders, query, TRUE, NULL);
44
	iter = tny_list_create_iterator (folders);
45
46
	while (!tny_iterator_is_done (iter)) {
47
48
		TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
49
		tny_list_prepend (all_folders, G_OBJECT (folder));
50
		recurse_folders (folder, query, all_folders);    
51
 		g_object_unref (G_OBJECT (folder));
52
53
		tny_iterator_next (iter);
54
	}
55
	 g_object_unref (iter);
56
	 g_object_unref (folders);
57
}
58
59
static gboolean
60
notify_update_account_observers (gpointer data)
61
{
62
	g_print ("Refresing %s\n", tny_folder_get_name(TNY_FOLDER(data)));
63
	g_object_unref (data);
64
65
	return FALSE;
66
}
67
68
static gboolean
69
update_account_quit (gpointer data)
70
{
71
	gtk_main_quit ();
72
73
	return FALSE;
74
}
75
76
static gpointer
77
update_account_thread (gpointer thr_user_data)
78
{
79
	TnyList *all_folders = NULL;
80
	TnyIterator *iter = NULL;
81
	TnyStoreAccount *account;
82
	GError *error = NULL;
83
84
	account = (TnyStoreAccount *) (thr_user_data);
85
86
	all_folders = tny_simple_list_new ();
87
	tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
88
				      all_folders,
89
				      NULL,
90
				      TRUE,
91
				      &error);
92
	if (error)
93
		goto out;
94
95
	iter = tny_list_create_iterator (all_folders);
96
	while (!tny_iterator_is_done (iter)) {
97
		TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
98
99
		recurse_folders (folder, NULL, all_folders);
100
		tny_iterator_next (iter);
101
	}
102
	g_object_unref (iter);
103
104
	/* Refresh folders */
105
	iter = tny_list_create_iterator (all_folders);
106
	while (!tny_iterator_is_done (iter)) {
107
108
		TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));
109
110
		/* Refresh the folder */
111
		tny_folder_refresh (TNY_FOLDER (folder), &error);
112
/* 		sleep (1); */
113
114
		if (!error) {
115
			if (mainloop)
116
				g_idle_add (notify_update_account_observers, g_object_ref (folder));
117
			else
118
				notify_update_account_observers (folder);
119
		} else {
120
			g_print ("%s %s\n", __FUNCTION__, error->message);
121
			g_clear_error (&error);
122
		}
123
		g_object_unref (folder);
124
		tny_iterator_next (iter);
125
	}
126
	g_object_unref (iter);
127
128
 out:
129
	/* Frees */
130
	g_object_unref (all_folders);
131
	g_object_unref (account);
132
133
	if (mainloop)
134
		g_timeout_add (1, update_account_quit, NULL);
135
136
	return NULL;
137
}
138
139
static gboolean
140
update_account (gpointer data)
141
{
142
	GThread *thread;
143
144
	thread = g_thread_create (update_account_thread, g_object_ref (data), FALSE, NULL);
145
146
	return FALSE;
147
}
148
149
static const GOptionEntry options[] = 
150
{
151
	{ "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
152
		"Cache directory", NULL },
153
	{ "online", 'o', 0, G_OPTION_ARG_NONE, &online,
154
		"Online or offline", NULL },
155
	{ "mainloop", 'm', 0, G_OPTION_ARG_NONE, &mainloop,
156
		"Use the Gtk+ mainloop", NULL },
157
    
158
	{ NULL }
159
};
160
161
int main (int argc, char **argv)
162
{
163
	GOptionContext *context;
164
	TnyAccountStore *account_store;
165
	TnyList *accounts;
166
	TnyStoreAccount *account;
167
	TnyIterator *iter;
168
169
	g_type_init ();
170
171
	if (mainloop)
172
		gtk_init (&argc, &argv);
173
174
	context = g_option_context_new ("- The tinymail functional tester");
175
	g_option_context_add_main_entries (context, options, "tinymail");
176
	g_option_context_parse (context, &argc, &argv, NULL);
177
178
	account_store = tny_test_account_store_new (online, cachedir);
179
180
	if (cachedir)
181
		g_print ("Using %s as cache directory\n", cachedir);
182
183
	g_option_context_free (context);
184
	accounts = tny_simple_list_new ();
185
186
	tny_account_store_get_accounts (account_store, accounts,
187
		TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
188
	g_object_unref (G_OBJECT (account_store));
189
	iter = tny_list_create_iterator (accounts);
190
	account = (TnyStoreAccount*) tny_iterator_get_current (iter);
191
192
	if (mainloop) {
193
194
		g_print ("Using the Gtk+ mainloop\n");
195
		g_timeout_add (1, update_account, account);
196
197
		gtk_main ();
198
	} else {
199
		g_print ("Not using a mainloop (will sleep 20 secs)\n");
200
		update_account (account);
201
		sleep (20);
202
	}
203
204
	g_object_unref (G_OBJECT (account));
205
	g_object_unref (G_OBJECT (iter));
206
	g_object_unref (G_OBJECT (accounts));
207
208
	return 0;
209
}