Correct the use of pthread_create() in several places.
[claws.git] / src / plugins / libravatar / libravatar_image.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2016 Ricardo Mones and the Claws Mail Team
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 3 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, see <http://www.gnu.org/licenses/>.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #  include "config.h"
21 #include "claws-features.h"
22 #endif
23
24 #include <glib.h>
25 #include <curl/curl.h>
26 #include <pthread.h>
27
28 #include <common/claws.h>
29 #include <prefs_common.h>
30
31 #include "libravatar.h"
32 #include "libravatar_prefs.h"
33 #include "libravatar_missing.h"
34 #include "libravatar_image.h"
35
36 static size_t write_image_data_cb(void *ptr, size_t size, size_t nmemb, void *stream)
37 {
38         size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
39         debug_print("received %zu bytes from avatar server\n", written);
40
41         return written;
42 }
43
44 static GdkPixbuf *image_pixbuf_from_filename(const gchar *filename)
45 {
46         GdkPixbuf *picture = NULL;
47         GError *error = NULL;
48         gint w, h;
49
50         gdk_pixbuf_get_file_info(filename, &w, &h);
51
52         if (w != AVATAR_SIZE || h != AVATAR_SIZE)
53                 /* server can provide a different size from the requested in URL */
54                 picture = gdk_pixbuf_new_from_file_at_scale(
55                                 filename, AVATAR_SIZE, AVATAR_SIZE, TRUE, &error);
56         else    /* exact size */
57                 picture = gdk_pixbuf_new_from_file(filename, &error);
58
59         if (error != NULL) {
60                 g_warning("failed to load image '%s': %s", filename, error->message);
61                 g_error_free(error);
62         } else {
63                 if (!picture)
64                         g_warning("failed to load image '%s': no error returned!", filename);
65         }
66
67         return picture;
68 }
69
70 static GdkPixbuf *pixbuf_from_url(const gchar *url, const gchar *md5, const gchar *filename) {
71         GdkPixbuf *image = NULL;
72         FILE *file;
73         CURL *curl;
74         long filesize;
75
76         file = fopen(filename, "wb");
77         if (file == NULL) {
78                 g_warning("could not open '%s' for writing", filename);
79                 return NULL;
80         }
81         curl = curl_easy_init();
82         if (curl == NULL) {
83                 g_warning("could not initialize curl to get image from URL");
84                 fclose(file);
85                 return NULL;
86         }
87
88         curl_easy_setopt(curl, CURLOPT_URL, url);
89         curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_image_data_cb);
90         /* make sure timeout is less than general IO timeout */
91         curl_easy_setopt(curl, CURLOPT_TIMEOUT,
92                         (libravatarprefs.timeout == 0
93                                 || libravatarprefs.timeout
94                                         > prefs_common_get_prefs()->io_timeout_secs)
95                         ? prefs_common_get_prefs()->io_timeout_secs
96                         : libravatarprefs.timeout);
97         curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
98
99         if (libravatarprefs.allow_redirects) {
100                 long maxredirs = (libravatarprefs.default_mode == DEF_MODE_URL)? 3L
101                         : ((libravatarprefs.default_mode == DEF_MODE_MM)? 2L: 1L);
102                 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
103                 curl_easy_setopt(curl, CURLOPT_MAXREDIRS, maxredirs);
104         }
105         curl_easy_setopt(curl, CURLOPT_FILE, file);
106         debug_print("retrieving URL to file: %s -> %s\n", url, filename);
107         curl_easy_perform(curl);
108         filesize = ftell(file);
109         fclose(file);
110         if (filesize < MIN_PNG_SIZE)
111                 debug_print("not enough data for an avatar image: %ld bytes\n", filesize);
112         else
113                 image = image_pixbuf_from_filename(filename);
114
115         if (!libravatarprefs.cache_icons || filesize == 0) {
116                 if (g_unlink(filename) < 0)
117                         g_warning("failed to delete cache file '%s'", filename);
118         }
119
120         if (filesize == 0)
121                 missing_add_md5(libravatarmisses, md5);
122
123         curl_easy_cleanup(curl);
124
125         return image;
126 }
127
128 static void *get_image_thread(void *arg) {
129         AvatarImageFetch *ctx = (AvatarImageFetch *)arg;
130
131         /* get image */
132         ctx->pixbuf = pixbuf_from_url(ctx->url, ctx->md5, ctx->filename);
133         /* done here */
134         ctx->ready = TRUE;
135
136         return arg;
137 }
138
139 GdkPixbuf *libravatar_image_fetch(AvatarImageFetch *ctx)
140 {
141 #ifdef USE_PTHREAD
142         pthread_t pt;
143 #endif
144
145         g_return_val_if_fail(ctx != NULL, NULL);
146
147 #ifdef USE_PTHREAD
148         if (pthread_create(&pt, NULL, get_image_thread, (void *)ctx) != 0) {
149                 debug_print("synchronous image fetching (couldn't create thread)\n");
150                 get_image_thread(ctx);
151         } else {
152                 debug_print("waiting for thread completion\n");
153                 /*
154                 while (!ctx->ready ) {
155                         claws_do_idle();
156                 }
157                 */
158                 pthread_join(pt, NULL);
159                 debug_print("thread completed\n");
160         }
161 #else
162         debug_print("synchronous image fetching (pthreads unavailable)\n");
163         get_image_thread(ctx);
164 #endif
165         if (ctx->pixbuf == NULL) {
166                 g_warning("could not get image");
167         }
168         return ctx->pixbuf;
169 }