Convert devicemanager to QAbstractItemModel

This commit is contained in:
Jonas Kvinge
2018-12-29 02:57:22 +01:00
parent fc9f93791d
commit 404283be19
35 changed files with 574 additions and 379 deletions

View File

@@ -2,6 +2,7 @@
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
* Copyright 2018, Jonas Kvinge <jonas@jkvinge.net>
*
* Strawberry is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -30,28 +31,29 @@
#include <QMetaObject>
#include <QThreadPool>
#include <QAbstractItemModel>
#include <QAbstractListModel>
#include <QList>
#include <QMap>
#include <QMultiMap>
#include <QMetaType>
#include <QVariant>
#include <QUrl>
#include <QString>
#include <QStringList>
#include <QUrl>
#include <QIcon>
#include "core/song.h"
#include "core/musicstorage.h"
#include "core/simpletreemodel.h"
#include "collection/collectionmodel.h"
#include "devicedatabasebackend.h"
#include "deviceinfo.h"
class Application;
class ConnectedDevice;
class DeviceLister;
class DeviceStateFilterModel;
class DeviceManager : public QAbstractListModel {
class DeviceManager : public SimpleTreeModel<DeviceInfo> {
Q_OBJECT
public:
@@ -90,8 +92,10 @@ class DeviceManager : public QAbstractListModel {
DeviceLister *GetLister(int row) const;
std::shared_ptr<ConnectedDevice> GetConnectedDevice(int row) const;
DeviceInfo *ItemFromRow(int row);
int FindDeviceById(const QString &id) const;
int FindDeviceByUrl(const QList<QUrl> &url) const;
QString DeviceNameByID(QString unique_id);
// Actions on devices
std::shared_ptr<ConnectedDevice> Connect(int row);
@@ -101,14 +105,13 @@ class DeviceManager : public QAbstractListModel {
void SetDeviceOptions(int row, const QString &friendly_name, const QString &icon_name, MusicStorage::TranscodeMode mode, Song::FileType format);
// QAbstractListModel
int rowCount(const QModelIndex &parent = QModelIndex()) const;
// QAbstractItemModel
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
public slots:
void Unmount(int row);
signals:
signals:
void DeviceConnected(int row);
void DeviceDisconnected(int row);
@@ -121,55 +124,11 @@ signals:
void DeviceSongCountUpdated(int count);
void LoadAllDevices();
protected:
void LazyPopulate(DeviceInfo *item) { LazyPopulate(item, true); }
void LazyPopulate(DeviceInfo *item, bool signal);
private:
// Devices can be in three different states:
// 1) Remembered in the database but not physically connected at the moment.
// database_id valid, lister null, device null
// 2) Physically connected but the user hasn't "connected" it to Strawberry
// yet.
// database_id == -1, lister valid, device null
// 3) Physically connected and connected to Strawberry
// database_id valid, lister valid, device valid
// Devices in all states will have a unique_id.
struct DeviceInfo {
DeviceInfo();
// A device can be discovered in different ways (devicekit, gio, etc.)
// Sometimes the same device is discovered more than once. In this case the device will have multiple "backends".
struct Backend {
Backend(DeviceLister *lister = nullptr, const QString &id = QString())
: lister_(lister), unique_id_(id) {}
DeviceLister *lister_; // nullptr if not physically connected
QString unique_id_;
};
// Serialising to the database
void InitFromDb(const DeviceDatabaseBackend::Device &dev);
DeviceDatabaseBackend::Device SaveToDb() const;
// Tries to load a good icon for the device. Sets icon_name_ and icon_.
void LoadIcon(const QVariantList &icons, const QString &name_hint);
// Gets the best backend available (the one with the highest priority)
const Backend *BestBackend() const;
int database_id_; // -1 if not remembered in the database
std::shared_ptr<ConnectedDevice>
device_; // nullptr if not connected
QList<Backend> backends_;
QString friendly_name_;
quint64 size_;
QString icon_name_;
QIcon icon_;
MusicStorage::TranscodeMode transcode_mode_;
Song::FileType transcode_format_;
int task_percentage_;
};
void AddLister(DeviceLister *lister);
template <typename T> void AddDeviceClass();
@@ -185,7 +144,7 @@ signals:
QIcon not_connected_overlay_;
QList<DeviceLister*> listers_;
QList<DeviceInfo> devices_;
QList<DeviceInfo*> devices_;
QMultiMap<QString, QMetaObject> device_classes_;