quickopenplugin.cpp 7.8 KB
Newer Older
1
/**************************************************************************
con's avatar
con committed
2
3
4
**
** This file is part of Qt Creator
**
5
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
con's avatar
con committed
6
**
7
** Contact: Nokia Corporation (qt-info@nokia.com)
con's avatar
con committed
8
**
9
** Commercial Usage
10
**
11
12
13
14
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
15
**
16
** GNU Lesser General Public License Usage
17
**
18
19
20
21
22
23
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24
**
25
** If you are unsure which license is appropriate for your use, please
hjk's avatar
hjk committed
26
** contact the sales department at http://qt.nokia.com/contact.
con's avatar
con committed
27
**
28
**************************************************************************/
hjk's avatar
hjk committed
29

con's avatar
con committed
30
#include "quickopenplugin.h"
31
#include "quickopenconstants.h"
con's avatar
con committed
32
#include "quickopenfiltersfilter.h"
con's avatar
con committed
33
#include "locatormanager.h"
34
#include "locatorwidget.h"
con's avatar
con committed
35
36
37
38
#include "opendocumentsfilter.h"
#include "filesystemfilter.h"
#include "settingspage.h"

39
#include <QtCore/QSettings>
40
#include <QtCore/QtPlugin>
con's avatar
con committed
41
42
43
44
45
#include <QtCore/QFuture>
#include <QtCore/QFutureWatcher>

#include <coreplugin/baseview.h>
#include <coreplugin/coreconstants.h>
46
#include <coreplugin/settingsdatabase.h>
47
#include <coreplugin/icore.h>
con's avatar
con committed
48
#include <coreplugin/uniqueidmanager.h>
49
#include <coreplugin/actionmanager/actionmanager.h>
50
#include <coreplugin/progressmanager/progressmanager.h>
51
#include <extensionsystem/pluginmanager.h>
con's avatar
con committed
52
53
#include <qtconcurrent/QtConcurrentTools>

con's avatar
con committed
54
55
56
57
58
59
60
61
62
/*!
    \namespace QuickOpen
    The QuickOpen namespace provides the hooks for Locator content.
*/
/*!
    \namespace QuickOpen::Internal
    \internal
*/

con's avatar
con committed
63
64
65
66
using namespace QuickOpen;
using namespace QuickOpen::Internal;

namespace {
67
    static bool filterLessThan(const ILocatorFilter *first, const ILocatorFilter *second)
con's avatar
con committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    {
        return first->priority() < second->priority();
    }
}

QuickOpenPlugin::QuickOpenPlugin()
{
    m_refreshTimer.setSingleShot(false);
    connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
}

QuickOpenPlugin::~QuickOpenPlugin()
{
    removeObject(m_openDocumentsFilter);
    removeObject(m_fileSystemFilter);
    removeObject(m_settingsPage);
    delete m_openDocumentsFilter;
    delete m_fileSystemFilter;
    delete m_settingsPage;
87
    qDeleteAll(m_customFilters);
con's avatar
con committed
88
89
90
91
}

bool QuickOpenPlugin::initialize(const QStringList &, QString *)
{
92
    Core::ICore *core = Core::ICore::instance();
hjk's avatar
hjk committed
93
    m_settingsPage = new SettingsPage(this);
con's avatar
con committed
94
95
    addObject(m_settingsPage);

96
97
    m_locatorWidget = new LocatorWidget(this);
    m_locatorWidget->setEnabled(false);
98
    Core::BaseView *view = new Core::BaseView;
99
    view->setUniqueViewName("QuickOpen");
100
    view->setWidget(m_locatorWidget);
101
    view->setContext(QList<int>() << core->uniqueIDManager()
102
        ->uniqueIdentifier(QLatin1String("LocatorWidget")));
103
    view->setDefaultPosition(Core::IView::First);
con's avatar
con committed
104
105
    addAutoReleasedObject(view);

mae's avatar
mae committed
106
    const QString actionId = QLatin1String("QtCreator.Locate");
107
    QAction *action = new QAction(m_locatorWidget->windowIcon(), m_locatorWidget->windowTitle(), this);
con's avatar
con committed
108
    Core::Command *cmd = core->actionManager()->registerAction(action, actionId, QList<int>() << Core::Constants::C_GLOBAL_ID);
con's avatar
con committed
109
110
111
    cmd->setDefaultKeySequence(QKeySequence("Ctrl+K"));
    connect(action, SIGNAL(triggered()), this, SLOT(openQuickOpen()));

112
    Core::ActionContainer *mtools = core->actionManager()->actionContainer(Core::Constants::M_TOOLS);
con's avatar
con committed
113
114
    mtools->addAction(cmd);

con's avatar
con committed
115
    addObject(new LocatorManager(m_locatorWidget));
con's avatar
con committed
116
117
118
119

    m_openDocumentsFilter = new OpenDocumentsFilter(core->editorManager());
    addObject(m_openDocumentsFilter);

120
    m_fileSystemFilter = new FileSystemFilter(core->editorManager(), m_locatorWidget);
con's avatar
con committed
121
122
    addObject(m_fileSystemFilter);

123
    addAutoReleasedObject(new QuickOpenFiltersFilter(this, m_locatorWidget));
con's avatar
con committed
124
125
126
127
128
129
130

    connect(core, SIGNAL(coreOpened()), this, SLOT(startSettingsLoad()));
    return true;
}

