2010-05-09 02:10:26 +02:00
|
|
|
/* This file is part of Clementine.
|
2010-11-20 14:27:10 +01:00
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
2010-05-09 02:10:26 +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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "librarymodel.h"
|
|
|
|
#include "librarybackend.h"
|
|
|
|
#include "libraryitem.h"
|
|
|
|
#include "librarydirectorymodel.h"
|
2011-02-16 02:04:48 +01:00
|
|
|
#include "libraryview.h"
|
2010-08-03 20:57:17 +02:00
|
|
|
#include "sqlrow.h"
|
2012-02-12 14:41:50 +01:00
|
|
|
#include "core/application.h"
|
2010-06-02 18:22:20 +02:00
|
|
|
#include "core/database.h"
|
2011-06-14 18:13:48 +02:00
|
|
|
#include "core/logging.h"
|
2011-02-26 15:27:57 +01:00
|
|
|
#include "core/taskmanager.h"
|
2011-04-02 15:34:06 +02:00
|
|
|
#include "covers/albumcoverloader.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "playlist/songmimedata.h"
|
2010-11-18 21:19:33 +01:00
|
|
|
#include "smartplaylists/generator.h"
|
2010-11-17 21:21:04 +01:00
|
|
|
#include "smartplaylists/generatormimedata.h"
|
2010-11-18 21:19:33 +01:00
|
|
|
#include "smartplaylists/querygenerator.h"
|
2010-05-19 17:45:29 +02:00
|
|
|
#include "ui/iconloader.h"
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2010-11-27 21:09:00 +01:00
|
|
|
#include <QFuture>
|
|
|
|
#include <QFutureWatcher>
|
|
|
|
#include <QMetaEnum>
|
2012-06-02 15:52:30 +02:00
|
|
|
#include <QPixmapCache>
|
2010-11-17 21:21:04 +01:00
|
|
|
#include <QSettings>
|
2010-05-09 02:10:26 +02:00
|
|
|
#include <QStringList>
|
|
|
|
#include <QUrl>
|
2010-11-27 21:09:00 +01:00
|
|
|
#include <QtConcurrentRun>
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
#include <boost/bind.hpp>
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
using smart_playlists::Generator;
|
|
|
|
using smart_playlists::GeneratorMimeData;
|
|
|
|
using smart_playlists::GeneratorPtr;
|
|
|
|
using smart_playlists::QueryGenerator;
|
|
|
|
|
2010-11-17 21:21:04 +01:00
|
|
|
const char* LibraryModel::kSmartPlaylistsMimeType = "application/x-clementine-smart-playlist-generator";
|
|
|
|
const char* LibraryModel::kSmartPlaylistsSettingsGroup = "SerialisedSmartPlaylists";
|
2011-04-24 19:52:16 +02:00
|
|
|
const int LibraryModel::kSmartPlaylistsVersion = 4;
|
2011-11-13 01:31:27 +01:00
|
|
|
const int LibraryModel::kPrettyCoverSize = 32;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
2012-10-25 03:54:12 +02:00
|
|
|
typedef QFuture<struct LibraryModel::QueryResult> RootQueryFuture;
|
|
|
|
typedef QFutureWatcher<struct LibraryModel::QueryResult> RootQueryWatcher;
|
2010-11-27 21:09:00 +01:00
|
|
|
|
2011-11-28 07:03:28 +01:00
|
|
|
static bool IsArtistGroupBy(const LibraryModel::GroupBy by) {
|
|
|
|
return by == LibraryModel::GroupBy_Artist || by == LibraryModel::GroupBy_AlbumArtist;
|
|
|
|
}
|
|
|
|
|
2012-02-12 14:41:50 +01:00
|
|
|
LibraryModel::LibraryModel(LibraryBackend* backend, Application* app,
|
2011-02-26 15:27:57 +01:00
|
|
|
QObject* parent)
|
2010-05-09 02:10:26 +02:00
|
|
|
: SimpleTreeModel<LibraryItem>(new LibraryItem(this), parent),
|
|
|
|
backend_(backend),
|
2012-02-12 14:41:50 +01:00
|
|
|
app_(app),
|
2010-05-09 02:10:26 +02:00
|
|
|
dir_model_(new LibraryDirectoryModel(backend, this)),
|
2010-11-17 21:21:04 +01:00
|
|
|
show_smart_playlists_(false),
|
2010-11-27 20:37:34 +01:00
|
|
|
show_various_artists_(true),
|
2010-11-27 21:20:26 +01:00
|
|
|
total_song_count_(0),
|
2010-09-18 15:35:52 +02:00
|
|
|
artist_icon_(":/icons/22x22/x-clementine-artist.png"),
|
|
|
|
album_icon_(":/icons/22x22/x-clementine-album.png"),
|
2010-11-20 16:29:42 +01:00
|
|
|
playlists_dir_icon_(IconLoader::Load("folder-sound")),
|
2011-01-02 15:51:01 +01:00
|
|
|
playlist_icon_(":/icons/22x22/x-clementine-albums.png"),
|
2011-02-26 15:27:57 +01:00
|
|
|
init_task_id_(-1),
|
2011-03-05 16:20:27 +01:00
|
|
|
use_pretty_covers_(false),
|
2012-02-13 21:44:04 +01:00
|
|
|
show_dividers_(true)
|
2010-05-09 02:10:26 +02:00
|
|
|
{
|
|
|
|
root_->lazy_loaded = true;
|
|
|
|
|
|
|
|
group_by_[0] = GroupBy_Artist;
|
|
|
|
group_by_[1] = GroupBy_Album;
|
|
|
|
group_by_[2] = GroupBy_None;
|
2011-01-02 19:58:52 +01:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
cover_loader_options_.desired_height_ = kPrettyCoverSize;
|
|
|
|
cover_loader_options_.pad_output_image_ = true;
|
|
|
|
cover_loader_options_.scale_output_image_ = true;
|
2011-11-13 01:31:27 +01:00
|
|
|
|
2012-02-13 21:44:04 +01:00
|
|
|
connect(app_->album_cover_loader(),
|
2011-11-13 01:31:27 +01:00
|
|
|
SIGNAL(ImageLoaded(quint64,QImage)),
|
|
|
|
SLOT(AlbumArtLoaded(quint64,QImage)));
|
|
|
|
|
2012-06-10 18:15:32 +02:00
|
|
|
no_cover_icon_ = QPixmap(":nocover.png").scaled(
|
2011-11-13 01:31:27 +01:00
|
|
|
kPrettyCoverSize, kPrettyCoverSize,
|
|
|
|
Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
2012-06-16 21:54:08 +02:00
|
|
|
|
|
|
|
connect(backend_, SIGNAL(SongsDiscovered(SongList)), SLOT(SongsDiscovered(SongList)));
|
|
|
|
connect(backend_, SIGNAL(SongsDeleted(SongList)), SLOT(SongsDeleted(SongList)));
|
|
|
|
connect(backend_, SIGNAL(SongsStatisticsChanged(SongList)), SLOT(SongsStatisticsChanged(SongList)));
|
|
|
|
connect(backend_, SIGNAL(DatabaseReset()), SLOT(Reset()));
|
|
|
|
connect(backend_, SIGNAL(TotalSongCountUpdated(int)), SLOT(TotalSongCountUpdatedSlot(int)));
|
|
|
|
|
|
|
|
backend_->UpdateTotalSongCountAsync();
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LibraryModel::~LibraryModel() {
|
|
|
|
delete root_;
|
|
|
|
}
|
|
|
|
|
2011-01-02 15:51:01 +01:00
|
|
|
void LibraryModel::set_pretty_covers(bool use_pretty_covers) {
|
|
|
|
if (use_pretty_covers != use_pretty_covers_) {
|
|
|
|
use_pretty_covers_ = use_pretty_covers;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-05 16:20:27 +01:00
|
|
|
void LibraryModel::set_show_dividers(bool show_dividers) {
|
|
|
|
if (show_dividers != show_dividers_) {
|
|
|
|
show_dividers_ = show_dividers;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-26 14:34:31 +01:00
|
|
|
void LibraryModel::Init(bool async) {
|
|
|
|
if (async) {
|
2011-02-26 15:27:57 +01:00
|
|
|
// Show a loading indicator in the model.
|
|
|
|
LibraryItem* loading = new LibraryItem(LibraryItem::Type_LoadingIndicator, root_);
|
|
|
|
loading->display_text = tr("Loading...");
|
|
|
|
loading->lazy_loaded = true;
|
|
|
|
reset();
|
|
|
|
|
|
|
|
// Show a loading indicator in the status bar too.
|
2012-02-12 14:41:50 +01:00
|
|
|
init_task_id_ = app_->task_manager()->StartTask(tr("Loading songs"));
|
2011-02-26 15:27:57 +01:00
|
|
|
|
2011-02-26 14:34:31 +01:00
|
|
|
ResetAsync();
|
|
|
|
} else {
|
|
|
|
Reset();
|
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::SongsDiscovered(const SongList& songs) {
|
|
|
|
foreach (const Song& song, songs) {
|
|
|
|
// Sanity check to make sure we don't add songs that are outside the user's
|
|
|
|
// filter
|
|
|
|
if (!query_options_.Matches(song))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Hey, we've already got that one!
|
|
|
|
if (song_nodes_.contains(song.id()))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Before we can add each song we need to make sure the required container
|
|
|
|
// items already exist in the tree. These depend on which "group by"
|
|
|
|
// settings the user has on the library. Eg. if the user grouped by
|
|
|
|
// artist and album, we would need to make sure nodes for the song's artist
|
|
|
|
// and album were already in the tree.
|
|
|
|
|
|
|
|
// Find parent containers in the tree
|
|
|
|
LibraryItem* container = root_;
|
|
|
|
for (int i=0 ; i<3 ; ++i) {
|
|
|
|
GroupBy type = group_by_[i];
|
|
|
|
if (type == GroupBy_None) break;
|
|
|
|
|
|
|
|
// Special case: if we're at the top level and the song is a compilation
|
|
|
|
// and the top level is Artists, then we want the Various Artists node :(
|
2011-11-28 07:03:28 +01:00
|
|
|
if (i == 0 && IsArtistGroupBy(type) && song.is_compilation()) {
|
2010-05-09 02:10:26 +02:00
|
|
|
if (compilation_artist_node_ == NULL)
|
|
|
|
CreateCompilationArtistNode(true, root_);
|
|
|
|
container = compilation_artist_node_;
|
|
|
|
} else {
|
|
|
|
// Otherwise find the proper container at this level based on the
|
|
|
|
// item's key
|
|
|
|
QString key;
|
|
|
|
switch (type) {
|
|
|
|
case GroupBy_Album: key = song.album(); break;
|
|
|
|
case GroupBy_Artist: key = song.artist(); break;
|
|
|
|
case GroupBy_Composer: key = song.composer(); break;
|
|
|
|
case GroupBy_Genre: key = song.genre(); break;
|
2011-11-28 06:58:27 +01:00
|
|
|
case GroupBy_AlbumArtist: key = song.effective_albumartist(); break;
|
2010-05-09 02:10:26 +02:00
|
|
|
case GroupBy_Year:
|
|
|
|
key = QString::number(qMax(0, song.year())); break;
|
|
|
|
case GroupBy_YearAlbum:
|
|
|
|
key = PrettyYearAlbum(qMax(0, song.year()), song.album()); break;
|
2010-07-31 18:13:50 +02:00
|
|
|
case GroupBy_FileType: key = song.filetype(); break;
|
2011-06-14 17:33:51 +02:00
|
|
|
case GroupBy_None:
|
|
|
|
qLog(Error) << "GroupBy_None";
|
|
|
|
break;
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Does it exist already?
|
|
|
|
if (!container_nodes_[i].contains(key)) {
|
|
|
|
// Create the container
|
|
|
|
container_nodes_[i][key] =
|
|
|
|
ItemFromSong(type, true, i == 0, container, song, i);
|
|
|
|
}
|
|
|
|
container = container_nodes_[i][key];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we just created the damn thing then we don't need to continue into
|
|
|
|
// it any further because it'll get lazy-loaded properly later.
|
|
|
|
if (!container->lazy_loaded)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!container->lazy_loaded)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// We've gone all the way down to the deepest level and everything was
|
|
|
|
// already lazy loaded, so now we have to create the song in the container.
|
|
|
|
song_nodes_[song.id()] =
|
|
|
|
ItemFromSong(GroupBy_None, true, false, container, song, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-17 21:34:45 +02:00
|
|
|
void LibraryModel::SongsStatisticsChanged(const SongList& songs) {
|
|
|
|
// This is called if there was a minor change to the songs that will not
|
|
|
|
// normally require the library to be restructured. We can just update our
|
|
|
|
// internal cache of Song objects without worrying about resetting the model.
|
|
|
|
foreach (const Song& song, songs) {
|
|
|
|
if (song_nodes_.contains(song.id())) {
|
|
|
|
song_nodes_[song.id()]->metadata = song;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
LibraryItem* LibraryModel::CreateCompilationArtistNode(bool signal, LibraryItem* parent) {
|
|
|
|
if (signal)
|
|
|
|
beginInsertRows(ItemToIndex(parent), parent->children.count(), parent->children.count());
|
|
|
|
|
|
|
|
compilation_artist_node_ =
|
|
|
|
new LibraryItem(LibraryItem::Type_Container, parent);
|
2011-10-30 17:53:39 +01:00
|
|
|
compilation_artist_node_->key = tr("Various artists");
|
2010-05-09 02:10:26 +02:00
|
|
|
compilation_artist_node_->sort_text = " various";
|
|
|
|
compilation_artist_node_->container_level = parent->container_level + 1;
|
|
|
|
|
|
|
|
if (signal)
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
return compilation_artist_node_;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString LibraryModel::DividerKey(GroupBy type, LibraryItem* item) const {
|
|
|
|
// Items which are to be grouped under the same divider must produce the
|
|
|
|
// same divider key. This will only get called for top-level items.
|
|
|
|
|
|
|
|
if (item->sort_text.isEmpty())
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GroupBy_Album:
|
|
|
|
case GroupBy_Artist:
|
|
|
|
case GroupBy_Composer:
|
|
|
|
case GroupBy_Genre:
|
|
|
|
case GroupBy_AlbumArtist:
|
2010-08-29 18:46:28 +02:00
|
|
|
case GroupBy_FileType: {
|
2010-08-29 18:58:22 +02:00
|
|
|
QChar c = item->sort_text[0];
|
|
|
|
if (c.isDigit())
|
2010-05-09 02:10:26 +02:00
|
|
|
return "0";
|
2010-08-29 18:58:22 +02:00
|
|
|
if (c == ' ')
|
2010-05-09 02:10:26 +02:00
|
|
|
return QString();
|
2010-08-29 18:46:28 +02:00
|
|
|
if (c.decompositionTag() != QChar::NoDecomposition)
|
2010-08-29 18:58:22 +02:00
|
|
|
return QChar(c.decomposition()[0]);
|
|
|
|
return c;
|
2010-08-29 18:46:28 +02:00
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
case GroupBy_Year:
|
|
|
|
return SortTextForYear(item->sort_text.toInt() / 10 * 10);
|
|
|
|
|
|
|
|
case GroupBy_YearAlbum:
|
|
|
|
return SortTextForYear(item->metadata.year());
|
|
|
|
|
|
|
|
case GroupBy_None:
|
2010-08-30 18:37:29 +02:00
|
|
|
return QString();
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
2011-06-14 17:33:51 +02:00
|
|
|
qLog(Error) << "Unknown GroupBy type" << type << "for item" << item->display_text;
|
2010-07-31 16:14:03 +02:00
|
|
|
return QString();
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString LibraryModel::DividerDisplayText(GroupBy type, const QString& key) const {
|
|
|
|
// Pretty display text for the dividers.
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GroupBy_Album:
|
|
|
|
case GroupBy_Artist:
|
|
|
|
case GroupBy_Composer:
|
|
|
|
case GroupBy_Genre:
|
|
|
|
case GroupBy_AlbumArtist:
|
2010-07-31 16:14:03 +02:00
|
|
|
case GroupBy_FileType:
|
2010-05-09 02:10:26 +02:00
|
|
|
if (key == "0")
|
|
|
|
return "0-9";
|
|
|
|
return key.toUpper();
|
|
|
|
|
|
|
|
case GroupBy_YearAlbum:
|
|
|
|
if (key == "0000")
|
|
|
|
return tr("Unknown");
|
|
|
|
return key.toUpper();
|
|
|
|
|
|
|
|
case GroupBy_Year:
|
|
|
|
if (key == "0000")
|
|
|
|
return tr("Unknown");
|
|
|
|
return QString::number(key.toInt()); // To remove leading 0s
|
|
|
|
|
|
|
|
case GroupBy_None:
|
2010-07-31 16:14:03 +02:00
|
|
|
// fallthrough
|
|
|
|
;
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
2011-06-14 17:33:51 +02:00
|
|
|
qLog(Error) << "Unknown GroupBy type" << type << "for divider key" << key;
|
2010-07-31 16:14:03 +02:00
|
|
|
return QString();
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::SongsDeleted(const SongList& songs) {
|
|
|
|
// Delete the actual song nodes first, keeping track of each parent so we
|
|
|
|
// might check to see if they're empty later.
|
|
|
|
QSet<LibraryItem*> parents;
|
|
|
|
foreach (const Song& song, songs) {
|
|
|
|
if (song_nodes_.contains(song.id())) {
|
|
|
|
LibraryItem* node = song_nodes_[song.id()];
|
|
|
|
|
|
|
|
if (node->parent != root_)
|
|
|
|
parents << node->parent;
|
|
|
|
|
|
|
|
beginRemoveRows(ItemToIndex(node->parent), node->row, node->row);
|
|
|
|
node->parent->Delete(node->row);
|
|
|
|
song_nodes_.remove(song.id());
|
|
|
|
endRemoveRows();
|
|
|
|
} else {
|
|
|
|
// If we get here it means some of the songs we want to delete haven't
|
|
|
|
// been lazy-loaded yet. This is bad, because it would mean that to
|
|
|
|
// clean up empty parents we would need to lazy-load them all
|
|
|
|
// individually to see if they're empty. This can take a very long time,
|
|
|
|
// so better to just reset the model and be done with it.
|
|
|
|
Reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now delete empty parents
|
|
|
|
QSet<QString> divider_keys;
|
|
|
|
while (!parents.isEmpty()) {
|
|
|
|
foreach (LibraryItem* node, parents) {
|
|
|
|
parents.remove(node);
|
|
|
|
if (node->children.count() != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Consider its parent for the next round
|
|
|
|
if (node->parent != root_)
|
|
|
|
parents << node->parent;
|
|
|
|
|
|
|
|
// Maybe consider its divider node
|
|
|
|
if (node->container_level == 0)
|
|
|
|
divider_keys << DividerKey(group_by_[0], node);
|
|
|
|
|
|
|
|
// Special case the Various Artists node
|
|
|
|
if (node == compilation_artist_node_)
|
|
|
|
compilation_artist_node_ = NULL;
|
|
|
|
else
|
|
|
|
container_nodes_[node->container_level].remove(node->key);
|
|
|
|
|
|
|
|
// It was empty - delete it
|
|
|
|
beginRemoveRows(ItemToIndex(node->parent), node->row, node->row);
|
|
|
|
node->parent->Delete(node->row);
|
|
|
|
endRemoveRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete empty dividers
|
|
|
|
foreach (const QString& divider_key, divider_keys) {
|
|
|
|
if (!divider_nodes_.contains(divider_key))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Look to see if there are any other items still under this divider
|
|
|
|
bool found = false;
|
|
|
|
foreach (LibraryItem* node, container_nodes_[0].values()) {
|
|
|
|
if (DividerKey(group_by_[0], node) == divider_key) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Remove the divider
|
|
|
|
int row = divider_nodes_[divider_key]->row;
|
|
|
|
beginRemoveRows(ItemToIndex(root_), row, row);
|
|
|
|
root_->Delete(row);
|
|
|
|
endRemoveRows();
|
|
|
|
divider_nodes_.remove(divider_key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-02 15:52:30 +02:00
|
|
|
QString LibraryModel::AlbumIconPixmapCacheKey(const QModelIndex& index) const {
|
|
|
|
QStringList path;
|
|
|
|
QModelIndex index_copy(index);
|
|
|
|
while (index_copy.isValid()) {
|
|
|
|
path.prepend(index_copy.data().toString());
|
|
|
|
index_copy = index_copy.parent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return "libraryart:" + path.join("/");
|
|
|
|
}
|
|
|
|
|
2011-11-13 01:31:27 +01:00
|
|
|
QVariant LibraryModel::AlbumIcon(const QModelIndex& index) {
|
2011-01-02 15:51:01 +01:00
|
|
|
LibraryItem* item = IndexToItem(index);
|
|
|
|
if (!item)
|
2012-06-10 18:15:32 +02:00
|
|
|
return no_cover_icon_;
|
2011-11-13 01:31:27 +01:00
|
|
|
|
2012-06-02 15:52:30 +02:00
|
|
|
// Check the cache for a pixmap we already loaded.
|
|
|
|
const QString cache_key = AlbumIconPixmapCacheKey(index);
|
|
|
|
QPixmap cached_pixmap;
|
|
|
|
if (QPixmapCache::find(cache_key, &cached_pixmap)) {
|
|
|
|
return cached_pixmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maybe we're loading a pixmap already?
|
|
|
|
if (pending_cache_keys_.contains(cache_key)) {
|
2012-06-10 18:15:32 +02:00
|
|
|
return no_cover_icon_;
|
2012-06-02 15:52:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No art is cached and we're not loading it already. Load art for the first
|
|
|
|
// Song in the album.
|
2011-01-02 15:51:01 +01:00
|
|
|
SongList songs = GetChildSongs(index);
|
|
|
|
if (!songs.isEmpty()) {
|
2012-02-13 21:44:04 +01:00
|
|
|
const quint64 id = app_->album_cover_loader()->LoadImageAsync(
|
|
|
|
cover_loader_options_, songs.first());
|
2012-06-02 15:52:30 +02:00
|
|
|
pending_art_[id] = ItemAndCacheKey(item, cache_key);
|
|
|
|
pending_cache_keys_.insert(cache_key);
|
2011-01-02 15:51:01 +01:00
|
|
|
}
|
2011-11-13 01:31:27 +01:00
|
|
|
|
2012-06-10 18:15:32 +02:00
|
|
|
return no_cover_icon_;
|
2011-01-02 15:51:01 +01:00
|
|
|
}
|
|
|
|
|
2011-11-13 01:31:27 +01:00
|
|
|
void LibraryModel::AlbumArtLoaded(quint64 id, const QImage& image) {
|
2012-06-02 15:52:30 +02:00
|
|
|
ItemAndCacheKey item_and_cache_key = pending_art_.take(id);
|
|
|
|
LibraryItem* item = item_and_cache_key.first;
|
|
|
|
const QString& cache_key = item_and_cache_key.second;
|
2011-11-13 01:31:27 +01:00
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
2012-06-02 15:52:30 +02:00
|
|
|
pending_cache_keys_.remove(cache_key);
|
|
|
|
|
|
|
|
// Insert this image in the cache.
|
2011-11-13 01:31:27 +01:00
|
|
|
if (image.isNull()) {
|
|
|
|
// Set the no_cover image so we don't continually try to load art.
|
2012-06-10 18:15:32 +02:00
|
|
|
QPixmapCache::insert(cache_key, no_cover_icon_);
|
2011-11-13 01:31:27 +01:00
|
|
|
} else {
|
2012-06-02 15:52:30 +02:00
|
|
|
QPixmapCache::insert(cache_key, QPixmap::fromImage(image));
|
2011-11-13 01:31:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const QModelIndex index = ItemToIndex(item);
|
|
|
|
emit dataChanged(index, index);
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
QVariant LibraryModel::data(const QModelIndex& index, int role) const {
|
|
|
|
const LibraryItem* item = IndexToItem(index);
|
2011-01-02 15:51:01 +01:00
|
|
|
|
|
|
|
// Handle a special case for returning album artwork instead of a generic CD icon.
|
|
|
|
// this is here instead of in the other data() function to let us use the
|
|
|
|
// QModelIndex& version of GetChildSongs, which satisfies const-ness, instead
|
|
|
|
// of the LibraryItem* version, which doesn't.
|
|
|
|
if (use_pretty_covers_) {
|
2011-11-13 01:31:27 +01:00
|
|
|
bool is_album_node = false;
|
2011-01-02 15:51:01 +01:00
|
|
|
if (role == Qt::DecorationRole && item->type == LibraryItem::Type_Container) {
|
|
|
|
GroupBy container_type = group_by_[item->container_level];
|
2011-11-13 01:31:27 +01:00
|
|
|
is_album_node = container_type == GroupBy_Album
|
2012-01-29 16:39:24 +01:00
|
|
|
|| container_type == GroupBy_YearAlbum;
|
2011-11-13 01:31:27 +01:00
|
|
|
}
|
|
|
|
if (is_album_node) {
|
|
|
|
// It has const behaviour some of the time - that's ok right?
|
|
|
|
return const_cast<LibraryModel*>(this)->AlbumIcon(index);
|
2011-01-02 15:51:01 +01:00
|
|
|
}
|
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
return data(item, role);
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant LibraryModel::data(const LibraryItem* item, int role) const {
|
|
|
|
GroupBy container_type =
|
|
|
|
item->type == LibraryItem::Type_Container ?
|
|
|
|
group_by_[item->container_level] : GroupBy_None;
|
|
|
|
|
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
return item->DisplayText();
|
|
|
|
|
|
|
|
case Qt::DecorationRole:
|
2010-11-20 16:29:42 +01:00
|
|
|
switch (item->type) {
|
2010-11-17 21:21:04 +01:00
|
|
|
case LibraryItem::Type_PlaylistContainer:
|
|
|
|
return playlists_dir_icon_;
|
2010-05-09 02:10:26 +02:00
|
|
|
case LibraryItem::Type_Container:
|
|
|
|
switch (container_type) {
|
|
|
|
case GroupBy_Album:
|
|
|
|
case GroupBy_YearAlbum:
|
|
|
|
return album_icon_;
|
|
|
|
case GroupBy_Artist:
|
2012-01-29 16:39:24 +01:00
|
|
|
case GroupBy_AlbumArtist:
|
2010-05-09 02:10:26 +02:00
|
|
|
return artist_icon_;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2010-11-20 16:29:42 +01:00
|
|
|
break;
|
2010-11-17 21:21:04 +01:00
|
|
|
case LibraryItem::Type_SmartPlaylist:
|
|
|
|
return playlist_icon_;
|
2010-05-09 02:10:26 +02:00
|
|
|
default:
|
|
|
|
break;
|
2010-11-20 16:29:42 +01:00
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Role_Type:
|
|
|
|
return item->type;
|
|
|
|
|
2010-11-24 22:34:54 +01:00
|
|
|
case Role_IsDivider:
|
|
|
|
return item->type == LibraryItem::Type_Divider;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
case Role_ContainerType:
|
|
|
|
return container_type;
|
|
|
|
|
|
|
|
case Role_Key:
|
|
|
|
return item->key;
|
|
|
|
|
|
|
|
case Role_Artist:
|
|
|
|
return item->metadata.artist();
|
|
|
|
|
2011-02-21 21:06:44 +01:00
|
|
|
case Role_Editable:
|
2011-03-17 20:52:15 +01:00
|
|
|
if (!item->lazy_loaded) {
|
|
|
|
const_cast<LibraryModel*>(this)->LazyPopulate(
|
|
|
|
const_cast<LibraryItem*>(item), true);
|
|
|
|
}
|
|
|
|
|
2011-02-21 21:06:44 +01:00
|
|
|
if(item->type == LibraryItem::Type_Container) {
|
|
|
|
// if we have even one non editable item as a child, we ourselves
|
|
|
|
// are not available for edit
|
|
|
|
if(!item->children.isEmpty()) {
|
|
|
|
foreach(LibraryItem* child, item->children) {
|
|
|
|
if(!data(child, role).toBool()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else if(item->type == LibraryItem::Type_Song) {
|
|
|
|
return item->metadata.IsEditable();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
case Role_SortText:
|
|
|
|
return item->SortText();
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2012-10-25 03:47:14 +02:00
|
|
|
bool LibraryModel::HasCompilations(const LibraryQuery query) {
|
|
|
|
LibraryQuery q = query;
|
|
|
|
q.AddCompilationRequirement(true);
|
|
|
|
q.SetLimit(1);
|
|
|
|
|
|
|
|
QMutexLocker l(backend_->db()->Mutex());
|
|
|
|
if (!backend_->ExecQuery(&q)) return false;
|
|
|
|
|
|
|
|
return q.Next();
|
|
|
|
}
|
|
|
|
|
2012-10-25 03:54:12 +02:00
|
|
|
struct LibraryModel::QueryResult LibraryModel::RunQuery(LibraryItem* parent,
|
|
|
|
bool signal) {
|
|
|
|
struct QueryResult result = { .rows = SqlRowList(), .create_va = false };
|
|
|
|
|
2010-11-27 21:09:00 +01:00
|
|
|
// Information about what we want the children to be
|
2012-10-22 04:06:25 +02:00
|
|
|
int child_level = parent == root_ ? 0 : parent->container_level + 1;
|
|
|
|
GroupBy child_type = child_level >= 3 ? GroupBy_None : group_by_[child_level];
|
2010-11-27 21:09:00 +01:00
|
|
|
|
|
|
|
// Initialise the query. child_type says what type of thing we want (artists,
|
|
|
|
// songs, etc.)
|
2012-10-22 04:06:25 +02:00
|
|
|
LibraryQuery q(query_options_);
|
2010-11-27 21:09:00 +01:00
|
|
|
InitQuery(child_type, &q);
|
|
|
|
|
2012-10-22 04:06:25 +02:00
|
|
|
// Walk up through the item's parents adding filters as necessary
|
|
|
|
LibraryItem* p = parent;
|
|
|
|
while (p && p->type == LibraryItem::Type_Container) {
|
|
|
|
FilterQuery(group_by_[p->container_level], p, &q);
|
|
|
|
p = p->parent;
|
|
|
|
}
|
|
|
|
|
2010-11-27 21:09:00 +01:00
|
|
|
// Top-level artists is special - we don't want compilation albums appearing
|
2012-10-22 04:06:25 +02:00
|
|
|
if (child_level == 0 && IsArtistGroupBy(child_type)) {
|
2012-10-25 03:47:14 +02:00
|
|
|
// Various artists?
|
|
|
|
if (show_various_artists_ && HasCompilations(q))
|
2012-10-25 03:54:12 +02:00
|
|
|
result.create_va = true;
|
2012-10-25 03:47:14 +02:00
|
|
|
|
2010-11-27 21:09:00 +01:00
|
|
|
q.AddCompilationRequirement(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute the query
|
|
|
|
QMutexLocker l(backend_->db()->Mutex());
|
|
|
|
if (!backend_->ExecQuery(&q))
|
2012-10-25 03:54:12 +02:00
|
|
|
return result;
|
2010-11-27 21:09:00 +01:00
|
|
|
|
|
|
|
while (q.Next()) {
|
2012-10-25 03:54:12 +02:00
|
|
|
result.rows << SqlRow(q);
|
2010-11-27 21:09:00 +01:00
|
|
|
}
|
2012-10-25 03:54:12 +02:00
|
|
|
return result;
|
2010-11-27 21:09:00 +01:00
|
|
|
}
|
|
|
|
|
2012-10-25 03:54:12 +02:00
|
|
|
void LibraryModel::PostQuery(LibraryItem* parent,
|
|
|
|
struct LibraryModel::QueryResult result,
|
|
|
|
bool signal ) {
|
2010-05-09 02:10:26 +02:00
|
|
|
// Information about what we want the children to be
|
2012-10-22 04:06:25 +02:00
|
|
|
int child_level = parent == root_ ? 0 : parent->container_level + 1;
|
2010-05-09 02:10:26 +02:00
|
|
|
GroupBy child_type = child_level >= 3 ? GroupBy_None : group_by_[child_level];
|
|
|
|
|
2012-10-25 03:54:12 +02:00
|
|
|
if (result.create_va)
|
|
|
|
CreateCompilationArtistNode(signal, parent);
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
// Step through the results
|
2012-10-25 03:54:12 +02:00
|
|
|
foreach (const SqlRow& row, result.rows) {
|
2010-05-09 02:10:26 +02:00
|
|
|
// Create the item - it will get inserted into the model here
|
|
|
|
LibraryItem* item =
|
2012-10-22 04:06:25 +02:00
|
|
|
ItemFromQuery(child_type, signal, child_level == 0, parent, row,
|
2010-11-27 21:09:00 +01:00
|
|
|
child_level);
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
// Save a pointer to it for later
|
|
|
|
if (child_type == GroupBy_None)
|
|
|
|
song_nodes_[item->metadata.id()] = item;
|
|
|
|
else
|
|
|
|
container_nodes_[child_level][item->key] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-22 04:06:25 +02:00
|
|
|
void LibraryModel::LazyPopulate(LibraryItem* parent, bool signal) {
|
|
|
|
if (parent->lazy_loaded)
|
|
|
|
return;
|
|
|
|
parent->lazy_loaded = true;
|
|
|
|
|
2012-10-25 03:54:12 +02:00
|
|
|
struct QueryResult result = RunQuery(parent, signal);
|
2012-10-22 04:06:25 +02:00
|
|
|
|
2012-10-25 03:54:12 +02:00
|
|
|
PostQuery(parent, result, signal);
|
2012-10-22 04:06:25 +02:00
|
|
|
}
|
|
|
|
|
2010-11-27 21:09:00 +01:00
|
|
|
void LibraryModel::ResetAsync() {
|
|
|
|
RootQueryFuture future = QtConcurrent::run(
|
2012-10-22 04:06:25 +02:00
|
|
|
this, &LibraryModel::RunQuery, root_, false);
|
2010-11-27 21:09:00 +01:00
|
|
|
RootQueryWatcher* watcher = new RootQueryWatcher(this);
|
|
|
|
watcher->setFuture(future);
|
|
|
|
|
|
|
|
connect(watcher, SIGNAL(finished()), SLOT(ResetAsyncQueryFinished()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::ResetAsyncQueryFinished() {
|
|
|
|
RootQueryWatcher* watcher = static_cast<RootQueryWatcher*>(sender());
|
2012-10-25 03:54:12 +02:00
|
|
|
const struct QueryResult result = watcher->result();
|
2010-11-27 21:09:00 +01:00
|
|
|
watcher->deleteLater();
|
|
|
|
|
|
|
|
BeginReset();
|
|
|
|
root_->lazy_loaded = true;
|
|
|
|
|
2012-10-25 03:54:12 +02:00
|
|
|
PostQuery(root_, result , false);
|
2010-11-27 21:09:00 +01:00
|
|
|
|
2011-02-26 15:27:57 +01:00
|
|
|
if (init_task_id_ != -1) {
|
2012-02-12 14:41:50 +01:00
|
|
|
app_->task_manager()->SetTaskFinished(init_task_id_);
|
2011-02-26 15:27:57 +01:00
|
|
|
init_task_id_ = -1;
|
|
|
|
}
|
|
|
|
|
2012-08-26 14:36:44 +02:00
|
|
|
endResetModel();
|
2010-11-27 21:09:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::BeginReset() {
|
2012-08-26 14:36:44 +02:00
|
|
|
beginResetModel();
|
2010-05-09 02:10:26 +02:00
|
|
|
delete root_;
|
|
|
|
song_nodes_.clear();
|
|
|
|
container_nodes_[0].clear();
|
|
|
|
container_nodes_[1].clear();
|
|
|
|
container_nodes_[2].clear();
|
|
|
|
divider_nodes_.clear();
|
2011-11-13 01:31:27 +01:00
|
|
|
pending_art_.clear();
|
2010-05-09 02:10:26 +02:00
|
|
|
compilation_artist_node_ = NULL;
|
2010-11-17 21:21:04 +01:00
|
|
|
smart_playlist_node_ = NULL;
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
root_ = new LibraryItem(this);
|
|
|
|
root_->lazy_loaded = false;
|
|
|
|
|
2010-11-17 21:21:04 +01:00
|
|
|
// Smart playlists?
|
2011-02-14 18:29:56 +01:00
|
|
|
if (show_smart_playlists_ && query_options_.filter().isEmpty())
|
2010-11-17 21:21:04 +01:00
|
|
|
CreateSmartPlaylists();
|
2010-11-27 21:09:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::Reset() {
|
|
|
|
BeginReset();
|
2010-11-17 21:21:04 +01:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
// Populate top level
|
|
|
|
LazyPopulate(root_, false);
|
|
|
|
|
2012-08-26 14:36:44 +02:00
|
|
|
endResetModel();
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::InitQuery(GroupBy type, LibraryQuery* q) {
|
|
|
|
// Say what type of thing we want to get back from the database.
|
|
|
|
switch (type) {
|
|
|
|
case GroupBy_Artist:
|
|
|
|
q->SetColumnSpec("DISTINCT artist");
|
|
|
|
break;
|
|
|
|
case GroupBy_Album:
|
|
|
|
q->SetColumnSpec("DISTINCT album");
|
|
|
|
break;
|
|
|
|
case GroupBy_Composer:
|
|
|
|
q->SetColumnSpec("DISTINCT composer");
|
|
|
|
break;
|
|
|
|
case GroupBy_YearAlbum:
|
|
|
|
q->SetColumnSpec("DISTINCT year, album");
|
|
|
|
break;
|
|
|
|
case GroupBy_Year:
|
|
|
|
q->SetColumnSpec("DISTINCT year");
|
|
|
|
break;
|
|
|
|
case GroupBy_Genre:
|
|
|
|
q->SetColumnSpec("DISTINCT genre");
|
|
|
|
break;
|
|
|
|
case GroupBy_AlbumArtist:
|
2011-11-28 06:58:27 +01:00
|
|
|
q->SetColumnSpec("DISTINCT effective_albumartist");
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
|
|
|
case GroupBy_None:
|
2010-06-20 18:30:10 +02:00
|
|
|
q->SetColumnSpec("%songs_table.ROWID, " + Song::kColumnSpec);
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
2010-07-31 16:14:03 +02:00
|
|
|
case GroupBy_FileType:
|
|
|
|
q->SetColumnSpec("DISTINCT filetype");
|
|
|
|
break;
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::FilterQuery(GroupBy type, LibraryItem* item, LibraryQuery* q) {
|
|
|
|
// Say how we want the query to be filtered. This is done once for each
|
|
|
|
// parent going up the tree.
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GroupBy_Artist:
|
|
|
|
if (item == compilation_artist_node_)
|
|
|
|
q->AddCompilationRequirement(true);
|
|
|
|
else {
|
|
|
|
if (item->container_level == 0) // Stupid hack
|
|
|
|
q->AddCompilationRequirement(false);
|
|
|
|
q->AddWhere("artist", item->key);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GroupBy_Album:
|
|
|
|
q->AddWhere("album", item->key);
|
|
|
|
break;
|
|
|
|
case GroupBy_YearAlbum:
|
|
|
|
q->AddWhere("year", item->metadata.year());
|
|
|
|
q->AddWhere("album", item->metadata.album());
|
|
|
|
break;
|
|
|
|
case GroupBy_Year:
|
|
|
|
q->AddWhere("year", item->key);
|
|
|
|
break;
|
|
|
|
case GroupBy_Composer:
|
|
|
|
q->AddWhere("composer", item->key);
|
|
|
|
break;
|
|
|
|
case GroupBy_Genre:
|
|
|
|
q->AddWhere("genre", item->key);
|
|
|
|
break;
|
|
|
|
case GroupBy_AlbumArtist:
|
2011-11-28 07:03:28 +01:00
|
|
|
if (item == compilation_artist_node_)
|
|
|
|
q->AddCompilationRequirement(true);
|
|
|
|
else {
|
|
|
|
if (item->container_level == 0) // Same stupid hack as above
|
|
|
|
q->AddCompilationRequirement(false);
|
|
|
|
q->AddWhere("effective_albumartist", item->key);
|
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
2010-07-31 16:14:03 +02:00
|
|
|
case GroupBy_FileType:
|
|
|
|
q->AddWhere("filetype", item->metadata.filetype());
|
|
|
|
break;
|
2010-05-09 02:10:26 +02:00
|
|
|
case GroupBy_None:
|
2011-06-14 17:33:51 +02:00
|
|
|
qLog(Error) << "Unknown GroupBy type" << type << "used in filter";
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LibraryItem* LibraryModel::InitItem(GroupBy type, bool signal, LibraryItem *parent,
|
|
|
|
int container_level) {
|
|
|
|
LibraryItem::Type item_type =
|
|
|
|
type == GroupBy_None ? LibraryItem::Type_Song :
|
|
|
|
LibraryItem::Type_Container;
|
|
|
|
|
|
|
|
if (signal)
|
|
|
|
beginInsertRows(ItemToIndex(parent),
|
|
|
|
parent->children.count(),parent->children.count());
|
|
|
|
|
|
|
|
// Initialise the item depending on what type it's meant to be
|
|
|
|
LibraryItem* item = new LibraryItem(item_type, parent);
|
|
|
|
item->container_level = container_level;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
LibraryItem* LibraryModel::ItemFromQuery(GroupBy type,
|
2010-11-27 21:09:00 +01:00
|
|
|
bool signal, bool create_divider,
|
|
|
|
LibraryItem* parent, const SqlRow& row,
|
|
|
|
int container_level) {
|
2010-05-09 02:10:26 +02:00
|
|
|
LibraryItem* item = InitItem(type, signal, parent, container_level);
|
|
|
|
int year = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GroupBy_Artist:
|
2010-11-27 21:09:00 +01:00
|
|
|
item->key = row.value(0).toString();
|
2010-05-09 02:10:26 +02:00
|
|
|
item->display_text = TextOrUnknown(item->key);
|
|
|
|
item->sort_text = SortTextForArtist(item->key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GroupBy_YearAlbum:
|
2010-11-27 21:09:00 +01:00
|
|
|
year = qMax(0, row.value(0).toInt());
|
|
|
|
item->metadata.set_year(row.value(0).toInt());
|
|
|
|
item->metadata.set_album(row.value(1).toString());
|
2010-05-09 02:10:26 +02:00
|
|
|
item->key = PrettyYearAlbum(year, item->metadata.album());
|
|
|
|
item->sort_text = SortTextForYear(year) + item->metadata.album();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GroupBy_Year:
|
2010-11-27 21:09:00 +01:00
|
|
|
year = qMax(0, row.value(0).toInt());
|
2010-05-09 02:10:26 +02:00
|
|
|
item->key = QString::number(year);
|
|
|
|
item->sort_text = SortTextForYear(year) + " ";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GroupBy_Composer:
|
|
|
|
case GroupBy_Genre:
|
|
|
|
case GroupBy_Album:
|
|
|
|
case GroupBy_AlbumArtist:
|
2010-11-27 21:09:00 +01:00
|
|
|
item->key = row.value(0).toString();
|
2010-05-09 02:10:26 +02:00
|
|
|
item->display_text = TextOrUnknown(item->key);
|
2010-06-16 16:52:01 +02:00
|
|
|
item->sort_text = SortTextForArtist(item->key);
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
|
|
|
|
2010-07-31 16:14:03 +02:00
|
|
|
case GroupBy_FileType:
|
2010-11-27 21:09:00 +01:00
|
|
|
item->metadata.set_filetype(Song::FileType(row.value(0).toInt()));
|
2010-07-31 16:14:03 +02:00
|
|
|
item->key = item->metadata.TextForFiletype();
|
|
|
|
break;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
case GroupBy_None:
|
2011-06-17 22:00:10 +02:00
|
|
|
item->metadata.InitFromQuery(row, true);
|
2010-05-09 02:10:26 +02:00
|
|
|
item->key = item->metadata.title();
|
2010-11-21 22:36:27 +01:00
|
|
|
item->display_text = item->metadata.TitleWithCompilationArtist();
|
2010-07-31 14:50:00 +02:00
|
|
|
item->sort_text = SortTextForSong(item->metadata);
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FinishItem(type, signal, create_divider, parent, item);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
LibraryItem* LibraryModel::ItemFromSong(GroupBy type,
|
|
|
|
bool signal, bool create_divider,
|
|
|
|
LibraryItem* parent, const Song& s,
|
|
|
|
int container_level) {
|
|
|
|
LibraryItem* item = InitItem(type, signal, parent, container_level);
|
|
|
|
int year = 0;
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case GroupBy_Artist:
|
|
|
|
item->key = s.artist();
|
|
|
|
item->display_text = TextOrUnknown(item->key);
|
|
|
|
item->sort_text = SortTextForArtist(item->key);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GroupBy_YearAlbum:
|
|
|
|
year = qMax(0, s.year());
|
|
|
|
item->metadata.set_year(year);
|
|
|
|
item->metadata.set_album(s.album());
|
|
|
|
item->key = PrettyYearAlbum(year, s.album());
|
|
|
|
item->sort_text = SortTextForYear(year) + s.album();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GroupBy_Year:
|
|
|
|
year = qMax(0, s.year());
|
|
|
|
item->key = QString::number(year);
|
|
|
|
item->sort_text = SortTextForYear(year) + " ";
|
|
|
|
break;
|
|
|
|
|
|
|
|
case GroupBy_Composer: item->key = s.composer();
|
|
|
|
case GroupBy_Genre: if (item->key.isNull()) item->key = s.genre();
|
|
|
|
case GroupBy_Album: if (item->key.isNull()) item->key = s.album();
|
2011-11-28 06:58:27 +01:00
|
|
|
case GroupBy_AlbumArtist: if (item->key.isNull()) item->key = s.effective_albumartist();
|
2010-05-09 02:10:26 +02:00
|
|
|
item->display_text = TextOrUnknown(item->key);
|
2010-06-16 16:52:01 +02:00
|
|
|
item->sort_text = SortTextForArtist(item->key);
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
|
|
|
|
2010-07-31 16:14:03 +02:00
|
|
|
case GroupBy_FileType:
|
|
|
|
item->metadata.set_filetype(s.filetype());
|
|
|
|
item->key = s.TextForFiletype();
|
|
|
|
break;
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
case GroupBy_None:
|
|
|
|
item->metadata = s;
|
|
|
|
item->key = s.title();
|
2010-11-21 22:36:27 +01:00
|
|
|
item->display_text = s.TitleWithCompilationArtist();
|
2010-07-31 14:50:00 +02:00
|
|
|
item->sort_text = SortTextForSong(s);
|
2010-05-09 02:10:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
FinishItem(type, signal, create_divider, parent, item);
|
2011-08-05 02:15:16 +02:00
|
|
|
if (s.url().scheme() == "cdda")
|
|
|
|
item->lazy_loaded = true;
|
2010-05-09 02:10:26 +02:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::FinishItem(GroupBy type,
|
|
|
|
bool signal, bool create_divider,
|
|
|
|
LibraryItem *parent, LibraryItem *item) {
|
|
|
|
if (type == GroupBy_None)
|
|
|
|
item->lazy_loaded = true;
|
|
|
|
|
|
|
|
if (signal)
|
|
|
|
endInsertRows();
|
|
|
|
|
|
|
|
// Create the divider entry if we're supposed to
|
2011-03-05 16:20:27 +01:00
|
|
|
if (create_divider && show_dividers_) {
|
2010-05-09 02:10:26 +02:00
|
|
|
QString divider_key = DividerKey(type, item);
|
2010-08-30 18:20:04 +02:00
|
|
|
item->sort_text.prepend(divider_key);
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
if (!divider_key.isEmpty() && !divider_nodes_.contains(divider_key)) {
|
|
|
|
if (signal)
|
|
|
|
beginInsertRows(ItemToIndex(parent), parent->children.count(),
|
|
|
|
parent->children.count());
|
|
|
|
|
|
|
|
LibraryItem* divider =
|
|
|
|
new LibraryItem(LibraryItem::Type_Divider, root_);
|
|
|
|
divider->key = divider_key;
|
|
|
|
divider->display_text = DividerDisplayText(type, divider_key);
|
|
|
|
divider->lazy_loaded = true;
|
|
|
|
|
|
|
|
divider_nodes_[divider_key] = divider;
|
|
|
|
|
|
|
|
if (signal)
|
|
|
|
endInsertRows();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
QString LibraryModel::TextOrUnknown(const QString& text) {
|
2010-05-09 02:10:26 +02:00
|
|
|
if (text.isEmpty()) {
|
|
|
|
return tr("Unknown");
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
QString LibraryModel::PrettyYearAlbum(int year, const QString& album) {
|
2010-05-09 02:10:26 +02:00
|
|
|
if (year <= 0)
|
|
|
|
return TextOrUnknown(album);
|
|
|
|
return QString::number(year) + " - " + TextOrUnknown(album);
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
QString LibraryModel::SortText(QString text) {
|
2010-05-09 02:10:26 +02:00
|
|
|
if (text.isEmpty()) {
|
|
|
|
text = " unknown";
|
|
|
|
} else {
|
|
|
|
text = text.toLower();
|
|
|
|
}
|
|
|
|
text = text.remove(QRegExp("[^\\w ]"));
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
QString LibraryModel::SortTextForArtist(QString artist) {
|
2010-05-09 02:10:26 +02:00
|
|
|
artist = SortText(artist);
|
|
|
|
|
|
|
|
if (artist.startsWith("the ")) {
|
|
|
|
artist = artist.right(artist.length() - 4) + ", the";
|
|
|
|
}
|
|
|
|
|
|
|
|
return artist;
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
QString LibraryModel::SortTextForYear(int year) {
|
2010-05-09 02:10:26 +02:00
|
|
|
QString str = QString::number(year);
|
|
|
|
return QString("0").repeated(qMax(0, 4 - str.length())) + str;
|
|
|
|
}
|
|
|
|
|
2012-06-04 19:18:37 +02:00
|
|
|
QString LibraryModel::SortTextForSong(const Song& song) {
|
2010-07-31 17:00:31 +02:00
|
|
|
QString ret = QString::number(qMax(0, song.disc()) * 1000 + qMax(0, song.track()));
|
2010-07-31 14:50:00 +02:00
|
|
|
ret.prepend(QString("0").repeated(6 - ret.length()));
|
2011-04-28 14:27:53 +02:00
|
|
|
ret.append(song.url().toString());
|
2010-07-31 14:50:00 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
Qt::ItemFlags LibraryModel::flags(const QModelIndex& index) const {
|
|
|
|
switch (IndexToItem(index)->type) {
|
|
|
|
case LibraryItem::Type_Song:
|
|
|
|
case LibraryItem::Type_Container:
|
2010-11-17 21:21:04 +01:00
|
|
|
case LibraryItem::Type_SmartPlaylist:
|
2010-05-09 02:10:26 +02:00
|
|
|
return Qt::ItemIsSelectable |
|
|
|
|
Qt::ItemIsEnabled |
|
|
|
|
Qt::ItemIsDragEnabled;
|
|
|
|
case LibraryItem::Type_Divider:
|
|
|
|
case LibraryItem::Type_Root:
|
2011-02-26 15:27:57 +01:00
|
|
|
case LibraryItem::Type_LoadingIndicator:
|
2010-05-09 02:10:26 +02:00
|
|
|
default:
|
|
|
|
return Qt::ItemIsEnabled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList LibraryModel::mimeTypes() const {
|
|
|
|
return QStringList() << "text/uri-list";
|
|
|
|
}
|
|
|
|
|
|
|
|
QMimeData* LibraryModel::mimeData(const QModelIndexList& indexes) const {
|
2010-11-17 21:21:04 +01:00
|
|
|
if (indexes.isEmpty())
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
// Special case: a smart playlist was dragged
|
|
|
|
if (IndexToItem(indexes.first())->type == LibraryItem::Type_SmartPlaylist) {
|
2010-11-18 21:19:33 +01:00
|
|
|
GeneratorPtr generator = CreateGenerator(indexes.first());
|
2010-11-17 21:21:04 +01:00
|
|
|
if (!generator)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
GeneratorMimeData* data = new GeneratorMimeData(generator);
|
|
|
|
data->setData(kSmartPlaylistsMimeType, QByteArray());
|
2011-02-16 19:29:35 +01:00
|
|
|
data->name_for_new_playlist_ = this->data(indexes.first()).toString();
|
2010-11-17 21:21:04 +01:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
SongMimeData* data = new SongMimeData;
|
|
|
|
QList<QUrl> urls;
|
|
|
|
QSet<int> song_ids;
|
|
|
|
|
2010-05-09 19:32:07 +02:00
|
|
|
data->backend = backend_;
|
2011-02-15 00:06:36 +01:00
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
foreach (const QModelIndex& index, indexes) {
|
|
|
|
GetChildSongs(IndexToItem(index), &urls, &data->songs, &song_ids);
|
|
|
|
}
|
|
|
|
|
|
|
|
data->setUrls(urls);
|
2011-03-25 20:16:12 +01:00
|
|
|
data->name_for_new_playlist_ = PlaylistManager::GetNameForNewPlaylist(data->songs);
|
2010-05-09 02:10:26 +02:00
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LibraryModel::CompareItems(const LibraryItem* a, const LibraryItem* b) const {
|
|
|
|
QVariant left(data(a, LibraryModel::Role_SortText));
|
|
|
|
QVariant right(data(b, LibraryModel::Role_SortText));
|
|
|
|
|
|
|
|
if (left.type() == QVariant::Int)
|
|
|
|
return left.toInt() < right.toInt();
|
|
|
|
return left.toString() < right.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::GetChildSongs(LibraryItem* item, QList<QUrl>* urls,
|
|
|
|
SongList* songs, QSet<int>* song_ids) const {
|
|
|
|
switch (item->type) {
|
|
|
|
case LibraryItem::Type_Container: {
|
|
|
|
const_cast<LibraryModel*>(this)->LazyPopulate(item);
|
|
|
|
|
|
|
|
QList<LibraryItem*> children = item->children;
|
|
|
|
qSort(children.begin(), children.end(), boost::bind(
|
|
|
|
&LibraryModel::CompareItems, this, _1, _2));
|
|
|
|
|
|
|
|
foreach (LibraryItem* child, children)
|
|
|
|
GetChildSongs(child, urls, songs, song_ids);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case LibraryItem::Type_Song:
|
2011-04-28 14:27:53 +02:00
|
|
|
urls->append(item->metadata.url());
|
2010-05-09 02:10:26 +02:00
|
|
|
if (!song_ids->contains(item->metadata.id())) {
|
|
|
|
songs->append(item->metadata);
|
|
|
|
song_ids->insert(item->metadata.id());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-09 17:49:05 +02:00
|
|
|
SongList LibraryModel::GetChildSongs(const QModelIndexList& indexes) const {
|
2010-05-09 02:10:26 +02:00
|
|
|
QList<QUrl> dontcare;
|
|
|
|
SongList ret;
|
|
|
|
QSet<int> song_ids;
|
|
|
|
|
2010-06-09 17:49:05 +02:00
|
|
|
foreach (const QModelIndex& index, indexes) {
|
|
|
|
GetChildSongs(IndexToItem(index), &dontcare, &ret, &song_ids);
|
|
|
|
}
|
2010-05-09 02:10:26 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-06-09 17:49:05 +02:00
|
|
|
SongList LibraryModel::GetChildSongs(const QModelIndex &index) const {
|
|
|
|
return GetChildSongs(QModelIndexList() << index);
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
void LibraryModel::SetFilterAge(int age) {
|
2011-02-06 14:18:18 +01:00
|
|
|
query_options_.set_max_age(age);
|
2010-11-27 21:09:00 +01:00
|
|
|
ResetAsync();
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::SetFilterText(const QString& text) {
|
2011-02-06 14:18:18 +01:00
|
|
|
query_options_.set_filter(text);
|
2011-01-30 22:00:49 +01:00
|
|
|
ResetAsync();
|
|
|
|
}
|
|
|
|
|
2011-02-06 14:18:18 +01:00
|
|
|
void LibraryModel::SetFilterQueryMode(QueryOptions::QueryMode query_mode) {
|
|
|
|
query_options_.set_query_mode(query_mode);
|
2010-11-27 21:09:00 +01:00
|
|
|
ResetAsync();
|
2010-05-09 02:10:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LibraryModel::canFetchMore(const QModelIndex &parent) const {
|
|
|
|
if (!parent.isValid())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
LibraryItem* item = IndexToItem(parent);
|
|
|
|
return !item->lazy_loaded;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::SetGroupBy(const Grouping& g) {
|
|
|
|
group_by_ = g;
|
|
|
|
|
2010-11-27 21:09:00 +01:00
|
|
|
ResetAsync();
|
2010-05-09 02:10:26 +02:00
|
|
|
emit GroupingChanged(g);
|
|
|
|
}
|
|
|
|
|
|
|
|
const LibraryModel::GroupBy& LibraryModel::Grouping::operator [](int i) const {
|
|
|
|
switch (i) {
|
|
|
|
case 0: return first;
|
|
|
|
case 1: return second;
|
|
|
|
case 2: return third;
|
|
|
|
}
|
2011-06-14 17:33:51 +02:00
|
|
|
qLog(Error) << "LibraryModel::Grouping[] index out of range" << i;
|
2010-05-09 02:10:26 +02:00
|
|
|
return first;
|
|
|
|
}
|
|
|
|
|
|
|
|
LibraryModel::GroupBy& LibraryModel::Grouping::operator [](int i) {
|
|
|
|
switch (i) {
|
|
|
|
case 0: return first;
|
|
|
|
case 1: return second;
|
|
|
|
case 2: return third;
|
|
|
|
}
|
2011-06-14 17:33:51 +02:00
|
|
|
qLog(Error) << "LibraryModel::Grouping[] index out of range" << i;
|
2010-05-09 02:10:26 +02:00
|
|
|
return first;
|
|
|
|
}
|
2010-11-17 21:21:04 +01:00
|
|
|
|
|
|
|
void LibraryModel::CreateSmartPlaylists() {
|
|
|
|
smart_playlist_node_ = new LibraryItem(LibraryItem::Type_PlaylistContainer, root_);
|
|
|
|
smart_playlist_node_->container_level = 0;
|
2010-11-27 20:11:36 +01:00
|
|
|
smart_playlist_node_->sort_text = "\0";
|
2010-11-17 21:21:04 +01:00
|
|
|
smart_playlist_node_->key = tr("Smart playlists");
|
2011-06-14 17:27:07 +02:00
|
|
|
smart_playlist_node_->lazy_loaded = true;
|
2010-11-17 21:21:04 +01:00
|
|
|
|
|
|
|
QSettings s;
|
2010-11-19 00:08:37 +01:00
|
|
|
s.beginGroup(kSmartPlaylistsSettingsGroup);
|
2010-11-27 18:52:08 +01:00
|
|
|
int version = s.value(backend_->songs_table() + "_version", 0).toInt();
|
2010-11-17 21:21:04 +01:00
|
|
|
|
2010-11-27 18:52:08 +01:00
|
|
|
// How many defaults do we have to write?
|
|
|
|
int unwritten_defaults = 0;
|
|
|
|
for (int i=version; i < default_smart_playlists_.count() ; ++i) {
|
|
|
|
unwritten_defaults += default_smart_playlists_[i].count();
|
2010-11-20 19:49:54 +01:00
|
|
|
}
|
|
|
|
|
2010-11-27 18:52:08 +01:00
|
|
|
// Save the defaults if there are any unwritten ones
|
|
|
|
if (unwritten_defaults) {
|
|
|
|
// How many items are stored already?
|
|
|
|
int playlist_index = s.beginReadArray(backend_->songs_table());
|
2010-11-20 13:05:51 +01:00
|
|
|
s.endArray();
|
2010-11-20 19:49:54 +01:00
|
|
|
|
2010-11-27 18:52:08 +01:00
|
|
|
// Append the new ones
|
|
|
|
s.beginWriteArray(backend_->songs_table(), playlist_index + unwritten_defaults);
|
|
|
|
for (; version < default_smart_playlists_.count() ; ++version) {
|
|
|
|
foreach (smart_playlists::GeneratorPtr gen, default_smart_playlists_[version]) {
|
|
|
|
SaveGenerator(&s, playlist_index++, gen);
|
|
|
|
}
|
|
|
|
}
|
2010-11-20 19:49:54 +01:00
|
|
|
s.endArray();
|
|
|
|
}
|
|
|
|
|
2010-11-27 18:52:08 +01:00
|
|
|
s.setValue(backend_->songs_table() + "_version", version);
|
2010-11-17 21:21:04 +01:00
|
|
|
|
2010-11-27 18:52:08 +01:00
|
|
|
const int count = s.beginReadArray(backend_->songs_table());
|
2010-11-17 21:21:04 +01:00
|
|
|
for (int i=0 ; i<count ; ++i) {
|
|
|
|
s.setArrayIndex(i);
|
2010-11-19 00:08:37 +01:00
|
|
|
ItemFromSmartPlaylist(s, false);
|
2010-11-17 21:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-19 00:08:37 +01:00
|
|
|
void LibraryModel::ItemFromSmartPlaylist(const QSettings& s, bool notify) const {
|
|
|
|
LibraryItem* item = new LibraryItem(LibraryItem::Type_SmartPlaylist,
|
|
|
|
notify ? NULL : smart_playlist_node_);
|
2011-06-22 22:41:48 +02:00
|
|
|
item->display_text = tr(qPrintable(s.value("name").toString()));
|
2010-11-19 00:08:37 +01:00
|
|
|
item->sort_text = item->display_text;
|
|
|
|
item->key = s.value("type").toString();
|
|
|
|
item->smart_playlist_data = s.value("data").toByteArray();
|
|
|
|
item->lazy_loaded = true;
|
|
|
|
|
|
|
|
if (notify)
|
|
|
|
item->InsertNotify(smart_playlist_node_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::AddGenerator(GeneratorPtr gen) {
|
|
|
|
QSettings s;
|
2010-11-20 13:27:19 +01:00
|
|
|
s.beginGroup(kSmartPlaylistsSettingsGroup);
|
2010-11-19 00:08:37 +01:00
|
|
|
|
|
|
|
// Count the existing items
|
2010-11-27 18:52:08 +01:00
|
|
|
const int count = s.beginReadArray(backend_->songs_table());
|
2010-11-19 00:08:37 +01:00
|
|
|
s.endArray();
|
|
|
|
|
|
|
|
// Add this one to the end
|
2010-11-27 18:52:08 +01:00
|
|
|
s.beginWriteArray(backend_->songs_table(), count + 1);
|
2010-11-19 00:08:37 +01:00
|
|
|
SaveGenerator(&s, count, gen);
|
|
|
|
|
|
|
|
// Add it to the model
|
|
|
|
ItemFromSmartPlaylist(s, true);
|
|
|
|
|
|
|
|
s.endArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::UpdateGenerator(const QModelIndex& index, GeneratorPtr gen) {
|
|
|
|
if (index.parent() != ItemToIndex(smart_playlist_node_))
|
|
|
|
return;
|
|
|
|
LibraryItem* item = IndexToItem(index);
|
|
|
|
if (!item)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Update the config
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSmartPlaylistsSettingsGroup);
|
|
|
|
|
|
|
|
// Count the existing items
|
2010-11-27 18:52:08 +01:00
|
|
|
const int count = s.beginReadArray(backend_->songs_table());
|
2010-11-19 00:08:37 +01:00
|
|
|
s.endArray();
|
|
|
|
|
2010-11-27 18:52:08 +01:00
|
|
|
s.beginWriteArray(backend_->songs_table(), count);
|
2010-11-19 00:08:37 +01:00
|
|
|
SaveGenerator(&s, index.row(), gen);
|
|
|
|
|
|
|
|
// Update the text of the item
|
|
|
|
item->display_text = gen->name();
|
|
|
|
item->sort_text = item->display_text;
|
|
|
|
item->key = gen->type();
|
|
|
|
item->smart_playlist_data = gen->Save();
|
|
|
|
item->ChangedNotify();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LibraryModel::DeleteGenerator(const QModelIndex& index) {
|
2010-11-20 13:27:19 +01:00
|
|
|
if (index.parent() != ItemToIndex(smart_playlist_node_))
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove the item from the tree
|
|
|
|
smart_playlist_node_->DeleteNotify(index.row());
|
|
|
|
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSmartPlaylistsSettingsGroup);
|
|
|
|
|
|
|
|
// Rewrite all the items to the settings
|
2010-11-27 18:52:08 +01:00
|
|
|
s.beginWriteArray(backend_->songs_table(), smart_playlist_node_->children.count());
|
2010-11-20 13:27:19 +01:00
|
|
|
int i = 0;
|
|
|
|
foreach (LibraryItem* item, smart_playlist_node_->children) {
|
2010-11-20 16:47:44 +01:00
|
|
|
s.setArrayIndex(i++);
|
2010-11-20 13:27:19 +01:00
|
|
|
s.setValue("name", item->display_text);
|
|
|
|
s.setValue("type", item->key);
|
|
|
|
s.setValue("data", item->smart_playlist_data);
|
|
|
|
}
|
|
|
|
s.endArray();
|
2010-11-19 00:08:37 +01:00
|
|
|
}
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
void LibraryModel::SaveGenerator(QSettings* s, int i, GeneratorPtr generator) const {
|
2010-11-17 21:21:04 +01:00
|
|
|
s->setArrayIndex(i);
|
|
|
|
s->setValue("name", generator->name());
|
|
|
|
s->setValue("type", generator->type());
|
|
|
|
s->setValue("data", generator->Save());
|
|
|
|
}
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
GeneratorPtr LibraryModel::CreateGenerator(const QModelIndex& index) const {
|
|
|
|
GeneratorPtr ret;
|
2010-11-17 21:21:04 +01:00
|
|
|
|
|
|
|
const LibraryItem* item = IndexToItem(index);
|
|
|
|
if (!item || item->type != LibraryItem::Type_SmartPlaylist)
|
|
|
|
return ret;
|
|
|
|
|
2010-11-18 21:19:33 +01:00
|
|
|
ret = Generator::Create(item->key);
|
2010-11-17 21:21:04 +01:00
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret->set_name(item->display_text);
|
|
|
|
ret->set_library(backend());
|
|
|
|
ret->Load(item->smart_playlist_data);
|
|
|
|
return ret;
|
|
|
|
}
|
2010-11-27 21:20:26 +01:00
|
|
|
|
|
|
|
void LibraryModel::TotalSongCountUpdatedSlot(int count) {
|
|
|
|
total_song_count_ = count;
|
|
|
|
emit TotalSongCountUpdated(count);
|
|
|
|
}
|
2011-11-13 01:31:27 +01:00
|
|
|
|
|
|
|
|