summaryrefslogtreecommitdiffstats
path: root/archivemodel.h
blob: 7e77642720c2aec5fc25a770d38d9cecab6a51f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
  This program 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
  2 of the License, or (at your option) any later version.
*/

#ifndef ARCHIVEMODEL_H
#define ARCHIVEMODEL_H

#include <QSqlDatabase>
#include <QThread>
#include <QDataStream>
#include <QMutex>
#include <QSet>

#include "smtreemodel.h"

class ArchiveCollector;

class ArchiveModel : public SmTreeModel {
    Q_OBJECT
    public:
        enum CustomRoles { NameRole = Qt::UserRole + 1, GenericIdRole = Qt::UserRole + 2, SeriesPartIdRole = Qt::UserRole + 3, SeriesPartRole = Qt::UserRole + 4, TypeRole = Qt::UserRole + 5, FavoriteRole = Qt::UserRole + 6, SubtitleRole = Qt::UserRole + 7, CountRole = Qt::UserRole + 8 };
        enum Fields { Name = 0, GenericId = 1, SeriesPartId = 2, SeriesPart = 3, Type = 4, Favorite = 5, Subtitle = 6, Count = 7 };
        enum Order { SeriesName, Actor, Genre, NoOrder };
        enum { NumFields = 8 };
        enum NodeType { SeriesNode, SeriesPartNode, GenreNode, ActorNode };
        explicit ArchiveModel(const QStringList &headers, QObject *parent = 0);
        virtual ~ArchiveModel();
        const QStringList availableOrders() const;
        const QHash<QString, int> availableOrdersHash() const { return mAvailableOrders; }
        virtual QVariant data(const QModelIndex &index, int role) const;
        virtual bool setData(const QModelIndex &idx, const QVariant &value, int role);
        virtual bool matchRecursive(const QModelIndex &parent, const QRegExp &regex, int column = 0) const;
        virtual bool removeNode(const QModelIndex &idx);
        QStringList indexToPath(const QModelIndex &idx) const;
        QModelIndexList pathToIndex(const QStringList &path) const;
        QSet<int> seriesPartIds(const QModelIndex &idx) const;

    signals:
        void needRefresh();
        void databaseError(const QString &error);
        void message(const QString &msg);

    public slots:
        void setOrder(int order);
        void setOrder(const QString &order);
        void refresh();

    private slots:
        void collectorFinished(QObject *thread);

    private:
        bool checkParents(const SmTreeItem *item, const QRegExp &regex, int column) const;
        void emitDatabaseError(const QSqlError &e);
        void writeCache(int o, SmTreeItem *rItem);
        void writeRecursive(SmTreeItem *start, QDataStream &stream);
        void writeItem(SmTreeItem *item, QDataStream &stream);
        SmTreeItem *readCache(int o);
        SmTreeItem *readItem(QDataStream &stream) const;
        const QString cacheFile(int o) const;
        QSqlDatabase mDb;
        QHash<QString, int> mAvailableOrders;
        QList<ArchiveCollector*> mCollectors;
        int mOrder;
};

class ArchiveFilesModel : public SmTreeModel {
    Q_OBJECT
    public:
        enum CustomRoles { ExpansionRole = Qt::UserRole + 1, SeriesPartIdRole = Qt::UserRole + 2, FilenameRole = Qt::UserRole + 3, Md5SumRole = Qt::UserRole + 4, SizeRole = Qt::UserRole + 5, DvdNoRole = Qt::UserRole + 6, FileTypeRole = Qt::UserRole + 7, FileNumberRole = Qt::UserRole + 8, QualityRole = Qt::UserRole + 9, FileIdRole = Qt::UserRole + 10, SizeDurRole = Qt::UserRole + 11, FullPathRole = Qt::UserRole + 12 };
        enum Fields { Expansion = 0, SeriesPartId = 1, Filename = 2, Md5Sum = 3, Size = 4, DvdNo = 5, FileType = 6, FileNumber = 7, Quality = 8, FileId = 9, SizeDur = 10, FullPath = 11 };
        enum FileType { Movie = 1, FrontCover = 2, BackCover = 3, GeneralCover = 4 };
        enum { NumFields = 12 };
        explicit ArchiveFilesModel(const QStringList &headers, QObject *parent = 0);
        virtual QVariant data(const QModelIndex &index, int role) const;
        virtual bool setData(const QModelIndex &idx, const QVariant &value, int role);
        virtual Qt::ItemFlags flags(const QModelIndex &index) const;
        int nextDvd() const;
        bool isMovie(const QModelIndex &idx) const;
        void populate(const QSet<int> &seriesPartIds);

    public slots:
        void refresh();

    private:
        QSet<int> mSeriesPartIds;
        QHash<int, QString> mRoleDbColumnMap;
        QSqlDatabase mDb;
};

class ArchiveCollector : public QThread {
    Q_OBJECT
    public:
        explicit ArchiveCollector(int numFields, int order, QObject *parent = 0);
        SmTreeItem *rootItem();
        int sortOrder() const { return mSortOrder; }
        void cancel();

    signals:
        void message(const QString message);

    protected:
        virtual void run();

    private:
        void populateBySeriesName();
        void populateByGenre();
        void populateByActor();
        void fetchChildren(SmTreeItem *parent);
        void fetchSeries(const QVariant &id, SmTreeItem *parent);
        void fetchParts(const QVariant &id, SmTreeItem *parent);
        void checkCancelled();
        QSqlDatabase mDb;
        SmTreeItem *mRootItem;
        QMutex mAccessMx;
        QMutex mCancelledMx;
        int mNumFields;
        int mSortOrder;
        bool mCancelled;
};

#endif // ARCHIVEMODEL_H