File indexing completed on 2025-01-05 04:00:08

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2003-05-03
0007  * Description : mime types setup tab
0008  *
0009  * SPDX-FileCopyrightText: 2004-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 "setupmime.h"
0016 
0017 // Qt includes
0018 
0019 #include <QGridLayout>
0020 #include <QGroupBox>
0021 #include <QLabel>
0022 #include <QToolButton>
0023 #include <QVBoxLayout>
0024 #include <QWhatsThis>
0025 #include <QApplication>
0026 #include <QStyle>
0027 #include <QLineEdit>
0028 #include <QIcon>
0029 #include <QMessageBox>
0030 
0031 // KDE includes
0032 
0033 #include <klocalizedstring.h>
0034 
0035 // Local includes
0036 
0037 #include "applicationsettings.h"
0038 #include "coredb.h"
0039 #include "coredbaccess.h"
0040 #include "dlayoutbox.h"
0041 #include "scancontroller.h"
0042 #include "setuputils.h"
0043 
0044 namespace Digikam
0045 {
0046 
0047 class Q_DECL_HIDDEN SetupMime::Private
0048 {
0049 public:
0050 
0051     explicit Private()
0052       : imageFileFilterLabel(nullptr),
0053         movieFileFilterLabel(nullptr),
0054         audioFileFilterLabel(nullptr),
0055         imageFileFilterEdit (nullptr),
0056         movieFileFilterEdit (nullptr),
0057         audioFileFilterEdit (nullptr)
0058     {
0059     }
0060 
0061     QLabel*    imageFileFilterLabel;
0062     QLabel*    movieFileFilterLabel;
0063     QLabel*    audioFileFilterLabel;
0064 
0065     QLineEdit* imageFileFilterEdit;
0066     QLineEdit* movieFileFilterEdit;
0067     QLineEdit* audioFileFilterEdit;
0068 };
0069 
0070 SetupMime::SetupMime(QWidget* const parent)
0071     : QScrollArea(parent),
0072       d          (new Private)
0073 {
0074     QWidget* const panel      = new QWidget(viewport());
0075     setWidget(panel);
0076     setWidgetResizable(true);
0077 
0078     const int spacing         = qMin(QApplication::style()->pixelMetric(QStyle::PM_LayoutHorizontalSpacing),
0079                              QApplication::style()->pixelMetric(QStyle::PM_LayoutVerticalSpacing));
0080     QVBoxLayout* const layout = new QVBoxLayout(panel);
0081 
0082     // --------------------------------------------------------
0083 
0084     QLabel* const explanationLabel = new QLabel;
0085     explanationLabel->setText(i18n("<p>Add new file types to show as album items.</p>"
0086                                    "<p>digiKam attempts to support all of the image formats that digital cameras produce, "
0087                                    "while being able to handle a few other important video and audio formats.</p> "
0088                                    "<p>You can add to the already-appreciable list of formats that digiKam handles by "
0089                                    "adding the extension of the type you want to add. "
0090                                    "Multiple extensions need to be separated by a semicolon or space.</p>"
0091                                    "<p><b><u>Note:</u> changes done in this view will perform "
0092                                    "a database rescan in the background.</b></p>"));
0093     explanationLabel->setWordWrap(true);
0094 
0095     // --------------------------------------------------------
0096 
0097     QGroupBox* const imageFileFilterBox = new QGroupBox(i18n("Image Files"), panel);
0098     QGridLayout* const grid1            = new QGridLayout(imageFileFilterBox);
0099     QLabel* const logoLabel1            = new QLabel(imageFileFilterBox);
0100     logoLabel1->setPixmap(QIcon::fromTheme(QLatin1String("image-jpeg")).pixmap(48));
0101 
0102     d->imageFileFilterLabel = new QLabel(imageFileFilterBox);
0103     d->imageFileFilterLabel->setText(i18n("Additional &image file extensions (<a href='image'>Currently-supported types</a>):"));
0104 
0105     DHBox* const hbox1      = new DHBox(imageFileFilterBox);
0106     d->imageFileFilterEdit  = new QLineEdit(hbox1);
0107     d->imageFileFilterEdit->setWhatsThis(i18n("<p>Here you can add the extensions of image files (including RAW files) "
0108                                               "to be displayed in the Album view. Just put \"xyz abc\" "
0109                                               "to display files with the xyz and abc extensions in your Album view.</p>"
0110                                               "<p>You can also remove file formats that are shown by default "
0111                                               "by putting a minus sign in front of the extension: e.g. \"-gif\" would remove all GIF files "
0112                                               "from your Album view and any trace of them in your database. "
0113                                               "They would not be deleted, just not shown in digiKam.</p>"
0114                                               "<p><b>Warning:</b> Removing files from the database means losing "
0115                                               "all of their tags and ratings.</p>"));
0116     d->imageFileFilterEdit->setClearButtonEnabled(true);
0117     d->imageFileFilterEdit->setPlaceholderText(i18n("Enter additional image file extensions."));
0118     d->imageFileFilterLabel->setBuddy(d->imageFileFilterEdit);
0119     hbox1->setStretchFactor(d->imageFileFilterEdit, 10);
0120 
0121     grid1->addWidget(logoLabel1,              0, 0, 2, 1);
0122     grid1->addWidget(d->imageFileFilterLabel, 0, 1, 1, 1);
0123     grid1->addWidget(hbox1,                   1, 1, 1, 1);
0124     grid1->setColumnStretch(1, 10);
0125     grid1->setSpacing(spacing);
0126 
0127     // --------------------------------------------------------
0128 
0129     QGroupBox* const movieFileFilterBox = new QGroupBox(i18n("Video Files"), panel);
0130     QGridLayout* const grid2            = new QGridLayout(movieFileFilterBox);
0131 
0132     QLabel* const logoLabel2 = new QLabel(movieFileFilterBox);
0133     logoLabel2->setPixmap(QIcon::fromTheme(QLatin1String("video-x-matroska")).pixmap(48));
0134 
0135     d->movieFileFilterLabel  = new QLabel(movieFileFilterBox);
0136     d->movieFileFilterLabel->setText(i18n("Additional &video file extensions (<a href='video'>Currently-supported types</a>):"));
0137 
0138     DHBox* const hbox2       = new DHBox(movieFileFilterBox);
0139     d->movieFileFilterEdit   = new QLineEdit(hbox2);
0140     d->movieFileFilterEdit->setWhatsThis(i18n("<p>Here you can add extra extensions of video files "
0141                                               "to be displayed in your Album view. Just write \"xyz abc\" "
0142                                               "to support files with the *.xyz and *.abc extensions. "
0143                                               "Clicking on these files will "
0144                                               "play them in an embedded video player.</p>"
0145                                               "<p>You can also remove file formats that are supported by default "
0146                                               "by putting a minus sign in front of the extension: e.g. \"-avi\" would remove "
0147                                               "all AVI files from your Album view and any trace of them in your database. "
0148                                               "They would not be deleted, just not shown in digiKam.</p>"
0149                                               "<p><b>Warning:</b> Removing files from the database means losing "
0150                                               "all of their tags and ratings.</p>"));
0151     d->movieFileFilterEdit->setClearButtonEnabled(true);
0152     d->movieFileFilterEdit->setPlaceholderText(i18n("Enter additional video file extensions."));
0153     d->movieFileFilterLabel->setBuddy(d->movieFileFilterEdit);
0154     hbox2->setStretchFactor(d->movieFileFilterEdit, 10);
0155 
0156     grid2->addWidget(logoLabel2,                0, 0, 2, 1);
0157     grid2->addWidget(d->movieFileFilterLabel,   0, 1, 1, 1);
0158     grid2->addWidget(hbox2,                     1, 1, 1, 1);
0159     grid2->setColumnStretch(1, 10);
0160     grid2->setSpacing(spacing);
0161 
0162     // --------------------------------------------------------
0163 
0164     QGroupBox* const audioFileFilterBox = new QGroupBox(i18n("Audio Files"), panel);
0165     QGridLayout* const grid3            = new QGridLayout(audioFileFilterBox);
0166 
0167     QLabel* const logoLabel3 = new QLabel(audioFileFilterBox);
0168     logoLabel3->setPixmap(QIcon::fromTheme(QLatin1String("audio-x-mpeg")).pixmap(48));
0169 
0170     d->audioFileFilterLabel  = new QLabel(audioFileFilterBox);
0171     d->audioFileFilterLabel->setText(i18n("Additional &audio file extensions (<a href='audio'>Currently-supported types</a>):"));
0172 
0173     DHBox* const hbox3       = new DHBox(audioFileFilterBox);
0174     d->audioFileFilterEdit   = new QLineEdit(hbox3);
0175     d->audioFileFilterEdit->setWhatsThis(i18n("<p>Here you can add extra extensions of audio files "
0176                                               "to be displayed in your Album view. Just write \"xyz abc\" "
0177                                               "to support files with the *.xyz and *.abc extensions. "
0178                                               "Clicking on these files will "
0179                                               "play them in an embedded audio player.</p>"
0180                                               "<p>You can also remove file formats that are supported by default "
0181                                               "by putting a minus sign in front of the extension: e.g. \"-ogg\" would "
0182                                               "remove all OGG files from your Album view and any trace of them in your database. "
0183                                               "They would not be deleted, just not shown in digiKam.</p>"
0184                                               "<p><b>Warning:</b> Removing files from the database means losing "
0185                                               "all of their tags and ratings.</p>"));
0186     d->audioFileFilterEdit->setClearButtonEnabled(true);
0187     d->audioFileFilterEdit->setPlaceholderText(i18n("Enter additional audio file extensions."));
0188     d->audioFileFilterLabel->setBuddy(d->audioFileFilterEdit);
0189     hbox3->setStretchFactor(d->audioFileFilterEdit, 10);
0190 
0191     grid3->addWidget(logoLabel3,              0, 0, 2, 1);
0192     grid3->addWidget(d->audioFileFilterLabel, 0, 1, 1, 1);
0193     grid3->addWidget(hbox3,                   1, 1, 1, 1);
0194     grid3->setColumnStretch(1, 10);
0195     grid3->setSpacing(spacing);
0196 
0197     // --------------------------------------------------------
0198 
0199     layout->setContentsMargins(spacing, spacing, spacing, spacing);
0200     layout->setSpacing(spacing);
0201     layout->addWidget(explanationLabel);
0202     layout->addWidget(imageFileFilterBox);
0203     layout->addWidget(movieFileFilterBox);
0204     layout->addWidget(audioFileFilterBox);
0205     layout->addStretch();
0206 
0207     // --------------------------------------------------------
0208 
0209     connect(d->imageFileFilterLabel, SIGNAL(linkActivated(QString)),
0210             this, SLOT(slotShowCurrentImageSettings()));
0211 
0212     connect(d->movieFileFilterLabel, SIGNAL(linkActivated(QString)),
0213             this, SLOT(slotShowCurrentMovieSettings()));
0214 
0215     connect(d->audioFileFilterLabel, SIGNAL(linkActivated(QString)),
0216             this, SLOT(slotShowCurrentAudioSettings()));
0217 
0218     // --------------------------------------------------------
0219 
0220     readSettings();
0221 }
0222 
0223 SetupMime::~SetupMime()
0224 {
0225     delete d;
0226 }
0227 
0228 void SetupMime::applySettings()
0229 {
0230     // Display warning if user removes a core format
0231 
0232     QStringList coreImageFormats, removedImageFormats;
0233     coreImageFormats << QLatin1String("jpg") << QLatin1String("jpeg") << QLatin1String("jpe") // JPEG
0234                      << QLatin1String("tif") << QLatin1String("tiff")                         // TIFF
0235                      << QLatin1String("png");                                                 // PNG
0236 
0237     QString imageFilter = d->imageFileFilterEdit->text();
0238 
0239     Q_FOREACH (const QString& format, coreImageFormats)
0240     {
0241         if (imageFilter.contains(QLatin1Char('-')     + format) ||
0242             imageFilter.contains(QLatin1String("-*.") + format))
0243         {
0244             removedImageFormats << format;
0245         }
0246     }
0247 
0248     if (!removedImageFormats.isEmpty())
0249     {
0250         int result = QMessageBox::warning(this, qApp->applicationName(),
0251                                           i18n("<p>You have chosen to remove the following image formats "
0252                                                "from the list of supported formats: <b>%1</b>.</p>"
0253                                                "<p>These are very common formats. If you have images in your collection "
0254                                                "with these formats, they will be removed from the database and you will "
0255                                                "lose all information about them, including rating and tags.</p>"
0256                                                "<p>Are you sure you want to apply your changes and lose the support for these formats?</p>",
0257                                                removedImageFormats.join(QLatin1Char(' '))),
0258                                           QMessageBox::Yes | QMessageBox::No);
0259 
0260         if (result != QMessageBox::Yes)
0261         {
0262             return;
0263         }
0264     }
0265 
0266     QString imageFilterString;
0267     QString movieFilterString;
0268     QString audioFilterString;
0269 
0270     CoreDbAccess().db()->getUserFilterSettings(&imageFilterString, &movieFilterString, &audioFilterString);
0271 
0272     imageFilterString.replace(QLatin1Char(';'), QLatin1Char(' '));
0273     movieFilterString.replace(QLatin1Char(';'), QLatin1Char(' '));
0274     audioFilterString.replace(QLatin1Char(';'), QLatin1Char(' '));
0275 
0276     if ((d->imageFileFilterEdit->text() != imageFilterString) ||
0277         (d->movieFileFilterEdit->text() != movieFilterString) ||
0278         (d->audioFileFilterEdit->text() != audioFilterString))
0279     {
0280         CoreDbAccess().db()->setUserFilterSettings(cleanUserFilterString(d->imageFileFilterEdit->text()),
0281                                                    cleanUserFilterString(d->movieFileFilterEdit->text()),
0282                                                    cleanUserFilterString(d->audioFileFilterEdit->text()));
0283         ScanController::instance()->completeCollectionScanInBackground(false, false);
0284     }
0285 }
0286 
0287 void SetupMime::readSettings()
0288 {
0289     QString image;
0290     QString audio;
0291     QString video;
0292 
0293     CoreDbAccess().db()->getUserFilterSettings(&image, &video, &audio);
0294 
0295     d->imageFileFilterEdit->setText(image.replace(QLatin1Char(';'), QLatin1Char(' ')));
0296     d->movieFileFilterEdit->setText(video.replace(QLatin1Char(';'), QLatin1Char(' ')));
0297     d->audioFileFilterEdit->setText(audio.replace(QLatin1Char(';'), QLatin1Char(' ')));
0298 }
0299 
0300 void SetupMime::slotShowCurrentImageSettings()
0301 {
0302     QStringList imageList;
0303     CoreDbAccess().db()->getFilterSettings(&imageList, nullptr, nullptr);
0304     QString text = i18n("<p>Files with these extensions will be recognized as images "
0305                         "and included into the database:<br/> <code>%1</code></p>",
0306                         imageList.join(QLatin1Char(' ')));
0307     QWhatsThis::showText(d->imageFileFilterLabel->mapToGlobal(QPoint(0, 0)), text, d->imageFileFilterLabel);
0308 }
0309 
0310 void SetupMime::slotShowCurrentMovieSettings()
0311 {
0312     QStringList movieList;
0313     CoreDbAccess().db()->getFilterSettings(nullptr, &movieList, nullptr);
0314     QString text = i18n("<p>Files with these extensions will be recognized as video files "
0315                         "and included into the database:<br/> <code>%1</code></p>",
0316                         movieList.join(QLatin1Char(' ')));
0317     QWhatsThis::showText(d->movieFileFilterLabel->mapToGlobal(QPoint(0, 0)), text, d->movieFileFilterLabel);
0318 }
0319 
0320 void SetupMime::slotShowCurrentAudioSettings()
0321 {
0322     QStringList audioList;
0323     CoreDbAccess().db()->getFilterSettings(nullptr, nullptr, &audioList);
0324     QString text = i18n("<p>Files with these extensions will be recognized as audio files "
0325                         "and included into the database:<br/> <code>%1</code></p>",
0326                         audioList.join(QLatin1Char(' ')));
0327     QWhatsThis::showText(d->audioFileFilterLabel->mapToGlobal(QPoint(0, 0)), text, d->audioFileFilterLabel);
0328 }
0329 
0330 } // namespace Digikam
0331 
0332 #include "moc_setupmime.cpp"