strawberry-audio-player-win.../src/collection/collectiondirectorymodel.cpp

109 lines
3.1 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
*
* 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
*/
#include "config.h"
#include <memory>
#include <QObject>
#include <QStandardItemModel>
#include <QAbstractItemModel>
#include <QVariant>
#include <QString>
#include "core/shared_ptr.h"
2018-02-27 18:06:05 +01:00
#include "core/filesystemmusicstorage.h"
#include "core/iconloader.h"
2018-02-27 18:06:05 +01:00
#include "core/musicstorage.h"
#include "utilities/diskutils.h"
#include "collectiondirectory.h"
#include "collectionbackend.h"
#include "collectiondirectorymodel.h"
2018-02-27 18:06:05 +01:00
using std::make_shared;
CollectionDirectoryModel::CollectionDirectoryModel(SharedPtr<CollectionBackend> backend, QObject *parent)
2018-02-27 18:06:05 +01:00
: QStandardItemModel(parent),
dir_icon_(IconLoader::Load("document-open-folder")),
2021-01-26 16:48:04 +01:00
backend_(backend) {
2018-02-27 18:06:05 +01:00
QObject::connect(&*backend_, &CollectionBackend::DirectoryDiscovered, this, &CollectionDirectoryModel::DirectoryDiscovered);
QObject::connect(&*backend_, &CollectionBackend::DirectoryDeleted, this, &CollectionDirectoryModel::DirectoryDeleted);
2018-02-27 18:06:05 +01:00
}
void CollectionDirectoryModel::DirectoryDiscovered(const CollectionDirectory &dir) {
2018-02-27 18:06:05 +01:00
2019-11-25 00:35:48 +01:00
QStandardItem *item = new QStandardItem(dir.path);
2018-02-27 18:06:05 +01:00
item->setData(dir.id, kIdRole);
item->setIcon(dir_icon_);
storage_ << make_shared<FilesystemMusicStorage>(backend_->source(), dir.path, dir.id);
2018-02-27 18:06:05 +01:00
appendRow(item);
}
void CollectionDirectoryModel::DirectoryDeleted(const CollectionDirectory &dir) {
2018-02-27 18:06:05 +01:00
for (int i = 0; i < rowCount(); ++i) {
if (item(i, 0)->data(kIdRole).toInt() == dir.id) {
removeRow(i);
storage_.removeAt(i);
break;
}
}
}
void CollectionDirectoryModel::AddDirectory(const QString &path) {
if (!backend_) return;
backend_->AddDirectory(path);
}
2021-01-26 16:48:04 +01:00
void CollectionDirectoryModel::RemoveDirectory(const QModelIndex &idx) {
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
if (!backend_ || !idx.isValid()) return;
2018-02-27 18:06:05 +01:00
CollectionDirectory dir;
2021-01-26 16:48:04 +01:00
dir.path = idx.data().toString();
dir.id = idx.data(kIdRole).toInt();
2018-02-27 18:06:05 +01:00
backend_->RemoveDirectory(dir);
}
2021-01-26 16:48:04 +01:00
QVariant CollectionDirectoryModel::data(const QModelIndex &idx, int role) const {
2018-02-27 18:06:05 +01:00
switch (role) {
case MusicStorage::Role_Storage:
case MusicStorage::Role_StorageForceConnect:
2021-01-26 16:48:04 +01:00
return QVariant::fromValue(storage_[idx.row()]);
2018-02-27 18:06:05 +01:00
case MusicStorage::Role_FreeSpace:
2021-07-11 09:49:38 +02:00
return Utilities::FileSystemFreeSpace(data(idx, Qt::DisplayRole).toString());
2018-02-27 18:06:05 +01:00
case MusicStorage::Role_Capacity:
2021-07-11 09:49:38 +02:00
return Utilities::FileSystemCapacity(data(idx, Qt::DisplayRole).toString());
2018-02-27 18:06:05 +01:00
default:
2021-01-26 16:48:04 +01:00
return QStandardItemModel::data(idx, role);
2018-02-27 18:06:05 +01:00
}
}