1
/* tinymail - Tiny Mail
2
 * Copyright (C) 2006-2007 Antia Puentes Felpeto <apuentes@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 const gchar *src_name = NULL;
39
static TnyFolder *src_folder = NULL;
40
static gint recursion_level=0;
41
42
43
static const GOptionEntry options[] = {
44
		{ "name", 'n', 0, G_OPTION_ARG_STRING, &src_name,
45
		  "Name folder", NULL},
46
		{ "cachedir", 'c', 0, G_OPTION_ARG_STRING, &cachedir,
47
		  "Cache directory", NULL },
48
		{ "online", 'o', 0, G_OPTION_ARG_NONE, &online,
49
		  "Online or offline", NULL },
50
		{ NULL }
51
};
52
53
static void
54
recurse_folders (TnyFolderStore *store, TnyFolderStoreQuery *query)
55
{
56
	TnyIterator *iter;
57
	TnyList *folders = tny_simple_list_new ();
58
59
	tny_folder_store_get_folders (store, folders, query, TRUE, NULL);
60
	iter = tny_list_create_iterator (folders);
61
62
	while (!tny_iterator_is_done (iter))
63
	{
64
		TnyFolderStore *folder = (TnyFolderStore*) tny_iterator_get_current (iter);
65
		gint i=0;
66
		const gchar *folder_name = NULL;
67
68
		for (i=0; i<recursion_level; i++)
69
			g_print ("\t");
70
71
		folder_name = tny_folder_get_name (TNY_FOLDER (folder));
72
		g_print ("%s\n", folder_name);
73
74
		if (!strcmp (folder_name, src_name))
75
			src_folder = g_object_ref (folder);
76
77
		recursion_level++;
78
		recurse_folders (folder, query);
79
		recursion_level--;
80
	    
81
 		g_object_unref (G_OBJECT (folder));
82
83
		tny_iterator_next (iter);
84
	}
85
86
	 g_object_unref (G_OBJECT (iter));
87
	 g_object_unref (G_OBJECT (folders));
88
}
89
90
int 
91
main (int argc, char **argv)
92
{
93
	GOptionContext *context;
94
	TnyAccountStore *account_store;
95
	TnyList *accounts;
96
	TnyStoreAccount *account;
97
	TnyIterator *iter;
98
	gint i;
99
	GError *err = NULL;
100
101
	free (malloc (10));
102
103
	g_type_init ();
104
105
	context = g_option_context_new ("- The tinymail functional tester");
106
	g_option_context_add_main_entries (context, options, "tinymail");
107
	g_option_context_parse (context, &argc, &argv, NULL);
108
109
	account_store = tny_test_account_store_new (online, cachedir);
110
111
	if (cachedir)
112
		g_print ("Using %s as cache directory\n", cachedir);
113
114
	g_option_context_free (context);
115
	accounts = tny_simple_list_new ();
116
117
	tny_account_store_get_accounts (account_store, accounts, 
118
		TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
119
	g_object_unref (G_OBJECT (account_store));
120
	iter = tny_list_create_iterator (accounts);
121
	account = (TnyStoreAccount*) tny_iterator_get_current (iter);
122
123
	recursion_level = 0;
124
	for (i=0; i<1; i++) 
125
		recurse_folders (TNY_FOLDER_STORE (account), NULL);
126
127
   
128
	if (!(src_folder)) {
129
		g_message ("Folder not found");
130
		goto cleanup;
131
	}
132
133
	g_print ("Remove folder %s\n",
134
		 tny_folder_get_name (src_folder));
135
136
	TnyFolderStore *folder_store = tny_folder_get_folder_store (src_folder);
137
       
138
	if (!(folder_store)) {
139
		g_message ("Folderstore not found");
140
		goto cleanup;
141
	}
142
	
143
	tny_folder_store_remove_folder (folder_store, src_folder, &err);
144
        g_object_unref (G_OBJECT (src_folder));
145
146
	if (err)
147
		g_warning ("%s", err->message);
148
149
	for (i=0; i<1; i++) 
150
		recurse_folders (TNY_FOLDER_STORE (account), NULL);
151
152
       
153
 cleanup:
154
	g_object_unref (G_OBJECT (account));
155
	g_object_unref (G_OBJECT (iter));
156
	g_object_unref (G_OBJECT (accounts));
157
	return 0;
158
159
}