File indexing completed on 2024-07-21 03:46:02

0001 /*
0002     This file is part of the syndication library
0003     SPDX-FileCopyrightText: 2006 Frank Osterfeld <osterfeld@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "statement.h"
0009 #include "literal.h"
0010 #include "model.h"
0011 #include "model_p.h"
0012 #include "property.h"
0013 #include "resource.h"
0014 
0015 #include <QString>
0016 #include <QWeakPointer>
0017 
0018 namespace Syndication
0019 {
0020 namespace RDF
0021 {
0022 class SYNDICATION_NO_EXPORT Statement::StatementPrivate
0023 {
0024 public:
0025     uint subjectID;
0026     uint predicateID;
0027     uint objectID;
0028     QWeakPointer<Model::ModelPrivate> model;
0029 
0030     bool operator==(const StatementPrivate &other) const
0031     {
0032         // FIXME: use better check that works also with multiple models
0033         return subjectID == other.subjectID && predicateID == other.predicateID && objectID == other.objectID;
0034     }
0035 };
0036 
0037 Statement::Statement()
0038     : d(new StatementPrivate)
0039 {
0040     d->subjectID = 0;
0041     d->predicateID = 0;
0042     d->objectID = 0;
0043 }
0044 
0045 Statement::Statement(const Statement &other)
0046 {
0047     d = other.d;
0048 }
0049 
0050 Statement::Statement(ResourcePtr subject, PropertyPtr predicate, NodePtr object)
0051     : d(new StatementPrivate)
0052 {
0053     d->model = subject->model().d;
0054     d->subjectID = subject->id();
0055     d->predicateID = predicate->id();
0056     d->objectID = object->id();
0057 }
0058 
0059 Statement::~Statement()
0060 {
0061 }
0062 
0063 Statement &Statement::operator=(const Statement &other)
0064 {
0065     d = other.d;
0066     return *this;
0067 }
0068 
0069 bool Statement::operator==(const Statement &other) const
0070 {
0071     if (!d || !other.d) {
0072         return d == other.d;
0073     }
0074 
0075     return *d == *(other.d);
0076 }
0077 
0078 bool Statement::isNull() const
0079 {
0080     return d->subjectID == 0;
0081 }
0082 
0083 ResourcePtr Statement::subject() const
0084 {
0085     const QSharedPointer<Model::ModelPrivate> m = d ? d->model.toStrongRef() : QSharedPointer<Model::ModelPrivate>();
0086     return m ? m->resourceByID(d->subjectID) : ResourcePtr(new Resource);
0087 }
0088 
0089 PropertyPtr Statement::predicate() const
0090 {
0091     const QSharedPointer<Model::ModelPrivate> m = d ? d->model.toStrongRef() : QSharedPointer<Model::ModelPrivate>();
0092     return m ? m->propertyByID(d->predicateID) : PropertyPtr(new Property());
0093 }
0094 
0095 NodePtr Statement::object() const
0096 {
0097     const QSharedPointer<Model::ModelPrivate> m = d ? d->model.toStrongRef() : QSharedPointer<Model::ModelPrivate>();
0098     return m ? m->nodeByID(d->objectID) : NodePtr(LiteralPtr(new Literal()));
0099 }
0100 
0101 ResourcePtr Statement::asResource() const
0102 {
0103     const QSharedPointer<Model::ModelPrivate> m = d ? d->model.toStrongRef() : QSharedPointer<Model::ModelPrivate>();
0104 
0105     if (isNull() || !m || !m->nodeByID(d->objectID)->isResource()) {
0106         return ResourcePtr(new Resource);
0107     }
0108 
0109     return m->resourceByID(d->objectID);
0110 }
0111 
0112 QString Statement::asString() const
0113 {
0114     if (isNull()) {
0115         return QString();
0116     }
0117 
0118     const QSharedPointer<Model::ModelPrivate> m = d ? d->model.toStrongRef() : QSharedPointer<Model::ModelPrivate>();
0119     return m ? m->nodeByID(d->objectID)->text() : QString();
0120 }
0121 
0122 } // namespace RDF
0123 } // namespace Syndication