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

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 "mailsettingspage.h"
0016 
0017 // Qt includes
0018 
0019 #include <QIcon>
0020 #include <QLabel>
0021 #include <QUrl>
0022 #include <QWidget>
0023 #include <QApplication>
0024 #include <QStyle>
0025 #include <QComboBox>
0026 #include <QCheckBox>
0027 #include <QSpinBox>
0028 #include <QGridLayout>
0029 #include <QGroupBox>
0030 
0031 // KDE includes
0032 
0033 #include <klocalizedstring.h>
0034 
0035 // Local includes
0036 
0037 #include "mailwizard.h"
0038 #include "dfileselector.h"
0039 #include "filesaveconflictbox.h"
0040 #include "digikam_debug.h"
0041 
0042 namespace DigikamGenericSendByMailPlugin
0043 {
0044 
0045 class Q_DECL_HIDDEN MailSettingsPage::Private
0046 {
0047 public:
0048 
0049     explicit Private(QWizard* const dialog)
0050       : mailAgentName(nullptr),
0051         imageFormat(nullptr),
0052         changeImagesProp(nullptr),
0053         addFileProperties(nullptr),
0054         removeMetadata(nullptr),
0055         imageCompression(nullptr),
0056         attachmentlimit(nullptr),
0057         imageResize(nullptr),
0058         wizard(nullptr),
0059         iface(nullptr),
0060         settings(nullptr)
0061     {
0062         wizard = dynamic_cast<MailWizard*>(dialog);
0063 
0064         if (wizard)
0065         {
0066             settings = wizard->settings();
0067             iface    = wizard->iface();
0068         }
0069     }
0070 
0071     QComboBox*      mailAgentName;
0072     QComboBox*      imageFormat;
0073 
0074     QCheckBox*      changeImagesProp;
0075     QCheckBox*      addFileProperties;
0076     QCheckBox*      removeMetadata;
0077 
0078     QSpinBox*       imageCompression;
0079     QSpinBox*       attachmentlimit;
0080     QSpinBox*       imageResize;
0081 
0082     MailWizard*     wizard;
0083     DInfoInterface* iface;
0084     MailSettings*   settings;
0085 };
0086 
0087 MailSettingsPage::MailSettingsPage(QWizard* const dialog, const QString& title)
0088     : DWizardPage(dialog, title),
0089       d(new Private(dialog))
0090 {
0091     QWidget* const main = new QWidget(this);
0092     const int spacing   = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0093                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0094 
0095     // --------------------
0096 
0097     QLabel* const labelMailAgent = new QLabel(main);
0098     labelMailAgent->setWordWrap(false);
0099     labelMailAgent->setText(i18n("Mail program:"));
0100 
0101     d->mailAgentName             = new QComboBox(main);
0102     d->mailAgentName->setEditable(false);
0103     d->mailAgentName->setWhatsThis(i18n("Select your preferred external mail client program here."));
0104 
0105     QMap<MailSettings::MailClient, QString> map                = MailSettings::mailClientNames();
0106     QMap<MailSettings::MailClient, QString>::const_iterator it = map.constBegin();
0107 
0108     while (it != map.constEnd())
0109     {
0110         d->mailAgentName->insertItem((int)it.key(), it.value(), (int)it.key());
0111         ++it;
0112     }
0113 
0114     labelMailAgent->setBuddy(d->mailAgentName);
0115 
0116     //---------------------------------------------
0117 
0118     d->addFileProperties = new QCheckBox(i18n("Attach a file with items properties"), main);
0119     d->addFileProperties->setWhatsThis(i18n("If you enable this option, all item properties "
0120                                             "as Comments, Rating, or Tags, will be added as "
0121                                             "an attached file."));
0122 
0123     // --------------------------------------------
0124 
0125     d->attachmentlimit                 = new QSpinBox(main);
0126     d->attachmentlimit->setRange(1, 50);
0127     d->attachmentlimit->setSingleStep(1);
0128     d->attachmentlimit->setValue(17);
0129     d->attachmentlimit->setSuffix(i18n(" MB"));
0130 
0131     QLabel* const labelAttachmentLimit = new QLabel(i18n("Maximum email size limit:"), main);
0132     labelAttachmentLimit->setBuddy(d->attachmentlimit);
0133 
0134     //---------------------------------------------
0135 
0136     d->changeImagesProp       = new QCheckBox(i18n("Adjust image properties"), main);
0137     d->changeImagesProp->setChecked(true);
0138     d->changeImagesProp->setWhatsThis(i18n("If you enable this option, "
0139                                            "all images to be sent can be "
0140                                            "resized and recompressed."));
0141 
0142     QGroupBox* const groupBox = new QGroupBox(i18n("Image Properties"), main);
0143     QGridLayout* const grid2  = new QGridLayout(groupBox);
0144 
0145     //---------------------------------------------
0146 
0147     d->imageResize                 = new QSpinBox(groupBox);
0148     d->imageResize->setRange(300, 4000);
0149     d->imageResize->setSingleStep(1);
0150     d->imageResize->setValue(1024);
0151     d->imageResize->setSuffix(i18n(" px"));
0152     d->imageResize->setWhatsThis(i18n("Select the length of the images that are to be sent. "
0153                                        "The aspect ratio is preserved."));
0154 
0155     QLabel* const labelImageResize = new QLabel(i18n("Image Length:"), groupBox);
0156     labelImageResize->setBuddy(d->imageResize);
0157 
0158     //---------------------------------------------
0159 
0160     QLabel* const labelImageFormat = new QLabel(groupBox);
0161     labelImageFormat->setWordWrap(false);
0162     labelImageFormat->setText(i18n("Image Format:"));
0163 
0164     d->imageFormat                 = new QComboBox(groupBox);
0165     d->imageFormat->setEditable(false);
0166     d->imageFormat->setWhatsThis(i18n("Select your preferred format to convert image."));
0167 
0168     QMap<MailSettings::ImageFormat, QString> map2                = MailSettings::imageFormatNames();
0169     QMap<MailSettings::ImageFormat, QString>::const_iterator it2 = map2.constBegin();
0170 
0171     while (it2 != map2.constEnd())
0172     {
0173         d->imageFormat->addItem(it2.value(), (int)it2.key());
0174         ++it2;
0175     }
0176 
0177     labelImageFormat->setBuddy(d->imageFormat);
0178 
0179     // --------------------
0180 
0181     d->imageCompression                 = new QSpinBox(groupBox);
0182     d->imageCompression->setRange(1, 100);
0183     d->imageCompression->setSingleStep(1);
0184     d->imageCompression->setValue(75);
0185     QString whatsThis = i18n("<p>The new compression value of JPEG images to be sent:</p>");
0186     whatsThis         = whatsThis + i18n("<p><b>1</b>: very high compression<br/>"
0187                                          "<b>25</b>: high compression<br/>"
0188                                          "<b>50</b>: medium compression<br/>"
0189                                          "<b>75</b>: low compression (default value)<br/>"
0190                                          "<b>100</b>: no compression</p>");
0191 
0192     d->imageCompression->setWhatsThis(whatsThis);
0193 
0194     QLabel* const labelImageCompression = new QLabel(i18n("Image quality:"), this);
0195     labelImageCompression->setBuddy(d->imageCompression);
0196 
0197     // --------------------
0198 
0199     d->removeMetadata = new QCheckBox(i18n("Remove all metadata"), main);
0200     d->removeMetadata->setWhatsThis(i18n("If you enable this option, all metadata "
0201                                          "as Exif, Iptc, and Xmp will be removed."));
0202 
0203     // --------------------
0204 
0205     grid2->addWidget(labelImageResize,      0, 0, 1, 1);
0206     grid2->addWidget(d->imageResize,        0, 1, 1, 2);
0207     grid2->addWidget(labelImageFormat,      1, 0, 1, 1);
0208     grid2->addWidget(d->imageFormat,        1, 1, 1, 2);
0209     grid2->addWidget(labelImageCompression, 2, 0, 1, 1);
0210     grid2->addWidget(d->imageCompression,   2, 1, 1, 2);
0211     grid2->addWidget(d->removeMetadata,     3, 0, 1, 2);
0212     grid2->setRowStretch(4, 10);
0213     grid2->setColumnStretch(2, 10);
0214     grid2->setSpacing(spacing);
0215     grid2->setAlignment(Qt::AlignTop);
0216 
0217     // --------------------
0218 
0219     QGridLayout* const grid = new QGridLayout(main);
0220     grid->addWidget(labelMailAgent,       0, 0, 1, 1);
0221     grid->addWidget(d->mailAgentName,     0, 1, 1, 2);
0222     grid->addWidget(labelAttachmentLimit, 1, 0, 1, 1);
0223     grid->addWidget(d->attachmentlimit,   1, 1, 1, 4);
0224     grid->addWidget(d->addFileProperties, 2, 0, 1, 4);
0225     grid->addWidget(d->changeImagesProp,  3, 0, 1, 4);
0226     grid->addWidget(groupBox,             4, 0, 1, 4);
0227     grid->setRowStretch(5, 10);
0228     grid->setColumnStretch(3, 10);
0229     grid->setSpacing(spacing);
0230 
0231     setPageWidget(main);
0232     setLeftBottomPix(QIcon::fromTheme(QLatin1String("mail-attachment")));
0233 
0234     //---------------------------------------------
0235 
0236     connect(d->imageFormat, SIGNAL(activated(int)),
0237             this, SLOT(slotImageFormatChanged(int)));
0238 
0239     connect(d->changeImagesProp, SIGNAL(toggled(bool)),
0240             groupBox, SLOT(setEnabled(bool)));
0241 }
0242 
0243 MailSettingsPage::~MailSettingsPage()
0244 {
0245     delete d;
0246 }
0247 
0248 void MailSettingsPage::slotImageFormatChanged(int i)
0249 {
0250     if (i == MailSettings::JPEG)
0251     {
0252         d->imageCompression->setEnabled(true);
0253     }
0254     else
0255     {
0256         d->imageCompression->setEnabled(false);
0257     }
0258 }
0259 
0260 void MailSettingsPage::initializePage()
0261 {
0262     QMap<MailSettings::MailClient, QString> map                = d->settings->binPaths;
0263     QMap<MailSettings::MailClient, QString>::const_iterator it = map.constBegin();
0264 
0265     while (it != map.constEnd())
0266     {
0267         if (d->settings->binPaths[it.key()].isEmpty())
0268         {
0269             d->mailAgentName->setItemData((int)it.key(), false, Qt::UserRole-1);
0270         }
0271         else if (it.key() == d->settings->mailProgram)
0272         {
0273             d->mailAgentName->setCurrentIndex((int)d->settings->mailProgram);
0274         }
0275 
0276         ++it;
0277     }
0278 
0279     d->imageResize->setValue(d->settings->imageSize);
0280     d->imageFormat->setCurrentIndex((int)d->settings->imageFormat);
0281 
0282     d->changeImagesProp->setChecked(d->settings->imagesChangeProp);
0283 
0284     d->addFileProperties->setChecked(d->iface ? d->settings->addFileProperties : false);
0285     d->addFileProperties->setEnabled(d->iface);
0286 
0287     d->imageCompression->setValue(d->settings->imageCompression);
0288     d->attachmentlimit->setValue(d->settings->attLimitInMbytes);
0289     d->removeMetadata->setChecked(d->settings->removeMetadata);
0290 
0291     slotImageFormatChanged(d->imageFormat->currentIndex());
0292 }
0293 
0294 bool MailSettingsPage::validatePage()
0295 {
0296     d->settings->mailProgram       = MailSettings::MailClient(d->mailAgentName->currentIndex());
0297     d->settings->imageSize         = d->imageResize->value();
0298     d->settings->imageFormat       = MailSettings::ImageFormat(d->imageFormat->currentIndex());
0299 
0300     d->settings->imagesChangeProp  = d->changeImagesProp->isChecked();
0301     d->settings->addFileProperties = d->addFileProperties->isChecked();
0302     d->settings->removeMetadata    = d->removeMetadata->isChecked();
0303 
0304     d->settings->imageCompression  = d->imageCompression->value();
0305     d->settings->attLimitInMbytes  = d->attachmentlimit->value();
0306 
0307     return true;
0308 }
0309 
0310 } // namespace DigikamGenericSendByMailPlugin
0311 
0312 #include "moc_mailsettingspage.cpp"