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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
|
/*
*
* $Id: sourceheader,v 1.3 2005/01/19 13:03:46 trueg Exp $
* Copyright (C) 2005-2007 Sebastian Trueg <trueg@k3b.org>
* Copyright (C) 2013 Timothy Pearson <kb9vqf@pearsoncomputing.net>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
* Copyright (C) 2013 Timothy Pearson <kb9vqf@pearsoncomputing.net>
*
* 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.
* See the file "COPYING" for the exact licensing terms.
*/
#include "k3bhalconnection.h"
#include "k3bdevice.h"
#include <k3bdebug.h>
#include <tdelocale.h>
#include <tqtimer.h>
#ifdef HAVE_HAL
// We acknowledge the the dbus API is unstable
#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/connection.h>
#include <dbus/dbus.h>
#include <hal/libhal.h>
#else // HAVE_HAL
#include <tdehardwaredevices.h>
#endif // HAVE_HAL
#ifdef HAVE_HAL
static char** qstringListToArray( const TQStringList& s )
{
char** a = new char*[s.count()];
for( unsigned int i = 0; i < s.count(); ++i ) {
a[i] = new char[s[i].length()+1];
::strncpy( a[i], s[i].local8Bit().data(), s[i].length() );
a[s[i].length()] = '\0';
}
return a;
}
static void freeArray( char** a, unsigned int length )
{
for( unsigned int i = 0; i < length; ++i )
delete [] a[i];
delete a;
}
// CALLBACKS
void halDeviceAdded( LibHalContext* ctx, const char* udi )
{
Q_UNUSED( ctx );
k3bDebug() << "adding udi " << udi << endl;
K3bDevice::HalConnection::instance()->addDevice( udi );
}
void halDeviceRemoved( LibHalContext* ctx, const char* udi )
{
Q_UNUSED( ctx );
k3bDebug() << "removing udi " << udi << endl;
K3bDevice::HalConnection::instance()->removeDevice( udi );
}
K3bDevice::HalConnection* K3bDevice::HalConnection::s_instance = 0;
class K3bDevice::HalConnection::Private
{
public:
Private()
: halContext(0),
dBusTQtConnection(0),
bOpen(false) {
}
LibHalContext* halContext;
DBusConnection* connection;
DBusQt::Connection* dBusTQtConnection;
bool bOpen;
TQMap<TQCString, TQString> udiDeviceMap;
TQMap<TQString, TQCString> deviceUdiMap;
TQMap<TQCString, TQCString> deviceMediumUdiMap;
};
K3bDevice::HalConnection* K3bDevice::HalConnection::instance()
{
if( s_instance == 0 )
s_instance = new HalConnection( 0 );
if( !s_instance->isConnected() && !s_instance->open() )
k3bDebug() << "(K3bDevice::HalConnection) failed to open connection to HAL." << endl;
return s_instance;
}
K3bDevice::HalConnection::HalConnection( TQObject* parent, const char* name )
: TQObject( parent, name )
{
d = new Private();
}
K3bDevice::HalConnection::~HalConnection()
{
s_instance = 0;
close();
delete d;
}
bool K3bDevice::HalConnection::isConnected() const
{
return d->bOpen;
}
bool K3bDevice::HalConnection::open()
{
close();
k3bDebug() << "(K3bDevice::HalConnection) initializing HAL >= 0.5" << endl;
d->halContext = libhal_ctx_new();
if( !d->halContext ) {
k3bDebug() << "(K3bDevice::HalConnection) unable to create HAL context." << endl;
return false;
}
DBusError error;
dbus_error_init( &error );
d->connection = dbus_bus_get( DBUS_BUS_SYSTEM, &error );
if( dbus_error_is_set(&error) ) {
k3bDebug() << "(K3bDevice::HalConnection) unable to connect to DBUS: " << error.message << endl;
return false;
}
setupDBusTQtConnection( d->connection );
libhal_ctx_set_dbus_connection( d->halContext, d->connection );
libhal_ctx_set_device_added( d->halContext, halDeviceAdded );
libhal_ctx_set_device_removed( d->halContext, halDeviceRemoved );
libhal_ctx_set_device_new_capability( d->halContext, 0 );
libhal_ctx_set_device_lost_capability( d->halContext, 0 );
libhal_ctx_set_device_property_modified( d->halContext, 0 );
libhal_ctx_set_device_condition( d->halContext, 0 );
if( !libhal_ctx_init( d->halContext, 0 ) ) {
k3bDebug() << "(K3bDevice::HalConnection) Failed to init HAL context!" << endl;
return false;
}
d->bOpen = true;
//
// report all devices
//
int numDevices;
char** halDeviceList = libhal_get_all_devices( d->halContext, &numDevices, 0 );
for( int i = 0; i < numDevices; ++i )
addDevice( halDeviceList[i] );
return true;
}
void K3bDevice::HalConnection::close()
{
if( d->halContext ) {
// clear the context
if( isConnected() )
libhal_ctx_shutdown( d->halContext, 0 );
libhal_ctx_free( d->halContext );
// delete the connection (may be 0 if open() failed)
delete d->dBusTQtConnection;
d->halContext = 0;
d->dBusTQtConnection = 0;
d->bOpen = false;
}
}
TQStringList K3bDevice::HalConnection::devices() const
{
return TQStringList( d->udiDeviceMap.values() );
}
void K3bDevice::HalConnection::addDevice( const char* udi )
{
// ignore devices that have no property "info.capabilities" to suppress error messages
if( !libhal_device_property_exists( d->halContext, udi, "info.capabilities", 0 ) )
return;
if( libhal_device_query_capability( d->halContext, udi, "storage.cdrom", 0 ) ) {
char* dev = libhal_device_get_property_string( d->halContext, udi, "block.device", 0 );
if( dev ) {
TQString s( dev );
libhal_free_string( dev );
if( !s.isEmpty() ) {
k3bDebug() << "Mapping udi " << udi << " to device " << s << endl;
d->udiDeviceMap[udi] = s;
d->deviceUdiMap[s] = udi;
emit deviceAdded( s );
}
}
}
else {
if( libhal_device_property_exists( d->halContext, udi, "block.storage_device", 0 ) ) {
char* deviceUdi = libhal_device_get_property_string( d->halContext, udi, "block.storage_device", 0 );
if( deviceUdi ) {
TQCString du( deviceUdi );
libhal_free_string( deviceUdi );
if( d->udiDeviceMap.contains( du ) ) {
//
// A new medium has been inserted. Save this medium's udi so we can reuse it later
// on for the mount/unmount/eject methods
//
d->deviceMediumUdiMap[du] = TQCString( udi );
emit mediumChanged( d->udiDeviceMap[du] );
}
}
}
}
}
void K3bDevice::HalConnection::removeDevice( const char* udi )
{
TQMapIterator<TQCString, TQString> it = d->udiDeviceMap.find( udi );
if( it != d->udiDeviceMap.end() ) {
k3bDebug() << "Unmapping udi " << udi << " from device " << it.data() << endl;
emit deviceRemoved( it.data() );
d->udiDeviceMap.erase( it );
d->deviceUdiMap.erase( it.data() );
}
else {
if( libhal_device_property_exists( d->halContext, udi, "block.storage_device", 0 ) ) {
char* deviceUdi = libhal_device_get_property_string( d->halContext, udi, "block.storage_device", 0 );
if( deviceUdi ) {
TQCString du( deviceUdi );
libhal_free_string( deviceUdi );
if( d->udiDeviceMap.contains( du ) ) {
//
// A medium has been removed/ejected.
//
d->deviceMediumUdiMap[du] = 0;
emit mediumChanged( d->udiDeviceMap[du] );
}
}
}
}
}
int K3bDevice::HalConnection::lock( Device* dev )
{
//
// The code below is based on the code from tdeioslave/media/mediamanager/halbackend.cpp in the tdebase package
// Copyright (c) 2004-2005 Jérôme Lodewyck <jerome dot lodewyck at normalesup dot org>
//
DBusMessage* dmesg = 0;
DBusMessage* reply = 0;
DBusError error;
if( !d->deviceUdiMap.contains( dev->blockDeviceName() ) ) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TQCString udi = d->deviceUdiMap[dev->blockDeviceName()];
if( !( dmesg = dbus_message_new_method_call( "org.freedesktop.Hal", udi.data(),
"org.freedesktop.Hal.Device",
"Lock" ) ) ) {
k3bDebug() << "(K3bDevice::HalConnection) lock failed for " << udi << ": could not create dbus message\n";
return org_freedesktop_Hal_CommunicationError;
}
const char* lockComment = "Locked by the K3b libraries";
if( !dbus_message_append_args( dmesg,
DBUS_TYPE_STRING, &lockComment,
DBUS_TYPE_INVALID ) ) {
k3bDebug() << "(K3bDevice::HalConnection) lock failed for " << udi << ": could not append args to dbus message\n";
dbus_message_unref( dmesg );
return org_freedesktop_Hal_CommunicationError;
}
int ret = org_freedesktop_Hal_Success;
dbus_error_init( &error );
reply = dbus_connection_send_with_reply_and_block( d->connection, dmesg, -1, &error );
if( dbus_error_is_set( &error ) ) {
kdError() << "(K3bDevice::HalConnection) lock failed for " << udi << ": " << error.name << " - " << error.message << endl;
if( !strcmp(error.name, "org.freedesktop.Hal.NoSuchDevice" ) )
ret = org_freedesktop_Hal_NoSuchDevice;
else if( !strcmp(error.name, "org.freedesktop.Hal.DeviceAlreadyLocked" ) )
ret = org_freedesktop_Hal_DeviceAlreadyLocked;
else if( !strcmp(error.name, "org.freedesktop.Hal.PermissionDenied" ) )
ret = org_freedesktop_Hal_PermissionDenied;
dbus_error_free( &error );
}
else
k3bDebug() << "(K3bDevice::HalConnection) lock queued for " << udi << endl;
dbus_message_unref( dmesg );
if( reply )
dbus_message_unref( reply );
return ret;
}
int K3bDevice::HalConnection::unlock( Device* dev )
{
//
// The code below is based on the code from tdeioslave/media/mediamanager/halbackend.cpp in the tdebase package
// Copyright (c) 2004-2005 Jérôme Lodewyck <jerome dot lodewyck at normalesup dot org>
//
DBusMessage* dmesg = 0;
DBusMessage* reply = 0;
DBusError error;
if( !d->deviceUdiMap.contains( dev->blockDeviceName() ) ) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TQCString udi = d->deviceUdiMap[dev->blockDeviceName()];
if( !( dmesg = dbus_message_new_method_call( "org.freedesktop.Hal", udi.data(),
"org.freedesktop.Hal.Device",
"Unlock" ) ) ) {
k3bDebug() << "(K3bDevice::HalConnection) unlock failed for " << udi << ": could not create dbus message\n";
return org_freedesktop_Hal_CommunicationError;
}
if( !dbus_message_append_args( dmesg,
DBUS_TYPE_INVALID ) ) {
k3bDebug() << "(K3bDevice::HalConnection) unlock failed for " << udi << ": could not append args to dbus message\n";
dbus_message_unref( dmesg );
return org_freedesktop_Hal_CommunicationError;
}
int ret = org_freedesktop_Hal_Success;
dbus_error_init( &error );
reply = dbus_connection_send_with_reply_and_block( d->connection, dmesg, -1, &error );
if( dbus_error_is_set( &error ) ) {
kdError() << "(K3bDevice::HalConnection) unlock failed for " << udi << ": " << error.name << " - " << error.message << endl;
if( !strcmp(error.name, "org.freedesktop.Hal.NoSuchDevice" ) )
ret = org_freedesktop_Hal_NoSuchDevice;
else if( !strcmp(error.name, "org.freedesktop.Hal.DeviceAlreadyLocked" ) )
ret = org_freedesktop_Hal_DeviceAlreadyLocked;
else if( !strcmp(error.name, "org.freedesktop.Hal.PermissionDenied" ) )
ret = org_freedesktop_Hal_PermissionDenied;
dbus_error_free( &error );
}
else
k3bDebug() << "(K3bDevice::HalConnection) unlock queued for " << udi << endl;
dbus_message_unref( dmesg );
if( reply )
dbus_message_unref( reply );
return ret;
}
int K3bDevice::HalConnection::mount( K3bDevice::Device* dev,
const TQString& mountPoint,
const TQString& fstype,
const TQStringList& options )
{
//
// The code below is based on the code from tdeioslave/media/mediamanager/halbackend.cpp in the tdebase package
// Copyright (c) 2004-2005 Jérôme Lodewyck <jerome dot lodewyck at normalesup dot org>
//
DBusMessage* dmesg = 0;
DBusMessage* reply = 0;
DBusError error;
if( !d->deviceUdiMap.contains( dev->blockDeviceName() ) )
return org_freedesktop_Hal_NoSuchDevice;
if( !d->deviceMediumUdiMap.contains( d->deviceUdiMap[dev->blockDeviceName()] ) )
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
TQCString mediumUdi = d->deviceMediumUdiMap[d->deviceUdiMap[dev->blockDeviceName()]];
if( !( dmesg = dbus_message_new_method_call( "org.freedesktop.Hal", mediumUdi.data(),
"org.freedesktop.Hal.Device.Volume",
"Mount" ) ) ) {
k3bDebug() << "(K3bDevice::HalConnection) mount failed for " << mediumUdi << ": could not create dbus message\n";
return org_freedesktop_Hal_CommunicationError;
}
char** poptions = qstringListToArray( options );
TQByteArray strMountPoint = mountPoint.local8Bit();
TQByteArray strFstype = fstype.local8Bit();
if( !dbus_message_append_args( dmesg,
DBUS_TYPE_STRING, strMountPoint.data(),
DBUS_TYPE_STRING, strFstype.data(),
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &poptions, options.count(),
DBUS_TYPE_INVALID ) ) {
k3bDebug() << "(K3bDevice::HalConnection) mount failed for " << mediumUdi << ": could not append args to dbus message\n";
dbus_message_unref( dmesg );
freeArray( poptions, options.count() );
return org_freedesktop_Hal_CommunicationError;
}
freeArray( poptions, options.count() );
int ret = org_freedesktop_Hal_Success;
dbus_error_init( &error );
reply = dbus_connection_send_with_reply_and_block( d->connection, dmesg, -1, &error );
if( dbus_error_is_set( &error ) ) {
kdError() << "(K3bDevice::HalConnection) mount failed for " << mediumUdi << ": " << error.name << " - " << error.message << endl;
if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.NoSuchDevice" ) )
ret = org_freedesktop_Hal_Device_Volume_NoSuchDevice;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.PermissionDenied" ) )
ret = org_freedesktop_Hal_Device_Volume_PermissionDenied;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.UnknownFilesystemType" ) )
ret = org_freedesktop_Hal_Device_Volume_UnknownFilesystemType;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.MountPointNotAvailable" ) )
ret = org_freedesktop_Hal_Device_Volume_MountPointNotAvailable;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.AlreadyMounted" ) )
ret = org_freedesktop_Hal_Device_Volume_AlreadyMounted;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.InvalidMountpoint" ) )
ret = org_freedesktop_Hal_Device_Volume_InvalidMountpoint;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.InvalidMountOption" ) )
ret = org_freedesktop_Hal_Device_Volume_InvalidMountOption;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.PermissionDeniedByPolicy" ) )
ret = org_freedesktop_Hal_Device_Volume_PermissionDeniedByPolicy;
dbus_error_free( &error );
}
else
k3bDebug() << "(K3bDevice::HalConnection) mount queued for " << mediumUdi << endl;
dbus_message_unref( dmesg );
if( reply )
dbus_message_unref( reply );
return ret;
}
int K3bDevice::HalConnection::unmount( K3bDevice::Device* dev,
const TQStringList& options )
{
//
// The code below is based on the code from tdeioslave/media/mediamanager/halbackend.cpp in the tdebase package
// Copyright (c) 2004-2005 Jérôme Lodewyck <jerome dot lodewyck at normalesup dot org>
//
DBusMessage* dmesg = 0;
DBusMessage* reply = 0;
DBusError error;
if( !d->deviceUdiMap.contains( dev->blockDeviceName() ) )
return org_freedesktop_Hal_NoSuchDevice;
if( !d->deviceMediumUdiMap.contains( d->deviceUdiMap[dev->blockDeviceName()] ) )
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
TQCString mediumUdi = d->deviceMediumUdiMap[d->deviceUdiMap[dev->blockDeviceName()]];
if( !( dmesg = dbus_message_new_method_call( "org.freedesktop.Hal", mediumUdi.data(),
"org.freedesktop.Hal.Device.Volume",
"Unmount" ) ) ) {
k3bDebug() << "(K3bDevice::HalConnection) unmount failed for " << mediumUdi << ": could not create dbus message\n";
return org_freedesktop_Hal_CommunicationError;
}
char** poptions = qstringListToArray( options );
if( !dbus_message_append_args( dmesg,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &poptions, options.count(),
DBUS_TYPE_INVALID ) ) {
k3bDebug() << "(K3bDevice::HalConnection) unmount failed for " << mediumUdi << ": could not append args to dbus message\n";
dbus_message_unref( dmesg );
freeArray( poptions, options.count() );
return org_freedesktop_Hal_CommunicationError;
}
freeArray( poptions, options.count() );
int ret = org_freedesktop_Hal_Success;
dbus_error_init( &error );
reply = dbus_connection_send_with_reply_and_block( d->connection, dmesg, -1, &error );
if( dbus_error_is_set( &error ) ) {
kdError() << "(K3bDevice::HalConnection) unmount failed for " << mediumUdi << ": " << error.name << " - " << error.message << endl;
if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.NoSuchDevice" ) )
ret = org_freedesktop_Hal_Device_Volume_NoSuchDevice;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.PermissionDenied" ) )
ret = org_freedesktop_Hal_Device_Volume_PermissionDenied;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.MountPointNotAvailable" ) )
ret = org_freedesktop_Hal_Device_Volume_MountPointNotAvailable;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.InvalidUnmountOption" ) )
ret = org_freedesktop_Hal_Device_Volume_InvalidUnmountOption;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.InvalidMountpoint" ) )
ret = org_freedesktop_Hal_Device_Volume_InvalidMountpoint;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.PermissionDeniedByPolicy" ) )
ret = org_freedesktop_Hal_Device_Volume_PermissionDeniedByPolicy;
dbus_error_free( &error );
}
else
k3bDebug() << "(K3bDevice::HalConnection) unmount queued for " << mediumUdi << endl;
dbus_message_unref( dmesg );
if( reply )
dbus_message_unref( reply );
return ret;
}
int K3bDevice::HalConnection::eject( K3bDevice::Device* dev,
const TQStringList& options )
{
//
// The code below is based on the code from tdeioslave/media/mediamanager/halbackend.cpp in the tdebase package
// Copyright (c) 2004-2005 Jérôme Lodewyck <jerome dot lodewyck at normalesup dot org>
//
DBusMessage* dmesg = 0;
DBusMessage* reply = 0;
DBusError error;
if( !d->deviceUdiMap.contains( dev->blockDeviceName() ) )
return org_freedesktop_Hal_NoSuchDevice;
if( !d->deviceMediumUdiMap.contains( d->deviceUdiMap[dev->blockDeviceName()] ) )
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
TQCString mediumUdi = d->deviceMediumUdiMap[d->deviceUdiMap[dev->blockDeviceName()]];
if( !( dmesg = dbus_message_new_method_call( "org.freedesktop.Hal", mediumUdi.data(),
"org.freedesktop.Hal.Device.Volume",
"Eject" ) ) ) {
k3bDebug() << "(K3bDevice::HalConnection) eject failed for " << mediumUdi << ": could not create dbus message\n";
return org_freedesktop_Hal_CommunicationError;
}
char** poptions = qstringListToArray( options );
if( !dbus_message_append_args( dmesg,
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &poptions, options.count(),
DBUS_TYPE_INVALID ) ) {
k3bDebug() << "(K3bDevice::HalConnection) eject failed for " << mediumUdi << ": could not append args to dbus message\n";
dbus_message_unref( dmesg );
freeArray( poptions, options.count() );
return org_freedesktop_Hal_CommunicationError;
}
freeArray( poptions, options.count() );
int ret = org_freedesktop_Hal_Success;
dbus_error_init( &error );
reply = dbus_connection_send_with_reply_and_block( d->connection, dmesg, -1, &error );
if( dbus_error_is_set( &error ) ) {
kdError() << "(K3bDevice::HalConnection) eject failed for " << mediumUdi << ": " << error.name << " - " << error.message << endl;
if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.NoSuchDevice" ) )
ret = org_freedesktop_Hal_Device_Volume_NoSuchDevice;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.PermissionDenied" ) )
ret = org_freedesktop_Hal_Device_Volume_PermissionDenied;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.InvalidEjectOption" ) )
ret = org_freedesktop_Hal_Device_Volume_InvalidEjectOption;
else if( !strcmp(error.name, "org.freedesktop.Hal.Device.Volume.PermissionDeniedByPolicy" ) )
ret = org_freedesktop_Hal_Device_Volume_PermissionDeniedByPolicy;
dbus_error_free( &error );
}
else
k3bDebug() << "(K3bDevice::HalConnection) eject queued for " << mediumUdi << endl;
dbus_message_unref( dmesg );
if( reply )
dbus_message_unref( reply );
return ret;
}
void K3bDevice::HalConnection::setupDBusTQtConnection( DBusConnection* dbusConnection )
{
d->dBusTQtConnection = new DBusQt::Connection( this );
d->dBusTQtConnection->dbus_connection_setup_with_qt_main( dbusConnection );
}
#else // HAVE_HAL
K3bDevice::HalConnection* K3bDevice::HalConnection::s_instance = 0;
class K3bDevice::HalConnection::Private
{
public:
Private()
: bOpen(false),
m_hwdevices(NULL) {
//
}
bool bOpen;
TDEHardwareDevices *m_hwdevices;
TQMap<TQString, TQString> udiDeviceMap;
TQMap<TQString, TQString> deviceUdiMap;
TQMap<TQString, bool> deviceMediumUdiMap;
};
K3bDevice::HalConnection* K3bDevice::HalConnection::instance()
{
if (s_instance == 0) {
s_instance = new HalConnection(0);
}
if ((!s_instance->isConnected()) && (!s_instance->open())) {
k3bDebug() << "(K3bDevice::HalConnection) failed to initialize the TDE hardware backend." << endl;
}
return s_instance;
}
K3bDevice::HalConnection::HalConnection( TQObject* parent, const char* name )
: TQObject( parent, name )
{
d = new Private();
}
K3bDevice::HalConnection::~HalConnection()
{
s_instance = 0;
close();
delete d;
}
bool K3bDevice::HalConnection::isConnected() const
{
return d->bOpen;
}
bool K3bDevice::HalConnection::open()
{
// Initialize the TDE device manager
d->m_hwdevices = TDEGlobal::hardwareDevices();
// Connect device monitoring signals/slots
connect(d->m_hwdevices, TQT_SIGNAL(hardwareAdded(TDEGenericDevice*)), this, TQT_SLOT(AddDeviceHandler(TDEGenericDevice*)));
connect(d->m_hwdevices, TQT_SIGNAL(hardwareRemoved(TDEGenericDevice*)), this, TQT_SLOT(RemoveDeviceHandler(TDEGenericDevice*)));
connect(d->m_hwdevices, TQT_SIGNAL(hardwareUpdated(TDEGenericDevice*)), this, TQT_SLOT(ModifyDeviceHandler(TDEGenericDevice*)));
d->bOpen = true;
//
// Report all devices
//
TDEGenericHardwareList hwlist = d->m_hwdevices->listAllPhysicalDevices();
TDEGenericDevice *hwdevice;
for (hwdevice = hwlist.first(); hwdevice; hwdevice = hwlist.next()) {
AddDeviceHandler(hwdevice);
}
return true;
}
void K3bDevice::HalConnection::close()
{
d->bOpen = false;
}
TQStringList K3bDevice::HalConnection::devices() const
{
return TQStringList(d->udiDeviceMap.values());
}
void K3bDevice::HalConnection::AddDeviceHandler(TDEGenericDevice* hwdevice)
{
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return;
}
TQString udi = hwdevice->uniqueID();
TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
if (sdevice->diskType() & TDEDiskDeviceType::Optical) {
TQString blockDevice = sdevice->deviceNode();
if (!blockDevice.isEmpty()) {
k3bDebug() << "Mapping udi " << udi << " to device " << blockDevice << endl;
d->udiDeviceMap[udi] = blockDevice;
d->deviceUdiMap[blockDevice] = udi;
emit deviceAdded(blockDevice);
// Check for medium
if (sdevice->mediaInserted()) {
d->deviceMediumUdiMap[blockDevice] = true;
emit mediumChanged(blockDevice);
}
}
}
}
void K3bDevice::HalConnection::RemoveDeviceHandler(TDEGenericDevice* hwdevice)
{
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return;
}
TQString udi = hwdevice->uniqueID();
TQString blockDevice = hwdevice->deviceNode();
TQMapIterator<TQString, TQString> it = d->udiDeviceMap.find(udi);
if (it != d->udiDeviceMap.end()) {
k3bDebug() << "Unmapping udi " << udi << " from device " << it.data() << endl;
emit deviceRemoved(it.data());
d->udiDeviceMap.erase(it);
d->deviceUdiMap.erase(it.data());
if (d->deviceMediumUdiMap[blockDevice]) {
d->deviceMediumUdiMap[blockDevice] = false;
emit mediumChanged(blockDevice);
}
}
}
void K3bDevice::HalConnection::ModifyDeviceHandler(TDEGenericDevice* hwdevice)
{
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return;
}
TQString udi = hwdevice->uniqueID();
TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
TQString blockDevice = hwdevice->deviceNode();
if (d->deviceMediumUdiMap[blockDevice] != sdevice->mediaInserted()) {
d->deviceMediumUdiMap[blockDevice] = sdevice->mediaInserted();
emit mediumChanged(blockDevice);
}
}
int K3bDevice::HalConnection::lock(Device* dev)
{
if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
if (!hwdevice) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
if (sdevice->lockDriveMedia(true)) {
return org_freedesktop_Hal_Success;
}
else {
return org_freedesktop_Hal_Device_Volume_InvalidEjectOption;
}
}
int K3bDevice::HalConnection::unlock(Device* dev)
{
if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
if (!hwdevice) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
if (sdevice->lockDriveMedia(false)) {
return org_freedesktop_Hal_Success;
}
else {
return org_freedesktop_Hal_Device_Volume_InvalidEjectOption;
}
}
int K3bDevice::HalConnection::mount( K3bDevice::Device* dev,
const TQString& mountPoint,
const TQString& fstype,
const TQStringList& options )
{
if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
if (!hwdevice) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
// FIXME
// Options from 'options' are not currently loaded into 'optionString'
TQString optionString;
TQString mountedPath = sdevice->mountDevice(mountPoint, optionString);
if (mountedPath.isNull()) {
return org_freedesktop_Hal_CommunicationError;
}
else {
return org_freedesktop_Hal_Success;
}
}
int K3bDevice::HalConnection::unmount(K3bDevice::Device* dev, const TQStringList& options)
{
if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
if (!hwdevice) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
// FIXME
// Options from 'options' are not currently loaded into 'optionString'
TQString optionString;
if (!sdevice->unmountDevice(NULL)) {
// Unmount failed!
return org_freedesktop_Hal_CommunicationError;
}
else {
return org_freedesktop_Hal_Success;
}
}
int K3bDevice::HalConnection::eject(K3bDevice::Device* dev, const TQStringList& options)
{
if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
if (!hwdevice) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
if (hwdevice->type() != TDEGenericDeviceType::Disk) {
return org_freedesktop_Hal_Device_Volume_NoSuchDevice;
}
TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);
if (sdevice->ejectDriveMedia()) {
return org_freedesktop_Hal_Success;
}
else {
return org_freedesktop_Hal_Device_Volume_InvalidEjectOption;
}
}
#endif // HAVE_HAL
#include "k3bhalconnection.moc"
|