1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2017-06-27
7 * Description : a tool to export items by email.
8 *
9 * Copyright (C) 2017-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
10 *
11 * This program is free software; you can redistribute it
12 * and/or modify it under the terms of the GNU General
13 * Public License as published by the Free Software Foundation;
14 * either version 2, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * ============================================================ */
22
23 #include "mailsettingspage.h"
24
25 // Qt includes
26
27 #include <QIcon>
28 #include <QLabel>
29 #include <QUrl>
30 #include <QWidget>
31 #include <QApplication>
32 #include <QStyle>
33 #include <QComboBox>
34 #include <QCheckBox>
35 #include <QSpinBox>
36 #include <QGridLayout>
37 #include <QGroupBox>
38
39 // KDE includes
40
41 #include <klocalizedstring.h>
42
43 // Local includes
44
45 #include "mailwizard.h"
46 #include "dfileselector.h"
47 #include "filesaveconflictbox.h"
48 #include "digikam_debug.h"
49
50 namespace DigikamGenericSendByMailPlugin
51 {
52
53 class Q_DECL_HIDDEN MailSettingsPage::Private
54 {
55 public:
56
Private(QWizard * const dialog)57 explicit Private(QWizard* const dialog)
58 : mailAgentName(nullptr),
59 imageFormat(nullptr),
60 changeImagesProp(nullptr),
61 addFileProperties(nullptr),
62 removeMetadata(nullptr),
63 imageCompression(nullptr),
64 attachmentlimit(nullptr),
65 imageResize(nullptr),
66 wizard(nullptr),
67 iface(nullptr),
68 settings(nullptr)
69 {
70 wizard = dynamic_cast<MailWizard*>(dialog);
71
72 if (wizard)
73 {
74 settings = wizard->settings();
75 iface = wizard->iface();
76 }
77 }
78
79 QComboBox* mailAgentName;
80 QComboBox* imageFormat;
81
82 QCheckBox* changeImagesProp;
83 QCheckBox* addFileProperties;
84 QCheckBox* removeMetadata;
85
86 QSpinBox* imageCompression;
87 QSpinBox* attachmentlimit;
88 QSpinBox* imageResize;
89
90 MailWizard* wizard;
91 DInfoInterface* iface;
92 MailSettings* settings;
93 };
94
MailSettingsPage(QWizard * const dialog,const QString & title)95 MailSettingsPage::MailSettingsPage(QWizard* const dialog, const QString& title)
96 : DWizardPage(dialog, title),
97 d(new Private(dialog))
98 {
99 QWidget* const main = new QWidget(this);
100 const int spacing = QApplication::style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing);
101
102 // --------------------
103
104 QLabel* const labelMailAgent = new QLabel(main);
105 labelMailAgent->setWordWrap(false);
106 labelMailAgent->setText(i18n("Mail program:"));
107
108 d->mailAgentName = new QComboBox(main);
109 d->mailAgentName->setEditable(false);
110 d->mailAgentName->setWhatsThis(i18n("Select your preferred external mail client program here."));
111
112 QMap<MailSettings::MailClient, QString> map = MailSettings::mailClientNames();
113 QMap<MailSettings::MailClient, QString>::const_iterator it = map.constBegin();
114
115 while (it != map.constEnd())
116 {
117 d->mailAgentName->insertItem((int)it.key(), it.value(), (int)it.key());
118 ++it;
119 }
120
121 labelMailAgent->setBuddy(d->mailAgentName);
122
123 //---------------------------------------------
124
125 d->addFileProperties = new QCheckBox(i18n("Attach a file with items properties"), main);
126 d->addFileProperties->setWhatsThis(i18n("If you enable this option, all item properties "
127 "as Comments, Rating, or Tags, will be added as "
128 "an attached file."));
129
130 // --------------------------------------------
131
132 d->attachmentlimit = new QSpinBox(main);
133 d->attachmentlimit->setRange(1, 50);
134 d->attachmentlimit->setSingleStep(1);
135 d->attachmentlimit->setValue(17);
136 d->attachmentlimit->setSuffix(i18n(" MB"));
137
138 QLabel* const labelAttachmentLimit = new QLabel(i18n("Maximum email size limit:"), main);
139 labelAttachmentLimit->setBuddy(d->attachmentlimit);
140
141 //---------------------------------------------
142
143 d->changeImagesProp = new QCheckBox(i18n("Adjust image properties"), main);
144 d->changeImagesProp->setChecked(true);
145 d->changeImagesProp->setWhatsThis(i18n("If you enable this option, "
146 "all images to be sent can be "
147 "resized and recompressed."));
148
149 QGroupBox* const groupBox = new QGroupBox(i18n("Image Properties"), main);
150 QGridLayout* const grid2 = new QGridLayout(groupBox);
151
152 //---------------------------------------------
153
154 d->imageResize = new QSpinBox(groupBox);
155 d->imageResize->setRange(300, 4000);
156 d->imageResize->setSingleStep(1);
157 d->imageResize->setValue(1024);
158 d->imageResize->setSuffix(i18n(" px"));
159 d->imageResize->setWhatsThis(i18n("Select the length of the images that are to be sent. "
160 "The aspect ratio is preserved."));
161
162 QLabel* const labelImageResize = new QLabel(i18n("Image Length:"), groupBox);
163 labelImageResize->setBuddy(d->imageResize);
164
165 //---------------------------------------------
166
167 QLabel* const labelImageFormat = new QLabel(groupBox);
168 labelImageFormat->setWordWrap(false);
169 labelImageFormat->setText(i18n("Image Format:"));
170
171 d->imageFormat = new QComboBox(groupBox);
172 d->imageFormat->setEditable(false);
173 d->imageFormat->setWhatsThis(i18n("Select your preferred format to convert image."));
174
175 QMap<MailSettings::ImageFormat, QString> map2 = MailSettings::imageFormatNames();
176 QMap<MailSettings::ImageFormat, QString>::const_iterator it2 = map2.constBegin();
177
178 while (it2 != map2.constEnd())
179 {
180 d->imageFormat->addItem(it2.value(), (int)it2.key());
181 ++it2;
182 }
183
184 labelImageFormat->setBuddy(d->imageFormat);
185
186 // --------------------
187
188 d->imageCompression = new QSpinBox(groupBox);
189 d->imageCompression->setRange(1, 100);
190 d->imageCompression->setSingleStep(1);
191 d->imageCompression->setValue(75);
192 QString whatsThis = i18n("<p>The new compression value of JPEG images to be sent:</p>");
193 whatsThis = whatsThis + i18n("<p><b>1</b>: very high compression<br/>"
194 "<b>25</b>: high compression<br/>"
195 "<b>50</b>: medium compression<br/>"
196 "<b>75</b>: low compression (default value)<br/>"
197 "<b>100</b>: no compression</p>");
198
199 d->imageCompression->setWhatsThis(whatsThis);
200
201 QLabel* const labelImageCompression = new QLabel(i18n("Image quality:"), this);
202 labelImageCompression->setBuddy(d->imageCompression);
203
204 // --------------------
205
206 d->removeMetadata = new QCheckBox(i18n("Remove all metadata"), main);
207 d->removeMetadata->setWhatsThis(i18n("If you enable this option, all metadata "
208 "as Exif, Iptc, and Xmp will be removed."));
209
210 // --------------------
211
212 grid2->addWidget(labelImageResize, 0, 0, 1, 1);
213 grid2->addWidget(d->imageResize, 0, 1, 1, 2);
214 grid2->addWidget(labelImageFormat, 1, 0, 1, 1);
215 grid2->addWidget(d->imageFormat, 1, 1, 1, 2);
216 grid2->addWidget(labelImageCompression, 2, 0, 1, 1);
217 grid2->addWidget(d->imageCompression, 2, 1, 1, 2);
218 grid2->addWidget(d->removeMetadata, 3, 0, 1, 2);
219 grid2->setRowStretch(4, 10);
220 grid2->setColumnStretch(2, 10);
221 grid2->setSpacing(spacing);
222 grid2->setAlignment(Qt::AlignTop);
223
224 // --------------------
225
226 QGridLayout* const grid = new QGridLayout(main);
227 grid->addWidget(labelMailAgent, 0, 0, 1, 1);
228 grid->addWidget(d->mailAgentName, 0, 1, 1, 2);
229 grid->addWidget(labelAttachmentLimit, 1, 0, 1, 1);
230 grid->addWidget(d->attachmentlimit, 1, 1, 1, 4);
231 grid->addWidget(d->addFileProperties, 2, 0, 1, 4);
232 grid->addWidget(d->changeImagesProp, 3, 0, 1, 4);
233 grid->addWidget(groupBox, 4, 0, 1, 4);
234 grid->setRowStretch(5, 10);
235 grid->setColumnStretch(3, 10);
236 grid->setSpacing(spacing);
237
238 setPageWidget(main);
239 setLeftBottomPix(QIcon::fromTheme(QLatin1String("mail-attachment")));
240
241 //---------------------------------------------
242
243 connect(d->imageFormat, SIGNAL(activated(int)),
244 this, SLOT(slotImageFormatChanged(int)));
245
246 connect(d->changeImagesProp, SIGNAL(toggled(bool)),
247 groupBox, SLOT(setEnabled(bool)));
248 }
249
~MailSettingsPage()250 MailSettingsPage::~MailSettingsPage()
251 {
252 delete d;
253 }
254
slotImageFormatChanged(int i)255 void MailSettingsPage::slotImageFormatChanged(int i)
256 {
257 if (i == MailSettings::JPEG)
258 {
259 d->imageCompression->setEnabled(true);
260 }
261 else
262 {
263 d->imageCompression->setEnabled(false);
264 }
265 }
266
initializePage()267 void MailSettingsPage::initializePage()
268 {
269 QMap<MailSettings::MailClient, QString> map = d->settings->binPaths;
270 QMap<MailSettings::MailClient, QString>::const_iterator it = map.constBegin();
271
272 while (it != map.constEnd())
273 {
274 if (d->settings->binPaths[it.key()].isEmpty())
275 {
276 d->mailAgentName->setItemData((int)it.key(), false, Qt::UserRole-1);
277 }
278 else if (it.key() == d->settings->mailProgram)
279 {
280 d->mailAgentName->setCurrentIndex((int)d->settings->mailProgram);
281 }
282
283 ++it;
284 }
285
286 d->imageResize->setValue(d->settings->imageSize);
287 d->imageFormat->setCurrentIndex((int)d->settings->imageFormat);
288
289 d->changeImagesProp->setChecked(d->settings->imagesChangeProp);
290
291 d->addFileProperties->setChecked(d->iface ? d->settings->addFileProperties : false);
292 d->addFileProperties->setEnabled(d->iface);
293
294 d->imageCompression->setValue(d->settings->imageCompression);
295 d->attachmentlimit->setValue(d->settings->attLimitInMbytes);
296 d->removeMetadata->setChecked(d->settings->removeMetadata);
297
298 slotImageFormatChanged(d->imageFormat->currentIndex());
299 }
300
validatePage()301 bool MailSettingsPage::validatePage()
302 {
303 d->settings->mailProgram = MailSettings::MailClient(d->mailAgentName->currentIndex());
304 d->settings->imageSize = d->imageResize->value();
305 d->settings->imageFormat = MailSettings::ImageFormat(d->imageFormat->currentIndex());
306
307 d->settings->imagesChangeProp = d->changeImagesProp->isChecked();
308 d->settings->addFileProperties = d->addFileProperties->isChecked();
309 d->settings->removeMetadata = d->removeMetadata->isChecked();
310
311 d->settings->imageCompression = d->imageCompression->value();
312 d->settings->attLimitInMbytes = d->attachmentlimit->value();
313
314 return true;
315 }
316
317 } // namespace DigikamGenericSendByMailPlugin
318