| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- |
| 2 |
* |
| 3 |
* Copyright (C) 2010 Richard Hughes <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the GNU General Public License Version 2 |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <glib-object.h> |
| 25 |
|
| 26 |
#include "cd-common.h" |
| 27 |
#include "cd-profile-array.h" |
| 28 |
|
| 29 |
static void cd_profile_array_finalize (GObject *object); |
| 30 |
|
| 31 |
#define GET_PRIVATE(o) (cd_profile_array_get_instance_private (o)) |
| 32 |
|
| 33 |
typedef struct |
| 34 |
{ |
| 35 |
GPtrArray *array; |
| 36 |
} CdProfileArrayPrivate; |
| 37 |
|
| 38 |
G_DEFINE_TYPE_WITH_PRIVATE (CdProfileArray, cd_profile_array, G_TYPE_OBJECT) |
| 39 |
|
| 40 |
static gpointer cd_profile_array_object = NULL; |
| 41 |
|
| 42 |
void |
| 43 |
cd_profile_array_add (CdProfileArray *profile_array, CdProfile *profile) |
| 44 |
{ |
| 45 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 46 |
g_return_if_fail (CD_IS_PROFILE_ARRAY (profile_array)); |
| 47 |
g_return_if_fail (CD_IS_PROFILE (profile)); |
| 48 |
g_ptr_array_add (priv->array, g_object_ref (profile)); |
| 49 |
} |
| 50 |
|
| 51 |
void |
| 52 |
cd_profile_array_remove (CdProfileArray *profile_array, CdProfile *profile) |
| 53 |
{ |
| 54 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 55 |
g_return_if_fail (CD_IS_PROFILE_ARRAY (profile_array)); |
| 56 |
g_return_if_fail (CD_IS_PROFILE (profile)); |
| 57 |
g_ptr_array_remove (priv->array, profile); |
| 58 |
} |
| 59 |
|
| 60 |
CdProfile * |
| 61 |
cd_profile_array_get_by_id_owner (CdProfileArray *profile_array, |
| 62 |
const gchar *id, |
| 63 |
guint owner) |
| 64 |
{ |
| 65 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 66 |
CdProfile *profile_tmp; |
| 67 |
guint i; |
| 68 |
|
| 69 |
/* find profile */ |
| 70 |
for (i = 0; i < priv->array->len; i++) { |
| 71 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 72 |
if (cd_profile_get_owner (profile_tmp) != owner) |
| 73 |
continue; |
| 74 |
if (g_strcmp0 (cd_profile_get_id (profile_tmp), id) == 0) |
| 75 |
return g_object_ref (profile_tmp); |
| 76 |
} |
| 77 |
for (i = 0; i < priv->array->len; i++) { |
| 78 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 79 |
if (g_strcmp0 (cd_profile_get_id (profile_tmp), id) == 0) |
| 80 |
return g_object_ref (profile_tmp); |
| 81 |
} |
| 82 |
return NULL; |
| 83 |
} |
| 84 |
|
| 85 |
static CdProfile * |
| 86 |
cd_profile_array_get_by_basename (CdProfileArray *profile_array, |
| 87 |
const gchar *filename) |
| 88 |
{ |
| 89 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 90 |
CdProfile *profile = NULL; |
| 91 |
CdProfile *profile_tmp; |
| 92 |
const gchar *tmp; |
| 93 |
gchar *basename; |
| 94 |
guint i; |
| 95 |
|
| 96 |
/* find profile */ |
| 97 |
for (i = 0; i < priv->array->len && profile == NULL; i++) { |
| 98 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 99 |
tmp = cd_profile_get_filename (profile_tmp); |
| 100 |
if (tmp == NULL) |
| 101 |
continue; |
| 102 |
basename = g_path_get_basename (tmp); |
| 103 |
if (g_strcmp0 (basename, filename) == 0) |
| 104 |
profile = g_object_ref (profile_tmp); |
| 105 |
g_free (basename); |
| 106 |
} |
| 107 |
return profile; |
| 108 |
} |
| 109 |
|
| 110 |
CdProfile * |
| 111 |
cd_profile_array_get_by_filename (CdProfileArray *profile_array, |
| 112 |
const gchar *filename) |
| 113 |
{ |
| 114 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 115 |
CdProfile *profile_tmp; |
| 116 |
guint i; |
| 117 |
|
| 118 |
g_return_val_if_fail (filename != NULL, NULL); |
| 119 |
|
| 120 |
/* support getting the file without the path */ |
| 121 |
if (filename[0] != '/') |
| 122 |
return cd_profile_array_get_by_basename (profile_array, filename); |
| 123 |
|
| 124 |
/* find profile */ |
| 125 |
for (i = 0; i < priv->array->len; i++) { |
| 126 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 127 |
if (g_strcmp0 (cd_profile_get_filename (profile_tmp), filename) == 0) |
| 128 |
return g_object_ref (profile_tmp); |
| 129 |
} |
| 130 |
return NULL; |
| 131 |
} |
| 132 |
|
| 133 |
CdProfile * |
| 134 |
cd_profile_array_get_by_property (CdProfileArray *profile_array, |
| 135 |
const gchar *key, |
| 136 |
const gchar *value) |
| 137 |
{ |
| 138 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 139 |
CdProfile *profile_tmp; |
| 140 |
guint i; |
| 141 |
|
| 142 |
/* special case */ |
| 143 |
if (g_strcmp0 (key, CD_PROFILE_PROPERTY_FILENAME) == 0) |
| 144 |
return cd_profile_array_get_by_filename (profile_array, value); |
| 145 |
|
| 146 |
/* find profile */ |
| 147 |
for (i = 0; i < priv->array->len; i++) { |
| 148 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 149 |
if (g_strcmp0 (cd_profile_get_metadata_item (profile_tmp, key), value) == 0) |
| 150 |
return g_object_ref (profile_tmp); |
| 151 |
} |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
GPtrArray * |
| 156 |
cd_profile_array_get_by_kind (CdProfileArray *profile_array, |
| 157 |
CdProfileKind kind) |
| 158 |
{ |
| 159 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 160 |
CdProfile *profile_tmp; |
| 161 |
GPtrArray *array; |
| 162 |
guint i; |
| 163 |
|
| 164 |
/* find profile */ |
| 165 |
array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 166 |
for (i = 0; i < priv->array->len; i++) { |
| 167 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 168 |
if (cd_profile_get_kind (profile_tmp) == kind) |
| 169 |
g_ptr_array_add (array, g_object_ref (profile_tmp)); |
| 170 |
} |
| 171 |
return array; |
| 172 |
} |
| 173 |
|
| 174 |
GPtrArray * |
| 175 |
cd_profile_array_get_by_metadata (CdProfileArray *profile_array, |
| 176 |
const gchar *key, |
| 177 |
const gchar *value) |
| 178 |
{ |
| 179 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 180 |
CdProfile *profile_tmp; |
| 181 |
GPtrArray *array; |
| 182 |
GHashTable *hash_tmp; |
| 183 |
guint i; |
| 184 |
const gchar *value_tmp; |
| 185 |
|
| 186 |
/* find profile */ |
| 187 |
array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 188 |
for (i = 0; i < priv->array->len; i++) { |
| 189 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 190 |
hash_tmp = cd_profile_get_metadata (profile_tmp); |
| 191 |
value_tmp = g_hash_table_lookup (hash_tmp, key); |
| 192 |
if (g_strcmp0 (value_tmp, value) == 0) |
| 193 |
g_ptr_array_add (array, g_object_ref (profile_tmp)); |
| 194 |
} |
| 195 |
return array; |
| 196 |
} |
| 197 |
|
| 198 |
CdProfile * |
| 199 |
cd_profile_array_get_by_object_path (CdProfileArray *profile_array, |
| 200 |
const gchar *object_path) |
| 201 |
{ |
| 202 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 203 |
CdProfile *profile = NULL; |
| 204 |
CdProfile *profile_tmp; |
| 205 |
guint i; |
| 206 |
|
| 207 |
/* find profile */ |
| 208 |
for (i = 0; i < priv->array->len; i++) { |
| 209 |
profile_tmp = g_ptr_array_index (priv->array, i); |
| 210 |
if (g_strcmp0 (cd_profile_get_object_path (profile_tmp), object_path) == 0) |
| 211 |
return g_object_ref (profile_tmp); |
| 212 |
} |
| 213 |
return profile; |
| 214 |
} |
| 215 |
|
| 216 |
GVariant * |
| 217 |
cd_profile_array_get_variant (CdProfileArray *profile_array) |
| 218 |
{ |
| 219 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 220 |
CdProfile *profile; |
| 221 |
GVariant **variant_array = NULL; |
| 222 |
guint i; |
| 223 |
|
| 224 |
/* copy the object paths */ |
| 225 |
variant_array = g_new0 (GVariant *, priv->array->len + 1); |
| 226 |
for (i = 0; i < priv->array->len; i++) { |
| 227 |
profile = g_ptr_array_index (priv->array, i); |
| 228 |
variant_array[i] = g_variant_new_object_path (cd_profile_get_object_path (profile)); |
| 229 |
} |
| 230 |
|
| 231 |
/* format the value */ |
| 232 |
return g_variant_new_array (G_VARIANT_TYPE_OBJECT_PATH, |
| 233 |
variant_array, |
| 234 |
priv->array->len); |
| 235 |
} |
| 236 |
|
| 237 |
static void |
| 238 |
cd_profile_array_class_init (CdProfileArrayClass *klass) |
| 239 |
{ |
| 240 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 241 |
object_class->finalize = cd_profile_array_finalize; |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
cd_profile_array_init (CdProfileArray *profile_array) |
| 246 |
{ |
| 247 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 248 |
priv->array = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref); |
| 249 |
} |
| 250 |
|
| 251 |
static void |
| 252 |
cd_profile_array_finalize (GObject *object) |
| 253 |
{ |
| 254 |
CdProfileArray *profile_array = CD_PROFILE_ARRAY (object); |
| 255 |
CdProfileArrayPrivate *priv = GET_PRIVATE (profile_array); |
| 256 |
|
| 257 |
g_ptr_array_unref (priv->array); |
| 258 |
|
| 259 |
G_OBJECT_CLASS (cd_profile_array_parent_class)->finalize (object); |
| 260 |
} |
| 261 |
|
| 262 |
CdProfileArray * |
| 263 |
cd_profile_array_new (void) |
| 264 |
{ |
| 265 |
if (cd_profile_array_object != NULL) { |
| 266 |
g_object_ref (cd_profile_array_object); |
| 267 |
} else { |
| 268 |
cd_profile_array_object = g_object_new (CD_TYPE_PROFILE_ARRAY, NULL); |
| 269 |
g_object_add_weak_pointer (cd_profile_array_object, |
| 270 |
&cd_profile_array_object); |
| 271 |
} |
| 272 |
return CD_PROFILE_ARRAY (cd_profile_array_object); |
| 273 |
} |
| 274 |
|