summaryrefslogtreecommitdiffstats
path: root/src/gs-vendor.c
blob: bc4bc27aa7670d98fa146f72f62bf08539373cf6 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 * vi:set noexpandtab tabstop=8 shiftwidth=8:
 *
 * Copyright (C) 2008 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2015 Kalev Lember <klember@redhat.com>
 *
 * SPDX-License-Identifier: GPL-2.0+
 */

#include "config.h"

#include "gs-vendor.h"

struct _GsVendor
{
	GObject				  parent_instance;

	GKeyFile			 *file;
};

G_DEFINE_TYPE (GsVendor, gs_vendor, G_TYPE_OBJECT)

#ifdef HAVE_PACKAGEKIT
static const gchar *
gs_vendor_type_to_string (GsVendorUrlType type)
{
	if (type == GS_VENDOR_URL_TYPE_CODEC)
		return "CodecUrl";
	if (type == GS_VENDOR_URL_TYPE_FONT)
		return "FontUrl";
	if (type == GS_VENDOR_URL_TYPE_MIME)
		return "MimeUrl";
	if (type == GS_VENDOR_URL_TYPE_HARDWARE)
		return "HardwareUrl";
	return "DefaultUrl";
}
#endif

gchar *
gs_vendor_get_not_found_url (GsVendor *vendor, GsVendorUrlType type)
{
#ifdef HAVE_PACKAGEKIT
	const gchar *key;
	gchar *url = NULL;

	/* get data */
	key = gs_vendor_type_to_string (type);
	url = g_key_file_get_string (vendor->file, "PackagesNotFound", key, NULL);

	/* none is a special value */
	if (g_strcmp0 (url, "none") == 0) {
		g_free (url);
		url = NULL;
	}

	/* got a valid URL */
	if (url != NULL)
		goto out;

	/* default has no fallback */
	if (type == GS_VENDOR_URL_TYPE_DEFAULT)
		goto out;

	/* get fallback data */
	g_debug ("using fallback");
	key = gs_vendor_type_to_string (GS_VENDOR_URL_TYPE_DEFAULT);
	url = g_key_file_get_string (vendor->file, "PackagesNotFound", key, NULL);

	/* none is a special value */
	if (g_strcmp0 (url, "none") == 0) {
		g_free (url);
		url = NULL;
	}
out:
	g_debug ("url=%s", url);
	return url;
#else
	return NULL;
#endif
}

static void
gs_vendor_init (GsVendor *vendor)
{
#ifdef HAVE_PACKAGEKIT
	g_autoptr(GError) local_error = NULL;
	const gchar *fn = "/etc/PackageKit/Vendor.conf";
	gboolean ret;

	vendor->file = g_key_file_new ();
	ret = g_key_file_load_from_file (vendor->file, fn, G_KEY_FILE_NONE, &local_error);
	if (!ret && local_error && !g_error_matches (local_error, G_FILE_ERROR, G_FILE_ERROR_NOENT))
		g_warning ("Failed to read '%s': %s", fn, local_error->message);
#endif
}

static void
gs_vendor_finalize (GObject *object)
{
	GsVendor *vendor = GS_VENDOR (object);

	if (vendor->file != NULL)
		g_key_file_free (vendor->file);

	G_OBJECT_CLASS (gs_vendor_parent_class)->finalize (object);
}

static void
gs_vendor_class_init (GsVendorClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = gs_vendor_finalize;
}

/**
 * gs_vendor_new:
 *
 * Return value: a new GsVendor object.
 **/
GsVendor *
gs_vendor_new (void)
{
	GsVendor *vendor;
	vendor = g_object_new (GS_TYPE_VENDOR, NULL);
	return GS_VENDOR (vendor);
}