KCompactDisc

kcompactdisc.cpp
1 /*
2  * KCompactDisc - A CD drive interface for the KDE Project.
3  *
4  * Copyright (C) 2005 Shaheedur R. Haque <[email protected]>
5  * Copyright (C) 2007 Alexander Kern <[email protected]>
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 
36 static QMap<QString, QUrl> cdromsNameToDeviceUrl;
37 static QMap<QString, QString> cdromsNameToUdi;
38 static QString ___null = QString();
39 
40 static void refreshListOfCdromDevices()
41 {
42  cdromsNameToDeviceUrl.clear();
43  cdromsNameToUdi.clear();
44  QString name, type;
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 
91 static QMap<QString, QUrl> &getListOfCdromDevicesNamesAndUrl()
92 {
93  if(cdromsNameToDeviceUrl.empty())
94  refreshListOfCdromDevices();
95 
96  return cdromsNameToDeviceUrl;
97 }
98 
99 static 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 
164 const 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 
189 const QString KCompactDisc::cdromDeviceUdi(const QString &cdromDeviceName)
190 {
191  return getListOfCdromDevicesNamesAndUdi().value(cdromDeviceName, KCompactDisc::defaultCdromDeviceUdi());
192 }
193 
194 KCompactDisc::KCompactDisc(InformationMode infoMode) :
195  d_ptr(new KCompactDiscPrivate(this, KCompactDisc::defaultCdromDeviceName()))
196 {
197  Q_D(KCompactDisc);
198  d->m_infoMode = infoMode;
199 }
200 
201 KCompactDisc::~KCompactDisc()
202 {
203  stop();
204  delete d_ptr;
205 }
206 
208 {
209  Q_D(KCompactDisc);
210  return d->m_deviceVendor;
211 }
212 
214 {
215  Q_D(KCompactDisc);
216  return d->m_deviceModel;
217 }
218 
220 {
221  Q_D(KCompactDisc);
222  return d->m_deviceRevision;
223 }
224 
226 {
227  Q_D(KCompactDisc);
228  return d->m_deviceName;
229 }
230 
232 {
233  Q_D(KCompactDisc);
234  return KCompactDisc::cdromDeviceUrl(d->m_deviceName);
235 }
236 
238 {
239  Q_D(KCompactDisc);
240  return d->m_discId;
241 }
242 
244 {
245  Q_D(KCompactDisc);
246  return d->m_trackStartFrames;
247 }
248 
250 {
251  Q_D(KCompactDisc);
252  if (!d->m_tracks)
253  return ___null;
254  return d->m_trackArtists[0];
255 }
256 
258 {
259  Q_D(KCompactDisc);
260  if (!d->m_tracks)
261  return ___null;
262  return d->m_trackTitles[0];
263 }
264 
266 {
267  Q_D(KCompactDisc);
268  if (!d->m_tracks)
269  return 0;
270  return d->m_discLength;
271 }
272 
274 {
275  Q_D(KCompactDisc);
276  return d->m_discPosition;
277 }
278 
279 KCompactDisc::DiscStatus KCompactDisc::discStatus()
280 {
281  Q_D(KCompactDisc);
282  return d->m_status;
283 }
284 
285 QString KCompactDisc::discStatusString(KCompactDisc::DiscStatus status)
286 {
287  return KCompactDiscPrivate::discStatusI18n(status);
288 }
289 
291 {
292  Q_D(KCompactDisc);
293  return trackArtist(d->m_track);
294 }
295 
297 {
298  Q_D(KCompactDisc);
299  if (!track)
300  return QString();
301  return d->m_trackArtists[track];
302 }
303 
305 {
306  Q_D(KCompactDisc);
307  return trackTitle(d->m_track);
308 }
309 
311 {
312  Q_D(KCompactDisc);
313  if (!track)
314  return QString();
315  return d->m_trackTitles[track];
316 }
317 
319 {
320  Q_D(KCompactDisc);
321  return trackLength(d->m_track);
322 }
323 
325 {
326  Q_D(KCompactDisc);
327  if (!track)
328  return 0;
329  return d->trackLength(track);
330 }
331 
333 {
334  Q_D(KCompactDisc);
335  return d->m_track;
336 }
337 
339 {
340  Q_D(KCompactDisc);
341  return d->m_trackPosition;
342 }
343 
345 {
346  Q_D(KCompactDisc);
347  return d->m_tracks;
348 }
349 
351 {
352  Q_D(KCompactDisc);
353  return (d->m_status == KCompactDisc::Playing);
354 }
355 
357 {
358  Q_D(KCompactDisc);
359  return (d->m_status == KCompactDisc::Paused);
360 }
361 
363 {
364  Q_D(KCompactDisc);
365  return (d->m_status == KCompactDisc::NoDisc);
366 }
367 
369 {
370  Q_D(KCompactDisc);
371  if (!track)
372  return 0;
373  return d->isTrackAudio(track);
374 }
375 
377 {
378  Q_D(KCompactDisc);
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 
387 void KCompactDisc::playPosition(unsigned position)
388 {
389  Q_D(KCompactDisc);
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 
438 void KCompactDisc::doCommand(KCompactDisc::DiscCommand cmd)
439 {
440  Q_D(KCompactDisc);
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)
453  playTrack(track);
454  break;
455 
456  case Prev:
457  track = d->getPrevTrackInPlaylist();
458  if(track)
459  playTrack(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 
500 void KCompactDisc::metadataLookup()
501 {
502  Q_D(KCompactDisc);
503  d->queryMetadata();
504 }
505 
506 void KCompactDisc::setRandomPlaylist(bool random)
507 {
508  Q_D(KCompactDisc);
509  d->m_randomPlaylist = random;
510  d->make_playlist();
511  Q_EMIT randomPlaylistChanged(d->m_randomPlaylist);
512 }
513 
514 void KCompactDisc::setLoopPlaylist(bool loop)
515 {
516  Q_D(KCompactDisc);
517  d->m_loopPlaylist = loop;
518  Q_EMIT loopPlaylistChanged(d->m_loopPlaylist);
519 }
520 
521 void KCompactDisc::setAutoMetadataLookup(bool autoMetadata)
522 {
523  Q_D(KCompactDisc);
524  d->m_autoMetadata = autoMetadata;
525  if(d->m_autoMetadata)
526  metadataLookup();
527 }
528 
529 bool 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 
545 void KCompactDisc::setVolume(unsigned volume)
546 {
547  Q_D(KCompactDisc);
548  qDebug() << "change volume: " << volume;
549  d->setVolume(volume);
550 }
551 
552 void KCompactDisc::setBalance(unsigned balance)
553 {
554  Q_D(KCompactDisc);
555  qDebug() << "change balance: " << balance;
556  d->setBalance(balance);
557 }
void next()
Start playout of next track.
static const QString cdromDeviceUdi(const QString &)
The Udi of named CDROM device for this system.
const QString & deviceRevision()
SCSI parameter REVISION of current CDROM device.
void playPosition(unsigned int position)
Start playout or seek to given position of track.
void pause()
Pause/resume playout.
QString name(const QVariant &location)
bool empty() const const
QList< T > values() const const
const QString & deviceModel()
SCSI parameter MODEL of current CDROM device.
static const QUrl cdromDeviceUrl(const QString &)
The Url of named CDROM device for this system.
QString discStatusString(KCompactDisc::DiscStatus status)
Status as string.
static const QStringList audioSystems()
All installed audio backends.
void play()
Start playout.
void doCommand(KCompactDisc::DiscCommand)
Pipe GUI command.
bool isValid() const const
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.
unsigned discPosition()
Current position on the disc.
void clear()
QUrl fromUserInput(const QString &userInput)
const QString & deviceName()
Current CDROM device.
const QString & discArtist()
Artist for whole disc.
unsigned track()
Current track.
MediumTypes supportedMedia() const
int count(const T &value) const const
bool setDevice(const QString &device, unsigned volume=50, bool digitalPlayback=true, const QString &audioSystem=QString(), const QString &audioDevice=QString())
void prev()
Start playout of previous track.
const QString & deviceVendor()
SCSI parameter VENDOR of current CDROM device.
void playTrack(unsigned int track)
Start playout of track.
bool isAudio(unsigned track)
unsigned trackLength()
Length of current track.
Type type(const QSqlDatabase &db)
bool isEmpty() const const
unsigned trackPosition()
Current track position.
QString path(QUrl::ComponentFormattingOptions options) const const
unsigned discId()
Current disc, 0 if no disc or impossible to calculate id.
unsigned discLength()
Known length of disc.
KCompactDisc::DiscStatus discStatus()
Current status.
bool isPlaying()
Is status playing.
unsigned tracks()
Number of tracks.
QString scheme() const const
void random()
Switch random playout on/off.
void loop()
Switch endless playout on/off.
KCompactDisc - A CD drive interface for the KDE Project.
Definition: kcompactdisc.h:86
static const QString defaultCdromDeviceName()
The default CDROM device for this system.
const QList< unsigned > & discSignature()
CDDB signature of disc, empty if no disc or not possible to deliver.
static QList< Device > listFromType(const DeviceInterface::Type &type, const QString &parentUdi=QString())
bool isNoDisc()
Is status no disc.
const QUrl deviceUrl()
Current device as QUrl.
static const QStringList cdromDeviceNames()
All present CDROM devices.
bool isValid() const const
QDBusMessage call(const QString &method, Args &&...args)
QByteArray toLatin1() const const
QString trackTitle()
Title of current track.
static const QString defaultCdromDeviceUdi()
The Udi of default CDROM device for this system.
const QString & discTitle()
Title of disc.
QString device() const
void eject()
Open/close tray.
void setVolume(unsigned int volume)
Set volume.
static const QUrl defaultCdromDeviceUrl()
The Url of default CDROM device for this system.
QMap::iterator insert(const Key &key, const T &value)
void stop()
Stop playout.
bool isPaused()
Is status pausing.
QString fileName(QUrl::ComponentFormattingOptions options) const const
Q_EMITQ_EMIT
QUrl fromLocalFile(const QString &localFile)
const T value(const Key &key, const T &defaultValue) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 18 2021 23:23:11 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.