v0.2.2 release
[yandexfotkisp] / src / send.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 <string.h>
14 #include <stdlib.h>
15 #include <glib.h>
16 #include <osso-log.h>
17 #include "send.h"
18 #include "common.h"
19
20 /**
21  * send:
22  * @account: #SharingTransfer to be send
23  * @con: Connection used
24  * @dead_mans_switch: Turn to %FALSE at least every 30 seconds.
25  *
26  * Sends #SharingTransfer to service.
27  *
28  * Returns: #SharingPluginInterfaceSendResult
29  */
30 SharingPluginInterfaceSendResult share_item (SharingTransfer* transfer,
31     ConIcConnection* con, gboolean* dead_mans_switch)
32 {
33     SharingPluginInterfaceSendResult ret = SHARING_SEND_ERROR_UNKNOWN;
34     SharingEntry *entry = sharing_transfer_get_entry( transfer );
35
36     yandexPhotoAccessType access = YANDEX_PHOTO_ACCESS_PUBLIC;
37     const gchar* privacyOption = sharing_entry_get_option(entry,"privacy");
38     if (privacyOption) {
39         if (strcmp(privacyOption,"friends")==0) access = YANDEX_PHOTO_ACCESS_FRIENDS;
40         else if (strcmp(privacyOption,"private")==0) access = YANDEX_PHOTO_ACCESS_PRIVATE;
41     }
42     yandexPhotoPublishSettings publish = YANDEX_PHOTO_DONT_PUBLISH;
43     const gchar* publishOption = sharing_entry_get_option(entry,"publish");
44     if (publishOption) {
45         if (strcmp(publishOption,"yes")==0) publish = YANDEX_PHOTO_PUBLISH;
46     }
47     const gchar* album = sharing_entry_get_option(entry,"album");
48     if (strcmp(album,"0")==0) album = NULL;
49
50     SharingAccount* account = sharing_entry_get_account(entry);
51     char* sessionKey = NULL;
52     char* sessionRequestId = NULL;
53     char* token = NULL;
54
55     yandexGetAuthTokenResult token_res = YANDEX_GET_AUTH_TOKEN_FAILED;
56     *dead_mans_switch = FALSE;
57     if (yandexGetSessionKey(&sessionKey, &sessionRequestId) == YANDEX_GET_SESSION_KEY_SUCCESS) {
58         *dead_mans_switch = FALSE;
59         token_res = yandexGetAuthToken(sessionRequestId, sessionKey,
60                                                            sharing_account_get_username(account), sharing_account_get_password(account),
61                                                            &token);
62     }
63     *dead_mans_switch = FALSE;
64
65     if (YANDEX_GET_AUTH_TOKEN_SUCCESS == token_res && token != NULL) {
66                 GSList* p;
67                 ret = SHARING_SEND_SUCCESS;
68                 for (p = sharing_entry_get_media (entry); p != NULL; p = g_slist_next(p)) {
69                         *dead_mans_switch = FALSE;
70                         if (!sharing_transfer_continue(transfer)) break;
71                         SharingEntryMedia* media = p->data;
72                         if (!sharing_entry_media_get_sent (media)) {
73                                 yandexPhotoOptions options;
74                                 options.album = album;
75                                 options.access = access;
76                                 yandexSendPhotoResult send_res = yandexSendPhoto(token,media,transfer,options);
77                                 if (YANDEX_SEND_PHOTO_SUCCESS == send_res) sharing_entry_media_set_sent(media,TRUE);
78                                 else ret = SHARING_SEND_ERROR_AUTH;
79                         }
80                 }
81                 if (!sharing_transfer_continue(transfer)) ret = SHARING_SEND_CANCELLED;
82     } else if (YANDEX_GET_AUTH_TOKEN_INVALID_USER == token_res) {
83         ret = SHARING_SEND_ERROR_AUTH;
84     }
85
86     if (token) free(token);
87     if (sessionKey) free(sessionKey);
88     if (sessionRequestId) free(sessionRequestId);
89
90     return ret;
91 }
92