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
|
/*
*
* $Id: k3bcore.cpp 619556 2007-01-03 17:38:12Z trueg $
* Copyright (C) 2003 Sebastian Trueg <trueg@k3b.org>
*
* This file is part of the K3b project.
* Copyright (C) 1998-2007 Sebastian Trueg <trueg@k3b.org>
*
* 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 <config.h>
#include "k3bcore.h"
#include "k3bjob.h"
#include <k3bdevicemanager.h>
#ifdef HAVE_HAL
#include <k3bhalconnection.h>
#endif
#include <k3bexternalbinmanager.h>
#include <k3bdefaultexternalprograms.h>
#include <k3bglobals.h>
#include <k3bversion.h>
#include <k3bjob.h>
#include <k3bthreadwidget.h>
#include <k3bglobalsettings.h>
#include <k3bpluginmanager.h>
#include <klocale.h>
#include <kconfig.h>
#include <kaboutdata.h>
#include <kstandarddirs.h>
#include <kapplication.h>
#include <tqptrlist.h>
#include <tqthread.h>
#include <tqmutex.h>
static TQt::HANDLE s_guiThreadHandle = TQThread::currentThread();
// We cannot use TQWaitCondition here since the event might be handled faster
// than the thread starts the waiting
class DeviceBlockingEventDoneCondition {
public:
DeviceBlockingEventDoneCondition()
: m_done(false) {
}
void done() {
m_doneMutex.lock();
m_done = true;
m_doneMutex.unlock();
}
void wait() {
while( true ) {
m_doneMutex.lock();
bool done = m_done;
m_doneMutex.unlock();
if( done )
return;
}
}
private:
TQMutex m_doneMutex;
bool m_done;
};
class DeviceBlockingEvent : public TQCustomEvent
{
public:
DeviceBlockingEvent( bool block_, K3bDevice::Device* dev, DeviceBlockingEventDoneCondition* cond_, bool* success_ )
: TQCustomEvent( TQEvent::User + 33 ),
block(block_),
device(dev),
cond(cond_),
success(success_) {
}
bool block;
K3bDevice::Device* device;
DeviceBlockingEventDoneCondition* cond;
bool* success;
};
class K3bCore::Private {
public:
Private()
: version( LIBK3B_VERSION ),
config(0),
deleteConfig(false),
deviceManager(0),
externalBinManager(0),
pluginManager(0),
globalSettings(0) {
}
K3bVersion version;
KConfig* config;
bool deleteConfig;
K3bDevice::DeviceManager* deviceManager;
K3bExternalBinManager* externalBinManager;
K3bPluginManager* pluginManager;
K3bGlobalSettings* globalSettings;
TQValueList<K3bJob*> runningJobs;
TQValueList<K3bDevice::Device*> blockedDevices;
};
K3bCore* K3bCore::s_k3bCore = 0;
K3bCore::K3bCore( TQObject* parent, const char* name )
: TQObject( parent, name )
{
d = new Private();
if( s_k3bCore )
qFatal("ONLY ONE INSTANCE OF K3BCORE ALLOWED!");
s_k3bCore = this;
// create the thread widget instance in the GUI thread
K3bThreadWidget::instance();
}
K3bCore::~K3bCore()
{
s_k3bCore = 0;
delete d->globalSettings;
delete d;
}
K3bDevice::DeviceManager* K3bCore::deviceManager() const
{
const_cast<K3bCore*>(this)->initDeviceManager();
return d->deviceManager;
}
K3bExternalBinManager* K3bCore::externalBinManager() const
{
const_cast<K3bCore*>(this)->initExternalBinManager();
return d->externalBinManager;
}
K3bPluginManager* K3bCore::pluginManager() const
{
const_cast<K3bCore*>(this)->initPluginManager();
return d->pluginManager;
}
K3bGlobalSettings* K3bCore::globalSettings() const
{
const_cast<K3bCore*>(this)->initGlobalSettings();
return d->globalSettings;
}
const K3bVersion& K3bCore::version() const
{
return d->version;
}
KConfig* K3bCore::config() const
{
if( !d->config ) {
kdDebug() << "(K3bCore) opening k3b config file." << endl;
kdDebug() << "(K3bCore) while I am a " << className() << endl;
d->deleteConfig = true;
d->config = new KConfig( "k3brc" );
}
return d->config;
}
void K3bCore::init()
{
initGlobalSettings();
initExternalBinManager();
initDeviceManager();
initPluginManager();
// load the plugins before doing anything else
// they might add external bins
pluginManager()->loadAll();
externalBinManager()->search();
#ifdef HAVE_HAL
connect( K3bDevice::HalConnection::instance(), TQT_SIGNAL(deviceAdded(const TQString&)),
deviceManager(), TQT_SLOT(addDevice(const TQString&)) );
connect( K3bDevice::HalConnection::instance(), TQT_SIGNAL(deviceRemoved(const TQString&)),
deviceManager(), TQT_SLOT(removeDevice(const TQString&)) );
TQStringList devList = K3bDevice::HalConnection::instance()->devices();
if( devList.isEmpty() )
deviceManager()->scanBus();
else
for( unsigned int i = 0; i < devList.count(); ++i )
deviceManager()->addDevice( devList[i] );
#else
deviceManager()->scanBus();
#endif
}
void K3bCore::initGlobalSettings()
{
if( !d->globalSettings )
d->globalSettings = new K3bGlobalSettings();
}
void K3bCore::initExternalBinManager()
{
if( !d->externalBinManager ) {
d->externalBinManager = new K3bExternalBinManager( this );
K3b::addDefaultPrograms( d->externalBinManager );
}
}
void K3bCore::initDeviceManager()
{
if( !d->deviceManager )
d->deviceManager = new K3bDevice::DeviceManager( this );
}
void K3bCore::initPluginManager()
{
if( !d->pluginManager )
d->pluginManager = new K3bPluginManager( this );
}
void K3bCore::readSettings( KConfig* cnf )
{
KConfig* c = cnf;
if( !c )
c = config();
TQString oldGrp = c->group();
globalSettings()->readSettings( c );
deviceManager()->readConfig( c );
externalBinManager()->readConfig( c );
c->setGroup( oldGrp );
}
void K3bCore::saveSettings( KConfig* cnf )
{
KConfig* c = cnf;
if( !c )
c = config();
TQString oldGrp = c->group();
c->setGroup( "General Options" );
c->writeEntry( "config version", version() );
deviceManager()->saveConfig( c );
externalBinManager()->saveConfig( c );
d->globalSettings->saveSettings( c );
c->setGroup( oldGrp );
}
void K3bCore::registerJob( K3bJob* job )
{
d->runningJobs.append( job );
emit jobStarted( job );
if( K3bBurnJob* bj = dynamic_cast<K3bBurnJob*>( job ) )
emit burnJobStarted( bj );
}
void K3bCore::unregisterJob( K3bJob* job )
{
d->runningJobs.remove( job );
emit jobFinished( job );
if( K3bBurnJob* bj = dynamic_cast<K3bBurnJob*>( job ) )
emit burnJobFinished( bj );
}
bool K3bCore::jobsRunning() const
{
return !d->runningJobs.isEmpty();
}
const TQValueList<K3bJob*>& K3bCore::runningJobs() const
{
return d->runningJobs;
}
bool K3bCore::blockDevice( K3bDevice::Device* dev )
{
if( TQThread::currentThread() == s_guiThreadHandle ) {
return internalBlockDevice( dev );
}
else {
bool success = false;
DeviceBlockingEventDoneCondition w;
TQApplication::postEvent( this, new DeviceBlockingEvent( true, dev, &w, &success ) );
w.wait();
return success;
}
}
void K3bCore::unblockDevice( K3bDevice::Device* dev )
{
if( TQThread::currentThread() == s_guiThreadHandle ) {
internalUnblockDevice( dev );
}
else {
DeviceBlockingEventDoneCondition w;
TQApplication::postEvent( this, new DeviceBlockingEvent( false, dev, &w, 0 ) );
w.wait();
}
}
bool K3bCore::internalBlockDevice( K3bDevice::Device* dev )
{
if( !d->blockedDevices.contains( dev ) ) {
d->blockedDevices.append( dev );
return true;
}
else
return false;
}
void K3bCore::internalUnblockDevice( K3bDevice::Device* dev )
{
d->blockedDevices.remove( dev );
}
void K3bCore::customEvent( TQCustomEvent* e )
{
if( DeviceBlockingEvent* de = dynamic_cast<DeviceBlockingEvent*>(e) ) {
if( de->block )
*de->success = internalBlockDevice( de->device );
else
internalUnblockDevice( de->device );
de->cond->done();
}
}
#include "k3bcore.moc"
|