-
Notifications
You must be signed in to change notification settings - Fork 0
/
qlipperwidget.cpp
executable file
·341 lines (330 loc) · 10.2 KB
/
qlipperwidget.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#include "qlipperwidget.h"
#include "ui_qlipperwidget.h"
#include <QMimeData>
#include <QFile>
#include <QTextStream>
#include <QCloseEvent>
#include <map>
#include <QMessageBox>
/*!
* \brief Creates the Qlipper widget
* \param savePath Where to store the clipboard log
* \param parent The owning widget, used for Qt's memory management.
*/
QlipperWidget::QlipperWidget(QString savePath, QWidget *parent)
: QDialog(parent), ui(new Ui::QlipperWidget), path(savePath),
isLogEnabled(false)
{
ui->setupUi(this);
yearNode = monthNode = dayNode = nullptr;
clipboard = QApplication::clipboard();
nodeDate = QDate::currentDate();
model = new QStandardItemModel(this);
if(!load())
{
model->clear();
if(yearNode)
delete yearNode;
yearNode = new QStandardItem(nodeDate.toString("yyyy"));
if(monthNode)
delete monthNode;
monthNode = new QStandardItem(nodeDate.toString("MM - MMMM"));
if(dayNode)
delete dayNode;
dayNode = new QStandardItem(nodeDate.toString("dd - dddd"));
monthNode->appendRow(dayNode);
yearNode->appendRow(monthNode);
model->invisibleRootItem()->appendRow(yearNode);
}
ui->clipboardTree->setModel(model);
ui->clipboardTree->setHeaderHidden(true);
ui->clipboardTree->expandAll();
delayedSave.setSingleShot(true);
connect(clipboard, SIGNAL(dataChanged()), this, SLOT(clipboardChanges()));
connect(ui->removeButton, SIGNAL(clicked()), this,
SLOT(removeButtonClicked()));
connect(&delayedSave, SIGNAL(timeout()), this, SLOT(save()));
connect(ui->clipboardTree, SIGNAL(activated(QModelIndex)), this,
SLOT(toClipboard(QModelIndex)));
}
/*!
* \brief Saves the clipboard history, and deletes the GUI.
*/
QlipperWidget::~QlipperWidget()
{
save();
delete ui;
}
/*!
* \brief Sets if logging should be enabled.
* \param enabled If logging should be enabled
*/
void QlipperWidget::setLogEnabled(bool enabled) { isLogEnabled = enabled; }
/*!
* \brief Sets where logs should be stored.
* \param newPath Where logs should be stored.
*/
void QlipperWidget::setStatePath(QString newPath)
{
path = newPath;
load();
}
/*!
* \brief Hides the UI, rather than actually closing it.
* \param event used to tell the program not to close the window.
*/
void QlipperWidget::closeEvent(QCloseEvent *event)
{
setVisible(false);
event->ignore();
}
/*!
* \brief Saves the clipboard history to file.
* \details Saves the history to a file. It is compressed using qCompress(),
* which is based on zlib, but may not be compatible with it. The string "/|\"
* is used as a delimiter, which should help prevent daily usage from including
* delimiters. `TIME is used to mark a change in time in the file.
*/
void QlipperWidget::save()
{
if(!path.isEmpty())
{
std::map<QString, bool> usedStrings;
QByteArray compressMe;
QTextStream stream(&compressMe, QIODevice::WriteOnly);
stream << nodeDate.toString("yyyy-MM-dd") << '\n';
QStandardItem *root = model->invisibleRootItem();
for(int i = 0; i < root->rowCount(); ++i)
{
QStandardItem *year = root->child(i);
stream << "`YEAR:" << year->text() << "/|\\";
for(int j = 0; j < year->rowCount(); ++j)
{
QStandardItem *month = year->child(j);
stream << "`MONTH:" << month->text() << "/|\\";
for(int k = 0; k < month->rowCount(); ++k)
{
QStandardItem *day = month->child(k);
stream << "`DAY:" << day->text() << "/|\\";
for(int l = 0; l < day->rowCount(); ++l)
{
QStandardItem *node = day->child(l);
if(!usedStrings[node->text()])
{
stream << node->text() << "/|\\";
usedStrings[node->text()] = true;
}
}
}
}
}
stream.flush();
QFile file(path);
file.open(QIODevice::WriteOnly);
file.write(qCompress(compressMe, 9));
}
}
/*!
* \brief Sets selected text to the current clipboard.
* \param index What to set the clipboard too.
*/
void QlipperWidget::toClipboard(QModelIndex index)
{
clipboard->setText(index.data().toString());
}
/*!
* \brief Loads the clipboard history from file.
* \details Saves the history to a file. It is compressed using qUncompress(),
* which is based on zlib, but may not be compatible with it. The string "/|\"
* is used as a delimiter, which should help prevent daily usage from including
* delimiters. `TIME is used to mark a change in time in the file.
*/
bool QlipperWidget::load()
{
if(!path.isEmpty())
{
QFile file(path);
if(file.exists())
{
file.open(QIODevice::ReadOnly);
QByteArray compressed = file.readAll();
QByteArray uncompressed = qUncompress(compressed);
QTextStream stream(&uncompressed, QIODevice::ReadOnly);
QString dateString = stream.readLine();
nodeDate = QDate::fromString(dateString, "yyyy-MM-dd");
QStringList strings = stream.readAll().split("/|\\");
QStandardItem *year = nullptr, *month = nullptr, *day = nullptr;
yearNode = monthNode = dayNode = nullptr;
for(QString string : strings)
{
if(string.startsWith("`YEAR:"))
{
QString yearText = string.mid(6);
year = new QStandardItem(string.mid(6));
if(!yearNode)
{
yearNode = year;
}
model->invisibleRootItem()->appendRow(year);
}
else if(string.startsWith("`MONTH:"))
{
QString yearText = string.mid(6);
month = new QStandardItem(string.mid(7));
if(!monthNode)
{
monthNode = month;
}
if(!year)
return false;
year->appendRow(month);
}
else if(string.startsWith("`DAY:"))
{
QString yearText = string.mid(5);
day = new QStandardItem(string.mid(5));
if(!dayNode)
{
dayNode = day;
}
if(!month)
return false;
month->appendRow(day);
}
else if(!string.isEmpty())
{
QStringList entries = string.split("\n");
for(const QString &entry : entries)
{
if(!day)
return false;
day->appendRow(new QStandardItem(entry));
}
}
}
return true;
}
}
return false;
}
/*!
* \brief Called when the clipboard changes. It checks if the day has changed
* since the last clipboard change, and if it has it creates a new "day",
* "month", and potentially "year" node, depending on how the date has changed.
*/
void QlipperWidget::clipboardChanges()
{
if(isLogEnabled && clipboard->mimeData()->hasText())
{
if(nodeDate != QDate::currentDate())
{
QDate newDate = QDate::currentDate();
if(newDate.year() != nodeDate.year())
{
yearNode = new QStandardItem(newDate.toString("yyyy"));
monthNode = new QStandardItem(newDate.toString("MM - MMMM"));
dayNode = new QStandardItem(newDate.toString("dd - dddd"));
monthNode->appendRow(dayNode);
yearNode->appendRow(monthNode);
model->invisibleRootItem()->insertRow(0, yearNode);
}
else if(newDate.month() != nodeDate.month())
{
monthNode = new QStandardItem(newDate.toString("MM - MMMM"));
dayNode = new QStandardItem(newDate.toString("dd - dddd"));
monthNode->appendRow(dayNode);
yearNode->insertRow(0, monthNode);
}
else if(newDate.day() != nodeDate.day())
{
dayNode = new QStandardItem(newDate.toString("dd - dddd"));
monthNode->insertRow(0, dayNode);
}
nodeDate = newDate;
}
dayNode->insertRow(0, new QStandardItem(clipboard->text()));
Q_EMIT speakThis(clipboard->text());
delayedSave.start(60000);
}
}
/*!
* \brief Removes the selected item from the list of items in the clipboard, as
* well as the history.
*/
void QlipperWidget::removeButtonClicked()
{
QModelIndexList list = ui->clipboardTree->selectionModel()->selectedIndexes();
for(QModelIndex i : list)
{
if(!model->hasChildren(i))
{
model->removeRow(i.row(), i.parent());
}
}
ui->clipboardTree->clearSelection();
delayedSave.start(60000);
}
/*!
* \brief Presents every entry in the clipboard history
* that matches the query in searchText, and offers the ability to continue,
* copy the entry to clipboard, delete the entry, or continue.
*/
void QlipperWidget::on_searchButton_clicked()
{
QStandardItem *root = model->invisibleRootItem();
bool results = false;
for(int i = 0; i < root->rowCount(); ++i)
{
// years
for(int j = 0; j < root->child(i)->rowCount(); ++j)
{
// months
for(int k = 0; k < root->child(i)->child(j)->rowCount(); ++k)
{
// days
for(int l = 0; l < root->child(i)->child(j)->child(k)->rowCount(); ++l)
{
if(root->child(i)->child(j)->child(k)->child(l)->text().contains(
ui->searchText->text(), Qt::CaseInsensitive))
{
results = true;
QMessageBox box;
box.setText(root->child(i)->child(j)->child(k)->child(l)->text());
box.setWindowTitle("Result Found");
QPushButton *del = new QPushButton("Delete", &box);
QPushButton *copy = new QPushButton("Copy", &box);
QPushButton *cont = new QPushButton("Continue", &box);
QPushButton *stop = new QPushButton("Stop", &box);
box.addButton(del, QMessageBox::DestructiveRole);
box.addButton(copy, QMessageBox::ApplyRole);
box.addButton(cont, QMessageBox::AcceptRole);
box.addButton(stop, QMessageBox::RejectRole);
box.exec();
const auto result = box.clickedButton();
if(result == del)
{
root->child(i)->child(j)->child(k)->removeRow(l);
}
else if(result == cont)
{
continue;
}
else if(result == copy)
{
clipboard->setText(box.text());
return;
}
else
{
return;
}
}
}
}
}
}
QMessageBox box;
box.setText("No " + tr(results ? "more " : "") + "results");
box.addButton(QMessageBox::Ok);
box.exec();
}