2010-03-23 23:11:46 +00:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2009-12-24 19:16:07 +00:00
|
|
|
#ifndef LIBRARY_H
|
|
|
|
#define LIBRARY_H
|
|
|
|
|
|
|
|
#include <QAbstractItemModel>
|
|
|
|
#include <QIcon>
|
|
|
|
|
|
|
|
#include "backgroundthread.h"
|
|
|
|
#include "librarybackend.h"
|
|
|
|
#include "librarywatcher.h"
|
|
|
|
#include "libraryquery.h"
|
2010-04-04 17:50:11 +00:00
|
|
|
#include "engines/engine_fwd.h"
|
2009-12-24 19:16:07 +00:00
|
|
|
#include "song.h"
|
2009-12-26 15:13:38 +00:00
|
|
|
#include "libraryitem.h"
|
|
|
|
#include "simpletreemodel.h"
|
2009-12-24 19:16:07 +00:00
|
|
|
|
2010-03-23 17:26:54 +00:00
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
|
2010-02-03 16:17:04 +00:00
|
|
|
class LibraryDirectoryModel;
|
2009-12-24 19:16:07 +00:00
|
|
|
|
2009-12-26 15:13:38 +00:00
|
|
|
class Library : public SimpleTreeModel<LibraryItem> {
|
2009-12-24 19:16:07 +00:00
|
|
|
Q_OBJECT
|
2010-03-31 15:18:39 +00:00
|
|
|
Q_ENUMS(GroupBy);
|
2009-12-24 19:16:07 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
Library(EngineBase* engine, QObject* parent = 0);
|
|
|
|
~Library();
|
|
|
|
|
|
|
|
enum {
|
|
|
|
Role_Type = Qt::UserRole + 1,
|
2010-03-31 00:30:57 +00:00
|
|
|
Role_ContainerType,
|
2009-12-24 19:16:07 +00:00
|
|
|
Role_SortText,
|
|
|
|
Role_Key,
|
2010-03-20 23:59:39 +00:00
|
|
|
Role_Artist,
|
2009-12-24 19:16:07 +00:00
|
|
|
};
|
|
|
|
|
2010-03-31 15:18:39 +00:00
|
|
|
// These values get saved in QSettings - don't change them
|
|
|
|
enum GroupBy {
|
|
|
|
GroupBy_None = 0,
|
|
|
|
GroupBy_Artist = 1,
|
|
|
|
GroupBy_Album = 2,
|
|
|
|
GroupBy_YearAlbum = 3,
|
|
|
|
GroupBy_Year = 4,
|
|
|
|
GroupBy_Composer = 5,
|
|
|
|
GroupBy_Genre = 6,
|
2010-04-11 22:21:21 +00:00
|
|
|
GroupBy_AlbumArtist = 7,
|
2010-03-31 15:18:39 +00:00
|
|
|
};
|
|
|
|
|
2010-03-31 23:11:45 +00:00
|
|
|
struct Grouping {
|
2010-04-01 17:20:31 +00:00
|
|
|
Grouping(GroupBy f = GroupBy_None,
|
|
|
|
GroupBy s = GroupBy_None,
|
|
|
|
GroupBy t = GroupBy_None)
|
|
|
|
: first(f), second(s), third(t) {}
|
2010-03-31 23:11:45 +00:00
|
|
|
|
|
|
|
GroupBy first;
|
|
|
|
GroupBy second;
|
|
|
|
GroupBy third;
|
|
|
|
|
|
|
|
const GroupBy& operator [](int i) const;
|
|
|
|
GroupBy& operator [](int i);
|
2010-04-01 17:20:31 +00:00
|
|
|
bool operator ==(const Grouping& other) const {
|
|
|
|
return first == other.first &&
|
|
|
|
second == other.second &&
|
|
|
|
third == other.third;
|
|
|
|
}
|
2010-03-31 23:11:45 +00:00
|
|
|
};
|
2010-03-31 15:18:39 +00:00
|
|
|
|
2010-03-23 17:26:54 +00:00
|
|
|
// Useful for tests. The library takes ownership.
|
|
|
|
void set_backend_factory(BackgroundThreadFactory<LibraryBackendInterface>* factory);
|
|
|
|
void set_watcher_factory(BackgroundThreadFactory<LibraryWatcher>* factory);
|
|
|
|
|
|
|
|
void Init();
|
2009-12-24 19:16:07 +00:00
|
|
|
void StartThreads();
|
|
|
|
|
2010-02-03 16:17:04 +00:00
|
|
|
LibraryDirectoryModel* GetDirectoryModel() const { return dir_model_; }
|
2010-03-23 17:26:54 +00:00
|
|
|
boost::shared_ptr<LibraryBackendInterface> GetBackend() const { return backend_->Worker(); }
|
2010-02-03 16:17:04 +00:00
|
|
|
|
2009-12-24 20:27:32 +00:00
|
|
|
// Get information about the library
|
2010-04-08 22:14:43 +00:00
|
|
|
void GetChildSongs(LibraryItem* item, QList<QUrl>* urls, SongList* songs,
|
|
|
|
QSet<int>* song_ids) const;
|
2009-12-24 19:16:07 +00:00
|
|
|
SongList GetChildSongs(const QModelIndex& index) const;
|
|
|
|
|
|
|
|
// QAbstractItemModel
|
|
|
|
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
|
|
|
|
Qt::ItemFlags flags(const QModelIndex& index) const;
|
|
|
|
QStringList mimeTypes() const;
|
|
|
|
QMimeData* mimeData(const QModelIndexList& indexes) const;
|
2010-01-15 16:36:34 +00:00
|
|
|
bool canFetchMore(const QModelIndex &parent) const;
|
2009-12-24 19:16:07 +00:00
|
|
|
|
|
|
|
signals:
|
|
|
|
void Error(const QString& message);
|
|
|
|
void TotalSongCountUpdated(int count);
|
2010-03-31 23:11:45 +00:00
|
|
|
void GroupingChanged(const Library::Grouping& g);
|
2009-12-24 19:16:07 +00:00
|
|
|
|
2010-01-16 17:52:51 +00:00
|
|
|
void ScanStarted();
|
|
|
|
void ScanFinished();
|
|
|
|
|
2010-03-23 17:26:54 +00:00
|
|
|
void BackendReady(boost::shared_ptr<LibraryBackendInterface> backend);
|
2010-02-28 18:04:50 +00:00
|
|
|
|
2009-12-24 19:16:07 +00:00
|
|
|
public slots:
|
|
|
|
void SetFilterAge(int age);
|
|
|
|
void SetFilterText(const QString& text);
|
2010-03-31 23:11:45 +00:00
|
|
|
void SetGroupBy(const Library::Grouping& g);
|
2009-12-24 19:16:07 +00:00
|
|
|
|
2009-12-26 15:13:38 +00:00
|
|
|
protected:
|
2010-03-31 00:30:57 +00:00
|
|
|
void LazyPopulate(LibraryItem* item) { LazyPopulate(item, false); }
|
|
|
|
void LazyPopulate(LibraryItem* item, bool signal);
|
2009-12-26 15:13:38 +00:00
|
|
|
|
2009-12-24 19:16:07 +00:00
|
|
|
private slots:
|
|
|
|
// From LibraryBackend
|
|
|
|
void BackendInitialised();
|
|
|
|
void SongsDiscovered(const SongList& songs);
|
|
|
|
void SongsDeleted(const SongList& songs);
|
|
|
|
void Reset();
|
|
|
|
|
|
|
|
// From LibraryWatcher
|
|
|
|
void WatcherInitialised();
|
|
|
|
|
|
|
|
private:
|
|
|
|
void Initialise();
|
|
|
|
|
2010-03-31 19:55:32 +00:00
|
|
|
// Functions for working with queries and creating items.
|
|
|
|
// When the model is reset or when a node is lazy-loaded the Library
|
|
|
|
// constructs a database query to populate the items. Filters are added
|
|
|
|
// for each parent item, restricting the songs returned to a particular
|
|
|
|
// album or artist for example.
|
2010-03-31 15:18:39 +00:00
|
|
|
void InitQuery(GroupBy type, LibraryQuery* q);
|
2010-04-01 16:59:32 +00:00
|
|
|
void FilterQuery(GroupBy type, LibraryItem* item, LibraryQuery* q);
|
2010-03-31 19:55:32 +00:00
|
|
|
|
|
|
|
// Items can be created either from a query that's been run to populate a
|
|
|
|
// node, or by a spontaneous SongsDiscovered emission from the backend.
|
2010-03-31 15:18:39 +00:00
|
|
|
LibraryItem* ItemFromQuery(GroupBy type, bool signal, bool create_divider,
|
2010-04-01 16:59:32 +00:00
|
|
|
LibraryItem* parent, const LibraryQuery& q,
|
|
|
|
int container_level);
|
2010-03-31 15:18:39 +00:00
|
|
|
LibraryItem* ItemFromSong(GroupBy type, bool signal, bool create_divider,
|
2010-04-01 16:59:32 +00:00
|
|
|
LibraryItem* parent, const Song& s,
|
|
|
|
int container_level);
|
2010-03-31 19:55:32 +00:00
|
|
|
|
|
|
|
// The "Various Artists" node is an annoying special case.
|
2010-03-31 00:30:57 +00:00
|
|
|
LibraryItem* CreateCompilationArtistNode(bool signal, LibraryItem* parent);
|
|
|
|
|
|
|
|
// Helpers for ItemFromQuery and ItemFromSong
|
2010-04-01 16:59:32 +00:00
|
|
|
LibraryItem* InitItem(GroupBy type, bool signal, LibraryItem* parent,
|
|
|
|
int container_level);
|
2010-03-31 15:18:39 +00:00
|
|
|
void FinishItem(GroupBy type, bool signal, bool create_divider,
|
2010-03-31 00:30:57 +00:00
|
|
|
LibraryItem* parent, LibraryItem* item);
|
|
|
|
|
|
|
|
// Functions for manipulating text
|
|
|
|
QString TextOrUnknown(const QString& text) const;
|
|
|
|
QString PrettyYearAlbum(int year, const QString& album) const;
|
|
|
|
|
|
|
|
QString SortText(QString text) const;
|
2009-12-24 19:16:07 +00:00
|
|
|
QString SortTextForArtist(QString artist) const;
|
2010-03-31 00:30:57 +00:00
|
|
|
QString SortTextForYear(int year) const;
|
2009-12-24 19:16:07 +00:00
|
|
|
|
2010-03-31 15:18:39 +00:00
|
|
|
QString DividerKey(GroupBy type, LibraryItem* item) const;
|
|
|
|
QString DividerDisplayText(GroupBy type, const QString& key) const;
|
2009-12-24 19:16:07 +00:00
|
|
|
|
2010-03-31 00:30:57 +00:00
|
|
|
// Helpers
|
|
|
|
QVariant data(const LibraryItem* item, int role) const;
|
2009-12-24 19:16:07 +00:00
|
|
|
bool CompareItems(const LibraryItem* a, const LibraryItem* b) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
EngineBase* engine_;
|
2010-03-23 17:26:54 +00:00
|
|
|
boost::scoped_ptr<BackgroundThreadFactory<LibraryBackendInterface> > backend_factory_;
|
|
|
|
boost::scoped_ptr<BackgroundThreadFactory<LibraryWatcher> > watcher_factory_;
|
|
|
|
BackgroundThread<LibraryBackendInterface>* backend_;
|
2009-12-24 19:16:07 +00:00
|
|
|
BackgroundThread<LibraryWatcher>* watcher_;
|
2010-02-03 16:17:04 +00:00
|
|
|
LibraryDirectoryModel* dir_model_;
|
2009-12-24 19:16:07 +00:00
|
|
|
|
|
|
|
int waiting_for_threads_;
|
|
|
|
|
|
|
|
QueryOptions query_options_;
|
2010-03-31 23:11:45 +00:00
|
|
|
Grouping group_by_;
|
2009-12-24 19:16:07 +00:00
|
|
|
|
2010-03-31 00:30:57 +00:00
|
|
|
// Keyed on database ID
|
2009-12-24 19:16:07 +00:00
|
|
|
QMap<int, LibraryItem*> song_nodes_;
|
2010-03-31 00:30:57 +00:00
|
|
|
|
|
|
|
// Keyed on whatever the key is for that level - artist, album, year, etc.
|
2010-03-31 23:11:45 +00:00
|
|
|
QMap<QString, LibraryItem*> container_nodes_[3];
|
2010-03-31 00:30:57 +00:00
|
|
|
|
|
|
|
// Keyed on a letter, a year, a century, etc.
|
|
|
|
QMap<QString, LibraryItem*> divider_nodes_;
|
|
|
|
|
|
|
|
// Only applies if the first level is "artist"
|
2009-12-24 19:16:07 +00:00
|
|
|
LibraryItem* compilation_artist_node_;
|
|
|
|
|
|
|
|
QIcon artist_icon_;
|
|
|
|
QIcon album_icon_;
|
2010-02-28 22:07:59 +00:00
|
|
|
QIcon no_cover_icon_;
|
2009-12-24 19:16:07 +00:00
|
|
|
};
|
|
|
|
|
2010-04-01 17:20:31 +00:00
|
|
|
Q_DECLARE_METATYPE(Library::Grouping);
|
|
|
|
|
2009-12-24 19:16:07 +00:00
|
|
|
#endif // LIBRARY_H
|