1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #include "filesystemfilter.h"
34 #include "locatorwidget.h"
35 #include <coreplugin/editormanager/ieditor.h>
36 #include <coreplugin/editormanager/editormanager.h>
38 #include <QtCore/QDir>
41 using namespace Locator;
42 using namespace Locator::Internal;
44 FileSystemFilter::FileSystemFilter(EditorManager *editorManager, LocatorWidget *locatorWidget)
45 : m_editorManager(editorManager), m_locatorWidget(locatorWidget), m_includeHidden(true)
47 setShortcutString(QString(QLatin1Char('f')));
48 setIncludedByDefault(false);
51 QList<FilterEntry> FileSystemFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &entry)
53 QList<FilterEntry> value;
54 QFileInfo entryInfo(entry);
55 QString name = entryInfo.fileName();
56 QString directory = entryInfo.path();
57 QString filePath = entryInfo.filePath();
58 if (entryInfo.isRelative()) {
59 if (filePath.startsWith(QLatin1String("~/"))) {
60 directory.replace(0, 1, QDir::homePath());
62 IEditor *editor = m_editorManager->currentEditor();
63 if (editor && !editor->file()->fileName().isEmpty()) {
64 QFileInfo info(editor->file()->fileName());
65 directory.prepend(info.absolutePath() + QLatin1Char('/'));
69 QDir dirInfo(directory);
70 QDir::Filters dirFilter = QDir::Dirs|QDir::Drives;
71 QDir::Filters fileFilter = QDir::Files;
72 if (m_includeHidden) {
73 dirFilter |= QDir::Hidden;
74 fileFilter |= QDir::Hidden;
76 QStringList dirs = dirInfo.entryList(dirFilter,
77 QDir::Name|QDir::IgnoreCase|QDir::LocaleAware);
78 QStringList files = dirInfo.entryList(fileFilter,
79 QDir::Name|QDir::IgnoreCase|QDir::LocaleAware);
80 foreach (const QString &dir, dirs) {
81 if (future.isCanceled())
83 if (dir != QLatin1String(".") && (name.isEmpty() || dir.startsWith(name, Qt::CaseInsensitive))) {
84 FilterEntry filterEntry(this, dir, dirInfo.filePath(dir));
85 filterEntry.resolveFileIcon = true;
86 value.append(filterEntry);
89 foreach (const QString &file, files) {
90 if (future.isCanceled())
92 if (name.isEmpty() || file.startsWith(name, Qt::CaseInsensitive)) {
93 const QString fullPath = dirInfo.filePath(file);
94 FilterEntry filterEntry(this, file, fullPath);
95 filterEntry.resolveFileIcon = true;
96 value.append(filterEntry);
102 void FileSystemFilter::accept(FilterEntry selection) const
104 QFileInfo info(selection.internalData.toString());
106 QString value = shortcutString();
107 value += QLatin1Char(' ');
108 value += QDir::toNativeSeparators(info.absoluteFilePath() + QLatin1Char('/'));
109 m_locatorWidget->show(value, value.length());
112 m_editorManager->openEditor(selection.internalData.toString(), QString(),
113 Core::EditorManager::ModeSwitch);
116 bool FileSystemFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)
118 Q_UNUSED(needsRefresh)
119 Ui::FileSystemFilterOptions ui;
120 QDialog dialog(parent);
123 ui.hiddenFilesFlag->setChecked(m_includeHidden);
124 ui.limitCheck->setChecked(!isIncludedByDefault());
125 ui.shortcutEdit->setText(shortcutString());
127 if (dialog.exec() == QDialog::Accepted) {
128 m_includeHidden = ui.hiddenFilesFlag->isChecked();
129 setShortcutString(ui.shortcutEdit->text().trimmed());
130 setIncludedByDefault(!ui.limitCheck->isChecked());
136 QByteArray FileSystemFilter::saveState() const
139 QDataStream out(&value, QIODevice::WriteOnly);
140 out << m_includeHidden;
141 out << shortcutString();
142 out << isIncludedByDefault();
146 bool FileSystemFilter::restoreState(const QByteArray &state)
148 QDataStream in(state);
149 in >> m_includeHidden;
151 // An attempt to prevent setting this on old configuration
157 setShortcutString(shortcut);
158 setIncludedByDefault(defaultFilter);