1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2006-02-23
 * Description : item metadata interface - file I/O helpers.
 *
 * SPDX-FileCopyrightText: 2006-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
 * SPDX-FileCopyrightText: 2006-2013 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
 * SPDX-FileCopyrightText: 2011      by Leif Huhn <leif at dkstat dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "dmetadata.h"

// Qt includes

#include <QString>
#include <QFile>
#include <QFileInfo>
#include <QLocale>
#include <QUuid>
#include <QMimeDatabase>

// Local includes

#include "filereadwritelock.h"
#include "metaenginesettings.h"
#include "digikam_version.h"
#include "digikam_globals.h"
#include "digikam_debug.h"

namespace Digikam
{

bool DMetadata::load(const QString& filePath, bool videoAll, Backend* backend)
{
    FileReadLocker lock(filePath);

    Backend usedBackend = NoBackend;
    bool hasLoaded      = false;
    QFileInfo info(filePath);
    QMimeDatabase mimeDB;

    if (
        !mimeDB.mimeTypeForFile(info).name().startsWith(QLatin1String("video/")) &&
        !mimeDB.mimeTypeForFile(info).name().startsWith(QLatin1String("audio/")) &&
        (info.suffix().toUpper() != QLatin1String("INSV"))                       &&
        (info.suffix().toUpper() != QLatin1String("H264"))
       )
    {
        // Process images only with Exiv2 backend first, or Exiftool in 2nd, or libraw for RAW files,
        // or with libheif, or at end with ImageMagick.
        // Never process video files with Exiv2, the backend is very unstable and this feature will be removed.

        if (!(hasLoaded = MetaEngine::load(filePath)))
        {
            if (!(hasLoaded = loadUsingExifTool(filePath)))
            {
                if (!(hasLoaded = loadUsingRawEngine(filePath)))
                {

                    if (!(hasLoaded =
#ifdef HAVE_HEIF
                        loadUsingLibheif(filePath)
#else
                        false
#endif
                       ))
                    {
                        // cppcheck-suppress knownConditionTrueFalse
                        if (!(hasLoaded = false/*loadUsingImageMagick(filePath)*/))
                        {
                            usedBackend = NoBackend;
                        }
                        else
                        {
                            usedBackend = ImageMagickBackend;
                        }
                    }
                    else
                    {
                        usedBackend = LibHeifBackend;
                    }
                }
                else
                {
                    usedBackend = LibRawBackend;
                }
            }
            else
            {
                usedBackend = ExifToolBackend;
            }
        }
        else
        {
            // Special case when Exiv2 has empty metadata container
            // but no loading error, give ExifTool a chance.

            if (isEmpty() && (loadUsingExifTool(filePath)))
            {
                usedBackend = ExifToolBackend;
            }
            else
            {
                usedBackend = Exiv2Backend;
            }
        }

        hasLoaded |= loadFromSidecarAndMerge(filePath);
    }
    else
    {
        // No image files (aka video or audio), process with ExifTool and ffmpeg backends.

        hasLoaded          = loadUsingExifTool(filePath, videoAll);

        bool sidcareLoaded = loadFromSidecarAndMerge(filePath);

        if      (hasLoaded)
        {
            if (videoAll && (hasLoaded = loadUsingFFmpeg(filePath)))
            {
                usedBackend = VideoMergeBackend;
            }
            else
            {
                usedBackend = ExifToolBackend;
            }
        }
        else if ((hasLoaded = loadUsingFFmpeg(filePath)))
        {
            usedBackend = FFMpegBackend;
        }
        else
        {
            usedBackend = NoBackend;
        }

        hasLoaded |= sidcareLoaded;
    }

    qCDebug(DIGIKAM_METAENGINE_LOG) << "Loading metadata with"
                                    << backendName(usedBackend)
                                    << "backend from" << filePath;

    if (backend)
    {
        *backend = usedBackend;
    }

    return hasLoaded;
}

bool DMetadata::save(const QString& filePath, bool setVersion) const<--- Derived function 'DMetadata::save'
{
    FileWriteLocker lock(filePath);

    return MetaEngine::save(filePath, setVersion);
}

bool DMetadata::applyChanges(bool setVersion) const<--- Derived function 'DMetadata::applyChanges'
{
    FileWriteLocker lock(getFilePath());

    return MetaEngine::applyChanges(setVersion);
}

} // namespace Digikam