2
This file is part of KDevelop
4
Copyright 2013 Milian Wolff <mail@milianw.de>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Library General Public License
17
along with this library; see the file COPYING.LIB. If not, write to
18
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19
Boston, MA 02110-1301, USA.
22
#include "projectfilterprovider.h"
24
#include <KPluginLoader>
25
#include <KPluginFactory>
27
#include <KSettings/Dispatcher>
29
#include <KMessageBox>
30
#include <KParts/MainWindow>
33
#include <interfaces/iproject.h>
34
#include <interfaces/icore.h>
35
#include <interfaces/iprojectcontroller.h>
36
#include <interfaces/context.h>
37
#include <interfaces/contextmenuextension.h>
38
#include <interfaces/iuicontroller.h>
40
#include "projectfilterdebug.h"
41
#include <project/projectmodel.h>
43
using namespace KDevelop;
45
K_PLUGIN_FACTORY(ProjectFilterProviderFactory, registerPlugin<ProjectFilterProvider>(); )
46
K_EXPORT_PLUGIN(ProjectFilterProviderFactory(
47
KAboutData("kdevprojectfilter", "kdevprojectfilter", ki18n("Project Filter"),
48
"0.1", ki18n("Configure which files and folders inside the project folder should be included or excluded."),
49
KAboutData::License_GPL)))
51
ProjectFilterProvider::ProjectFilterProvider( QObject* parent, const QVariantList& /*args*/ )
52
: IPlugin( ProjectFilterProviderFactory::componentData(), parent )
54
KDEV_USE_EXTENSION_INTERFACE( IProjectFilterProvider )
56
connect(core()->projectController(), SIGNAL(projectClosing(KDevelop::IProject*)),
57
SLOT(projectClosing(KDevelop::IProject*)));
58
connect(core()->projectController(), SIGNAL(projectAboutToBeOpened(KDevelop::IProject*)),
59
SLOT(projectAboutToBeOpened(KDevelop::IProject*)));
61
updateProjectFilters();
63
KSettings::Dispatcher::registerComponent(componentData(), this, "updateProjectFilters");
66
QSharedPointer<IProjectFilter> ProjectFilterProvider::createFilter(IProject* project) const
68
return QSharedPointer<IProjectFilter>(new ProjectFilter(project, m_filters[project]));
71
ContextMenuExtension ProjectFilterProvider::contextMenuExtension(Context* context)
73
ContextMenuExtension ret;
74
if (!context->hasType(Context::ProjectItemContext)) {
78
ProjectItemContext* ctx = static_cast<ProjectItemContext*>( context );
80
QList<ProjectBaseItem*> items = ctx->items();
81
// filter out project roots, targets and items in targets
82
QList< ProjectBaseItem* >::iterator it = items.begin();
83
while (it != items.end()) {
84
if ((*it)->isProjectRoot() || (*it)->target() || !(*it)->parent()->folder()) {
90
if (items.isEmpty()) {
94
QAction* action = new QAction(KIcon("view-filter"),
95
i18np("Exclude item from project.",
96
"Exclude items from project",
98
action->setData(QVariant::fromValue(items));
99
connect(action, SIGNAL(triggered(bool)), SLOT(addFilterFromContextMenu()));
100
ret.addAction(ContextMenuExtension::FileGroup, action);
104
void ProjectFilterProvider::addFilterFromContextMenu()
106
QAction* action = qobject_cast<QAction*>(sender());
108
QList<ProjectBaseItem*> items = action->data().value<QList<ProjectBaseItem*> >();
109
QHash<IProject*, SerializedFilters> changedProjectFilters;
110
foreach(ProjectBaseItem* item, items) {
111
if (!changedProjectFilters.contains(item->project())) {
112
changedProjectFilters[item->project()] = readFilters(item->project()->projectConfiguration());
114
SerializedFilters& filters = changedProjectFilters[item->project()];
115
filters << SerializedFilter('/' + KUrl::relativeUrl(item->project()->folder(), item->url()),
116
item->folder() ? Filter::Folders : Filter::Files);
118
QHash< IProject*, SerializedFilters >::const_iterator it = changedProjectFilters.constBegin();
119
while (it != changedProjectFilters.constEnd()) {
120
writeFilters(it.value(), it.key()->projectConfiguration());
121
m_filters[it.key()] = deserialize(it.value());
122
emit filterChanged(this, it.key());
126
KMessageBox::information(ICore::self()->uiController()->activeMainWindow(),
127
i18np("A filter for the item was added. To undo, use the project filter settings.",
128
"A filter for the items was added. To undo, use the project filter settings.",
129
items.size()), i18n("Project Filter Added"), "projectfilter-addfromctxmenu");
132
void ProjectFilterProvider::updateProjectFilters()
134
foreach(IProject* project, core()->projectController()->projects()) {
135
Filters newFilters = deserialize(readFilters(project->projectConfiguration()));
136
Filters& filters = m_filters[project];
137
if (filters != newFilters) {
138
projectFilterDebug() << "project filter changed:" << project->name();
139
filters = newFilters;
140
emit filterChanged(this, project);
145
void ProjectFilterProvider::projectAboutToBeOpened(IProject* project)
147
m_filters[project] = deserialize(readFilters(project->projectConfiguration()));
150
void ProjectFilterProvider::projectClosing(IProject* project)
152
m_filters.remove(project);
155
#include "projectfilterprovider.moc"