File indexing completed on 2025-01-19 03:52:56

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2019-03-27
0007  * Description : a tool to export items to a local storage
0008  *
0009  * SPDX-FileCopyrightText: 2006-2009 by Johannes Wienke <languitar at semipol dot de>
0010  * SPDX-FileCopyrightText: 2011-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  * SPDX-FileCopyrightText: 2019-2020 by Maik Qualmann <metzpinguin at gmail dot com>
0012  *
0013  * SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  * ============================================================ */
0016 
0017 #include "fcexportwindow.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 
0030 // Local includes
0031 
0032 #include "digikam_debug.h"
0033 #include "digikam_version.h"
0034 #include "fcexportwidget.h"
0035 #include "ditemslist.h"
0036 #include "fcthread.h"
0037 #include "fctask.h"
0038 
0039 namespace DigikamGenericFileCopyPlugin
0040 {
0041 
0042 class Q_DECL_HIDDEN FCExportWindow::Private
0043 {
0044 public:
0045 
0046     explicit Private()
0047       : exportWidget(nullptr),
0048         thread      (nullptr)
0049     {
0050     }
0051 
0052     const static QString TARGET_URL_PROPERTY;
0053     const static QString TARGET_SIDECARS;
0054     const static QString TARGET_WRITE_METADATA_TO_FILE;
0055     const static QString TARGET_OVERWRITE;
0056     const static QString TARGET_ALBUMPATH;
0057     const static QString TARGET_BEHAVIOR;
0058     const static QString CONFIG_GROUP;
0059 
0060     const static QString CHANGE_IMAGE_PROPERTIES;
0061     const static QString IMAGE_RESIZE;
0062     const static QString IMAGE_FORMAT;
0063     const static QString IMAGE_COMPRESSION;
0064     const static QString REMOVE_METADATA;
0065 
0066     FCExportWidget*      exportWidget;
0067     FCThread*            thread;
0068 };
0069 
0070 const QString FCExportWindow::Private::TARGET_URL_PROPERTY           = QLatin1String("targetUrl");
0071 const QString FCExportWindow::Private::TARGET_SIDECARS               = QLatin1String("sidecars");
0072 const QString FCExportWindow::Private::TARGET_WRITE_METADATA_TO_FILE = QLatin1String("writeMetadataToFile");
0073 const QString FCExportWindow::Private::TARGET_OVERWRITE              = QLatin1String("overwrite");
0074 const QString FCExportWindow::Private::TARGET_ALBUMPATH              = QLatin1String("albumPath");
0075 const QString FCExportWindow::Private::TARGET_BEHAVIOR               = QLatin1String("targetBehavior");
0076 const QString FCExportWindow::Private::CONFIG_GROUP                  = QLatin1String("FileCopyExport");
0077 
0078 const QString FCExportWindow::Private::CHANGE_IMAGE_PROPERTIES       = QLatin1String("changeImageProperties");
0079 const QString FCExportWindow::Private::IMAGE_RESIZE                  = QLatin1String("imageResize");
0080 const QString FCExportWindow::Private::IMAGE_FORMAT                  = QLatin1String("imageFormat");
0081 const QString FCExportWindow::Private::IMAGE_COMPRESSION             = QLatin1String("imageCompression");
0082 const QString FCExportWindow::Private::REMOVE_METADATA               = QLatin1String("removeMetadata");
0083 
0084 FCExportWindow::FCExportWindow(DInfoInterface* const iface, QWidget* const /*parent*/)
0085     : WSToolDialog(nullptr, QLatin1String("FileCopy Export Dialog")),
0086       d           (new Private)
0087 {
0088     d->exportWidget = new FCExportWidget(iface, this);
0089     setMainWidget(d->exportWidget);
0090 
0091     // -- Window setup ------------------------------------------------------
0092 
0093     setWindowTitle(i18nc("@title:window", "Export to Local Storage"));
0094     setModal(false);
0095 
0096     startButton()->setText(i18nc("@action:button", "Start Export"));
0097     startButton()->setToolTip(i18nc("@info:tooltip, button", "Start export to the specified target"));
0098 
0099     connect(startButton(), SIGNAL(clicked()),
0100             this, SLOT(slotCopy()));
0101 
0102     connect(this, SIGNAL(finished(int)),
0103             this, SLOT(slotFinished()));
0104 
0105     connect(d->exportWidget->imagesList(), SIGNAL(signalImageListChanged()),
0106             this, SLOT(slotImageListChanged()));
0107 
0108     connect(d->exportWidget, SIGNAL(signalTargetUrlChanged(QUrl)),
0109             this, SLOT(slotTargetUrlChanged(QUrl)));
0110 
0111     // -- initial sync ------------------------------------------------------
0112 
0113     restoreSettings();
0114     updateUploadButton();
0115 }
0116 
0117 FCExportWindow::~FCExportWindow()
0118 {
0119     delete d;
0120 }
0121 
0122 void FCExportWindow::slotFinished()
0123 {
0124     saveSettings();
0125     d->exportWidget->imagesList()->listView()->clear();
0126 }
0127 
0128 void FCExportWindow::closeEvent(QCloseEvent* e)
0129 {
0130     if (!e)
0131     {
0132         return;
0133     }
0134 
0135     slotFinished();
0136     e->accept();
0137 }
0138 
0139 void FCExportWindow::reactivate()
0140 {
0141     d->exportWidget->imagesList()->loadImagesFromCurrentSelection();
0142     show();
0143 }
0144 
0145 void FCExportWindow::restoreSettings()
0146 {
0147     KSharedConfigPtr config = KSharedConfig::openConfig();
0148     KConfigGroup group      = config->group(d->CONFIG_GROUP);
0149 
0150     FCContainer settings;
0151 
0152     settings.destUrl               = group.readEntry(d->TARGET_URL_PROPERTY,           QUrl());
0153     settings.behavior              = group.readEntry(d->TARGET_BEHAVIOR,               (int)FCContainer::CopyFile);
0154     settings.imageFormat           = group.readEntry(d->IMAGE_FORMAT,                  (int)FCContainer::JPEG);
0155     settings.imageResize           = group.readEntry(d->IMAGE_RESIZE,                  1024);
0156     settings.imageCompression      = group.readEntry(d->IMAGE_COMPRESSION,             75);
0157     settings.sidecars              = group.readEntry(d->TARGET_SIDECARS,               false);
0158     settings.writeMetadataToFile   = group.readEntry(d->TARGET_WRITE_METADATA_TO_FILE, false);
0159     settings.overwrite             = group.readEntry(d->TARGET_OVERWRITE,              false);
0160     settings.albumPath             = group.readEntry(d->TARGET_ALBUMPATH,              false);
0161     settings.removeMetadata        = group.readEntry(d->REMOVE_METADATA,               false);
0162     settings.changeImageProperties = group.readEntry(d->CHANGE_IMAGE_PROPERTIES,       false);
0163 
0164     d->exportWidget->setSettings(settings);
0165 }
0166 
0167 void FCExportWindow::saveSettings()
0168 {
0169     KSharedConfigPtr config = KSharedConfig::openConfig();
0170     KConfigGroup group      = config->group(d->CONFIG_GROUP);
0171     FCContainer settings    = d->exportWidget->getSettings();
0172 
0173     group.writeEntry(d->TARGET_URL_PROPERTY,           settings.destUrl);
0174     group.writeEntry(d->TARGET_BEHAVIOR,               settings.behavior);
0175     group.writeEntry(d->IMAGE_FORMAT,                  settings.imageFormat);
0176     group.writeEntry(d->IMAGE_RESIZE,                  settings.imageResize);
0177     group.writeEntry(d->IMAGE_COMPRESSION,             settings.imageCompression);
0178     group.writeEntry(d->TARGET_SIDECARS,               settings.sidecars);
0179     group.writeEntry(d->TARGET_WRITE_METADATA_TO_FILE, settings.writeMetadataToFile);
0180     group.writeEntry(d->TARGET_OVERWRITE,              settings.overwrite);
0181     group.writeEntry(d->TARGET_ALBUMPATH,              settings.albumPath);
0182     group.writeEntry(d->REMOVE_METADATA,               settings.removeMetadata);
0183     group.writeEntry(d->CHANGE_IMAGE_PROPERTIES,       settings.changeImageProperties);
0184 }
0185 
0186 void FCExportWindow::slotImageListChanged()
0187 {
0188     updateUploadButton();
0189 }
0190 
0191 void FCExportWindow::slotTargetUrlChanged(const QUrl& target)
0192 {
0193     Q_UNUSED(target);
0194     updateUploadButton();
0195 }
0196 
0197 void FCExportWindow::updateUploadButton()
0198 {
0199     bool listNotEmpty = !d->exportWidget->imagesList()->imageUrls().isEmpty();
0200     startButton()->setEnabled(listNotEmpty && d->exportWidget->targetUrl().isValid());
0201 /*
0202     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "Updated upload button with listNotEmpty ="
0203                                      << listNotEmpty << ", targetUrl().isValid() ="
0204                                      << d->exportWidget->targetUrl().isValid();
0205 */
0206 }
0207 
0208 void FCExportWindow::slotCopyingDone(const QUrl& from, const QUrl& to)
0209 {
0210     qCDebug(DIGIKAM_WEBSERVICES_LOG) << "Copied to:" << to.toLocalFile();
0211 
0212     d->exportWidget->imagesList()->removeItemByUrl(from);
0213 }
0214 
0215 void FCExportWindow::slotCopyingFinished()
0216 {
0217     setEnabled(true);
0218 
0219     if (!d->exportWidget->imagesList()->imageUrls().isEmpty())
0220     {
0221         QMessageBox::information(this, i18nc("@title:window", "Copy not Completed"),
0222                                  i18n("Some of the items have not been copied "
0223                                       "and are still in the list. "
0224                                       "You can retry to copy these items now."));
0225     }
0226 }
0227 
0228 void FCExportWindow::slotCopy()
0229 {
0230     saveSettings();
0231 
0232     // start copying and react on signals
0233 
0234     setEnabled(false);
0235 
0236     if (d->thread)
0237     {
0238         d->thread->cancel();
0239     }
0240     else
0241     {
0242         d->thread = new FCThread(this);
0243 
0244         connect(d->thread, SIGNAL(finished()),
0245                 this, SLOT(slotCopyingFinished()));
0246 
0247         connect(d->thread, SIGNAL(signalUrlProcessed(QUrl,QUrl)),
0248                 this, SLOT(slotCopyingDone(QUrl,QUrl)));
0249     }
0250 
0251     d->thread->createCopyJobs(d->exportWidget->imagesList()->imageUrls(),
0252                               d->exportWidget->getSettings());
0253 
0254     d->thread->start();
0255 }
0256 
0257 } // namespace DigikamGenericFileCopyPlugin
0258 
0259 #include "moc_fcexportwindow.cpp"