KDevelop API Documentation

dockdevtocplugin.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Alexander Dymo                                  *
00003  *   cloudtemple@mksat.net                                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #include "dockdevtocplugin.h"
00021 
00022 #include <unistd.h>
00023 
00024 #include <qdom.h>
00025 #include <qfile.h>
00026 #include <qfileinfo.h>
00027 #include <qdialog.h>
00028 
00029 #include <kurl.h>
00030 #include <kaboutdata.h>
00031 #include <kconfig.h>
00032 #include <klocale.h>
00033 #include <klistview.h>
00034 #include <kstandarddirs.h>
00035 
00036 #include <urlutil.h>
00037 #include <kdevgenericfactory.h>
00038 
00039 #include "../../../../config.h"
00040 
00041 class TOCDocumentationCatalogItem: public DocumentationCatalogItem
00042 {
00043 public:
00044     TOCDocumentationCatalogItem(const QString &tocFile, DocumentationPlugin* plugin,
00045         KListView *parent, const QString &name)
00046         :DocumentationCatalogItem(plugin, parent, name), m_tocFile(tocFile)
00047     {
00048     }
00049     TOCDocumentationCatalogItem(const QString &tocFile, DocumentationPlugin* plugin,
00050         DocumentationItem *parent, const QString &name)
00051         :DocumentationCatalogItem(plugin, parent, name), m_tocFile(tocFile)
00052     {
00053     }
00054     QString tocFile() const { return m_tocFile; }
00055     
00056 private:
00057     QString m_tocFile;
00058 };
00059 
00060 
00061 static const KAboutData data("dockdevtocplugin", I18N_NOOP("KDevelopTOC documentation plugin"), "1.0");
00062 typedef KDevGenericFactory<DocKDevTOCPlugin> DocKDevTOCPluginFactory;
00063 K_EXPORT_COMPONENT_FACTORY( libdockdevtocplugin, DocKDevTOCPluginFactory(&data) )
00064 
00065 DocKDevTOCPlugin::DocKDevTOCPlugin(QObject* parent, const char* name,
00066     const QStringList /*args*/)
00067     :DocumentationPlugin(DocKDevTOCPluginFactory::instance()->config(), parent, name)
00068 {
00069     setCapabilities(Index);
00070     autoSetup();
00071 }
00072 
00073 DocKDevTOCPlugin::~DocKDevTOCPlugin()
00074 {
00075 }
00076 
00077 QString DocKDevTOCPlugin::pluginName() const
00078 {
00079     return i18n("KDevelopTOC Documentation Collection");
00080 }
00081 
00082 DocumentationCatalogItem* DocKDevTOCPlugin::createCatalog(KListView* contents, const QString& title, const QString& url)
00083 {
00084     return new TOCDocumentationCatalogItem(url, this, contents, title);
00085 }
00086 
00087 QPair<KFile::Mode, QString> DocKDevTOCPlugin::catalogLocatorProps()
00088 {
00089     return QPair<KFile::Mode, QString>(KFile::File, "*.toc");
00090 }
00091 
00092 QString DocKDevTOCPlugin::catalogTitle(const QString& url)
00093 {
00094     QFileInfo fi(url);
00095     if (!fi.exists())
00096         return QString::null;
00097 
00098     QFile f(url);
00099     if (!f.open(IO_ReadOnly))
00100         return QString::null;
00101     
00102     QDomDocument doc;
00103     if (!doc.setContent(&f) || (doc.doctype().name() != "kdeveloptoc"))
00104         return QString::null;
00105     f.close();
00106 
00107     QDomElement titleEl = doc.documentElement().namedItem("title").toElement();
00108 
00109     return titleEl.firstChild().toText().data();
00110 }
00111 
00112 
00113 QStringList DocKDevTOCPlugin::fullTextSearchLocations()
00114 {
00115     return QStringList();
00116 }
00117 
00118 bool DocKDevTOCPlugin::needRefreshIndex(DocumentationCatalogItem* /*item*/)
00119 {
00120     return false;
00121 }
00122 
00123 void DocKDevTOCPlugin::autoSetupPlugin()
00124 {
00125     QStringList tocsDir = DocKDevTOCPluginFactory::instance()->dirs()->findAllResources("data", "kdevdocumentation/tocs/*.toc");
00126     
00127     for (QStringList::const_iterator it = tocsDir.begin(); it != tocsDir.end(); ++it)
00128     {
00129         config->setGroup("Locations");
00130         config->writePathEntry(catalogTitle(*it), *it);
00131     }
00132 }
00133 
00134 void DocKDevTOCPlugin::createIndex(IndexBox* index, DocumentationCatalogItem* item)
00135 {
00136     TOCDocumentationCatalogItem *tocItem = dynamic_cast<TOCDocumentationCatalogItem *>(item);
00137     if (!tocItem)
00138         return;
00139     
00140     QFileInfo fi(tocItem->tocFile());
00141 
00142     QFile f(tocItem->tocFile());
00143     if (!f.open(IO_ReadOnly))
00144     {
00145         kdDebug(9002) << "Could not read" << tocItem->tocFile() << endl;
00146         return;
00147     }
00148     QDomDocument doc;
00149     if (!doc.setContent(&f) || doc.doctype().name() != "kdeveloptoc")
00150     {
00151         kdDebug(9002) << "Not a valid kdeveloptoc file: " << tocItem->tocFile() << endl;
00152         return;
00153     }
00154     f.close();
00155 
00156     QDomElement docEl = doc.documentElement();
00157     QDomElement baseEl = docEl.namedItem("base").toElement();
00158     
00159     QString base;
00160     if (!baseEl.isNull())
00161         base = baseEl.attribute("href", QString::null);
00162     
00163     QDomElement indexEl = docEl.namedItem("index").toElement();
00164     QDomElement childEl = indexEl.firstChild().toElement();
00165     while (!childEl.isNull())
00166     {
00167         if (childEl.tagName() == "entry")
00168         {
00169             QString name = childEl.attribute("name");
00170             QString url = childEl.attribute("url");
00171             
00172             IndexItemProto *ii = new IndexItemProto(this, item, index, name, item->text(0));
00173             ii->addURL(KURL(constructURL(base, url)));
00174         }
00175         childEl = childEl.nextSibling().toElement();
00176     }
00177 }
00178 
00179 void DocKDevTOCPlugin::createTOC(DocumentationCatalogItem* item)
00180 {
00181     TOCDocumentationCatalogItem *tocItem = dynamic_cast<TOCDocumentationCatalogItem *>(item);
00182     if (!tocItem)
00183         return;
00184     
00185     QFileInfo fi(tocItem->tocFile());
00186 
00187     QFile f(tocItem->tocFile());
00188     if (!f.open(IO_ReadOnly))
00189     {
00190         kdDebug(9002) << "Could not read" << tocItem->tocFile() << endl;
00191         return;
00192     }
00193     QDomDocument doc;
00194     if (!doc.setContent(&f) || doc.doctype().name() != "kdeveloptoc")
00195     {
00196         kdDebug(9002) << "Not a valid kdeveloptoc file: " << tocItem->tocFile() << endl;
00197         return;
00198     }
00199     f.close();
00200 
00201     QDomElement docEl = doc.documentElement();
00202     QDomElement baseEl = docEl.namedItem("base").toElement();
00203     
00204     QString base;
00205     if (!baseEl.isNull())
00206         base = baseEl.attribute("href", QString::null);
00207     
00208     QDomElement childEl = docEl.lastChild().toElement();
00209     addTocSect(tocItem, childEl, base, 1);
00210 }
00211 
00212 void DocKDevTOCPlugin::addTocSect(DocumentationItem *parent, QDomElement childEl, const QString &base, uint level)
00213 {
00214     while (!childEl.isNull())
00215     {
00216         if (childEl.tagName() == QString("tocsect%1").arg(level))
00217         {
00218             QString name = childEl.attribute("name");
00219             QString url = childEl.attribute("url");
00220             
00221             DocumentationItem *item = new DocumentationItem(level == 1 ? DocumentationItem::Book : DocumentationItem::Document, parent, name);
00222             item->setURL(KURL(constructURL(base, url)));
00223 
00224             QDomElement grandchildEl = childEl.lastChild().toElement();
00225             addTocSect(item, grandchildEl, base, level+1);
00226         }
00227         childEl = childEl.previousSibling().toElement();
00228     }
00229 }
00230 
00231 void DocKDevTOCPlugin::setCatalogURL(DocumentationCatalogItem* item)
00232 {
00233     TOCDocumentationCatalogItem *tocItem = dynamic_cast<TOCDocumentationCatalogItem *>(item);
00234     if (!tocItem)
00235         return;
00236     
00237     QFileInfo fi(tocItem->tocFile());
00238 
00239     QFile f(tocItem->tocFile());
00240     if (!f.open(IO_ReadOnly))
00241     {
00242         kdDebug(9002) << "Could not read" << tocItem->tocFile() << endl;
00243         return;
00244     }
00245     QDomDocument doc;
00246     if (!doc.setContent(&f) || doc.doctype().name() != "kdeveloptoc")
00247     {
00248         kdDebug(9002) << "Not a valid kdeveloptoc file: " << tocItem->tocFile() << endl;
00249         return;
00250     }
00251     f.close();
00252 
00253     QDomElement docEl = doc.documentElement();
00254     QDomElement baseEl = docEl.namedItem("base").toElement();
00255     
00256     if (item->url().isEmpty())
00257     {
00258         if (baseEl.isNull())
00259             item->setURL(KURL());
00260         else
00261             item->setURL(KURL(constructURL(baseEl.attribute("href", QString::null),
00262                 baseEl.attribute("url", QString::null))));
00263     }
00264 }
00265 
00266 QString DocKDevTOCPlugin::constructURL(const QString &base, const QString &url)
00267 {
00268     if (base.isEmpty() && !url.isEmpty())
00269         return url;
00270     if (!url.isEmpty())
00271         return base.endsWith("/") ? base + url : base + "/" + url;
00272     else
00273         return base;
00274 }
00275 
00276 #include "dockdevtocplugin.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Mar 23 00:03:56 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003