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 <string.h>
21
22
#include <glib.h>
23
#include <stdlib.h>
24
25
#include <tny-list.h>
26
#include <tny-iterator.h>
27
#include <tny-simple-list.h>
28
#include <tny-account-store.h>
29
#include <tny-store-account.h>
30
#include <tny-folder.h>
31
#include <tny-folder-store.h>
32
#include <tny-folder-store-query.h>
33
34
#include <account-store.h>
35
36
static gchar *cachedir=NULL;
37
static gboolean online=FALSE;
38
static gboolean move=FALSE;
39
static const gchar *src_name = NULL;
40
static const gchar *dst_name = NULL;
41
static TnyFolder *src_folder = NULL;
42
static TnyFolder *dst_folder = NULL;
43
static gint recursion_level=0;
44
45
static const GOptionEntry options[] = {
46
		{ "from",  'f', 0, G_OPTION_ARG_STRING, &src_name,
47
		  "Source folder", NULL},
48
		{ "to", 't', 0, G_OPTION_ARG_STRING, &dst_name,
49
		  "Destination folder", NULL},
50
		{ "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
51
		  "Cache directory", NULL },
52
		{ "online", 'o', 0, G_OPTION_ARG_NONE, &online,
53
		  "Online or offline", NULL },
54
		{ "move", 'm', 0, G_OPTION_ARG_NONE, &move,
55
		  "Move the messages instead of copy them", NULL },
56
		{ NULL }
57
};
58
59
static void
60
recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query)
61
{
62
	TnyIterator *iter;
63
	TnyList *folders = tny_simple_list_new ();
64
65
	tny_folder_store_get_folders (store, folders, query, TRUE, NULL);
66
	iter = tny_list_create_iterator (folders);
67
68
	while (!tny_iterator_is_done (iter))
69
	{
70
		TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
71
		gint i=0;
72
		const gchar *folder_name = NULL;
73
74
		for (i=0; i<recursion_level; i++)
75
			g_print ("\t");
76
77
		folder_name = tny_folder_get_name (TNY_FOLDER (folder));
78
		g_print ("%s\n", folder_name);
79
80
		if (!strcmp (folder_name, src_name))
81
			src_folder = g_object_ref (folder);
82
		
83
		if (!strcmp (folder_name, dst_name))
84
			dst_folder = g_object_ref (folder);
85
86
		recursion_level++;
87
		recurse_folders (folder, query);
88
		recursion_level--;
89
	    
90
 		g_object_unref (folder);
91
92
		tny_iterator_next (iter);
93
	}
94
95
	 g_object_unref (iter);
96
	 g_object_unref (folders);
97
}
98
99
int 
100
main (int argc, char **argv)
101
{
102
	GOptionContext *context;
103
	TnyAccountStore *account_store;
104
	TnyList *accounts;
105
	TnyStoreAccount *account;
106
	TnyIterator *iter;
107
	gint i;
108
	GError *err = NULL;
109
110
	free (malloc (10));
111
112
	g_type_init ();
113
114
	context = g_option_context_new ("- The tinymail functional tester");
115
	g_option_context_add_main_entries (context, options, "tinymail");
116
	g_option_context_parse (context, &argc, &argv, NULL);
117
118
	account_store = tny_test_account_store_new (online, cachedir);
119
120
	if (cachedir)
121
		g_print ("Using %s as cache directory\n", cachedir);
122
123
	g_option_context_free (context);
124
	accounts = tny_simple_list_new ();
125
126
	tny_account_store_get_accounts (account_store, accounts, 
127
		TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
128
	g_object_unref (G_OBJECT (account_store));
129
	iter = tny_list_create_iterator (accounts);
130
	account = (TnyStoreAccount*) tny_iterator_get_current (iter);
131
132
	recursion_level = 0;
133
	for (i=0; i<1; i++) 
134
		recurse_folders (TNY_FOLDER_STORE (account), NULL);
135
136
	/* Test 1 */
137
	if (!(src_folder && dst_folder)) {
138
		g_message ("Folders not found");
139
		goto cleanup;
140
	}
141
142
	g_print ("%s folder %s to %s\n",
143
		 (move) ? "Moving" : "Copying",
144
		 tny_folder_get_name (src_folder),
145
		 tny_folder_get_name (dst_folder));
146
147
	tny_folder_copy (src_folder,
148
			 TNY_FOLDER_STORE (dst_folder),
149
			 tny_folder_get_name (src_folder),
150
			 move,
151
			 &err);
152
153
	if (err)
154
		g_warning ("%s", err->message);
155
156
	for (i=0; i<1; i++) 
157
		recurse_folders (TNY_FOLDER_STORE (account), NULL);
158
159
 cleanup:
160
	g_object_unref (account);
161
	g_object_unref (iter);
162
	g_object_unref (accounts);
163
164
	return 0;
165
}