| 94 | TemplatesModel::~TemplatesModel() = default; |
| 95 | |
| 96 | void TemplatesModel::refresh() |
| 97 | { |
| 98 | Q_D(TemplatesModel); |
| 99 | |
| 100 | clear(); |
| 101 | d->templateItems.clear(); |
| 102 | d->templateItems[QString()] = invisibleRootItem(); |
| 103 | d->extractTemplateDescriptions(); |
| 104 | |
| 105 | QStringList templateArchives; |
| 106 | for (const QString& archivePath : std::as_const(d->searchPaths)) { |
| 107 | const QStringList files = QDir(archivePath).entryList(QDir::Files); |
| 108 | for (const QString& file : files) { |
| 109 | templateArchives.append(archivePath + file); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | QStringList templateDescriptions; |
| 114 | const QStringList templatePaths = |
| 115 | {QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + d->resourceFilter( |
| 116 | TemplatesModelPrivate::Description)}; |
| 117 | for (const QString& templateDescription : templatePaths) { |
| 118 | const QStringList files = QDir(templateDescription).entryList(QDir::Files); |
| 119 | for (const QString& file : files) { |
| 120 | templateDescriptions.append(templateDescription + file); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | for (const QString& templateDescription : std::as_const(templateDescriptions)) { |
| 125 | QFileInfo fi(templateDescription); |
| 126 | bool archiveFound = false; |
| 127 | for (const QString& templateArchive : std::as_const(templateArchives)) { |
| 128 | if (QFileInfo(templateArchive).baseName() == fi.baseName()) { |
| 129 | archiveFound = true; |
| 130 | |
| 131 | KConfig templateConfig(templateDescription); |
| 132 | KConfigGroup general(&templateConfig, QStringLiteral("General")); |
| 133 | QString name = general.readEntry("Name"); |
| 134 | QString category = general.readEntry("Category"); |
| 135 | QString comment = general.readEntry("Comment"); |
| 136 | TemplatePreviewIcon icon(general.readEntry("Icon"), templateArchive, d->resourceFilter( |
| 137 | TemplatesModelPrivate::Preview)); |
| 138 | |
| 139 | QStandardItem* templateItem = d->createItem(name, category, invisibleRootItem()); |
| 140 | templateItem->setData(templateDescription, DescriptionFileRole); |
| 141 | templateItem->setData(templateArchive, ArchiveFileRole); |
| 142 | templateItem->setData(comment, CommentRole); |
| 143 | templateItem->setData(QVariant::fromValue<TemplatePreviewIcon>(icon), PreviewIconRole); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | if (!archiveFound) { |
| 148 | // Template file doesn't exist anymore, so remove the description |
| 149 | // saves us the extra lookups for templateExists on the next run |
| 150 | QFile(templateDescription).remove(); |
| 151 | } |
| 152 | } |
| 153 | } |