OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / qt4projectmanager / profilereader.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 "profilereader.h"
35
36 #include <coreplugin/icore.h>
37 #include <coreplugin/messagemanager.h>
38
39 #include <QtCore/QDir>
40 #include <QtCore/QDebug>
41
42 using namespace Qt4ProjectManager;
43 using namespace Qt4ProjectManager::Internal;
44
45 static QString format(const QString &fileName, int lineNo, const QString &msg)
46 {
47     if (lineNo)
48         return QString::fromLatin1("%1(%2): %3").arg(fileName, QString::number(lineNo), msg);
49     else
50         return msg;
51 }
52
53 ProMessageHandler::ProMessageHandler(bool verbose)
54     : m_verbose(verbose)
55 {
56     QObject::connect(this, SIGNAL(errorFound(QString)),
57                      Core::ICore::instance()->messageManager(), SLOT(printToOutputPane(QString)),
58                      Qt::QueuedConnection);
59 }
60
61 void ProMessageHandler::parseError(const QString &fileName, int lineNo, const QString &msg)
62 {
63     emit errorFound(format(fileName, lineNo, msg));
64 }
65
66 void ProMessageHandler::configError(const QString &msg)
67 {
68     emit errorFound(msg);
69 }
70
71 void ProMessageHandler::evalError(const QString &fileName, int lineNo, const QString &msg)
72 {
73     if (m_verbose)
74         emit errorFound(format(fileName, lineNo, msg));
75 }
76
77 void ProMessageHandler::fileMessage(const QString &)
78 {
79     // we ignore these...
80 }
81
82
83 ProFileReader::ProFileReader(ProFileOption *option)
84     : ProFileParser(ProFileCacheManager::instance()->cache(), this)
85     , ProFileEvaluator(option, this, this)
86     , m_ignoreLevel(0)
87 {
88 }
89
90 ProFileReader::~ProFileReader()
91 {
92     foreach (ProFile *pf, m_proFiles)
93         pf->deref();
94 }
95
96 void ProFileReader::aboutToEval(ProFile *, ProFile *pro, EvalFileType type)
97 {
98     if (m_ignoreLevel || (type != EvalProjectFile && type != EvalIncludeFile)) {
99         m_ignoreLevel++;
100     } else if (!m_includeFiles.contains(pro->fileName())) {
101         m_includeFiles.insert(pro->fileName(), pro);
102         m_proFiles.append(pro);
103         pro->ref();
104     }
105 }
106
107 void ProFileReader::doneWithEval(ProFile *)
108 {
109     if (m_ignoreLevel)
110         m_ignoreLevel--;
111 }
112
113 QList<ProFile*> ProFileReader::includeFiles() const
114 {
115     return m_includeFiles.values();
116 }
117
118 ProFile *ProFileReader::proFileFor(const QString &name)
119 {
120     return m_includeFiles.value(name);
121 }
122
123 ProFileCacheManager *ProFileCacheManager::s_instance = 0;
124
125 ProFileCacheManager::ProFileCacheManager(QObject *parent) :
126         QObject(parent),
127         m_cache(0),
128         m_refCount(0)
129 {
130     s_instance = this;
131     m_timer.setInterval(5000);
132     m_timer.setSingleShot(true);
133     connect(&m_timer, SIGNAL(timeout()),
134             this, SLOT(clear()));
135 }
136
137 void ProFileCacheManager::incRefCount()
138 {
139     ++m_refCount;
140     m_timer.stop();
141 }
142
143 void ProFileCacheManager::decRefCount()
144 {
145     --m_refCount;
146     if (!m_refCount)
147         m_timer.start();
148 }
149
150 ProFileCacheManager::~ProFileCacheManager()
151 {
152     s_instance = 0;
153     clear();
154 }
155
156 ProFileCache *ProFileCacheManager::cache()
157 {
158     if (!m_cache)
159         m_cache = new ProFileCache;
160     return m_cache;
161 }
162
163 void ProFileCacheManager::clear()
164 {
165     Q_ASSERT(m_refCount == 0);
166     // Just deleting the cache will be safe as long as the sequence of
167     // obtaining a cache pointer and using it is atomic as far as the main
168     // loop is concerned. Use a shared pointer once this is not true anymore.
169     delete m_cache;
170     m_cache = 0;
171 }
172
173 void ProFileCacheManager::discardFiles(const QString &prefix)
174 {
175     if (m_cache)
176         m_cache->discardFiles(prefix);
177 }
178
179 void ProFileCacheManager::discardFile(const QString &fileName)
180 {
181     if (m_cache)
182         m_cache->discardFile(fileName);
183 }