~ubuntu-branches/ubuntu/trusty/kdevplatform/trusty-proposed

« back to all changes in this revision

Viewing changes to plugins/projectfilter/projectfilterprovider.cpp

  • Committer: Package Import Robot
  • Author(s): Rohan Garg
  • Date: 2013-12-09 17:24:43 UTC
  • mfrom: (0.3.25)
  • Revision ID: package-import@ubuntu.com-20131209172443-rstb3roh6fj0c5vw
Tags: 1.6.0-0ubuntu1
* New upstream release (LP: #1259220)
* Update install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    This file is part of KDevelop
 
3
 
 
4
    Copyright 2013 Milian Wolff <mail@milianw.de>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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.
 
20
*/
 
21
 
 
22
#include "projectfilterprovider.h"
 
23
 
 
24
#include <KPluginLoader>
 
25
#include <KPluginFactory>
 
26
#include <KAboutData>
 
27
#include <KSettings/Dispatcher>
 
28
#include <KIcon>
 
29
#include <KMessageBox>
 
30
#include <KParts/MainWindow>
 
31
#include <QAction>
 
32
 
 
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>
 
39
 
 
40
#include "projectfilterdebug.h"
 
41
#include <project/projectmodel.h>
 
42
 
 
43
using namespace KDevelop;
 
44
 
 
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)))
 
50
 
 
51
ProjectFilterProvider::ProjectFilterProvider( QObject* parent, const QVariantList& /*args*/ )
 
52
    : IPlugin( ProjectFilterProviderFactory::componentData(), parent )
 
53
{
 
54
    KDEV_USE_EXTENSION_INTERFACE( IProjectFilterProvider )
 
55
 
 
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*)));
 
60
 
 
61
    updateProjectFilters();
 
62
 
 
63
    KSettings::Dispatcher::registerComponent(componentData(), this, "updateProjectFilters");
 
64
}
 
65
 
 
66
QSharedPointer<IProjectFilter> ProjectFilterProvider::createFilter(IProject* project) const
 
67
{
 
68
    return QSharedPointer<IProjectFilter>(new ProjectFilter(project, m_filters[project]));
 
69
}
 
70
 
 
71
ContextMenuExtension ProjectFilterProvider::contextMenuExtension(Context* context)
 
72
{
 
73
    ContextMenuExtension ret;
 
74
    if (!context->hasType(Context::ProjectItemContext)) {
 
75
        return ret;
 
76
    }
 
77
 
 
78
    ProjectItemContext* ctx = static_cast<ProjectItemContext*>( context );
 
79
 
 
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()) {
 
85
            it = items.erase(it);
 
86
        } else {
 
87
            ++it;
 
88
        }
 
89
    }
 
90
    if (items.isEmpty()) {
 
91
        return ret;
 
92
    }
 
93
 
 
94
    QAction* action = new QAction(KIcon("view-filter"),
 
95
                                  i18np("Exclude item from project.",
 
96
                                        "Exclude items from project",
 
97
                                        items.size()), this);
 
98
    action->setData(QVariant::fromValue(items));
 
99
    connect(action, SIGNAL(triggered(bool)), SLOT(addFilterFromContextMenu()));
 
100
    ret.addAction(ContextMenuExtension::FileGroup, action);
 
101
    return ret;
 
102
}
 
103
 
 
104
void ProjectFilterProvider::addFilterFromContextMenu()
 
105
{
 
106
    QAction* action = qobject_cast<QAction*>(sender());
 
107
    Q_ASSERT(action);
 
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());
 
113
        }
 
114
        SerializedFilters& filters = changedProjectFilters[item->project()];
 
115
        filters << SerializedFilter('/' + KUrl::relativeUrl(item->project()->folder(), item->url()),
 
116
                                    item->folder() ? Filter::Folders : Filter::Files);
 
117
    }
 
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());
 
123
        ++it;
 
124
    }
 
125
 
 
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");
 
130
}
 
131
 
 
132
void ProjectFilterProvider::updateProjectFilters()
 
133
{
 
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);
 
141
        }
 
142
    }
 
143
}
 
144
 
 
145
void ProjectFilterProvider::projectAboutToBeOpened(IProject* project)
 
146
{
 
147
    m_filters[project] = deserialize(readFilters(project->projectConfiguration()));
 
148
}
 
149
 
 
150
void ProjectFilterProvider::projectClosing(IProject* project)
 
151
{
 
152
    m_filters.remove(project);
 
153
}
 
154
 
 
155
#include "projectfilterprovider.moc"