2010-06-26 00:01:47 +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-26 00:01:47 +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/>.
|
|
|
|
*/
|
|
|
|
|
2012-11-21 17:21:20 +01:00
|
|
|
#include "devicemanager.h"
|
|
|
|
|
|
|
|
#include <QApplication>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QSortFilterProxyModel>
|
|
|
|
#include <QUrl>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <memory>
|
2012-11-21 17:21:20 +01:00
|
|
|
|
2010-07-04 22:52:45 +02:00
|
|
|
#include "config.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2012-06-29 16:56:17 +02:00
|
|
|
#include "core/concurrentrun.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/database.h"
|
2011-04-22 18:50:29 +02:00
|
|
|
#include "core/logging.h"
|
2010-07-19 23:16:22 +02:00
|
|
|
#include "core/musicstorage.h"
|
2019-01-21 10:06:48 +01:00
|
|
|
#include "core/simpletreemodel.h"
|
2010-07-04 17:56:08 +02:00
|
|
|
#include "core/taskmanager.h"
|
2010-07-03 23:05:55 +02:00
|
|
|
#include "core/utilities.h"
|
2016-12-15 12:36:05 +01:00
|
|
|
#include "devicedatabasebackend.h"
|
2019-01-21 10:06:48 +01:00
|
|
|
#include "deviceinfo.h"
|
2016-12-15 12:36:05 +01:00
|
|
|
#include "devicestatefiltermodel.h"
|
|
|
|
#include "filesystemdevice.h"
|
2010-07-03 23:05:55 +02:00
|
|
|
#include "ui/iconloader.h"
|
2010-06-26 00:01:47 +02:00
|
|
|
|
2011-08-10 17:23:32 +02:00
|
|
|
#ifdef HAVE_AUDIOCD
|
2014-02-07 16:34:20 +01:00
|
|
|
#include "cddadevice.h"
|
2016-12-15 12:36:05 +01:00
|
|
|
#include "cddalister.h"
|
2011-08-10 17:23:32 +02:00
|
|
|
#endif
|
2011-08-05 02:15:16 +02:00
|
|
|
|
2016-12-15 12:36:05 +01:00
|
|
|
#if defined(Q_OS_DARWIN) and defined(HAVE_LIBMTP)
|
2014-02-07 16:34:20 +01:00
|
|
|
#include "macdevicelister.h"
|
2010-07-24 19:53:40 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_LIBGPOD
|
2014-02-07 16:34:20 +01:00
|
|
|
#include "gpoddevice.h"
|
2010-07-24 19:53:40 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_GIO
|
2014-02-07 16:34:20 +01:00
|
|
|
#include "giolister.h"
|
2010-07-23 15:46:30 +02:00
|
|
|
#endif
|
2010-08-14 17:57:05 +02:00
|
|
|
#ifdef HAVE_LIBMTP
|
2014-02-07 16:34:20 +01:00
|
|
|
#include "mtpdevice.h"
|
2010-08-14 17:57:05 +02:00
|
|
|
#endif
|
2016-05-17 21:56:16 +02:00
|
|
|
#ifdef HAVE_UDISKS2
|
|
|
|
#include "udisks2lister.h"
|
|
|
|
#endif
|
2010-07-23 15:46:30 +02:00
|
|
|
|
2014-02-06 14:48:00 +01:00
|
|
|
using std::bind;
|
2012-06-29 16:56:17 +02:00
|
|
|
|
2010-07-04 13:34:25 +02:00
|
|
|
const int DeviceManager::kDeviceIconSize = 32;
|
|
|
|
const int DeviceManager::kDeviceIconOverlaySize = 16;
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
DeviceManager::DeviceManager(Application* app, QObject* parent)
|
2019-01-21 10:06:48 +01:00
|
|
|
: SimpleTreeModel<DeviceInfo>(new DeviceInfo(this), parent),
|
2014-02-07 16:34:20 +01:00
|
|
|
app_(app),
|
2016-12-15 12:36:05 +01:00
|
|
|
not_connected_overlay_(
|
|
|
|
IconLoader::Load("edit-delete", IconLoader::Base)) {
|
2012-06-29 16:58:30 +02:00
|
|
|
thread_pool_.setMaxThreadCount(1);
|
2012-02-12 14:41:50 +01:00
|
|
|
connect(app_->task_manager(), SIGNAL(TasksChanged()), SLOT(TasksChanged()));
|
2010-07-04 17:56:08 +02:00
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
// Create the backend in the database thread
|
2010-08-27 20:36:57 +02:00
|
|
|
backend_ = new DeviceDatabaseBackend;
|
2012-02-12 14:41:50 +01:00
|
|
|
backend_->moveToThread(app_->database()->thread());
|
|
|
|
backend_->Init(app_->database());
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2019-02-24 07:12:07 +01:00
|
|
|
connect(this, SIGNAL(DeviceCreatedFromDb(DeviceInfo*)),
|
|
|
|
SLOT(AddDeviceFromDb(DeviceInfo*)));
|
2012-06-29 16:56:17 +02:00
|
|
|
// This reads from the database and contends on the database mutex, which can
|
|
|
|
// be very slow on startup.
|
2014-02-07 16:34:20 +01:00
|
|
|
ConcurrentRun::Run<void>(&thread_pool_,
|
|
|
|
bind(&DeviceManager::LoadAllDevices, this));
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2010-07-19 21:56:29 +02:00
|
|
|
// This proxy model only shows connected devices
|
|
|
|
connected_devices_model_ = new DeviceStateFilterModel(this);
|
|
|
|
connected_devices_model_->setSourceModel(this);
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
// CD devices are detected via the DiskArbitration framework instead on Darwin.
|
2011-08-11 21:46:48 +02:00
|
|
|
#if defined(HAVE_AUDIOCD) && !defined(Q_OS_DARWIN)
|
2011-08-05 02:15:16 +02:00
|
|
|
AddLister(new CddaLister);
|
2011-08-11 21:37:09 +02:00
|
|
|
#endif
|
2016-05-17 21:56:16 +02:00
|
|
|
#ifdef HAVE_UDISKS2
|
|
|
|
AddLister(new Udisks2Lister);
|
|
|
|
#endif
|
2010-07-17 16:22:07 +02:00
|
|
|
#ifdef HAVE_GIO
|
|
|
|
AddLister(new GioLister);
|
|
|
|
#endif
|
2016-12-15 12:36:05 +01:00
|
|
|
#if defined(Q_OS_DARWIN) and defined(HAVE_LIBMTP)
|
2010-07-23 15:46:30 +02:00
|
|
|
AddLister(new MacDeviceLister);
|
|
|
|
#endif
|
2010-07-17 16:22:07 +02:00
|
|
|
|
|
|
|
AddDeviceClass<FilesystemDevice>();
|
|
|
|
|
2011-08-10 17:23:32 +02:00
|
|
|
#ifdef HAVE_AUDIOCD
|
|
|
|
AddDeviceClass<CddaDevice>();
|
|
|
|
#endif
|
|
|
|
|
2010-07-17 16:22:07 +02:00
|
|
|
#ifdef HAVE_LIBGPOD
|
|
|
|
AddDeviceClass<GPodDevice>();
|
|
|
|
#endif
|
2010-08-14 17:57:05 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_LIBMTP
|
|
|
|
AddDeviceClass<MtpDevice>();
|
|
|
|
#endif
|
2010-06-26 14:41:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DeviceManager::~DeviceManager() {
|
2014-02-10 14:29:07 +01:00
|
|
|
for (DeviceLister* lister : listers_) {
|
2010-08-23 12:26:00 +02:00
|
|
|
lister->ShutDown();
|
2010-08-22 17:42:21 +02:00
|
|
|
delete lister;
|
|
|
|
}
|
|
|
|
|
2010-07-04 01:00:07 +02:00
|
|
|
backend_->deleteLater();
|
2019-01-21 10:06:48 +01:00
|
|
|
delete root_;
|
2010-06-26 14:41:18 +02:00
|
|
|
}
|
|
|
|
|
2020-06-19 00:14:08 +02:00
|
|
|
// This runs concurrently.
|
2012-06-29 16:56:17 +02:00
|
|
|
void DeviceManager::LoadAllDevices() {
|
|
|
|
Q_ASSERT(QThread::currentThread() != qApp->thread());
|
|
|
|
DeviceDatabaseBackend::DeviceList devices = backend_->GetAllDevices();
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const DeviceDatabaseBackend::Device& device : devices) {
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = new DeviceInfo(DeviceInfo::Type_Device, root_);
|
2019-01-21 10:06:48 +01:00
|
|
|
info->InitFromDb(device);
|
2019-02-24 07:12:07 +01:00
|
|
|
// Use of QPixMap and device insertion should only be done on the main
|
|
|
|
// thread. Send a signal to finish the device addition.
|
|
|
|
emit DeviceCreatedFromDb(info);
|
|
|
|
}
|
|
|
|
}
|
2016-05-17 21:56:16 +02:00
|
|
|
|
2019-02-24 07:12:07 +01:00
|
|
|
void DeviceManager::AddDeviceFromDb(DeviceInfo* info) {
|
|
|
|
// At this point, icon_name_ contains the value from the database where the
|
|
|
|
// value is allowed to be a comma delimited list.
|
|
|
|
QStringList icon_names = info->icon_name_.split(',');
|
|
|
|
QVariantList icons;
|
|
|
|
for (const QString& icon_name : icon_names) {
|
|
|
|
icons << icon_name;
|
2012-06-29 16:56:17 +02:00
|
|
|
}
|
2019-02-24 07:12:07 +01:00
|
|
|
info->LoadIcon(icons, info->friendly_name_);
|
|
|
|
|
2019-02-24 08:42:31 +01:00
|
|
|
DeviceInfo* existing = FindEquivalentDevice(info);
|
|
|
|
if (existing) {
|
|
|
|
qLog(Info) << "Found existing device: " << info->friendly_name_;
|
|
|
|
// Update user configuration from the database.
|
|
|
|
existing->icon_name_ = info->icon_name_;
|
|
|
|
existing->icon_ = info->icon_;
|
|
|
|
QModelIndex idx = ItemToIndex(existing);
|
|
|
|
if (idx.isValid()) emit dataChanged(idx, idx);
|
2020-06-19 00:14:08 +02:00
|
|
|
|
|
|
|
// Discard the info loaded from the database. This doesn't use
|
|
|
|
// begin/endRemoveRows, but beginInsertRows has not yet been called to
|
|
|
|
// notify listeners of the existence.
|
|
|
|
root_->Delete(info->row);
|
2019-02-24 08:42:31 +01:00
|
|
|
} else {
|
|
|
|
qLog(Info) << "Device added from database: " << info->friendly_name_;
|
|
|
|
beginInsertRows(ItemToIndex(root_), devices_.count(), devices_.count());
|
|
|
|
devices_ << info;
|
|
|
|
endInsertRows();
|
|
|
|
}
|
2012-06-29 16:56:17 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
QVariant DeviceManager::data(const QModelIndex& idx, int role) const {
|
|
|
|
if (!idx.isValid() || idx.column() != 0) return QVariant();
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info) return QVariant();
|
2010-07-03 23:05:55 +02:00
|
|
|
|
|
|
|
switch (role) {
|
2010-07-04 13:34:25 +02:00
|
|
|
case Qt::DisplayRole: {
|
2010-07-17 19:18:02 +02:00
|
|
|
QString text;
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->friendly_name_.isEmpty())
|
|
|
|
text = info->friendly_name_;
|
|
|
|
else if (info->BestBackend())
|
|
|
|
text = info->BestBackend()->unique_id_;
|
|
|
|
|
|
|
|
if (info->size_)
|
|
|
|
text = text + QString(" (%1)").arg(Utilities::PrettySize(info->size_));
|
2010-07-04 13:34:25 +02:00
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::DecorationRole: {
|
2019-01-21 10:06:48 +01:00
|
|
|
QPixmap pixmap = info->icon_.pixmap(kDeviceIconSize);
|
2010-07-04 13:34:25 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->backends_.isEmpty() || !info->BestBackend()->lister_) {
|
2010-07-04 13:34:25 +02:00
|
|
|
// Disconnected but remembered
|
|
|
|
QPainter p(&pixmap);
|
|
|
|
p.drawPixmap(kDeviceIconSize - kDeviceIconOverlaySize,
|
|
|
|
kDeviceIconSize - kDeviceIconOverlaySize,
|
|
|
|
not_connected_overlay_.pixmap(kDeviceIconOverlaySize));
|
|
|
|
}
|
|
|
|
|
|
|
|
return pixmap;
|
|
|
|
}
|
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
case Role_FriendlyName:
|
2019-01-21 10:06:48 +01:00
|
|
|
return info->friendly_name_;
|
2010-07-04 13:34:25 +02:00
|
|
|
|
|
|
|
case Role_UniqueId:
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->BestBackend()) return QString();
|
|
|
|
return info->BestBackend()->unique_id_;
|
2010-07-04 13:34:25 +02:00
|
|
|
|
2010-07-04 17:01:24 +02:00
|
|
|
case Role_IconName:
|
2019-01-21 10:06:48 +01:00
|
|
|
return info->icon_name_;
|
2010-07-04 17:01:24 +02:00
|
|
|
|
|
|
|
case Role_Capacity:
|
2010-07-30 00:16:12 +02:00
|
|
|
case MusicStorage::Role_Capacity:
|
2019-01-21 10:06:48 +01:00
|
|
|
return info->size_;
|
2010-07-04 17:01:24 +02:00
|
|
|
|
2010-07-18 00:06:19 +02:00
|
|
|
case Role_FreeSpace:
|
2010-07-30 00:16:12 +02:00
|
|
|
case MusicStorage::Role_FreeSpace:
|
2019-01-21 10:06:48 +01:00
|
|
|
return info->BestBackend() && info->BestBackend()->lister_
|
|
|
|
? info->BestBackend()->lister_->DeviceFreeSpace(
|
|
|
|
info->BestBackend()->unique_id_)
|
2014-02-07 16:34:20 +01:00
|
|
|
: QVariant();
|
2010-07-18 00:06:19 +02:00
|
|
|
|
2010-07-04 13:34:25 +02:00
|
|
|
case Role_State:
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->device_) return State_Connected;
|
|
|
|
if (info->BestBackend() && info->BestBackend()->lister_) {
|
|
|
|
if (info->BestBackend()->lister_->DeviceNeedsMount(
|
|
|
|
info->BestBackend()->unique_id_))
|
2010-09-11 14:29:44 +02:00
|
|
|
return State_NotMounted;
|
2010-07-04 13:34:25 +02:00
|
|
|
return State_NotConnected;
|
2010-09-11 14:29:44 +02:00
|
|
|
}
|
2010-07-04 13:34:25 +02:00
|
|
|
return State_Remembered;
|
|
|
|
|
2010-07-04 17:56:08 +02:00
|
|
|
case Role_UpdatingPercentage:
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->task_percentage_ == -1) return QVariant();
|
|
|
|
return info->task_percentage_;
|
2010-07-04 17:56:08 +02:00
|
|
|
|
2010-07-30 00:16:12 +02:00
|
|
|
case MusicStorage::Role_Storage:
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->device_ && info->database_id_ != -1)
|
2019-01-21 22:38:46 +01:00
|
|
|
const_cast<DeviceManager*>(this)->Connect(info);
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->device_) return QVariant();
|
|
|
|
return QVariant::fromValue<std::shared_ptr<MusicStorage>>(info->device_);
|
2010-08-14 14:30:51 +02:00
|
|
|
|
|
|
|
case MusicStorage::Role_StorageForceConnect:
|
2019-01-21 22:38:46 +01:00
|
|
|
if (!info->BestBackend()) return QVariant();
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->device_) {
|
2019-01-21 22:38:46 +01:00
|
|
|
if (info->database_id_ == -1 &&
|
2019-01-21 10:06:48 +01:00
|
|
|
!info->BestBackend()->lister_->DeviceNeedsMount(
|
|
|
|
info->BestBackend()->unique_id_)) {
|
|
|
|
if (info->BestBackend()->lister_->AskForScan(
|
|
|
|
info->BestBackend()->unique_id_)) {
|
2014-02-06 14:48:00 +01:00
|
|
|
std::unique_ptr<QMessageBox> dialog(new QMessageBox(
|
2011-08-05 02:15:16 +02:00
|
|
|
QMessageBox::Information, tr("Connect device"),
|
2014-02-07 16:34:20 +01:00
|
|
|
tr("This is the first time you have connected this device. "
|
|
|
|
"Clementine will now scan the device to find music files - "
|
|
|
|
"this may take some time."),
|
2011-08-05 02:15:16 +02:00
|
|
|
QMessageBox::Cancel));
|
2014-02-07 16:34:20 +01:00
|
|
|
QPushButton* connect = dialog->addButton(tr("Connect device"),
|
|
|
|
QMessageBox::AcceptRole);
|
2011-08-05 02:15:16 +02:00
|
|
|
dialog->exec();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (dialog->clickedButton() != connect) return QVariant();
|
2011-08-05 02:15:16 +02:00
|
|
|
}
|
2010-08-14 14:30:51 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
const_cast<DeviceManager*>(this)->Connect(info);
|
2010-08-14 14:30:51 +02:00
|
|
|
}
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->device_) return QVariant();
|
|
|
|
return QVariant::fromValue<std::shared_ptr<MusicStorage>>(info->device_);
|
2010-07-19 23:16:22 +02:00
|
|
|
|
2010-08-28 21:07:23 +02:00
|
|
|
case Role_MountPath: {
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->device_) return QVariant();
|
2010-08-28 21:07:23 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
QString ret = info->device_->url().path();
|
2014-02-07 16:34:20 +01:00
|
|
|
#ifdef Q_OS_WIN32
|
|
|
|
if (ret.startsWith('/')) ret.remove(0, 1);
|
|
|
|
#endif
|
2010-08-28 21:07:23 +02:00
|
|
|
return QDir::toNativeSeparators(ret);
|
|
|
|
}
|
2010-07-25 01:20:34 +02:00
|
|
|
|
2010-08-29 17:32:36 +02:00
|
|
|
case Role_TranscodeMode:
|
2019-01-21 10:06:48 +01:00
|
|
|
return info->transcode_mode_;
|
2010-08-29 17:32:36 +02:00
|
|
|
|
|
|
|
case Role_TranscodeFormat:
|
2019-01-21 10:06:48 +01:00
|
|
|
return info->transcode_format_;
|
2010-08-29 17:32:36 +02:00
|
|
|
|
2010-08-30 16:45:27 +02:00
|
|
|
case Role_SongCount:
|
2019-01-21 10:06:48 +01:00
|
|
|
if (!info->device_) return QVariant();
|
|
|
|
return info->device_->song_count();
|
2010-08-30 16:45:27 +02:00
|
|
|
|
2010-07-04 13:34:25 +02:00
|
|
|
default:
|
|
|
|
return QVariant();
|
2010-07-03 23:05:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceManager::AddLister(DeviceLister* lister) {
|
2010-06-26 00:36:21 +02:00
|
|
|
listers_ << lister;
|
2014-02-07 16:34:20 +01:00
|
|
|
connect(lister, SIGNAL(DeviceAdded(QString)),
|
|
|
|
SLOT(PhysicalDeviceAdded(QString)));
|
|
|
|
connect(lister, SIGNAL(DeviceRemoved(QString)),
|
|
|
|
SLOT(PhysicalDeviceRemoved(QString)));
|
|
|
|
connect(lister, SIGNAL(DeviceChanged(QString)),
|
|
|
|
SLOT(PhysicalDeviceChanged(QString)));
|
2010-06-26 00:01:47 +02:00
|
|
|
|
2010-06-26 00:36:21 +02:00
|
|
|
lister->Start();
|
2010-06-26 00:01:47 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* DeviceManager::FindDeviceById(const QString& id) const {
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < devices_.count(); ++i) {
|
2019-01-21 10:06:48 +01:00
|
|
|
for (const DeviceInfo::Backend& backend : devices_[i]->backends_) {
|
2019-01-21 22:38:46 +01:00
|
|
|
if (backend.unique_id_ == id) return devices_[i];
|
2010-07-17 19:18:02 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-21 22:38:46 +01:00
|
|
|
return nullptr;
|
2010-07-17 19:18:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* DeviceManager::FindDeviceByUrl(const QList<QUrl>& urls) const {
|
|
|
|
if (urls.isEmpty()) return nullptr;
|
2014-02-07 16:34:20 +01:00
|
|
|
|
|
|
|
for (int i = 0; i < devices_.count(); ++i) {
|
2019-01-21 10:06:48 +01:00
|
|
|
for (const DeviceInfo::Backend& backend : devices_[i]->backends_) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!backend.lister_) continue;
|
|
|
|
|
|
|
|
QList<QUrl> device_urls =
|
|
|
|
backend.lister_->MakeDeviceUrls(backend.unique_id_);
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QUrl& url : device_urls) {
|
2019-01-21 22:38:46 +01:00
|
|
|
if (urls.contains(url)) return devices_[i];
|
2010-08-01 13:55:01 +02:00
|
|
|
}
|
2010-07-17 19:18:02 +02:00
|
|
|
}
|
2010-07-03 23:05:55 +02:00
|
|
|
}
|
2019-01-21 22:38:46 +01:00
|
|
|
return nullptr;
|
2010-07-03 23:05:55 +02:00
|
|
|
}
|
|
|
|
|
2019-02-24 08:42:31 +01:00
|
|
|
DeviceInfo* DeviceManager::FindEquivalentDevice(DeviceInfo* info) const {
|
|
|
|
for (const DeviceInfo::Backend& backend : info->backends_) {
|
|
|
|
DeviceInfo* match = FindDeviceById(backend.unique_id_);
|
|
|
|
if (match) return match;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-06-02 18:17:27 +02:00
|
|
|
QList<DeviceInfo*> DeviceManager::FindDevicesByUrlSchemes(
|
2021-05-30 14:23:37 +02:00
|
|
|
QStringList url_schemes) const {
|
|
|
|
QList<DeviceInfo*> matches;
|
|
|
|
for (DeviceInfo* device_info : devices_) {
|
|
|
|
for (const DeviceInfo::Backend& backend : device_info->backends_) {
|
|
|
|
if (!backend.lister_) continue;
|
|
|
|
|
|
|
|
QList<QUrl> device_urls =
|
|
|
|
backend.lister_->MakeDeviceUrls(backend.unique_id_);
|
|
|
|
for (const QUrl& url : device_urls) {
|
|
|
|
if (url_schemes.contains(url.scheme())) {
|
|
|
|
matches << device_info;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceManager::PhysicalDeviceAdded(const QString& id) {
|
2010-06-26 14:41:18 +02:00
|
|
|
DeviceLister* lister = qobject_cast<DeviceLister*>(sender());
|
2010-06-26 00:01:47 +02:00
|
|
|
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Info) << "Device added:" << id;
|
2010-06-26 14:41:18 +02:00
|
|
|
|
2010-07-03 23:05:55 +02:00
|
|
|
// Do we have this device already?
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = FindDeviceById(id);
|
|
|
|
if (info) {
|
2019-01-21 10:06:48 +01:00
|
|
|
for (int backend_index = 0; backend_index < info->backends_.count();
|
2014-02-07 16:34:20 +01:00
|
|
|
++backend_index) {
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->backends_[backend_index].unique_id_ == id) {
|
|
|
|
info->backends_[backend_index].lister_ = lister;
|
2010-07-17 19:18:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-01-21 22:38:46 +01:00
|
|
|
QModelIndex idx = ItemToIndex(info);
|
|
|
|
if (idx.isValid()) emit dataChanged(idx, idx);
|
2010-07-17 19:18:02 +02:00
|
|
|
} else {
|
|
|
|
// Check if we have another device with the same URL
|
2019-01-21 22:38:46 +01:00
|
|
|
info = FindDeviceByUrl(lister->MakeDeviceUrls(id));
|
|
|
|
if (info) {
|
2010-07-17 19:18:02 +02:00
|
|
|
// Add this device's lister to the existing device
|
2019-01-21 10:06:48 +01:00
|
|
|
info->backends_ << DeviceInfo::Backend(lister, id);
|
2010-07-17 19:18:02 +02:00
|
|
|
|
|
|
|
// If the user hasn't saved the device in the DB yet then overwrite the
|
|
|
|
// device's name and icon etc.
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->database_id_ == -1 && info->BestBackend()->lister_ == lister) {
|
|
|
|
info->friendly_name_ = lister->MakeFriendlyName(id);
|
|
|
|
info->size_ = lister->DeviceCapacity(id);
|
|
|
|
info->LoadIcon(lister->DeviceIcons(id), info->friendly_name_);
|
2010-07-17 19:18:02 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
QModelIndex idx = ItemToIndex(info);
|
|
|
|
if (idx.isValid()) emit dataChanged(idx, idx);
|
2010-07-17 19:18:02 +02:00
|
|
|
} else {
|
|
|
|
// It's a completely new device
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = new DeviceInfo(DeviceInfo::Type_Device, root_);
|
2019-01-21 10:06:48 +01:00
|
|
|
info->backends_ << DeviceInfo::Backend(lister, id);
|
|
|
|
info->friendly_name_ = lister->MakeFriendlyName(id);
|
|
|
|
info->size_ = lister->DeviceCapacity(id);
|
|
|
|
info->LoadIcon(lister->DeviceIcons(id), info->friendly_name_);
|
2010-07-17 19:18:02 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
beginInsertRows(ItemToIndex(root_), devices_.count(), devices_.count());
|
2010-07-17 19:18:02 +02:00
|
|
|
devices_ << info;
|
|
|
|
endInsertRows();
|
|
|
|
}
|
2010-07-03 23:05:55 +02:00
|
|
|
}
|
2010-06-26 00:01:47 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceManager::PhysicalDeviceRemoved(const QString& id) {
|
2010-07-17 19:18:02 +02:00
|
|
|
DeviceLister* lister = qobject_cast<DeviceLister*>(sender());
|
|
|
|
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Info) << "Device removed:" << id;
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = FindDeviceById(id);
|
|
|
|
if (!info) {
|
2010-07-03 23:05:55 +02:00
|
|
|
// Shouldn't happen
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
QModelIndex idx = ItemToIndex(info);
|
|
|
|
if (!idx.isValid()) return;
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->database_id_ != -1) {
|
2010-07-03 23:05:55 +02:00
|
|
|
// Keep the structure around, but just "disconnect" it
|
2019-01-21 10:06:48 +01:00
|
|
|
for (int backend_index = 0; backend_index < info->backends_.count();
|
2014-02-07 16:34:20 +01:00
|
|
|
++backend_index) {
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->backends_[backend_index].unique_id_ == id) {
|
|
|
|
info->backends_[backend_index].lister_ = nullptr;
|
2010-07-17 19:18:02 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->device_ && info->device_->lister() == lister)
|
|
|
|
info->device_.reset();
|
2010-07-03 23:05:55 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
if (!info->device_) emit DeviceDisconnected(idx);
|
2010-09-18 15:54:23 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
emit dataChanged(idx, idx);
|
2010-07-17 19:18:02 +02:00
|
|
|
} else {
|
|
|
|
// If this was the last lister for the device then remove it from the model
|
2019-01-21 10:06:48 +01:00
|
|
|
for (int backend_index = 0; backend_index < info->backends_.count();
|
2014-02-07 16:34:20 +01:00
|
|
|
++backend_index) {
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->backends_[backend_index].unique_id_ == id) {
|
|
|
|
info->backends_.removeAt(backend_index);
|
2010-07-17 19:18:02 +02:00
|
|
|
break;
|
|
|
|
}
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->backends_.isEmpty()) {
|
2019-01-21 22:38:46 +01:00
|
|
|
beginRemoveRows(ItemToIndex(root_), idx.row(), idx.row());
|
|
|
|
devices_.removeAll(info);
|
|
|
|
root_->Delete(info->row);
|
2010-07-17 19:18:02 +02:00
|
|
|
endRemoveRows();
|
|
|
|
}
|
2010-06-26 14:41:18 +02:00
|
|
|
}
|
2010-06-26 00:01:47 +02:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void DeviceManager::PhysicalDeviceChanged(const QString& id) {
|
2010-06-26 14:41:18 +02:00
|
|
|
DeviceLister* lister = qobject_cast<DeviceLister*>(sender());
|
2010-07-04 13:46:42 +02:00
|
|
|
Q_UNUSED(lister);
|
2010-06-26 00:01:47 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = FindDeviceById(id);
|
|
|
|
if (!info) {
|
2010-07-03 23:05:55 +02:00
|
|
|
// Shouldn't happen
|
|
|
|
return;
|
2010-06-26 00:01:47 +02:00
|
|
|
}
|
2010-07-03 23:05:55 +02:00
|
|
|
|
|
|
|
// TODO
|
2010-06-26 00:01:47 +02:00
|
|
|
}
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
std::shared_ptr<ConnectedDevice> DeviceManager::Connect(QModelIndex idx) {
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info) return std::shared_ptr<ConnectedDevice>();
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
return Connect(info);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<ConnectedDevice> DeviceManager::Connect(DeviceInfo* info) {
|
2014-02-06 14:48:00 +01:00
|
|
|
std::shared_ptr<ConnectedDevice> ret;
|
2010-07-17 16:22:07 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
if (!info) return ret;
|
|
|
|
if (info->device_) // Already connected
|
|
|
|
return info->device_;
|
|
|
|
|
|
|
|
if (!info->BestBackend() ||
|
|
|
|
!info->BestBackend()->lister_) // Not physically connected
|
2010-07-17 16:22:07 +02:00
|
|
|
return ret;
|
2010-07-04 01:00:07 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->BestBackend()->lister_->DeviceNeedsMount(
|
|
|
|
info->BestBackend()->unique_id_)) {
|
2010-09-11 14:29:44 +02:00
|
|
|
// Mount the device
|
2019-01-21 10:06:48 +01:00
|
|
|
info->BestBackend()->lister_->MountDevice(info->BestBackend()->unique_id_);
|
2010-09-11 14:29:44 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
bool first_time = (info->database_id_ == -1);
|
2010-07-04 01:00:07 +02:00
|
|
|
if (first_time) {
|
|
|
|
// We haven't stored this device in the database before
|
2019-01-21 10:06:48 +01:00
|
|
|
info->database_id_ = backend_->AddDevice(info->SaveToDb());
|
2010-07-04 01:00:07 +02:00
|
|
|
}
|
|
|
|
|
2010-08-01 13:55:01 +02:00
|
|
|
// Get the device URLs
|
2019-01-21 10:06:48 +01:00
|
|
|
QList<QUrl> urls = info->BestBackend()->lister_->MakeDeviceUrls(
|
|
|
|
info->BestBackend()->unique_id_);
|
2014-02-07 16:34:20 +01:00
|
|
|
if (urls.isEmpty()) return ret;
|
2010-07-17 16:22:07 +02:00
|
|
|
|
2010-08-01 13:55:01 +02:00
|
|
|
// Take the first URL that we have a handler for
|
|
|
|
QUrl device_url;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QUrl& url : urls) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Info) << "Connecting" << url;
|
2010-08-01 13:55:01 +02:00
|
|
|
|
|
|
|
// Find a device class for this URL's scheme
|
|
|
|
if (device_classes_.contains(url.scheme())) {
|
|
|
|
device_url = url;
|
2010-08-14 17:57:05 +02:00
|
|
|
break;
|
2010-08-01 13:55:01 +02:00
|
|
|
}
|
2010-09-04 14:57:34 +02:00
|
|
|
|
|
|
|
// If we get here it means that this URL scheme wasn't supported. If it
|
|
|
|
// was "ipod" or "mtp" then the user compiled out support and the device
|
|
|
|
// won't work properly.
|
|
|
|
if (url.scheme() == "mtp" || url.scheme() == "gphoto2") {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (QMessageBox::critical(
|
|
|
|
nullptr, tr("This device will not work properly"),
|
|
|
|
tr("This is an MTP device, but you compiled Clementine without "
|
|
|
|
"libmtp support.") +
|
2020-09-18 16:15:19 +02:00
|
|
|
" " +
|
|
|
|
tr("If you continue, this device will work slowly and "
|
|
|
|
"songs copied to it may not work."),
|
2014-02-07 16:34:20 +01:00
|
|
|
QMessageBox::Abort, QMessageBox::Ignore) == QMessageBox::Abort)
|
2010-09-04 14:57:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url.scheme() == "ipod") {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (QMessageBox::critical(
|
|
|
|
nullptr, tr("This device will not work properly"),
|
|
|
|
tr("This is an iPod, but you compiled Clementine without libgpod "
|
|
|
|
"support.") +
|
2020-09-18 16:15:19 +02:00
|
|
|
" " +
|
|
|
|
tr("If you continue, this device will work slowly and "
|
|
|
|
"songs copied to it may not work."),
|
2014-02-07 16:34:20 +01:00
|
|
|
QMessageBox::Abort, QMessageBox::Ignore) == QMessageBox::Abort)
|
2010-09-04 14:57:34 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2010-08-01 13:55:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (device_url.isEmpty()) {
|
|
|
|
// Munge the URL list into a string list
|
|
|
|
QStringList url_strings;
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QUrl& url : urls) {
|
|
|
|
url_strings << url.toString();
|
|
|
|
}
|
2010-07-17 19:18:02 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
app_->AddError(tr("This type of device is not supported: %1")
|
|
|
|
.arg(url_strings.join(", ")));
|
2010-07-17 16:22:07 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-06-05 19:25:22 +02:00
|
|
|
QMetaObject meta_object = device_classes_.value(device_url.scheme());
|
|
|
|
QObject* instance = meta_object.newInstance(
|
|
|
|
Q_ARG(QUrl, device_url),
|
|
|
|
Q_ARG(DeviceLister*, info->BestBackend()->lister_),
|
|
|
|
Q_ARG(QString, info->BestBackend()->unique_id_),
|
|
|
|
Q_ARG(DeviceManager*, this), Q_ARG(Application*, app_),
|
|
|
|
Q_ARG(int, info->database_id_), Q_ARG(bool, first_time));
|
|
|
|
ret.reset(static_cast<ConnectedDevice*>(instance));
|
2010-07-17 16:22:07 +02:00
|
|
|
|
|
|
|
if (!ret) {
|
2011-04-22 18:50:29 +02:00
|
|
|
qLog(Warning) << "Could not create device for" << device_url.toString();
|
2010-07-17 16:22:07 +02:00
|
|
|
} else {
|
2021-06-05 19:25:22 +02:00
|
|
|
if (!ret->Init()) {
|
|
|
|
qLog(Warning) << "Could not initialize device for "
|
|
|
|
<< device_url.toString();
|
|
|
|
ret.reset();
|
|
|
|
return ret;
|
|
|
|
}
|
2010-08-08 19:41:06 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
info->device_ = ret;
|
2019-01-21 22:38:46 +01:00
|
|
|
QModelIndex idx = ItemToIndex(info);
|
|
|
|
if (!idx.isValid()) return ret;
|
|
|
|
emit dataChanged(idx, idx);
|
2019-01-21 10:06:48 +01:00
|
|
|
connect(info->device_.get(), SIGNAL(TaskStarted(int)),
|
2014-02-07 16:34:20 +01:00
|
|
|
SLOT(DeviceTaskStarted(int)));
|
2019-01-21 10:06:48 +01:00
|
|
|
connect(info->device_.get(), SIGNAL(SongCountUpdated(int)),
|
2014-02-07 16:34:20 +01:00
|
|
|
SLOT(DeviceSongCountUpdated(int)));
|
2019-01-21 10:06:48 +01:00
|
|
|
connect(info->device_.get(), SIGNAL(ConnectFinished(const QString&, bool)),
|
2019-01-15 22:10:05 +01:00
|
|
|
SLOT(DeviceConnectFinished(const QString&, bool)));
|
|
|
|
ret->ConnectAsync();
|
2010-07-17 16:22:07 +02:00
|
|
|
}
|
2010-07-04 17:56:08 +02:00
|
|
|
|
2010-07-17 16:22:07 +02:00
|
|
|
return ret;
|
2010-07-04 01:00:07 +02:00
|
|
|
}
|
|
|
|
|
2019-01-15 22:10:05 +01:00
|
|
|
void DeviceManager::DeviceConnectFinished(const QString& id, bool success) {
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = FindDeviceById(id);
|
|
|
|
if (!info) return;
|
|
|
|
|
|
|
|
QModelIndex idx = ItemToIndex(info);
|
|
|
|
if (!idx.isValid()) return;
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
emit DeviceConnected(idx);
|
|
|
|
} else {
|
|
|
|
info->device_.reset();
|
2020-01-08 07:51:45 +01:00
|
|
|
emit dataChanged(idx, idx);
|
2019-01-15 22:10:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-15 12:36:05 +01:00
|
|
|
std::shared_ptr<ConnectedDevice> DeviceManager::GetConnectedDevice(
|
2019-01-21 22:38:46 +01:00
|
|
|
QModelIndex idx) const {
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info) return std::shared_ptr<ConnectedDevice>();
|
|
|
|
return info->device_;
|
2010-07-04 01:00:07 +02:00
|
|
|
}
|
2010-07-04 02:33:34 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
std::shared_ptr<ConnectedDevice> DeviceManager::GetConnectedDevice(
|
|
|
|
DeviceInfo* info) const {
|
|
|
|
if (!info) return std::shared_ptr<ConnectedDevice>();
|
|
|
|
return info->device_;
|
2010-07-04 14:56:49 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
int DeviceManager::GetDatabaseId(const QModelIndex& idx) const {
|
|
|
|
if (!idx.isValid()) return -1;
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info) return -1;
|
|
|
|
return info->database_id_;
|
2010-07-04 17:01:24 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceLister* DeviceManager::GetLister(QModelIndex idx) const {
|
|
|
|
if (!idx.isValid()) return nullptr;
|
|
|
|
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info || !info->BestBackend()) return nullptr;
|
|
|
|
return info->BestBackend()->lister_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::Disconnect(QModelIndex idx) {
|
|
|
|
if (!idx.isValid()) return;
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info || !info->device_) // Already disconnected
|
2010-07-04 02:33:34 +02:00
|
|
|
return;
|
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
info->device_.reset();
|
2019-01-21 22:38:46 +01:00
|
|
|
emit DeviceDisconnected(idx);
|
|
|
|
emit dataChanged(idx, idx);
|
2010-07-04 02:33:34 +02:00
|
|
|
}
|
2010-07-04 14:56:49 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
void DeviceManager::Forget(QModelIndex idx) {
|
|
|
|
if (!idx.isValid()) return;
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info) return;
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->database_id_ == -1) return;
|
2010-07-04 14:56:49 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
if (info->device_) Disconnect(idx);
|
2010-07-04 14:56:49 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
backend_->RemoveDevice(info->database_id_);
|
|
|
|
info->database_id_ = -1;
|
2010-07-04 14:56:49 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
if (!info->BestBackend() ||
|
|
|
|
(info->BestBackend() && !info->BestBackend()->lister_)) {
|
2010-07-04 14:56:49 +02:00
|
|
|
// It's not attached any more so remove it from the list
|
2019-01-21 22:38:46 +01:00
|
|
|
beginRemoveRows(ItemToIndex(root_), idx.row(), idx.row());
|
|
|
|
devices_.removeAll(info);
|
|
|
|
root_->Delete(info->row);
|
2010-07-04 14:56:49 +02:00
|
|
|
endRemoveRows();
|
|
|
|
} else {
|
2010-07-04 17:18:37 +02:00
|
|
|
// It's still attached, set the name and icon back to what they were
|
|
|
|
// originally
|
2019-01-21 10:06:48 +01:00
|
|
|
const QString id = info->BestBackend()->unique_id_;
|
2010-07-17 23:48:09 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
info->friendly_name_ = info->BestBackend()->lister_->MakeFriendlyName(id);
|
|
|
|
info->LoadIcon(info->BestBackend()->lister_->DeviceIcons(id),
|
|
|
|
info->friendly_name_);
|
2010-07-04 17:18:37 +02:00
|
|
|
|
2021-07-14 00:57:48 +02:00
|
|
|
emit dataChanged(idx, idx);
|
2010-07-04 14:56:49 +02:00
|
|
|
}
|
|
|
|
}
|
2010-07-04 17:18:37 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
void DeviceManager::SetDeviceOptions(QModelIndex idx,
|
|
|
|
const QString& friendly_name,
|
2014-02-07 16:34:20 +01:00
|
|
|
const QString& icon_name,
|
|
|
|
MusicStorage::TranscodeMode mode,
|
|
|
|
Song::FileType format) {
|
2019-01-21 22:38:46 +01:00
|
|
|
if (!idx.isValid()) return;
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info) return;
|
2019-01-21 10:06:48 +01:00
|
|
|
info->friendly_name_ = friendly_name;
|
|
|
|
info->LoadIcon(QVariantList() << icon_name, friendly_name);
|
|
|
|
info->transcode_mode_ = mode;
|
|
|
|
info->transcode_format_ = format;
|
2010-07-04 17:18:37 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
emit dataChanged(idx, idx);
|
2010-07-04 17:18:37 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->database_id_ != -1)
|
|
|
|
backend_->SetDeviceOptions(info->database_id_, friendly_name, icon_name,
|
2010-08-29 17:32:36 +02:00
|
|
|
mode, format);
|
2010-07-04 17:18:37 +02:00
|
|
|
}
|
2010-07-04 17:56:08 +02:00
|
|
|
|
|
|
|
void DeviceManager::DeviceTaskStarted(int id) {
|
|
|
|
ConnectedDevice* device = qobject_cast<ConnectedDevice*>(sender());
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!device) return;
|
2010-07-04 17:56:08 +02:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
for (int i = 0; i < devices_.count(); ++i) {
|
2019-01-21 10:06:48 +01:00
|
|
|
DeviceInfo* info = devices_[i];
|
|
|
|
if (info->device_.get() == device) {
|
|
|
|
QModelIndex index = ItemToIndex(info);
|
|
|
|
if (!index.isValid()) continue;
|
|
|
|
active_tasks_[id] = index;
|
|
|
|
info->task_percentage_ = 0;
|
|
|
|
emit dataChanged(index, index);
|
2010-07-04 17:56:08 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::TasksChanged() {
|
2012-02-12 14:41:50 +01:00
|
|
|
QList<TaskManager::Task> tasks = app_->task_manager()->GetTasks();
|
2010-07-04 17:56:08 +02:00
|
|
|
QList<QPersistentModelIndex> finished_tasks = active_tasks_.values();
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const TaskManager::Task& task : tasks) {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!active_tasks_.contains(task.id)) continue;
|
2010-07-04 17:56:08 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
QPersistentModelIndex idx = active_tasks_[task.id];
|
|
|
|
if (!idx.isValid()) continue;
|
2010-07-04 17:56:08 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
2010-07-04 17:56:08 +02:00
|
|
|
if (task.progress_max)
|
2019-01-21 10:06:48 +01:00
|
|
|
info->task_percentage_ = float(task.progress) / task.progress_max * 100;
|
2010-07-04 17:56:08 +02:00
|
|
|
else
|
2019-01-21 10:06:48 +01:00
|
|
|
info->task_percentage_ = 0;
|
2019-01-21 22:38:46 +01:00
|
|
|
emit dataChanged(idx, idx);
|
|
|
|
finished_tasks.removeAll(idx);
|
2010-07-04 17:56:08 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
for (const QPersistentModelIndex& idx : finished_tasks) {
|
|
|
|
if (!idx.isValid()) continue;
|
2010-07-04 17:56:08 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info) continue;
|
2019-01-21 10:06:48 +01:00
|
|
|
info->task_percentage_ = -1;
|
2019-01-21 22:38:46 +01:00
|
|
|
emit dataChanged(idx, idx);
|
2010-08-29 21:22:21 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
active_tasks_.remove(active_tasks_.key(idx));
|
2010-07-04 17:56:08 +02:00
|
|
|
}
|
|
|
|
}
|
2010-07-25 03:07:51 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
void DeviceManager::UnmountAsync(QModelIndex idx) {
|
|
|
|
Q_ASSERT(QMetaObject::invokeMethod(this, "Unmount", Q_ARG(QModelIndex, idx)));
|
2010-08-01 13:01:07 +02:00
|
|
|
}
|
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
void DeviceManager::Unmount(QModelIndex idx) {
|
|
|
|
if (!idx.isValid()) return;
|
|
|
|
DeviceInfo* info = IndexToItem(idx);
|
|
|
|
if (!info) return;
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->database_id_ != -1 && !info->device_) return;
|
2010-07-25 03:07:51 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
if (info->device_) Disconnect(idx);
|
2010-07-25 03:07:51 +02:00
|
|
|
|
2019-01-21 10:06:48 +01:00
|
|
|
if (info->BestBackend()->lister_)
|
|
|
|
info->BestBackend()->lister_->UnmountDevice(
|
|
|
|
info->BestBackend()->unique_id_);
|
2010-07-25 03:07:51 +02:00
|
|
|
}
|
2010-08-30 16:45:27 +02:00
|
|
|
|
|
|
|
void DeviceManager::DeviceSongCountUpdated(int count) {
|
|
|
|
ConnectedDevice* device = qobject_cast<ConnectedDevice*>(sender());
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!device) return;
|
2010-08-30 16:45:27 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
DeviceInfo* info = FindDeviceById(device->unique_id());
|
|
|
|
if (!info) return;
|
2010-08-30 16:45:27 +02:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
QModelIndex idx = ItemToIndex(info);
|
|
|
|
if (!idx.isValid()) return;
|
2019-01-21 10:06:48 +01:00
|
|
|
|
2019-01-21 22:38:46 +01:00
|
|
|
emit dataChanged(idx, idx);
|
2019-01-21 10:06:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DeviceManager::LazyPopulate(DeviceInfo* parent, bool signal) {
|
|
|
|
if (parent->lazy_loaded) return;
|
|
|
|
parent->lazy_loaded = true;
|
2010-08-30 16:45:27 +02:00
|
|
|
}
|