1 | #pragma once |
2 | |
3 | /**************************************************************************************** |
4 | ** GitQlient is an application to manage and operate one or several Git repositories. With |
5 | ** GitQlient you will be able to add commits, branches and manage all the options Git provides. |
6 | ** Copyright (C) 2021 Francesc Martinez |
7 | ** |
8 | ** LinkedIn: www.linkedin.com/in/cescmm/ |
9 | ** Web: www.francescmm.com |
10 | ** |
11 | ** This program is free software; you can redistribute it and/or |
12 | ** modify it under the terms of the GNU Lesser General Public |
13 | ** License as published by the Free Software Foundation; either |
14 | ** version 2 of the License, or (at your option) any later version. |
15 | ** |
16 | ** This program is distributed in the hope that it will be useful, |
17 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
19 | ** Lesser General Public License for more details. |
20 | ** |
21 | ** You should have received a copy of the GNU Lesser General Public |
22 | ** License along with this library; if not, write to the Free Software |
23 | ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
24 | ***************************************************************************************/ |
25 | #include <CircularPixmap.h> |
26 | |
27 | #include <QString> |
28 | #include <QLabel> |
29 | #include <QStandardPaths> |
30 | #include <QNetworkAccessManager> |
31 | #include <QNetworkReply> |
32 | #include <QNetworkRequest> |
33 | #include <QFile> |
34 | #include <QDir> |
35 | #include <QPointer> |
36 | |
37 | inline void storeCreatorAvatar(QNetworkAccessManager *manager, QNetworkReply *reply, QLabel *avatar, |
38 | const QString &fileName) |
39 | { |
40 | const auto data = reply->readAll(); |
41 | const auto cache = QStandardPaths::writableLocation(QStandardPaths::CacheLocation); |
42 | QDir dir(cache); |
43 | |
44 | if (!dir.exists()) |
45 | dir.mkpath(cache); |
46 | |
47 | const auto path = QString("%1/%2" ).arg(dir.absolutePath(), fileName); |
48 | |
49 | if (QFile file(path); file.open(QIODevice::WriteOnly)) |
50 | { |
51 | file.write(data); |
52 | file.close(); |
53 | |
54 | QPixmap img(path); |
55 | |
56 | if (!img.isNull()) |
57 | { |
58 | img = img.scaled(50, 50, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
59 | |
60 | avatar->setPixmap(img); |
61 | } |
62 | } |
63 | |
64 | reply->deleteLater(); |
65 | manager->deleteLater(); |
66 | } |
67 | |
68 | inline QPointer<CircularPixmap> createAvatar(const QString &userName, const QString &avatarUrl, |
69 | const QSize &avatarSize = QSize(50, 50)) |
70 | { |
71 | const auto fileName |
72 | = QString("%1/%2" ).arg(QStandardPaths::writableLocation(QStandardPaths::CacheLocation), userName); |
73 | QPointer<CircularPixmap> avatar = new CircularPixmap(avatarSize); |
74 | avatar->setObjectName("Avatar" ); |
75 | |
76 | if (!QFile(fileName).exists()) |
77 | { |
78 | const auto manager = new QNetworkAccessManager(); |
79 | QNetworkRequest request; |
80 | request.setUrl(avatarUrl); |
81 | request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, true); |
82 | const auto reply = manager->get(request); |
83 | QObject::connect(reply, &QNetworkReply::finished, [manager, reply, avatar, userName]() { |
84 | if (avatar) |
85 | storeCreatorAvatar(manager, reply, avatar, userName); |
86 | }); |
87 | } |
88 | else |
89 | { |
90 | QPixmap img(fileName); |
91 | |
92 | if (!img.isNull()) |
93 | { |
94 | img = img.scaled(avatarSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
95 | |
96 | avatar->setPixmap(img); |
97 | } |
98 | } |
99 | |
100 | return avatar; |
101 | } |
102 | |