1
1
/* This file is part of the KDE project
2
2
Copyright (C) 2010 KO GmbH <ben.martin@kogmbh.com>
3
Copyright (C) 2011 Ben Martin hacking for fun!
3
Copyright (C) 2011,2012 Ben Martin hacking for fun!
5
5
This library is free software; you can redistribute it and/or
6
6
modify it under the terms of the GNU Library General Public
88
88
model->deleteLater();
91
Soprano::Model *model; ///< Main Model containing all Rdf for doc
91
QSharedPointer<Soprano::Model> model; ///< Main Model containing all Rdf for doc
92
92
QMap<QString, QWeakPointer<KoTextInlineRdf> > inlineRdfObjects; ///< Cache of weak pointers to inline Rdf
93
93
KoRdfPrefixMapping *prefixMapping; ///< prefix -> URI mapping
95
QList<KoRdfFoaF*> foafObjects;
96
QList<KoRdfCalendarEvent*> calObjects;
97
QList<KoRdfLocation*> locObjects;
95
QList<hKoRdfFoaF> foafObjects;
96
QList<hKoRdfCalendarEvent> calObjects;
97
QList<hKoRdfLocation> locObjects;
99
QMap<QString,QList<KoSemanticStylesheet*> > userStylesheets;
99
QMap<QString,QList<hKoSemanticStylesheet> > userStylesheets;
204
204
bool KoDocumentRdf::loadRdf(KoStore *store, const Soprano::Parser *parser, const QString &fileName)
206
Soprano::Model *tmpmodel(Soprano::createModel());
206
QSharedPointer<Soprano::Model> tmpmodel(Soprano::createModel());
207
207
if (!d->model || !tmpmodel) {
208
208
kWarning(30003) << "No soprano model";
248
248
QStringList classNames = KoRdfSemanticItem::classNames();
249
249
foreach (const QString &semanticClass, classNames) {
250
KoRdfSemanticItem *si = KoRdfSemanticItem::createSemanticItem(this, this, semanticClass);
250
hKoRdfSemanticItem si = KoRdfSemanticItem::createSemanticItem(this, this, semanticClass);
251
251
si->loadUserStylesheets(d->model);
331
330
QStringList classNames = KoRdfSemanticItem::classNames();
332
331
foreach (const QString &semanticClass, classNames) {
333
KoRdfSemanticItem *si = KoRdfSemanticItem::createSemanticItem(qobject_cast<QObject*>(const_cast<KoDocumentRdf*>(this)), this, semanticClass);
332
hKoRdfSemanticItem si = KoRdfSemanticItem::createSemanticItem(qobject_cast<QObject*>(const_cast<KoDocumentRdf*>(this)), this, semanticClass);
334
333
si->saveUserStylesheets(d->model, context);
454
453
// lastKnownObjects is used to perform a sematic set diff
455
454
// at return time d->foafObjects will have any new objects and
456
455
// ones that are no longer available will be removed.
457
QList<KoRdfFoaF*> lastKnownObjects = d->foafObjects;
456
QList<hKoRdfFoaF> lastKnownObjects = d->foafObjects;
459
458
// uniqfilter is needed because soprano is not honouring
460
459
// the DISTINCT sparql keyword
470
KoRdfFoaF *newItem = new KoRdfFoaF(this, this, it);
469
hKoRdfFoaF newItem(new KoRdfFoaF(0, this, it));
471
470
QString newItemLs = newItem->linkingSubject().toString();
472
foreach (KoRdfFoaF *semItem, lastKnownObjects) {
471
foreach (hKoRdfFoaF semItem, lastKnownObjects) {
473
472
if (newItemLs == semItem->linkingSubject().toString()) {
474
473
lastKnownObjects.removeAll(semItem);
532
531
Soprano::QueryResultIterator it =
533
532
m->executeQuery(sparqlQuery,
534
533
Soprano::Query::QueryLanguageSparql);
535
QList<KoRdfCalendarEvent*> lastKnownObjects = d->calObjects;
534
QList<hKoRdfCalendarEvent> lastKnownObjects = d->calObjects;
536
535
// uniqfilter is needed because soprano is not honouring
537
536
// the DISTINCT sparql keyword
538
537
QSet<QString> uniqfilter;
544
543
RDEBUG << " g:" << it.binding("g").toString();
545
544
RDEBUG << " uid:" << it.binding("uid").toString();
547
KoRdfCalendarEvent *newItem(new KoRdfCalendarEvent(this, this, it));
546
hKoRdfCalendarEvent newItem(new KoRdfCalendarEvent(0, this, it));
548
547
QString newItemLs = newItem->linkingSubject().toString();
549
foreach (KoRdfCalendarEvent *semItem, lastKnownObjects) {
548
foreach (hKoRdfCalendarEvent semItem, lastKnownObjects) {
550
549
if (newItemLs == semItem->linkingSubject().toString()) {
551
550
lastKnownObjects.removeAll(semItem);
558
556
d->calObjects << newItem;
561
foreach (KoRdfCalendarEvent *semItem, lastKnownObjects) {
559
foreach (hKoRdfCalendarEvent semItem, lastKnownObjects) {
562
560
d->calObjects.removeAll(semItem);
564
562
RDEBUG << "calendarEvents() size:" << d->calObjects.size() << endl;
602
void KoDocumentRdf::addLocations(Soprano::Model *m, QList<KoRdfLocation*> &ret, bool isGeo84,
600
void KoDocumentRdf::addLocations(QSharedPointer<Soprano::Model> m, QList<hKoRdfLocation> &ret, bool isGeo84,
603
601
const QString &sparqlQuery)
605
603
Soprano::QueryResultIterator it = m->executeQuery(sparqlQuery,
610
608
if (uniqFilter.shouldSkip(it))
613
KoRdfLocation *semObj(new KoRdfLocation(this, this, it, isGeo84));
611
hKoRdfLocation semObj(new KoRdfLocation(0, this, it, isGeo84));
616
614
RDEBUG << "addLocations() size:" << ret.size() << endl;
619
QList<KoRdfLocation*> KoDocumentRdf::locations(Soprano::Model *m)
617
QList<hKoRdfLocation> KoDocumentRdf::locations(QSharedPointer<Soprano::Model> m)
625
623
RDEBUG << "locations(top) full-model.sz:" << d->model->statementCount();
626
624
RDEBUG << " passed model.size:" << m->statementCount();
627
QList<KoRdfLocation*> currentKoRdfLocations;
625
QList<hKoRdfLocation> currentKoRdfLocations;
628
626
addLocations(m, currentKoRdfLocations, false,
629
627
" prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
630
628
" prefix foaf: <http://xmlns.com/foaf/0.1/> \n"
669
667
// Creating a list of locations each time similifies the query and discovery process
670
668
// at the expense of this little mess to merge the new and old with explicit pointer
671
669
// and object lifetime handling
672
QList<KoRdfLocation*> removeSet;
673
foreach (KoRdfLocation *oldItem, d->locObjects) {
670
QList<hKoRdfLocation> removeSet;
671
foreach (hKoRdfLocation oldItem, d->locObjects) {
674
672
QString oldItemLs = oldItem->linkingSubject().toString();
675
673
bool found = false;
676
foreach (KoRdfLocation *newItem, currentKoRdfLocations) {
674
foreach (hKoRdfLocation newItem, currentKoRdfLocations) {
677
675
if (oldItemLs == newItem->linkingSubject().toString()) {
687
foreach (KoRdfLocation *item, removeSet) {
685
foreach (hKoRdfLocation item, removeSet) {
688
686
d->locObjects.removeAll(item);
691
QList<KoRdfLocation*> addedSet;
692
foreach (KoRdfLocation *newItem, currentKoRdfLocations) {
689
foreach (hKoRdfLocation newItem, currentKoRdfLocations) {
693
690
QString newItemLs = newItem->linkingSubject().toString();
694
691
bool found = false;
695
foreach (KoRdfLocation *oldItem, d->locObjects) {
692
foreach (hKoRdfLocation oldItem, d->locObjects) {
696
693
if (newItemLs == oldItem->linkingSubject().toString()) {
702
699
d->locObjects << newItem;
706
foreach (KoRdfLocation *item, addedSet) {
707
currentKoRdfLocations.removeAll(item);
710
RDEBUG << "locations(end) deleting duplicates size:" << currentKoRdfLocations.size() << endl;
711
qDeleteAll(currentKoRdfLocations);
712
703
RDEBUG << "locations(end) size:" << d->locObjects.size() << endl;
713
704
return d->locObjects;
716
void KoDocumentRdf::dumpModel(const QString &msg, Soprano::Model *m) const
707
void KoDocumentRdf::dumpModel(const QString &msg, QSharedPointer<Soprano::Model> m) const
827
818
_model->addStatements(addList);
830
void KoDocumentRdf::expandStatementsSubjectPointsTo(Soprano::Model *_model, const Soprano::Node &n) const
821
void KoDocumentRdf::expandStatementsSubjectPointsTo(QSharedPointer<Soprano::Model> _model, const Soprano::Node &n) const
832
823
Q_ASSERT(_model);
833
824
Q_ASSERT(d->model);
841
832
_model->addStatements(addList);
844
void KoDocumentRdf::expandStatementsToIncludeRdfListsRecurse(Soprano::Model *_model,
835
void KoDocumentRdf::expandStatementsToIncludeRdfListsRecurse(QSharedPointer<Soprano::Model> _model,
845
836
QList<Statement> &addList, const Soprano::Node &n) const
847
838
Q_ASSERT(_model);
891
882
_model->addStatements(addList);
894
void KoDocumentRdf::expandStatements(Soprano::Model *model) const
885
void KoDocumentRdf::expandStatements(QSharedPointer<Soprano::Model> model) const
897
888
expandStatementsReferencingSubject(model);
919
910
KoTextInlineRdf *obj = findInlineRdfByID(xmlid);
921
912
QPair<int, int> ret = obj->findExtent();
922
RDEBUG << "have inline obj, extent:" << ret;
913
RDEBUG << "(Semantic) have inline obj, extent:" << ret;
925
916
return QPair<int, int>(0, 0);
1057
Soprano::Model *KoDocumentRdf::findStatements(const QString &xmlid, int depth)
1048
QSharedPointer<Soprano::Model> KoDocumentRdf::findStatements(const QString &xmlid, int depth)
1059
Soprano::Model *ret(Soprano::createModel());
1050
QSharedPointer<Soprano::Model> ret(Soprano::createModel());
1061
1052
addStatements(ret, xmlid);
1062
1053
for (int i = 1; i < depth; ++i) {
1068
Soprano::Model *KoDocumentRdf::findStatements(KoTextEditor *handler, int depth)
1059
QSharedPointer<Soprano::Model> KoDocumentRdf::findStatements(KoTextEditor *handler, int depth)
1070
1061
Q_ASSERT(d->model);
1072
Soprano::Model *ret(Soprano::createModel());
1063
QSharedPointer<Soprano::Model> ret(Soprano::createModel());
1075
1066
QString xmlid = findXmlId(handler);
1183
1174
RDEBUG << "done";
1186
void KoDocumentRdf::emitSemanticObjectAdded(KoRdfSemanticItem *item) const
1188
emit semanticObjectAdded(item);
1191
void KoDocumentRdf::emitSemanticObjectAddedConst(KoRdfSemanticItem *const item) const
1193
emit semanticObjectAdded(item);
1196
void KoDocumentRdf::emitSemanticObjectUpdated(KoRdfSemanticItem *item)
1198
RDEBUG << "item:" << item;
1177
void KoDocumentRdf::emitSemanticObjectAdded(hKoRdfSemanticItem item) const
1179
emit semanticObjectAdded(item);
1182
void KoDocumentRdf::emitSemanticObjectAddedConst(hKoRdfSemanticItem const item) const
1184
emit semanticObjectAdded(item);
1187
void KoDocumentRdf::emitSemanticObjectUpdated(hKoRdfSemanticItem item)
1201
1191
// reflow the formatting for each view of the semanticItem, in reverse document order
1211
1201
emit semanticObjectUpdated(item);
1214
void KoDocumentRdf::emitSemanticObjectViewSiteUpdated(KoRdfSemanticItem *item, const QString &xmlid)
1204
void KoDocumentRdf::emitSemanticObjectViewSiteUpdated(hKoRdfSemanticItem item, const QString &xmlid)
1216
RDEBUG << "item:" << item;
1218
1207
RDEBUG << "xmlid:" << xmlid << " reflow item:" << item->name();
1219
1208
emit semanticObjectViewSiteUpdated(item, xmlid);
1234
KoDocumentRdf::reflowItem::reflowItem(KoRdfSemanticItem *si, const QString &xmlid, KoSemanticStylesheet *ss, const QPair< int, int > &extent)
1223
KoDocumentRdf::reflowItem::reflowItem(hKoRdfSemanticItem si, const QString &xmlid, hKoSemanticStylesheet ss, const QPair< int, int > &extent)
1237
1226
, m_xmlid(xmlid)
1242
void KoDocumentRdf::insertReflow(QMap<int, reflowItem> &col, KoRdfSemanticItem *obj, KoSemanticStylesheet *ss)
1231
void KoDocumentRdf::insertReflow(QMap<int, reflowItem> &col, hKoRdfSemanticItem obj, hKoSemanticStylesheet ss)
1244
1233
RDEBUG << "reflowing object:" << obj->name();
1245
1234
QStringList xmlidlist = obj->xmlIdList();
1254
void KoDocumentRdf::insertReflow(QMap<int, reflowItem> &col, KoRdfSemanticItem *obj,
1243
void KoDocumentRdf::insertReflow(QMap<int, reflowItem> &col, hKoRdfSemanticItem obj,
1255
1244
const QString &sheetType, const QString &stylesheetName)
1257
KoSemanticStylesheet *ss = obj->findStylesheetByName(sheetType, stylesheetName);
1246
hKoSemanticStylesheet ss = obj->findStylesheetByName(sheetType, stylesheetName);
1258
1247
insertReflow(col, obj, ss);
1261
void KoDocumentRdf::insertReflow(QMap<int, reflowItem> &col, KoRdfSemanticItem *obj)
1250
void KoDocumentRdf::insertReflow(QMap<int, reflowItem> &col, hKoRdfSemanticItem obj)
1263
1252
RDEBUG << "reflowing object:" << obj->name();
1264
1253
foreach (const QString &xmlid, obj->xmlIdList()) {
1265
1254
QPair<int, int> extent = findExtent(xmlid);
1266
1255
RDEBUG << "format(), adding reflow xmlid location:" << xmlid << " extent:" << extent;
1267
reflowItem item(obj, xmlid, 0, extent);
1256
reflowItem item(obj, xmlid, hKoSemanticStylesheet(0), extent);
1268
1257
col.insert(extent.first, item);
1290
QList<KoSemanticStylesheet*> KoDocumentRdf::userStyleSheetList(const QString& className) const
1279
QList<hKoSemanticStylesheet> KoDocumentRdf::userStyleSheetList(const QString& className) const
1292
1281
return d->userStylesheets[className];
1295
void KoDocumentRdf::setUserStyleSheetList(const QString& className,const QList<KoSemanticStylesheet*>& l)
1284
void KoDocumentRdf::setUserStyleSheetList(const QString& className,const QList<hKoSemanticStylesheet>& l)
1297
1286
d->userStylesheets[className] = l;