summaryrefslogtreecommitdiff
path: root/src/backend/plugins/download-soup/download-soup.c
blob: 905f173922ec55b5556f33591cd3df1fc448830e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/* download-soup.c
 *
 * Copyright 2022-2023 Jan-Michael Brummer
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include <libpeas/peas.h>
#include <libsoup/soup.h>

#include "download-soup.h"

#include "px-plugin-download.h"
#include "px-manager.h"

static void px_download_iface_init (PxDownloadInterface *iface);
G_MODULE_EXPORT void peas_register_types (PeasObjectModule *module);

G_DEFINE_FINAL_TYPE_WITH_CODE (PxDownloadSoup,
                               px_download_soup,
                               G_TYPE_OBJECT,
                               G_IMPLEMENT_INTERFACE (PX_TYPE_DOWNLOAD, px_download_iface_init))

static void
px_download_soup_init (PxDownloadSoup *self)
{
  self->session = soup_session_new ();
}

static void
px_download_soup_class_init (PxDownloadSoupClass *klass)
{
}

static GBytes *
px_download_soup_download (PxDownload *download,
                           const char *uri)
{
  PxDownloadSoup *self = PX_DOWNLOAD_SOUP (download);
  g_autoptr (SoupMessage) msg = soup_message_new (SOUP_METHOD_GET, uri);
  g_autoptr (GError) error = NULL;
  g_autoptr (GBytes) bytes = NULL;

  bytes = soup_session_send_and_read (
    self->session,
    msg,
    NULL,     /* Pass a GCancellable here if you want to cancel a download */
    &error);
  if (!bytes || soup_message_get_status (msg) != SOUP_STATUS_OK) {
    g_debug ("Failed to download: %s\n", error ? error->message : "");
    return NULL;
  }

  return g_steal_pointer (&bytes);
}

static void
px_download_iface_init (PxDownloadInterface *iface)
{
  iface->download = px_download_soup_download;
}

G_MODULE_EXPORT void
peas_register_types (PeasObjectModule *module)
{
  peas_object_module_register_extension_type (module,
                                              PX_TYPE_DOWNLOAD,
                                              PX_DOWNLOAD_TYPE_SOUP);
}