fix README
[melted_gui] / src / whois.c
1 /*
2 * whois.c -- GTK+ 2 omnplay
3 * Copyright (C) 2011 Maksym Veremeyenko <verem@m1stereo.tv>
4 *
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.
9 *
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.
14 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <gtk/gtk.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <pthread.h>
30 #include "curl/curl.h"
31
32 #include "omnplay.h"
33 #include "ui.h"
34 #include "timecode.h"
35
36
37 static size_t upload_cb_write(void *ptr, size_t size, size_t nmemb, void *stream)
38 {
39 return fwrite(ptr, size, nmemb, (FILE*)stream);
40 };
41
42 static int save_list(playlist_item_t* item, int count, char* filename)
43 {
44 int i, r = 0;
45 FILE* f;
46
47 if((f = fopen(filename, "wt")))
48 {
49 char tc_in[32], tc_dur[32];
50
51 for(i = 0; i < count; i++)
52 fprintf(f, "%-40s%-15s%s\n",
53 item[i].id,
54 frames2tc(item[i].in, 25.0, tc_in),
55 frames2tc(item[i].dur, 25.0, tc_dur));
56
57 fclose(f);
58
59 g_warning("whois.c: save_list: filename=[%s], count=[%d]\n", filename, count);
60 }
61 else
62 r = 1;
63
64 return r;
65 };
66
67 static int post_file(char* url, char* file_in, char* file_out, char* curl_error_msg)
68 {
69 int r = 0;
70 FILE* f;
71 CURL *curl;
72 long http_responce = 0;
73 struct curl_httppost* post = NULL;
74 struct curl_httppost* last = NULL;
75
76 /* open out file */
77 f = fopen(file_out, "wt");
78 if(!f)
79 {
80 snprintf(curl_error_msg, CURL_ERROR_SIZE, "failed to create output file [%s]", file_out);
81 return -1;
82 };
83
84 /* prepare CURL to HTTP GET request*/
85 curl = curl_easy_init();
86 curl_easy_setopt(curl, CURLOPT_URL, url);
87 curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
88 curl_easy_setopt(curl, CURLOPT_NOSIGNAL , 1);
89 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, upload_cb_write);
90 curl_easy_setopt(curl, CURLOPT_WRITEDATA, f);
91 curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, curl_error_msg);
92 curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
93
94 curl_formadd(&post, &last,
95 CURLFORM_COPYNAME, "src_file",
96 CURLFORM_FILE, file_in,
97 CURLFORM_END);
98
99 /* Set the form info */
100 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
101
102 /* POST data to server */
103 r = curl_easy_perform(curl);
104
105 /* close file */
106 fclose(f);
107
108 /* check results */
109 if(!r)
110 {
111 curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_responce);
112 if(200 != http_responce)
113 {
114 snprintf(curl_error_msg, CURL_ERROR_SIZE, "FAILED [http_responce=%ld]", http_responce);
115 r = -2;
116 }
117 };
118
119 /* always cleanup */
120 curl_easy_cleanup(curl);
121 curl_formfree(post);
122
123 return r;
124 };
125
126 int omnplay_whois_list(omnplay_instance_t* app, playlist_item_t *items, int* pcount)
127 {
128 int r;
129 char *filenames[2];
130 char *curl_error_msg;
131
132 /* alloc filenames */
133 filenames[0] = (char *)malloc(PATH_MAX);
134 filenames[1] = (char *)malloc(PATH_MAX);
135 curl_error_msg = (char*)malloc(CURL_ERROR_SIZE);
136
137 /* compose filenames */
138 #ifdef _WIN32
139 snprintf(filenames[0], PATH_MAX, "%s\\omnplay.whois.in", getenv("TEMP"));
140 snprintf(filenames[1], PATH_MAX, "%s\\omnplay.whois.out", getenv("TEMP"));
141 #else
142 snprintf(filenames[0], PATH_MAX, "%s/omnplay.whois.in", getenv("HOME"));
143 snprintf(filenames[1], PATH_MAX, "%s/omnplay.whois.out", getenv("HOME"));
144 #endif
145
146 r = save_list(items, *pcount, filenames[0]);
147
148 if(r)
149 g_warning("Failed to save list to [%s]\n", filenames[0]);
150 else
151 {
152 r = post_file(app->library.whois, filenames[0], filenames[1], curl_error_msg);
153 if(r)
154 g_warning("Failed to whois: {%s}\n", curl_error_msg);
155 else
156 r = omnplay_library_load_file(items, pcount, filenames[1]);
157 };
158
159 free(filenames[0]);
160 free(filenames[1]);
161 free(curl_error_msg);
162
163 return r;
164 };
165