OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / locator / filesystemfilter.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 (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
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.
18 **
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.
22 **
23 ** Other Usage
24 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "filesystemfilter.h"
34 #include "locatorwidget.h"
35 #include <coreplugin/editormanager/ieditor.h>
36 #include <coreplugin/editormanager/editormanager.h>
37
38 #include <QtCore/QDir>
39
40 using namespace Core;
41 using namespace Locator;
42 using namespace Locator::Internal;
43
44 FileSystemFilter::FileSystemFilter(EditorManager *editorManager, LocatorWidget *locatorWidget)
45         : m_editorManager(editorManager), m_locatorWidget(locatorWidget), m_includeHidden(true)
46 {
47     setShortcutString(QString(QLatin1Char('f')));
48     setIncludedByDefault(false);
49 }
50
51 QList<FilterEntry> FileSystemFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &entry)
52 {
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());
61         } else {
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('/'));
66             }
67         }
68     }
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;
75     }
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())
82             break;
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);
87         }
88     }
89     foreach (const QString &file, files) {
90         if (future.isCanceled())
91             break;
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);
97         }
98     }
99     return value;
100 }
101
102 void FileSystemFilter::accept(FilterEntry selection) const
103 {
104     QFileInfo info(selection.internalData.toString());
105     if (info.isDir()) {
106         QString value = shortcutString();
107         value += QLatin1Char(' ');
108         value += QDir::toNativeSeparators(info.absoluteFilePath() + QLatin1Char('/'));
109         m_locatorWidget->show(value, value.length());
110         return;
111     }
112     m_editorManager->openEditor(selection.internalData.toString(), QString(),
113                                 Core::EditorManager::ModeSwitch);
114 }
115
116 bool FileSystemFilter::openConfigDialog(QWidget *parent, bool &needsRefresh)
117 {
118     Q_UNUSED(needsRefresh)
119     Ui::FileSystemFilterOptions ui;
120     QDialog dialog(parent);
121     ui.setupUi(&dialog);
122
123     ui.hiddenFilesFlag->setChecked(m_includeHidden);
124     ui.limitCheck->setChecked(!isIncludedByDefault());
125     ui.shortcutEdit->setText(shortcutString());
126
127     if (dialog.exec() == QDialog::Accepted) {
128         m_includeHidden = ui.hiddenFilesFlag->isChecked();
129         setShortcutString(ui.shortcutEdit->text().trimmed());
130         setIncludedByDefault(!ui.limitCheck->isChecked());
131         return true;
132     }
133     return false;
134 }
135
136 QByteArray FileSystemFilter::saveState() const
137 {
138     QByteArray value;
139     QDataStream out(&value, QIODevice::WriteOnly);
140     out << m_includeHidden;
141     out << shortcutString();
142     out << isIncludedByDefault();
143     return value;
144 }
145
146 bool FileSystemFilter::restoreState(const QByteArray &state)
147 {
148     QDataStream in(state);
149     in >> m_includeHidden;
150
151     // An attempt to prevent setting this on old configuration
152     if (!in.atEnd()) {
153         QString shortcut;
154         bool defaultFilter;
155         in >> shortcut;
156         in >> defaultFilter;
157         setShortcutString(shortcut);
158         setIncludedByDefault(defaultFilter);
159     }
160
161     return true;
162 }