File indexing completed on 2024-05-05 17:04:25

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2014 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #ifndef CALLIGRAMOBILE_DOCUMENTLISTMODEL_H
0021 #define CALLIGRAMOBILE_DOCUMENTLISTMODEL_H
0022 
0023 #include <QAbstractListModel>
0024 #include <QQmlParserStatus>
0025 #include <QRunnable>
0026 #include <QDateTime>
0027 
0028 class SearchThread;
0029 
0030 class DocumentListModel : public QAbstractListModel, public QQmlParserStatus
0031 {
0032     Q_OBJECT
0033     Q_PROPERTY(DocumentType filter READ filter WRITE setFilter NOTIFY filterChanged)
0034     Q_PROPERTY(QString documentsFolder READ documentsFolder CONSTANT)
0035     Q_ENUMS(GroupBy)
0036     Q_ENUMS(Filter)
0037     Q_ENUMS(DocumentType)
0038     Q_INTERFACES(QQmlParserStatus)
0039 
0040 public:
0041     explicit DocumentListModel(QObject *parent = 0);
0042     ~DocumentListModel() override;
0043 
0044     enum CustomRoles {
0045         FileNameRole = Qt::UserRole + 1,
0046         FilePathRole,
0047         DocTypeRole,
0048         SectionCategoryRole,
0049         FileSizeRole,
0050         AuthorNameRole,
0051         AccessedTimeRole,
0052         ModifiedTimeRole,
0053         UUIDRole,
0054     };
0055 
0056     enum GroupBy { GroupByName, GroupByDocType };
0057    
0058     enum DocumentType
0059     {
0060         UnknownType,
0061         TextDocumentType,
0062         PresentationType,
0063         SpreadsheetType,
0064     };
0065 
0066     struct DocumentInfo {
0067         bool operator==(const DocumentInfo &other) const { return filePath == other.filePath; }
0068         QString filePath;
0069         QString fileName;
0070         DocumentType docType;
0071         QString fileSize;
0072         QString authorName;
0073         QDateTime accessedTime;
0074         QDateTime modifiedTime;
0075         QString uuid;
0076     };
0077 
0078     // reimp from QAbstractListModel
0079     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0080     int columnCount(const QModelIndex &parent = QModelIndex()) const override;
0081     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0082     QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
0083 
0084     // reimp from QDeclarativeParserStatus
0085     void classBegin() override;
0086     void componentComplete() override;
0087 
0088     DocumentType filter();
0089     QString documentsFolder() const;
0090 
0091     static QString prettyTime(QDateTime theTime);
0092 
0093 Q_SIGNALS:
0094     void filterChanged();
0095 
0096 public Q_SLOTS:
0097     void startSearch();
0098     void stopSearch();
0099     void addDocument(const DocumentListModel::DocumentInfo &info);
0100     void setFilter(DocumentType newFilter);
0101 
0102 public:
0103     Q_INVOKABLE void groupBy(GroupBy role);
0104 
0105 private Q_SLOTS:
0106     void searchFinished();
0107 
0108 private:
0109     void relayout();
0110 
0111     QHash<QString, DocumentType> m_docTypes;
0112     QList<DocumentInfo> m_allDocumentInfos;
0113     QList<DocumentInfo> m_currentDocumentInfos;
0114     SearchThread *m_searchThread;
0115     GroupBy m_groupBy;
0116     DocumentType m_filter;
0117     QString m_filteredTypes;
0118     friend class SearchThread;
0119 };
0120 
0121 Q_DECLARE_METATYPE(DocumentListModel::DocumentInfo);
0122 
0123 class SearchThread : public QObject, public QRunnable
0124 {
0125     Q_OBJECT
0126 public:
0127     SearchThread(const QHash<QString, DocumentListModel::DocumentType> &docTypes, QObject *parent = 0);
0128     ~SearchThread() override;
0129 
0130     void run() override;
0131     
0132     void abort() { m_abort = true; }
0133 
0134 Q_SIGNALS:
0135     void documentFound(const DocumentListModel::DocumentInfo &);
0136     void finished();
0137 
0138 private:
0139     bool m_abort;
0140     QHash<QString, DocumentListModel::DocumentType> m_docTypes;
0141     static const QString textDocumentType;
0142     static const QString presentationType;
0143     static const QString spreadsheetType;
0144 };
0145 
0146 #endif // CALLIGRAMOBILE_DOCUMENTLISTMODEL_H
0147