-
Notifications
You must be signed in to change notification settings - Fork 1
/
photo.cpp
427 lines (353 loc) · 10.4 KB
/
photo.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#include <QDir>
#include <QFile>
#include <QJsonDocument>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include "filesystem.h"
#include "photosavethread.h"
#include "photo.h"
#ifdef Q_OS_LINUX
#include <QProcess>
#include <Magick++.h>
using namespace Magick;
#endif
#ifdef Q_OS_WIN
#include "windows.h"
#endif
Photo::Photo(QObject *parent) : QObject(parent)
{
}
Photo::Photo(QJsonObject json, QObject *parent) : QObject(parent)
{
this->json = json;
id = json["id"].toString();
width = json["width"].toInt();
height = json["height"].toInt();
QJsonObject urls = json["urls"].toObject();
rawUrl = QUrl(urls["raw"].toString());
fullUrl = QUrl(urls["full"].toString());
regularUrl = QUrl(urls["regular"].toString());
smallUrl = QUrl(urls["small"].toString());
thumbUrl = QUrl(urls["thumb"].toString());
QJsonObject links = json["links"].toObject();
apiUrl = QUrl(links["self"].toString());
pageUrl = QUrl(links["html"].toString());
downloadUrl = QUrl(links["download"].toString());
QJsonObject user = json["user"].toObject();
userFullName = user["name"].toString();
}
void Photo::download()
{
QNetworkAccessManager *network = new QNetworkAccessManager;
reply = network->get(QNetworkRequest(fullUrl));
PhotoSaveThread *thread = new PhotoSaveThread(this);
connect(reply, &QNetworkReply::downloadProgress, this, &Photo::downloadProgress);
connect(reply, &QNetworkReply::finished, this, &Photo::downloaded);
connect(reply, &QNetworkReply::finished, thread, &PhotoSaveThread::save);
connect(reply, static_cast<void(QNetworkReply::*)(QNetworkReply::NetworkError)>(&QNetworkReply::error), this, &Photo::downloadFailed);
connect(thread, &PhotoSaveThread::saved, this, &Photo::saved);
}
void Photo::save()
{
// Save photo in full size
QFile *file = new QFile;
file->setFileName(getSaveFileName());
file->open(QIODevice::ReadWrite); // Create file if not exists
file->write(reply->readAll());
emit saveProgress(1, 10);
// Save JSON
QFile jsonFile(getJsonFileName());
if (jsonFile.open(QIODevice::ReadWrite)) {
QJsonDocument doc(json);
QTextStream stream(&jsonFile);
stream << doc.toJson(QJsonDocument::Compact) << endl;
stream << endl;
}
emit saveProgress(2, 10);
#ifdef Q_OS_LINUX
QDir *dir = new QDir;
dir->mkpath(getWallpaperImagesPath()); // Create directory if not exists
generateWallpaperImage(3840, 2160); // 4K/UHD, popular desktop monitor resolution
emit saveProgress(3, 10);
generateWallpaperImage(2560, 1440); // 2K/QHD, popular desktop monitor resolution
emit saveProgress(4, 10);
generateWallpaperImage(1920, 1080); // FHD, most popular desktop and gaming laptop screen resolution
emit saveProgress(5, 10);
generateWallpaperImage(1366, 768); // HD, most popular laptop screen resolution
emit saveProgress(6, 10);
generateWallpaperImage(1280, 1024); // Legacy desktop monitor resolution
emit saveProgress(7, 10);
generateWallpaperImage(1024, 768); // Legacy laptop screen resolution
emit saveProgress(8, 10);
generateWallpaperImage(400, 250, getWallpaperScreenshotFileName()); // Screenshot used in desktop configuration
emit saveProgress(9, 10);
QFile metafile(getWallpaperMetadataFileName());
if (metafile.open(QIODevice::ReadWrite)) {
QTextStream stream(&metafile);
stream << "[Desktop Entry]" << endl;
stream << "Encoding=UTF-8" << endl;
stream << "Name=Unsplash " << id << endl;
stream << "X-KDE-PluginInfo-Name=Unsplash " << id << endl;
stream << "X-KDE-PluginInfo-Author=" << userFullName << endl;
stream << "X-KDE-PluginInfo-License=CC0" << endl;
stream << "X-KDE-PluginInfo-Website=https://unsplash.com/" << endl;
stream << endl;
}
emit saveProgress(10, 10);
#endif
emit saved();
emit localStatusChanged(true);
}
#ifdef Q_OS_LINUX
void Photo::generateWallpaperImage(int width, int height, QString path)
{
int resizeWidth;
int resizeHeight;
if (width * this->height > this->width * height) {
resizeWidth = width;
resizeHeight = (int) (resizeWidth * this->height * 1.0 / this->width + 0.5); // Round up
} else {
resizeHeight = height;
resizeWidth = (int) (resizeHeight * this->width * 1.0 / this->height + 0.5); // Round up
}
QFile *file = new QFile;
file->setFileName(getWallpaperImageFileName(width, height));
file->open(QIODevice::ReadWrite); // Create file if not exists
Image image;
try {
// Read full size photo
image.read(getSaveFileName().toUtf8().constData());
image.resize(Geometry(resizeWidth, resizeHeight));
image.crop(Geometry(width, height, (resizeWidth - width) / 2, (resizeHeight - height) / 2));
if (path.isNull() || path.isEmpty()) {
image.write(getWallpaperImageFileName(width, height).toUtf8().constData());
} else {
image.write(path.toUtf8().constData());
}
} catch ( Exception &error ) {
qDebug() << "Caught exception: " << error.what();
return;
}
}
#endif // Q_OS_LINUX
void Photo::remove()
{
#ifdef Q_OS_LINUX
// Remove wallpaper directory
QDir dir(getWallpaperPath());
dir.removeRecursively();
#endif
// Remove JSON
QFile jsonFile(getJsonFileName());
jsonFile.remove();
// Remove photo
QFile file(getSaveFileName());
file.remove();
emit localStatusChanged(false);
}
QString Photo::getId()
{
return id;
}
int Photo::getWidth()
{
return width;
}
int Photo::getHeight()
{
return height;
}
QUrl Photo::getRawUrl()
{
return rawUrl;
}
QUrl Photo::getFullUrl()
{
return fullUrl;
}
QUrl Photo::getRegularUrl()
{
return regularUrl;
}
QUrl Photo::getSmallUrl()
{
return smallUrl;
}
QUrl Photo::getThumbUrl()
{
return thumbUrl;
}
QUrl Photo::getApiUrl()
{
return apiUrl;
}
QUrl Photo::getPageUrl()
{
return pageUrl;
}
QUrl Photo::getDownloadUrl()
{
return downloadUrl;
}
QString Photo::getUserFullName()
{
return userFullName;
}
QJsonObject Photo::getJson()
{
return json;
}
/**
* @brief Path of saved full size photo
* Usually it is /home/<username>/Pictures/Unwallpaper/<photoId>.jpg
* @return
*/
QString Photo::getSaveFileName()
{
return FileSystem::getSavePath() + QString("/") + id + QString(".jpg");
}
/**
* @brief Path of saved JSON object
* Usually it is /home/<username>/.local/share/unwallpaper/data/photos/<photoId>.json
* @return
*/
QString Photo::getJsonFileName()
{
return FileSystem::getAppDataPath() + QString("/data/photos/") + id + QString(".json");
}
QString Photo::getWallpaperPath ()
{
return FileSystem::getWallpaperPath() + QString("/Unwallpaper-") + id;
}
QString Photo::getWallpaperContentsPath ()
{
return getWallpaperPath() + QString("/contents");
}
QString Photo::getWallpaperImagesPath ()
{
return getWallpaperContentsPath() + QString("/images");
}
QString Photo::getWallpaperImageFileName(int width, int height)
{
return getWallpaperImagesPath() + QString("/") + QString::number(width) + QString("x") + QString::number(height) + QString(".jpg");
}
QString Photo::getWallpaperMetadataFileName()
{
return getWallpaperPath() + QString("/metadata.desktop");
}
QString Photo::getWallpaperScreenshotFileName()
{
return getWallpaperContentsPath() + QString("/screenshot.jpg");
}
bool Photo::isLocal()
{
QFile file(getSaveFileName());
QFile json(getJsonFileName());
return file.exists() && json.exists();
}
void Photo::setId(QString id)
{
this->id = id;
emit idChanged(id);
}
void Photo::setWidth(int width)
{
this->width = width;
emit widthChanged(width);
}
void Photo::setHeight(int height)
{
this->height = height;
emit heightChanged(height);
}
void Photo::setRawUrl(QUrl rawUrl)
{
this->rawUrl = rawUrl;
emit rawUrlChanged(rawUrl);
}
void Photo::setFullUrl(QUrl fullUrl)
{
this->fullUrl = fullUrl;
emit fullUrlChanged(fullUrl);
}
void Photo::setRegularUrl(QUrl regularUrl)
{
this->regularUrl = regularUrl;
emit regularUrlChanged(regularUrl);
}
void Photo::setSmallUrl(QUrl smallUrl)
{
this->smallUrl = smallUrl;
emit smallUrlChanged(smallUrl);
}
void Photo::setThumbUrl(QUrl thumbUrl)
{
this->thumbUrl = thumbUrl;
emit thumbUrlChanged(thumbUrl);
}
void Photo::setApiUrl(QUrl apiUrl)
{
this->apiUrl = apiUrl;
emit apiUrlChanged(apiUrl);
}
void Photo::setPageUrl(QUrl pageUrl)
{
this->pageUrl = pageUrl;
emit pageUrlChanged(pageUrl);
}
void Photo::setDownloadUrl(QUrl downloadUrl)
{
this->downloadUrl = downloadUrl;
emit downloadUrlChanged(downloadUrl);
}
void Photo::setUserFullName(QString userFullName)
{
this->userFullName = userFullName;
emit userFullNameChanged(userFullName);
}
void Photo::setWallpaper()
{
#ifdef Q_OS_LINUX
// GNOME
process.start(QString("gsettings set org.gnome.desktop.background picture-uri ") + getSaveFileName());
process.waitForFinished();
process.start(QString("gsettings set org.gnome.desktop.background picture-options wallpaper"));
process.waitForFinished();
// KDE
// NOTE: KDE 5 API is still changing, it may not work on all KDE versions
QString script;
script += "var allDesktops=desktops();";
script += "for(var i=0;i<allDesktops.length;i++){";
script += " var d=allDesktops[i];";
script += " d.wallpaperPlugin=\"org.kde.image\";";
script += " d.currentConfigGroup=Array(\"Wallpaper\",\"org.kde.image\",\"General\");";
script += " d.writeConfig(\"Image\",\"file://";
script += getSaveFileName();
script += "\");";
script += "}";
QStringList args;
args << "org.kde.plasmashell"
<< "/PlasmaShell"
<< "org.kde.PlasmaShell.evaluateScript"
<< script;
process.start(QString("qdbus"), args);
process.waitForFinished();
#endif
#ifdef Q_OS_WIN
// Windows
// Set wallpaper style
HKEY hKey = NULL;
PWSTR style;
style = L"10"; // Fill
DWORD cbData = lstrlen(style) * sizeof(*style);
RegOpenKeyEx(HKEY_CURRENT_USER, L"Control Panel\\Desktop", 0, KEY_READ | KEY_WRITE, &hKey);
RegSetValueEx(hKey, L"WallpaperStyle", 0, REG_SZ, reinterpret_cast<const BYTE *>(style), cbData);
RegCloseKey(hKey);
// Set wallpaper image
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, (PVOID) getSaveFileName().utf16(), SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE);
#endif
#ifdef Q_OS_MACOS
// macOS
#endif
}