2 * whois.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 Maksym Veremeyenko <verem@m1stereo.tv>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include <gdk/gdkkeysyms.h>
30 #include "curl/curl.h"
37 static size_t upload_cb_write(void *ptr
, size_t size
, size_t nmemb
, void *stream
)
39 return fwrite(ptr
, size
, nmemb
, (FILE*)stream
);
42 static int save_list(playlist_item_t
* item
, int count
, char* filename
)
47 if((f
= fopen(filename
, "wt")))
49 char tc_in
[32], tc_dur
[32];
51 for(i
= 0; i
< count
; i
++)
52 fprintf(f
, "%-40s%-15s%s\n",
54 frames2tc(item
[i
].in
, 25.0, tc_in
),
55 frames2tc(item
[i
].dur
, 25.0, tc_dur
));
65 static int post_file(char* url
, char* file_in
, char* file_out
, char* curl_error_msg
)
70 long http_responce
= 0;
71 struct curl_httppost
* post
= NULL
;
72 struct curl_httppost
* last
= NULL
;
75 f
= fopen(file_out
, "wt");
78 snprintf(curl_error_msg
, CURL_ERROR_SIZE
, "failed to create output file [%s]", file_out
);
82 /* prepare CURL to HTTP GET request*/
83 curl
= curl_easy_init();
84 curl_easy_setopt(curl
, CURLOPT_URL
, url
);
85 curl_easy_setopt(curl
, CURLOPT_NOPROGRESS
, 1);
86 curl_easy_setopt(curl
, CURLOPT_NOSIGNAL
, 1);
87 curl_easy_setopt(curl
, CURLOPT_WRITEFUNCTION
, upload_cb_write
);
88 curl_easy_setopt(curl
, CURLOPT_WRITEDATA
, f
);
89 curl_easy_setopt(curl
, CURLOPT_ERRORBUFFER
, curl_error_msg
);
90 curl_easy_setopt(curl
, CURLOPT_FAILONERROR
, 1);
92 curl_formadd(&post
, &last
,
93 CURLFORM_COPYNAME
, "src_file",
94 CURLFORM_FILE
, file_in
,
97 /* Set the form info */
98 curl_easy_setopt(curl
, CURLOPT_HTTPPOST
, post
);
100 /* POST data to server */
101 r
= curl_easy_perform(curl
);
109 curl_easy_getinfo(curl
, CURLINFO_HTTP_CODE
, &http_responce
);
110 if(200 != http_responce
)
112 snprintf(curl_error_msg
, CURL_ERROR_SIZE
, "FAILED [http_responce=%ld]", http_responce
);
118 curl_easy_cleanup(curl
);
124 int omnplay_whois_list(omnplay_instance_t
* app
, playlist_item_t
*items
, int* pcount
)
128 char *curl_error_msg
;
130 /* alloc filenames */
131 filenames
[0] = (char *)malloc(PATH_MAX
);
132 filenames
[1] = (char *)malloc(PATH_MAX
);
133 curl_error_msg
= (char*)malloc(CURL_ERROR_SIZE
);
135 /* compose filenames */
137 snprintf(filenames
[0], PATH_MAX
, "%s\\omnplay.whois.in", getenv("TEMP"));
138 snprintf(filenames
[1], PATH_MAX
, "%s\\omnplay.whois.out", getenv("TEMP"));
140 snprintf(filenames
[0], PATH_MAX
, "%s/omnplay.whois.in", getenv("HOME"));
141 snprintf(filenames
[1], PATH_MAX
, "%s/omnplay.whois.out", getenv("HOME"));
144 r
= save_list(items
, *pcount
, filenames
[0]);
147 fprintf(stderr
, "Failed to save list to [%s]\n", filenames
[0]);
150 r
= post_file(app
->library
.whois
, filenames
[0], filenames
[1], curl_error_msg
);
152 fprintf(stderr
, "Failed to whois: {%s}\n", curl_error_msg
);
154 r
= omnplay_library_load_file(items
, pcount
, filenames
[1]);
159 free(curl_error_msg
);