File indexing completed on 2024-04-28 16:30:32

0001 /***************************************************************************
0002  * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr
0003  * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr
0004  * SPDX-License-Identifier: GPL-3.0-or-later
0005  ***************************************************************************/
0006 /** @file
0007  * This file implements classes SKGPropertyObject.
0008  *
0009  * @author Stephane MANKOWSKI / Guillaume DE BURE
0010  */
0011 #include "skgpropertyobject.h"
0012 #include <qdir.h>
0013 
0014 SKGPropertyObject::SKGPropertyObject(): SKGPropertyObject(nullptr)
0015 {}
0016 
0017 SKGPropertyObject::SKGPropertyObject(SKGDocument* iDocument, int iID): SKGNamedObject(iDocument, QStringLiteral("parameters"), iID)
0018 {}
0019 
0020 SKGPropertyObject::~SKGPropertyObject()
0021     = default;
0022 
0023 SKGPropertyObject::SKGPropertyObject(const SKGPropertyObject& iObject) = default;
0024 
0025 SKGPropertyObject::SKGPropertyObject(const SKGObjectBase& iObject) : SKGNamedObject(iObject)
0026 {}
0027 
0028 SKGPropertyObject& SKGPropertyObject::operator= (const SKGObjectBase& iObject)
0029 {
0030     copyFrom(iObject);
0031     return *this;
0032 }
0033 
0034 SKGPropertyObject& SKGPropertyObject::operator= (const SKGPropertyObject& iObject)
0035 {
0036     copyFrom(iObject);
0037     return *this;
0038 }
0039 
0040 SKGError SKGPropertyObject::setParentId(const QString& iParentId)
0041 {
0042     return setAttribute(QStringLiteral("t_uuid_parent"), iParentId);
0043 }
0044 
0045 QString SKGPropertyObject::getParentId() const
0046 {
0047     return getAttribute(QStringLiteral("t_uuid_parent"));
0048 }
0049 
0050 SKGError SKGPropertyObject::setValue(const QString& iValue)
0051 {
0052     return setAttribute(QStringLiteral("t_value"), iValue);
0053 }
0054 
0055 QString SKGPropertyObject::getValue() const
0056 {
0057     return getAttribute(QStringLiteral("t_value"));
0058 }
0059 
0060 QUrl SKGPropertyObject::getUrl(bool iBuildTemporaryFile) const
0061 {
0062     QUrl url;
0063     if (getID() != 0) {
0064         QStringList uuid = getParentId().split('-');
0065         if (uuid.count() == 2) {
0066             SKGObjectBase p(getDocument(), uuid.at(1), SKGServices::stringToInt(uuid.at(0)));
0067             QVariant blob = p.getPropertyBlob(getName());
0068 
0069             // Is it a copied file ?
0070             if (!blob.isNull()) {
0071                 QByteArray blob_bytes = blob.toByteArray();
0072                 if (blob_bytes.length() > 0) {
0073                     // Yes, this is a file
0074                     QString fileName = QDir::tempPath() % '/' % QFileInfo(getValue()).fileName();
0075                     if (iBuildTemporaryFile) {
0076                         // Save temporary file
0077                         QFile file(fileName);
0078                         file.setPermissions(QFile::ReadOwner | QFile::WriteOwner);
0079                         if (file.open(QIODevice::WriteOnly)) {
0080                             file.write(blob_bytes);
0081                             file.flush();
0082                             file.close();
0083                             file.setPermissions(QFile::ReadOwner);  // To be sure that no modifications are done
0084                         }
0085                     }
0086                     url = QUrl::fromLocalFile(fileName);
0087                 }
0088             } else if (QFile(getValue()).exists()) {
0089                 // Is it a linked file? Yes
0090                 url = QUrl::fromLocalFile(getValue());
0091             } else {
0092                 // Is it a linked file?  No, Is it a http url ?
0093                 QUrl url2 = QUrl(getValue());
0094                 if (!url2.scheme().isEmpty()) {
0095                     url = url2;
0096                 }
0097             }
0098         }
0099     }
0100     return url;
0101 }
0102 
0103 QString SKGPropertyObject::getWhereclauseId() const
0104 {
0105     // Could we use the id
0106     QString output = SKGObjectBase::getWhereclauseId();  // clazy:exclude=skipped-base-method
0107     if (output.isEmpty()) {
0108         if (!(getAttribute(QStringLiteral("t_name")).isEmpty())) {
0109             output = "t_name='" % SKGServices::stringToSqlString(getAttribute(QStringLiteral("t_name"))) % '\'';
0110         }
0111         if (!(getParentId().isEmpty())) {
0112             if (!output.isEmpty()) {
0113                 output += QStringLiteral(" AND ");
0114             }
0115             output += "t_uuid_parent='" % getParentId() % '\'';
0116         }
0117     }
0118     return output;
0119 }
0120 
0121