syndication/rdf
document.cpp
00001 /* 00002 * This file is part of the syndication library 00003 * 00004 * Copyright (C) 2006 Frank Osterfeld <osterfeld@kde.org> 00005 * 00006 * This library is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Library General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Library General Public License 00017 * along with this library; see the file COPYING.LIB. If not, write to 00018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 * Boston, MA 02110-1301, USA. 00020 * 00021 */ 00022 00023 #include "document.h" 00024 #include "dublincore.h" 00025 #include "image.h" 00026 #include "item.h" 00027 #include "model.h" 00028 #include "model_p.h" 00029 #include "resource.h" 00030 #include "rssvocab.h" 00031 #include "sequence.h" 00032 #include "statement.h" 00033 #include "syndicationinfo.h" 00034 #include "textinput.h" 00035 00036 #include <documentvisitor.h> 00037 #include <tools.h> 00038 00039 #include <QtCore/QList> 00040 #include <QtCore/QString> 00041 #include <QtCore/QStringList> 00042 #include <QtCore/QVector> 00043 00044 using namespace boost; 00045 00046 namespace Syndication { 00047 namespace RDF { 00048 00049 class Document::Private 00050 { 00051 public: 00052 Private() : itemTitleContainsMarkup(false), 00053 itemTitlesGuessed(false), 00054 itemDescriptionContainsMarkup(false), 00055 itemDescGuessed(false) 00056 {} 00057 mutable bool itemTitleContainsMarkup; 00058 mutable bool itemTitlesGuessed; 00059 mutable bool itemDescriptionContainsMarkup; 00060 mutable bool itemDescGuessed; 00061 shared_ptr<Model::ModelPrivate> modelPrivate; 00062 }; 00063 00064 Document::Document() : Syndication::SpecificDocument(), 00065 ResourceWrapper(), 00066 d(new Private) 00067 { 00068 d->modelPrivate = resource()->model().d; 00069 } 00070 00071 Document::Document(ResourcePtr resource) : Syndication::SpecificDocument(), 00072 ResourceWrapper(resource), 00073 d(new Private) 00074 { 00075 d->modelPrivate = resource->model().d; 00076 } 00077 00078 Document::Document(const Document& other) : SpecificDocument(other), ResourceWrapper(other), 00079 d(new Private) 00080 { 00081 *d = *(other.d); 00082 } 00083 00084 Document::~Document() 00085 { 00086 delete d; 00087 } 00088 00089 00090 bool Document::operator==(const Document& other) const 00091 { 00092 return ResourceWrapper::operator==(other); 00093 } 00094 00095 00096 Document& Document::operator=(const Document& other) 00097 { 00098 ResourceWrapper::operator=(other); 00099 *d = *(other.d); 00100 00101 return *this; 00102 } 00103 00104 00105 bool Document::accept(DocumentVisitor* visitor) 00106 { 00107 return visitor->visitRDFDocument(this); 00108 } 00109 00110 bool Document::isValid() const 00111 { 00112 return !isNull(); 00113 } 00114 00115 QString Document::title() const 00116 { 00117 QString str = resource()->property(RSSVocab::self()->title())->asString(); 00118 return normalize(str); 00119 00120 } 00121 00122 QString Document::description() const 00123 { 00124 QString str = resource()->property(RSSVocab::self()->description())->asString(); 00125 return normalize(str); 00126 } 00127 00128 QString Document::link() const 00129 { 00130 return resource()->property(RSSVocab::self()->link())->asString(); 00131 } 00132 00133 DublinCore Document::dc() const 00134 { 00135 return DublinCore(resource()); 00136 } 00137 00138 SyndicationInfo Document::syn() const 00139 { 00140 return SyndicationInfo(resource()); 00141 } 00142 00143 00144 struct SortItem { 00145 Item item; 00146 int index; 00147 }; 00148 00149 struct LessThanByIndex { 00150 bool operator()(const SortItem& lhs, const SortItem& rhs) const { 00151 return lhs.index < rhs.index; 00152 } 00153 }; 00154 00155 static QList<Item> sortListToMatchSequence(QList<Item> items, const QStringList& uriSequence) { 00156 QVector<SortItem> toSort; 00157 toSort.reserve(items.size()); 00158 Q_FOREACH(const Item& i, items) { 00159 SortItem item; 00160 item.item = i; 00161 item.index = uriSequence.indexOf(i.resource()->uri()); 00162 toSort.append(item); 00163 } 00164 qSort(toSort.begin(), toSort.end(), LessThanByIndex()); 00165 00166 int i = 0; 00167 Q_FOREACH(const SortItem& sortItem, toSort) { 00168 items[i] = sortItem.item; 00169 i++; 00170 } 00171 00172 return items; 00173 } 00174 00175 QList<Item> Document::items() const 00176 { 00177 QList<Item> list; 00178 00179 const QList<ResourcePtr> items = resource()->model().resourcesWithType(RSSVocab::self()->item()); 00180 DocumentPtr doccpy(new Document(*this)); 00181 Q_FOREACH (const ResourcePtr& i, items) 00182 list.append(Item(i, doccpy)); 00183 00184 if (resource()->hasProperty(RSSVocab::self()->items())) { 00185 NodePtr n = resource()->property(RSSVocab::self()->items())->object(); 00186 if (n->isSequence()) 00187 { 00188 Sequence* seq = static_cast<Sequence*>(n.get()); 00189 00190 const QList<NodePtr> seqItems = seq->items(); 00191 00192 QStringList uriSequence; 00193 uriSequence.reserve(seqItems.size()); 00194 00195 Q_FOREACH(const NodePtr& i, seqItems) 00196 if (i->isResource()) 00197 uriSequence.append(static_cast<Resource*>(i.get())->uri()); 00198 list = sortListToMatchSequence(list, uriSequence); 00199 } 00200 } 00201 00202 return list; 00203 } 00204 00205 Image Document::image() const 00206 { 00207 ResourcePtr img = resource()->property(RSSVocab::self()->image())->asResource(); 00208 00209 return img ? Image(img) : Image(); 00210 } 00211 00212 TextInput Document::textInput() const 00213 { 00214 ResourcePtr ti = resource()->property(RSSVocab::self()->textinput())->asResource(); 00215 00216 return ti ? TextInput(ti) : TextInput(); 00217 } 00218 00219 void Document::getItemTitleFormatInfo(bool* containsMarkup) const 00220 { 00221 if (!d->itemTitlesGuessed) 00222 { 00223 QString titles; 00224 QList<Item> litems = items(); 00225 00226 if (litems.isEmpty()) 00227 { 00228 d->itemTitlesGuessed = true; 00229 return; 00230 } 00231 00232 int nmax = litems.size() < 10 ? litems.size() : 10; // we check a maximum of 10 items 00233 int i = 0; 00234 00235 QList<Item>::ConstIterator it = litems.constBegin(); 00236 00237 while (i < nmax) 00238 { 00239 titles += (*it).originalTitle(); 00240 ++it; 00241 ++i; 00242 } 00243 00244 d->itemTitleContainsMarkup = stringContainsMarkup(titles); 00245 d->itemTitlesGuessed = true; 00246 } 00247 if (containsMarkup != 0L) 00248 *containsMarkup = d->itemTitleContainsMarkup; 00249 } 00250 00251 void Document::getItemDescriptionFormatInfo(bool* containsMarkup) const 00252 { 00253 if (!d->itemDescGuessed) 00254 { 00255 QString desc; 00256 QList<Item> litems = items(); 00257 00258 00259 if (litems.isEmpty()) 00260 { 00261 d->itemDescGuessed = true; 00262 return; 00263 } 00264 00265 int nmax = litems.size() < 10 ? litems.size() : 10; // we check a maximum of 10 items 00266 int i = 0; 00267 00268 QList<Item>::ConstIterator it = litems.constBegin(); 00269 00270 while (i < nmax) 00271 { 00272 desc += (*it).originalDescription(); 00273 ++it; 00274 ++i; 00275 } 00276 00277 d->itemDescriptionContainsMarkup = stringContainsMarkup(desc); 00278 d->itemDescGuessed = true; 00279 } 00280 00281 if (containsMarkup != 0L) 00282 *containsMarkup = d->itemDescriptionContainsMarkup; 00283 } 00284 00285 QString Document::debugInfo() const 00286 { 00287 QString info; 00288 info += QLatin1String("### Document: ###################\n"); 00289 info += QLatin1String("title: #") + title() + QLatin1String("#\n"); 00290 info += QLatin1String("link: #") + link() + QLatin1String("#\n"); 00291 info += QLatin1String("description: #") + description() + QLatin1String("#\n"); 00292 info += dc().debugInfo(); 00293 info += syn().debugInfo(); 00294 Image img = image(); 00295 if (!img.resource() == 0L) 00296 info += img.debugInfo(); 00297 TextInput input = textInput(); 00298 if (!input.isNull()) 00299 info += input.debugInfo(); 00300 00301 QList<Item> itlist = items(); 00302 QList<Item>::ConstIterator it = itlist.constBegin(); 00303 QList<Item>::ConstIterator end = itlist.constEnd(); 00304 for ( ; it != end; ++it) 00305 info += (*it).debugInfo(); 00306 00307 00308 info += QLatin1String("### Document end ################\n"); 00309 return info; 00310 } 00311 00312 } // namespace RDF 00313 } // namespace Syndication
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:57:54 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 7 2012 23:57:54 by doxygen 1.8.0 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.