2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2018-12-29 02:57:22 +01:00
|
|
|
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
|
2018-02-27 18:06:05 +01:00
|
|
|
*
|
|
|
|
* Strawberry 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.
|
|
|
|
*
|
|
|
|
* Strawberry 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 Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DEVICEMANAGER_H
|
|
|
|
#define DEVICEMANAGER_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QObject>
|
|
|
|
#include <QMetaObject>
|
|
|
|
#include <QThreadPool>
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QList>
|
|
|
|
#include <QMap>
|
|
|
|
#include <QMultiMap>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QString>
|
|
|
|
#include <QStringList>
|
2018-12-29 02:57:22 +01:00
|
|
|
#include <QUrl>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QIcon>
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/song.h"
|
|
|
|
#include "core/musicstorage.h"
|
2018-12-29 02:57:22 +01:00
|
|
|
#include "core/simpletreemodel.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "collection/collectionmodel.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "devicedatabasebackend.h"
|
2018-12-29 02:57:22 +01:00
|
|
|
#include "deviceinfo.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2020-02-09 02:29:35 +01:00
|
|
|
class QModelIndex;
|
|
|
|
class QPersistentModelIndex;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
class Application;
|
|
|
|
class ConnectedDevice;
|
|
|
|
class DeviceLister;
|
|
|
|
class DeviceStateFilterModel;
|
|
|
|
|
2018-12-29 02:57:22 +01:00
|
|
|
class DeviceManager : public SimpleTreeModel<DeviceInfo> {
|
2018-02-27 18:06:05 +01:00
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
2020-04-07 16:49:15 +02:00
|
|
|
explicit DeviceManager(Application *app, QObject *parent = nullptr);
|
2020-06-15 21:55:05 +02:00
|
|
|
~DeviceManager() override;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
enum Role {
|
|
|
|
Role_State = CollectionModel::LastRole,
|
|
|
|
Role_UniqueId,
|
|
|
|
Role_FriendlyName,
|
|
|
|
Role_Capacity,
|
|
|
|
Role_FreeSpace,
|
|
|
|
Role_IconName,
|
|
|
|
Role_UpdatingPercentage,
|
|
|
|
Role_MountPath,
|
|
|
|
Role_TranscodeMode,
|
|
|
|
Role_TranscodeFormat,
|
|
|
|
Role_SongCount,
|
2020-03-17 20:44:51 +01:00
|
|
|
Role_CopyMusic,
|
2018-02-27 18:06:05 +01:00
|
|
|
LastRole,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum State {
|
|
|
|
State_Remembered,
|
|
|
|
State_NotMounted,
|
|
|
|
State_NotConnected,
|
|
|
|
State_Connected,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int kDeviceIconSize;
|
|
|
|
static const int kDeviceIconOverlaySize;
|
|
|
|
|
2019-07-24 19:16:51 +02:00
|
|
|
void Exit();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
DeviceStateFilterModel *connected_devices_model() const { return connected_devices_model_; }
|
|
|
|
|
|
|
|
// Get info about devices
|
2019-01-21 17:44:37 +01:00
|
|
|
int GetDatabaseId(QModelIndex idx) const;
|
|
|
|
DeviceLister *GetLister(QModelIndex idx) const;
|
|
|
|
DeviceInfo *GetDevice(QModelIndex idx) const;
|
|
|
|
std::shared_ptr<ConnectedDevice> GetConnectedDevice(QModelIndex idx) const;
|
|
|
|
std::shared_ptr<ConnectedDevice> GetConnectedDevice(DeviceInfo *info) const;
|
|
|
|
|
|
|
|
DeviceInfo *FindDeviceById(const QString &id) const;
|
|
|
|
DeviceInfo *FindDeviceByUrl(const QList<QUrl> &url) const;
|
2019-09-15 20:27:32 +02:00
|
|
|
QString DeviceNameByID(const QString &unique_id);
|
2019-03-22 23:10:42 +01:00
|
|
|
DeviceInfo *FindEquivalentDevice(DeviceInfo *info) const;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
// Actions on devices
|
2019-01-21 17:44:37 +01:00
|
|
|
std::shared_ptr<ConnectedDevice> Connect(DeviceInfo *info);
|
|
|
|
std::shared_ptr<ConnectedDevice> Connect(QModelIndex idx);
|
2019-09-07 23:30:35 +02:00
|
|
|
void Disconnect(DeviceInfo *info, QModelIndex idx);
|
2019-01-21 17:44:37 +01:00
|
|
|
void Forget(QModelIndex idx);
|
|
|
|
void UnmountAsync(QModelIndex idx);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-01-21 17:44:37 +01:00
|
|
|
void SetDeviceOptions(QModelIndex idx, const QString &friendly_name, const QString &icon_name, MusicStorage::TranscodeMode mode, Song::FileType format);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-29 02:57:22 +01:00
|
|
|
// QAbstractItemModel
|
2020-06-15 21:55:05 +02:00
|
|
|
QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const override;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
public slots:
|
2019-01-21 17:44:37 +01:00
|
|
|
void Unmount(QModelIndex idx);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-29 02:57:22 +01:00
|
|
|
signals:
|
2019-07-24 19:16:51 +02:00
|
|
|
void ExitFinished();
|
2019-01-21 17:44:37 +01:00
|
|
|
void DeviceConnected(QModelIndex idx);
|
|
|
|
void DeviceDisconnected(QModelIndex idx);
|
2019-03-22 23:10:42 +01:00
|
|
|
void DeviceCreatedFromDB(DeviceInfo* info);
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
private slots:
|
|
|
|
void PhysicalDeviceAdded(const QString &id);
|
|
|
|
void PhysicalDeviceRemoved(const QString &id);
|
|
|
|
void PhysicalDeviceChanged(const QString &id);
|
|
|
|
void DeviceTaskStarted(int id);
|
|
|
|
void TasksChanged();
|
|
|
|
void DeviceSongCountUpdated(int count);
|
|
|
|
void LoadAllDevices();
|
2019-01-21 18:58:54 +01:00
|
|
|
void DeviceConnectFinished(const QString &id, bool success);
|
2019-09-07 23:30:35 +02:00
|
|
|
void DeviceCloseFinished(const QString &id);
|
2019-03-22 23:10:42 +01:00
|
|
|
void AddDeviceFromDB(DeviceInfo *info);
|
2019-09-07 23:30:35 +02:00
|
|
|
void BackendClosed();
|
|
|
|
void ListerClosed();
|
|
|
|
void DeviceDestroyed();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-12-29 02:57:22 +01:00
|
|
|
protected:
|
2020-06-15 21:55:05 +02:00
|
|
|
void LazyPopulate(DeviceInfo *item) override { LazyPopulate(item, true); }
|
2020-06-14 18:58:24 +02:00
|
|
|
void LazyPopulate(DeviceInfo *parent, const bool signal);
|
2018-12-29 02:57:22 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
private:
|
|
|
|
void AddLister(DeviceLister *lister);
|
|
|
|
template <typename T> void AddDeviceClass();
|
|
|
|
|
|
|
|
DeviceDatabaseBackend::Device InfoToDatabaseDevice(const DeviceInfo &info) const;
|
|
|
|
|
2019-09-07 23:30:35 +02:00
|
|
|
void RemoveFromDB(DeviceInfo *info, QModelIndex idx);
|
|
|
|
|
|
|
|
void CloseDevices();
|
|
|
|
void CloseListers();
|
|
|
|
void CloseBackend();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
private:
|
|
|
|
Application *app_;
|
|
|
|
DeviceDatabaseBackend *backend_;
|
|
|
|
|
|
|
|
DeviceStateFilterModel *connected_devices_model_;
|
|
|
|
|
|
|
|
QIcon not_connected_overlay_;
|
|
|
|
|
|
|
|
QList<DeviceLister*> listers_;
|
2018-12-29 02:57:22 +01:00
|
|
|
QList<DeviceInfo*> devices_;
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
QMultiMap<QString, QMetaObject> device_classes_;
|
|
|
|
|
|
|
|
// Map of task ID to device index
|
|
|
|
QMap<int, QPersistentModelIndex> active_tasks_;
|
|
|
|
|
|
|
|
QThreadPool thread_pool_;
|
2019-09-07 23:30:35 +02:00
|
|
|
|
|
|
|
QList<QObject*> wait_for_exit_;
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void DeviceManager::AddDeviceClass() {
|
|
|
|
QStringList schemes = T::url_schemes();
|
|
|
|
QMetaObject obj = T::staticMetaObject;
|
|
|
|
|
|
|
|
for (const QString &scheme : schemes) {
|
|
|
|
device_classes_.insert(scheme, obj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DEVICEMANAGER_H
|