<commit_before>/*
 * This file is a part of Xpiks - cross platform application for
 * keywording and uploading images for microstocks
 * Copyright (C) 2014-2015 <NAME> <<EMAIL>>
 *
 * Xpiks is distributed under the GNU General Public License, version 3.0
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "metadatareadingworker.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QDir>
#include <QTemporaryFile>
#include <QTextStream>
#include "../Models/settingsmodel.h"
#include "../Models/artworkmetadata.h"

#define SOURCEFILE QLatin1String("SourceFile")
#define TITLE QLatin1String("Title")
#define DESCRIPTION QLatin1String("Description")
#define OBJECTNAME QLatin1String("ObjectName")
#define IMAGEDESCRIPTION QLatin1String("ImageDescription")
#define CAPTIONABSTRACT QLatin1String("Caption-Abstract")
#define KEYWORDS QLatin1String("Keywords")
#define SUBJECT QLatin1String("Subject")

namespace MetadataIO {
    void parseJsonKeywords(const QJsonArray &array, ImportDataResult &result) {
        int size = array.size();
        QStringList keywords;
        keywords.reserve(size);

        for (int i = 0; i < size; ++i) {
            keywords.append(array.at(i).toString());
        }

        result.Keywords = keywords;
    }

    void jsonObjectToImportResult(const QJsonObject &object, ImportDataResult &result) {
        if (object.contains(SOURCEFILE)) {
            result.FilePath = object.value(SOURCEFILE).toString();
        }

        if (object.contains(TITLE)) {
            result.Title = object.value(TITLE).toString();
        } else if (object.contains(OBJECTNAME)) {
            result.Title = object.value(OBJECTNAME).toString();
        }

        if (object.contains(DESCRIPTION)) {
            result.Description = object.value(DESCRIPTION).toString();
        } else if (object.contains(CAPTIONABSTRACT)) {
            result.Description = object.value(CAPTIONABSTRACT).toString();
        } else if (object.contains(IMAGEDESCRIPTION)) {
            result.Description = object.value(IMAGEDESCRIPTION).toString();
        }

        if (object.contains(KEYWORDS)) {
            QJsonValue keywords = object.value(KEYWORDS);
            if (keywords.isArray()) {
                parseJsonKeywords(keywords.toArray(), result);
            } else {
                qWarning() << "Keywords object in json is not array";
                result.Keywords = object.value(KEYWORDS).toString().split(QChar(','), QString::SkipEmptyParts);
            }
        } else if (object.contains(SUBJECT)) {
            result.Keywords = object.value(SUBJECT).toString().split(QChar(','), QString::SkipEmptyParts);
        }
    }

    MetadataReadingWorker::MetadataReadingWorker(const QVector<Models::ArtworkMetadata *> &itemsToRead,
                                                 Models::SettingsModel *settingsModel,
                                                 bool ignoreBackups):
        m_ItemsToRead(itemsToRead),<commit_msg>Added fixes for reading metadata written with older Xpiks<commit_after>/*
 * This file is a part of Xpiks - cross platform application for
 * keywording and uploading images for microstocks
 * Copyright (C) 2014-2015 <NAME> <<EMAIL>>
 *
 * Xpiks is distributed under the GNU General Public License, version 3.0
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "metadatareadingworker.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include <QDir>
#include <QTemporaryFile>
#include <QTextStream>
#include "../Models/settingsmodel.h"
#include "../Models/artworkmetadata.h"

#define SOURCEFILE QLatin1String("SourceFile")
#define TITLE QLatin1String("Title")
#define DESCRIPTION QLatin1String("Description")
#define OBJECTNAME QLatin1String("ObjectName")
#define IMAGEDESCRIPTION QLatin1String("ImageDescription")
#define CAPTIONABSTRACT QLatin1String("Caption-Abstract")
#define KEYWORDS QLatin1String("Keywords")
#define SUBJECT QLatin1String("Subject")

namespace MetadataIO {
    void parseJsonKeywords(const QJsonArray &array, ImportDataResult &result) {
        int size = array.size();
        QStringList keywords;
        keywords.reserve(size);

        for (int i = 0; i < size; ++i) {
            keywords.append(array.at(i).toString());
        }

        result.Keywords = keywords;
    }

    void jsonObjectToImportResult(const QJsonObject &object, ImportDataResult &result) {
        if (object.contains(SOURCEFILE)) {
            result.FilePath = object.value(SOURCEFILE).toString();
        }

        if (object.contains(TITLE)) {
            result.Title = object.value(TITLE).toString();
        } else if (object.contains(OBJECTNAME)) {
            result.Title = object.value(OBJECTNAME).toString();
        }

        if (object.contains(DESCRIPTION)) {
            result.Description = object.value(DESCRIPTION).toString();
        } else if (object.contains(CAPTIONABSTRACT)) {
            result.Description = object.value(CAPTIONABSTRACT).toString();
        } else if (object.contains(IMAGEDESCRIPTION)) {
            result.Description = object.value(IMAGEDESCRIPTION).toString();
        }

        bool keywordsSet = false;

        if (object.contains(KEYWORDS)) {
            QJsonValue keywords = object.value(KEYWORDS);
            if (keywords.isArray()) {
                parseJsonKeywords(keywords.toArray(), result);
                keywordsSet = true;
            } else {
                qWarning() << "Keywords object in json is not array";
                QString keywordsStr = object.value(KEYWORDS).toString();
                if (!keywordsStr.simplified().isEmpty()) {
                    // old Xpiks bug when it called exiftool with wrong arguments
                    if (keywordsStr.startsWith(QChar('"')) && keywordsStr.endsWith('"')) {
                        keywordsStr = keywordsStr.mid(1, keywordsStr.length() - 2);
                    }

                    result.Keywords = keywordsStr.split(QChar(','), QString::SkipEmptyParts);
                    keywordsSet = true;
                }
            }
        }

        if (!keywordsSet && object.contains(SUBJECT)) {
            result.Keywords = object.value(SUBJECT).toString().split(QChar(','), QString::SkipEmptyParts);
        }
    }

    MetadataReadingWorker::MetadataReadingWorker(const QVector<Models::ArtworkMetadata *> &itemsToRead,
                                                 Models::SettingsModel *settingsModel,
                                                 bool ignoreBackups):
        m_ItemsToRead(itemsToRead),
{'fraction_non_alphanumeric': 0.105814696485623, 'fraction_numerical': 0.00536741214057508, 'mean_word_length': 2.8343949044585988, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6534451', 'n_tokens_mistral': 2121, 'n_tokens_neox': 2036, 'n_words': 574}