File indexing completed on 2024-05-12 15:56:12

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     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     q.bindValue(":resource_type", ResourceType::Brushes);
0028     q.bindValue(":metadata_key", KisBrush::brushTypeMetaDataKey);
0029 
0030     if (!q.exec()) {
0031         // the message is not translatable for Krita 5.1 due to a string freeze
0032         errorMessages.append(/*i18n*/("Could not access brush tip metadata"));
0033     }
0034     else {
0035         bool updatedAtLeastOneResource = false;
0036 
0037         while(q.next()) {
0038             /// we cannot use KisResourceModel here because it
0039             /// is not yet initialized at this state
0040             KoResourceSP res = KisResourceLocator::instance()->resourceForId(q.value(0).toInt());
0041             KIS_SAFE_ASSERT_RECOVER(res) { continue; }
0042             KisBrushSP brush = res.dynamicCast<KisBrush>();
0043             KIS_SAFE_ASSERT_RECOVER(brush) { continue; }
0044 
0045             /// on loading the metadata of the brush has been
0046             /// intialized properly, so we can just write the
0047             /// updated version into the database back...
0048             KisResourceLocator::instance()->setMetaDataForResource(res->resourceId(), res->metadata());
0049 
0050             updatedAtLeastOneResource = true;
0051         }
0052 
0053         if (updatedAtLeastOneResource) {
0054             qWarning() << "Successfully updated brush type metadata in the database";
0055         }
0056     }
0057 
0058     return errorMessages;
0059 }