-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnippetmodel.cpp
285 lines (244 loc) · 8.97 KB
/
snippetmodel.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
277
278
279
280
281
282
283
284
285
/*
Copyright (c) 2015 Sergio Martins <iamsergio@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
As a special exception, permission is given to link this program
with any edition of Qt, and distribute the resulting executable,
without including the source code for Qt in the source distribution.
*/
#include "snippetmodel.h"
#include <QStandardPaths>
#include <QStandardItem>
#include <QDir>
#include <QDebug>
#include <QStyle>
#include <QApplication>
#include <QFile>
#include <QUuid>
SnippetModel::SnippetModel(QObject *parent)
: QStandardItemModel(parent)
, m_numSnippets(0)
{
}
QVariant SnippetModel::data(const QModelIndex &index, int role) const
{
QStandardItem *item = itemFromIndex(index);
if (!item) {
qWarning() << Q_FUNC_INFO << "Invalid item for index " << index;
return {};
}
const bool isFolder = this->isFolder(index);
if (isFolder) {
if (role == Qt::DecorationRole) {
QStyle *style = qApp->style();
return style->standardIcon(QStyle::SP_DirOpenIcon);
} else if (role == Qt::DisplayRole) {
return data(index, FolderNameRole);
} else if (role == Qt::EditRole) {
return data(index, FolderNameRole);
}
} else {
Snippet *snip = snippet(index);
if (role == Qt::DisplayRole) {
return snip->title();
} else if (role == Qt::EditRole) {
return index.data(Qt::DisplayRole);
}
}
if (role == RelativePathRole) {
QFileInfo absolute(QStandardItemModel::data(index, AbsolutePathRole).toString());
QFileInfo root(rootPath());
return absolute.absoluteFilePath().replace(root.absoluteFilePath(), QString());
} else if (role == Qt::FontRole) {
QFont font = QStandardItemModel::data(index, Qt::FontRole).value<QFont>();
font.setBold(isFolder);
return font;
}
return QStandardItemModel::data(index, role);
}
bool SnippetModel::setData(const QModelIndex &index, const QVariant &value, int /*role*/)
{
QString text = value.toString();
if (text.isEmpty())
return false;
QStandardItem *item = itemFromIndex(index);
if (isFolder(index)) {
const QString currentFolderPath = index.data(AbsolutePathRole).toString();
QDir dir(currentFolderPath);
dir.cdUp();
const QString newFolderPath = dir.absoluteFilePath(text);
bool success = dir.rename(currentFolderPath, newFolderPath);
if (success) {
item->setData(text, FolderNameRole);
item->setData(newFolderPath, AbsolutePathRole);
}
} else {
Snippet *snip = snippet(index);
snip->setTitle(text);
}
return true;
}
bool SnippetModel::isFolder(const QModelIndex &index) const
{
QStandardItem *item = itemFromIndex(index);
return item ? item->data(IsFolderRole).toBool() : false;
}
Snippet* SnippetModel::snippet(const QModelIndex &index) const
{
QStandardItem *item = itemFromIndex(index);
return item ? item->data(SnippetRole).value<Snippet*>() : nullptr;
}
void SnippetModel::load()
{
beginResetModel();
clear();
m_numSnippets = 0;
import(QDir(rootPath()), invisibleRootItem());
endResetModel();
emit loaded(m_numSnippets, rootPath());
}
void SnippetModel::removeSnippet(const QModelIndex &index)
{
if (!index.isValid()) {
qWarning() << Q_FUNC_INFO << "Refusing to delete invalid index";
return;
}
const bool isFolder = index.data(SnippetModel::IsFolderRole).toBool();
if (isFolder) {
// TODO: Implement this if folder is empty
qWarning() << Q_FUNC_INFO << "Refusing to delete folder";
return;
}
Snippet *snippet = index.data(SnippetModel::SnippetRole).value<Snippet*>();
if (!snippet) {
qWarning() << Q_FUNC_INFO << "Refusing to delete null snippet";
return;
}
removeRow(index.row(), index.parent());
if (!QFile::remove(snippet->absolutePath()))
qWarning() << "Error removing" << snippet->absolutePath();
}
QModelIndex SnippetModel::addSnippet(const QModelIndex &parentIndex)
{
QString parentFolderPath;
QStandardItem *parentItem;
if (parentIndex.isValid()) {
parentFolderPath = parentIndex.data(AbsolutePathRole).toString();
parentItem = itemFromIndex(parentIndex);
} else {
parentFolderPath = rootPath();
parentItem = invisibleRootItem();
}
if (parentFolderPath.isEmpty()) {
qWarning() << Q_FUNC_INFO << "Could not retrieve parent folder path for" << parentIndex;
return {};
}
QUuid uuid = QUuid::createUuid();
const QString filename = uuid.toString().replace("{", "").replace("}", "") + ".snip";
auto snip = new Snippet(parentFolderPath + "/" + filename, this);
snip->setTitle("Empty snippet");
snip->saveToFile();
QStandardItem *item = addSnippet(snip, parentItem);
return indexFromItem(item);
}
/*static*/
QString SnippetModel::emptySnippetTitle()
{
return tr("Empty snippet");
}
QByteArray SnippetModel::snippetDataFolder() const
{
return qgetenv("SNIPPY_FOLDER");
}
QStandardItem * SnippetModel::createFolder(const QString &name, const QModelIndex &parentIndex)
{
QString parentFolderPath;
QStandardItem *parentItem;
if (parentIndex.isValid()) {
parentFolderPath = parentIndex.data(AbsolutePathRole).toString();
parentItem = itemFromIndex(parentIndex);
} else {
parentFolderPath = rootPath();
parentItem = invisibleRootItem();
}
if (parentFolderPath.isEmpty()) {
qWarning() << Q_FUNC_INFO << "Could not retrieve parent folder path for" << parentIndex;
return nullptr;
}
QDir dir(parentFolderPath);
const QString absolutePath = parentFolderPath + "/" + name;
if (QFile::exists(absolutePath)) {
qWarning() << "Folder already exists" << absolutePath;
return itemForName(name, parentIndex); // But still return it, so it can be selected
}
QStandardItem *newItem = nullptr;
if (dir.mkpath(name)) {
newItem = addFolder(name, parentFolderPath + "/" + name, parentItem);
} else {
qWarning() << "Failed to create folder" << name;
}
return newItem;
}
QStandardItem* SnippetModel::addSnippet(Snippet *snippet, QStandardItem *parentItem)
{
QStandardItem *fileItem = new QStandardItem();
fileItem->setData(QVariant::fromValue(snippet), SnippetRole);
parentItem->appendRow(fileItem);
m_numSnippets++;
return fileItem;
}
QStandardItem* SnippetModel::addFolder(const QString &foldername,
const QString &absolutePath, QStandardItem *parentItem)
{
QStandardItem *folderItem = new QStandardItem(foldername);
folderItem->setData(true, IsFolderRole);
folderItem->setData(foldername, FolderNameRole);
folderItem->setData(absolutePath, AbsolutePathRole);
parentItem->appendRow(folderItem);
return folderItem;
}
QStandardItem *SnippetModel::itemForName(const QString &name, const QModelIndex &parentIndex)
{
const int childCount = rowCount(parentIndex);
for (int i = 0; i < childCount; ++i) {
auto childIndex = index(i, 0, parentIndex);
auto childName = data(childIndex, Qt::DisplayRole).toString();
if (childName == name)
return itemFromIndex(childIndex);
}
return nullptr;
}
void SnippetModel::import(QDir dir, QStandardItem *parentItem)
{
dir.setNameFilters({"*.snip"});
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot | QDir::NoSymLinks);
foreach (const QString &filename, dir.entryList()) {
const QString &absoluteFileName = dir.absoluteFilePath(filename);
auto snippet = new Snippet(absoluteFileName, this);
addSnippet(snippet, parentItem);
}
dir.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::NoSymLinks);
foreach (const QString &foldername, dir.entryList()) {
const QString absolutePath = dir.absoluteFilePath(foldername);
QStandardItem *folderItem = addFolder(foldername, absolutePath, parentItem);
import(QDir(absolutePath), folderItem);
}
}
QString SnippetModel::rootPath() const
{
static QString path;
if (path.isEmpty()) {
QByteArray env_path = snippetDataFolder();
path = env_path.isEmpty() ? QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) : QString::fromUtf8(env_path);
}
return path;
}