File: udisksopticaldrive.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (221 lines) | stat: -rw-r--r-- 7,080 bytes parent folder | download
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
/*
    Copyright 2010 Michael Zanetti <mzanetti@kde.org>
    Copyright 2010-2012 Lukáš Tinkl <ltinkl@redhat.com>

    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 <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <QDebug>
#include <QFile>

#include "dbus/manager.h"
#include "udisks2.h"
#include "udisksdevice.h"
#include "udisksopticaldrive.h"

using namespace Solid::Backends::UDisks2;

OpticalDrive::OpticalDrive(Device* device)
	: StorageDrive(device), m_ejectInProgress(false), m_readSpeed(0), m_writeSpeed(0), m_speedsInit(false)
{
	m_device->registerAction("eject", this,
	                         SLOT(slotEjectRequested()),
	                         SLOT(slotEjectDone(int, const QString&)));

	connect(m_device, SIGNAL(changed()), this, SLOT(slotChanged()));
}

OpticalDrive::~OpticalDrive()
{
}

bool OpticalDrive::eject()
{
	if (m_ejectInProgress)
		return false;
	m_ejectInProgress = true;
	m_device->broadcastActionRequested("eject");

	const QString path = m_device->udi();
	QDBusConnection c = QDBusConnection::connectToBus(QDBusConnection::SystemBus, "Solid::Udisks2::OpticalDrive::" + path);

	// if the device is mounted, unmount first
	QString blockPath;
	org::freedesktop::DBus::ObjectManager manager(UD2_DBUS_SERVICE, UD2_DBUS_PATH, c);
	QDBusPendingReply<DBUSManagerStruct> reply = manager.GetManagedObjects();
	reply.waitForFinished();
	if (!reply.isError()) {// enum devices
		Q_FOREACH (const QDBusObjectPath& objPath, reply.value().keys()) {
			const QString udi = objPath.path();

			//qDebug() << "Inspecting" << udi;

			if (udi == UD2_DBUS_PATH_MANAGER || udi == UD2_UDI_DISKS_PREFIX || udi.startsWith(UD2_DBUS_PATH_JOBS))
				continue;

			Device device(udi);
			if (device.drivePath() == path && device.isMounted()) {
				//qDebug() << "Got mounted block device:" << udi;
				blockPath = udi;
				break;
			}
		}
	}
	else// show error
	{
		qWarning() << "Failed enumerating UDisks2 objects:" << reply.error().name() << "\n"
				   << reply.error().message();
	}

	if (!blockPath.isEmpty()) {
		//qDebug() << "Calling unmount on" << blockPath;
		QDBusMessage msg = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, blockPath, UD2_DBUS_INTERFACE_FILESYSTEM, "Unmount");
		msg << QVariantMap();// options, unused now
		c.call(msg, QDBus::BlockWithGui);
	}

	QDBusMessage msg = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, path, UD2_DBUS_INTERFACE_DRIVE, "Eject");
	msg << QVariantMap();
	return c.callWithCallback(msg, this, SLOT(slotDBusReply(const QDBusMessage&)), SLOT(slotDBusError(const QDBusError&)));
}

void OpticalDrive::slotDBusReply(const QDBusMessage& /*reply*/)
{
	m_ejectInProgress = false;
	m_device->broadcastActionDone("eject");
}

void OpticalDrive::slotDBusError(const QDBusError& error)
{
	m_ejectInProgress = false;
	m_device->broadcastActionDone("eject", m_device->errorToSolidError(error.name()),
	                              m_device->errorToString(error.name()) + ": " + error.message());
}

void OpticalDrive::slotEjectRequested()
{
	m_ejectInProgress = true;
	Q_EMIT ejectRequested(m_device->udi());
}

void OpticalDrive::slotEjectDone(int error, const QString& errorString)
{
	m_ejectInProgress = false;
	Q_EMIT ejectDone(static_cast<Solid::ErrorType>(error), errorString, m_device->udi());
}

