Initial commit
[twcano:sharing-service-twitpic.git] / src / twitpic_post.c
1 #include <glib.h>
2 #include <glib/gprintf.h>
3 #include <osso-log.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <curl/curl.h>
7
8 #include <sharing-http.h>
9 #include <sharing-plugin-interface.h>
10 #include <sharing-entry.h>
11 #include <sharing-tag.h>
12
13 #include "twitpic_post.h"
14
15 #define END_POINT "http://twitpic.com/api/uploadAndPost"
16
17 #if 0
18 size_t my_write_func(void *ptr, size_t size, size_t nmemb, GString *data)
19 {
20       return fwrite(ptr, size, nmemb, stream);
21 }
22 #endif
23
24 size_t twitpic_write_func(void *ptr, size_t size, size_t nmemb, void *user_data)
25 {
26     GString *data = (GString *)user_data;
27     ULOG_DEBUG_L ("WRITE FUNC:  %p[%d][%d]", data, size, nmemb);
28
29     g_string_append_len(data, (char*)ptr, size * nmemb);
30
31     return size*nmemb;
32 }
33
34 int twitpic_progress_func(GtkWidget *bar,
35                      double t, /* dltotal */ 
36                      double d, /* dlnow */ 
37                      double ultotal,
38                      double ulnow)
39 {
40   ULOG_DEBUG_L ("Progress %d / %d (%g %%)\n", d, t, d*100.0/t);
41   return 0;
42 }
43
44 int
45 twitpic_upload_media(const char *username,
46                      const char *password,
47                      SharingEntryMedia *media)
48 {
49   GString *response;
50   CURL* hCurl = NULL;
51   CURLcode hResult;
52   char curl_errorstr[CURL_ERROR_SIZE];
53
54
55   struct curl_httppost *post = NULL;
56   struct curl_httppost *last = NULL;
57
58   hCurl = curl_easy_init();
59
60   curl_formadd(&post, &last, CURLFORM_COPYNAME, "username",
61                              CURLFORM_COPYCONTENTS, username,
62                              CURLFORM_END);
63
64   curl_formadd(&post, &last, CURLFORM_COPYNAME, "password",
65                              CURLFORM_COPYCONTENTS, password,
66                              CURLFORM_END);
67
68   curl_formadd(&post, &last, CURLFORM_COPYNAME, "message",
69                              CURLFORM_COPYCONTENTS, sharing_entry_media_get_desc(media),
70                              CURLFORM_END);
71
72   curl_formadd(&post, &last, CURLFORM_COPYNAME, "media",
73                              CURLFORM_FILE, sharing_entry_media_get_localpath(media),
74                              CURLFORM_END);
75
76   hResult = curl_easy_setopt(hCurl, CURLOPT_URL, END_POINT);
77   hResult = curl_easy_setopt(hCurl, CURLOPT_HTTPPOST, post);
78   hResult = curl_easy_setopt(hCurl, CURLOPT_ERRORBUFFER, curl_errorstr);
79
80   response = g_string_new("");
81   ULOG_DEBUG_L("response created at: %p", response);
82
83   hResult = curl_easy_setopt(hCurl, CURLOPT_WRITEDATA, response);
84 //  hResult = curl_easy_setopt(hCurl, CURLOPT_READFUNCTION, twitpic_read_func);
85   hResult = curl_easy_setopt(hCurl, CURLOPT_WRITEFUNCTION, twitpic_write_func);
86 //  hResult = curl_easy_setopt(hCurl, CURLOPT_PROGRESSFUNCTION, twitpic_progress_func);
87
88   hResult = curl_easy_perform(hCurl);
89
90   int ret = SHARING_SEND_SUCCESS;
91
92   if (hResult != CURLE_OK) {
93     ULOG_DEBUG_L ("SEND ERROR: %s, media: %s, username: %s, password: %s", curl_errorstr, sharing_entry_media_get_localpath(media), username, password);
94     return -1;
95   } else {
96     ULOG_DEBUG_L ("SEND RESPONSE: %s", response->str);
97     if (g_strstr(response->str, "<rsp stat=\"ok\">") == 0) {
98       ULOG_DEBUG_L ("API ERROR: %s", response->str);
99       if (g_strstr(response->str, "<err code=\"1001\"") != 0) {
100         ULOG_DEBUG_L ("API ERROR FIX: 1001");
101         ret = SHARING_SEND_ERROR_AUTH;
102       } else if (g_strstr(response->str, "<err code=\"1002\"") != 0) {
103         ULOG_DEBUG_L ("API ERROR FIX: 1002");
104         ret = SHARING_SEND_ERROR_FILE_FORMAT;
105       } else if (g_strstr(response->str, "<err code=\"1003\"") != 0) {
106         ULOG_DEBUG_L ("API ERROR FIX: 1003");
107         ret = SHARING_SEND_ERROR_FILE_FORMAT;
108       } else if (g_strstr(response->str, "<err code=\"1004\"") != 0) {
109         ULOG_DEBUG_L ("API ERROR FIX: 1004");
110         ret = SHARING_SEND_ERROR_UPLOAD_LIMIT;
111       }
112     }
113   }
114
115   curl_formfree(post);
116   curl_easy_cleanup(hCurl);
117
118   g_string_free(response, TRUE);
119
120   sharing_entry_media_set_sent(media, TRUE);
121   return ret;
122 }
123
124 SharingPluginInterfaceAccountValidateResult
125 twitpic_post_validate_account (SharingAccount *account,
126                                ConIcConnection *con,
127                                gboolean *cont,
128                                gboolean *dead_mans_switch) {
129
130 #ifdef DEBUG
131   gchar *fn = "twitpic_post_validate_account()";
132 #endif
133
134   ULOG_DEBUG_L ("Entered %s",fn);
135
136   return SHARING_ACCOUNT_VALIDATE_SUCCESS;
137 }
138
139
140 SharingPluginInterfaceSendResult 
141 twitpic_post_upload_to_service (SharingTransfer *transfer,
142                                ConIcConnection *con,
143                                gboolean *dead_mans_switch) {
144
145 #ifdef DEBUG
146   gchar *fn = "twitpic_post_upload_to_service()";
147 #endif
148   GSList *m = NULL;
149   int result = SHARING_SEND_SUCCESS;
150
151   ULOG_DEBUG_L ("Entered %s",fn);
152
153   SharingEntry* entry = sharing_transfer_get_entry(transfer);
154   SharingAccount* account = sharing_entry_get_account(entry);
155
156   curl_global_init(CURL_GLOBAL_ALL);
157
158   for(m = sharing_entry_get_media(entry); m != NULL; m = g_slist_next(m)) {
159       result = twitpic_upload_media(sharing_account_get_username(account),
160                                     sharing_account_get_password(account),
161                                     m->data);
162       if (result != 0) {
163           break;
164       }
165   }
166
167   curl_global_cleanup();
168   return result;
169 }
170