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 "uicodecompletionsupport.h"
35 #include <QtCore/QProcess>
36 #include <QtCore/QFile>
37 #include <QtCore/QFileInfo>
41 using namespace CppTools;
42 using namespace CPlusPlus;
44 UiCodeModelSupport::UiCodeModelSupport(CppModelManagerInterface *modelmanager,
45 const QString &source,
46 const QString &uiHeaderFile)
47 : AbstractEditorSupport(modelmanager),
49 m_fileName(uiHeaderFile),
50 m_updateIncludingFiles(false),
54 qDebug()<<"ctor UiCodeModelSupport for"<<m_sourceName<<uiHeaderFile;
57 UiCodeModelSupport::~UiCodeModelSupport()
60 qDebug()<<"dtor ~UiCodeModelSupport for"<<m_sourceName;
63 void UiCodeModelSupport::init() const
66 QDateTime sourceTime = QFileInfo(m_sourceName).lastModified();
67 QFileInfo uiHeaderFileInfo(m_fileName);
68 QDateTime uiHeaderTime = uiHeaderFileInfo.exists() ? uiHeaderFileInfo.lastModified() : QDateTime();
69 if (uiHeaderTime.isValid() && (uiHeaderTime > sourceTime)) {
70 QFile file(m_fileName);
71 if (file.open(QFile::ReadOnly)) {
73 qDebug()<<"ui*h file is more recent then source file, using information from ui*h file"<<m_fileName;
74 QTextStream stream(&file);
75 m_contents = stream.readAll().toUtf8();
76 m_cacheTime = uiHeaderTime;
82 qDebug()<<"ui*h file not found, or not recent enough, trying to create it on the fly";
83 QFile file(m_sourceName);
84 if (file.open(QFile::ReadOnly)) {
85 QTextStream stream(&file);
86 const QString contents = stream.readAll();
87 if (runUic(contents)) {
89 qDebug()<<"created on the fly";
92 // uic run was unsuccesfull
94 qDebug()<<"uic run wasn't succesfull";
95 m_cacheTime = QDateTime ();
96 m_contents = QByteArray();
97 // and if the header file wasn't there, next time we need to update
98 // all of the files that include this header
99 if (!uiHeaderFileInfo.exists())
100 m_updateIncludingFiles = true;
105 qDebug()<<"Could open "<<m_sourceName<<"needed for the cpp model";
106 m_contents = QByteArray();
110 QByteArray UiCodeModelSupport::contents() const
118 QString UiCodeModelSupport::fileName() const
123 void UiCodeModelSupport::setFileName(const QString &name)
125 if (m_fileName == name && m_cacheTime.isValid())
129 qDebug() << "UiCodeModelSupport::setFileName"<<name;
133 m_cacheTime = QDateTime();
137 bool UiCodeModelSupport::runUic(const QString &ui) const
140 const QString uic = uicCommand();
141 process.setEnvironment(environment());
144 qDebug() << "UiCodeModelSupport::runUic " << uic << " on " << ui.size() << " bytes";
145 process.start(uic, QStringList(), QIODevice::ReadWrite);
146 if (!process.waitForStarted())
148 process.write(ui.toUtf8());
149 process.closeWriteChannel();
150 if (process.waitForFinished() && process.exitStatus() == QProcess::NormalExit && process.exitCode() == 0) {
151 m_contents = process.readAllStandardOutput();
152 m_cacheTime = QDateTime::currentDateTime();
154 qDebug() << "ok" << m_contents.size() << "bytes.";
158 qDebug() << "failed" << process.readAllStandardError();
164 void UiCodeModelSupport::updateFromEditor(const QString &formEditorContents)
166 if (runUic(formEditorContents)) {
171 void UiCodeModelSupport::updateFromBuild()
174 qDebug()<<"UiCodeModelSupport::updateFromBuild() for file"<<m_sourceName;
175 // This is mostly a fall back for the cases when uic couldn't be run
176 // it pays special attention to the case where a ui_*h was newly created
177 QDateTime sourceTime = QFileInfo(m_sourceName).lastModified();
178 if (m_cacheTime.isValid() && m_cacheTime >= sourceTime) {
180 qDebug()<<"Cache is still more recent then source";
183 QFileInfo fi(m_fileName);
184 QDateTime uiHeaderTime = fi.exists() ? fi.lastModified() : QDateTime();
185 if (uiHeaderTime.isValid() && (uiHeaderTime > sourceTime)) {
186 if (m_cacheTime >= uiHeaderTime)
189 qDebug()<<"found ui*h updating from it";
191 QFile file(m_fileName);
192 if (file.open(QFile::ReadOnly)) {
193 QTextStream stream(&file);
194 m_contents = stream.readAll().toUtf8();
195 m_cacheTime = uiHeaderTime;
201 qDebug()<<"ui*h not found or not more recent then source not changing anything";