1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "perforcesubmiteditor.h"
35 #include "perforcesubmiteditorwidget.h"
36 #include "perforceplugin.h"
37 #include "perforceconstants.h"
39 #include <vcsbase/submitfilemodel.h>
40 #include <utils/qtcassert.h>
42 #include <QtCore/QDebug>
47 enum { FileSpecRole = Qt::UserRole + 1 };
49 PerforceSubmitEditor::PerforceSubmitEditor(const VCSBase::VCSBaseSubmitEditorParameters *parameters, QWidget *parent) :
50 VCSBaseSubmitEditor(parameters, new PerforceSubmitEditorWidget(parent)),
51 m_fileModel(new VCSBase::SubmitFileModel(this))
53 setDisplayName(tr("Perforce Submit"));
54 setFileModel(m_fileModel);
57 PerforceSubmitEditorWidget *PerforceSubmitEditor::submitEditorWidget()
59 return static_cast<PerforceSubmitEditorWidget *>(widget());
62 QString PerforceSubmitEditor::fileContents() const
64 const_cast<PerforceSubmitEditor*>(this)->updateEntries();
66 QTextStream out(&text);
67 QMapIterator<QString, QString> it(m_entries);
68 while (it.hasNext()) {
70 out << it.key() << ":" << it.value();
72 if (Perforce::Constants::debug)
73 qDebug() << Q_FUNC_INFO << text;
77 bool PerforceSubmitEditor::setFileContents(const QString &contents)
79 if (Perforce::Constants::debug)
80 qDebug() << Q_FUNC_INFO << contents;
81 if (!parseText(contents))
87 bool PerforceSubmitEditor::parseText(QString text)
89 const QRegExp formField(QLatin1String("^\\S+:"));
90 const QString newLine = QString(QLatin1Char('\n'));
94 QTextStream stream(&text, QIODevice::ReadOnly);
98 line = stream.readLine();
99 while (!stream.atEnd()) {
100 match = formField.indexIn(line);
102 matchLen = formField.matchedLength();
103 key = line.left(matchLen-1);
104 value = line.mid(matchLen) + newLine;
105 while (!stream.atEnd()) {
106 line = stream.readLine();
107 if (formField.indexIn(line) != -1)
109 value += line + newLine;
111 m_entries.insert(key, value);
113 line = stream.readLine();
119 void PerforceSubmitEditor::restrictToProjectFiles(const QStringList &knownProjectFiles)
121 m_fileModel->filter(knownProjectFiles, fileNameColumn());
124 void PerforceSubmitEditor::updateFields()
126 PerforceSubmitEditorWidget *widget = submitEditorWidget();
127 widget->setData(m_entries.value(QLatin1String("Change")).trimmed(),
128 m_entries.value(QLatin1String("Client")).trimmed(),
129 m_entries.value(QLatin1String("User")).trimmed());
131 const QString newLine = QString(QLatin1Char('\n'));
132 QStringList lines = m_entries.value(QLatin1String("Description")).split(newLine);
133 lines.removeFirst(); // that is the line break after 'Description:'
134 lines.removeLast(); // that is the empty line at the end
136 const QRegExp leadingTabPattern = QRegExp(QLatin1String("^\\t"));
137 QTC_ASSERT(leadingTabPattern.isValid(), /**/);
139 lines.replaceInStrings(leadingTabPattern, QString());
140 widget->setDescriptionText(lines.join(newLine));
142 lines = m_entries.value(QLatin1String("Files")).split(newLine);
143 // split up "file#add" and store complete spec line as user data
144 foreach (const QString &specLine, lines) {
145 const QStringList list = specLine.split(QLatin1Char('#'));
146 if (list.size() == 2) {
147 const QString file = list.at(0).trimmed();
148 const QString state = list.at(1).trimmed();
149 m_fileModel->addFile(file, state).at(0)->setData(specLine, FileSpecRole);
154 void PerforceSubmitEditor::updateEntries()
156 const QString newLine = QString(QLatin1Char('\n'));
157 const QString tab = QString(QLatin1Char('\t'));
159 QStringList lines = submitEditorWidget()->descriptionText().split(newLine);
161 while (!lines.empty() && lines.last().isEmpty())
164 lines.replaceInStrings(QRegExp(QLatin1String("^")), tab);
165 m_entries.insert(QLatin1String("Description"), newLine + lines.join(newLine) + QLatin1String("\n\n"));
166 QString files = newLine;
167 // Re-build the file spec '<tab>file#add' from the user data
168 const int count = m_fileModel->rowCount();
169 for (int r = 0; r < count; r++) {
170 const QStandardItem *item = m_fileModel->item(r, 0);
171 if (item->checkState() == Qt::Checked) {
172 files += item->data(FileSpecRole).toString();
177 m_entries.insert(QLatin1String("Files"), files);