File indexing completed on 2025-03-09 03:52:14

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-09-28
0007  * Description : a tool to export image to a KIO accessible
0008  *               location
0009  *
0010  * SPDX-FileCopyrightText: 2006-2009 by Johannes Wienke <languitar at semipol dot de>
0011  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "ftexportwindow.h"
0018 
0019 // Qt includes
0020 
0021 #include <QCloseEvent>
0022 #include <QMessageBox>
0023 
0024 // KDE includes
0025 
0026 #include <klocalizedstring.h>
0027 #include <ksharedconfig.h>
0028 #include <kconfiggroup.h>
0029 #include <kio/job.h>
0030 #include <kio/copyjob.h>
0031 
0032 // Local includes
0033 
0034 #include "digikam_debug.h"
0035 #include "digikam_version.h"
0036 #include "ditemslist.h"
0037 #include "ftexportwidget.h"
0038 
0039 namespace DigikamGenericFileTransferPlugin
0040 {
0041 
0042 class Q_DECL_HIDDEN FTExportWindow::Private
0043 {
0044 public:
0045 
0046     explicit Private()
0047     {
0048         exportWidget = nullptr;
0049     }
0050 
0051     const static QString TARGET_URL_PROPERTY;
0052     const static QString HISTORY_URL_PROPERTY;
0053     const static QString CONFIG_GROUP;
0054 
0055     FTExportWidget* exportWidget;
0056 };
0057 
0058 const QString FTExportWindow::Private::TARGET_URL_PROPERTY  = QLatin1String("targetUrl");
0059 const QString FTExportWindow::Private::HISTORY_URL_PROPERTY = QLatin1String("historyUrls");
0060 const QString FTExportWindow::Private::CONFIG_GROUP         = QLatin1String("KioExport");
0061 
0062 FTExportWindow::FTExportWindow(DInfoInterface* const iface, QWidget* const /*parent*/)
0063     : WSToolDialog(nullptr, QLatin1String("Kio Export Dialog")),
0064       d           (new Private)
0065 {
0066     d->exportWidget = new FTExportWidget(iface, this);
0067     setMainWidget(d->exportWidget);
0068 
0069     // -- Window setup ------------------------------------------------------
0070 
0071     setWindowTitle(i18nc("@title:window", "Export to Remote Storage"));
0072     setModal(false);
0073 
0074     startButton()->setText(i18nc("@action:button", "Start Export"));
0075     startButton()->setToolTip(i18nc("@info:tooltip, button", "Start export to the specified target"));
0076 
0077     connect(startButton(), SIGNAL(clicked()),
0078             this, SLOT(slotUpload()));
0079 
0080     connect(this, SIGNAL(finished(int)),
0081             this, SLOT(slotFinished()));
0082 
0083     connect(d->exportWidget->imagesList(), SIGNAL(signalImageListChanged()),
0084             this, SLOT(slotImageListChanged()));
0085 
0086     connect(d->exportWidget, SIGNAL(signalTargetUrlChanged(QUrl)),
0087             this, SLOT(slotTargetUrlChanged(QUrl)));
0088 
0089     // -- initial sync ------------------------------------------------------
0090 
0091     restoreSettings();
0092     updateUploadButton();
0093 }
0094 
0095 FTExportWindow::~FTExportWindow()
0096 {
0097     delete d;
0098 }
0099 
0100 void FTExportWindow::slotFinished()
0101 {
0102     saveSettings();
0103     d->exportWidget->imagesList()->listView()->clear();
0104 }
0105 
0106 void FTExportWindow::closeEvent(QCloseEvent* e)
0107 {
0108     if (!e)
0109     {
0110         return;
0111     }
0112 
0113     slotFinished();
0114     e->accept();
0115 }
0116 
0117 void FTExportWindow::reactivate()
0118 {
0119     d->exportWidget->imagesList()->loadImagesFromCurrentSelection();
0120     show();
0121 }
0122 
0123 void FTExportWindow::restoreSettings()
0124 {
0125     KSharedConfigPtr config = KSharedConfig::openConfig();
0126     KConfigGroup group      = config->group(d->CONFIG_GROUP);
0127     d->exportWidget->setHistory(group.readEntry(d->HISTORY_URL_PROPERTY, QList<QUrl>()));
0128     d->exportWidget->setTargetUrl(group.readEntry(d->TARGET_URL_PROPERTY, QUrl()));
0129 }
0130 
0131 void FTExportWindow::saveSettings()
0132 {
0133     KSharedConfigPtr config = KSharedConfig::openConfig();
0134     KConfigGroup group      = config->group(d->CONFIG_GROUP);
0135     group.writeEntry(d->HISTORY_URL_PROPERTY, d->exportWidget->history());
0136     group.writeEntry(d->TARGET_URL_PROPERTY,  d->exportWidget->targetUrl().url());
0137 }
0138 
0139 void FTExportWindow::slotImageListChanged()
0140 {
0141     updateUploadButton();
0142 }
0143 
0144 void FTExportWindow::slotTargetUrlChanged(const QUrl& target)
0145 {
0146     Q_UNUSED(target);
0147     updateUploadButton();
0148 }
0149 
0150 void FTExportWindow::updateUploadButton()
0151 {
0152     bool listNotEmpty = !d->exportWidget->imagesList()->imageUrls().isEmpty();
0153     startButton()->setEnabled(listNotEmpty && d->exportWidget->targetUrl().isValid());
0154 
0155     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "Updated upload button with listNotEmpty = "
0156                                      << listNotEmpty << ", targetUrl().isValid() = "
0157                                      << d->exportWidget->targetUrl().isValid();
0158 }
0159 
0160 void FTExportWindow::slotCopyingDone(KIO::Job* job,
0161                                      const QUrl& from,
0162                                      const QUrl& to,
0163                                      const QDateTime& mtime,
0164                                      bool directory,
0165                                      bool renamed)
0166 {
0167     Q_UNUSED(job);
0168     Q_UNUSED(to);
0169     Q_UNUSED(mtime);
0170     Q_UNUSED(directory);
0171     Q_UNUSED(renamed);
0172 
0173     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "copied " << to.toDisplayString();
0174 
0175     d->exportWidget->imagesList()->removeItemByUrl(from);
0176 }
0177 
0178 void FTExportWindow::slotCopyingFinished(KJob* job)
0179 {
0180     Q_UNUSED(job);
0181 
0182     setEnabled(true);
0183 
0184     if (!d->exportWidget->imagesList()->imageUrls().isEmpty())
0185     {
0186         QMessageBox::information(this, i18nc("@title:window", "Upload not Completed"),
0187                                  i18n("Some of the images have not been transferred "
0188                                       "and are still in the list. "
0189                                       "You can retry to export these images now."));
0190     }
0191 }
0192 
0193 void FTExportWindow::slotUpload()
0194 {
0195     saveSettings();
0196 
0197     // start copying and react on signals
0198 
0199     setEnabled(false);
0200     KIO::CopyJob* const copyJob = KIO::copy(d->exportWidget->imagesList()->imageUrls(),
0201                                             d->exportWidget->targetUrl());
0202 
0203     connect(copyJob, SIGNAL(copyingDone(KIO::Job*,QUrl,QUrl,QDateTime,bool,bool)),
0204             this, SLOT(slotCopyingDone(KIO::Job*,QUrl,QUrl,QDateTime,bool,bool)));
0205 
0206     connect(copyJob, SIGNAL(result(KJob*)),
0207             this, SLOT(slotCopyingFinished(KJob*)));
0208 }
0209 
0210 } // namespace DigikamGenericFileTransferPlugin
0211 
0212 #include "moc_ftexportwindow.cpp"