libpurple/purpletags.c

Tue, 21 Mar 2023 00:39:45 -0500

author
Elliott Sales de Andrade <quantum.analyst@gmail.com>
date
Tue, 21 Mar 2023 00:39:45 -0500
changeset 42172
7c2d151b410d
parent 41960
c8a4853205e3
child 42238
48bb3244b5ea
permissions
-rw-r--r--

Use g_clear_* helpers where useful

That is:

* when the variable is set to `NULL` right after freeing
* when the variable is checked for non-`NULL` before freeing
* when the variable is a global (because they should be set to `NULL`, even if we don't really claim that things can be re-init'd)

Testing Done:
Compiled, and ran tests in valgrind.

Reviewed at https://reviews.imfreedom.org/r/2369/

41736
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
1 /*
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
2 * Purple - Internet Messaging Library
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
3 * Copyright (C) Pidgin Developers <devel@pidgin.im>
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
4 *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
5 * Purple is the legal property of its developers, whose names are too numerous
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
6 * to list here. Please refer to the COPYRIGHT file distributed with this
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
7 * source distribution.
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
8 *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
9 * This program is free software; you can redistribute it and/or modify
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
10 * it under the terms of the GNU General Public License as published by
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
11 * the Free Software Foundation; either version 2 of the License, or
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
12 * (at your option) any later version.
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
13 *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful,
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
17 * GNU General Public License for more details.
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
18 *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
19 * You should have received a copy of the GNU General Public License
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
20 * along with this program; if not, see <https://www.gnu.org/licenses/>.
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
21 */
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
22
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
23 #include "purpletags.h"
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
24
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
25 #include "util.h"
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
26
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
27 struct _PurpleTags {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
28 GObject parent;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
29
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
30 GList *tags;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
31 };
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
32
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
33 G_DEFINE_TYPE(PurpleTags, purple_tags, G_TYPE_OBJECT)
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
34
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
35 /******************************************************************************
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
36 * GObject Implementation
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
37 *****************************************************************************/
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
38
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
39 static void
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
40 purple_tags_dispose(GObject *obj) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
41 PurpleTags *tags = PURPLE_TAGS(obj);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
42
42172
7c2d151b410d Use g_clear_* helpers where useful
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 41960
diff changeset
43 g_clear_list(&tags->tags, g_free);
41736
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
44
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
45 G_OBJECT_CLASS(purple_tags_parent_class)->dispose(obj);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
46 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
47
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
48 static void
41960
c8a4853205e3 Bump C standard to C99 for libpurple files and fix warnings
Elliott Sales de Andrade <quantum.analyst@gmail.com>
parents: 41945
diff changeset
49 purple_tags_init(G_GNUC_UNUSED PurpleTags *tags) {
41736
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
50 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
51
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
52 static void
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
53 purple_tags_class_init(PurpleTagsClass *klass) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
54 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
55
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
56 obj_class->dispose = purple_tags_dispose;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
57 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
58
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
59 /******************************************************************************
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
60 * Public API
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
61 *****************************************************************************/
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
62 PurpleTags *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
63 purple_tags_new(void) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
64 return g_object_new(PURPLE_TYPE_TAGS, NULL);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
65 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
66
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
67 const gchar *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
68 purple_tags_lookup(PurpleTags *tags, const gchar *name, gboolean *found) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
69 size_t name_len = 0;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
70
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
71 g_return_val_if_fail(PURPLE_IS_TAGS(tags), FALSE);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
72 g_return_val_if_fail(name != NULL, FALSE);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
73
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
74 /* Assume we're going to find the tag, if we don't we set found to false
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
75 * before we return. This sounds silly, but it saves some additional logic
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
76 * below.
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
77 */
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
78 if(found) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
79 *found = TRUE;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
80 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
81
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
82 name_len = strlen(name);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
83
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
84 for(GList *l = tags->tags; l != NULL; l = l->next) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
85 const gchar *tag = l->data;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
86
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
87 if(g_str_has_prefix(tag, name)) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
88 const gchar *value = tag + name_len;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
89
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
90 if(*value == '\0') {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
91 return NULL;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
92 } else if(*value == ':') {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
93 return value+1;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
94 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
95 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
96 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
97
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
98 /* We didn't find the tag, so set found to false if necessary. */
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
99 if(found) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
100 *found = FALSE;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
101 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
102
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
103 return NULL;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
104 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
105
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
106 const gchar *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
107 purple_tags_get(PurpleTags *tags, const gchar *name) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
108 g_return_val_if_fail(PURPLE_IS_TAGS(tags), NULL);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
109 g_return_val_if_fail(name != NULL, NULL);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
110
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
111 return purple_tags_lookup(tags, name, NULL);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
112 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
113
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
114 void
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
115 purple_tags_add(PurpleTags *tags, const gchar *tag) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
116 g_return_if_fail(PURPLE_IS_TAGS(tags));
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
117 g_return_if_fail(tag != NULL);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
118
41908
9d2735c0e848 Don't allow duplicate tags in PurpleTags
Gary Kramlich <grim@reaperworld.com>
parents: 41892
diff changeset
119 /* Remove any existing tags with this value. */
9d2735c0e848 Don't allow duplicate tags in PurpleTags
Gary Kramlich <grim@reaperworld.com>
parents: 41892
diff changeset
120 purple_tags_remove(tags, tag);
9d2735c0e848 Don't allow duplicate tags in PurpleTags
Gary Kramlich <grim@reaperworld.com>
parents: 41892
diff changeset
121
9d2735c0e848 Don't allow duplicate tags in PurpleTags
Gary Kramlich <grim@reaperworld.com>
parents: 41892
diff changeset
122 /* Add the new tag. */
41736
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
123 tags->tags = g_list_append(tags->tags, g_strdup(tag));
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
124 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
125
41945
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
126 void
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
127 purple_tags_add_with_value(PurpleTags *tags, const char *name,
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
128 const char *value)
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
129 {
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
130 char *tag = NULL;
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
131
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
132 g_return_if_fail(PURPLE_IS_TAGS(tags));
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
133 g_return_if_fail(name != NULL);
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
134
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
135 if(value != NULL) {
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
136 tag = g_strdup_printf("%s:%s", name, value);
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
137 } else {
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
138 tag = g_strdup(name);
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
139 }
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
140
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
141 purple_tags_add(tags, tag);
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
142
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
143 g_free(tag);
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
144 }
fd826d652e7d Add purple_tags_add_with_value to help people not have to construct their own tags
Gary Kramlich <grim@reaperworld.com>
parents: 41908
diff changeset
145
41736
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
146 gboolean
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
147 purple_tags_remove(PurpleTags *tags, const gchar *tag) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
148 g_return_val_if_fail(PURPLE_IS_TAGS(tags), FALSE);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
149 g_return_val_if_fail(tag != NULL, FALSE);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
150
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
151 for(GList *l = tags->tags; l != NULL; l = l->next) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
152 gchar *etag = l->data;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
153
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
154 if(purple_strequal(etag, tag)) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
155 g_free(etag);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
156 tags->tags = g_list_delete_link(tags->tags, l);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
157
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
158 return TRUE;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
159 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
160 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
161
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
162 return FALSE;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
163 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
164
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
165 guint
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
166 purple_tags_get_count(PurpleTags *tags) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
167 g_return_val_if_fail(PURPLE_IS_TAGS(tags), 0);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
168
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
169 return g_list_length(tags->tags);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
170 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
171
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
172 GList *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
173 purple_tags_get_all(PurpleTags *tags) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
174 g_return_val_if_fail(PURPLE_IS_TAGS(tags), NULL);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
175
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
176 return tags->tags;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
177 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
178
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
179 gchar *
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
180 purple_tags_to_string(PurpleTags *tags, const gchar *separator) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
181 GString *value = NULL;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
182
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
183 g_return_val_if_fail(PURPLE_IS_TAGS(tags), NULL);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
184
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
185 value = g_string_new("");
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
186
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
187 for(GList *l = tags->tags; l != NULL; l = l->next) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
188 const gchar *tag = l->data;
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
189
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
190 g_string_append(value, tag);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
191
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
192 if(separator != NULL && l->next != NULL) {
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
193 g_string_append(value, separator);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
194 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
195 }
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
196
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
197 return g_string_free(value, FALSE);
d78c0951ea2c Create the PurpleTags object for handling tags
Gary Kramlich <grim@reaperworld.com>
parents:
diff changeset
198 }
41892
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
199
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
200 void
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
201 purple_tag_parse(const char *tag, char **name, char **value) {
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
202 const char *colon = NULL;
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
203
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
204 g_return_if_fail(tag != NULL);
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
205
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
206 colon = g_strstr_len(tag, -1, ":");
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
207 if(colon == NULL) {
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
208 if(name != NULL) {
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
209 *name = g_strdup(tag);
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
210 }
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
211 if(value != NULL) {
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
212 *value = NULL;
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
213 }
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
214 } else {
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
215 if(name != NULL) {
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
216 *name = g_strndup(tag, colon - tag);
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
217 }
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
218 if(value != NULL) {
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
219 *value = g_strdup(colon + 1);
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
220 }
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
221 }
39e595f7d056 Add purple_tag_parse for parsing tags
Gary Kramlich <grim@reaperworld.com>
parents: 41736
diff changeset
222 }

mercurial