File indexing completed on 2025-01-19 03:50:57

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-03-04
0007  * Description : assign metadata template batch tool.
0008  *
0009  * SPDX-FileCopyrightText: 2009-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 "assigntemplate.h"
0016 
0017 // Qt includes
0018 
0019 #include <QLabel>
0020 #include <QWidget>
0021 #include <QFile>
0022 #include <QScopedPointer>
0023 
0024 // KDE includes
0025 
0026 #include <klocalizedstring.h>
0027 
0028 // Local includes
0029 
0030 #include "dimg.h"
0031 #include "dmetadata.h"
0032 #include "template.h"
0033 #include "templatemanager.h"
0034 #include "dfileoperations.h"
0035 
0036 namespace DigikamBqmAssignTemplatePlugin
0037 {
0038 
0039 AssignTemplate::AssignTemplate(QObject* const parent)
0040     : BatchTool(QLatin1String("AssignTemplate"), MetadataTool, parent),
0041       m_templateSelector(nullptr),
0042       m_templateViewer(nullptr)
0043 {
0044 }
0045 
0046 AssignTemplate::~AssignTemplate()
0047 {
0048 }
0049 
0050 BatchTool* AssignTemplate::clone(QObject* const parent) const
0051 {
0052     return new AssignTemplate(parent);
0053 }
0054 
0055 void AssignTemplate::registerSettingsWidget()
0056 {
0057     DVBox* const vbox  = new DVBox;
0058     m_templateSelector = new TemplateSelector(vbox);
0059     m_templateViewer   = new TemplateViewer(vbox);
0060     m_settingsWidget   = vbox;
0061 
0062     connect(m_templateSelector, SIGNAL(signalTemplateSelected()),
0063             this, SLOT(slotSettingsChanged()));
0064 
0065     BatchTool::registerSettingsWidget();
0066 }
0067 
0068 BatchToolSettings AssignTemplate::defaultSettings()
0069 {
0070     BatchToolSettings settings;
0071     settings.insert(QLatin1String("TemplateTitle"), QString());
0072     return settings;
0073 }
0074 
0075 void AssignTemplate::slotAssignSettings2Widget()
0076 {
0077     QString title = settings()[QLatin1String("TemplateTitle")].toString();
0078 
0079     Template t;
0080 
0081     if      (title == Template::removeTemplateTitle())
0082     {
0083         t.setTemplateTitle(Template::removeTemplateTitle());
0084     }
0085     else if (title.isEmpty())
0086     {
0087         // Nothing to do.
0088     }
0089     else
0090     {
0091         t = TemplateManager::defaultManager()->findByTitle(title);
0092     }
0093 
0094     m_templateSelector->setTemplate(t);
0095     m_templateViewer->setTemplate(t);
0096 }
0097 
0098 void AssignTemplate::slotSettingsChanged()
0099 {
0100     m_templateViewer->setTemplate(m_templateSelector->getTemplate());
0101     BatchToolSettings settings;
0102     settings.insert(QLatin1String("TemplateTitle"), m_templateSelector->getTemplate().templateTitle());
0103     BatchTool::slotSettingsChanged(settings);
0104 }
0105 
0106 bool AssignTemplate::toolOperations()
0107 {
0108     QScopedPointer<DMetadata> meta(new DMetadata);
0109 
0110     if (image().isNull())
0111     {
0112         if (!meta->load(inputUrl().toLocalFile()))
0113         {
0114             return false;
0115         }
0116     }
0117     else
0118     {
0119         meta->setData(image().getMetadata());
0120     }
0121 
0122     QString title = settings()[QLatin1String("TemplateTitle")].toString();
0123 
0124     if      (title == Template::removeTemplateTitle())
0125     {
0126         meta->removeMetadataTemplate();
0127     }
0128     else if (!title.isEmpty())
0129     {
0130         Template t = meta->getMetadataTemplate();
0131         t.merge(TemplateManager::defaultManager()->findByTitle(title));
0132 
0133         meta->setMetadataTemplate(t);
0134     }
0135 
0136     bool ret = true;
0137 
0138     if (image().isNull())
0139     {
0140         QFile::remove(outputUrl().toLocalFile());
0141         ret = DFileOperations::copyFile(inputUrl().toLocalFile(), outputUrl().toLocalFile());
0142 
0143         if (ret && !title.isEmpty())
0144         {
0145             ret = meta->save(outputUrl().toLocalFile());
0146         }
0147     }
0148     else
0149     {
0150         if (!title.isEmpty())
0151         {
0152             image().setMetadata(meta->data());
0153         }
0154 
0155         ret = savefromDImg();
0156     }
0157 
0158     return ret;
0159 }
0160 
0161 } // namespace DigikamBqmAssignTemplatePlugin
0162 
0163 #include "moc_assigntemplate.cpp"