File: account-refresh.c

package info (click to toggle)
libtinymail 0.0.9-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,148 kB
  • ctags: 19,175
  • sloc: ansic: 151,565; xml: 20,145; sh: 9,245; makefile: 2,394; cs: 243; cpp: 141; python: 93; perl: 71
file content (209 lines) | stat: -rw-r--r-- 5,300 bytes parent folder | download
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
/* tinymail - Tiny Mail
 * Copyright (C) 2006-2007 Sergio Villar Senin <svillar@igalia.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with self program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <glib.h>

#include <gtk/gtk.h>

#include <gtk/gtkwindow.h>
#include <gtk/gtkprogressbar.h>
#include <tny-iterator.h>
#include <tny-simple-list.h>
#include <tny-account-store.h>
#include <tny-store-account.h>
#include <tny-folder.h>
#include <tny-folder-store.h>
#include <tny-folder-store-query.h>

#include <account-store.h>

static gchar *cachedir=NULL;
static gboolean online=FALSE, mainloop=FALSE;

static void
recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query, TnyList *all_folders)
{
	TnyIterator *iter;
	TnyList *folders = tny_simple_list_new ();

	tny_folder_store_get_folders (store, folders, query, NULL);
	iter = tny_list_create_iterator (folders);

	while (!tny_iterator_is_done (iter)) {

		TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
		tny_list_prepend (all_folders, G_OBJECT (folder));
		recurse_folders (folder, query, all_folders);    
 		g_object_unref (G_OBJECT (folder));

		tny_iterator_next (iter);
	}
	 g_object_unref (iter);
	 g_object_unref (folders);
}

static gboolean
notify_update_account_observers (gpointer data)
{
	g_print ("Refresing %s\n", tny_folder_get_name(TNY_FOLDER(data)));
	g_object_unref (data);

	return FALSE;
}

static gboolean
update_account_quit (gpointer data)
{
	gtk_main_quit ();

	return FALSE;
}

static gpointer
update_account_thread (gpointer thr_user_data)
{
	TnyList *all_folders = NULL;
	TnyIterator *iter = NULL;
	TnyStoreAccount *account;
	GError *error = NULL;

	account = (TnyStoreAccount *) (thr_user_data);

	all_folders = tny_simple_list_new ();
	tny_folder_store_get_folders (TNY_FOLDER_STORE (account),
				      all_folders,
				      NULL,
				      &error);
	if (error)
		goto out;

	iter = tny_list_create_iterator (all_folders);
	while (!tny_iterator_is_done (iter)) {
		TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));

		recurse_folders (folder, NULL, all_folders);
		tny_iterator_next (iter);
	}
	g_object_unref (iter);

	/* Refresh folders */
	iter = tny_list_create_iterator (all_folders);
	while (!tny_iterator_is_done (iter)) {

		TnyFolderStore *folder = TNY_FOLDER_STORE (tny_iterator_get_current (iter));

		/* Refresh the folder */
		tny_folder_refresh (TNY_FOLDER (folder), &error);
/* 		sleep (1); */

		if (!error) {
			if (mainloop)
				g_idle_add (notify_update_account_observers, g_object_ref (folder));
			else
				notify_update_account_observers (folder);
		} else {
			g_print ("%s %s\n", __FUNCTION__, error->message);
			g_clear_error (&error);
		}
		g_object_unref (folder);
		tny_iterator_next (iter);
	}
	g_object_unref (iter);

 out:
	/* Frees */
	g_object_unref (all_folders);
	g_object_unref (account);

	if (mainloop)
		g_timeout_add (1, update_account_quit, NULL);

	return NULL;
}

static gboolean
update_account (gpointer data)
{
	GThread *thread;

	thread = g_thread_create (update_account_thread, g_object_ref (data), FALSE, NULL);

	return FALSE;
}

static const GOptionEntry options[] = 
{
	{ "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
		"Cache directory", NULL },
	{ "online", 'o', 0, G_OPTION_ARG_NONE, &online,
		"Online or offline", NULL },
	{ "mainloop", 'm', 0, G_OPTION_ARG_NONE, &mainloop,
		"Use the Gtk+ mainloop", NULL },
    
	{ NULL }
};

int main (int argc, char **argv)
{
	GOptionContext *context;
	TnyAccountStore *account_store;
	TnyList *accounts;
	TnyStoreAccount *account;
	TnyIterator *iter;

	g_type_init ();

	if (mainloop)
		gtk_init (&argc, &argv);

	context = g_option_context_new ("- The tinymail functional tester");
	g_option_context_add_main_entries (context, options, "tinymail");
	g_option_context_parse (context, &argc, &argv, NULL);

	account_store = tny_test_account_store_new (online, cachedir);

	if (cachedir)
		g_print ("Using %s as cache directory\n", cachedir);

	g_option_context_free (context);
	accounts = tny_simple_list_new ();

	tny_account_store_get_accounts (account_store, accounts,
		TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
	g_object_unref (G_OBJECT (account_store));
	iter = tny_list_create_iterator (accounts);
	account = (TnyStoreAccount*) tny_iterator_get_current (iter);

	if (mainloop) {

		g_print ("Using the Gtk+ mainloop\n");
		g_timeout_add (1, update_account, account);

		gtk_main ();
	} else {
		g_print ("Not using a mainloop (will sleep 20 secs)\n");
		update_account (account);
		sleep (20);
	}

	g_object_unref (G_OBJECT (account));
	g_object_unref (G_OBJECT (iter));
	g_object_unref (G_OBJECT (accounts));

	return 0;
}