void OpticalDrive::initReadWriteSpeeds() const
{
#if 0
    int read_speed, write_speed;
    char *write_speeds = 0;
    QByteArray device_file = QFile::encodeName(m_device->property("Device").toString());

    //qDebug("Doing open (\"%s\", O_RDONLY | O_NONBLOCK)", device_file.constData());
    int fd = open(device_file, O_RDONLY | O_NONBLOCK);
    if (fd < 0) {
        qWarning("Cannot open %s: %s", device_file.constData(), strerror (errno));
        return;
    }

    if (get_read_write_speed(fd, &read_speed, &write_speed, &write_speeds) >= 0) {
        m_readSpeed = read_speed;
        m_writeSpeed = write_speed;

        QStringList list = QString::fromLatin1(write_speeds).split(',', QString::SkipEmptyParts);
        Q_FOREACH (const QString & speed, list)
            m_writeSpeeds.append(speed.toInt());

        free(write_speeds);

        m_speedsInit = true;
    }

    close(fd);
#endif
}

QList<int> OpticalDrive::writeSpeeds() const
{
	if (!m_speedsInit)
		initReadWriteSpeeds();
	//qDebug() << "solid write speeds:" << m_writeSpeeds;
	return m_writeSpeeds;
}

int OpticalDrive::writeSpeed() const
{
	if (!m_speedsInit)
		initReadWriteSpeeds();
	return m_writeSpeed;
}

int OpticalDrive::readSpeed() const
{
	if (!m_speedsInit)
		initReadWriteSpeeds();
	return m_readSpeed;
}

Solid::OpticalDrive::MediumTypes OpticalDrive::supportedMedia() const
{
	const QStringList mediaTypes = m_device->prop("MediaCompatibility").toStringList();
	Solid::OpticalDrive::MediumTypes supported;

	QMap<Solid::OpticalDrive::MediumType, QString> map;
	map[Solid::OpticalDrive::Cdr] = "optical_cd_r";
	map[Solid::OpticalDrive::Cdrw] = "optical_cd_rw";
	map[Solid::OpticalDrive::Dvd] = "optical_dvd";
	map[Solid::OpticalDrive::Dvdr] = "optical_dvd_r";
	map[Solid::OpticalDrive::Dvdrw] = "optical_dvd_rw";
	map[Solid::OpticalDrive::Dvdram] = "optical_dvd_ram";
	map[Solid::OpticalDrive::Dvdplusr] = "optical_dvd_plus_r";
	map[Solid::OpticalDrive::Dvdplusrw] = "optical_dvd_plus_rw";
	map[Solid::OpticalDrive::Dvdplusdl] = "optical_dvd_plus_r_dl";
	map[Solid::OpticalDrive::Dvdplusdlrw] = "optical_dvd_plus_rw_dl";
	map[Solid::OpticalDrive::Bd] = "optical_bd";
	map[Solid::OpticalDrive::Bdr] = "optical_bd_r";
	map[Solid::OpticalDrive::Bdre] = "optical_bd_re";
	map[Solid::OpticalDrive::HdDvd] = "optical_hddvd";
	map[Solid::OpticalDrive::HdDvdr] = "optical_hddvd_r";
	map[Solid::OpticalDrive::HdDvdrw] = "optical_hddvd_rw";
	// TODO add these to Solid
	//map[Solid::OpticalDrive::Mo] ="optical_mo";
	//map[Solid::OpticalDrive::Mr] ="optical_mrw";
	//map[Solid::OpticalDrive::Mrw] ="optical_mrw_w";

	Q_FOREACH (const Solid::OpticalDrive::MediumType& type, map.keys()) {
		if (mediaTypes.contains(map[type])) {
			supported |= type;
		}
	}

	return supported;
}

void OpticalDrive::slotChanged()
{
	m_speedsInit = false;// reset the read/write speeds, changes eg. with an inserted media
}