Remove old patches
Tim Dengel
6 years ago
0 | From: vinszent <vinszent@vinszent.com> | |
1 | Date: Sat, 22 Apr 2017 21:58:05 +0200 | |
2 | Subject: Download resources on separate threads | |
3 | ||
4 | Fixes #260 | |
5 | --- | |
6 | src/gt-resource-downloader.c | 44 +++++++++++++++++++++++++++++++++----------- | |
7 | 1 file changed, 33 insertions(+), 11 deletions(-) | |
8 | ||
9 | diff --git a/src/gt-resource-downloader.c b/src/gt-resource-downloader.c | |
10 | index afb223a..30d734e 100644 | |
11 | --- a/src/gt-resource-downloader.c | |
12 | +++ b/src/gt-resource-downloader.c | |
13 | @@ -22,8 +22,11 @@ typedef struct | |
14 | gpointer udata; | |
15 | GtResourceDownloader* self; | |
16 | SoupMessage* msg; | |
17 | + GInputStream* istream; | |
18 | } ResourceData; /* FIXME: Better name? */ | |
19 | ||
20 | +static GThreadPool* dl_pool; | |
21 | + | |
22 | G_DEFINE_TYPE_WITH_PRIVATE(GtResourceDownloader, gt_resource_downloader, G_TYPE_OBJECT); | |
23 | ||
24 | static ResourceData* | |
25 | @@ -41,6 +44,7 @@ resource_data_free(ResourceData* data) | |
26 | g_free(data->name); | |
27 | g_object_unref(data->self); | |
28 | g_object_unref(data->msg); | |
29 | + g_object_unref(data->istream); | |
30 | ||
31 | g_slice_free(ResourceData, data); | |
32 | } | |
33 | @@ -154,6 +158,25 @@ download_image(GtResourceDownloader* self, | |
34 | } | |
35 | ||
36 | static void | |
37 | +download_cb(ResourceData* data, | |
38 | + gpointer udata) | |
39 | +{ | |
40 | + RETURN_IF_FAIL(data != NULL); | |
41 | + | |
42 | + g_autoptr(GdkPixbuf) ret = NULL; | |
43 | + g_autoptr(GError) err = NULL; | |
44 | + gboolean from_file = FALSE; | |
45 | + | |
46 | + ret = download_image(data->self, data->uri, data->name, data->msg, | |
47 | + data->istream, &from_file, &err); | |
48 | + | |
49 | + data->cb(from_file ? NULL : g_steal_pointer(&ret), | |
50 | + data->udata, g_steal_pointer(&err)); | |
51 | + | |
52 | + resource_data_free(data); | |
53 | +} | |
54 | + | |
55 | +static void | |
56 | send_message_cb(GObject* source, | |
57 | GAsyncResult* res, gpointer udata) | |
58 | { | |
59 | @@ -162,23 +185,19 @@ send_message_cb(GObject* source, | |
60 | RETURN_IF_FAIL(udata != NULL); | |
61 | ||
62 | ResourceData* data = udata; | |
63 | + GtResourceDownloader* self = GT_RESOURCE_DOWNLOADER(data->self); | |
64 | + GtResourceDownloaderPrivate* priv = gt_resource_downloader_get_instance_private(self); | |
65 | g_autoptr(GError) err = NULL; | |
66 | - g_autoptr(GInputStream) istream = NULL; | |
67 | - g_autoptr(GdkPixbuf) ret = NULL; | |
68 | - gboolean from_file = FALSE; | |
69 | ||
70 | - istream = soup_session_send_finish(SOUP_SESSION(source), res, &err); | |
71 | + data->istream = soup_session_send_finish(SOUP_SESSION(source), res, &err); | |
72 | ||
73 | if (!err) | |
74 | + g_thread_pool_push(dl_pool, data, NULL); | |
75 | + else | |
76 | { | |
77 | - ret = download_image(data->self, data->uri, data->name, | |
78 | - data->msg, istream, &from_file, &err); | |
79 | + data->cb(NULL, data->udata, g_steal_pointer(&err)); | |
80 | + resource_data_free(data); | |
81 | } | |
82 | - | |
83 | - data->cb(from_file ? NULL : g_steal_pointer(&ret), | |
84 | - data->udata, g_steal_pointer(&err)); | |
85 | - | |
86 | - resource_data_free(data); | |
87 | } | |
88 | ||
89 | static void | |
90 | @@ -214,6 +233,8 @@ gt_resource_downloader_class_init(GtResourceDownloaderClass* klass) | |
91 | { | |
92 | G_OBJECT_CLASS(klass)->finalize = finalize; | |
93 | G_OBJECT_CLASS(klass)->dispose = dispose; | |
94 | + | |
95 | + dl_pool = g_thread_pool_new((GFunc) download_cb, NULL, g_get_num_processors(), FALSE, NULL); | |
96 | } | |
97 | ||
98 | static void | |
99 | @@ -289,6 +310,7 @@ gt_resource_downloader_download_image(GtResourceDownloader* self, | |
100 | } | |
101 | ||
102 | ret = download_image(self, uri, name, msg, istream, NULL, error); | |
103 | + | |
104 | return g_steal_pointer(&ret); | |
105 | } | |
106 |