summaryrefslogtreecommitdiffstats
path: root/libk3bdevice/k3bconnection.cpp
blob: 47cc98c8e774cc6a606c2f60126f7bd13c8be1aa (plain)
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
/*
 *
 * $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 "k3bconnection.h"
#include "k3bdevice.h"

#include <k3bdebug.h>
#include <tdelocale.h>

#include <tqtimer.h>
#include <tqvariant.h>

#ifdef HAVE_TDEHWLIB
#include <tdehardwaredevices.h>
#else
#define TDEHardwareDevices void
#endif

K3bDevice::Connection* K3bDevice::Connection::s_instance = 0;


class K3bDevice::Connection::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::Connection* K3bDevice::Connection::instance()
{
	if (s_instance == 0) {
		s_instance = new Connection(0);
	}

	if ((!s_instance->isConnected()) && (!s_instance->open())) {
		k3bDebug() << "(K3bDevice::Connection) failed to initialize the TDE hardware backend." << endl;
	}

	return s_instance;
}


K3bDevice::Connection::Connection( TQObject* parent, const char* name )
  : TQObject( parent, name )
{
	d = new Private();
}


K3bDevice::Connection::~Connection()
{
	s_instance = 0;
	close();
	delete d;
}


bool K3bDevice::Connection::isConnected() const
{
	return d->bOpen;
}


bool K3bDevice::Connection::open()
{
#ifdef HAVE_TDEHWLIB
	// 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;
#else
	return false;
#endif
}


void K3bDevice::Connection::close()
{
	d->bOpen = false;
}


TQStringList K3bDevice::Connection::devices() const
{
	return TQStringList(d->udiDeviceMap.values());
}

void K3bDevice::Connection::AddDeviceHandler(TDEGenericDevice* hwdevice)
{
#ifdef HAVE_TDEHWLIB
	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);
			}
		}
	}
#endif
}

void K3bDevice::Connection::RemoveDeviceHandler(TDEGenericDevice* hwdevice)
{
#ifdef HAVE_TDEHWLIB
	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);
		}
	}
#endif
}

void K3bDevice::Connection::ModifyDeviceHandler(TDEGenericDevice* hwdevice)
{
#ifdef HAVE_TDEHWLIB
	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);
	}
#endif
}

int K3bDevice::Connection::lock(Device* dev)
{
#ifdef HAVE_TDEHWLIB
	if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
	TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
	if (!hwdevice) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}

	if (hwdevice->type() != TDEGenericDeviceType::Disk) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);

	if (sdevice->lockDriveMedia(true)) {
		return ErrorCodes::Success;
	}
	else {
		return ErrorCodes::Device_Volume_InvalidEjectOption;
	}
#else
		return ErrorCodes::Device_Volume_NoSuchDevice;
#endif
}


int K3bDevice::Connection::unlock(Device* dev)
{
#ifdef HAVE_TDEHWLIB
	if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
	TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
	if (!hwdevice) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}

	if (hwdevice->type() != TDEGenericDeviceType::Disk) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);

	if (sdevice->lockDriveMedia(false)) {
		return ErrorCodes::Success;
	}
	else {
		return ErrorCodes::Device_Volume_InvalidEjectOption;
	}
#else
		return ErrorCodes::Device_Volume_NoSuchDevice;
#endif
}


int K3bDevice::Connection::mount( K3bDevice::Device* dev,
				     const TQString& mountPoint,
				     const TQString& fstype,
				     const TQStringList& options )
{
#ifdef HAVE_TDEHWLIB
	if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
	TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
	if (!hwdevice) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}

	if (hwdevice->type() != TDEGenericDeviceType::Disk) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);

	// FIXME
	// Options from 'options' are not currently loaded into 'mountOptions'
	TDEStorageMountOptions mountOptions;
	TQStringVariantMap mountResult = sdevice->mountDevice(mountPoint, mountOptions);
	TQString mountedPath = mountResult.contains("mountPath") ? mountResult["mountPath"].toString() : TQString::null;
	if (mountedPath.isEmpty()) {
		return ErrorCodes::CommunicationError;
	}
	else {
		return ErrorCodes::Success;
	}
#else
		return ErrorCodes::Device_Volume_NoSuchDevice;
#endif
}


int K3bDevice::Connection::unmount(K3bDevice::Device* dev, const TQStringList& options)
{
#ifdef HAVE_TDEHWLIB
	if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
	TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
	if (!hwdevice) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}

	if (hwdevice->type() != TDEGenericDeviceType::Disk) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);

	// FIXME
	// Options from 'options' are not currently loaded into 'mountOptions'
	TQString mountOptions;

	TQStringVariantMap unmountResult = sdevice->unmountDevice();
	if (unmountResult["result"].toBool() == false) {
		// Unmount failed!
		return ErrorCodes::CommunicationError;
	}
	else {
		return ErrorCodes::Success;
	}
#else
		return ErrorCodes::Device_Volume_NoSuchDevice;
#endif
}


int K3bDevice::Connection::eject(K3bDevice::Device* dev, const TQStringList& options)
{
#ifdef HAVE_TDEHWLIB
	if (!d->deviceUdiMap.contains(dev->blockDeviceName())) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TQString udi = d->deviceUdiMap[dev->blockDeviceName()];
	TDEGenericDevice *hwdevice = d->m_hwdevices->findByUniqueID(udi);
	if (!hwdevice) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}

	if (hwdevice->type() != TDEGenericDeviceType::Disk) {
		return ErrorCodes::Device_Volume_NoSuchDevice;
	}
	TDEStorageDevice* sdevice = static_cast<TDEStorageDevice*>(hwdevice);

	if (sdevice->ejectDriveMedia()) {
		return ErrorCodes::Success;
	}
	else {
		return ErrorCodes::Device_Volume_InvalidEjectOption;
	}
#else
		return ErrorCodes::Device_Volume_NoSuchDevice;
#endif
}

#include "k3bconnection.moc"