forked from flameshot-org/flameshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.cpp
More file actions
118 lines (108 loc) · 3.6 KB
/
history.cpp
File metadata and controls
118 lines (108 loc) · 3.6 KB
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
#include "history.h"
#include "src/utils/confighandler.h"
#include <QDir>
#include <QFile>
#include <QProcessEnvironment>
#include <QStringList>
History::History()
{
// Get cache history path
ConfigHandler config;
#ifdef Q_OS_WIN
m_historyPath = QDir::homePath() + "/AppData/Roaming/flameshot/history/";
#else
QString cachepath = QProcessEnvironment::systemEnvironment().value(
"XDG_CACHE_HOME", QDir::homePath() + "/.cache");
m_historyPath = cachepath + "/flameshot/history/";
#endif
// Check if directory for history exists and create if doesn't
QDir dir = QDir(m_historyPath);
if (!dir.exists()) {
dir.mkpath(".");
}
}
const QString& History::path()
{
return m_historyPath;
}
void History::save(const QPixmap& pixmap, const QString& fileName)
{
// scale preview only in local disk
QPixmap pixmapScaled = QPixmap(pixmap);
if (pixmap.height() / HISTORYPIXMAP_MAX_PREVIEW_HEIGHT >=
pixmap.width() / HISTORYPIXMAP_MAX_PREVIEW_WIDTH) {
pixmapScaled = pixmap.scaledToHeight(HISTORYPIXMAP_MAX_PREVIEW_HEIGHT,
Qt::SmoothTransformation);
} else {
pixmapScaled = pixmap.scaledToWidth(HISTORYPIXMAP_MAX_PREVIEW_WIDTH,
Qt::SmoothTransformation);
}
// save preview
QFile file(path() + fileName);
file.open(QIODevice::WriteOnly);
pixmapScaled.save(&file, "PNG");
history();
}
const QList<QString>& History::history()
{
QDir directory(path());
QStringList images = directory.entryList(QStringList() << "*.png"
<< "*.PNG",
QDir::Files,
QDir::Time);
int cnt = 0;
int max = ConfigHandler().uploadHistoryMax();
m_thumbs.clear();
foreach (QString fileName, images) {
if (++cnt <= max) {
m_thumbs.append(fileName);
} else {
QFile file(path() + fileName);
file.remove();
}
}
return m_thumbs;
}
const HistoryFileName& History::unpackFileName(const QString& fileNamePacked)
{
int nPathIndex = fileNamePacked.lastIndexOf("/");
QStringList unpackedFileName;
if (nPathIndex == -1) {
unpackedFileName = fileNamePacked.split("-");
} else {
unpackedFileName = fileNamePacked.mid(nPathIndex + 1).split("-");
}
switch (unpackedFileName.length()) {
case 3:
m_unpackedFileName.file = unpackedFileName[2];
m_unpackedFileName.token = unpackedFileName[1];
m_unpackedFileName.type = unpackedFileName[0];
break;
case 2:
m_unpackedFileName.file = unpackedFileName[1];
m_unpackedFileName.token = "";
m_unpackedFileName.type = unpackedFileName[0];
break;
default:
m_unpackedFileName.file = unpackedFileName[0];
m_unpackedFileName.token = "";
m_unpackedFileName.type = "";
break;
}
return m_unpackedFileName;
}
const QString& History::packFileName(const QString& storageType,
const QString& deleteToken,
const QString& fileName)
{
m_packedFileName = fileName;
if (storageType.length() > 0) {
if (deleteToken.length() > 0) {
m_packedFileName =
storageType + "-" + deleteToken + "-" + m_packedFileName;
} else {
m_packedFileName = storageType + "-" + m_packedFileName;
}
}
return m_packedFileName;
}