OSDN Git Service

80c363b4b40c5902ca64418d7ce93bd3f05b3808
[qt-creator-jp/qt-creator-jp.git] / src / plugins / locator / locatorplugin.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "locatorplugin.h"
35 #include "locatorconstants.h"
36 #include "locatorfiltersfilter.h"
37 #include "locatormanager.h"
38 #include "locatorwidget.h"
39 #include "opendocumentsfilter.h"
40 #include "filesystemfilter.h"
41 #include "settingspage.h"
42
43 #include <coreplugin/statusbarwidget.h>
44 #include <coreplugin/coreconstants.h>
45 #include <coreplugin/settingsdatabase.h>
46 #include <coreplugin/icore.h>
47 #include <coreplugin/actionmanager/actionmanager.h>
48 #include <coreplugin/actionmanager/actioncontainer.h>
49 #include <coreplugin/actionmanager/command.h>
50 #include <coreplugin/progressmanager/progressmanager.h>
51 #include <coreplugin/progressmanager/futureprogress.h>
52 #include <coreplugin/uniqueidmanager.h>
53 #include <extensionsystem/pluginmanager.h>
54 #include <qtconcurrent/QtConcurrentTools>
55
56 #include <QtCore/QSettings>
57 #include <QtCore/QtPlugin>
58 #include <QtCore/QFuture>
59 #include <QtCore/QFutureWatcher>
60 #include <QtGui/QAction>
61
62 /*!
63     \namespace Locator
64     The Locator namespace provides the hooks for Locator content.
65 */
66 /*!
67     \namespace Locator::Internal
68     \internal
69 */
70
71 using namespace Locator;
72 using namespace Locator::Internal;
73
74 namespace {
75     static bool filterLessThan(const ILocatorFilter *first, const ILocatorFilter *second)
76     {
77         if (first->priority() < second->priority())
78             return true;
79         if (first->priority() > second->priority())
80             return false;
81         if (first->id().compare(second->id(), Qt::CaseInsensitive) < 0)
82             return true;
83         return false;
84     }
85 }
86
87 LocatorPlugin::LocatorPlugin()
88 {
89     m_refreshTimer.setSingleShot(false);
90     connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh()));
91 }
92
93 LocatorPlugin::~LocatorPlugin()
94 {
95     removeObject(m_openDocumentsFilter);
96     removeObject(m_fileSystemFilter);
97     removeObject(m_settingsPage);
98     delete m_openDocumentsFilter;
99     delete m_fileSystemFilter;
100     delete m_settingsPage;
101     qDeleteAll(m_customFilters);
102 }
103
104 bool LocatorPlugin::initialize(const QStringList &, QString *)
105 {
106     Core::ICore *core = Core::ICore::instance();
107     m_settingsPage = new SettingsPage(this);
108     addObject(m_settingsPage);
109
110     m_locatorWidget = new LocatorWidget(this);
111     m_locatorWidget->setEnabled(false);
112     Core::StatusBarWidget *view = new Core::StatusBarWidget;
113     view->setWidget(m_locatorWidget);
114     view->setContext(Core::Context("LocatorWidget"));
115     view->setPosition(Core::StatusBarWidget::First);
116     addAutoReleasedObject(view);
117
118     QAction *action = new QAction(m_locatorWidget->windowIcon(), m_locatorWidget->windowTitle(), this);
119     Core::Command *cmd = core->actionManager()
120         ->registerAction(action, "QtCreator.Locate", Core::Context(Core::Constants::C_GLOBAL));
121     cmd->setDefaultKeySequence(QKeySequence("Ctrl+K"));
122     connect(action, SIGNAL(triggered()), this, SLOT(openLocator()));
123
124     Core::ActionContainer *mtools = core->actionManager()->actionContainer(Core::Constants::M_TOOLS);
125     mtools->addAction(cmd);
126
127     addObject(new LocatorManager(m_locatorWidget));
128
129     m_openDocumentsFilter = new OpenDocumentsFilter(core->editorManager());
130     addObject(m_openDocumentsFilter);
131
132     m_fileSystemFilter = new FileSystemFilter(core->editorManager(), m_locatorWidget);
133     addObject(m_fileSystemFilter);
134
135     addAutoReleasedObject(new LocatorFiltersFilter(this, m_locatorWidget));
136
137     connect(core, SIGNAL(coreOpened()), this, SLOT(startSettingsLoad()));
138     return true;
139 }
140
141 void LocatorPlugin::openLocator()
142 {
143     m_locatorWidget->show("");
144 }
145
146 void LocatorPlugin::extensionsInitialized()
147 {
148     m_filters = ExtensionSystem::PluginManager::instance()->getObjects<ILocatorFilter>();
149     qSort(m_filters.begin(), m_filters.end(), filterLessThan);
150     setFilters(m_filters);
151 }
152
153 void LocatorPlugin::startSettingsLoad()
154 {
155     connect(&m_loadWatcher, SIGNAL(finished()), this, SLOT(settingsLoaded()));
156     m_loadWatcher.setFuture(QtConcurrent::run(this, &LocatorPlugin::loadSettings));
157 }
158
159 void LocatorPlugin::loadSettings()
160 {
161     Core::ICore *core = Core::ICore::instance();
162     QSettings *qs = core->settings();
163
164     // Backwards compatibility to old settings location
165     if (qs->contains("QuickOpen/FiltersFilter")) {
166         loadSettingsHelper(qs);
167     } else {
168         Core::SettingsDatabase *settings = core->settingsDatabase();
169         loadSettingsHelper(settings);
170     }
171
172     qs->remove("QuickOpen");
173 }
174
175 void LocatorPlugin::settingsLoaded()
176 {
177     m_locatorWidget->updateFilterList();
178     m_locatorWidget->setEnabled(true);
179     if (m_refreshTimer.interval() > 0)
180         m_refreshTimer.start();
181 }
182
183 void LocatorPlugin::saveSettings()
184 {
185     Core::ICore *core = Core::ICore::instance();
186     if (core && core->settingsDatabase()) {
187         Core::SettingsDatabase *s = core->settingsDatabase();
188         s->beginGroup("QuickOpen");
189         s->remove("");
190         s->setValue("RefreshInterval", refreshInterval());
191         foreach (ILocatorFilter *filter, m_filters) {
192             if (!m_customFilters.contains(filter))
193                 s->setValue(filter->id(), filter->saveState());
194         }
195         s->beginGroup("CustomFilters");
196         int i = 0;
197         foreach (ILocatorFilter *filter, m_customFilters) {
198             s->setValue(QString("directory%1").arg(i), filter->saveState());
199             ++i;
200         }
201         s->endGroup();
202         s->endGroup();
203     }
204 }
205
206 /*!
207     \fn QList<ILocatorFilter*> LocatorPlugin::filters()
208
209     Return all filters, including the ones created by the user.
210 */
211 QList<ILocatorFilter*> LocatorPlugin::filters()
212 {
213     return m_filters;
214 }
215
216 /*!
217     \fn QList<ILocatorFilter*> LocatorPlugin::customFilters()
218
219     This returns a subset of all the filters, that contains only the filters that
220     have been created by the user at some point (maybe in a previous session).
221  */
222 QList<ILocatorFilter*> LocatorPlugin::customFilters()
223 {
224     return m_customFilters;
225 }
226
227 void LocatorPlugin::setFilters(QList<ILocatorFilter*> f)
228 {
229     m_filters = f;
230     m_locatorWidget->updateFilterList();
231 }
232
233 void LocatorPlugin::setCustomFilters(QList<ILocatorFilter *> filters)
234 {
235     m_customFilters = filters;
236 }
237
238 int LocatorPlugin::refreshInterval()
239 {
240     return m_refreshTimer.interval() / 60000;
241 }
242
243 void LocatorPlugin::setRefreshInterval(int interval)
244 {
245     if (interval < 1) {
246         m_refreshTimer.stop();
247         m_refreshTimer.setInterval(0);
248         return;
249     }
250     m_refreshTimer.setInterval(interval * 60000);
251     m_refreshTimer.start();
252 }
253
254 void LocatorPlugin::refresh(QList<ILocatorFilter*> filters)
255 {
256     if (filters.isEmpty())
257         filters = m_filters;
258     QFuture<void> task = QtConcurrent::run(&ILocatorFilter::refresh, filters);
259     Core::FutureProgress *progress = Core::ICore::instance()
260             ->progressManager()->addTask(task, tr("Indexing"),
261                                          Locator::Constants::TASK_INDEX);
262     connect(progress, SIGNAL(finished()), this, SLOT(saveSettings()));
263 }
264
265 Q_EXPORT_PLUGIN(LocatorPlugin)