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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2007 Andreas Obergrusberger <tradiaz@yahoo.de>
* Copyright (C) 2008-2010 Valeriy Lyasotskiy <onestep@ukr.net>
* Copyright (C) 2010-2011 Jonathan Conder <jonno.conder@gmail.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <gio/gio.h>
#include <string.h>
#include "pk-backend-groups.h"
static GHashTable *grps = NULL;
static PkBitfield groups = 0;
static GHashTable *
group_map_new (GError **error)
{
GHashTable *map;
GFile *file;
GFileInputStream *is;
GDataInputStream *input;
GError *e = NULL;
g_debug ("reading group map from %s", PK_BACKEND_GROUP_FILE);
file = g_file_new_for_path (PK_BACKEND_GROUP_FILE);
is = g_file_read (file, NULL, &e);
if (is == NULL) {
g_object_unref (file);
g_propagate_error (error, e);
return NULL;
}
map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
input = g_data_input_stream_new (G_INPUT_STREAM (is));
/* read groups line by line, ignoring comments */
while (TRUE) {
PkGroupEnum group;
gchar *key, *value;
value = g_data_input_stream_read_line (input, NULL, NULL, &e);
if (value != NULL) {
g_strstrip (value);
} else {
break;
}
if (*value == '\0' || *value == '#') {
g_free (value);
continue;
}
/* line format: grp (space|tab)+ group */
key = strsep (&value, " ");
g_strchomp (key);
if (value == NULL) {
/* safe to cast as it is never freed or modified */
value = (gchar *) "other";
group = PK_GROUP_ENUM_OTHER;
} else {
g_strchug (value);
group = pk_group_enum_from_string (value);
}
if (group != PK_GROUP_ENUM_UNKNOWN) {
/* key and value are allocated together */
g_hash_table_replace (map, key, value);
pk_bitfield_add (groups, group);
}
}
g_object_unref (input);
g_object_unref (is);
g_object_unref (file);
if (e != NULL) {
g_hash_table_unref (map);
g_propagate_error (error, e);
return NULL;
} else {
return map;
}
}
gboolean
pk_backend_initialize_groups (PkBackend *self, GError **error)
{
g_return_val_if_fail (self != NULL, FALSE);
grps = group_map_new (error);
return (grps != NULL);
}
void
pk_backend_destroy_groups (PkBackend *self)
{
g_return_if_fail (self != NULL);
if (grps != NULL) {
g_hash_table_unref (grps);
}
}
const gchar *
alpm_pkg_get_group (pmpkg_t *pkg)
{
const alpm_list_t *i;
g_return_val_if_fail (pkg != NULL, NULL);
g_return_val_if_fail (grps != NULL, NULL);
/* use the first group that we recognise */
for (i = alpm_pkg_get_groups (pkg); i != NULL; i = i->next) {
gpointer value = g_hash_table_lookup (grps, i->data);
if (value != NULL) {
return (const gchar *) value;
}
}
return "other";
}
PkBitfield
pk_backend_get_groups (PkBackend *self)
{
g_return_val_if_fail (self != NULL, 0);
return groups;
}
|