KDevelop API Documentation

genericproject_part.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 Roberto Raggi                                      *
00003  *   roberto@kdevelop.org                                                  *
00004  *   Copyright (C) 2003 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 "genericproject_widget.h"
00015 #include "genericproject_part.h"
00016 
00017 #include "kdevcore.h"
00018 #include "kdevmainwindow.h"
00019 
00020 #include <kiconloader.h>
00021 #include <klocale.h>
00022 #include <kdevgenericfactory.h>
00023 #include <ktrader.h>
00024 #include <kdebug.h>
00025 #include <kmessagebox.h>
00026 #include <kparts/componentfactory.h>
00027 
00028 #include <qwhatsthis.h>
00029 #include <qdir.h>
00030 #include <qstringlist.h>
00031 
00032 #include "kdevbuildsystem.h"
00033 
00034 #include "genericlistviewitem.h"
00035 #include "variantserializer.h"
00036 
00037 #if QT_VERSION < 0x030100
00038 inline QString QDomDocument_toString(QDomDocument & cQDomDocument, int indent )
00039 {
00040     QString str;
00041     QTextStream s( str, IO_WriteOnly );
00042     cQDomDocument.save( s, indent );
00043 
00044     return str;
00045 };
00046 #endif // Qt<3.1.0
00047 
00048 typedef KDevGenericFactory<GenericProjectPart> GenericProjectFactory;
00049 static const KAboutData data("kdevgenericproject", I18N_NOOP("Build Tool"), "1.0");
00050 K_EXPORT_COMPONENT_FACTORY( libkdevgenericproject, GenericProjectFactory( &data ) )
00051 
00052 GenericProjectPart::GenericProjectPart(QObject *parent, const char *name, const QStringList& )
00053     : KDevProject("KDevPart", "kdevpart", parent, name ? name : "GenericProjectPart" )
00054 {
00055     setInstance( GenericProjectFactory::instance() );
00056     setXMLFile( "kdevgenericproject.rc" );
00057 
00058     m_widget = new GenericProjectWidget( this );
00059 
00060     QWhatsThis::add(m_widget, i18n("<b>Generic project manager</b><p>"
00061                                    "The project tree consists of two parts. The 'overview' "
00062                                    "in the upper half shows the groups, "
00063                                    "The 'details' view in the lower half shows the "
00064                                    "targets and files for the group selected in the overview."));
00065 
00066     mainWindow()->embedSelectViewRight( m_widget, i18n("Project Manager"), i18n("Generic project manager") );
00067 
00068     m_mainBuild = new BuildGroupItem("/", 0);
00069     m_mainGroup = m_widget->addGroup(m_mainBuild);
00070 }
00071 
00072 GenericProjectPart::~GenericProjectPart()
00073 {
00074     if ( m_widget )
00075         mainWindow()->removeView( m_widget );
00076     delete (GenericProjectWidget*) m_widget;
00077 }
00078 
00079 /*void GenericProjectPart::setMainGroup( BuildGroupItem * mainGroup )
00080 {
00081     m_mainGroup = mainGroup;
00082     emit mainGroupChanged( m_mainGroup );
00083 }*/
00084 
00085 void GenericProjectPart::openProject( const QString & dirName, const QString & projectName )
00086 {
00087     m_buildDir = dirName;
00088     m_projectDir = dirName;
00089     m_projectName = projectName;
00090 
00091     QDomDocument &dom = *projectDom();
00092 
00093     QString bsName = DomUtil::readEntry(dom, "/kdevgenericproject/backend");
00094     kdDebug() << "------------------------------------------------------" << endl;
00095     kdDebug() << "Looking for build system support for " << bsName << endl;
00096 
00097     KTrader::OfferList buildBackendsOffers =
00098         KTrader::self()->query(QString::fromLatin1("KDevelop/BuildSystems"),
00099                             QString::fromLatin1("[X-KDevelop-BuildSystem] == '%1'").arg(bsName));
00100     if (buildBackendsOffers.isEmpty()) {
00101         KMessageBox::sorry(0, i18n("No build system plugin for %1 found.").arg(bsName));
00102         return;
00103     }
00104 
00105     QValueList<KService::Ptr>::ConstIterator it;
00106     for (it = buildBackendsOffers.begin(); it != buildBackendsOffers.end(); ++it) {
00107         kdDebug() << (*it)->property("Name").toString() << endl;
00108         kdDebug() << (*it)->property("X-KDevelop-BuildSystem").toString() << endl;
00109     }
00110 
00111     kdDebug() << "Offers count: " << buildBackendsOffers.count() << endl;;
00112 
00113     KService::Ptr buildSystemService;
00114     KTrader::OfferList::ConstIterator serviceIt = buildBackendsOffers.begin();
00115     for ( ; serviceIt != buildBackendsOffers.end(); ++serviceIt )
00116        buildSystemService = *serviceIt;
00117 
00118 /*    QStringList args;
00119     QVariant prop = buildSystemService->property( "X-KDevelop-Args" );
00120     if ( prop.isValid() )
00121         args = QStringList::split( " ", prop.toString() );  , this->name(), args */
00122 /*    KDevBuildSystem *buildSystem = KParts::ComponentFactory
00123         ::createInstanceFromService<KDevBuildSystem>( buildSystemService, this,
00124         buildSystemService->name().latin1());*/
00125     int error;
00126     m_buildSystem = KParts::ComponentFactory
00127         ::createInstanceFromService<KDevBuildSystem>( buildSystemService, 0,
00128         buildSystemService->name().latin1(), QStringList(), &error);
00129     kdDebug() << "error: " << error << endl;
00130 
00131     if ( !m_buildSystem ) {
00132         KMessageBox::sorry(0, i18n("Could not create build system plugin for %1.").arg(bsName));
00133         return;
00134     }
00135     m_buildSystem->initProject(this);
00136 
00137 /*    // Set the default directory radio to "executable"
00138     if (DomUtil::readEntry(dom, "/kdevpascalproject/run/directoryradio") == "" ) {
00139         DomUtil::writeEntry(dom, "/kdevpascalproject/run/directoryradio", "executable");
00140     }
00141 */
00142     loadProjectConfig(DomUtil::readEntry(dom, "/kdevgenericproject/project"));
00143 
00144     KDevProject::openProject( dirName, projectName );    
00145 }
00146 
00147 void GenericProjectPart::closeProject( )
00148 {
00149 }
00150 
00151 QString GenericProjectPart::projectDirectory( ) const
00152 {
00153     return m_projectDir;
00154 }
00155 
00156 QString GenericProjectPart::projectName( ) const
00157 {
00158     return m_projectName;
00159 }
00160 
00161 DomUtil::PairList GenericProjectPart::runEnvironmentVars( ) const
00162 {
00163     return DomUtil::PairList();
00164 }
00165 
00166 QString GenericProjectPart::mainProgram( bool relative ) const
00167 {
00168     return "";
00169 }
00170 
00171 QString GenericProjectPart::runDirectory( ) const
00172 {
00173     return m_runDir;
00174 }
00175 
00176 QString GenericProjectPart::runArguments( ) const
00177 {
00178     return "";
00179 }
00180 
00181 QString GenericProjectPart::activeDirectory( ) const
00182 {
00183     return m_activeDir;
00184 }
00185 
00186 QString GenericProjectPart::buildDirectory( ) const
00187 {
00188     return m_buildDir;
00189 }
00190 
00191 QStringList GenericProjectPart::allFiles( ) const
00192 {
00193     QStringList path;
00194     return allFiles( m_mainBuild, path );
00195 }
00196 
00197 void GenericProjectPart::addFiles( const QStringList & fileList )
00198 {
00199     BuildTargetItem *tit = m_widget->activeTarget();
00200     if (!tit)
00201         return;
00202 
00203     for (QStringList::ConstIterator it = fileList.begin(); it != fileList.end(); ++it)
00204     {
00205     if( tit->fileByName(QFileInfo(*it).fileName()) != 0 )
00206         continue;
00207 
00208     QString absFilename = m_projectDir + QString::fromLatin1( "/" ) + (*it);
00209     addFilePrivate( *it, tit );
00210     }
00211 
00212     kdDebug() << "======> files: " << fileList.join( ", " ) << endl;
00213     emit addedFilesToProject( fileList );
00214 }
00215 
00216 void GenericProjectPart::addFile( const QString & fileName )
00217 {
00218     QStringList lst;
00219     lst << fileName;
00220     addFiles( lst );
00221 }
00222 
00223 void GenericProjectPart::addFilePrivate( const QString & fileName, BuildTargetItem *tit )
00224 {
00225     kdDebug() << "GenericProjectPart::addFilePrivate " << fileName << endl;
00226     KURL url;
00227     url.setPath(projectDirectory() + "/" + fileName);
00228     kdDebug() << "GenericProjectPart::addFilePrivate " << url.prettyURL() << endl;
00229     BuildFileItem *fit = new BuildFileItem(url, tit);
00230     m_widget->addFile(fit);
00231 }
00232 
00233 void GenericProjectPart::removeFiles( const QStringList & fileList )
00234 {
00235 }
00236 
00237 void GenericProjectPart::removeFile( const QString & fileName )
00238 {
00239     QStringList lst;
00240     removeFiles( lst );
00241 }
00242 
00243 void GenericProjectPart::loadProjectConfig( QString projectFile )
00244 {
00245     QDomDocument dom;
00246     DomUtil::openDOMFile(dom, QDir::cleanDirPath(projectDirectory() + "/" + projectFile));
00247 
00248 #if QT_VERSION >= 0x030100
00249     kdDebug() << dom.toString(4) << endl;
00250 #else
00251     kdDebug() << QDomDocument_toString(dom, 4) << endl;
00252 #endif
00253 
00254     QDomElement docElem = dom.documentElement();
00255 
00256     QDomNode n = docElem.firstChild();
00257     while( !n.isNull() ) {
00258         QDomElement e = n.toElement();
00259         if( (!e.isNull()) && (e.tagName() == "group") )
00260         {
00261             kdDebug() << "GenericProjectPart: parsing group " << e.attribute("name") << endl;
00262             parseGroup(e, m_mainGroup);
00263         }
00264         n = n.nextSibling();
00265     }
00266 }
00267 
00268 void GenericProjectPart::parseGroup( const QDomElement & el, const GenericGroupListViewItem *parentGroup )
00269 {
00270     GenericGroupListViewItem *group = createGroupItem(el, parentGroup);
00271 
00272     QDomNode n = el.firstChild();
00273     while( !n.isNull() ) {
00274         QDomElement e = n.toElement();
00275         if (e.isNull())
00276             return;
00277         if (e.tagName() == "group")
00278         {
00279             kdDebug() << "GenericProjectPart: parsing group " << e.attribute("name") << endl;
00280             parseGroup(e, group);
00281         }
00282         else if (e.tagName() == "target")
00283         {
00284             kdDebug() << "GenericProjectPart: parsing target " << e.attribute("name") << endl;
00285             parseTarget(e, group->groupItem());
00286         }
00287 
00288         n = n.nextSibling();
00289     }
00290 }
00291 
00292 void GenericProjectPart::parseTarget( const QDomElement & el, BuildGroupItem *group )
00293 {
00294     BuildTargetItem *ti = createTargetItem(el, group);
00295 
00296     QDomNode n = el.firstChild();
00297     while( !n.isNull() ) {
00298         QDomElement e = n.toElement();
00299         if (e.isNull())
00300             return;
00301         if (e.tagName() == "file")
00302         {
00303             kdDebug() << "GenericProjectPart: parsing file " << e.attribute("name") << endl;
00304             parseFile(e, ti);
00305         }
00306         n = n.nextSibling();
00307     }
00308 }
00309 
00310 void GenericProjectPart::parseFile( const QDomElement & el, BuildTargetItem *target )
00311 {
00312     createFileItem(el, target);
00313 }
00314 
00315 GenericGroupListViewItem * GenericProjectPart::createGroupItem( const QDomElement & el, const GenericGroupListViewItem *parent )
00316 {
00317     BuildGroupItem *it = new BuildGroupItem(el.attribute("name"), parent->groupItem());
00318     kdDebug() << "adding group to widget " << el.attribute("name") << endl;
00319     GenericGroupListViewItem *git = m_widget->addGroup(it);
00320 
00321     QDomNode n = el.firstChild();
00322     while( !n.isNull() ) {
00323         QDomElement e = n.toElement();
00324         if (e.isNull())
00325             return 0;
00326         if (e.tagName() == "attribute")
00327         {
00328             it->setAttribute(e.attribute("name") , VariantSerializer::loadValue(e));
00329             kdDebug() << "GenericProjectPart: parsing attribute " << e.tagName() << endl;
00330         }
00331         n = n.nextSibling();
00332     }
00333 
00334     return git;
00335 }
00336 
00337 BuildTargetItem * GenericProjectPart::createTargetItem( const QDomElement & el, BuildGroupItem *group )
00338 {
00339     BuildTargetItem *it = new BuildTargetItem(el.attribute("name"), group);
00340     return it;
00341 }
00342 
00343 BuildFileItem * GenericProjectPart::createFileItem( const QDomElement & el, BuildTargetItem *target )
00344 {
00345     KURL url;
00346     kdDebug() << "GenericProjectPart::createFileItem url" << endl;
00347     url.setPath(QDir::cleanDirPath(projectDirectory() + "/" + target->parentGroup()->path() + "/" + el.attribute("name")));
00348     kdDebug() << "GenericProjectPart::createFileItem new" << endl;
00349     BuildFileItem *it = new BuildFileItem(url, target);
00350     return it;
00351 }
00352 
00353 KDevBuildSystem *GenericProjectPart::buildSystem( ) const
00354 {
00355     return m_buildSystem;
00356 }
00357 
00358 QStringList GenericProjectPart::allFiles( BuildGroupItem * group, QStringList& path ) const
00359 {
00360     QStringList fileList;
00361 
00362     if( group != m_mainBuild )
00363         path.push_back( group->name() );
00364 
00365     const QValueList<BuildTargetItem*> targets = group->targets();
00366     for( QValueList<BuildTargetItem*>::ConstIterator it=targets.begin(); it!=targets.end(); ++it )
00367     {
00368     fileList += allFiles( *it, path );
00369     }
00370 
00371     const QValueList<BuildGroupItem*> groups = group->groups();
00372     for( QValueList<BuildGroupItem*>::ConstIterator it=groups.begin(); it!=groups.end(); ++it )
00373     {
00374     fileList += allFiles( *it, path );
00375     }
00376 
00377     if( group != m_mainBuild )
00378         path.pop_back();
00379 
00380     return fileList;
00381 }
00382 
00383 QStringList GenericProjectPart::allFiles( BuildTargetItem * target, QStringList& path ) const
00384 {
00385     QStringList fileList;
00386 
00387     int prjdir = m_projectDir.length() + 1;
00388     const QValueList<BuildFileItem*> files = target->files();
00389     for( QValueList<BuildFileItem*>::ConstIterator it=files.begin(); it!=files.end(); ++it )
00390     {
00391     const BuildFileItem* file = *it;
00392     QString fileName = file->url().path().mid( prjdir );
00393 
00394     fileList.push_back( fileName );
00395     }
00396 
00397     return fileList;
00398 }
00399 
00403 QStringList GenericProjectPart::distFiles() const
00404 {
00405     QStringList sourceList = allFiles();
00406     // Scan current source directory for any .pro files.
00407     QString projectDir = projectDirectory();
00408     QDir dir(projectDir);
00409     QStringList files = dir.entryList( "*README* Makefile");
00410     return sourceList + files;
00411 }
00412 
00413 #include "genericproject_part.moc"
00414 
00415 
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:22 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003