2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-03-24 00:11:46 +01: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-02-03 17:17:04 +01:00
|
|
|
#include "librarydirectorymodel.h"
|
|
|
|
#include "librarybackend.h"
|
2013-10-03 17:08:42 +02:00
|
|
|
#include "core/application.h"
|
2010-07-19 21:56:29 +02:00
|
|
|
#include "core/filesystemmusicstorage.h"
|
|
|
|
#include "core/musicstorage.h"
|
2010-07-30 00:16:12 +02:00
|
|
|
#include "core/utilities.h"
|
2010-05-19 17:45:29 +02:00
|
|
|
#include "ui/iconloader.h"
|
2010-02-03 17:17:04 +01:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
LibraryDirectoryModel::LibraryDirectoryModel(LibraryBackend* backend, QObject* parent)
|
2010-02-03 17:17:04 +01:00
|
|
|
: QStandardItemModel(parent),
|
2010-05-19 17:45:29 +02:00
|
|
|
dir_icon_(IconLoader::Load("document-open-folder")),
|
2010-05-09 02:10:26 +02:00
|
|
|
backend_(backend)
|
2010-02-03 17:17:04 +01:00
|
|
|
{
|
2010-05-09 02:10:26 +02:00
|
|
|
connect(backend_, SIGNAL(DirectoryDiscovered(Directory, SubdirectoryList)), SLOT(DirectoryDiscovered(Directory)));
|
|
|
|
connect(backend_, SIGNAL(DirectoryDeleted(Directory)), SLOT(DirectoryDeleted(Directory)));
|
2010-02-03 17:17:04 +01:00
|
|
|
}
|
|
|
|
|
2010-07-19 21:56:29 +02:00
|
|
|
LibraryDirectoryModel::~LibraryDirectoryModel() {
|
|
|
|
}
|
|
|
|
|
2010-04-01 18:59:32 +02:00
|
|
|
void LibraryDirectoryModel::DirectoryDiscovered(const Directory &dir) {
|
2013-10-03 17:08:42 +02:00
|
|
|
QStandardItem* item;
|
|
|
|
if (Application::kIsPortable
|
|
|
|
&& Utilities::UrlOnSameDriveAsClementine(QUrl::fromLocalFile(dir.path))) {
|
|
|
|
item = new QStandardItem(Utilities::GetRelativePathToClementineBin(
|
|
|
|
QUrl::fromLocalFile(dir.path)).toLocalFile());
|
|
|
|
} else {
|
|
|
|
item = new QStandardItem(dir.path);
|
|
|
|
}
|
2010-04-01 18:59:32 +02:00
|
|
|
item->setData(dir.id, kIdRole);
|
|
|
|
item->setIcon(dir_icon_);
|
2010-08-09 23:50:46 +02:00
|
|
|
storage_ << boost::shared_ptr<MusicStorage>(new FilesystemMusicStorage(dir.path));
|
2010-04-01 18:59:32 +02:00
|
|
|
appendRow(item);
|
2010-02-03 17:17:04 +01:00
|
|
|
}
|
|
|
|
|
2010-04-01 18:59:32 +02:00
|
|
|
void LibraryDirectoryModel::DirectoryDeleted(const Directory &dir) {
|
|
|
|
for (int i=0 ; i<rowCount() ; ++i) {
|
|
|
|
if (item(i, 0)->data(kIdRole).toInt() == dir.id) {
|
|
|
|
removeRow(i);
|
2010-08-09 23:50:46 +02:00
|
|
|
storage_.removeAt(i);
|
2010-04-01 18:59:32 +02:00
|
|
|
break;
|
2010-02-03 17:17:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryDirectoryModel::AddDirectory(const QString& path) {
|
|
|
|
if (!backend_)
|
|
|
|
return;
|
|
|
|
|
|
|
|
backend_->AddDirectory(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryDirectoryModel::RemoveDirectory(const QModelIndex& index) {
|
|
|
|
if (!backend_ || !index.isValid())
|
|
|
|
return;
|
|
|
|
|
|
|
|
Directory dir;
|
|
|
|
dir.path = index.data().toString();
|
|
|
|
dir.id = index.data(kIdRole).toInt();
|
|
|
|
|
|
|
|
backend_->RemoveDirectory(dir);
|
|
|
|
}
|
2010-07-19 21:56:29 +02:00
|
|
|
|
|
|
|
QVariant LibraryDirectoryModel::data(const QModelIndex &index, int role) const {
|
2010-07-30 00:16:12 +02:00
|
|
|
switch (role) {
|
|
|
|
case MusicStorage::Role_Storage:
|
2010-08-30 17:40:53 +02:00
|
|
|
case MusicStorage::Role_StorageForceConnect:
|
2010-07-19 21:56:29 +02:00
|
|
|
return QVariant::fromValue(storage_[index.row()]);
|
|
|
|
|
2010-07-30 00:16:12 +02:00
|
|
|
case MusicStorage::Role_FreeSpace:
|
|
|
|
return Utilities::FileSystemFreeSpace(data(index, Qt::DisplayRole).toString());
|
|
|
|
|
|
|
|
case MusicStorage::Role_Capacity:
|
|
|
|
return Utilities::FileSystemCapacity(data(index, Qt::DisplayRole).toString());
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QStandardItemModel::data(index, role);
|
|
|
|
}
|
2010-07-19 21:56:29 +02:00
|
|
|
}
|