2010-06-25 21:04:10 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-06-25 21:04:10 +02:00
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-06-26 00:36:21 +02:00
|
|
|
#include "devicekitlister.h"
|
2020-09-18 16:15:19 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2010-07-30 00:16:12 +02:00
|
|
|
#include "core/utilities.h"
|
2010-06-25 21:04:10 +02:00
|
|
|
#include "dbus/udisks.h"
|
|
|
|
#include "dbus/udisksdevice.h"
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "filesystemdevice.h"
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2010-07-04 22:52:45 +02:00
|
|
|
#ifdef HAVE_LIBGPOD
|
2014-02-07 16:34:20 +01:00
|
|
|
#include "gpoddevice.h"
|
2010-07-04 22:52:45 +02:00
|
|
|
#endif
|
|
|
|
|
2010-07-29 21:06:27 +02:00
|
|
|
#include <sys/statvfs.h>
|
2010-07-29 20:14:26 +02:00
|
|
|
|
2010-06-26 00:01:47 +02:00
|
|
|
#include <QtDebug>
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
DeviceKitLister::DeviceKitLister() {}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
DeviceKitLister::~DeviceKitLister() { qLog(Debug) << __PRETTY_FUNCTION__; }
|
2010-06-26 00:01:47 +02:00
|
|
|
|
2010-06-26 00:36:21 +02:00
|
|
|
QString DeviceKitLister::DeviceData::unique_id() const {
|
2014-02-07 16:34:20 +01:00
|
|
|
return QString("DeviceKit/%1/%2/%3/%4")
|
|
|
|
.arg(drive_serial, drive_vendor, drive_model)
|
|
|
|
.arg(device_size);
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
|
|
|
|
2010-06-26 00:36:21 +02:00
|
|
|
void DeviceKitLister::Init() {
|
2010-06-25 21:04:10 +02:00
|
|
|
interface_.reset(new OrgFreedesktopUDisksInterface(
|
|
|
|
OrgFreedesktopUDisksInterface::staticInterfaceName(),
|
|
|
|
"/org/freedesktop/UDisks", QDBusConnection::systemBus()));
|
|
|
|
|
2010-06-26 00:01:47 +02:00
|
|
|
// Get all the devices currently attached
|
2020-09-18 16:15:19 +02:00
|
|
|
QDBusPendingReply<QList<QDBusObjectPath>> reply =
|
2014-02-07 16:34:20 +01:00
|
|
|
interface_->EnumerateDevices();
|
2010-06-26 00:01:47 +02:00
|
|
|
reply.waitForFinished();
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2010-06-26 00:01:47 +02:00
|
|
|
if (!reply.isValid()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Warning) << "Error enumerating DeviceKit-disks devices:"
|
|
|
|
<< reply.error().name() << reply.error().message();
|
2010-12-25 15:25:05 +01:00
|
|
|
interface_.reset();
|
2010-06-26 00:01:47 +02:00
|
|
|
return;
|
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2010-12-25 15:25:05 +01:00
|
|
|
// Listen for changes
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(interface_.get(), SIGNAL(DeviceAdded(QDBusObjectPath)),
|
|
|
|
SLOT(DBusDeviceAdded(QDBusObjectPath)));
|
|
|
|
connect(interface_.get(), SIGNAL(DeviceRemoved(QDBusObjectPath)),
|
|
|
|
SLOT(DBusDeviceRemoved(QDBusObjectPath)));
|
|
|
|
connect(interface_.get(), SIGNAL(DeviceChanged(QDBusObjectPath)),
|
|
|
|
SLOT(DBusDeviceChanged(QDBusObjectPath)));
|
2010-12-25 15:25:05 +01:00
|
|
|
|
2010-06-26 00:01:47 +02:00
|
|
|
// Get information about each one
|
|
|
|
QMap<QString, DeviceData> device_data;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QDBusObjectPath& path : reply.value()) {
|
2010-06-26 00:01:47 +02:00
|
|
|
DeviceData data = ReadDeviceData(path);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (data.suitable) device_data[data.unique_id()] = data;
|
2010-06-26 00:01:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the internal cache
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
device_data_ = device_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify about the changes
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& id : device_data.keys()) {
|
|
|
|
emit DeviceAdded(id);
|
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
|
|
|
|
2010-06-26 00:36:21 +02:00
|
|
|
QStringList DeviceKitLister::DeviceUniqueIDs() {
|
2010-06-26 00:01:47 +02:00
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
return device_data_.keys();
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QVariantList DeviceKitLister::DeviceIcons(const QString& id) {
|
2010-07-25 02:20:18 +02:00
|
|
|
QString path = LockAndGetDeviceInfo(id, &DeviceData::device_mount_paths)[0];
|
2014-02-07 16:34:20 +01:00
|
|
|
return QVariantList() << GuessIconForPath(path)
|
|
|
|
<< GuessIconForModel(DeviceManufacturer(id),
|
|
|
|
DeviceModel(id))
|
|
|
|
<< LockAndGetDeviceInfo(
|
|
|
|
id, &DeviceData::device_presentation_icon_name);
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString DeviceKitLister::DeviceManufacturer(const QString& id) {
|
2010-07-04 17:01:24 +02:00
|
|
|
return LockAndGetDeviceInfo(id, &DeviceData::drive_vendor);
|
|
|
|
}
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString DeviceKitLister::DeviceModel(const QString& id) {
|
2010-07-04 17:01:24 +02:00
|
|
|
return LockAndGetDeviceInfo(id, &DeviceData::drive_model);
|
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
quint64 DeviceKitLister::DeviceCapacity(const QString& id) {
|
2010-07-04 17:01:24 +02:00
|
|
|
return LockAndGetDeviceInfo(id, &DeviceData::device_size);
|
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
quint64 DeviceKitLister::DeviceFreeSpace(const QString& id) {
|
2010-07-29 20:14:26 +02:00
|
|
|
return LockAndGetDeviceInfo(id, &DeviceData::free_space);
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QVariantMap DeviceKitLister::DeviceHardwareInfo(const QString& id) {
|
2010-07-04 17:01:24 +02:00
|
|
|
QVariantMap ret;
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!device_data_.contains(id)) return ret;
|
2010-07-04 17:01:24 +02:00
|
|
|
const DeviceData& data = device_data_[id];
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2017-08-28 23:37:25 +02:00
|
|
|
ret[QT_TR_NOOP("D-Bus path")] = data.dbus_path;
|
2010-07-04 17:01:24 +02:00
|
|
|
ret[QT_TR_NOOP("Serial number")] = data.drive_serial;
|
2010-07-04 17:57:09 +02:00
|
|
|
ret[QT_TR_NOOP("Mount points")] = data.device_mount_paths.join(", ");
|
2010-07-04 17:01:24 +02:00
|
|
|
ret[QT_TR_NOOP("Device")] = data.device_file;
|
|
|
|
return ret;
|
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString DeviceKitLister::MakeFriendlyName(const QString& id) {
|
2010-07-04 17:01:24 +02:00
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!device_data_.contains(id)) return QString();
|
2010-07-04 17:01:24 +02:00
|
|
|
const DeviceData& data = device_data_[id];
|
|
|
|
|
|
|
|
if (!data.device_presentation_name.isEmpty())
|
|
|
|
return data.device_presentation_name;
|
|
|
|
if (!data.drive_model.isEmpty() && !data.drive_vendor.isEmpty())
|
|
|
|
return data.drive_vendor + " " + data.drive_model;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!data.drive_model.isEmpty()) return data.drive_model;
|
2010-07-04 17:01:24 +02:00
|
|
|
return data.drive_serial;
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
|
|
|
|
2010-06-26 00:36:21 +02:00
|
|
|
DeviceKitLister::DeviceData DeviceKitLister::ReadDeviceData(
|
2014-02-07 16:34:20 +01:00
|
|
|
const QDBusObjectPath& path) const {
|
2010-06-26 00:01:47 +02:00
|
|
|
DeviceData ret;
|
2010-06-25 21:04:10 +02:00
|
|
|
|
|
|
|
OrgFreedesktopUDisksDeviceInterface device(
|
2014-02-07 16:34:20 +01:00
|
|
|
OrgFreedesktopUDisksInterface::staticInterfaceName(), path.path(),
|
|
|
|
QDBusConnection::systemBus());
|
2010-06-25 21:04:10 +02:00
|
|
|
if (!device.isValid()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
qLog(Warning) << "Error connecting to the device interface on"
|
|
|
|
<< path.path();
|
2010-06-25 21:04:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-06-04 21:43:55 +02:00
|
|
|
// Don't do anything with internal drives, hidden drives, or partition tables
|
2014-02-07 16:34:20 +01:00
|
|
|
if (device.deviceIsSystemInternal() || device.devicePresentationHide() ||
|
|
|
|
device.deviceMountPaths().isEmpty() || device.deviceIsPartitionTable()) {
|
2010-06-25 21:04:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret.suitable = true;
|
|
|
|
ret.dbus_path = path.path();
|
|
|
|
ret.drive_serial = device.driveSerial();
|
|
|
|
ret.drive_model = device.driveModel();
|
|
|
|
ret.drive_vendor = device.driveVendor();
|
2010-07-04 17:01:24 +02:00
|
|
|
ret.device_file = device.deviceFile();
|
2010-06-25 21:04:10 +02:00
|
|
|
ret.device_presentation_name = device.devicePresentationName();
|
|
|
|
ret.device_presentation_icon_name = device.devicePresentationIconName();
|
|
|
|
ret.device_size = device.deviceSize();
|
2010-06-26 00:01:47 +02:00
|
|
|
ret.device_mount_paths = device.deviceMountPaths();
|
2010-07-29 20:14:26 +02:00
|
|
|
|
|
|
|
// Get free space info
|
2010-07-30 00:16:12 +02:00
|
|
|
if (!ret.device_mount_paths.isEmpty())
|
|
|
|
ret.free_space = Utilities::FileSystemFreeSpace(ret.device_mount_paths[0]);
|
2010-07-29 20:14:26 +02:00
|
|
|
|
2010-06-25 21:04:10 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceKitLister::DBusDeviceAdded(const QDBusObjectPath& path) {
|
2010-06-26 00:01:47 +02:00
|
|
|
DeviceData data = ReadDeviceData(path);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!data.suitable) return;
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2010-06-26 00:01:47 +02:00
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
device_data_[data.unique_id()] = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
emit DeviceAdded(data.unique_id());
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceKitLister::DBusDeviceRemoved(const QDBusObjectPath& path) {
|
2010-06-26 00:01:47 +02:00
|
|
|
QString id;
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
id = FindUniqueIdByPath(path);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (id.isNull()) return;
|
2010-06-26 00:01:47 +02:00
|
|
|
|
|
|
|
device_data_.remove(id);
|
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
|
2010-06-26 00:01:47 +02:00
|
|
|
emit DeviceRemoved(id);
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceKitLister::DBusDeviceChanged(const QDBusObjectPath& path) {
|
2010-06-26 00:01:47 +02:00
|
|
|
bool already_known = false;
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
already_known = !FindUniqueIdByPath(path).isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceData data = ReadDeviceData(path);
|
|
|
|
|
|
|
|
if (already_known && !data.suitable)
|
2010-06-26 01:38:21 +02:00
|
|
|
DBusDeviceRemoved(path);
|
2010-06-26 00:01:47 +02:00
|
|
|
else if (!already_known && data.suitable)
|
2010-06-26 01:38:21 +02:00
|
|
|
DBusDeviceAdded(path);
|
2010-06-26 00:01:47 +02:00
|
|
|
else if (already_known && data.suitable) {
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
|
|
|
device_data_[data.unique_id()] = data;
|
|
|
|
}
|
|
|
|
emit DeviceChanged(data.unique_id());
|
|
|
|
}
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QString DeviceKitLister::FindUniqueIdByPath(const QDBusObjectPath& path) const {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const DeviceData& data : device_data_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (data.dbus_path == path.path()) return data.unique_id();
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
2010-06-26 00:01:47 +02:00
|
|
|
return QString();
|
2010-06-25 21:04:10 +02:00
|
|
|
}
|
2010-06-26 01:38:21 +02:00
|
|
|
|
2010-08-01 13:55:01 +02:00
|
|
|
QList<QUrl> DeviceKitLister::MakeDeviceUrls(const QString& id) {
|
2014-02-07 16:34:20 +01:00
|
|
|
QString mount_point =
|
|
|
|
LockAndGetDeviceInfo(id, &DeviceData::device_mount_paths)[0];
|
2010-07-04 22:52:45 +02:00
|
|
|
|
2010-08-01 13:55:01 +02:00
|
|
|
return QList<QUrl>() << MakeUrlFromLocalPath(mount_point);
|
2010-06-26 01:38:21 +02:00
|
|
|
}
|
2010-07-25 03:07:51 +02:00
|
|
|
|
|
|
|
void DeviceKitLister::UnmountDevice(const QString& id) {
|
|
|
|
QString path = LockAndGetDeviceInfo(id, &DeviceData::dbus_path);
|
|
|
|
|
|
|
|
OrgFreedesktopUDisksDeviceInterface device(
|
2014-02-07 16:34:20 +01:00
|
|
|
OrgFreedesktopUDisksInterface::staticInterfaceName(), path,
|
|
|
|
QDBusConnection::systemBus());
|
2010-07-25 03:07:51 +02:00
|
|
|
if (!device.isValid()) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Error connecting to the device interface on" << path;
|
2010-07-25 03:07:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the device's parent drive
|
|
|
|
QString drive_path = device.partitionSlave().path();
|
|
|
|
OrgFreedesktopUDisksDeviceInterface drive(
|
2014-02-07 16:34:20 +01:00
|
|
|
OrgFreedesktopUDisksInterface::staticInterfaceName(), drive_path,
|
|
|
|
QDBusConnection::systemBus());
|
2010-07-25 03:07:51 +02:00
|
|
|
if (!drive.isValid()) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Error connecting to the drive interface on" << drive_path;
|
2010-07-25 03:07:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmount the filesystem
|
|
|
|
QDBusPendingReply<> reply = device.FilesystemUnmount(QStringList());
|
|
|
|
reply.waitForFinished();
|
|
|
|
|
|
|
|
// Eject the drive
|
|
|
|
drive.DriveEject(QStringList());
|
|
|
|
// Don't bother waiting for the eject to finish
|
|
|
|
}
|
2010-08-11 20:47:53 +02:00
|
|
|
|
|
|
|
void DeviceKitLister::UpdateDeviceFreeSpace(const QString& id) {
|
|
|
|
{
|
|
|
|
QMutexLocker l(&mutex_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!device_data_.contains(id)) return;
|
2010-08-11 20:47:53 +02:00
|
|
|
|
|
|
|
DeviceData& data = device_data_[id];
|
|
|
|
if (!data.device_mount_paths.isEmpty())
|
2014-02-07 16:34:20 +01:00
|
|
|
data.free_space =
|
|
|
|
Utilities::FileSystemFreeSpace(data.device_mount_paths[0]);
|
2010-08-11 20:47:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
emit DeviceChanged(id);
|
|
|
|
}
|