Commit 353a86e3 authored by Milian Wolff's avatar Milian Wolff
Browse files

Simplify manpagemodel and prevent leaks in test

Most notably, don't use a heap-allocated QListIterator, that's just
insane. Use a plain int to index into a vector, which is much much
more efficient.

Also simplify the test to ensure it properly awaits the load-or-error
case properly.
parent 6ff9410a
......@@ -129,15 +129,15 @@ void ManPageModel::indexLoaded(KJob* job)
emit sectionListUpdated();
iterator = new QListIterator<ManSection>(m_sectionList);
if (iterator->hasNext()) {
Q_ASSERT(m_nbSectionLoaded == 0);
if (!m_sectionList.isEmpty()) {
initSection();
}
}
void ManPageModel::initSection()
{
const QString sectionUrl = iterator->peekNext().first;
const QString sectionUrl = m_sectionList.at(m_nbSectionLoaded).first;
m_manMap[sectionUrl].clear();
auto list = KIO::listDir(QUrl(sectionUrl), KIO::HideProgressInfo);
connect(list, &KIO::ListJob::entries, this, &ManPageModel::sectionEntries);
......@@ -146,7 +146,7 @@ void ManPageModel::initSection()
void ManPageModel::sectionEntries(KIO::Job* /*job*/, const KIO::UDSEntryList& entries)
{
const QString sectionUrl = iterator->peekNext().first;
const QString sectionUrl = m_sectionList.at(m_nbSectionLoaded).first;
auto& pages = m_manMap[sectionUrl];
pages.reserve(pages.size() + entries.size());
for (const KIO::UDSEntry& entry : entries) {
......@@ -156,10 +156,9 @@ void ManPageModel::sectionEntries(KIO::Job* /*job*/, const KIO::UDSEntryList& en
void ManPageModel::sectionLoaded()
{
iterator->next();
m_nbSectionLoaded++;
emit sectionParsed();
if (iterator->hasNext()) {
if (m_nbSectionLoaded < m_sectionList.size()) {
initSection();
} else {
// End of init
......@@ -171,7 +170,6 @@ void ManPageModel::sectionLoaded()
m_index.sort();
m_index.removeDuplicates();
m_indexModel->setStringList(m_index);
delete iterator;
emit manPagesLoaded();
}
}
......
......@@ -84,8 +84,7 @@ private:
QString manPage(const QString &sectionUrl, int position) const;
void initSection();
QListIterator<ManSection> *iterator = nullptr;
QList<ManSection> m_sectionList;
QVector<ManSection> m_sectionList;
QHash<QString, QVector<QString> > m_manMap;
QStringList m_index;
QStringListModel* m_indexModel;
......
......@@ -43,19 +43,16 @@ private Q_SLOTS:
void TestManPageModel::testModel()
{
ManPageModel model;
QSignalSpy spy(&model, SIGNAL(manPagesLoaded()));
spy.wait();
if (model.isLoaded()) {
QVERIFY(model.rowCount() > 0);
#if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
new QAbstractItemModelTester(&model, this);
#else
new ModelTest(&model);
#endif
} else {
QCOMPARE(model.rowCount(), 0);
}
QTRY_VERIFY(model.isLoaded() || model.hasError());
if (model.isLoaded())
QVERIFY(model.rowCount() > 0);
}
void TestManPageModel::testDocumentation()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment