1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "currentprojectfind.h"
27 
28 #include "project.h"
29 #include "projecttree.h"
30 #include "session.h"
31 
32 #include <utils/qtcassert.h>
33 #include <utils/filesearch.h>
34 
35 #include <QDebug>
36 #include <QSettings>
37 
38 using namespace ProjectExplorer;
39 using namespace ProjectExplorer::Internal;
40 using namespace TextEditor;
41 
CurrentProjectFind()42 CurrentProjectFind::CurrentProjectFind()
43 {
44     connect(ProjectTree::instance(), &ProjectTree::currentProjectChanged,
45             this, &CurrentProjectFind::handleProjectChanged);
46     connect(SessionManager::instance(), &SessionManager::projectDisplayNameChanged,
47             this, [this](ProjectExplorer::Project *p) {
48         if (p == ProjectTree::currentProject())
49             emit displayNameChanged();
50     });
51 }
52 
id() const53 QString CurrentProjectFind::id() const
54 {
55     return QLatin1String("Current Project");
56 }
57 
displayName() const58 QString CurrentProjectFind::displayName() const
59 {
60     Project *p = ProjectTree::currentProject();
61     if (p)
62         return tr("Project \"%1\"").arg(p->displayName());
63     else
64         return tr("Current Project");
65 }
66 
isEnabled() const67 bool CurrentProjectFind::isEnabled() const
68 {
69     return ProjectTree::currentProject() != nullptr && BaseFileFind::isEnabled();
70 }
71 
additionalParameters() const72 QVariant CurrentProjectFind::additionalParameters() const
73 {
74     Project *project = ProjectTree::currentProject();
75     if (project)
76         return QVariant::fromValue(project->projectFilePath().toString());
77     return QVariant();
78 }
79 
files(const QStringList & nameFilters,const QStringList & exclusionFilters,const QVariant & additionalParameters) const80 Utils::FileIterator *CurrentProjectFind::files(const QStringList &nameFilters,
81                                                const QStringList &exclusionFilters,
82                                                const QVariant &additionalParameters) const
83 {
84     QTC_ASSERT(additionalParameters.isValid(),
85                return new Utils::FileListIterator(QStringList(), QList<QTextCodec *>()));
86     QString projectFile = additionalParameters.toString();
87     for (Project *project : SessionManager::projects()) {
88         if (project && projectFile == project->projectFilePath().toString())
89             return filesForProjects(nameFilters, exclusionFilters, {project});
90     }
91     return new Utils::FileListIterator(QStringList(), QList<QTextCodec *>());
92 }
93 
label() const94 QString CurrentProjectFind::label() const
95 {
96     Project *p = ProjectTree::currentProject();
97     QTC_ASSERT(p, return QString());
98     return tr("Project \"%1\":").arg(p->displayName());
99 }
100 
handleProjectChanged()101 void CurrentProjectFind::handleProjectChanged()
102 {
103     emit enabledChanged(isEnabled());
104     emit displayNameChanged();
105 }
106 
recheckEnabled()107 void CurrentProjectFind::recheckEnabled()
108 {
109     auto search = qobject_cast<Core::SearchResult *>(sender());
110     if (!search)
111         return;
112     QString projectFile = getAdditionalParameters(search).toString();
113     for (Project *project : SessionManager::projects()) {
114         if (projectFile == project->projectFilePath().toString()) {
115             search->setSearchAgainEnabled(true);
116             return;
117         }
118     }
119     search->setSearchAgainEnabled(false);
120 }
121 
writeSettings(QSettings * settings)122 void CurrentProjectFind::writeSettings(QSettings *settings)
123 {
124     settings->beginGroup(QLatin1String("CurrentProjectFind"));
125     writeCommonSettings(settings);
126     settings->endGroup();
127 }
128 
readSettings(QSettings * settings)129 void CurrentProjectFind::readSettings(QSettings *settings)
130 {
131     settings->beginGroup(QLatin1String("CurrentProjectFind"));
132     readCommonSettings(settings, "*", "");
133     settings->endGroup();
134 }
135