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 <glib.h>
21
22
#include <string.h>
23
24
#include <tny-list.h>
25
#include <tny-iterator.h>
26
#include <tny-simple-list.h>
27
#include <tny-account-store.h>
28
#include <tny-store-account.h>
29
#include <tny-folder.h>
30
#include <tny-folder-store.h>
31
#include <tny-folder-store-query.h>
32
33
#include <account-store.h>
34
35
static gchar *cachedir=NULL;
36
static gboolean online=FALSE;
37
static gboolean move=FALSE;
38
static const gchar *src_name = NULL;
39
static const gchar *dst_name = NULL;
40
41
static void
42
find_folders (TnyFolderStore *store, TnyFolderStoreQuery *query,
43
	      TnyFolder **folder_src, TnyFolder **folder_dst)
44
{
45
	TnyIterator *iter;
46
	TnyList *folders;
47
48
	if ((*folder_src != NULL) && (*folder_dst != NULL))
49
		return;
50
51
	folders = tny_simple_list_new ();
52
	tny_folder_store_get_folders (store, folders, query, TRUE, NULL);
53
	iter = tny_list_create_iterator (folders);
54
55
	while (!tny_iterator_is_done (iter) && (!*folder_src || !*folder_dst))
56
	{
57
		TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
58
		const gchar *folder_name = NULL;
59
60
		folder_name = tny_folder_get_name (TNY_FOLDER (folder));
61
62
		if (!strcmp (folder_name, src_name))
63
		    *folder_src = g_object_ref (folder);
64
65
		if (!strcmp (folder_name, dst_name))
66
		    *folder_dst = g_object_ref (folder);
67
68
		find_folders (folder, query, folder_src, folder_dst);
69
	    
70
 		g_object_unref (G_OBJECT (folder));
71
72
		tny_iterator_next (iter);
73
	}
74
75
	 g_object_unref (G_OBJECT (iter));
76
	 g_object_unref (G_OBJECT (folders));
77
}
78
79
static const GOptionEntry options[] = {
80
		{ "from",  'f', 0, G_OPTION_ARG_STRING, &src_name,
81
		  "Source folder", NULL},
82
		{ "to", 't', 0, G_OPTION_ARG_STRING, &dst_name,
83
		  "Destination folder", NULL},
84
		{ "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
85
		  "Cache directory", NULL },
86
		{ "online", 'o', 0, G_OPTION_ARG_NONE, &online,
87
		  "Online or offline", NULL },
88
		{ "move", 'm', 0, G_OPTION_ARG_NONE, &move,
89
		  "Move the messages instead of copy them", NULL },
90
		{ NULL }
91
};
92
93
int 
94
main (int argc, char **argv)
95
{
96
	GOptionContext *context;
97
	TnyAccountStore *account_store;
98
	TnyList *accounts, *src_headers;
99
	TnyFolderStoreQuery *query;
100
	TnyStoreAccount *account;
101
	TnyIterator *iter;
102
	TnyFolder *folder_src = NULL, *folder_dst = NULL;
103
	guint src_num_headers = 0, dst_num_headers = 0;
104
	GError *err;
105
    
106
	g_type_init ();
107
108
    	context = g_option_context_new ("- The tinymail functional tester");
109
	g_option_context_add_main_entries (context, options, "tinymail");
110
	if (!g_option_context_parse (context, &argc, &argv, &err)) {
111
		g_printerr ("Error in command line parameter(s): '%s', exiting\n",
112
			    err ? err->message : "");
113
		return 1;
114
	}
115
	g_option_context_free (context);
116
117
	account_store = tny_test_account_store_new (online, cachedir);
118
119
	if (cachedir)
120
		g_print ("Using %s as cache directory\n", cachedir);
121
122
	if (!src_name || !dst_name) {
123
		g_printerr ("Error in command line parameter(s), specify source and target folders\n");	
124
		return 1;
125
	}
126
127
	/* Get accounts */    
128
	accounts = tny_simple_list_new ();
129
130
	tny_account_store_get_accounts (account_store, accounts, 
131
	      TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
132
	g_object_unref (G_OBJECT (account_store));
133
    
134
	iter = tny_list_create_iterator (accounts);
135
	account = (TnyStoreAccount*) tny_iterator_get_current (iter);
136
137
	g_object_unref (G_OBJECT (iter));
138
	g_object_unref (G_OBJECT (accounts));
139
140
	query = tny_folder_store_query_new ();
141
	tny_folder_store_query_add_item (query, NULL, TNY_FOLDER_STORE_QUERY_OPTION_SUBSCRIBED);
142
143
	/* Find the two folders */
144
	find_folders (TNY_FOLDER_STORE (account), query, 
145
		      &folder_src, &folder_dst);
146
147
	if (!folder_src || !folder_dst)
148
		goto cleanup;
149
150
	/* Refresh folders */
151
	tny_folder_refresh (folder_src, NULL);
152
	src_num_headers = tny_folder_get_all_count (folder_src);
153
154
	tny_folder_refresh (folder_dst, NULL);
155
	dst_num_headers = tny_folder_get_all_count (folder_dst);
156
157
	/* Get all the headers of the source & target folder */
158
	src_headers = tny_simple_list_new ();
159
	tny_folder_get_headers (folder_src, src_headers, TRUE, NULL);
160
		
161
	g_print ("%s %d messages from %s to %s\n",
162
		  move ? "Moving" : "Copying",
163
		  src_num_headers,
164
		  tny_folder_get_name (folder_src),
165
		  tny_folder_get_name (folder_dst));
166
167
	/* Copy/move messages */
168
	tny_folder_transfer_msgs (folder_src, src_headers, folder_dst, move, NULL);
169
170
	/* Check that all the messages have been transferred */
171
	tny_folder_refresh (folder_dst, NULL);
172
	g_print ("Transferred %d of %d messages\n",
173
		  tny_folder_get_all_count (folder_dst) - dst_num_headers,
174
		  src_num_headers);
175
	
176
	g_object_unref (G_OBJECT (src_headers));
177
178
 cleanup:
179
	g_object_unref (account);
180
	g_object_unref (query);
181
    
182
	return 0;
183
}