void QuickOpenPlugin::openQuickOpen()
{
131
    m_locatorWidget->show("");
con's avatar
con committed
132
133
134
135
}

void QuickOpenPlugin::extensionsInitialized()
{
136
    m_filters = ExtensionSystem::PluginManager::instance()->getObjects<ILocatorFilter>();
137
    qSort(m_filters.begin(), m_filters.end(), filterLessThan);
con's avatar
con committed
138
139
140
141
142
143
144
145
}

void QuickOpenPlugin::startSettingsLoad()
{
    m_loadWatcher.setFuture(QtConcurrent::run(this, &QuickOpenPlugin::loadSettings));
    connect(&m_loadWatcher, SIGNAL(finished()), this, SLOT(settingsLoaded()));
}

146
147
148
149
150
151
152
void QuickOpenPlugin::loadSettings()
{
    Core::ICore *core = Core::ICore::instance();
    QSettings *qs = core->settings();

    // Backwards compatibility to old settings location
    if (qs->contains("QuickOpen/FiltersFilter")) {
153
        loadSettingsHelper(qs);
154
155
    } else {
        Core::SettingsDatabase *settings = core->settingsDatabase();
156
        loadSettingsHelper(settings);
157
158
159
160
161
    }

    qs->remove("QuickOpen");
}

con's avatar
con committed
162
163
void QuickOpenPlugin::settingsLoaded()
{
164
165
    m_locatorWidget->updateFilterList();
    m_locatorWidget->setEnabled(true);
166
167
    if (m_refreshTimer.interval() > 0)
        m_refreshTimer.start();
con's avatar
con committed
168
169
170
171
}

void QuickOpenPlugin::saveSettings()
{
172
    Core::ICore *core = Core::ICore::instance();
173
174
    if (core && core->settingsDatabase()) {
        Core::SettingsDatabase *s = core->settingsDatabase();
con's avatar
con committed
175
176
        s->beginGroup("QuickOpen");
        s->remove("");
177
        s->setValue("RefreshInterval", refreshInterval());
178
        foreach (ILocatorFilter *filter, m_filters) {
179
            if (!m_customFilters.contains(filter))
con's avatar
con committed
180
181
182
183
                s->setValue(filter->name(), filter->saveState());
        }
        s->beginGroup("CustomFilters");
        int i = 0;
184
        foreach (ILocatorFilter *filter, m_customFilters) {
con's avatar
con committed
185
186
187
188
189
190
191
192
193
            s->setValue(QString("directory%1").arg(i), filter->saveState());
            ++i;
        }
        s->endGroup();
        s->endGroup();
    }
}

/*!
194
    \fn QList<ILocatorFilter*> QuickOpenPlugin::filter()
con's avatar
con committed
195
196
197

    Return all filters, including the ones created by the user.
*/
198
QList<ILocatorFilter*> QuickOpenPlugin::filters()
con's avatar
con committed
199
{
200
    return m_filters;
con's avatar
con committed
201
202
203
}

/*!
204
    \fn QList<ILocatorFilter*> QuickOpenPlugin::customFilter()
con's avatar
con committed
205
206
207
208

    This returns a subset of all the filters, that contains only the filters that
    have been created by the user at some point (maybe in a previous session).
 */
209
QList<ILocatorFilter*> QuickOpenPlugin::customFilters()
con's avatar
con committed
210
{
211
    return m_customFilters;
con's avatar
con committed
212
213
}

214
void QuickOpenPlugin::setFilters(QList<ILocatorFilter*> f)
con's avatar
con committed
215
{
216
    m_filters = f;
217
    m_locatorWidget->updateFilterList();
con's avatar
con committed
218
219
}

220
void QuickOpenPlugin::setCustomFilters(QList<ILocatorFilter *> filters)
con's avatar
con committed
221
{
222
    m_customFilters = filters;
con's avatar
con committed
223
224
225
226
}

int QuickOpenPlugin::refreshInterval()
{
227
    return m_refreshTimer.interval() / 60000;
con's avatar
con committed
228
229
230
231
232
233
234
235
236
}

void QuickOpenPlugin::setRefreshInterval(int interval)
{
    if (interval < 1) {
        m_refreshTimer.stop();
        m_refreshTimer.setInterval(0);
        return;
    }
237
    m_refreshTimer.setInterval(interval * 60000);
con's avatar
con committed
238
239
240
    m_refreshTimer.start();
}

241
void QuickOpenPlugin::refresh(QList<ILocatorFilter*> filters)
con's avatar
con committed
242
243
{
    if (filters.isEmpty())
244
        filters = m_filters;
245
    QFuture<void> task = QtConcurrent::run(&ILocatorFilter::refresh, filters);
246
    Core::FutureProgress *progress = Core::ICore::instance()
247
248
249
            ->progressManager()->addTask(task, tr("Indexing"),
                                         QuickOpen::Constants::TASK_INDEX,
                                         Core::ProgressManager::CloseOnSuccess);
con's avatar
con committed
250
251
252
253
    connect(progress, SIGNAL(finished()), this, SLOT(saveSettings()));
}

Q_EXPORT_PLUGIN(QuickOpenPlugin)