File indexing completed on 2024-05-05 04:40:04

0001 /*
0002     SPDX-FileCopyrightText: 2010 Yannick Motta <yannick.motta@gmail.com>
0003     SPDX-FileCopyrightText: 2010 Benjamin Port <port.benjamin@gmail.com>
0004     SPDX-FileCopyrightText: 2014 Milian Wolff <mail@milianw.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef MANPAGEMODEL_H
0010 #define MANPAGEMODEL_H
0011 
0012 #include <QAbstractItemModel>
0013 
0014 #include <KIO/Job>
0015 
0016 #include <QListIterator>
0017 
0018 class QStringListModel;
0019 
0020 // id and name for man section
0021 using ManSection = QPair<QString, QString>;
0022 
0023 class ManPageModel : public QAbstractItemModel
0024 {
0025     Q_OBJECT
0026 
0027 public:
0028     explicit ManPageModel(QObject* parent = nullptr);
0029     ~ManPageModel() override;
0030 
0031     /**
0032      * You can use @p DeclarationRole to get the Declaration for a given index.
0033      * NOTE: If you use that, don't forget to lock the DUChain if you access the declaration!
0034      */
0035     QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
0036     int rowCount(const QModelIndex& parent = QModelIndex()) const override;
0037     int columnCount(const QModelIndex& = QModelIndex()) const override { return 1; }
0038     QModelIndex parent(const QModelIndex& child = QModelIndex()) const override;
0039     QModelIndex index(int row, int column, const QModelIndex& parent = QModelIndex()) const override;
0040 
0041     QStringListModel* indexList();
0042     bool containsIdentifier(const QString& identifier);
0043     int sectionCount() const;
0044     bool isLoaded() const;
0045     int nbSectionLoaded() const;
0046     bool identifierInSection(const QString &identifier, const QString &section) const;
0047     bool hasError() const;
0048     const QString& errorString() const;
0049 
0050 Q_SIGNALS:
0051     void sectionParsed();
0052     void sectionListUpdated();
0053     void manPagesLoaded();
0054     void error(const QString& errorString);
0055 
0056 public Q_SLOTS:
0057     void showItem(const QModelIndex& idx);
0058     void showItemFromUrl(const QUrl& url);
0059 
0060 private Q_SLOTS:
0061     void initModel();
0062 
0063     void indexEntries(KIO::Job* job, const KIO::UDSEntryList& entries);
0064     void indexLoaded(KJob* job);
0065 
0066     void sectionEntries(KIO::Job* job, const KIO::UDSEntryList& entries);
0067     void sectionLoaded();
0068 
0069 private:
0070     QString manPage(const QString &sectionUrl, int position) const;
0071     void initSection();
0072 
0073     QVector<ManSection> m_sectionList;
0074     QHash<QString, QVector<QString> > m_manMap;
0075     QStringList m_index;
0076     QStringListModel* m_indexModel;
0077 
0078     bool m_loaded = false;
0079     int m_nbSectionLoaded = 0;
0080     QString m_errorString;
0081 };
0082 
0083 #endif // MANPAGEMODEL_H