| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
#if HAVE_CONFIG_H |
|---|
| 21 |
#include "config.h" |
|---|
| 22 |
#endif |
|---|
| 23 |
|
|---|
| 24 |
#include <string.h> |
|---|
| 25 |
#include <glib.h> |
|---|
| 26 |
#include <gtk/gtk.h> |
|---|
| 27 |
#include <glib/gi18n-lib.h> |
|---|
| 28 |
#include <glib/gstdio.h> |
|---|
| 29 |
#include <errno.h> |
|---|
| 30 |
#include <stdlib.h> |
|---|
| 31 |
|
|---|
| 32 |
#include "tmut-account-store.h" |
|---|
| 33 |
#include "tmut-platform-factory.h" |
|---|
| 34 |
|
|---|
| 35 |
#include <tny-gtk-lockable.h> |
|---|
| 36 |
#include <tny-platform-factory.h> |
|---|
| 37 |
#include <tny-password-getter.h> |
|---|
| 38 |
#include <tny-account-store.h> |
|---|
| 39 |
#include <tny-account.h> |
|---|
| 40 |
#include <tny-store-account.h> |
|---|
| 41 |
#include <tny-transport-account.h> |
|---|
| 42 |
#include <tny-device.h> |
|---|
| 43 |
|
|---|
| 44 |
#include <tny-camel-account.h> |
|---|
| 45 |
#include <tny-camel-store-account.h> |
|---|
| 46 |
#include <tny-camel-transport-account.h> |
|---|
| 47 |
#include <tny-session-camel.h> |
|---|
| 48 |
|
|---|
| 49 |
static GObjectClass *parent_class = NULL; |
|---|
| 50 |
|
|---|
| 51 |
typedef enum { |
|---|
| 52 |
TMUT_ACCOUNT_STORE_ACCOUNT_DELETED, |
|---|
| 53 |
TMUT_ACCOUNT_STORE_ACCOUNT_CREATED, |
|---|
| 54 |
TMUT_ACCOUNT_STORE_ACCOUNT_EDITED, |
|---|
| 55 |
TMUT_ACCOUNT_STORE_LAST_SIGNAL |
|---|
| 56 |
} TMutAccountStoreSignal; |
|---|
| 57 |
|
|---|
| 58 |
static guint tmut_account_store_signals [TMUT_ACCOUNT_STORE_LAST_SIGNAL]; |
|---|
| 59 |
|
|---|
| 60 |
typedef struct _TMutAccountStorePriv TMutAccountStorePriv; |
|---|
| 61 |
|
|---|
| 62 |
struct _TMutAccountStorePriv |
|---|
| 63 |
{ |
|---|
| 64 |
gchar *cache_dir; |
|---|
| 65 |
TnySessionCamel *session; |
|---|
| 66 |
TnyDevice *device; |
|---|
| 67 |
guint notify; |
|---|
| 68 |
GList *accounts; |
|---|
| 69 |
}; |
|---|
| 70 |
|
|---|
| 71 |
#define TMUT_ACCOUNT_STORE_GET_PRIVATE(o) \ |
|---|
| 72 |
(G_TYPE_INSTANCE_GET_PRIVATE ((o), TMUT_TYPE_ACCOUNT_STORE, TMutAccountStorePriv)) |
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 |
static gchar* |
|---|
| 76 |
per_account_get_pass(TnyAccount *account, const gchar *prompt, gboolean *cancel) |
|---|
| 77 |
{ |
|---|
| 78 |
TnyPlatformFactory *platfact = tmut_platform_factory_get_instance (); |
|---|
| 79 |
TnyPasswordGetter *pwdgetter; |
|---|
| 80 |
gchar *retval; |
|---|
| 81 |
|
|---|
| 82 |
pwdgetter = tny_platform_factory_new_password_getter (platfact); |
|---|
| 83 |
retval = (gchar*) tny_password_getter_get_password (pwdgetter, |
|---|
| 84 |
tny_account_get_id (account), prompt, cancel); |
|---|
| 85 |
g_object_unref (pwdgetter); |
|---|
| 86 |
|
|---|
| 87 |
return retval; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 |
static void |
|---|
| 92 |
per_account_forget_pass(TnyAccount *account) |
|---|
| 93 |
{ |
|---|
| 94 |
TnyPlatformFactory *platfact = tmut_platform_factory_get_instance (); |
|---|
| 95 |
TnyPasswordGetter *pwdgetter; |
|---|
| 96 |
|
|---|
| 97 |
pwdgetter = tny_platform_factory_new_password_getter (platfact); |
|---|
| 98 |
tny_password_getter_forget_password (pwdgetter, tny_account_get_id (account)); |
|---|
| 99 |
g_object_unref (pwdgetter); |
|---|
| 100 |
|
|---|
| 101 |
return; |
|---|
| 102 |
} |
|---|
| 103 |
|
|---|
| 104 |
static TnyAccount* |
|---|
| 105 |
create_account_instance (TMutAccountStorePriv *priv, const gchar *type, const gchar *proto, const gchar *mech, const gchar *name, gchar **options, const gchar *user, const gchar *hostname, gint port, const gchar *url_string, const gchar *fullfilen, const gchar *from) |
|---|
| 106 |
{ |
|---|
| 107 |
TnyAccount *account = NULL; |
|---|
| 108 |
|
|---|
| 109 |
if (!g_ascii_strncasecmp (proto, "smtp", 4)) |
|---|
| 110 |
account = TNY_ACCOUNT (tny_camel_transport_account_new ()); |
|---|
| 111 |
else if (!g_ascii_strncasecmp (proto, "imap", 4)) |
|---|
| 112 |
account = TNY_ACCOUNT (tny_camel_imap_store_account_new ()); |
|---|
| 113 |
else if (!g_ascii_strncasecmp (proto, "nntp", 4)) |
|---|
| 114 |
account = TNY_ACCOUNT (tny_camel_nntp_store_account_new ()); |
|---|
| 115 |
else if (!g_ascii_strncasecmp (proto, "pop", 3)) |
|---|
| 116 |
account = TNY_ACCOUNT (tny_camel_pop_store_account_new ()); |
|---|
| 117 |
else |
|---|
| 118 |
account = TNY_ACCOUNT (tny_camel_store_account_new ()); |
|---|
| 119 |
|
|---|
| 120 |
if (account) { |
|---|
| 121 |
gint i; |
|---|
| 122 |
|
|---|
| 123 |
tny_camel_account_set_session (TNY_CAMEL_ACCOUNT (account), priv->session); |
|---|
| 124 |
tny_account_set_proto (account, proto); |
|---|
| 125 |
|
|---|
| 126 |
if (name) |
|---|
| 127 |
tny_account_set_name (account, name); |
|---|
| 128 |
if (mech) |
|---|
| 129 |
tny_account_set_secure_auth_mech (account, mech); |
|---|
| 130 |
|
|---|
| 131 |
if (options) { |
|---|
| 132 |
gint i = 0; |
|---|
| 133 |
while (options[i] != NULL) { |
|---|
| 134 |
gchar *str = g_strdup (options [i]); |
|---|
| 135 |
gchar *key = str; |
|---|
| 136 |
gchar *value = strchr (str, '='); |
|---|
| 137 |
|
|---|
| 138 |
if (value) { |
|---|
| 139 |
*value = '\0'; |
|---|
| 140 |
value++; |
|---|
| 141 |
} else |
|---|
| 142 |
value = ""; |
|---|
| 143 |
|
|---|
| 144 |
tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (account), |
|---|
| 145 |
tny_pair_new (key, value)); |
|---|
| 146 |
i++; |
|---|
| 147 |
g_free (str); |
|---|
| 148 |
} |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
if (!g_ascii_strncasecmp (proto, "pop", 3) || |
|---|
| 152 |
!g_ascii_strncasecmp (proto, "imap", 4) || |
|---|
| 153 |
!g_ascii_strncasecmp (proto, "smtp", 4)) { |
|---|
| 154 |
tny_account_set_user (account, user); |
|---|
| 155 |
tny_account_set_hostname (account, hostname); |
|---|
| 156 |
if (port != -1) |
|---|
| 157 |
tny_account_set_port (account, port); |
|---|
| 158 |
} |
|---|
| 159 |
|
|---|
| 160 |
tny_account_set_id (account, fullfilen); |
|---|
| 161 |
|
|---|
| 162 |
tny_account_set_forget_pass_func (TNY_ACCOUNT (account), |
|---|
| 163 |
per_account_forget_pass); |
|---|
| 164 |
tny_account_set_pass_func (TNY_ACCOUNT (account), |
|---|
| 165 |
per_account_get_pass); |
|---|
| 166 |
|
|---|
| 167 |
} |
|---|
| 168 |
|
|---|
| 169 |
if (TNY_IS_CAMEL_TRANSPORT_ACCOUNT (account)) |
|---|
| 170 |
tny_camel_transport_account_set_from (TNY_CAMEL_TRANSPORT_ACCOUNT (account), from); |
|---|
| 171 |
|
|---|
| 172 |
return account; |
|---|
| 173 |
} |
|---|
| 174 |
|
|---|
| 175 |
void |
|---|
| 176 |
tmut_account_store_create_account (TMutAccountStore *self, |
|---|
| 177 |
gboolean enabled, |
|---|
| 178 |
const gchar *name, |
|---|
| 179 |
const gchar *hostname, |
|---|
| 180 |
const gchar *proto, |
|---|
| 181 |
const gchar *type, |
|---|
| 182 |
const gchar *user, |
|---|
| 183 |
const gchar *mech, |
|---|
| 184 |
const gchar *from, |
|---|
| 185 |
gint port, |
|---|
| 186 |
const gchar **options) |
|---|
| 187 |
{ |
|---|
| 188 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 189 |
GList *found; |
|---|
| 190 |
TnyAccount *account = NULL; |
|---|
| 191 |
FILE *file; |
|---|
| 192 |
GKeyFile *keyfile = g_key_file_new (); |
|---|
| 193 |
|
|---|
| 194 |
gchar *filen = g_build_filename (g_get_home_dir(), |
|---|
| 195 |
".tmut", "accounts", name, NULL); |
|---|
| 196 |
|
|---|
| 197 |
gchar *dirn = g_build_filename (g_get_home_dir(), |
|---|
| 198 |
".tmut", "accounts", NULL); |
|---|
| 199 |
|
|---|
| 200 |
if (!g_file_test (dirn, G_FILE_TEST_EXISTS)) |
|---|
| 201 |
g_mkdir_with_parents (dirn, S_IRUSR | S_IWUSR | S_IXUSR); |
|---|
| 202 |
|
|---|
| 203 |
g_free (dirn); |
|---|
| 204 |
|
|---|
| 205 |
if (g_file_test (filen, G_FILE_TEST_EXISTS)) { |
|---|
| 206 |
g_warning ("%s already existed\n", filen); |
|---|
| 207 |
g_unlink (filen); |
|---|
| 208 |
} |
|---|
| 209 |
|
|---|
| 210 |
g_key_file_set_value (keyfile, "tmut", "proto", proto); |
|---|
| 211 |
g_key_file_set_value (keyfile, "tmut", "name", name); |
|---|
| 212 |
g_key_file_set_value (keyfile, "tmut", "hostname", hostname); |
|---|
| 213 |
g_key_file_set_value (keyfile, "tmut", "user", user); |
|---|
| 214 |
g_key_file_set_value (keyfile, "tmut", "type", type); |
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 |
if (options) { |
|---|
| 218 |
gint options_len = 0; |
|---|
| 219 |
while (options[options_len] != NULL) |
|---|
| 220 |
options_len++; |
|---|
| 221 |
g_key_file_set_string_list (keyfile, "tmut", "options", |
|---|
| 222 |
options, options_len); |
|---|
| 223 |
} |
|---|
| 224 |
|
|---|
| 225 |
|
|---|
| 226 |
|
|---|
| 227 |
file = fopen (filen, "w"); |
|---|
| 228 |
|
|---|
| 229 |
if (file) { |
|---|
| 230 |
gsize len; |
|---|
| 231 |
char *str = g_key_file_to_data (keyfile, &len, NULL); |
|---|
| 232 |
fputs (str, file); |
|---|
| 233 |
fclose (file); |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|
| 236 |
account = create_account_instance (priv, type, proto, mech, name, |
|---|
| 237 |
(gchar **) options, user, hostname, port, NULL, filen, from); |
|---|
| 238 |
|
|---|
| 239 |
if (account) { |
|---|
| 240 |
priv->accounts = g_list_prepend (priv->accounts, account); |
|---|
| 241 |
g_signal_emit (self, |
|---|
| 242 |
tmut_account_store_signals [TMUT_ACCOUNT_STORE_ACCOUNT_CREATED], |
|---|
| 243 |
0, account); |
|---|
| 244 |
} |
|---|
| 245 |
|
|---|
| 246 |
g_key_file_free (keyfile); |
|---|
| 247 |
g_free (filen); |
|---|
| 248 |
|
|---|
| 249 |
} |
|---|
| 250 |
|
|---|
| 251 |
void |
|---|
| 252 |
tmut_account_store_delete_account (TMutAccountStore *self, TnyAccount *account) |
|---|
| 253 |
{ |
|---|
| 254 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 255 |
const gchar *filen = tny_account_get_id (account); |
|---|
| 256 |
GList *found; |
|---|
| 257 |
TnyAccount *found_account = NULL; |
|---|
| 258 |
|
|---|
| 259 |
if (g_file_test (filen, G_FILE_TEST_EXISTS)) |
|---|
| 260 |
g_unlink (filen); |
|---|
| 261 |
|
|---|
| 262 |
found = g_list_find (priv->accounts, account); |
|---|
| 263 |
if (found) { |
|---|
| 264 |
found_account = found->data; |
|---|
| 265 |
priv->accounts = g_list_delete_link (priv->accounts, found); |
|---|
| 266 |
} |
|---|
| 267 |
|
|---|
| 268 |
if (found_account) { |
|---|
| 269 |
g_signal_emit (self, |
|---|
| 270 |
tmut_account_store_signals [TMUT_ACCOUNT_STORE_ACCOUNT_DELETED], |
|---|
| 271 |
0, found_account); |
|---|
| 272 |
g_object_unref (found_account); |
|---|
| 273 |
} |
|---|
| 274 |
|
|---|
| 275 |
} |
|---|
| 276 |
|
|---|
| 277 |
void |
|---|
| 278 |
tmut_account_store_edit_account (TMutAccountStore *self, TnyAccount *account, |
|---|
| 279 |
gboolean enabled, |
|---|
| 280 |
const gchar *name, |
|---|
| 281 |
const gchar *hostname, |
|---|
| 282 |
const gchar *proto, |
|---|
| 283 |
const gchar *user, |
|---|
| 284 |
const gchar *mech, |
|---|
| 285 |
const gchar *from, |
|---|
| 286 |
gint port, |
|---|
| 287 |
const gchar **options) |
|---|
| 288 |
{ |
|---|
| 289 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 290 |
const gchar *filen = tny_account_get_id (account); |
|---|
| 291 |
FILE *file; |
|---|
| 292 |
GKeyFile *keyfile = g_key_file_new (); |
|---|
| 293 |
|
|---|
| 294 |
g_key_file_load_from_file (keyfile, filen, G_KEY_FILE_NONE, NULL); |
|---|
| 295 |
|
|---|
| 296 |
if (name) { |
|---|
| 297 |
tny_account_set_name (account, name); |
|---|
| 298 |
g_key_file_set_value (keyfile, "tmut", "name", name); |
|---|
| 299 |
} |
|---|
| 300 |
|
|---|
| 301 |
if (hostname) { |
|---|
| 302 |
tny_account_set_hostname (account, hostname); |
|---|
| 303 |
g_key_file_set_value (keyfile, "tmut", "hostname", hostname); |
|---|
| 304 |
} |
|---|
| 305 |
|
|---|
| 306 |
if (proto) { |
|---|
| 307 |
tny_account_set_proto (account, proto); |
|---|
| 308 |
g_key_file_set_value (keyfile, "tmut", "proto", proto); |
|---|
| 309 |
} |
|---|
| 310 |
|
|---|
| 311 |
if (user) { |
|---|
| 312 |
tny_account_set_user (account, user); |
|---|
| 313 |
g_key_file_set_value (keyfile, "tmut", "user", user); |
|---|
| 314 |
} |
|---|
| 315 |
|
|---|
| 316 |
if (port != -1) { |
|---|
| 317 |
tny_account_set_port (account, port); |
|---|
| 318 |
g_key_file_set_integer (keyfile, "tmut", "port", port); |
|---|
| 319 |
} |
|---|
| 320 |
|
|---|
| 321 |
if (mech) { |
|---|
| 322 |
tny_account_set_secure_auth_mech (account, mech); |
|---|
| 323 |
g_key_file_set_value (keyfile, "tmut", "mech", mech); |
|---|
| 324 |
} |
|---|
| 325 |
|
|---|
| 326 |
if (from && TNY_IS_CAMEL_TRANSPORT_ACCOUNT (account)) { |
|---|
| 327 |
tny_camel_transport_account_set_from (TNY_CAMEL_TRANSPORT_ACCOUNT (account), from); |
|---|
| 328 |
g_key_file_set_value (keyfile, "tmut", "from", from); |
|---|
| 329 |
} |
|---|
| 330 |
|
|---|
| 331 |
tny_camel_account_clear_options (TNY_CAMEL_ACCOUNT (account)); |
|---|
| 332 |
|
|---|
| 333 |
if (options) { |
|---|
| 334 |
gint i = 0; |
|---|
| 335 |
while (options[i] != NULL) { |
|---|
| 336 |
gchar *str = g_strdup (options [i]); |
|---|
| 337 |
gchar *key = str; |
|---|
| 338 |
gchar *value = strchr (str, '='); |
|---|
| 339 |
|
|---|
| 340 |
if (value) { |
|---|
| 341 |
*value = '\0'; |
|---|
| 342 |
value++; |
|---|
| 343 |
} else |
|---|
| 344 |
value = ""; |
|---|
| 345 |
|
|---|
| 346 |
tny_camel_account_add_option (TNY_CAMEL_ACCOUNT (account), |
|---|
| 347 |
tny_pair_new (key, value)); |
|---|
| 348 |
i++; |
|---|
| 349 |
g_free (str); |
|---|
| 350 |
} |
|---|
| 351 |
|
|---|
| 352 |
g_key_file_set_string_list (keyfile, "tmut", "options", |
|---|
| 353 |
options, i); |
|---|
| 354 |
} else { |
|---|
| 355 |
g_key_file_remove_key (keyfile, "tmut", "options", NULL); |
|---|
| 356 |
} |
|---|
| 357 |
|
|---|
| 358 |
file = fopen (filen, "w"); |
|---|
| 359 |
|
|---|
| 360 |
if (file) { |
|---|
| 361 |
gsize len; |
|---|
| 362 |
char *str = g_key_file_to_data (keyfile, &len, NULL); |
|---|
| 363 |
fputs (str, file); |
|---|
| 364 |
fclose (file); |
|---|
| 365 |
} |
|---|
| 366 |
|
|---|
| 367 |
g_signal_emit (self, |
|---|
| 368 |
tmut_account_store_signals [TMUT_ACCOUNT_STORE_ACCOUNT_EDITED], |
|---|
| 369 |
0, account); |
|---|
| 370 |
|
|---|
| 371 |
g_key_file_free (keyfile); |
|---|
| 372 |
} |
|---|
| 373 |
|
|---|
| 374 |
|
|---|
| 375 |
static void |
|---|
| 376 |
kill_stored_accounts (TMutAccountStorePriv *priv) |
|---|
| 377 |
{ |
|---|
| 378 |
if (priv->accounts) { |
|---|
| 379 |
g_list_foreach (priv->accounts, (GFunc) g_object_unref, NULL); |
|---|
| 380 |
g_list_free (priv->accounts); |
|---|
| 381 |
priv->accounts = NULL; |
|---|
| 382 |
} |
|---|
| 383 |
} |
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
static void |
|---|
| 387 |
load_accounts (TnyAccountStore *self) |
|---|
| 388 |
{ |
|---|
| 389 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 390 |
|
|---|
| 391 |
const gchar *filen; |
|---|
| 392 |
gchar *configd; |
|---|
| 393 |
GDir *dir; |
|---|
| 394 |
|
|---|
| 395 |
configd = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir(), |
|---|
| 396 |
".tmut", "accounts", NULL); |
|---|
| 397 |
dir = g_dir_open (configd, 0, NULL); |
|---|
| 398 |
g_free (configd); |
|---|
| 399 |
|
|---|
| 400 |
if (!dir) |
|---|
| 401 |
return; |
|---|
| 402 |
|
|---|
| 403 |
for (filen = g_dir_read_name (dir); filen; filen = g_dir_read_name (dir)) { |
|---|
| 404 |
GError *port_err = NULL; |
|---|
| 405 |
GKeyFile *keyfile; |
|---|
| 406 |
gchar *proto, *type, *from, *user, *hostname, *url_string; |
|---|
| 407 |
gchar *name, *mech, **options; |
|---|
| 408 |
gsize options_len; |
|---|
| 409 |
TnyAccount *account = NULL; |
|---|
| 410 |
gint port = 0; |
|---|
| 411 |
gchar *fullfilen = g_build_filename (g_get_home_dir(), |
|---|
| 412 |
".tmut", "accounts", filen, NULL); |
|---|
| 413 |
|
|---|
| 414 |
keyfile = g_key_file_new (); |
|---|
| 415 |
|
|---|
| 416 |
if (!g_key_file_load_from_file (keyfile, fullfilen, G_KEY_FILE_NONE, NULL)) { |
|---|
| 417 |
g_free (fullfilen); |
|---|
| 418 |
continue; |
|---|
| 419 |
} |
|---|
| 420 |
|
|---|
| 421 |
if (g_key_file_get_boolean (keyfile, "tmut", "disabled", NULL)) { |
|---|
| 422 |
g_free (fullfilen); |
|---|
| 423 |
continue; |
|---|
| 424 |
} |
|---|
| 425 |
|
|---|
| 426 |
type = g_key_file_get_value (keyfile, "tmut", "type", NULL); |
|---|
| 427 |
proto = g_key_file_get_value (keyfile, "tmut", "proto", NULL); |
|---|
| 428 |
mech = g_key_file_get_value (keyfile, "tmut", "mech", NULL); |
|---|
| 429 |
name = g_key_file_get_value (keyfile, "tmut", "name", NULL); |
|---|
| 430 |
options = g_key_file_get_string_list (keyfile, "tmut", |
|---|
| 431 |
"options", &options_len, NULL); |
|---|
| 432 |
user = g_key_file_get_value (keyfile, "tmut", "user", NULL); |
|---|
| 433 |
hostname = g_key_file_get_value (keyfile, "tmut", "hostname", NULL); |
|---|
| 434 |
url_string = g_key_file_get_value (keyfile, "tmut", "url_string", NULL); |
|---|
| 435 |
port = g_key_file_get_integer (keyfile, "tmut", "port", &port_err); |
|---|
| 436 |
from = g_key_file_get_value (keyfile, "tmut", "from", NULL); |
|---|
| 437 |
|
|---|
| 438 |
if (port_err) { |
|---|
| 439 |
port = -1; |
|---|
| 440 |
g_error_free (port_err); |
|---|
| 441 |
} |
|---|
| 442 |
|
|---|
| 443 |
account = create_account_instance (priv, type, proto, mech, name, options, user, hostname, port, url_string, fullfilen, from); |
|---|
| 444 |
|
|---|
| 445 |
if (account) |
|---|
| 446 |
priv->accounts = g_list_prepend (priv->accounts, account); |
|---|
| 447 |
|
|---|
| 448 |
g_free (type); |
|---|
| 449 |
if (options) |
|---|
| 450 |
g_strfreev (options); |
|---|
| 451 |
g_free (url_string); |
|---|
| 452 |
g_free (hostname); |
|---|
| 453 |
g_free (user); |
|---|
| 454 |
g_free (proto); |
|---|
| 455 |
g_free (mech); |
|---|
| 456 |
|
|---|
| 457 |
g_key_file_free (keyfile); |
|---|
| 458 |
} |
|---|
| 459 |
g_dir_close (dir); |
|---|
| 460 |
|
|---|
| 461 |
tny_session_camel_set_initialized (priv->session); |
|---|
| 462 |
} |
|---|
| 463 |
|
|---|
| 464 |
|
|---|
| 465 |
static void |
|---|
| 466 |
tmut_account_store_get_accounts (TnyAccountStore *self, TnyList *list, TnyGetAccountsRequestType types) |
|---|
| 467 |
{ |
|---|
| 468 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 469 |
|
|---|
| 470 |
g_assert (TNY_IS_LIST (list)); |
|---|
| 471 |
|
|---|
| 472 |
if (!priv->accounts) |
|---|
| 473 |
load_accounts (self); |
|---|
| 474 |
|
|---|
| 475 |
if (priv->accounts) { |
|---|
| 476 |
GList *copy = priv->accounts; |
|---|
| 477 |
while (copy) { |
|---|
| 478 |
TnyAccount *account = copy->data; |
|---|
| 479 |
if (types == TNY_ACCOUNT_STORE_BOTH || types == TNY_ACCOUNT_STORE_STORE_ACCOUNTS) { |
|---|
| 480 |
if (TNY_IS_STORE_ACCOUNT (account)) |
|---|
| 481 |
tny_list_prepend (list, (GObject*) account); |
|---|
| 482 |
} |
|---|
| 483 |
|
|---|
| 484 |
if (types == TNY_ACCOUNT_STORE_BOTH || types == TNY_ACCOUNT_STORE_TRANSPORT_ACCOUNTS) { |
|---|
| 485 |
if (TNY_IS_TRANSPORT_ACCOUNT (account)) |
|---|
| 486 |
tny_list_prepend (list, (GObject*) account); |
|---|
| 487 |
} |
|---|
| 488 |
copy = g_list_next (copy); |
|---|
| 489 |
} |
|---|
| 490 |
} |
|---|
| 491 |
|
|---|
| 492 |
return; |
|---|
| 493 |
} |
|---|
| 494 |
|
|---|
| 495 |
static const gchar* |
|---|
| 496 |
tmut_account_store_get_cache_dir (TnyAccountStore *self) |
|---|
| 497 |
{ |
|---|
| 498 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 499 |
if (!priv->cache_dir) |
|---|
| 500 |
priv->cache_dir = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir(), ".tmut", NULL); |
|---|
| 501 |
return priv->cache_dir; |
|---|
| 502 |
} |
|---|
| 503 |
|
|---|
| 504 |
static TnyDevice* |
|---|
| 505 |
tmut_account_store_get_device (TnyAccountStore *self) |
|---|
| 506 |
{ |
|---|
| 507 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 508 |
return g_object_ref (priv->device); |
|---|
| 509 |
} |
|---|
| 510 |
|
|---|
| 511 |
static gboolean |
|---|
| 512 |
tmut_account_store_alert (TnyAccountStore *self, TnyAccount *account, TnyAlertType type, gboolean question, GError *error) |
|---|
| 513 |
{ |
|---|
| 514 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 515 |
GtkMessageType gtktype; |
|---|
| 516 |
gboolean retval = FALSE; |
|---|
| 517 |
GtkWidget *dialog; |
|---|
| 518 |
|
|---|
| 519 |
switch (type) { |
|---|
| 520 |
case TNY_ALERT_TYPE_INFO: |
|---|
| 521 |
gtktype = GTK_MESSAGE_INFO; |
|---|
| 522 |
break; |
|---|
| 523 |
case TNY_ALERT_TYPE_WARNING: |
|---|
| 524 |
gtktype = GTK_MESSAGE_WARNING; |
|---|
| 525 |
break; |
|---|
| 526 |
case TNY_ALERT_TYPE_ERROR: |
|---|
| 527 |
default: |
|---|
| 528 |
gtktype = GTK_MESSAGE_ERROR; |
|---|
| 529 |
break; |
|---|
| 530 |
} |
|---|
| 531 |
|
|---|
| 532 |
dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, |
|---|
| 533 |
gtktype, GTK_BUTTONS_YES_NO, error->message); |
|---|
| 534 |
|
|---|
| 535 |
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES) |
|---|
| 536 |
retval = TRUE; |
|---|
| 537 |
|
|---|
| 538 |
gtk_widget_destroy (dialog); |
|---|
| 539 |
|
|---|
| 540 |
return retval; |
|---|
| 541 |
} |
|---|
| 542 |
|
|---|
| 543 |
static TnyAccount* |
|---|
| 544 |
tmut_account_store_find_account (TnyAccountStore *self, const gchar *url_string) |
|---|
| 545 |
{ |
|---|
| 546 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 547 |
TnyAccount *found = NULL; |
|---|
| 548 |
|
|---|
| 549 |
if (!priv->accounts) |
|---|
| 550 |
load_accounts (self); |
|---|
| 551 |
|
|---|
| 552 |
if (priv->accounts) { |
|---|
| 553 |
GList *copy = priv->accounts; |
|---|
| 554 |
while (copy) { |
|---|
| 555 |
TnyAccount *account = copy->data; |
|---|
| 556 |
|
|---|
| 557 |
if (tny_account_matches_url_string (account, url_string)) { |
|---|
| 558 |
found = TNY_ACCOUNT (g_object_ref (G_OBJECT (account))); |
|---|
| 559 |
break; |
|---|
| 560 |
} |
|---|
| 561 |
copy = g_list_next (copy); |
|---|
| 562 |
} |
|---|
| 563 |
} |
|---|
| 564 |
|
|---|
| 565 |
return found; |
|---|
| 566 |
} |
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
static void |
|---|
| 570 |
tmut_account_store_finalize (GObject *object) |
|---|
| 571 |
{ |
|---|
| 572 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (object); |
|---|
| 573 |
|
|---|
| 574 |
kill_stored_accounts (priv); |
|---|
| 575 |
|
|---|
| 576 |
if (priv->cache_dir) |
|---|
| 577 |
g_free (priv->cache_dir); |
|---|
| 578 |
|
|---|
| 579 |
g_object_unref (priv->device); |
|---|
| 580 |
|
|---|
| 581 |
parent_class->finalize (object); |
|---|
| 582 |
} |
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
TnyAccountStore* |
|---|
| 586 |
tmut_account_store_new (void) |
|---|
| 587 |
{ |
|---|
| 588 |
TMutAccountStore *self = g_object_new (TMUT_TYPE_ACCOUNT_STORE, NULL); |
|---|
| 589 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (self); |
|---|
| 590 |
priv->session = tny_session_camel_new (TNY_ACCOUNT_STORE (self)); |
|---|
| 591 |
tny_session_camel_set_ui_locker (priv->session, tny_gtk_lockable_new ()); |
|---|
| 592 |
return TNY_ACCOUNT_STORE (self); |
|---|
| 593 |
} |
|---|
| 594 |
|
|---|
| 595 |
static void |
|---|
| 596 |
tmut_account_store_instance_init (GTypeInstance *instance, gpointer g_class) |
|---|
| 597 |
{ |
|---|
| 598 |
TMutAccountStorePriv *priv = TMUT_ACCOUNT_STORE_GET_PRIVATE (instance); |
|---|
| 599 |
TnyPlatformFactory *platfact = tmut_platform_factory_get_instance (); |
|---|
| 600 |
|
|---|
| 601 |
priv->cache_dir = NULL; |
|---|
| 602 |
priv->accounts = NULL; |
|---|
| 603 |
priv->device = tny_platform_factory_new_device (platfact); |
|---|
| 604 |
|
|---|
| 605 |
return; |
|---|
| 606 |
} |
|---|
| 607 |
|
|---|
| 608 |
static void |
|---|
| 609 |
tny_account_store_init (TnyAccountStoreIface *klass) |
|---|
| 610 |
{ |
|---|
| 611 |
klass->get_accounts = tmut_account_store_get_accounts; |
|---|
| 612 |
klass->get_cache_dir = tmut_account_store_get_cache_dir; |
|---|
| 613 |
klass->get_device = tmut_account_store_get_device; |
|---|
| 614 |
klass->alert = tmut_account_store_alert; |
|---|
| 615 |
klass->find_account = tmut_account_store_find_account; |
|---|
| 616 |
} |
|---|
| 617 |
|
|---|
| 618 |
static void |
|---|
| 619 |
tmut_account_store_class_init (TMutAccountStoreClass *klass) |
|---|
| 620 |
{ |
|---|
| 621 |
GObjectClass *object_class; |
|---|
| 622 |
|
|---|
| 623 |
parent_class = g_type_class_peek_parent (klass); |
|---|
| 624 |
object_class = (GObjectClass*) klass; |
|---|
| 625 |
|
|---|
| 626 |
object_class->finalize = tmut_account_store_finalize; |
|---|
| 627 |
|
|---|
| 628 |
g_type_class_add_private (object_class, sizeof (TMutAccountStorePriv)); |
|---|
| 629 |
|
|---|
| 630 |
} |
|---|
| 631 |
|
|---|
| 632 |
|
|---|
| 633 |
|
|---|
| 634 |
static void |
|---|
| 635 |
tmut_account_store_base_init (gpointer g_class) |
|---|
| 636 |
{ |
|---|
| 637 |
static gboolean tmut_account_store_initialized = FALSE; |
|---|
| 638 |
|
|---|
| 639 |
if (!tmut_account_store_initialized) |
|---|
| 640 |
{ |
|---|
| 641 |
tmut_account_store_signals[TMUT_ACCOUNT_STORE_ACCOUNT_DELETED] = |
|---|
| 642 |
g_signal_new ("account_deleted", |
|---|
| 643 |
TMUT_TYPE_ACCOUNT_STORE, |
|---|
| 644 |
G_SIGNAL_RUN_FIRST, |
|---|
| 645 |
G_STRUCT_OFFSET (TMutAccountStoreClass, account_deleted), |
|---|
| 646 |
NULL, NULL, |
|---|
| 647 |
g_cclosure_marshal_VOID__OBJECT, |
|---|
| 648 |
G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT); |
|---|
| 649 |
|
|---|
| 650 |
tmut_account_store_signals[TMUT_ACCOUNT_STORE_ACCOUNT_EDITED] = |
|---|
| 651 |
g_signal_new ("account_edited", |
|---|
| 652 |
TMUT_TYPE_ACCOUNT_STORE, |
|---|
| 653 |
G_SIGNAL_RUN_FIRST, |
|---|
| 654 |
G_STRUCT_OFFSET (TMutAccountStoreClass, account_edited), |
|---|
| 655 |
NULL, NULL, |
|---|
| 656 |
g_cclosure_marshal_VOID__OBJECT, |
|---|
| 657 |
G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT); |
|---|
| 658 |
|
|---|
| 659 |
tmut_account_store_signals[TMUT_ACCOUNT_STORE_ACCOUNT_CREATED] = |
|---|
| 660 |
g_signal_new ("account_created", |
|---|
| 661 |
TMUT_TYPE_ACCOUNT_STORE, |
|---|
| 662 |
G_SIGNAL_RUN_FIRST, |
|---|
| 663 |
G_STRUCT_OFFSET (TMutAccountStoreClass, account_created), |
|---|
| 664 |
NULL, NULL, |
|---|
| 665 |
g_cclosure_marshal_VOID__OBJECT, |
|---|
| 666 |
G_TYPE_NONE, 1, TNY_TYPE_ACCOUNT); |
|---|
| 667 |
|
|---|
| 668 |
tmut_account_store_initialized = TRUE; |
|---|
| 669 |
} |
|---|
| 670 |
} |
|---|
| 671 |
|
|---|
| 672 |
|
|---|
| 673 |
GType |
|---|
| 674 |
tmut_account_store_get_type (void) |
|---|
| 675 |
{ |
|---|
| 676 |
static GType type = 0; |
|---|
| 677 |
if (G_UNLIKELY(type == 0)) |
|---|
| 678 |
{ |
|---|
| 679 |
static const GTypeInfo info = |
|---|
| 680 |
{ |
|---|
| 681 |
sizeof (TMutAccountStoreClass), |
|---|
| 682 |
tmut_account_store_base_init, |
|---|
| 683 |
NULL, |
|---|
| 684 |
(GClassInitFunc) tmut_account_store_class_init, |
|---|
| 685 |
NULL, |
|---|
| 686 |
NULL, |
|---|
| 687 |
sizeof (TMutAccountStore), |
|---|
| 688 |
0, |
|---|
| 689 |
tmut_account_store_instance_init, |
|---|
| 690 |
NULL |
|---|
| 691 |
}; |
|---|
| 692 |
|
|---|
| 693 |
|
|---|
| 694 |
static const GInterfaceInfo tny_account_store_info = |
|---|
| 695 |
{ |
|---|
| 696 |
(GInterfaceInitFunc) tny_account_store_init, |
|---|
| 697 |
NULL, |
|---|
| 698 |
NULL |
|---|
| 699 |
}; |
|---|
| 700 |
|
|---|
| 701 |
type = g_type_register_static (G_TYPE_OBJECT, |
|---|
| 702 |
"TMutAccountStore", |
|---|
| 703 |
&info, 0); |
|---|
| 704 |
|
|---|
| 705 |
g_type_add_interface_static (type, TNY_TYPE_ACCOUNT_STORE, |
|---|
| 706 |
&tny_account_store_info); |
|---|
| 707 |
|
|---|
| 708 |
} |
|---|
| 709 |
return type; |
|---|
| 710 |
} |
|---|