Changeset View
Changeset View
Standalone View
Standalone View
ipfs/ipfswindow.cpp
- This file was added.
1 | /* ============================================================ | ||||
---|---|---|---|---|---|
2 | * | ||||
3 | * This file is a part of KDE project | ||||
4 | * | ||||
5 | * | ||||
6 | * Date : 2016-06-06 | ||||
7 | * Description : a kipi plugin to export images to the IPFS web service | ||||
8 | * | ||||
9 | * Copyright (C) 2018 by Amar Lakshya <amar dot lakshya at xaviers dot edu dot in> | ||||
10 | * | ||||
11 | * This program is free software; you can redistribute it | ||||
12 | * and/or modify it under the terms of the GNU General | ||||
13 | * Public License as published by the Free Software Foundation; | ||||
14 | * either version 2, 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 | ||||
19 | * GNU General Public License for more details. | ||||
20 | * | ||||
21 | * ============================================================ */ | ||||
22 | | ||||
23 | #include "ipfswindow.h" | ||||
24 | | ||||
25 | // Qt includes | ||||
26 | | ||||
27 | #include <QDesktopServices> | ||||
28 | #include <QInputDialog> | ||||
29 | #include <QCloseEvent> | ||||
30 | #include <QMessageBox> | ||||
31 | #include <QBoxLayout> | ||||
32 | #include <QWindow> | ||||
33 | | ||||
34 | // KDE includes | ||||
35 | | ||||
36 | #include <klocalizedstring.h> | ||||
37 | #include <kwindowconfig.h> | ||||
38 | #include <kconfig.h> | ||||
39 | | ||||
40 | // Local includes | ||||
41 | | ||||
42 | #include "kipiplugins_debug.h" | ||||
43 | #include "kpimageinfo.h" | ||||
44 | #include "kpaboutdata.h" | ||||
45 | #include "kpversion.h" | ||||
46 | | ||||
47 | namespace KIPIIPFSPlugin | ||||
48 | { | ||||
49 | | ||||
50 | IPFSWindow::IPFSWindow(QWidget* const /*parent*/) | ||||
51 | : KPToolDialog(0) | ||||
52 | { | ||||
53 | api = new IPFSGLOBALUPLOADAPI(this); | ||||
54 | | ||||
55 | /* Connect API signals */ | ||||
56 | | ||||
57 | connect(api, &IPFSGLOBALUPLOADAPI::progress, this, &IPFSWindow::apiProgress); | ||||
58 | connect(api, &IPFSGLOBALUPLOADAPI::success, this, &IPFSWindow::apiSuccess); | ||||
59 | connect(api, &IPFSGLOBALUPLOADAPI::error, this, &IPFSWindow::apiError); | ||||
60 | connect(api, &IPFSGLOBALUPLOADAPI::busy, this, &IPFSWindow::apiBusy); | ||||
61 | | ||||
62 | /* | List | Auth | */ | ||||
63 | auto* mainLayout = new QHBoxLayout; | ||||
64 | auto* mainWidget = new QWidget(this); | ||||
65 | mainWidget->setLayout(mainLayout); | ||||
66 | this->setMainWidget(mainWidget); | ||||
67 | | ||||
68 | this->list = new IPFSImagesList; | ||||
69 | mainLayout->addWidget(list); | ||||
70 | | ||||
71 | /* | Logged in as: | | ||||
72 | * | <Not logged in> | | ||||
73 | * | Forget | */ | ||||
74 | | ||||
75 | | ||||
76 | | ||||
77 | auto* authLayout = new QVBoxLayout; | ||||
78 | mainLayout->addLayout(authLayout); | ||||
79 | | ||||
80 | authLayout->insertStretch(-1, 1); | ||||
81 | | ||||
82 | /* Add anonymous upload button */ | ||||
83 | /* Connect UI signals */ | ||||
84 | connect(startButton(), &QPushButton::clicked, | ||||
85 | this, &IPFSWindow::slotUpload); | ||||
86 | connect(this, &IPFSWindow::finished, | ||||
87 | this, &IPFSWindow::slotFinished); | ||||
88 | connect(this, &IPFSWindow::cancelClicked, | ||||
89 | this, &IPFSWindow::slotCancel); | ||||
90 | | ||||
91 | setWindowIcon(QIcon::fromTheme(QString::fromLatin1("kipi-ipfs"))); | ||||
92 | setWindowTitle(i18n("Export to IPFS")); | ||||
93 | setModal(false); | ||||
94 | | ||||
95 | startButton()->setText(i18n("Upload")); | ||||
96 | startButton()->setToolTip(i18n("Start upload to IPFS")); | ||||
97 | startButton()->setEnabled(true); | ||||
98 | | ||||
99 | /* Add about data */ | ||||
100 | | ||||
101 | KPAboutData* const about = new KPAboutData(ki18n("IPFS Export"), | ||||
102 | ki18n("A tool to export images to IPFS web service"), | ||||
103 | ki18n("(c) 2018, Amar Lakshya")); | ||||
104 | | ||||
105 | about->addAuthor(ki18n("Amar Lakshya").toString(), | ||||
106 | ki18n("Author").toString(), | ||||
107 | QString::fromLatin1("amar dot lakshya at xaviers dot edu dot in")); | ||||
108 | | ||||
109 | about->setHandbookEntry(QString::fromLatin1("tool-ipfsexport")); | ||||
110 | setAboutData(about); | ||||
111 | | ||||
112 | /* Only used if not overwritten by readSettings() */ | ||||
113 | resize(650, 320); | ||||
114 | readSettings(); | ||||
115 | } | ||||
116 | | ||||
117 | IPFSWindow::~IPFSWindow() | ||||
118 | { | ||||
119 | saveSettings(); | ||||
120 | } | ||||
121 | | ||||
122 | void IPFSWindow::reactivate() | ||||
123 | { | ||||
124 | list->loadImagesFromCurrentSelection(); | ||||
125 | show(); | ||||
126 | } | ||||
127 | | ||||
128 | void IPFSWindow::slotUpload() | ||||
129 | { | ||||
130 | QList<const IPFSImageListViewItem*> pending = this->list->getPendingItems(); | ||||
131 | | ||||
132 | for (auto item : pending) | ||||
133 | { | ||||
134 | IPFSGLOBALUPLOADAPIAction action; | ||||
135 | action.type = IPFSGLOBALUPLOADAPIActionType::IMG_UPLOAD; | ||||
136 | action.upload.imgpath = item->url().toLocalFile(); | ||||
137 | action.upload.title = item->Title(); | ||||
138 | action.upload.description = item->Description(); | ||||
139 | | ||||
140 | api->queueWork(action); | ||||
141 | } | ||||
142 | } | ||||
143 | | ||||
144 | | ||||
145 | void IPFSWindow::slotFinished() | ||||
146 | { | ||||
147 | saveSettings(); | ||||
148 | } | ||||
149 | | ||||
150 | void IPFSWindow::slotCancel() | ||||
151 | { | ||||
152 | api->cancelAllWork(); | ||||
153 | } | ||||
154 | | ||||
155 | /* void IPFSWindow::apiAuthorized(bool success, const QString& username) */ | ||||
156 | // { | ||||
157 | // if (success) | ||||
158 | // { | ||||
159 | // this->username = username; | ||||
160 | // this->userLabel->setText(this->username); | ||||
161 | // this->forgetButton->setEnabled(true); | ||||
162 | // return; | ||||
163 | // } | ||||
164 | // | ||||
165 | // this->username = QString(); | ||||
166 | // this->userLabel->setText(i18n("<Not logged in>")); | ||||
167 | // this->forgetButton->setEnabled(false); | ||||
168 | /* } */ | ||||
169 | | ||||
170 | /* void IPFSWindow::apiAuthError(const QString& msg) */ | ||||
171 | // { | ||||
172 | // QMessageBox::critical(this, | ||||
173 | // i18n("Authorization Failed"), | ||||
174 | // i18n("Failed to log into IPFS: %1\n", msg)); | ||||
175 | // } | ||||
176 | /* */ | ||||
177 | void IPFSWindow::apiProgress(unsigned int /*percent*/, const IPFSGLOBALUPLOADAPIAction& action) | ||||
178 | { | ||||
179 | list->processing(QUrl::fromLocalFile(action.upload.imgpath)); | ||||
180 | } | ||||
181 | | ||||
182 | void IPFSWindow::apiRequestPin(const QUrl& url) | ||||
183 | { | ||||
184 | QDesktopServices::openUrl(url); | ||||
185 | } | ||||
186 | | ||||
187 | void IPFSWindow::apiSuccess(const IPFSGLOBALUPLOADAPIResult& result) | ||||
188 | { | ||||
189 | list->slotSuccess(result); | ||||
190 | } | ||||
191 | | ||||
192 | void IPFSWindow::apiError(const QString& msg, const IPFSGLOBALUPLOADAPIAction& action) | ||||
193 | { | ||||
194 | list->processed(QUrl::fromLocalFile(action.upload.imgpath), false); | ||||
195 | | ||||
196 | /* 1 here because the current item is still in the queue. */ | ||||
197 | if (api->workQueueLength() <= 1) | ||||
198 | { | ||||
199 | QMessageBox::critical(this, | ||||
200 | i18n("Uploading Failed"), | ||||
201 | i18n("Failed to upload photo to IPFS: %1\n", msg)); | ||||
202 | return; | ||||
203 | } | ||||
204 | | ||||
205 | QMessageBox::StandardButton cont = | ||||
206 | QMessageBox::question(this, | ||||
207 | i18n("Uploading Failed"), | ||||
208 | i18n("Failed to upload photo to IPFS: %1\n" | ||||
209 | "Do you want to continue?", msg)); | ||||
210 | | ||||
211 | if (cont != QMessageBox::Yes) | ||||
212 | api->cancelAllWork(); | ||||
213 | } | ||||
214 | | ||||
215 | void IPFSWindow::apiBusy(bool busy) | ||||
216 | { | ||||
217 | setCursor(busy ? Qt::WaitCursor : Qt::ArrowCursor); | ||||
218 | startButton()->setEnabled(!busy); | ||||
219 | } | ||||
220 | | ||||
221 | void IPFSWindow::closeEvent(QCloseEvent* e) | ||||
222 | { | ||||
223 | if (!e) | ||||
224 | return; | ||||
225 | | ||||
226 | slotFinished(); | ||||
227 | e->accept(); | ||||
228 | } | ||||
229 | | ||||
230 | void IPFSWindow::readSettings() | ||||
231 | { | ||||
232 | KConfig config(QString::fromLatin1("kipirc")); | ||||
233 | KConfigGroup groupAuth = config.group("IPFS Auth"); | ||||
234 | username = groupAuth.readEntry("username", QString()); | ||||
235 | // apiAuthorized(!username.isEmpty(), username); | ||||
236 | | ||||
237 | winId(); | ||||
238 | KConfigGroup groupDialog = config.group("IPFS Dialog"); | ||||
239 | KWindowConfig::restoreWindowSize(windowHandle(), groupDialog); | ||||
240 | resize(windowHandle()->size()); | ||||
241 | } | ||||
242 | | ||||
243 | void IPFSWindow::saveSettings() | ||||
244 | { | ||||
245 | KConfig config(QString::fromLatin1("kipirc")); | ||||
246 | KConfigGroup groupAuth = config.group("IPFS Auth"); | ||||
247 | groupAuth.writeEntry("username", username); | ||||
248 | | ||||
249 | KConfigGroup groupDialog = config.group("IPFS Dialog"); | ||||
250 | KWindowConfig::saveWindowSize(windowHandle(), groupDialog); | ||||
251 | config.sync(); | ||||
252 | } | ||||
253 | | ||||
254 | } // namespace KIPIIPFSPlugin |