PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 9ms RepoModel.GetById 32ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseImpl.h

http://github.com/tomahawk-player/tomahawk
C Header | 120 lines | 74 code | 27 blank | 19 comment | 0 complexity | 54d15f46d61c56e66f4bbab8f1543823 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *   Copyright 2014,      Teo Mrnjavac <teo@kde.org>
  6 *
  7 *   Tomahawk is free software: you can redistribute it and/or modify
  8 *   it under the terms of the GNU General Public License as published by
  9 *   the Free Software Foundation, either version 3 of the License, or
 10 *   (at your option) any later version.
 11 *
 12 *   Tomahawk is distributed in the hope that it will be useful,
 13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 *   GNU General Public License for more details.
 16 *
 17 *   You should have received a copy of the GNU General Public License
 18 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 19 */
 20
 21#ifndef DATABASEIMPL_H
 22#define DATABASEIMPL_H
 23
 24#include <QObject>
 25#include <QList>
 26#include <QMutex>
 27#include <QPair>
 28#include <QVariant>
 29#include <QVariantMap>
 30#include <QSqlDatabase>
 31#include <QSqlError>
 32#include <QSqlQuery>
 33#include <QHash>
 34#include <QThread>
 35
 36#include "DllMacro.h"
 37#include "TomahawkSqlQuery.h"
 38#include "Typedefs.h"
 39
 40
 41namespace Tomahawk
 42{
 43
 44class Database;
 45class DatabaseFuzzyIndex;
 46
 47class DLLEXPORT DatabaseImpl : public QObject
 48{
 49Q_OBJECT
 50
 51friend class DatabaseFuzzyIndex;
 52friend class DatabaseCommand_UpdateSearchIndex;
 53
 54public:
 55    DatabaseImpl( const QString& dbname );
 56    ~DatabaseImpl();
 57
 58    DatabaseImpl* clone() const;
 59
 60    TomahawkSqlQuery newquery();
 61    QSqlDatabase& database();
 62
 63    int artistId( const QString& name_orig, bool autoCreate ); //also for composers!
 64    int trackId( int artistid, const QString& name_orig, bool autoCreate );
 65    int albumId( int artistid, const QString& name_orig, bool autoCreate );
 66
 67    QList< QPair<int, float> > search( const Tomahawk::query_ptr& query, uint limit = 0 );
 68    QList< QPair<int, float> > searchAlbum( const Tomahawk::query_ptr& query, uint limit = 0 );
 69    QList< int > getTrackFids( int tid );
 70
 71    static QString sortname( const QString& str, bool replaceArticle = false );
 72
 73    QVariantMap artist( int id );
 74    QVariantMap album( int id );
 75    QVariantMap track( int id );
 76    Tomahawk::result_ptr file( int fid );
 77    Tomahawk::result_ptr resultFromHint( const Tomahawk::query_ptr& query );
 78
 79    static bool scorepairSorter( const QPair<int,float>& left, const QPair<int,float>& right )
 80    {
 81        return left.second > right.second;
 82    }
 83
 84    QString dbid() const { return m_dbid; }
 85
 86    void loadIndex();
 87
 88signals:
 89    void indexStarted();
 90    void indexReady();
 91
 92    void schemaUpdateStarted();
 93    void schemaUpdateStatus( const QString& message );
 94    void schemaUpdateDone();
 95
 96private:
 97    DatabaseImpl( const QString& dbname, bool internal );
 98    void setFuzzyIndex( DatabaseFuzzyIndex* fi ) { m_fuzzyIndex = fi; }
 99    void setDatabaseID( const QString& dbid ) { m_dbid = dbid; }
100
101    void init();
102    bool openDatabase( const QString& dbname, bool checkSchema = true );
103    bool updateSchema( int oldVersion );
104    void dumpDatabase();
105    QString cleanSql( const QString& sql );
106
107    bool m_ready;
108    QSqlDatabase m_db;
109
110    QString m_lastart, m_lastalb, m_lasttrk;
111    int m_lastartid, m_lastalbid, m_lasttrkid;
112
113    QString m_dbid;
114    Tomahawk::DatabaseFuzzyIndex* m_fuzzyIndex;
115    mutable QMutex m_mutex;
116};
117
118}
119
120#endif // DATABASEIMPL_H