OSDN Git Service

It's 2011 now.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / perforce / perforcesubmiteditor.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 "perforcesubmiteditor.h"
35 #include "perforcesubmiteditorwidget.h"
36 #include "perforceplugin.h"
37 #include "perforceconstants.h"
38
39 #include <vcsbase/submitfilemodel.h>
40 #include <utils/qtcassert.h>
41
42 #include <QtCore/QDebug>
43
44 namespace Perforce {
45 namespace Internal {
46
47 enum { FileSpecRole = Qt::UserRole + 1 };
48
49 PerforceSubmitEditor::PerforceSubmitEditor(const VCSBase::VCSBaseSubmitEditorParameters *parameters, QWidget *parent) :
50     VCSBaseSubmitEditor(parameters, new PerforceSubmitEditorWidget(parent)),
51     m_fileModel(new VCSBase::SubmitFileModel(this))
52 {
53     setDisplayName(tr("Perforce Submit"));
54     setFileModel(m_fileModel);
55 }
56
57 PerforceSubmitEditorWidget *PerforceSubmitEditor::submitEditorWidget()
58 {
59     return static_cast<PerforceSubmitEditorWidget *>(widget());
60 }
61
62 QString PerforceSubmitEditor::fileContents() const
63 {
64     const_cast<PerforceSubmitEditor*>(this)->updateEntries();
65     QString text;
66     QTextStream out(&text);
67     QMapIterator<QString, QString> it(m_entries);
68     while (it.hasNext()) {
69         it.next();
70         out << it.key() << ":" << it.value();
71     }
72     if (Perforce::Constants::debug)
73         qDebug() << Q_FUNC_INFO << text;
74     return text;
75 }
76
77 bool PerforceSubmitEditor::setFileContents(const QString &contents)
78 {
79     if (Perforce::Constants::debug)
80         qDebug() << Q_FUNC_INFO << contents;
81     if (!parseText(contents))
82         return false;
83     updateFields();
84     return true;
85 }
86
87 bool PerforceSubmitEditor::parseText(QString text)
88 {
89     const QRegExp formField(QLatin1String("^\\S+:"));
90     const QString newLine = QString(QLatin1Char('\n'));
91
92     int match;
93     int matchLen;
94     QTextStream stream(&text, QIODevice::ReadOnly);
95     QString line;
96     QString key;
97     QString value;
98     line = stream.readLine();
99     while (!stream.atEnd()) {
100         match = formField.indexIn(line);
101         if (match == 0) {
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)
108                     break;
109                 value += line + newLine;
110             }
111             m_entries.insert(key, value);
112         } else {
113             line = stream.readLine();
114         }
115     }
116     return true;
117 }
118
119 void PerforceSubmitEditor::restrictToProjectFiles(const QStringList &knownProjectFiles)
120 {
121     m_fileModel->filter(knownProjectFiles, fileNameColumn());
122 }
123
124 void PerforceSubmitEditor::updateFields()
125 {
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());
130
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
135
136     const QRegExp leadingTabPattern = QRegExp(QLatin1String("^\\t"));
137     QTC_ASSERT(leadingTabPattern.isValid(), /**/);
138
139     lines.replaceInStrings(leadingTabPattern, QString());
140     widget->setDescriptionText(lines.join(newLine));
141
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);
150         }
151     }
152 }
153
154 void PerforceSubmitEditor::updateEntries()
155 {
156     const QString newLine = QString(QLatin1Char('\n'));
157     const QString tab = QString(QLatin1Char('\t'));
158
159     QStringList lines = submitEditorWidget()->descriptionText().split(newLine);
160
161     while (!lines.empty() && lines.last().isEmpty())
162             lines.removeLast();
163     // Description
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();
173             files += newLine;
174         }
175     }
176     files += newLine;
177     m_entries.insert(QLatin1String("Files"), files);
178 }
179
180 } // Internal
181 } // Perforce