KDevelop API Documentation

misc.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Bernd Gehrmann                                  *
00003  *   bernd@kdevelop.org                                                    *
00004  *   Copyright (C) 2003 by Alexander Dymo                                  *
00005  *   cloudtemple@mksat.net                                                 *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  ***************************************************************************/
00013 
00014 #include <qdir.h>
00015 #include <kconfig.h>
00016 #include <kdebug.h>
00017 #include <kglobal.h>
00018 #include <kinstance.h>
00019 #include <klocale.h>
00020 #include <kurl.h>
00021 #include <kstandarddirs.h>
00022 #include <kio/netaccess.h>
00023 #include <kapplication.h>
00024 #include <qdom.h>
00025 #include <qvaluestack.h>
00026 
00027 #include "domutil.h"
00028 #include "urlutil.h"
00029 //#ifndef INDEXER
00030 #include "doctreeviewfactory.h"
00031 //#endif
00032 #include "misc.h"
00033 #include "../../config.h"
00034 
00035 static KConfig *instanceConfig()
00036 {
00037 #ifdef INDEXER
00038     return KGlobal::config();
00039 #else
00040     return DocTreeViewFactory::instance()->config();
00041 #endif
00042 }
00043 
00044 void DocTreeViewTool::getLibraries(QStringList *libNames, QStringList *docDirs, QStringList *sourceDirs)
00045 {
00046     KConfig *config = instanceConfig();
00047     config->setGroup("DocTreeView");
00048     *libNames = config->readListEntry("LibrariesNames");
00049     *docDirs = config->readListEntry("LibrariesDocDirs");
00050     *sourceDirs = config->readListEntry("LibrariesSourceDirs");
00051 }
00052 
00053 void DocTreeViewTool::setLibraries(QStringList *libNames, QStringList *docDirs, QStringList *sourceDirs)
00054 {
00055     KConfig *config = instanceConfig();
00056     config->setGroup("DocTreeView");
00057     config->writeEntry("LibrariesNames", *libNames);
00058     config->writeEntry("LibrariesDocDirs", *docDirs );
00059     config->writeEntry("LibrariesSourceDirs", *sourceDirs);
00060 }
00061 
00062 void DocTreeViewTool::readLibraryDocs(QString dir, QStringList *itemNames, QStringList *fileNames)
00063 {
00064     QDir globaldir(dir + "/kdoc-reference");
00065     QStringList globalentries =
00066         globaldir.exists()? globaldir.entryList("*", QDir::Files) : QStringList();
00067     for (QStringList::Iterator it = globalentries.begin(); it != globalentries.end(); ++it) {
00068         *itemNames += QFileInfo(*it).baseName();
00069         *fileNames += globaldir.filePath(*it);
00070                 kdDebug(9002) << "Global: " << globaldir.filePath(*it) << endl;
00071     }
00072     QDir privatedir(QDir::homeDirPath() + "/.kdoc");
00073     QStringList privateentries =
00074         privatedir.exists()? privatedir.entryList("*", QDir::Files) : QStringList();
00075     for (QStringList::Iterator it = privateentries.begin(); it != privateentries.end(); ++it) {
00076         QDir libdir(dir + "/" + QFileInfo(*it).baseName());
00077         if (libdir.exists())
00078         {
00079             *itemNames += i18n("%1 (private)").arg(QFileInfo(*it).baseName());
00080             *fileNames += privatedir.filePath(*it);
00081             kdDebug(9002) << "Local: " << privatedir.filePath(*it) << endl;
00082         }
00083     } 
00084 }
00085 
00086 void DocTreeViewTool::getAllLibraries(QStringList *itemNames, QStringList *fileNames)
00087 {
00088     KConfig *config = instanceConfig();
00089     config->setGroup("DocTreeView");
00090     QString idx_path = config->readPathEntry("KDEDocDir", KDELIBS_DOCDIR);// + "/kdoc-reference";
00091 
00092     readLibraryDocs(idx_path, itemNames, fileNames);
00093 }
00094 
00095 
00096 void DocTreeViewTool::getHiddenLibraries(QStringList *hiddenNames)
00097 {
00098     KConfig *config = instanceConfig();
00099     config->setGroup("DocTreeView");
00100     *hiddenNames = config->readListEntry("LibrariesHidden");
00101 }
00102 
00103 
00104 void DocTreeViewTool::setHiddenLibraries(const QStringList &hiddenNames)
00105 {
00106     KConfig *config = instanceConfig();
00107     config->setGroup("DocTreeView");
00108     config->writeEntry("LibrariesHidden", hiddenNames);
00109 }
00110 
00111 
00112 void DocTreeViewTool::getBookmarks(QStringList *itemNames, QStringList *fileNames)
00113 {
00114     KConfig *config = instanceConfig();
00115     config->setGroup("DocTreeView");
00116     *itemNames = config->readListEntry("BookmarksTitle");
00117     *fileNames = config->readListEntry("BookmarksURL");
00118 }
00119 
00120 
00121 void DocTreeViewTool::setBookmarks(const QStringList &itemNames, const QStringList &fileNames)
00122 {
00123     KConfig *config = instanceConfig();
00124     config->setGroup("DocTreeView");
00125     config->writeEntry("BookmarksTitle", itemNames);
00126     config->writeEntry("BookmarksURL", fileNames);
00127 }
00128 
00129 void DocTreeViewTool::addBookmark(const QString& itemName, const QString & fileName)
00130 {
00131     KConfig *config = instanceConfig();
00132     config->setGroup("DocTreeView");
00133     QStringList iNames = config->readListEntry("BookmarksTitle");
00134     QStringList fNames = config->readListEntry("BookmarksURL");
00135     
00136     iNames.append( itemName );
00137     fNames.append( fileName );
00138     
00139     setBookmarks( iNames, fNames );
00140 }
00141 void DocTreeViewTool::removeBookmark(int index)
00142 {
00143     KConfig *config = instanceConfig();
00144     config->setGroup("DocTreeView");
00145     QStringList itemNames = config->readListEntry("BookmarksTitle");
00146     QStringList fileNames = config->readListEntry("BookmarksURL");
00147     
00148     fileNames.remove( fileNames.at( fileNames.size() - index ) );
00149     itemNames.remove( itemNames.at( itemNames.size() - index ) );
00150     
00151     setBookmarks( itemNames, fileNames );
00152 }
00153 
00154 QString DocTreeViewTool::tocDocDefaultLocation(const QString& fileName)
00155 {
00156     QFile f(fileName);
00157     if (!f.open(IO_ReadOnly)) {
00158         kdDebug(9002) << "Could not read doc toc: " << fileName << endl;
00159         return QString::null;
00160     }
00161     QDomDocument doc;
00162     if (!doc.setContent(&f) || doc.doctype().name() != "kdeveloptoc") {
00163         kdDebug(9002) << "Not a valid kdeveloptoc file: " << fileName << endl;
00164         return QString::null;
00165     }
00166     f.close();
00167     
00168     QDomElement docEl = doc.documentElement();
00169     QDomElement childEl = docEl.firstChild().toElement();
00170     QString base;
00171     while (!childEl.isNull()) 
00172     {
00173         if (childEl.tagName() == "base") 
00174         {
00175             base = childEl.attribute("href");
00176             if (!base.isEmpty())
00177                 base += "/";
00178             break;
00179         }
00180         childEl = childEl.nextSibling().toElement();
00181     }
00182     return base;
00183 }
00184 
00185 QString DocTreeViewTool::tocTitle(const QString& fileName)
00186 {
00187     QFile f(fileName);
00188     if (!f.open(IO_ReadOnly)) {
00189         kdDebug(9002) << "Could not read doc toc: " << fileName << endl;
00190         return QString::null;
00191     }
00192     QDomDocument doc;
00193     if (!doc.setContent(&f) || doc.doctype().name() != "kdeveloptoc") {
00194         kdDebug(9002) << "Not a valid kdeveloptoc file: " << fileName << endl;
00195         return QString::null;
00196     }
00197     f.close();
00198     QDomElement docEl = doc.documentElement();
00199     QDomElement childEl = docEl.firstChild().toElement();
00200     QString title;
00201     while (!childEl.isNull()) 
00202     {
00203         if (childEl.tagName() == "title") 
00204         {
00205             title = childEl.text();
00206             break;
00207         }
00208         childEl = childEl.nextSibling().toElement();
00209     }
00210     return title;
00211 }
00212 
00213 
00214 QString DocTreeViewTool::tocLocation(const QString& fileName)
00215 {
00216     KConfig *config = instanceConfig();
00217     config->setGroup("TocDirs");
00218     const QString docName( QFileInfo( fileName ).baseName() );
00219     return config->readPathEntry( docName, DocTreeViewTool::tocDocDefaultLocation( fileName ));
00220 }
00221 
00222 QString DocTreeViewTool::devhelpLocation(const QString& docName, const QString &defaultLocation)
00223 {
00224     KConfig *config = instanceConfig();
00225     config->setGroup("TocDevHelp");
00226     return config->readPathEntry( docName, defaultLocation);
00227 }
00228 
00229 QString DocTreeViewTool::devhelpLocation(const QString& fileName)
00230 {
00231     KConfig *config = instanceConfig();
00232     config->setGroup("TocDevHelp");
00233     QString docName = QFileInfo(fileName).baseName();
00234     BookInfo inf = DocTreeViewTool::devhelpInfo(fileName);
00235     return config->readPathEntry( docName, inf.defaultLocation);
00236 }
00237 
00238 
00239 void DocTreeViewTool::scanDevHelpDirs( const QString /*path*/ )
00240 {
00241 //    scanDevHelpOldWay(path);
00242     scanDevHelpNewWay();
00243 }
00244 
00245 BookInfo DocTreeViewTool::devhelpInfo(const QString& fileName)
00246 {
00247     BookInfo inf;
00248 
00249     QFileInfo fi(fileName);
00250     if (!fi.exists())
00251         return inf;
00252     QFile f(fileName);
00253     if (!f.open(IO_ReadOnly)) {
00254         return inf;
00255     }
00256     QDomDocument doc;
00257     if (!doc.setContent(&f)) {
00258         return inf;
00259     }
00260     f.close();
00261     QDomElement docEl = doc.documentElement();
00262     inf.name = docEl.attribute("name", QString::null);
00263     inf.title = docEl.attribute("title", QString::null);
00264     inf.author = docEl.attribute("author", QString::null);
00265     inf.defaultLocation = docEl.attribute("base", QString::null);
00266 
00267     return inf;
00268 }
00269 
00270 void DocTreeViewTool::scanDevHelpOldWay( const QString path )
00271 {
00272     KStandardDirs *dirs = DocTreeViewFactory::instance()->dirs();
00273 
00274     QString devhelpDir;
00275     if (path.isEmpty())
00276     {
00277         KConfig *config = instanceConfig();
00278         config->setGroup("DevHelp");
00279         devhelpDir = config->readPathEntry("DevHelpDir", "~/.devhelp");
00280     }
00281     else
00282         devhelpDir = path;
00283 
00284 
00285     if (devhelpDir.isEmpty())
00286         return;
00287 
00288 
00289     if (devhelpDir[devhelpDir.length()-1] == QChar('/'))
00290         devhelpDir.remove(devhelpDir.length()-1, 1);
00291     QDir d(devhelpDir + QString("/specs/"));
00292     if (! d.exists())
00293     {
00294         return;
00295     }
00296     d.setFilter( QDir::Files );
00297     //scan for *.devhelp files in spec directory
00298     const QFileInfoList *list = d.entryInfoList();
00299     QFileInfoListIterator it( *list );
00300     QFileInfo *fi;
00301     while ( (fi = it.current()) != 0 ) {
00302         if (fi->extension() == "devhelp")
00303         {
00304             //extract document information and store into $docdevhelp$ resource dir
00305 
00306             KURL src;
00307             src.setPath(fi->absFilePath());
00308             KURL dest;
00309             dest.setPath(dirs->saveLocation("docdevhelp") + fi->baseName() + ".devhelp");
00310 
00311             QString contentDirURL = devhelpDir + QString("/books/") + fi->baseName() + "/";
00312             QDir contentDir(contentDirURL);
00313             if (contentDir.exists())
00314             {
00315                 KConfig *config = DocTreeViewFactory::instance()->config();
00316                 config->setGroup("TocDevHelp");
00317                 QString temp = config->readPathEntry( fi->baseName());
00318                 if (temp.isEmpty() ) {
00319 #if defined(KDE_IS_VERSION)
00320 # if KDE_IS_VERSION(3,1,3)
00321 #  ifndef _KDE_3_1_3_
00322 #   define _KDE_3_1_3_
00323 #  endif
00324 # endif
00325 #endif
00326 #if defined(_KDE_3_1_3_)
00327                     config->writePathEntry( fi->baseName(), contentDirURL);
00328 #else
00329                     config->writeEntry( fi->baseName(), contentDirURL);
00330 #endif
00331                 }
00332             }
00333 
00334             KIO::NetAccess::copy(src, dest);
00335         }
00336         ++it;
00337     }
00338 }
00339 
00340 void DocTreeViewTool::scanDevHelpNewWay( )
00341 {
00342     QValueStack<QString> scanStack;
00343 //    scanStack << URLUtil::envExpand("$DEVHELP_SEARCH_PATH");
00344     scanStack << "/usr/share/devhelp/books/"
00345              << "/usr/local/share/devhelp/books"
00346              << "/opt/gnome/share/devhelp/books"
00347              << "/opt/gnome2/share/devhelp/books"
00348              << "/usr/share/gtk-doc/html"
00349              << "/usr/local/share/gtk-doc/html"
00350              << "/opt/gnome/share/gtk-doc/html"
00351              << "/opt/gnome2/share/gtk-doc/html";
00352 
00353     KConfig *config = instanceConfig();
00354     config->setGroup("DevHelp");
00355     QString confDir = config->readPathEntry("DevHelpDir", URLUtil::envExpand("$HOME/.devhelp"));
00356     if ((!confDir.isEmpty()) && (!scanStack.contains(confDir)))
00357         scanStack << confDir;
00358 
00359     QStringList scanList;
00360 
00361     QDir dir;
00362     do {
00363         dir.setPath(scanStack.pop());
00364         if (!dir.exists())
00365             continue;
00366         scanList << dir.path();
00367 
00368         const QFileInfoList *dirEntries = dir.entryInfoList();
00369         QPtrListIterator<QFileInfo> it(*dirEntries);
00370         for (; it.current(); ++it) {
00371             QString fileName = it.current()->fileName();
00372             if (fileName == "." || fileName == "..")
00373                 continue;
00374             QString path = it.current()->absFilePath();
00375             if (it.current()->isDir()) {
00376                 scanStack.push(path);
00377             }
00378         }
00379     } while (!scanStack.isEmpty());
00380 
00381     for (QStringList::const_iterator it = scanList.begin(); it != scanList.end(); ++it)
00382     {
00383         scanDevHelpNewWay(*it);
00384     }
00385 }
00386 
00387 void DocTreeViewTool::scanDevHelpNewWay( const QString & path )
00388 {
00389     KStandardDirs *dirs = DocTreeViewFactory::instance()->dirs();
00390 
00391     QDir d(path);
00392     if (! d.exists())
00393     {
00394         return;
00395     }
00396     d.setFilter( QDir::Files );
00397     //scan for *.devhelp files in spec directory
00398     const QFileInfoList *list = d.entryInfoList();
00399     QFileInfoListIterator it( *list );
00400     QFileInfo *fi;
00401     while ( (fi = it.current()) != 0 ) {
00402         if (fi->extension() == "devhelp")
00403         {
00404             //extract document information and store into $docdevhelp$ resource dir
00405 
00406             KURL src;
00407             src.setPath(fi->absFilePath());
00408             KURL dest;
00409             dest.setPath(dirs->saveLocation("docdevhelp") + fi->baseName() + ".devhelp");
00410 
00411             QString contentDirURL = QDir::cleanDirPath(path) + "/";
00412             QDir contentDir(contentDirURL);
00413             if (contentDir.exists())
00414             {
00415                 KConfig *config = DocTreeViewFactory::instance()->config();
00416                 config->setGroup("TocDevHelp");
00417 #if defined(KDE_IS_VERSION)
00418 # if KDE_IS_VERSION(3,1,3)
00419 #  ifndef _KDE_3_1_3_
00420 #   define _KDE_3_1_3_
00421 #  endif
00422 # endif
00423 #endif
00424 #if defined(_KDE_3_1_3_)
00425                 config->writePathEntry( fi->baseName(), contentDirURL);
00426 #else
00427                 config->writeEntry( fi->baseName(), contentDirURL);
00428 #endif
00429             }
00430 
00431             KIO::NetAccess::copy(src, dest);
00432         }
00433         ++it;
00434     }
00435 }
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:37 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003