File indexing completed on 2024-05-19 04:24:17

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "KisBrushTypeMetaDataFixup.h"
0008 
0009 #include "kis_debug.h"
0010 
0011 #include <QSqlQuery>
0012 #include <QSqlError>
0013 
0014 #include <KisResourceLocator.h>
0015 #include "kis_brush.h"
0016 
0017 
0018 QStringList KisBrushTypeMetaDataFixup::executeFix()
0019 {
0020     QStringList errorMessages;
0021 
0022     QSqlQuery q;
0023     const bool r = q.prepare("SELECT resources.id FROM resources "
0024               "INNER JOIN resource_types ON resources.resource_type_id = resource_types.id "
0025               "LEFT JOIN metadata ON metadata.foreign_id = resources.id AND metadata.key = :metadata_key "
0026               "WHERE resource_types.name = :resource_type AND metadata.value IS Null;");
0027     if (!r) {
0028         errorMessages.append(i18n("Could not access brush tip metadata"));
0029         return errorMessages;
0030     }
0031     q.bindValue(":resource_type", ResourceType::Brushes);
0032     q.bindValue(":metadata_key", KisBrush::brushTypeMetaDataKey);
0033 
0034     if (!q.exec()) {
0035         errorMessages.append(i18n("Could not access brush tip metadata"));
0036     }
0037     else {
0038         bool updatedAtLeastOneResource = false;
0039 
0040         while(q.next()) {
0041             /// we cannot use KisResourceModel here because it
0042             /// is not yet initialized at this state
0043             KoResourceSP res = KisResourceLocator::instance()->resourceForId(q.value(0).toInt());
0044             KIS_SAFE_ASSERT_RECOVER(res) { continue; }
0045             KisBrushSP brush = res.dynamicCast<KisBrush>();
0046             KIS_SAFE_ASSERT_RECOVER(brush) { continue; }
0047 
0048             /// on loading the metadata of the brush has been
0049             /// initialized properly, so we can just write the
0050             /// updated version into the database back...
0051             KisResourceLocator::instance()->setMetaDataForResource(res->resourceId(), res->metadata());
0052 
0053             updatedAtLeastOneResource = true;
0054         }
0055 
0056         if (updatedAtLeastOneResource) {
0057             qWarning() << "Successfully updated brush type metadata in the database";
0058         }
0059     }
0060 
0061     return errorMessages;
0062 }