KCompactDisc

kcompactdisc.cpp
1/*
2 * KCompactDisc - A CD drive interface for the KDE Project.
3 *
4 * Copyright (C) 2005 Shaheedur R. Haque <srhaque@iee.org>
5 * Copyright (C) 2007 Alexander Kern <alex.kern@gmx.de>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22#include "kcompactdisc.h"
23#include "kcompactdisc_p.h"
24
25#include <config-alsa.h>
26
27#include <QDBusInterface>
28#include <QDBusReply>
29#include <QUrl>
30#include <QtGlobal>
31
32#include <Solid/Device>
33#include <Solid/Block>
34#include <Solid/OpticalDrive>
35
36static QMap<QString, QUrl> cdromsNameToDeviceUrl;
37static QMap<QString, QString> cdromsNameToUdi;
38static QString ___null = QString();
39
40static void refreshListOfCdromDevices()
41{
42 cdromsNameToDeviceUrl.clear();
43 cdromsNameToUdi.clear();
45 QUrl url;
46
47 //get a list of all devices that are Cdrom
48 const auto devices = Solid::Device::listFromType(Solid::DeviceInterface::OpticalDrive);
49 for (const Solid::Device &device : devices) {
50 qDebug() << device.udi().toLatin1().constData();
51 const Solid::Block *b = device.as<Solid::Block>();
52
53 if(!b) {
54 continue;
55 }
56
57 const Solid::OpticalDrive *o = device.as<Solid::OpticalDrive>();
59
61 //TODO translate them ?
62 if(mediumType < Solid::OpticalDrive::Cdrw) {
63 type = QLatin1String( "CD-ROM" );
64 } else if(mediumType < Solid::OpticalDrive::Dvd) {
65 type = QLatin1String( "CDRW" );
66 } else if(mediumType < Solid::OpticalDrive::Dvdr) {
67 type = QLatin1String( "DVD-ROM" );
68 } else if(mediumType < Solid::OpticalDrive::Bd) {
69 type = QLatin1String( "DVDRW" );
70 } else if(mediumType < Solid::OpticalDrive::HdDvd) {
71 type = QLatin1String( "Blu-ray" );
72 } else {
73 type = QLatin1String( "High Density DVD" );
74 }
75
76 if(!device.vendor().isEmpty())
77 name = (QLatin1Char('[') + type + QLatin1String( " - " ) + device.vendor() + QLatin1String( " - " ) + device.product() + QLatin1Char( ']' ));
78 else
79 name = (QLatin1Char('[') + type + QLatin1String( " - unknown vendor - " ) + device.product() + QLatin1Char( ']' ));
80
81 cdromsNameToDeviceUrl.insert(name, url);
82 cdromsNameToUdi.insert(name, device.udi());
83 }
84#if 0
85 if(cdromsNameToDeviceUrl.empty()) {
86 cdromsNameToDeviceUrl.insert(QString("Generic CDROM []"), QUrl::fromPath(wm_drive_default_device()));
87 }
88#endif
89}
90
91static QMap<QString, QUrl> &getListOfCdromDevicesNamesAndUrl()
92{
93 if(cdromsNameToDeviceUrl.empty())
94 refreshListOfCdromDevices();
95
96 return cdromsNameToDeviceUrl;
97}
98
99static QMap<QString, QString> &getListOfCdromDevicesNamesAndUdi()
100{
101 if(cdromsNameToUdi.empty())
102 refreshListOfCdromDevices();
103
104 return cdromsNameToUdi;
105}
106
108{
109 if(deviceUrl.scheme() == QLatin1String( "media" ) || deviceUrl.scheme() == QLatin1String( "system" )) {
110 qDebug() << "Asking mediamanager for " << deviceUrl.fileName();
111
112 QDBusInterface mediamanager( QLatin1String( "org.kde.kded" ), QLatin1String( "/modules/mediamanager" ), QLatin1String( "org.kde.MediaManager" ) );
113 QDBusReply<QStringList> reply = mediamanager.call(QLatin1String( "properties" ), deviceUrl.fileName());
114
115 QStringList properties = reply;
116 if(!reply.isValid() || properties.count() < 6) {
117 qCritical() << "Invalid reply from mediamanager";
118 return deviceUrl.path();
119 } else {
120 qDebug() << "Reply from mediamanager " << properties[5];
121 return properties[5];
122 }
123 } else if(deviceUrl.scheme() == QLatin1String( "file" )) {
124 return deviceUrl.path();
125 } else {
126 return QString();
127 }
128}
129
131{
132 QStringList list;
133
134 list << QLatin1String( "phonon" )
135#if defined(HAVE_ALSA)
136 << QLatin1String( "alsa" )
137#endif
138#if defined(sun) || defined(__sun__)
139 << QLatin1String( "sun" )
140#endif
141 ;
142 return list;
143}
144
146{
147 return getListOfCdromDevicesNamesAndUrl().keys();
148}
149
151{
152 const QStringList names = getListOfCdromDevicesNamesAndUrl().keys();
153 if (!names.isEmpty()) return names[0];
154 else return QString();
155}
156
158{
159 const QList<QUrl> urls = getListOfCdromDevicesNamesAndUrl().values();
160 if (!urls.isEmpty()) return urls[0];
161 else return QUrl();
162}
163
164const QUrl KCompactDisc::cdromDeviceUrl(const QString &cdromDeviceName)
165{
166 const QMap<QString, QUrl> &nameUrls = getListOfCdromDevicesNamesAndUrl();
167 QUrl result = nameUrls.value(cdromDeviceName);
168 if (!result.isValid())
169 {
170 const QUrl passedUrl = QUrl::fromLocalFile(cdromDeviceName);
171 for (const QUrl &url : nameUrls) {
172 if (url == passedUrl)
173 {
174 return passedUrl;
175 }
176 }
178 }
179 return result;
180}
181
183{
184 const QStringList udis = getListOfCdromDevicesNamesAndUdi().values();
185 if (!udis.isEmpty()) return udis[0];
186 else return QString();
187}
188
189const QString KCompactDisc::cdromDeviceUdi(const QString &cdromDeviceName)
190{
191 return getListOfCdromDevicesNamesAndUdi().value(cdromDeviceName, KCompactDisc::defaultCdromDeviceUdi());
192}
193
194KCompactDisc::KCompactDisc(InformationMode infoMode) :
195 d_ptr(new KCompactDiscPrivate(this, KCompactDisc::defaultCdromDeviceName()))
196{
198 d->m_infoMode = infoMode;
199}
200
201KCompactDisc::~KCompactDisc()
202{
203 stop();
204 delete d_ptr;
205}
206
208{
210 return d->m_deviceVendor;
211}
212
214{
216 return d->m_deviceModel;
217}
218
220{
222 return d->m_deviceRevision;
223}
224
226{
228 return d->m_deviceName;
229}
230
232{
234 return KCompactDisc::cdromDeviceUrl(d->m_deviceName);
235}
236
238{
240 return d->m_discId;
241}
242
244{
246 return d->m_trackStartFrames;
247}
248
250{
252 if (!d->m_tracks)
253 return ___null;
254 return d->m_trackArtists[0];
255}
256
258{
260 if (!d->m_tracks)
261 return ___null;
262 return d->m_trackTitles[0];
263}
264
266{
268 if (!d->m_tracks)
269 return 0;
270 return d->m_discLength;
271}
272
274{
276 return d->m_discPosition;
277}
278
279KCompactDisc::DiscStatus KCompactDisc::discStatus()
280{
282 return d->m_status;
283}
284
286{
287 return KCompactDiscPrivate::discStatusI18n(status);
288}
289
291{
293 return trackArtist(d->m_track);
294}
295
297{
299 if (!track)
300 return QString();
301 return d->m_trackArtists[track];
302}
303
305{
307 return trackTitle(d->m_track);
308}
309
311{
313 if (!track)
314 return QString();
315 return d->m_trackTitles[track];
316}
317
319{
321 return trackLength(d->m_track);
322}
323
324unsigned KCompactDisc::trackLength(unsigned track)
325{
327 if (!track)
328 return 0;
329 return d->trackLength(track);
330}
331
333{
335 return d->m_track;
336}
337
339{
341 return d->m_trackPosition;
342}
343
345{
347 return d->m_tracks;
348}
349
351{
353 return (d->m_status == KCompactDisc::Playing);
354}
355
357{
359 return (d->m_status == KCompactDisc::Paused);
360}
361
363{
365 return (d->m_status == KCompactDisc::NoDisc);
366}
367
368bool KCompactDisc::isAudio(unsigned track)
369{
371 if (!track)
372 return 0;
373 return d->isTrackAudio(track);
374}
375
376void KCompactDisc::playTrack(unsigned track)
377{
379
380 d->m_statusExpected = KCompactDisc::Playing;
381 d->m_trackExpectedPosition = 0;
382 d->m_seek = abs(int(d->m_trackExpectedPosition - trackPosition()));
383
384 d->playTrackPosition(track, 0);
385}
386
387void KCompactDisc::playPosition(unsigned position)
388{
390
391 d->m_statusExpected = Playing;
392 d->m_trackExpectedPosition = position;
393 d->m_seek = abs(int(d->m_trackExpectedPosition - trackPosition()));
394
395 d->playTrackPosition(d->m_track, position);
396}
397
399{
400 doCommand(KCompactDisc::Play);
401}
402
404{
405 doCommand(KCompactDisc::Next);
406}
407
409{
410 doCommand(KCompactDisc::Prev);
411}
412
414{
415 doCommand(KCompactDisc::Pause);
416}
417
419{
420 doCommand(KCompactDisc::Stop);
421}
422
424{
425 doCommand(KCompactDisc::Eject);
426}
427
429{
430 doCommand(KCompactDisc::Loop);
431}
432
434{
435 doCommand(KCompactDisc::Random);
436}
437
438void KCompactDisc::doCommand(KCompactDisc::DiscCommand cmd)
439{
441 unsigned track;
442
443 switch(cmd) {
444 case Play:
445 if(d->m_status == KCompactDisc::Playing)
446 return;
447 next();
448 break;
449
450 case Next:
451 track = d->getNextTrackInPlaylist();
452 if(track)
454 break;
455
456 case Prev:
457 track = d->getPrevTrackInPlaylist();
458 if(track)
460 break;
461
462 case Pause:
463 if(d->m_status == KCompactDisc::Paused)
464 d->m_statusExpected = KCompactDisc::Playing;
465 else
466 d->m_statusExpected = KCompactDisc::Paused;
467
468 d->pause();
469 break;
470
471 case Stop:
472 d->m_statusExpected = KCompactDisc::Stopped;
473 d->stop();
474 break;
475
476 case Eject:
477 if(d->m_status != KCompactDisc::Ejected) {
478 if(d->m_status != KCompactDisc::Stopped) {
479 d->m_statusExpected = KCompactDisc::Ejected;
480 d->stop();
481 } else {
482 d->eject();
483 }
484 } else {
485 d->m_statusExpected = KCompactDisc::Stopped;
486 d->closetray();
487 }
488 break;
489
490 case Loop:
491 setLoopPlaylist(!d->m_loopPlaylist);
492 break;
493
494 case Random:
495 setRandomPlaylist(!d->m_randomPlaylist);
496 break;
497 }
498}
499
500void KCompactDisc::metadataLookup()
501{
503 d->queryMetadata();
504}
505
506void KCompactDisc::setRandomPlaylist(bool random)
507{
509 d->m_randomPlaylist = random;
510 d->make_playlist();
511 Q_EMIT randomPlaylistChanged(d->m_randomPlaylist);
512}
513
514void KCompactDisc::setLoopPlaylist(bool loop)
515{
517 d->m_loopPlaylist = loop;
518 Q_EMIT loopPlaylistChanged(d->m_loopPlaylist);
519}
520
521void KCompactDisc::setAutoMetadataLookup(bool autoMetadata)
522{
524 d->m_autoMetadata = autoMetadata;
525 if(d->m_autoMetadata)
526 metadataLookup();
527}
528
529bool KCompactDisc::setDevice(const QString &deviceName, unsigned volume,
530 bool digitalPlayback, const QString &audioSystem, const QString &audioDevice)
531{
532 const QString as = digitalPlayback ? audioSystem : QLatin1String("cdin");
533 const QString ad = digitalPlayback ? audioDevice : QString();
534 qDebug() << "Device init: " << deviceName << ", " << as << ", " << ad;
535
536 if(d_ptr->moveInterface(deviceName, as, ad)) {
537 setVolume(volume);
538 return 1;
539 } else {
540 // Severe (OS-level) error.
541 return 0;
542 }
543}
544
545void KCompactDisc::setVolume(unsigned volume)
546{
548 qDebug() << "change volume: " << volume;
549 d->setVolume(volume);
550}
551
552void KCompactDisc::setBalance(unsigned balance)
553{
555 qDebug() << "change balance: " << balance;
556 d->setBalance(balance);
557}
558
559#include "moc_kcompactdisc.cpp"
KCompactDisc - A CD drive interface for the KDE Project.
static const QStringList audioSystems()
All installed audio backends.
static const QUrl cdromDeviceUrl(const QString &)
The Url of named CDROM device for this system.
void pause()
Pause/resume playout.
void playTrack(unsigned int track)
Start playout of track.
static const QStringList cdromDeviceNames()
All present CDROM devices.
const QString & deviceVendor()
SCSI parameter VENDOR of current CDROM device.
unsigned discId()
Current disc, 0 if no disc or impossible to calculate id.
bool setDevice(const QString &device, unsigned volume=50, bool digitalPlayback=true, const QString &audioSystem=QString(), const QString &audioDevice=QString())
const QList< unsigned > & discSignature()
CDDB signature of disc, empty if no disc or not possible to deliver.
unsigned trackLength()
Length of current track.
void random()
Switch random playout on/off.
KCompactDisc::DiscStatus discStatus()
Current status.
const QString & deviceRevision()
SCSI parameter REVISION of current CDROM device.
bool isPaused()
Is status pausing.
void prev()
Start playout of previous track.
void next()
Start playout of next track.
static const QString defaultCdromDeviceName()
The default CDROM device for this system.
bool isPlaying()
Is status playing.
static const QString defaultCdromDeviceUdi()
The Udi of default CDROM device for this system.
unsigned trackPosition()
Current track position.
unsigned discPosition()
Current position on the disc.
void play()
Start playout.
void playPosition(unsigned int position)
Start playout or seek to given position of track.
static const QString cdromDeviceUdi(const QString &)
The Udi of named CDROM device for this system.
const QString & deviceModel()
SCSI parameter MODEL of current CDROM device.
static QString urlToDevice(const QUrl &url)
If the url is a media:/ or system:/ URL returns the device it represents, otherwise returns device.
QString trackArtist()
Artist of current track.
void setBalance(unsigned int balance)
Set balance.
const QString & deviceName()
Current CDROM device.
const QUrl deviceUrl()
Current device as QUrl.
QString trackTitle()
Title of current track.
const QString & discTitle()
Title of disc.
void loop()
Switch endless playout on/off.
void eject()
Open/close tray.
unsigned track()
Current track.
bool isNoDisc()
Is status no disc.
unsigned discLength()
Known length of disc.
void doCommand(KCompactDisc::DiscCommand)
Pipe GUI command.
void stop()
Stop playout.
static const QUrl defaultCdromDeviceUrl()
The Url of default CDROM device for this system.
unsigned tracks()
Number of tracks.
QString discStatusString(KCompactDisc::DiscStatus status)
Status as string.
bool isAudio(unsigned track)
const QString & discArtist()
Artist for whole disc.
void setVolume(unsigned int volume)
Set volume.
QString device() const
static QList< Device > listFromType(const DeviceInterface::Type &type, const QString &parentUdi=QString())
MediumTypes supportedMedia() const
Q_SCRIPTABLE CaptureState status()
QString name(GameStandardAction id)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
QDBusMessage call(QDBus::CallMode mode, const QString &method, Args &&... args)
bool isValid() const const
bool isEmpty() const const
void clear()
bool empty() const const
iterator insert(const Key &key, const T &value)
T value(const Key &key, const T &defaultValue) const const
QList< T > values() const const
Q_EMITQ_EMIT
QByteArray toLatin1() const const
QString fileName(ComponentFormattingOptions options) const const
QUrl fromLocalFile(const QString &localFile)
QUrl fromUserInput(const QString &userInput, const QString &workingDirectory, UserInputResolutionOptions options)
bool isValid() const const
QString path(ComponentFormattingOptions options) const const
QString scheme() const const
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:57:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.