65
65
gchar *object_path;
66
66
GDBusConnection *connection;
68
GPtrArray *profiles_soft;
69
GPtrArray *profiles_hard;
67
GPtrArray *profiles; /* of CdDeviceProfileItem */
70
68
guint registration_id;
72
gboolean require_modified_signal;
74
73
gboolean is_virtual;
75
74
GHashTable *metadata;
92
CdDeviceRelation relation;
94
} CdDeviceProfileItem;
90
96
static guint signals[SIGNAL_LAST] = { 0 };
91
97
G_DEFINE_TYPE (CdDevice, cd_device, G_TYPE_OBJECT)
99
#if !GLIB_CHECK_VERSION (2, 25, 0)
101
g_get_real_time (void)
104
gettimeofday (&tm, NULL);
94
110
* cd_device_get_scope:
130
* cd_device_set_owner:
133
cd_device_set_owner (CdDevice *device, guint owner)
135
g_return_if_fail (CD_IS_DEVICE (device));
136
device->priv->owner = owner;
114
140
* cd_device_mode_to_string:
116
142
static const gchar *
153
* _cd_device_mode_from_string:
156
_cd_device_mode_from_string (const gchar *device_mode)
158
if (g_strcmp0 (device_mode, "physical") == 0)
159
return CD_DEVICE_MODE_PHYSICAL;
160
if (g_strcmp0 (device_mode, "virtual") == 0)
161
return CD_DEVICE_MODE_VIRTUAL;
162
return CD_DEVICE_MODE_UNKNOWN;
127
166
* cd_device_set_mode:
177
* cd_device_get_mode:
180
cd_device_get_mode (CdDevice *device)
182
g_return_val_if_fail (CD_IS_DEVICE (device), CD_DEVICE_MODE_UNKNOWN);
183
return _cd_device_mode_from_string (device->priv->mode);
138
187
* cd_device_get_object_path:
205
254
#if !GLIB_CHECK_VERSION (2, 25, 0)
206
255
device->priv->modified = g_get_real_time ();
208
cd_device_dbus_emit_property_changed (device,
210
g_variant_new_uint64 (device->priv->modified));
257
device->priv->require_modified_signal = TRUE;
282
if (device->priv->require_modified_signal) {
283
g_variant_builder_add (&builder,
285
CD_DEVICE_PROPERTY_MODIFIED,
286
g_variant_new_uint64 (device->priv->modified));
287
device->priv->require_modified_signal = FALSE;
235
289
g_dbus_connection_emit_signal (device->priv->connection,
237
291
device->priv->object_path,
323
377
* cd_device_find_by_qualifier:
325
379
static CdProfile *
326
cd_device_find_by_qualifier (const gchar *regex, GPtrArray *array)
380
cd_device_find_by_qualifier (const gchar *regex,
382
CdDeviceRelation relation)
384
CdDeviceProfileItem *item;
328
385
CdProfile *profile = NULL;
329
CdProfile *profile_tmp;
330
386
const gchar *qualifier;
334
390
/* find using a wildcard */
335
391
for (i=0; i<array->len; i++) {
336
profile_tmp = g_ptr_array_index (array, i);
392
item = g_ptr_array_index (array, i);
393
if (item->relation != relation)
338
396
/* '*' matches anything, including a blank qualifier */
339
397
if (g_strcmp0 (regex, "*") == 0) {
340
398
g_debug ("anything matches, returning %s",
341
cd_profile_get_id (profile_tmp));
342
profile = profile_tmp;
399
cd_profile_get_id (item->profile));
400
profile = item->profile;
346
404
/* match with a regex */
347
qualifier = cd_profile_get_qualifier (profile_tmp);
405
qualifier = cd_profile_get_qualifier (item->profile);
348
406
if (qualifier == NULL) {
349
407
g_debug ("no qualifier for %s, skipping",
350
cd_profile_get_id (profile_tmp));
408
cd_profile_get_id (item->profile));
353
411
ret = cd_device_match_qualifier (regex,
371
429
static CdProfile *
372
430
cd_device_find_profile_by_object_path (GPtrArray *array, const gchar *object_path)
432
CdDeviceProfileItem *item;
374
433
CdProfile *profile = NULL;
375
CdProfile *profile_tmp;
379
437
/* find using an object path */
380
438
for (i=0; i<array->len; i++) {
381
profile_tmp = g_ptr_array_index (array, i);
439
item = g_ptr_array_index (array, i);
382
440
ret = (g_strcmp0 (object_path,
383
cd_profile_get_object_path (profile_tmp)) == 0);
441
cd_profile_get_object_path (item->profile)) == 0);
385
profile = profile_tmp;
443
profile = item->profile;
396
454
static GVariant *
397
455
cd_device_get_profiles_as_variant (CdDevice *device)
402
459
GVariant **profiles = NULL;
462
CdDeviceProfileItem *item;
405
464
/* copy the object paths, hard then soft */
406
465
profiles = g_new0 (GVariant *, device->priv->profiles->len + 1);
407
for (i=0; i<device->priv->profiles_hard->len; i++) {
408
profile = g_ptr_array_index (device->priv->profiles_hard, i);
409
profiles[idx++] = g_variant_new_object_path (cd_profile_get_object_path (profile));
466
for (i=0; i<device->priv->profiles->len; i++) {
467
item = g_ptr_array_index (device->priv->profiles, i);
468
if (item->relation == CD_DEVICE_RELATION_SOFT)
470
tmp = cd_profile_get_object_path (item->profile);
471
profiles[idx++] = g_variant_new_object_path (tmp);
411
for (i=0; i<device->priv->profiles_soft->len; i++) {
412
profile = g_ptr_array_index (device->priv->profiles_soft, i);
413
profiles[idx++] = g_variant_new_object_path (cd_profile_get_object_path (profile));
473
for (i=0; i<device->priv->profiles->len; i++) {
474
item = g_ptr_array_index (device->priv->profiles, i);
475
if (item->relation == CD_DEVICE_RELATION_HARD)
477
tmp = cd_profile_get_object_path (item->profile);
478
profiles[idx++] = g_variant_new_object_path (tmp);
416
481
/* format the value */
432
497
CdDevicePrivate *priv = device->priv;
433
CdProfile *profile_tmp;
498
CdDeviceProfileItem *item;
434
499
gboolean ret = FALSE;
437
502
/* check the profile exists on this device */
438
503
for (i=0; i<priv->profiles->len; i++) {
439
profile_tmp = g_ptr_array_index (priv->profiles, i);
504
item = g_ptr_array_index (priv->profiles, i);
440
505
if (g_strcmp0 (profile_object_path,
441
cd_profile_get_object_path (profile_tmp)) == 0) {
506
cd_profile_get_object_path (item->profile)) == 0) {
456
521
/* remove from the arrays */
457
g_ptr_array_remove (priv->profiles_soft, profile_tmp);
458
g_ptr_array_remove (priv->profiles_hard, profile_tmp);
459
ret = g_ptr_array_remove (priv->profiles, profile_tmp);
522
ret = g_ptr_array_remove (priv->profiles, item);
525
/* reset modification time */
526
cd_device_reset_modified (device);
463
529
cd_device_dbus_emit_property_changed (device,
465
531
cd_device_get_profiles_as_variant (device));
467
/* reset modification time */
468
cd_device_reset_modified (device);
470
533
/* emit global signal */
471
534
cd_device_dbus_emit_device_changed (device);
481
544
const gchar *profile_object_path)
483
546
CdDevicePrivate *priv = device->priv;
547
CdDeviceProfileItem *item;
484
548
CdDeviceRelation relation = CD_DEVICE_RELATION_UNKNOWN;
485
CdProfile *profile_tmp;
489
for (i=0; i<priv->profiles_hard->len; i++) {
490
profile_tmp = g_ptr_array_index (priv->profiles_hard, i);
491
if (g_strcmp0 (profile_object_path,
492
cd_profile_get_object_path (profile_tmp)) == 0) {
493
relation = CD_DEVICE_RELATION_HARD;
499
for (i=0; i<priv->profiles_soft->len; i++) {
500
profile_tmp = g_ptr_array_index (priv->profiles_soft, i);
501
if (g_strcmp0 (profile_object_path,
502
cd_profile_get_object_path (profile_tmp)) == 0) {
503
relation = CD_DEVICE_RELATION_SOFT;
551
/* search profiles */
552
for (i=0; i<priv->profiles->len; i++) {
553
item = g_ptr_array_index (priv->profiles, i);
554
if (g_strcmp0 (profile_object_path,
555
cd_profile_get_object_path (item->profile)) == 0) {
556
relation = item->relation;
521
574
return "unknown";
525
_g_ptr_array_insert (GPtrArray *array, guint idx, gpointer data)
578
* cd_device_add_profile:
581
cd_device_profile_item_sort_cb (gconstpointer a, gconstpointer b)
527
g_return_if_fail (idx <= array->len);
584
CdDeviceProfileItem **item_a = (CdDeviceProfileItem **) a;
585
CdDeviceProfileItem **item_b = (CdDeviceProfileItem **) b;
529
g_ptr_array_add (array, NULL);
530
g_memmove (&array->pdata[idx+1],
531
&array->pdata[idx+0],
532
(array->len - idx - 1) * sizeof (gpointer));
533
array->pdata[idx] = data;
587
tmp = (gint64) (*item_b)->timestamp - (gint64) (*item_a)->timestamp;
540
599
cd_device_add_profile (CdDevice *device,
541
600
CdDeviceRelation relation,
542
601
const gchar *profile_object_path,
545
605
CdDevicePrivate *priv = device->priv;
606
CdDeviceProfileItem *item;
546
607
CdProfile *profile;
547
CdProfile *profile_tmp;
548
608
gboolean ret = TRUE;
564
624
/* check it does not already exist */
565
625
for (i=0; i<priv->profiles->len; i++) {
566
profile_tmp = g_ptr_array_index (priv->profiles, i);
626
item = g_ptr_array_index (priv->profiles, i);
567
627
if (g_strcmp0 (cd_profile_get_object_path (profile),
568
cd_profile_get_object_path (profile_tmp)) == 0) {
628
cd_profile_get_object_path (item->profile)) == 0) {
570
630
g_set_error (error,
581
641
cd_profile_get_id (profile),
582
642
_cd_device_relation_to_string (relation),
583
643
device->priv->id);
584
_g_ptr_array_insert (priv->profiles, 0, g_object_ref (profile));
585
if (relation == CD_DEVICE_RELATION_SOFT)
586
_g_ptr_array_insert (priv->profiles_soft, 0, g_object_ref (profile));
587
if (relation == CD_DEVICE_RELATION_HARD)
588
_g_ptr_array_insert (priv->profiles_hard, 0, g_object_ref (profile));
644
item = g_new0 (CdDeviceProfileItem, 1);
645
item->profile = g_object_ref (profile);
646
item->relation = relation;
647
item->timestamp = timestamp;
648
g_ptr_array_add (priv->profiles, item);
649
g_ptr_array_sort (priv->profiles,
650
cd_device_profile_item_sort_cb);
652
/* reset modification time */
653
cd_device_reset_modified (device);
591
656
cd_device_dbus_emit_property_changed (device,
593
658
cd_device_get_profiles_as_variant (device));
595
/* reset modification time */
596
cd_device_reset_modified (device);
598
660
/* emit global signal */
599
661
cd_device_dbus_emit_device_changed (device);
775
837
g_strdup (property),
776
838
g_strdup (value));
777
839
cd_device_dbus_emit_property_changed (device,
778
CD_DEVICE_PROPERTY_METADATA,
779
cd_device_get_metadata_as_variant (device));
840
CD_DEVICE_PROPERTY_METADATA,
841
cd_device_get_metadata_as_variant (device));
782
844
/* set this externally so we can add disk devices at startup
838
900
/* make the profile first in the array */
839
901
for (i=1; i<priv->profiles->len; i++) {
840
profile_tmp = g_ptr_array_index (priv->profiles, i);
841
if (profile_tmp == profile) {
842
/* swap [0] and [i] */
843
g_debug ("CdDevice: making %s the default on %s",
846
profile_tmp = priv->profiles->pdata[0];
847
priv->profiles->pdata[0] = profile;
848
priv->profiles->pdata[i] = profile_tmp;
853
/* ensure profile is in the 'hard' relation array */
854
ret = g_ptr_array_remove (priv->profiles_soft, profile);
856
g_ptr_array_add (priv->profiles_hard, g_object_ref (profile));
858
/* make the profile first in the hard array */
859
for (i=1; i<priv->profiles_hard->len; i++) {
860
profile_tmp = g_ptr_array_index (priv->profiles_hard, i);
861
if (profile_tmp == profile) {
862
/* swap [0] and [i] */
863
profile_tmp = priv->profiles_hard->pdata[0];
864
priv->profiles_hard->pdata[0] = profile;
865
priv->profiles_hard->pdata[i] = profile_tmp;
902
item = g_ptr_array_index (priv->profiles, i);
903
if (item->profile == profile) {
904
item->timestamp = g_get_real_time ();
905
item->relation = CD_DEVICE_RELATION_HARD;
906
g_ptr_array_sort (priv->profiles,
907
cd_device_profile_item_sort_cb);
912
/* reset modification time */
913
cd_device_reset_modified (device);
871
916
cd_device_dbus_emit_property_changed (device,
1079
1123
g_debug ("searching [hard]");
1080
1124
profile = cd_device_find_by_qualifier (regexes[i],
1081
priv->profiles_hard);
1126
CD_DEVICE_RELATION_HARD);
1083
1128
for (i=0; profile == NULL && regexes[i] != NULL; i++) {
1085
1130
g_debug ("searching [soft]");
1086
1131
profile = cd_device_find_by_qualifier (regexes[i],
1087
priv->profiles_soft);
1133
CD_DEVICE_RELATION_SOFT);
1089
1135
if (profile == NULL) {
1090
1136
g_dbus_method_invocation_return_error (invocation,
1125
1170
g_dbus_method_invocation_return_error (invocation,
1127
1172
CD_MAIN_ERROR_FAILED,
1128
"failed to make profile default",
1173
"failed to make profile default: %s",
1129
1174
error->message);
1130
1175
g_error_free (error);
1347
1390
retval = g_variant_new_string (cd_object_scope_to_string (priv->object_scope));
1393
if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_OWNER) == 0) {
1394
retval = g_variant_new_uint32 (priv->owner);
1351
1398
g_critical ("failed to get property %s", property_name);
1566
* cd_device_profiles_item_free:
1569
cd_device_profiles_item_free (CdDeviceProfileItem *item)
1571
g_object_unref (item->profile);
1519
1576
* cd_device_init:
1522
1579
cd_device_init (CdDevice *device)
1524
1581
device->priv = CD_DEVICE_GET_PRIVATE (device);
1525
device->priv->profiles = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1526
device->priv->profiles_soft = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1527
device->priv->profiles_hard = g_ptr_array_new_with_free_func ((GDestroyNotify) g_object_unref);
1582
device->priv->profiles = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_device_profiles_item_free);
1528
1583
device->priv->profile_array = cd_profile_array_new ();
1529
#if !GLIB_CHECK_VERSION (2, 25, 0)
1530
1584
device->priv->created = g_get_real_time ();
1531
1585
device->priv->modified = g_get_real_time ();
1535
gettimeofday (&tm, NULL);
1536
device->priv->created = tm.tv_sec;
1537
device->priv->modified = tm.tv_sec;
1540
1586
device->priv->mapping_db = cd_mapping_db_new ();
1541
1587
device->priv->device_db = cd_device_db_new ();
1542
1588
device->priv->inhibit = cd_inhibit_new ();
1578
1624
g_free (priv->kind);
1579
1625
g_free (priv->object_path);
1580
1626
g_ptr_array_unref (priv->profiles);
1581
g_ptr_array_unref (priv->profiles_soft);
1582
g_ptr_array_unref (priv->profiles_hard);
1583
1627
g_object_unref (priv->profile_array);
1584
1628
g_object_unref (priv->mapping_db);
1585
1629
g_object_unref (priv->device_db);