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

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2017-06-27
0007  * Description : a tool to export items by email.
0008  *
0009  * SPDX-FileCopyrightText: 2017-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "mailfinalpage.h"
0016 
0017 // Qt includes
0018 
0019 #include <QIcon>
0020 #include <QSpacerItem>
0021 #include <QVBoxLayout>
0022 #include <QDesktopServices>
0023 #include <QUrl>
0024 #include <QApplication>
0025 #include <QStyle>
0026 #include <QTimer>
0027 #include <QDir>
0028 
0029 // KDE includes
0030 
0031 #include <klocalizedstring.h>
0032 
0033 // Local includes
0034 
0035 #include "mailwizard.h"
0036 #include "dlayoutbox.h"
0037 #include "digikam_debug.h"
0038 #include "dprogresswdg.h"
0039 #include "dhistoryview.h"
0040 #include "mailprocess.h"
0041 
0042 namespace DigikamGenericSendByMailPlugin
0043 {
0044 
0045 class Q_DECL_HIDDEN MailFinalPage::Private
0046 {
0047 public:
0048 
0049     explicit Private(QWizard* const dialog)
0050       : progressView(nullptr),
0051         progressBar(nullptr),
0052         complete(false),
0053         processor(nullptr),
0054         wizard(nullptr),
0055         settings(nullptr),
0056         iface(nullptr)
0057     {
0058         wizard = dynamic_cast<MailWizard*>(dialog);
0059 
0060         if (wizard)
0061         {
0062             iface    = wizard->iface();
0063             settings = wizard->settings();
0064         }
0065     }
0066 
0067     DHistoryView*     progressView;
0068     DProgressWdg*     progressBar;
0069     bool              complete;
0070     MailProcess*      processor;
0071     MailWizard*       wizard;
0072     MailSettings*     settings;
0073     DInfoInterface*   iface;
0074 };
0075 
0076 MailFinalPage::MailFinalPage(QWizard* const dialog, const QString& title)
0077     : DWizardPage(dialog, title),
0078       d(new Private(dialog))
0079 {
0080     DVBox* const vbox = new DVBox(this);
0081     d->progressView   = new DHistoryView(vbox);
0082     d->progressBar    = new DProgressWdg(vbox);
0083 
0084     vbox->setStretchFactor(d->progressBar, 10);
0085     vbox->setContentsMargins(QMargins());
0086     vbox->setSpacing(qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0087                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing)));
0088 
0089     setPageWidget(vbox);
0090     setLeftBottomPix(QIcon::fromTheme(QLatin1String("mail_send")));
0091 }
0092 
0093 MailFinalPage::~MailFinalPage()
0094 {
0095     if (d->processor)
0096     {
0097         d->processor->slotCancel();
0098     }
0099 
0100     delete d;
0101 }
0102 
0103 void MailFinalPage::initializePage()
0104 {
0105     d->complete = false;
0106     Q_EMIT completeChanged();
0107     QTimer::singleShot(0, this, SLOT(slotProcess()));
0108 }
0109 
0110 void MailFinalPage::slotDone()
0111 {
0112     d->complete = true;
0113     Q_EMIT completeChanged();
0114 }
0115 
0116 void MailFinalPage::slotProcess()
0117 {
0118     if (!d->wizard)
0119     {
0120         d->progressView->addEntry(i18n("Internal Error"),
0121                                   DHistoryView::ErrorEntry);
0122         return;
0123     }
0124 
0125     d->progressView->clear();
0126     d->progressBar->reset();
0127 
0128     d->progressView->addEntry(i18n("Preparing file to export by mail..."),
0129                               DHistoryView::ProgressEntry);
0130 
0131     Q_FOREACH (const QUrl& url, d->settings->inputImages)
0132     {
0133         d->settings->setMailUrl(url, QUrl());
0134     }
0135 
0136     d->progressView->addEntry(i18n("%1 input items to process", d->settings->itemsList.count()),
0137                                   DHistoryView::ProgressEntry);
0138 
0139     for (QMap<QUrl, QUrl>::const_iterator it = d->settings->itemsList.constBegin() ;
0140          it != d->settings->itemsList.constEnd() ; ++it)
0141     {
0142         d->progressView->addEntry(QDir::toNativeSeparators(it.key().toLocalFile()),
0143                                   DHistoryView::ProgressEntry);
0144     }
0145 
0146 
0147     d->progressBar->setMinimum(0);
0148     d->progressBar->setMaximum(d->settings->itemsList.count());
0149 
0150     d->processor = new MailProcess(d->settings, d->iface, this);
0151 
0152     connect(d->processor, SIGNAL(signalProgress(int)),
0153             d->progressBar, SLOT(setValue(int)));
0154 
0155     connect(d->processor, SIGNAL(signalMessage(QString,bool)),
0156             this, SLOT(slotMessage(QString,bool)));
0157 
0158     connect(d->processor, SIGNAL(signalDone(bool)),
0159             this, SLOT(slotDone()));
0160 
0161     d->processor->firstStage();
0162 }
0163 
0164 void MailFinalPage::cleanupPage()
0165 {
0166     if (d->processor)
0167     {
0168         d->processor->slotCancel();
0169     }
0170 }
0171 
0172 void MailFinalPage::slotMessage(const QString& mess, bool err)
0173 {
0174     d->progressView->addEntry(mess, err ? DHistoryView::ErrorEntry
0175                                         : DHistoryView::ProgressEntry);
0176 }
0177 
0178 bool MailFinalPage::isComplete() const
0179 {
0180     return d->complete;
0181 }
0182 
0183 } // namespace DigikamGenericSendByMailPlugin
0184 
0185 #include "moc_mailfinalpage.cpp"