Fixes variants combo box.
[qt-labs:qt-autotester.git] / src / qtdirectory.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the Qt Autotester project.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
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 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
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 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qtdirectory.h"
43
44 #include <QProcess>
45 #include <QFile>
46 #include <QDir>
47 #include <QStandardItemModel>
48 #include <QDebug>
49
50 /**************************************************************************************************
51  **************************************************************************************************/
52 QtDirectory::QtDirectory(const QString &name, const QString &location
53 #ifdef Q_WS_WIN
54                         , const QString &make
55 #endif
56                 )
57     : QObject(),
58     repoName(name),
59     repoQmake(location)
60 {
61     repoBuildDir = location;
62     QString toRemove = "/bin/qmake";
63 #ifdef Q_WS_WIN
64     toRemove += QString(".exe");
65 #endif
66     repoBuildDir.replace(toRemove, QString(""));
67     repoTestBuildDir = repoBuildDir + QString("/tests/auto");
68     repoBinDir = repoBuildDir + QString("/bin");
69
70     bool symbianBuildSystem = false;
71     repoVariants.clear();
72
73     QFile cache(repoBuildDir + "/.qmake.cache");
74     if (cache.open(QIODevice::ReadOnly | QIODevice::Text)) {
75         while (!cache.atEnd()) {
76             QString line = cache.readLine();
77             if (line.contains("QT_SOURCE_TREE")) {
78                 line.replace("\\", "/");
79                 repoSrcDir = line;
80                 repoSrcDir.remove(0, repoSrcDir.indexOf("=") + 1);
81                 repoSrcDir = repoSrcDir.trimmed();
82                 repoSrcDir.replace("$$quote(", "");
83                 repoSrcDir.replace(")", "");
84                 repoTestSrcDir = repoSrcDir + QString("/tests/auto");
85                 break;
86             }
87             else if (line.startsWith("QMAKESPEC")) {
88                 if (line.contains("symbian")) {
89                     repoRemoteOS = "symbian";
90                 }
91                 else if (line.contains("wince")) {
92                     repoRemoteOS = "wince";
93                 }
94                 if (line.contains("symbian-abld") || line.contains("symbian-sbsv2")) {
95                     symbianBuildSystem = true;
96                     repoVariants.clear();
97                     repoVariants << "release-armv6";
98                     repoVariants << "release-armv5";
99                     repoVariants << "release-gcce";
100                     repoVariants << "release-winscw";
101                     repoVariants << "debug-armv6";
102                     repoVariants << "debug-armv5";
103                     repoVariants << "debug-gcce";
104                     repoVariants << "debug-winscw";
105                 }
106             }
107             else if (line.startsWith("CONFIG")) {
108                 if (!symbianBuildSystem) {
109                     if(line.contains(" release "))
110                         repoVariants << "release";
111                     if(line.contains(" debug "))
112                         repoVariants << "debug";
113                 }
114                 repoCrossCompiled = line.contains("cross_compile");
115             }
116         }
117     }
118
119     QStringList arguments;
120     arguments << "--version";
121     QProcess process;
122     process.start(location, arguments);
123     if (!process.waitForStarted(2000)) {
124         return;
125     }
126     process.waitForFinished(1000);
127     QString read = process.readAll();
128     repoLibDir = read.split(" in ").at(1).trimmed();
129
130 #ifdef Q_WS_WIN
131     repoMakeLocation = make;
132     repoMakeDir = make;
133     repoMakeDir.truncate(repoMakeDir.lastIndexOf("/"));
134 #endif
135 }
136
137 /**************************************************************************************************
138  **************************************************************************************************/
139 QtDirectory::~QtDirectory()
140 {
141 }
142
143 /**************************************************************************************************
144  **************************************************************************************************/
145 QStandardItemModel *QtDirectory::createModelFromPro() const
146 {
147     QStandardItemModel *model = new QStandardItemModel;
148     model->setObjectName(repoBuildDir);
149     model->setHorizontalHeaderItem(0, new QStandardItem("Auto-tests"));
150
151     QStandardItem *invisibleItem = model->invisibleRootItem();
152     QStandardItem *rootItem = new QStandardItem("All Tests");
153     rootItem->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
154     rootItem->setData(Qt::Unchecked, Qt::CheckStateRole);
155     rootItem->setIcon(QIcon(":/images/dir.png"));
156     rootItem->setData(true, Qt::UserRole + 1);
157     invisibleItem->appendRow(rootItem);
158     createChildrenItemsFromPro(".", "auto.pro", rootItem);
159
160     return model;
161 }
162
163 /**************************************************************************************************
164  **************************************************************************************************/
165 void QtDirectory::createChildrenItemsFromPro(const QString &dir, const QString &pro, QStandardItem *parent) const
166 {
167     if (dir.isEmpty() || pro.isEmpty() || !parent)
168         return;
169
170     QFile f(repoTestSrcDir + "/" + dir + "/" + pro);
171     if (!f.exists())
172         return;
173
174     QStandardItem *item;
175     if (pro == "auto.pro") {
176         item = parent;
177     } else {
178         QString name = pro;
179         name.replace(".pro", "");
180         item = new QStandardItem(name);
181         item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
182         item->setData(Qt::Unchecked, Qt::CheckStateRole);
183         item->setData("/" + dir, Qt::UserRole);
184         QString proFilePath = QFileInfo(f).absoluteFilePath();
185         proFilePath.replace(repoTestSrcDir, "");
186         item->setData(proFilePath, Qt::UserRole + 2);
187         parent->appendRow(item);
188     }
189
190     QStringList subdirs = getSubdirsFromPro(f);
191     QString tstName = pro;
192     tstName.replace(".pro", ".cpp");
193     QFileInfo srcFile(repoTestSrcDir + "/" + dir + "/tst_" + tstName);
194     QFileInfo srcFile2(repoTestSrcDir + "/" + dir + "/test/tst_" + tstName);
195     if (subdirs.isEmpty() || srcFile.exists() || srcFile2.exists()) {
196         // it is an autotest
197         item->setIcon(QIcon(":/images/test.png"));
198         item->setData(false, Qt::UserRole + 1);
199
200         if (srcFile2.exists())
201             item->setData("/" + dir + "/test", Qt::UserRole);
202
203     } else {
204         // it is a directory
205         item->setIcon(QIcon::fromTheme("folder", QIcon(":/images/dir.png")));
206         item->setData(true, Qt::UserRole + 1);
207         foreach (const QString &subdir, subdirs)
208             if (subdir.endsWith(".pro"))
209                 createChildrenItemsFromPro(dir, subdir, item);
210             else
211                 createChildrenItemsFromPro(dir + "/" + subdir, subdir + ".pro", item);
212     }
213 }
214
215 /**************************************************************************************************
216  **************************************************************************************************/
217 QStringList QtDirectory::getSubdirsFromPro(QFile &pro) const
218 {
219     QStringList list;
220
221     if (pro.open(QIODevice::ReadOnly | QIODevice::Text)) {
222         bool inSubdirs = false;
223         while (!pro.atEnd()) {
224             QString line = pro.readLine();
225             line = line.trimmed();
226             if (!inSubdirs) {
227                 int comPos;
228                 if ((comPos = line.indexOf("#")) != -1)
229                     line.truncate(comPos);
230                 if (line.contains("SUBDIRS +=") || line.contains("Q3SUBDIRS +=")
231                     || line.contains("SUBDIRS =") || line.contains("Q3SUBDIRS =")
232                     || line.contains("SUBDIRS+=") || line.contains("Q3SUBDIRS+=")
233                     || line.contains("SUBDIRS=") || line.contains("Q3SUBDIRS=")) {
234                     inSubdirs = true;
235                     line.remove(0, line.indexOf("=") + 1);
236                     if (!line.contains("\\"))
237                         inSubdirs = false;
238                     line.replace("\\", "");
239                     line = line.trimmed();
240                     QStringList sublist = line.split(" ", QString::SkipEmptyParts);
241                     for (int i = 0; i < sublist.count(); i++) {
242                         if (sublist.at(i).startsWith("$$")) {
243                             sublist.removeAt(i);
244                             i--;
245                         }
246                     }
247                     list.append(sublist);
248                 }
249             } else {
250                 if (line.isEmpty()) {
251                     inSubdirs = false;
252                 } else {
253                     if (!line.contains("\\"))
254                         inSubdirs = false;
255                     line.replace("\\", "");
256                     int comPos;
257                     if ((comPos = line.indexOf("#")) != -1)
258                         line.truncate(comPos);
259                     QStringList sublist = line.split(" ", QString::SkipEmptyParts);
260                     for (int i = 0; i < sublist.count(); i++) {
261                         if (sublist.at(i).startsWith("$$")) {
262                             sublist.removeAt(i);
263                             i--;
264                         }
265                     }
266                     list.append(sublist);
267                 }
268             }
269         }
270     }
271
272     list.removeDuplicates();
273     return list;
274 }
275