d868806b4e4a69c26a9bd42203885566eafbcc90
[modest] / tests / update-account.c
1 /* Copyright (c) 2006, Nokia Corporation
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  *   notice, this list of conditions and the following disclaimer in the
12  *   documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Nokia Corporation nor the names of its
14  *   contributors may be used to endorse or promote products derived from
15  *   this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
18  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <glib.h>
31 #include <string.h>
32
33 #include <tny-list.h>
34 #include <tny-iterator.h>
35 #include <tny-simple-list.h>
36 #include <tny-account-store.h>
37 #include <tny-store-account.h>
38 #include <tny-folder.h>
39 #include <tny-folder-store.h>
40
41 #include "modest-tny-platform-factory.h"
42 #include "modest-account-mgr.h"
43 #include "modest-mail-operation.h"
44 #include "modest-mail-operation-queue.h"
45
46 GMainLoop *main_loop;
47
48 static void
49 on_progress_changed (ModestMailOperation *mail_op, gpointer user_data)
50 {
51         ModestMailOperationStatus status;
52         ModestMailOperationQueue *queue = NULL;
53
54         g_print ("Refreshed %d of %d\n", 
55                  modest_mail_operation_get_task_done  (mail_op), 
56                  modest_mail_operation_get_task_total (mail_op));
57
58         if (modest_mail_operation_is_finished (mail_op)) {
59                 queue = MODEST_MAIL_OPERATION_QUEUE (user_data);
60                 modest_mail_operation_queue_remove (queue, mail_op);
61                 g_main_loop_quit (main_loop);
62         }
63 }
64
65 static gboolean
66 func (gpointer_data) 
67 {
68         TnyStoreAccount *account;
69         TnyIterator *iter;
70         TnyPlatformFactory *fact = NULL;
71         ModestAccountMgr *acc_mgr = NULL;
72         ModestMailOperation *mail_op = NULL;
73         ModestMailOperationQueue *queue = NULL;
74         TnyAccountStore *account_store = NULL;
75         TnyList *accounts;
76
77         fact = TNY_PLATFORM_FACTORY (modest_tny_platform_factory_get_instance ());
78         acc_mgr = MODEST_ACCOUNT_MGR (modest_tny_platform_factory_get_modest_account_mgr_instance (fact));
79         account_store = tny_platform_factory_new_account_store (fact);  
80
81         /* Get accounts */
82         accounts = tny_simple_list_new ();
83
84         tny_account_store_get_accounts (account_store, accounts,
85                                         TNY_ACCOUNT_STORE_STORE_ACCOUNTS);
86     
87         iter = tny_list_create_iterator (accounts);
88         account = TNY_STORE_ACCOUNT (tny_iterator_get_current (iter));
89
90         g_object_unref (G_OBJECT (iter));
91         g_object_unref (G_OBJECT (accounts));
92
93         queue = modest_mail_operation_queue_get_instance ();
94         mail_op = modest_mail_operation_new ();
95         
96         g_signal_connect (G_OBJECT (mail_op), "progress_changed", 
97                           G_CALLBACK (on_progress_changed), queue);
98
99         if (modest_mail_operation_update_account (mail_op, account))
100                 modest_mail_operation_queue_add (queue, mail_op);
101
102         g_object_unref (G_OBJECT (mail_op));
103
104         return FALSE;
105 }
106
107 int
108 main (int argc, char **argv)
109 {   
110         guint id;
111
112         g_type_init ();
113         g_thread_init(NULL);
114
115         main_loop = g_main_loop_new (NULL, FALSE);
116         id = g_timeout_add(1000, func, main_loop);
117         g_main_loop_run(main_loop);
118
119         return 0;
120 }