Query activesync for addressbooks too (if available).
[modest] / src / modest-default-connection-policy.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 <config.h>
31 #include <glib.h>
32
33 #include "modest-default-connection-policy.h"
34 #include "modest-account-mgr.h"
35 #include "modest-account-mgr-helpers.h"
36 #include "modest-runtime.h"
37 #include "modest-tny-account.h"
38 #include "modest-ui-actions.h"
39 #include <tny-account.h>
40 #include <tny-store-account.h>
41
42 static GObjectClass *parent_class = NULL;
43
44 static void
45 modest_default_connection_policy_set_current (TnyConnectionPolicy *self, TnyAccount *account, TnyFolder *folder)
46 {
47         return;
48 }
49
50 static void
51 modest_default_connection_policy_on_connect (TnyConnectionPolicy *self, TnyAccount *account)
52 {
53         /* Set the username as succedded */
54         if (TNY_IS_STORE_ACCOUNT (account)) {
55                 gboolean first_time = FALSE;
56                 const gchar *id;
57                 ModestAccountMgr *acc_mgr;
58
59                 acc_mgr = modest_runtime_get_account_mgr ();
60                 id = tny_account_get_id (account);
61                 first_time = !modest_account_mgr_get_server_account_username_has_succeeded (acc_mgr, id);
62
63                 /* If it's the first connection then perform a send&receive */
64                 if (first_time) {
65                         const gchar *account_name;
66                         ModestWindow *top_window;
67
68                         modest_account_mgr_set_server_account_username_has_succeeded (acc_mgr, id, TRUE);
69
70                         /* Perform a send receive */
71                         account_name = modest_tny_account_get_parent_modest_account_name_for_server_account (account);
72                         top_window = modest_window_mgr_get_current_top (modest_runtime_get_window_mgr ());
73                         if (top_window)
74                                 modest_ui_actions_do_send_receive (account_name, FALSE, FALSE, TRUE, top_window);
75                 }
76         }
77
78         /* Clear the pwd */
79         modest_tny_account_store_forget_password_in_memory (modest_runtime_get_account_store (), tny_account_get_id (account));
80
81         return;
82 }
83
84 static void
85 modest_default_connection_policy_on_connection_broken (TnyConnectionPolicy *self, TnyAccount *account)
86 {
87         return;
88 }
89
90 static void
91 modest_default_connection_policy_on_disconnect (TnyConnectionPolicy *self, TnyAccount *account)
92 {
93         /* Reset the pwd */
94         modest_tny_account_store_forget_password_in_memory (modest_runtime_get_account_store (),
95                                                             tny_account_get_id (account));
96 }
97
98 static void
99 modest_default_connection_policy_finalize (GObject *object)
100 {
101         parent_class->finalize (object);
102 }
103
104 static void
105 modest_default_connection_policy_instance_init (GTypeInstance *instance, gpointer g_class)
106 {
107 }
108
109 static void
110 tny_connection_policy_init (TnyConnectionPolicyIface *klass)
111 {
112         klass->on_connect= modest_default_connection_policy_on_connect;
113         klass->on_connection_broken= modest_default_connection_policy_on_connection_broken;
114         klass->on_disconnect= modest_default_connection_policy_on_disconnect;
115         klass->set_current= modest_default_connection_policy_set_current;
116 }
117
118 static void
119 modest_default_connection_policy_class_init (ModestDefaultConnectionPolicyClass *klass)
120 {
121         GObjectClass *object_class;
122
123         parent_class = g_type_class_peek_parent (klass);
124         object_class = (GObjectClass*) klass;
125         object_class->finalize = modest_default_connection_policy_finalize;
126 }
127
128
129
130 /**
131  * modest_default_connection_policy_new:
132  * 
133  * A connection policy
134  *
135  * Return value: A new #TnyConnectionPolicy instance 
136  **/
137 TnyConnectionPolicy*
138 modest_default_connection_policy_new (void)
139 {
140         return TNY_CONNECTION_POLICY (g_object_new (MODEST_TYPE_DEFAULT_CONNECTION_POLICY, NULL));
141 }
142
143 GType
144 modest_default_connection_policy_get_type (void)
145 {
146         static GType type = 0;
147         if (G_UNLIKELY(type == 0))
148         {
149                 static const GTypeInfo info = 
150                 {
151                         sizeof (ModestDefaultConnectionPolicyClass),
152                         NULL,   /* base_init */
153                         NULL,   /* base_finalize */
154                         (GClassInitFunc) modest_default_connection_policy_class_init,   /* class_init */
155                         NULL,   /* class_finalize */
156                         NULL,   /* class_data */
157                         sizeof (ModestDefaultConnectionPolicy),
158                         0,      /* n_preallocs */
159                         modest_default_connection_policy_instance_init,    /* instance_init */
160                         NULL
161                 };
162
163
164                 static const GInterfaceInfo tny_connection_policy_info = 
165                 {
166                         (GInterfaceInitFunc) tny_connection_policy_init, /* interface_init */
167                         NULL,         /* interface_finalize */
168                         NULL          /* interface_data */
169                 };
170
171                 type = g_type_register_static (G_TYPE_OBJECT,
172                         "ModestDefaultConnectionPolicy",
173                         &info, 0);
174
175                 g_type_add_interface_static (type, TNY_TYPE_CONNECTION_POLICY,
176                         &tny_connection_policy_info);
177
178         }
179         return type;
180 }