KNewStuff

knsrcmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Dan Leinir Turthra Jensen <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #include "knsrcmodel.h"
8 
9 #include "engine.h"
10 
11 #include <KConfig>
12 #include <KConfigGroup>
13 #include <QDir>
14 
15 class KNSRCModel::Private {
16 public:
17  struct Entry {
18  QString name;
19  QString filePath;
20  };
21  Private(KNSRCModel* qq)
22  : q(qq)
23  {}
24  KNSRCModel* q;
25  QUrl folder;
26  QList<Entry*> entries;
27 
28  void refreshEntries() {
29  q->beginResetModel();
30  qDeleteAll(entries);
31  entries.clear();
32  QDir configDir(folder.toLocalFile());
33  for(const QFileInfo& file : configDir.entryInfoList(QDir::Files)) {
34  KConfig conf(file.absoluteFilePath());
35  KConfigGroup group;
36  if (conf.hasGroup("KNewStuff3")) {
37  group = conf.group("KNewStuff3");
38  } else if (conf.hasGroup("KNewStuff2")) {
39  group = conf.group("KNewStuff2");
40  } else {
41  qWarning() << file.absoluteFilePath() << " doesn't contain a KNewStuff3 (or KNewStuff2) section.";
42  continue;
43  }
44 
45  Entry* entry = new Entry;
46  entry->name = group.readEntry("Name", file.baseName());
47  entry->filePath = file.absoluteFilePath();
48  entries << entry;
49  }
50  q->endResetModel();
51  }
52 };
53 
54 KNSRCModel::KNSRCModel(QObject* parent)
55  : QAbstractListModel(parent)
56  , d(new Private(this))
57 {
58 }
59 
60 KNSRCModel::~KNSRCModel()
61 {
62  delete d;
63 }
64 
65 QHash<int, QByteArray> KNSRCModel::roleNames() const
66 {
67  static const QHash<int, QByteArray> roleNames{
68  {NameRole, "name"},
69  {FilePathRole, "filePath"}
70  };
71  return roleNames;
72 }
73 
74 int KNSRCModel::rowCount(const QModelIndex& parent) const
75 {
76  if(parent.isValid()) {
77  return 0;
78  }
79  return d->entries.count();
80 }
81 
82 QVariant KNSRCModel::data(const QModelIndex& index, int role) const
83 {
84  QVariant result;
85  if(checkIndex(index)) {
86  Private::Entry* entry = d->entries[index.row()];
87  switch(role) {
88  case NameRole:
89  result.setValue(entry->name);
90  break;
91  case FilePathRole:
92  result.setValue(entry->filePath);
93  break;
94  default:
95  break;
96  }
97  }
98  return result;
99 }
100 
101 QUrl KNSRCModel::folder() const
102 {
103  return d->folder;
104 }
105 
106 void KNSRCModel::setFolder(const QUrl& folder)
107 {
108  if(d->folder != folder) {
109  d->folder = folder;
110  d->refreshEntries();
111  Q_EMIT folderChanged();
112  }
113 }
void clear()
QString name(const QVariant &location)
int count(const Key &key) const const
bool isValid() const const
int row() const const
QString toLocalFile() const const
void setValue(const T &value)
KConfigGroup group(const QString &group)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:47:27 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.