1 #include "historywidget.h"
2 #include "src/utils/confighandler.h"
3 #include "src/utils/history.h"
4 #include "src/widgets/notificationwidget.h"
5 #include <QApplication>
6 #include <QClipboard>
7 #include <QDateTime>
8 #include <QDesktopServices>
9 #include <QDesktopWidget>
10 #include <QFileInfo>
11 #include <QIcon>
12 #include <QLabel>
13 #include <QLayoutItem>
14 #include <QMessageBox>
15 #include <QPixmap>
16 #include <QPushButton>
17 #include <QScrollArea>
18 #include <QUrl>
19 #include <QVBoxLayout>
20
HistoryWidget(QWidget * parent)21 HistoryWidget::HistoryWidget(QWidget* parent)
22 : QDialog(parent)
23 {
24 setWindowIcon(QIcon(":img/app/flameshot.svg"));
25 setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
26 setWindowTitle(tr("Latest Uploads"));
27 resize(QDesktopWidget().availableGeometry(this).size() * 0.5);
28 m_notification = new NotificationWidget();
29
30 QGridLayout* layout = new QGridLayout(this);
31 layout->setContentsMargins(QMargins(0, 0, 0, 0));
32 setLayout(layout);
33
34 m_pVBox = new QVBoxLayout(this);
35 m_pVBox->setAlignment(Qt::AlignTop);
36
37 QScrollArea* scrollArea = new QScrollArea(this);
38 scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
39 scrollArea->setWidgetResizable(true);
40 scrollArea->setGeometry(this->frameGeometry());
41
42 QWidget* widget = new QWidget();
43 scrollArea->setWidget(widget);
44 widget->setLayout(m_pVBox);
45 layout->addWidget(scrollArea);
46 }
47
~HistoryWidget()48 HistoryWidget::~HistoryWidget()
49 {
50 delete m_notification;
51 }
52
clearHistoryLayout(QLayout * layout)53 void HistoryWidget::clearHistoryLayout(QLayout* layout)
54 {
55 QLayoutItem* child;
56 while (layout->count() != 0) {
57 child = layout->takeAt(0);
58 if (child->layout() != 0) {
59 clearHistoryLayout(child->layout());
60 } else if (child->widget() != 0) {
61 delete child->widget();
62 }
63
64 delete child;
65 }
66 }
67
loadHistory()68 void HistoryWidget::loadHistory()
69 {
70 // clear old history if exists
71 clearHistoryLayout(m_pVBox);
72
73 // read history files
74 History history = History();
75 QList<QString> historyFiles = history.history();
76
77 if (historyFiles.isEmpty()) {
78 setEmptyMessage();
79 } else {
80 // generate history list
81 foreach (QString fileName, historyFiles) {
82 addLine(history.path(), fileName);
83 }
84 }
85 }
86
setEmptyMessage()87 void HistoryWidget::setEmptyMessage()
88 {
89 QPushButton* buttonEmpty = new QPushButton;
90 buttonEmpty->setText(tr("Screenshots history is empty"));
91 buttonEmpty->setMinimumSize(1, HISTORYPIXMAP_MAX_PREVIEW_HEIGHT);
92 connect(buttonEmpty, &QPushButton::clicked, this, [=]() { this->close(); });
93 m_pVBox->addWidget(buttonEmpty);
94 }
95
addLine(const QString & path,const QString & fileName)96 void HistoryWidget::addLine(const QString& path, const QString& fileName)
97 {
98 QHBoxLayout* phbl = new QHBoxLayout();
99 QString fullFileName = path + fileName;
100
101 History history;
102 HISTORY_FILE_NAME unpackFileName = history.unpackFileName(fileName);
103
104 QString url = "https://imgur.com/" + unpackFileName.file;
105
106 // load pixmap
107 QPixmap pixmap;
108 pixmap.load(fullFileName, "png");
109
110 // TODO - remove much later, it is still required to keep old previews works
111 // fine
112 if (pixmap.height() / HISTORYPIXMAP_MAX_PREVIEW_HEIGHT >=
113 pixmap.width() / HISTORYPIXMAP_MAX_PREVIEW_WIDTH) {
114 pixmap = pixmap.scaledToHeight(HISTORYPIXMAP_MAX_PREVIEW_HEIGHT,
115 Qt::SmoothTransformation);
116 } else {
117 pixmap = pixmap.scaledToWidth(HISTORYPIXMAP_MAX_PREVIEW_WIDTH,
118 Qt::SmoothTransformation);
119 }
120
121 // get file info
122 QFileInfo* pFileInfo = new QFileInfo(fullFileName);
123 QString lastModified =
124 pFileInfo->lastModified().toString("yyyy-MM-dd\nhh:mm:ss");
125
126 // screenshot preview
127 QLabel* pScreenshot = new QLabel();
128 pScreenshot->setStyleSheet("padding: 5px;");
129 pScreenshot->setMinimumHeight(HISTORYPIXMAP_MAX_PREVIEW_HEIGHT);
130 pScreenshot->setPixmap(pixmap);
131
132 // screenshot datetime
133 QLabel* pScreenshotText = new QLabel();
134 pScreenshotText->setStyleSheet("padding: 5px;");
135 pScreenshotText->setMinimumHeight(HISTORYPIXMAP_MAX_PREVIEW_HEIGHT);
136 pScreenshotText->setAlignment(Qt::AlignCenter);
137 pScreenshotText->setText(lastModified);
138
139 // copy url
140 QPushButton* buttonCopyUrl = new QPushButton;
141 buttonCopyUrl->setText(tr("Copy URL"));
142 buttonCopyUrl->setMinimumHeight(HISTORYPIXMAP_MAX_PREVIEW_HEIGHT);
143 connect(buttonCopyUrl, &QPushButton::clicked, this, [=]() {
144 QApplication::clipboard()->setText(url);
145 m_notification->showMessage(tr("URL copied to clipboard."));
146 this->close();
147 });
148
149 // open in browser
150 QPushButton* buttonOpen = new QPushButton;
151 buttonOpen->setText(tr("Open in browser"));
152 buttonOpen->setMinimumHeight(HISTORYPIXMAP_MAX_PREVIEW_HEIGHT);
153 connect(buttonOpen, &QPushButton::clicked, this, [=]() {
154 QDesktopServices::openUrl(QUrl(url));
155 this->close();
156 });
157
158 // delete
159 QPushButton* buttonDelete = new QPushButton;
160 buttonDelete->setIcon(QIcon(":/img/material/black/delete.svg"));
161 buttonDelete->setMinimumHeight(HISTORYPIXMAP_MAX_PREVIEW_HEIGHT);
162 connect(buttonDelete, &QPushButton::clicked, this, [=]() {
163 if (ConfigHandler().historyConfirmationToDelete() &&
164 QMessageBox::No ==
165 QMessageBox::question(
166 this,
167 tr("Confirm to delete"),
168 tr("Are you sure you want to delete a screenshot from the "
169 "latest uploads and server?"),
170 QMessageBox::Yes | QMessageBox::No)) {
171 return;
172 }
173 QDesktopServices::openUrl(
174 QUrl(QStringLiteral("https://imgur.com/delete/%1")
175 .arg(unpackFileName.token)));
176 removeCacheFile(fullFileName);
177 removeLayoutItem(phbl);
178 });
179
180 // layout
181 phbl->addWidget(pScreenshot);
182 phbl->addWidget(pScreenshotText);
183 phbl->addWidget(buttonCopyUrl);
184 phbl->addWidget(buttonOpen);
185 phbl->addWidget(buttonDelete);
186
187 phbl->setStretchFactor(pScreenshot, 6);
188 phbl->setStretchFactor(pScreenshotText, 4);
189 phbl->setStretchFactor(buttonCopyUrl, 4);
190 phbl->setStretchFactor(buttonOpen, 4);
191 phbl->setStretchFactor(buttonDelete, 1);
192
193 // add to scroll
194 m_pVBox->addLayout(phbl);
195 }
196
removeItem(QLayout * pl,const QString & fileName,const QString & deleteToken)197 void HistoryWidget::removeItem(QLayout* pl,
198 const QString& fileName,
199 const QString& deleteToken)
200 {
201 /* hide();
202 ImgS3Uploader* imgUploader = new ImgS3Uploader();
203 imgUploader->show();
204 imgUploader->deleteResource(fileName, deleteToken);
205 connect(imgUploader, &QWidget::destroyed, this, [=]() {
206 if (imgUploader->resultStatus) {
207 removeLayoutItem(pl);
208 }
209 imgUploader->deleteLater();
210 show();
211 });*/
212 }
213
removeLayoutItem(QLayout * pl)214 void HistoryWidget::removeLayoutItem(QLayout* pl)
215 {
216 // remove current row or refresh list
217 while (pl->count() > 0) {
218 QLayoutItem* item = pl->takeAt(0);
219 delete item->widget();
220 delete item;
221 }
222 m_pVBox->removeItem(pl);
223 delete pl;
224
225 // set "empty" message if no items left
226 if (m_pVBox->count() == 0) {
227 setEmptyMessage();
228 }
229 }
230
removeCacheFile(const QString & fullFileName)231 void HistoryWidget::removeCacheFile(const QString& fullFileName)
232 {
233 // premove history preview
234 QFile file(fullFileName);
235 if (file.exists()) {
236 file.remove();
237 }
238 }
239