Even though the last alpha was .91 this one is going to be 82 as I'd like to have...
[kdevelopdvcssupport.git] / plugins / projectmanagerview / projectmanagerviewplugin.cpp
blob47b3af4dc74eceff664324452d81f060ecf2902a
1 /* This file is part of KDevelop
2 Copyright 2004 Roberto Raggi <roberto@kdevelop.org>
3 Copyright 2007 Andreas Pakulat <apaku@gmx.de>
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
20 #include "projectmanagerviewplugin.h"
22 #include <QtCore/QList>
23 #include <QtGui/QInputDialog>
25 #include <kaction.h>
26 #include <kactioncollection.h>
27 #include <kaboutdata.h>
28 #include <klocale.h>
29 #include <kmessagebox.h>
30 #include <kio/netaccess.h>
31 #include <kparts/mainwindow.h>
32 #include <ksavefile.h>
33 #include <kparts/componentfactory.h>
35 #include <project/projectmodel.h>
36 #include <interfaces/icore.h>
37 #include <interfaces/iproject.h>
38 #include <project/interfaces/iprojectfilemanager.h>
39 #include <project/interfaces/ibuildsystemmanager.h>
40 #include <interfaces/iuicontroller.h>
41 #include <interfaces/iruncontroller.h>
42 #include <interfaces/idocumentcontroller.h>
43 #include <project/interfaces/iprojectbuilder.h>
44 #include <interfaces/iprojectcontroller.h>
45 #include <interfaces/irun.h>
46 #include <interfaces/context.h>
47 #include <interfaces/contextmenuextension.h>
49 #include "projectmanagerview.h"
50 #include "projectbuildsetmodel.h"
52 using namespace KDevelop;
54 K_PLUGIN_FACTORY(ProjectManagerFactory, registerPlugin<ProjectManagerViewPlugin>(); )
55 K_EXPORT_PLUGIN(ProjectManagerFactory(KAboutData("kdevprojectmanagerview","kdevprojectmanagerview", ki18n("Project Management View"), "0.1", ki18n("Toolview to do all the project management stuff"), KAboutData::License_GPL)))
57 class KDevProjectManagerViewFactory: public KDevelop::IToolViewFactory
59 public:
60 KDevProjectManagerViewFactory( ProjectManagerViewPlugin *plugin ): mplugin( plugin )
62 virtual QWidget* create( QWidget *parent = 0 )
64 return new ProjectManagerView( mplugin, parent );
66 virtual Qt::DockWidgetArea defaultPosition()
68 return Qt::LeftDockWidgetArea;
70 virtual QString id() const
72 return "org.kdevelop.ProjectsView";
75 private:
76 ProjectManagerViewPlugin *mplugin;
79 class ProjectManagerViewPluginPrivate
81 public:
82 ProjectManagerViewPluginPrivate()
84 KDevProjectManagerViewFactory *factory;
85 QList<KDevelop::ProjectBaseItem*> ctxProjectItemList;
86 KAction* m_buildAll;
87 KAction* m_build;
88 KAction* m_install;
89 KAction* m_clean;
90 KAction* m_configure;
91 KAction* m_prune;
92 ProjectBuildSetModel* buildSet;
95 ProjectManagerViewPlugin::ProjectManagerViewPlugin( QObject *parent, const QVariantList& )
96 : IPlugin( ProjectManagerFactory::componentData(), parent ), d(new ProjectManagerViewPluginPrivate)
98 d->buildSet = new ProjectBuildSetModel( this );
100 connect( core()->projectController(), SIGNAL( projectOpened( KDevelop::IProject* ) ),
101 d->buildSet, SLOT( loadFromProject( KDevelop::IProject* ) ) );
103 connect( core()->projectController(), SIGNAL( projectClosing( KDevelop::IProject* ) ),
104 d->buildSet, SLOT( saveToProject( KDevelop::IProject* ) ) );
106 d->m_buildAll = new KAction( i18n("Build all Projects"), this );
107 d->m_buildAll->setIcon(KIcon("run-build"));
108 connect( d->m_buildAll, SIGNAL(triggered()), this, SLOT(buildAllProjects()) );
109 actionCollection()->addAction( "project_buildall", d->m_buildAll );
110 d->m_build = new KAction( i18n("Build"), this );
111 d->m_build->setShortcut( Qt::Key_F8 );
112 d->m_build->setIcon(KIcon("run-build"));
113 connect( d->m_build, SIGNAL(triggered()), this, SLOT(buildProjectItems()) );
114 actionCollection()->addAction( "project_build", d->m_build );
115 d->m_install = new KAction( i18n("Install"), this );
116 connect( d->m_install, SIGNAL(triggered()), this, SLOT(installProjectItems()) );
117 actionCollection()->addAction( "project_install", d->m_install );
118 d->m_clean = new KAction( i18n("Clean"), this );
119 connect( d->m_clean, SIGNAL(triggered()), this, SLOT(cleanProjectItems()) );
120 actionCollection()->addAction( "project_clean", d->m_clean );
121 d->m_configure = new KAction( i18n("Configure"), this );
122 d->m_configure->setIcon(KIcon("configure"));
123 connect( d->m_configure, SIGNAL(triggered()), this, SLOT(configureProjectItems()) );
124 actionCollection()->addAction( "project_configure", d->m_configure );
125 d->m_prune = new KAction( i18n("Prune"), this );
126 connect( d->m_prune, SIGNAL(triggered()), this, SLOT(pruneProjectItems()) );
127 actionCollection()->addAction( "project_prune", d->m_prune );
128 setXMLFile( "kdevprojectmanagerview.rc" );
129 d->factory = new KDevProjectManagerViewFactory( this );
130 core()->uiController()->addToolView( i18n("Projects"), d->factory );
134 ProjectManagerViewPlugin::~ProjectManagerViewPlugin()
136 delete d;
139 void ProjectManagerViewPlugin::unload()
141 kDebug() << "unloading manager view";
142 core()->uiController()->removeToolView(d->factory);
145 ContextMenuExtension ProjectManagerViewPlugin::contextMenuExtension( KDevelop::Context* context )
147 d->ctxProjectItemList.clear();
148 if( context->type() != KDevelop::Context::ProjectItemContext )
149 return IPlugin::contextMenuExtension( context );
151 KDevelop::ProjectItemContext* ctx = dynamic_cast<KDevelop::ProjectItemContext*>( context );
152 QList<KDevelop::ProjectBaseItem*> items = ctx->items();
154 if( items.isEmpty() )
155 return IPlugin::contextMenuExtension( context );
157 ContextMenuExtension menuExt;
158 bool closeProjectsAdded = false;
159 bool buildItemsAdded = false;
160 bool hasTargets=false;
161 foreach( ProjectBaseItem* item, items )
163 d->ctxProjectItemList << item;
164 if ( !buildItemsAdded && ( item->folder() || item->target() || item->file() ) )
166 KAction* action = new KAction( i18n( "Build" ), this );
167 connect( action, SIGNAL( triggered() ), this, SLOT(buildItemsFromContextMenu()) );
168 menuExt.addAction( ContextMenuExtension::ProjectGroup, action );
169 action = new KAction( i18n( "Install" ), this );
170 connect( action, SIGNAL( triggered() ), this, SLOT(installItemsFromContextMenu()) );
171 menuExt.addAction( ContextMenuExtension::ProjectGroup, action );
172 action = new KAction( i18n( "Clean" ), this );
173 connect( action, SIGNAL( triggered() ), this, SLOT(cleanItemsFromContextMenu()) );
174 menuExt.addAction( ContextMenuExtension::ProjectGroup, action );
175 action = new KAction( i18n( "Add to buildset" ), this );
176 connect( action, SIGNAL(triggered() ), this, SLOT(addItemsFromContextMenuToBuildset() ) );
177 menuExt.addAction( ContextMenuExtension::ProjectGroup, action );
178 buildItemsAdded = true;
181 KDevelop::ProjectFolderItem *prjitem = item->folder();
182 if ( !closeProjectsAdded && prjitem && prjitem->isProjectRoot() )
184 KAction* close = new KAction( i18n( "Close project(s)" ), this );
185 connect( close, SIGNAL(triggered()), this, SLOT(closeProjects()) );
186 menuExt.addAction( ContextMenuExtension::ProjectGroup, close );
187 closeProjectsAdded = true;
190 if ( item->folder() )
192 KAction* action = new KAction( i18n( "Create File" ), this );
193 connect( action, SIGNAL(triggered()), this, SLOT(createFileFromContextMenu()) );
194 menuExt.addAction( ContextMenuExtension::FileGroup, action );
196 action = new KAction( i18n( "Create Folder" ), this );
197 connect( action, SIGNAL(triggered()), this, SLOT(createFolderFromContextMenu()) );
198 menuExt.addAction( ContextMenuExtension::FileGroup, action );
200 action = new KAction( i18n( "Reload" ), this );
201 connect( action, SIGNAL(triggered()), this, SLOT(reloadFromContextMenu()) );
202 menuExt.addAction( ContextMenuExtension::FileGroup, action );
205 if(!hasTargets && item->executable())
207 KAction* action = new KAction( i18n("Run"), this );
208 connect( action, SIGNAL( triggered() ), this, SLOT( runTargetsFromContextMenu() ) );
209 menuExt.addAction( ContextMenuExtension::ProjectGroup, action );
214 // if( items.count() == 1 )
215 // {
216 // KAction* action = new KAction( i18n( "Project Configuration" ), this );
217 // connect( action, SIGNAL( triggered() ), this, SLOT( projectConfiguration() ) );
218 // menuExt.addAction( ContextMenuExtension::ProjectGroup, action );
219 // }
221 return menuExt;
225 KDevelop::IProjectBuilder* ProjectManagerViewPlugin::getProjectBuilder( KDevelop::ProjectBaseItem* item )
227 if( !item )
228 return 0;
229 IProject* project = item->project();
230 if (!project)
231 return 0;
233 ProjectFolderItem* prjitem = project->projectItem();
234 IPlugin* fmgr = project->managerPlugin();
235 IBuildSystemManager* mgr = fmgr->extension<IBuildSystemManager>();
236 if( mgr )
238 return mgr->builder( prjitem );
240 return 0;
243 void ProjectManagerViewPlugin::executeBuild( KDevelop::ProjectBaseItem* item )
245 if( !item )
246 return;
247 IProjectBuilder* builder = getProjectBuilder( item );
248 kDebug(9511) << "Building item:" << item->text();
250 core()->documentController()->saveAllDocuments(IDocument::Silent);
252 if( builder )
253 core()->runController()->registerJob(builder->build( item ));
256 void ProjectManagerViewPlugin::executeClean( KDevelop::ProjectBaseItem* item )
258 if( !item )
259 return;
260 IProjectBuilder* builder = getProjectBuilder( item );
261 kDebug(9511) << "Cleaning item:" << item->text();
262 if( builder )
263 core()->runController()->registerJob(builder->clean( item ));
266 void ProjectManagerViewPlugin::executeInstall( KDevelop::ProjectBaseItem* item )
268 if( !item )
269 return;
270 IProjectBuilder* builder = getProjectBuilder( item );
271 kDebug(9511) << "Installing item:" << item->text();
273 core()->documentController()->saveAllDocuments(IDocument::Silent);
275 if( builder )
276 core()->runController()->registerJob(builder->install( item ));
280 void ProjectManagerViewPlugin::executeConfigure( KDevelop::IProject* item )
282 if( !item )
283 return;
284 IProjectBuilder* builder = getProjectBuilder( item->projectItem() );
285 kDebug(9511) << "Configuring item:" << item->name();
287 core()->documentController()->saveAllDocuments(IDocument::Silent);
289 if( builder )
290 core()->runController()->registerJob(builder->configure( item ));
293 void ProjectManagerViewPlugin::executePrune( KDevelop::IProject* item )
295 if( !item )
296 return;
297 IProjectBuilder* builder = getProjectBuilder( item->projectItem() );
298 kDebug(9511) << "Pruning item:" << item->name();
300 core()->documentController()->saveAllDocuments(IDocument::Silent);
302 if( builder )
303 core()->runController()->registerJob(builder->prune( item ));
307 void ProjectManagerViewPlugin::closeProjects()
309 QList<KDevelop::IProject*> projectsToClose;
310 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
312 if( !projectsToClose.contains( item->project() ) )
314 projectsToClose << item->project();
317 d->ctxProjectItemList.clear();
318 foreach( KDevelop::IProject* proj, projectsToClose )
320 core()->projectController()->closeProject( proj );
325 void ProjectManagerViewPlugin::installItemsFromContextMenu()
327 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
329 executeInstall( item );
331 d->ctxProjectItemList.clear();
334 void ProjectManagerViewPlugin::cleanItemsFromContextMenu()
336 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
338 executeClean( item );
340 d->ctxProjectItemList.clear();
343 void ProjectManagerViewPlugin::buildItemsFromContextMenu()
345 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
347 executeBuild( item );
349 d->ctxProjectItemList.clear();
352 void ProjectManagerViewPlugin::buildAllProjects()
354 foreach( KDevelop::IProject* project, core()->projectController()->projects() )
356 executeBuild( project->projectItem() );
360 void ProjectManagerViewPlugin::installProjectItems()
362 foreach( BuildItem item, d->buildSet->items() )
364 executeInstall( item.findItem() );
368 void ProjectManagerViewPlugin::pruneProjectItems()
370 QSet<KDevelop::IProject*> projects;
371 foreach( BuildItem item, d->buildSet->items() )
373 if( item.findItem() )
374 projects << item.findItem()->project();
376 foreach( KDevelop::IProject* project, projects )
378 executePrune( project );
382 void ProjectManagerViewPlugin::configureProjectItems()
384 QSet<KDevelop::IProject*> projects;
386 foreach( BuildItem item, d->buildSet->items() )
388 if( item.findItem() )
389 projects << item.findItem()->project();
391 foreach( KDevelop::IProject* project, projects )
393 executeConfigure( project );
397 void ProjectManagerViewPlugin::cleanProjectItems()
399 foreach( BuildItem item, d->buildSet->items() )
401 executeClean( item.findItem() );
405 void ProjectManagerViewPlugin::buildProjectItems()
407 foreach( BuildItem item, d->buildSet->items() )
409 executeBuild( item.findItem() );
413 ProjectBuildSetModel* ProjectManagerViewPlugin::buildSet()
415 return d->buildSet;
418 void ProjectManagerViewPlugin::addItemsFromContextMenuToBuildset( )
420 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
422 buildSet()->addProjectItem( item );
426 void ProjectManagerViewPlugin::runTargetsFromContextMenu( )
428 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
430 KDevelop::ProjectExecutableTargetItem* t=item->executable();
431 if(t)
433 kDebug() << "Running target: " << t->text() << t->builtUrl();
434 IRun r;
435 r.setExecutable(t->builtUrl().toLocalFile());
436 r.setInstrumentor("default");
438 ICore::self()->runController()->execute(r);
443 void ProjectManagerViewPlugin::projectConfiguration( )
445 if( !d->ctxProjectItemList.isEmpty() )
447 core()->projectController()->configureProject( d->ctxProjectItemList.at( 0 )->project() );
451 void ProjectManagerViewPlugin::reloadFromContextMenu( )
453 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
455 item->project()->projectFileManager()->reload(item);
459 void ProjectManagerViewPlugin::createFolderFromContextMenu( )
461 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
463 if ( item->folder() ) {
464 QWidget* window(ICore::self()->uiController()->activeMainWindow()->window());
465 QString name = QInputDialog::getText ( window,
466 i18n ( "Create Folder" ), i18n ( "Folder Name" ) );
467 if (!name.isEmpty()) {
468 KUrl url = item->folder()->url();
469 url.addPath( name );
470 if ( !KIO::NetAccess::mkdir( url, window ) ) {
471 KMessageBox::error( window, i18n( "Can't create folder." ) );
472 continue;
474 item->project()->projectFileManager()->addFolder( url, item->folder() );
480 void ProjectManagerViewPlugin::createFileFromContextMenu( )
482 foreach( KDevelop::ProjectBaseItem* item, d->ctxProjectItemList )
484 if ( item->folder() ) {
485 QWidget* window(ICore::self()->uiController()->activeMainWindow()->window());
486 QString name = QInputDialog::getText ( window,
487 i18n ( "Create File" ), i18n ( "File Name" ) );
488 if (!name.isEmpty()) {
489 KUrl url = item->folder()->url();
490 url.addPath( name );
492 if (KIO::NetAccess::exists( url, KIO::NetAccess::SourceSide, window )) {
493 KMessageBox::error( window, i18n( "This file exists already." ) );
494 continue;
497 KSaveFile file(url.path());
498 if ( ! file.open() ) {
499 KMessageBox::error( window, i18n( "Can't create file." ) );
500 continue;
502 file.finalize();
503 file.close();
505 item->project()->projectFileManager()->addFile( url, item->folder() );
506 ICore::self()->documentController()->openDocument( url );
514 #include "projectmanagerviewplugin.moc"