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
|
/*
Copyright 2006 Kevin Ottens <ervin@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) version 3, or any
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "halcdrom.h"
#include <QtCore/QStringList>
#include <QtDBus/QDBusError>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>
#include <QtDBus/QDBusVariant>
#include "halfstabhandling.h"
using namespace Solid::Backends::Hal;
Cdrom::Cdrom(HalDevice* device)
: Storage(device), m_ejectInProgress(false)
{
connect(device, SIGNAL(conditionRaised(QString, QString)),
this, SLOT(slotCondition(QString, QString)));
m_device->registerAction("eject", this,
SLOT(slotEjectRequested()),
SLOT(slotEjectDone(int, QString)));
}
Cdrom::~Cdrom()
{
}
Solid::OpticalDrive::MediumTypes Cdrom::supportedMedia() const
{
Solid::OpticalDrive::MediumTypes supported;
QMap<Solid::OpticalDrive::MediumType, QString> map;
map[Solid::OpticalDrive::Cdr] = "storage.cdrom.cdr";
map[Solid::OpticalDrive::Cdrw] = "storage.cdrom.cdrw";
map[Solid::OpticalDrive::Dvd] = "storage.cdrom.dvd";
map[Solid::OpticalDrive::Dvdr] = "storage.cdrom.dvdr";
map[Solid::OpticalDrive::Dvdrw] = "storage.cdrom.dvdrw";
map[Solid::OpticalDrive::Dvdram] = "storage.cdrom.dvdram";
map[Solid::OpticalDrive::Dvdplusr] = "storage.cdrom.dvdplusr";
map[Solid::OpticalDrive::Dvdplusrw] = "storage.cdrom.dvdplusrw";
map[Solid::OpticalDrive::Dvdplusdl] = "storage.cdrom.dvdplusrdl";
map[Solid::OpticalDrive::Dvdplusdlrw] = "storage.cdrom.dvdplusrwdl";
map[Solid::OpticalDrive::Bd] = "storage.cdrom.bd";
map[Solid::OpticalDrive::Bdr] = "storage.cdrom.bdr";
map[Solid::OpticalDrive::Bdre] = "storage.cdrom.bdre";
map[Solid::OpticalDrive::HdDvd] = "storage.cdrom.hddvd";
map[Solid::OpticalDrive::HdDvdr] = "storage.cdrom.hddvdr";
map[Solid::OpticalDrive::HdDvdrw] = "storage.cdrom.hddvdrw";
foreach (const Solid::OpticalDrive::MediumType type, map.keys()) {
if (m_device->prop(map[type]).toBool()) {
supported |= type;
}
}
return supported;
}
int Cdrom::readSpeed() const
{
return m_device->prop("storage.cdrom.read_speed").toInt();
}
int Cdrom::writeSpeed() const
{
return m_device->prop("storage.cdrom.write_speed").toInt();
}
QList<int> Cdrom::writeSpeeds() const
{
QList<int> speeds;
QStringList speed_strlist = m_device->prop("storage.cdrom.write_speeds").toStringList();
foreach (const QString& speed_str, speed_strlist) {
speeds << speed_str.toInt();
}
return speeds;
}
void Cdrom::slotCondition(const QString& name, const QString& /*reason */)
{
if (name == "EjectPressed") {
emit ejectPressed(m_device->udi());
}
}
bool Cdrom::eject()
{
if (m_ejectInProgress) {
return false;
}
m_ejectInProgress = true;
m_device->broadcastActionRequested("eject");
if (FstabHandling::isInFstab(m_device->prop("block.device").toString())) {
return callSystemEject();
}
else {
return callHalDriveEject();
}
}
void Cdrom::slotEjectRequested()
{
m_ejectInProgress = true;
emit ejectRequested(m_device->udi());
}
bool Cdrom::callHalDriveEject()
{
QString udi = m_device->udi();
QString interface = "org.freedesktop.Hal.Device.Storage";
// HACK: Eject doesn't work on cdrom drives when there's a mounted disc,
// let's try to workaround this by calling a child volume...
if (m_device->prop("storage.removable.media_available").toBool()) {
QDBusInterface manager("org.freedesktop.Hal",
"/org/freedesktop/Hal/Manager",
"org.freedesktop.Hal.Manager",
QDBusConnection::systemBus());
QDBusReply<QStringList> reply = manager.call("FindDeviceStringMatch", "info.parent", udi);
if (reply.isValid()) {
const QStringList udis = reply;
if (!udis.isEmpty()) {
udi = udis[0];
interface = "org.freedesktop.Hal.Device.Volume";
}
}
}
QDBusConnection c = QDBusConnection::systemBus();
QDBusMessage msg = QDBusMessage::createMethodCall("org.freedesktop.Hal", udi,
interface, "Eject");
msg << QStringList();
return c.callWithCallback(msg, this,
SLOT(slotDBusReply(QDBusMessage)),
SLOT(slotDBusError(QDBusError)));
}
bool Solid::Backends::Hal::Cdrom::callSystemEject()
{
const QString device = m_device->prop("block.device").toString();
m_process = FstabHandling::callSystemCommand("eject", device,
this, SLOT(slotProcessFinished(int, QProcess::ExitStatus)));
return m_process != nullptr;
}
void Cdrom::slotDBusReply(const QDBusMessage& /*reply*/)
{
m_ejectInProgress = false;
m_device->broadcastActionDone("eject");
}
void Cdrom::slotDBusError(const QDBusError& error)
{
m_ejectInProgress = false;
// TODO: Better error reporting here
m_device->broadcastActionDone("eject", Solid::UnauthorizedOperation,
QString(error.name() + ": " + error.message()));
}
void Solid::Backends::Hal::Cdrom::slotProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
Q_UNUSED(exitStatus);
if (m_ejectInProgress) {
m_ejectInProgress = false;
if (exitCode == 0) {
m_device->broadcastActionDone("eject");
}
else {
m_device->broadcastActionDone("eject", Solid::UnauthorizedOperation,
m_process->readAllStandardError());
}
}
delete m_process;
}
void Cdrom::slotEjectDone(int error, const QString& errorString)
{
m_ejectInProgress = false;
emit ejectDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());
}
#include "backends/hal/moc_halcdrom.cpp"
|