summaryrefslogtreecommitdiffstats
path: root/tdeioslave/media/mediamanager/mediamanager.cpp
blob: dba4c11502b132948081832aa0f73554bf36ea8f (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
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
/* This file is part of the KDE Project
   Copyright (c) 2004 Kévin Ottens <ervin ipsquad net>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "mediamanager.h"

#include <config.h>
#include <tqtimer.h>

#include <kdebug.h>
#include <tdeglobal.h>
#include <tdelocale.h>

#include <kdirnotify_stub.h>
#include <kstandarddirs.h>

#include "mediamanagersettings.h"

#include "fstabbackend.h"

#ifdef COMPILE_TDEHARDWAREBACKEND
#include "tdehardwarebackend.h"
#endif // COMPILE_TDEHARDWAREBACKEND

#ifdef COMPILE_LINUXCDPOLLING
#include "linuxcdpolling.h"
#endif //COMPILE_LINUXCDPOLLING

MediaManager::MediaManager(const TQCString &obj)
    : KDEDModule(obj), m_dirNotify(m_mediaList)
{
    connect( &m_mediaList, TQT_SIGNAL(mediumAdded(const TQString&, const TQString&, bool)),
             TQT_SLOT(slotMediumAdded(const TQString&, const TQString&, bool)) );
    connect( &m_mediaList, TQT_SIGNAL(mediumRemoved(const TQString&, const TQString&, bool)),
             TQT_SLOT(slotMediumRemoved(const TQString&, const TQString&, bool)) );
    connect( &m_mediaList,
             TQT_SIGNAL(mediumStateChanged(const TQString&, const TQString&, bool, bool)),
             TQT_SLOT(slotMediumChanged(const TQString&, const TQString&, bool, bool)) );

    TQTimer::singleShot( 10, this, TQT_SLOT( loadBackends() ) );
}

MediaManager::~MediaManager()
{
    while ( !m_backends.isEmpty() )
    {
        BackendBase *b = m_backends.first();
        m_backends.remove( b );
        delete b;
    }
}

void MediaManager::loadBackends()
{
    m_mediaList.blockSignals(true);

    while ( !m_backends.isEmpty() )
    {
        BackendBase *b = m_backends.first();
        m_backends.remove( b );
        delete b;
    }

    mp_removableBackend = 0L;
    m_tdebackend = 0L;
    m_fstabbackend = 0L;

#ifdef COMPILE_TDEHARDWAREBACKEND
    if ( MediaManagerSettings::self()->tdeHardwareBackendEnabled() )
    {
        m_mediaList.blockSignals(false);
        m_tdebackend = new TDEBackend(m_mediaList, this);
        m_backends.append( m_tdebackend );
        m_fstabbackend = new FstabBackend(m_mediaList, true);
        m_backends.append( m_fstabbackend );
        // No need to load something else...
        m_mediaList.blockSignals(false);
        return;
    }
#endif // COMPILE_TDEHARDWAREBACKEND

    mp_removableBackend = new RemovableBackend(m_mediaList);
    m_backends.append( mp_removableBackend );

#ifdef COMPILE_LINUXCDPOLLING
    if ( MediaManagerSettings::self()->cdPollingEnabled() )
    {
        m_backends.append( new LinuxCDPolling(m_mediaList) );
    }
#endif //COMPILE_LINUXCDPOLLING

    m_fstabbackend = new FstabBackend(m_mediaList);
    m_backends.append( m_fstabbackend );
    m_mediaList.blockSignals(false);
}

const Medium* MediaManager::getMediumByName(const TQString &name)
{
    const Medium *m = m_mediaList.findByName(name);
    if (!m)
    {
        KURL u(name);
        kdDebug() << "Media::getMediumByName " << name << " " << u.isValid() << endl;
        if (u.isValid())
        {
            if (u.protocol() == "system")
            {
                TQString path = u.path();
                if (path.startsWith("/media/"))
                {
                    path = path.mid(strlen("/media/"));
                }
                m = m_mediaList.findByName(path);
                kdDebug() << "findByName " << path << m << endl;
            }
            else if (u.protocol() == "media")
            {
                m = m_mediaList.findByName(u.filename());
                kdDebug() << "findByName " << u.filename() << m << endl;
            }
            else if (u.protocol() == "file")
            {
                // look for the mount point
                TQPtrList<Medium> list = m_mediaList.list();
                TQPtrList<Medium>::const_iterator it = list.begin();
                TQPtrList<Medium>::const_iterator end = list.end();
                TQString path;

                for (; it!=end; ++it)
                {
                    path = TDEStandardDirs::realFilePath(u.path());
                    kdDebug() << "comparing " << (*it)->mountPoint()  << " " << path << " " <<  (*it)->deviceNode() << endl;
                    if ((*it)->mountPoint() == path || (*it)->deviceNode() == path)
                    {
                        m = *it;
                        break;
                    }
                }
            }
        }
    }
    return m;
}

TQStringList MediaManager::fullList()
{
    TQPtrList<Medium> list = m_mediaList.list();

    TQStringList result;

    TQPtrList<Medium>::const_iterator it = list.begin();
    TQPtrList<Medium>::const_iterator end = list.end();
    for (; it!=end; ++it)
    {
        result+= (*it)->properties();
        result+= Medium::SEPARATOR;
    }

    return result;
}

TQStringList MediaManager::properties(const TQString &name)
{
    const Medium *m = getMediumByName(name);
    if (m)
		{
        return m->properties();
    }
    else
		{
        return TQStringList();
    }
}

TQStringList MediaManager::mountoptions(const TQString &name)
{
#ifdef COMPILE_TDEHARDWAREBACKEND
	if (m_tdebackend)
	{
		return m_tdebackend->mountoptions(name);
	}
#endif
	return TQStringList();
}

bool MediaManager::setMountoptions(const TQString &name, const TQStringList &options)
{
#ifdef COMPILE_TDEHARDWAREBACKEND
	if (m_tdebackend)
	{
		return m_tdebackend->setMountoptions(name, options);
	}
#endif
	return false;
}

TQStringVariantMap MediaManager::mount(const TQString &uid)
{
#ifdef COMPILE_TDEHARDWAREBACKEND
	if (m_tdebackend)
	{
		return m_tdebackend->mount(uid);
	}
#else
	if (m_fstabbackend)
	{
		return m_fstabbackend->mount(uid);
	}
#endif
	TQStringVariantMap result;
	result["errStr"] = i18n("Feature only available with the TDE hardware or fstab backend");
	result["result"] = false;
	return result;
}

TQStringVariantMap MediaManager::unmount(const TQString &uid)
{
#ifdef COMPILE_TDEHARDWAREBACKEND
	if (m_tdebackend)
	{
		return m_tdebackend->unmount(uid);
	}
#else
	if (m_fstabbackend)
	{
		return m_fstabbackend->unmount(uid);
	}
#endif
	TQStringVariantMap result;
	result["errStr"] = i18n("Feature only available with the TDE hardware or fstab backend");
	result["result"] = false;
	return result;
}

TQStringVariantMap MediaManager::unlock(const TQString &uid, const TQString &password)
{
#ifdef COMPILE_TDEHARDWAREBACKEND
	if (m_tdebackend)
	{
		return m_tdebackend->unlock(uid, password);
	}
#endif
	TQStringVariantMap result;
	result["errStr"] = i18n("Feature only available with the TDE hardware backend");
	result["result"] = false;
	return result;
}

TQStringVariantMap MediaManager::lock(const TQString &uid, bool releaseHolders)
{
#ifdef COMPILE_TDEHARDWAREBACKEND
	if (m_tdebackend)
	{
		return m_tdebackend->lock(uid, releaseHolders);
	}
#endif
	TQStringVariantMap result;
	result["errStr"] = i18n("Feature only available with the TDE hardware backend");
	result["result"] = false;
	return result;
}

TQStringVariantMap MediaManager::eject(const TQString &uid)
{
#ifdef COMPILE_TDEHARDWAREBACKEND
	if (m_tdebackend)
	{
		return m_tdebackend->eject(uid);
	}
#endif
	TQStringVariantMap result;
	result["errStr"] = i18n("Feature only available with the TDE hardware backend");
	result["result"] = false;
	return result;
}

TQStringVariantMap MediaManager::mountByNode(const TQString &deviceNode)
{
	const Medium *medium = m_mediaList.findByNode(deviceNode);
	if (!medium) {
		TQStringVariantMap result;
		result["errStr"] = i18n("No such medium: %1").arg(deviceNode);
		result["result"] = false;
		return result;
	}
	return mount(medium->id());
}

TQStringVariantMap MediaManager::unmountByNode(const TQString &deviceNode)
{
	const Medium *medium = m_mediaList.findByNode(deviceNode);
	if (!medium) {
		TQStringVariantMap result;
		result["errStr"] = i18n("No such medium: %1").arg(deviceNode);
		result["result"] = false;
		return result;
	}
	return unmount(medium->id());
}

TQStringVariantMap MediaManager::unlockByNode(const TQString &deviceNode, const TQString &password)
{
	const Medium *medium = m_mediaList.findByNode(deviceNode);
	if (!medium) {
		TQStringVariantMap result;
		result["errStr"] = i18n("No such medium: %1").arg(deviceNode);
		result["result"] = false;
		return result;
	}
	return unlock(medium->id(), password);
}

TQStringVariantMap MediaManager::lockByNode(const TQString &deviceNode, bool releaseHolders)
{
	const Medium *medium = m_mediaList.findByNode(deviceNode);
	if (!medium) {
		TQStringVariantMap result;
		result["errStr"] = i18n("No such medium: %1").arg(deviceNode);
		result["result"] = false;
		return result;
	}
	return lock(medium->id(), releaseHolders);
}

TQStringVariantMap MediaManager::ejectByNode(const TQString &deviceNode)
{
	const Medium *medium = m_mediaList.findByNode(deviceNode);
	if (!medium) {
		TQStringVariantMap result;
		result["errStr"] = i18n("No such medium: %1").arg(deviceNode);
		result["result"] = false;
		return result;
	}
	return eject(medium->id());
}

TQString MediaManager::mimeType(const TQString &name)
{
    const Medium *m = getMediumByName(name);
    if (m)
    {
        return m->mimeType();
    }
    else
    {
        return TQString::null;
    }
}

TQString MediaManager::nameForLabel(const TQString &label)
{
    const TQPtrList<Medium> media = m_mediaList.list();

    TQPtrList<Medium>::const_iterator it = media.begin();
    TQPtrList<Medium>::const_iterator end = media.end();
    for (; it!=end; ++it)
    {
        const Medium *m = *it;

        if (m->prettyLabel()==label)
        {
            return m->name();
        }
    }

    return TQString::null;
}

ASYNC MediaManager::setUserLabel(const TQString &name, const TQString &label)
{
    m_mediaList.setUserLabel(name, label);
}

ASYNC MediaManager::reloadBackends()
{
    MediaManagerSettings::self()->readConfig();
    loadBackends();
}

bool MediaManager::removablePlug(const TQString &devNode, const TQString &label)
{
    if (mp_removableBackend)
    {
        return mp_removableBackend->plug(devNode, label);
    }
    return false;
}

bool MediaManager::removableUnplug(const TQString &devNode)
{
    if (mp_removableBackend)
    {
        return mp_removableBackend->unplug(devNode);
    }
    return false;
}

bool MediaManager::removableCamera(const TQString &devNode)
{
    if (mp_removableBackend)
    {
        return mp_removableBackend->camera(devNode);
    }
    return false;
}


void MediaManager::slotMediumAdded(const TQString &/*id*/, const TQString &name,
                                   bool allowNotification)
{
    kdDebug(1219) << "MediaManager::slotMediumAdded: " << name << endl;

    KDirNotify_stub notifier("*", "*");
    notifier.FilesAdded( KURL("media:/") );

    emit mediumAdded(name, allowNotification);
    emit mediumAdded(name);
}

void MediaManager::slotMediumRemoved(const TQString &/*id*/, const TQString &name,
                                     bool allowNotification)
{
    kdDebug(1219) << "MediaManager::slotMediumRemoved: " << name << endl;

    KDirNotify_stub notifier("*", "*");
    notifier.FilesRemoved( KURL("media:/"+name) );

    emit mediumRemoved(name, allowNotification);
    emit mediumRemoved(name);
}

void MediaManager::slotMediumChanged(const TQString &/*id*/, const TQString &name,
                                     bool mounted, bool allowNotification)
{
    kdDebug(1219) << "MediaManager::slotMediumChanged: " << name << endl;

    KDirNotify_stub notifier("*", "*");
    notifier.FilesChanged( KURL("media:/"+name) );

    emit mediumChanged(name, allowNotification);
    emit mediumChanged(name);
}


extern "C" {
    KDE_EXPORT KDEDModule *create_mediamanager(const TQCString &obj)
    {
        TDEGlobal::locale()->insertCatalogue("tdeio_media");
        return new MediaManager(obj);
    }
}

#include "mediamanager.moc"