File indexing completed on 2025-01-05 03:53:35

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2012-02-12
0007  * Description : a tool to export images to IPFS web service
0008  *
0009  * SPDX-FileCopyrightText: 2018      by Amar Lakshya <amar dot lakshya at xaviers dot edu dot in>
0010  * SPDX-FileCopyrightText: 2018-2020 by Caulier Gilles <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "ipfswindow.h"
0017 
0018 // Qt includes
0019 
0020 #include <QDesktopServices>
0021 #include <QInputDialog>
0022 #include <QCloseEvent>
0023 #include <QMessageBox>
0024 #include <QBoxLayout>
0025 #include <QWindow>
0026 
0027 // KDE includes
0028 
0029 #include <klocalizedstring.h>
0030 #include <ksharedconfig.h>
0031 #include <kconfiggroup.h>
0032 
0033 // Local includes
0034 
0035 #include "digikam_debug.h"
0036 #include "digikam_version.h"
0037 
0038 namespace DigikamGenericIpfsPlugin
0039 {
0040 
0041 class Q_DECL_HIDDEN IpfsWindow::Private
0042 {
0043 public:
0044 
0045     explicit Private()
0046       : list (nullptr),
0047         api  (nullptr)
0048     {
0049     }
0050 
0051     IpfsImagesList* list;
0052     IpfsTalker*     api;
0053 
0054     /**
0055      * Contains the ipfs username if API authorized.
0056      * If not, username is null.
0057      */
0058     QString         username;
0059 };
0060 
0061 IpfsWindow::IpfsWindow(DInfoInterface* const iface, QWidget* const /*parent*/)
0062     : WSToolDialog(nullptr, QLatin1String("IPFS Dialog")),
0063       d           (new Private)
0064 {
0065     d->api = new IpfsTalker(this);
0066 
0067     // Connect API signals
0068 
0069     connect(d->api, &IpfsTalker::progress,
0070             this, &IpfsWindow::apiProgress);
0071 
0072     connect(d->api, &IpfsTalker::success,
0073             this, &IpfsWindow::apiSuccess);
0074 
0075     connect(d->api, &IpfsTalker::error,
0076             this, &IpfsWindow::apiError);
0077 
0078     connect(d->api, &IpfsTalker::busy,
0079             this, &IpfsWindow::apiBusy);
0080 
0081     // | List | Auth |
0082 
0083     auto* const mainLayout = new QHBoxLayout;
0084     auto* const mainWidget = new QWidget(this);
0085     mainWidget->setLayout(mainLayout);
0086     setMainWidget(mainWidget);
0087 
0088     d->list = new IpfsImagesList;
0089     d->list->setIface(iface);
0090     d->list->loadImagesFromCurrentSelection();
0091     mainLayout->addWidget(d->list);
0092 
0093     /**
0094      * |  Logged in as:  |
0095      * | <Not logged in> |
0096      * |     Forget      |
0097      */
0098 
0099     auto* const authLayout = new QVBoxLayout;
0100     mainLayout->addLayout(authLayout);
0101 
0102     authLayout->insertStretch(-1, 1);
0103 
0104     /**
0105      * Add anonymous upload button
0106      * Connect UI signals
0107      */
0108 
0109     connect(startButton(), &QPushButton::clicked,
0110             this, &IpfsWindow::slotUpload);
0111 
0112     connect(this, &IpfsWindow::finished,
0113             this, &IpfsWindow::slotFinished);
0114 
0115     connect(this, &IpfsWindow::cancelClicked,
0116             this, &IpfsWindow::slotCancel);
0117 
0118     setWindowIcon(QIcon::fromTheme(QString::fromLatin1("ipfs")));
0119     setWindowTitle(i18nc("@title:window", "Export to IPFS"));
0120     setModal(false);
0121 
0122     startButton()->setText(i18nc("@action:button", "Upload"));
0123     startButton()->setToolTip(i18nc("@info:tooltip, button", "Start upload to IPFS"));
0124     startButton()->setEnabled(true);
0125 
0126     // Only used if not overwritten by readSettings()
0127 
0128     resize(650, 320);
0129     readSettings();
0130 }
0131 
0132 IpfsWindow::~IpfsWindow()
0133 {
0134     saveSettings();
0135     delete d;
0136 }
0137 
0138 void IpfsWindow::reactivate()
0139 {
0140     d->list->loadImagesFromCurrentSelection();
0141     show();
0142 }
0143 
0144 void IpfsWindow::slotUpload()
0145 {
0146     QList<const IpfsImagesListViewItem*> pending = d->list->getPendingItems();
0147 
0148     if (pending.isEmpty())
0149     {
0150         return;
0151     }
0152 
0153     for (const auto& item : qAsConst(pending))
0154     {
0155         IpfsTalkerAction action;
0156         action.type               = IpfsTalkerActionType::IMG_UPLOAD;
0157         action.upload.imgpath     = item->url().toLocalFile();
0158         action.upload.title       = item->Title();
0159         action.upload.description = item->Description();
0160 
0161         d->api->queueWork(action);
0162     }
0163 }
0164 
0165 void IpfsWindow::slotFinished()
0166 {
0167     saveSettings();
0168 }
0169 
0170 void IpfsWindow::slotCancel()
0171 {
0172     d->api->cancelAllWork();
0173 }
0174 
0175 /*
0176 void IpfsWindow::apiAuthorized(bool success, const QString& username)
0177 {
0178     if (success)
0179     {
0180         d->username = username;
0181         d->userLabel->setText(d->username);
0182         d->forgetButton->setEnabled(true);
0183         return;
0184     }
0185 
0186     d->username = QString();
0187     d->userLabel->setText(i18n("<Not logged in>"));
0188     d->forgetButton->setEnabled(false);
0189 }
0190 */
0191 
0192 /*
0193 void IpfsWindow::apiAuthError(const QString& msg)
0194 {
0195     QMessageBox::critical(this,
0196                           i18nc("@title:window", "Authorization Failed"),
0197                           i18n("Failed to log into IPFS: %1\n", msg));
0198 }
0199 */
0200 
0201 void IpfsWindow::apiProgress(unsigned int /*percent*/, const IpfsTalkerAction& action)
0202 {
0203     d->list->processing(QUrl::fromLocalFile(action.upload.imgpath));
0204 }
0205 
0206 void IpfsWindow::apiRequestPin(const QUrl& url)
0207 {
0208     QDesktopServices::openUrl(url);
0209 }
0210 
0211 void IpfsWindow::apiSuccess(const IpfsTalkerResult& result)
0212 {
0213     d->list->slotSuccess(result);
0214 }
0215 
0216 void IpfsWindow::apiError(const QString& msg, const IpfsTalkerAction& action)
0217 {
0218     d->list->processed(QUrl::fromLocalFile(action.upload.imgpath), false);
0219 
0220     // 1 here because the current item is still in the queue.
0221 
0222     if (d->api->workQueueLength() <= 1)
0223     {
0224         QMessageBox::critical(this,
0225                               i18nc("@title:window", "Uploading Failed"),
0226                               i18n("Failed to upload photo to IPFS: %1\n", msg));
0227         return;
0228     }
0229 
0230     QMessageBox::StandardButton cont = QMessageBox::question(this,
0231                                            i18nc("@title:window", "Uploading Failed"),
0232                                            i18n("Failed to upload photo to IPFS: %1\n"
0233                                                 "Do you want to continue?", msg));
0234 
0235     if (cont != QMessageBox::Yes)
0236     {
0237         d->api->cancelAllWork();
0238     }
0239 }
0240 
0241 void IpfsWindow::apiBusy(bool busy)
0242 {
0243     setCursor(busy ? Qt::WaitCursor : Qt::ArrowCursor);
0244     startButton()->setEnabled(!busy);
0245 }
0246 
0247 void IpfsWindow::closeEvent(QCloseEvent* e)
0248 {
0249     if (!e)
0250     {
0251         return;
0252     }
0253 
0254     slotFinished();
0255     e->accept();
0256 }
0257 
0258 void IpfsWindow::readSettings()
0259 {
0260     KSharedConfigPtr config  = KSharedConfig::openConfig();
0261     KConfigGroup groupAuth   = config->group(QLatin1String("IPFS Auth"));
0262     d->username              = groupAuth.readEntry("UserName", QString());
0263 /*
0264     apiAuthorized(!d->username.isEmpty(), d->username);
0265 */
0266 }
0267 
0268 void IpfsWindow::saveSettings()
0269 {
0270     KSharedConfigPtr config  = KSharedConfig::openConfig();
0271     KConfigGroup groupAuth   = config->group(QLatin1String("IPFS Auth"));
0272     groupAuth.writeEntry("UserName", d->username);
0273 }
0274 
0275 } // namespace DigikamGenericIpfsPlugin
0276 
0277 #include "moc_ipfswindow.cpp"