v0.2.2 release
[yandexfotkisp] / src / validate.c
1 /*
2  * This file is part of sharing-plugin-template
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation. All rights reserved.
5  *
6  * This maemo code example is licensed under a MIT-style license,
7  * that can be found in the file called "COPYING" in the root
8  * directory.
9  *
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <glib.h>
15 #include "libsharing/sharing-account.h"
16 #include "libsharing/sharing-service-option.h"
17 #include <osso-log.h>
18 #include <conicconnection.h>
19 #include "validate.h"
20 #include "common.h"
21
22 /**
23  * test:
24  * @account: #SharingAccount to be tested
25  * @con: Connection used
26  * @dead_mans_switch: Turn to %FALSE at least every 30 seconds.
27  *
28  * Test if #SharingAccount is valid.
29  *
30  * Returns: #SharingPluginInterfaceTestAccountResult
31  */
32 SharingPluginInterfaceAccountValidateResult validate (SharingAccount* account,
33     ConIcConnection* con, gboolean *cont, gboolean* dead_mans_switch)
34 {
35     SharingPluginInterfaceAccountValidateResult ret = SHARING_ACCOUNT_VALIDATE_FAILED;
36
37     char* sessionKey = NULL;
38     char* sessionRequestId = NULL;
39     char* token = NULL;
40
41     *dead_mans_switch = FALSE;
42     if (yandexGetSessionKey(&sessionKey, &sessionRequestId) == YANDEX_GET_SESSION_KEY_SUCCESS) {
43         *dead_mans_switch = FALSE;
44         yandexGetAuthTokenResult res =
45                 yandexGetAuthToken(sessionRequestId, sessionKey,
46                                                            sharing_account_get_username(account), sharing_account_get_password(account),
47                                                            &token);
48         switch (res) {
49         case YANDEX_GET_AUTH_TOKEN_SUCCESS:
50                 ret = SHARING_ACCOUNT_VALIDATE_SUCCESS;
51                 GSList* albumsList = NULL;
52                 if (YANDEX_GET_ALBUM_LIST_SUCCESS == yandexGetAlbumsList(token,sharing_account_get_username(account),&albumsList) && albumsList)
53                         sharing_account_set_option_values(account,"album",albumsList);
54                 if (albumsList) sharing_service_option_values_free(albumsList);
55                 break;
56         case YANDEX_GET_AUTH_TOKEN_FAILED:
57                 ret = SHARING_ACCOUNT_VALIDATE_ERROR_UNKNOWN;
58                 break;
59         case YANDEX_GET_AUTH_TOKEN_INVALID_USER:
60                 ret = SHARING_ACCOUNT_VALIDATE_FAILED;
61                 break;
62         }
63     }
64     *dead_mans_switch = FALSE;
65
66     if (token) free(token);
67     if (sessionKey) free(sessionKey);
68     if (sessionRequestId) free(sessionRequestId);
69
70     return ret;
71 }
72