File indexing completed on 2025-03-09 03:57:06

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2010-05-19
0007  * Description : an option to provide database information to the parser
0008  *
0009  * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "databaseoption.h"
0016 
0017 // Qt includes
0018 
0019 #include <QGridLayout>
0020 #include <QLabel>
0021 #include <QPointer>
0022 #include <QLineEdit>
0023 
0024 // KDE includes
0025 
0026 #include <klocalizedstring.h>
0027 
0028 // Local includes
0029 
0030 #include "itemcomments.h"
0031 #include "dbkeyselector.h"
0032 #include "digikam_debug.h"
0033 #include "commonkeys.h"
0034 #include "metadatakeys.h"
0035 #include "positionkeys.h"
0036 
0037 namespace Digikam
0038 {
0039 
0040 DatabaseOptionDialog::DatabaseOptionDialog(Rule* const parent) :
0041     RuleDialog(parent),
0042     dbkeySelectorView(nullptr),
0043     separatorLineEdit(nullptr)
0044 {
0045     QWidget* const mainWidget = new QWidget(this);
0046     dbkeySelectorView         = new DbKeySelectorView(this);
0047     QLabel* const customLabel = new QLabel(i18n("Keyword separator:"));
0048     separatorLineEdit         = new QLineEdit(this);
0049     separatorLineEdit->setText(QLatin1String("_"));
0050 
0051     // --------------------------------------------------------
0052 
0053     QGridLayout* const mainLayout = new QGridLayout(this);
0054     mainLayout->addWidget(customLabel,       0, 0, 1, 1);
0055     mainLayout->addWidget(separatorLineEdit, 0, 1, 1, 1);
0056     mainLayout->addWidget(dbkeySelectorView, 1, 0, 1, -1);
0057     mainWidget->setLayout(mainLayout);
0058 
0059     // --------------------------------------------------------
0060 
0061     setSettingsWidget(mainWidget);
0062     resize(450, 450);
0063 }
0064 
0065 DatabaseOptionDialog::~DatabaseOptionDialog()
0066 {
0067 }
0068 
0069 // --------------------------------------------------------
0070 
0071 DatabaseOption::DatabaseOption()
0072     : Option(i18n("Database..."),
0073              i18n("Add information from the database"),
0074              QLatin1String("network-server-database"))
0075 {
0076     addToken(QLatin1String("[db:||key||]"), i18n("Add database information"));
0077     QRegularExpression reg(QLatin1String("\\[db(:(.*))\\]"));
0078     reg.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
0079     setRegExp(reg);
0080 
0081     registerKeysCollection();
0082 }
0083 
0084 DatabaseOption::~DatabaseOption()
0085 {
0086     unregisterKeysCollection();
0087 }
0088 
0089 void DatabaseOption::registerKeysCollection()
0090 {
0091     addDbKeysCollection(new CommonKeys());
0092     addDbKeysCollection(new MetadataKeys());
0093     addDbKeysCollection(new PositionKeys());
0094 }
0095 
0096 void DatabaseOption::unregisterKeysCollection()
0097 {
0098     QSet<DbKeysCollection*> alreadyDeleted;
0099 
0100     Q_FOREACH (DbKeysCollection* const key, m_map)
0101     {
0102         if (key && !alreadyDeleted.contains(key))
0103         {
0104             alreadyDeleted.insert(key);
0105             delete key;
0106         }
0107     }
0108 
0109     m_map.clear();
0110 }
0111 
0112 void DatabaseOption::slotTokenTriggered(const QString& token)
0113 {
0114     Q_UNUSED(token)
0115 
0116     QStringList keys;
0117     QPointer<DatabaseOptionDialog> dlg = new DatabaseOptionDialog(this);
0118     dlg->dbkeySelectorView->setKeysMap(m_map);
0119 
0120     if (dlg->exec() == QDialog::Accepted)
0121     {
0122         QStringList checkedKeys = dlg->dbkeySelectorView->checkedKeysList();
0123 
0124         Q_FOREACH (const QString& key, checkedKeys)
0125         {
0126             QString keyStr = QString::fromUtf8("[db:%1]").arg(key);
0127             keys << keyStr;
0128         }
0129     }
0130 
0131     if (!keys.isEmpty())
0132     {
0133         QString tokenStr = keys.join(dlg->separatorLineEdit->text());
0134         Q_EMIT signalTokenTriggered(tokenStr);
0135     }
0136 
0137     delete dlg;
0138 }
0139 
0140 QString DatabaseOption::parseOperation(ParseSettings& settings, const QRegularExpressionMatch& match)
0141 {
0142     QString keyword = match.captured(2);
0143 
0144     return parseDatabase(keyword, settings);
0145 }
0146 
0147 QString DatabaseOption::parseDatabase(const QString& keyword, ParseSettings& settings)
0148 {
0149     if (settings.fileUrl.isEmpty() || keyword.isEmpty())
0150     {
0151         return QString();
0152     }
0153 
0154     DbKeysCollection* const dbkey = m_map.value(keyword);
0155 
0156     if (!dbkey)
0157     {
0158         return QString();
0159     }
0160 
0161     return dbkey->getValue(keyword, settings);
0162 }
0163 
0164 void DatabaseOption::addDbKeysCollection(DbKeysCollection* key)
0165 {
0166     if (!key)
0167     {
0168         return;
0169     }
0170 
0171     DbKeyIdsMap map = key->ids();
0172 
0173     for (DbKeyIdsMap::const_iterator it = map.constBegin() ;
0174          it != map.constEnd() ; ++it)
0175     {
0176         m_map.insert(it.key(), key);
0177     }
0178 }
0179 
0180 } // namespace Digikam
0181 
0182 #include "moc_databaseoption.cpp"