-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpackagemodel.cpp
277 lines (225 loc) · 7.15 KB
/
packagemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <packagemodel.h>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <KDirWatch>
#include <KIcon>
#include <KUser>
#include <Plasma/Package>
#include <Plasma/PackageMetadata>
PackageModel::PackageModel(QObject *parent)
: QAbstractItemModel(parent),
m_directory(0),
m_structure(0),
m_package(0)
{
}
PackageModel::~PackageModel()
{
delete m_directory;
delete m_package;
m_structure = 0;
}
void PackageModel::setPackageType(const QString &type)
{
m_structure = 0;
m_structure = Plasma::PackageStructure::load(type);
}
QString PackageModel::packageType() const
{
if (m_structure) {
return m_structure->type();
}
return QString();
}
void PackageModel::setPackage(const QString &path)
{
if (!m_structure) {
kDebug() << "Must set the package type FIRST!";
return;
}
m_structure->setPath(path);
delete m_package;
m_package = new Plasma::Package(path, m_structure);
delete m_directory;
m_directory = new KDirWatch(this);
m_directory->addDir(path, KDirWatch::WatchSubDirs);
loadPackage();
connect(m_directory, SIGNAL(created(QString)), this, SLOT(fileAddedOnDisk(QString)));
connect(m_directory, SIGNAL(deleted(QString)), this, SLOT(fileDeletedOnDisk(QString)));
}
QString PackageModel::package() const
{
if (m_package) {
return m_package->path();
}
return QString();
}
QVariant PackageModel::data(const QModelIndex &index, int role) const
{
if (!m_package) {
return QVariant();
}
//TODO: other display roles!
const char *key = static_cast<const char *>(index.internalPointer());
if (key) {
if (index.row() == 0) {
if (role == Qt::DisplayRole) {
return i18n("New...");
} /* else if (role == Qt::DecorationRole) {
return KIcon("file-new");
} */
} else if (role == Qt::DisplayRole) {
QList<const char *> named = m_namedFiles.value(key);
int row = index.row() - 1;
if (row < named.count()) {
return m_package->structure()->name(named.at(row));
}
row -= named.count();
QStringList l = m_files.value(key);
if (index.row() < l.count()) {
//kDebug() << "got" << l.at(index.row() - 1);
return l.at(index.row());
}
}
} else if (role == Qt::DisplayRole) {
if (index.row() == m_topEntries.count()) {
return i18n("Metadata");
}
return m_structure->name(m_topEntries.at(index.row()));
}
return QVariant();
}
QModelIndex PackageModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid()) {
if (parent.row() >= m_topEntries.count() || parent.parent().isValid()) {
//kDebug() << "FAIL" << row << column;
return QModelIndex();
}
const char *key = m_topEntries.at(parent.row());
if (row <= m_files[key].count() + m_namedFiles[key].count()) {
//kDebug() << "going to return" << row << column << key;
return createIndex(row, column, (void*)key);
} else {
//kDebug() << "FAIL";
return QModelIndex();
}
}
if (row < m_topEntries.count()) {
return createIndex(row, column);
}
return QModelIndex();
}
QModelIndex PackageModel::parent(const QModelIndex &index) const
{
const char *key = static_cast<const char *>(index.internalPointer());
if (m_topEntries.contains(key)) {
return createIndex(m_topEntries.indexOf(key), 0);
}
return QModelIndex();
}
int PackageModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
//FIXME: need to accmodate more information
return 1;
}
int PackageModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
if (parent.row() < m_topEntries.count()) {
const char *key = m_topEntries.at(parent.row());
//kDebug() << "looking for" << key << m_files[key].count();
return m_files.contains(key) ? m_files[key].count() + m_namedFiles[key].count() + 1 : 0;
} else {
return 0;
}
}
return m_topEntries.count() + 1;
}
void PackageModel::loadPackage()
{
reset();
if (!m_package) {
kDebug() << "No package to load.";
return;
}
QDir dir(m_package->path());
Plasma::PackageStructure::Ptr structure = m_package->structure();
if (!dir.exists("metadata.desktop")) {
KUser user;
Plasma::PackageMetadata metadata;
metadata.setAuthor(user.fullName());
metadata.setLicense("GPL");
metadata.setName(dir.dirName());
metadata.setServiceType(structure->type());
metadata.write(dir.path() + "/metadata.desktop");
}
QString contents = structure->contentsPrefix();
if (!contents.isEmpty()) {
dir.mkpath(contents);
dir.cd(contents);
}
foreach (const char *key, structure->directories()) {
QString path = structure->path(key);
if (!dir.exists(path)) {
dir.mkpath(path);
}
m_topEntries.append(key);
}
QList<const char *> files = structure->files();
QHash<QString, const char *> indexedFiles;
foreach (const char *key, structure->files()) {
QString path = structure->path(key);
if (!dir.exists(path)) {
QFileInfo info(dir.path() + '/' + path);
dir.mkpath(dir.relativeFilePath(info.absolutePath()));
QFile f(dir.path() + '/' + path);
f.open(QIODevice::WriteOnly);
}
indexedFiles.insert(path, key);
}
// we don't -1 because we have a "ghost" metadata entry
beginInsertRows(QModelIndex(), 0, m_topEntries.count());
endInsertRows();
foreach (const char *key, structure->directories()) {
QString path = structure->path(key);
if (!path.endsWith('/')) {
path += '/';
}
QStringList files = m_package->entryList(key);
QList<const char *> namedFiles;
QStringList userFiles;
foreach (const QString &file, files) {
QString filePath = path + file;
if (indexedFiles.contains(filePath)) {
namedFiles.append(indexedFiles.value(filePath));
indexedFiles.remove(filePath);
} else {
userFiles.append(file);
}
}
m_files[key] = userFiles;
m_namedFiles[key] = namedFiles;
kDebug() << m_topEntries.indexOf(key) << key << "has" << files.count() << "files" << files;
beginInsertRows(createIndex(m_topEntries.indexOf(key), 0), 0, files.count());
endInsertRows();
}
if (!indexedFiles.empty()) {
int currentTopCount = m_topEntries.count();
foreach (const char *key, indexedFiles) {
m_topEntries.append(key);
}
beginInsertRows(QModelIndex(), currentTopCount, indexedFiles.count());
endInsertRows();
}
}
void PackageModel::fileAddedOnDisk(const QString &path)
{
kDebug() << path;
}
void PackageModel::fileDeletedOnDisk(const QString &path)
{
kDebug() << path;
}