add whois implementation
[omnplay] / 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 else
60 r = 1;
61
62 return r;
63 };
64
65 static int post_file(char* url, char* file_in, char* file_out, char* curl_error_msg)
66 {
67 int r = 0;
68 FILE* f;
69 CURL *curl;
70 long http_responce = 0;
71 struct curl_httppost* post = NULL;
72 struct curl_httppost* last = NULL;
73
74 /* open out file */
75 f = fopen(file_out, "wt");
76 if(!f)
77 {
78 snprintf(curl_error_msg, CURL_ERROR_SIZE, "failed to create output file [%s]", file_out);
79 return -1;
80 };
81
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);
91
92 curl_formadd(&post, &last,
93 CURLFORM_COPYNAME, "src_file",
94 CURLFORM_FILE, file_in,
95 CURLFORM_END);
96
97 /* Set the form info */
98 curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
99
100 /* POST data to server */
101 r = curl_easy_perform(curl);
102
103 /* close file */
104 fclose(f);
105
106 /* check results */
107 if(!r)
108 {
109 curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &http_responce);
110 if(200 != http_responce)
111 {
112 snprintf(curl_error_msg, CURL_ERROR_SIZE, "FAILED [http_responce=%ld]", http_responce);
113 r = -2;
114 }
115 };
116
117 /* always cleanup */
118 curl_easy_cleanup(curl);
119 curl_formfree(post);
120
121 return r;
122 };
123
124 int omnplay_whois_list(omnplay_instance_t* app, playlist_item_t *items, int* pcount)
125 {
126 int r;
127 char *filenames[2];
128 char *curl_error_msg;
129
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);
134
135 /* compose filenames */
136 #ifdef _WIN32
137 snprintf(filenames[0], PATH_MAX, "%s\\omnplay.whois.in", getenv("TEMP"));
138 snprintf(filenames[1], PATH_MAX, "%s\\omnplay.whois.out", getenv("TEMP"));
139 #else
140 snprintf(filenames[0], PATH_MAX, "%s/omnplay.whois.in", getenv("HOME"));
141 snprintf(filenames[1], PATH_MAX, "%s/omnplay.whois.out", getenv("HOME"));
142 #endif
143
144 r = save_list(items, *pcount, filenames[0]);
145
146 if(r)
147 fprintf(stderr, "Failed to save list to [%s]\n", filenames[0]);
148 else
149 {
150 r = post_file(app->library.whois, filenames[0], filenames[1], curl_error_msg);
151 if(r)
152 fprintf(stderr, "Failed to whois: {%s}\n", curl_error_msg);
153 else
154 r = omnplay_library_load_file(items, pcount, filenames[1]);
155 };
156
157 free(filenames[0]);
158 free(filenames[1]);
159 free(curl_error_msg);
160
161 return r;
162 };
163