File indexing completed on 2025-01-19 03:55:58

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2015-06-22
0007  * Description : central place for metadata settings
0008  *
0009  * SPDX-FileCopyrightText: 2015      by Veaceslav Munteanu <veaceslav dot munteanu90 at gmail dot com>
0010  * SPDX-FileCopyrightText: 2015-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0011  *
0012  * SPDX-License-Identifier: GPL-2.0-or-later
0013  *
0014  * ============================================================ */
0015 
0016 #include "dmetadatasettings.h"
0017 
0018 // Qt includes
0019 
0020 #include <QDir>
0021 #include <QFileInfo>
0022 #include <QMutex>
0023 
0024 // KDE includes
0025 
0026 #include <kconfiggroup.h>
0027 #include <ksharedconfig.h>
0028 
0029 // Local includes
0030 
0031 #include "digikam_debug.h"
0032 
0033 namespace Digikam
0034 {
0035 
0036 class Q_DECL_HIDDEN DMetadataSettings::Private
0037 {
0038 public:
0039 
0040     explicit Private()
0041         : mutex      (),
0042           configGroup(QLatin1String("DMetadata Settings"))
0043     {
0044     }
0045 
0046     DMetadataSettingsContainer settings;
0047     QMutex                     mutex;
0048 
0049     const QString              configGroup;
0050 
0051 public:
0052 
0053     DMetadataSettingsContainer readFromConfig() const;
0054     void                       writeToConfig()  const;
0055     DMetadataSettingsContainer setSettings(const DMetadataSettingsContainer& s);
0056 };
0057 
0058 DMetadataSettingsContainer DMetadataSettings::Private::readFromConfig() const
0059 {
0060     DMetadataSettingsContainer s;
0061     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0062     KConfigGroup group        = config->group(configGroup);
0063     s.readFromConfig(group);
0064 
0065     return s;
0066 }
0067 
0068 void DMetadataSettings::Private::writeToConfig() const
0069 {
0070     KSharedConfig::Ptr config = KSharedConfig::openConfig();
0071     KConfigGroup group        = config->group(configGroup);
0072     settings.writeToConfig(group);
0073 }
0074 
0075 DMetadataSettingsContainer DMetadataSettings::Private::setSettings(const DMetadataSettingsContainer& s)
0076 {
0077     QMutexLocker lock(&mutex);
0078     DMetadataSettingsContainer old;
0079     old      = settings;
0080     settings = s;
0081 
0082     return old;
0083 }
0084 
0085 // -----------------------------------------------------------------------------------------------
0086 
0087 class Q_DECL_HIDDEN DMetadataSettingsCreator
0088 {
0089 public:
0090 
0091     DMetadataSettings object;
0092 };
0093 
0094 Q_GLOBAL_STATIC(DMetadataSettingsCreator, dmetatadaSettingsCreator)
0095 
0096 // -----------------------------------------------------------------------------------------------
0097 
0098 DMetadataSettings* DMetadataSettings::instance()
0099 {
0100     return &dmetatadaSettingsCreator->object;
0101 }
0102 
0103 DMetadataSettings::DMetadataSettings()
0104     : d(new Private)
0105 {
0106     readFromConfig();
0107     qRegisterMetaType<DMetadataSettingsContainer>("DMetadataSettingsContainer");
0108 }
0109 
0110 DMetadataSettings::~DMetadataSettings()
0111 {
0112     delete d;
0113 }
0114 
0115 DMetadataSettingsContainer DMetadataSettings::settings() const
0116 {
0117     QMutexLocker lock(&d->mutex);
0118     DMetadataSettingsContainer s(d->settings);
0119 
0120     return s;
0121 }
0122 
0123 void DMetadataSettings::setSettings(const DMetadataSettingsContainer& settings)
0124 {
0125     DMetadataSettingsContainer old = d->setSettings(settings);
0126     Q_EMIT signalDMetadataSettingsChanged(settings, old);
0127     Q_EMIT signalSettingsChanged();
0128     d->writeToConfig();
0129 }
0130 
0131 void DMetadataSettings::readFromConfig()
0132 {
0133     DMetadataSettingsContainer s   = d->readFromConfig();
0134     DMetadataSettingsContainer old = d->setSettings(s);
0135     Q_EMIT signalDMetadataSettingsChanged(s, old);
0136     Q_EMIT signalSettingsChanged();
0137 }
0138 
0139 } // namespace Digikam
0140 
0141 #include "moc_dmetadatasettings.cpp"