2013-05-07 14:02:08 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
|
** Contact: http://www.qt.io/licensing
|
2013-05-07 14:02:08 +02:00
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-14 18:07:15 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms and
|
|
|
|
|
** conditions see http://www.qt.io/terms-conditions. For further information
|
2014-10-01 13:21:18 +02:00
|
|
|
** use the contact form at http://www.qt.io/contact-us.
|
2013-05-07 14:02:08 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2014-10-01 13:21:18 +02:00
|
|
|
** General Public License version 2.1 or version 3 as published by the Free
|
|
|
|
|
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
|
|
|
|
|
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
|
|
|
|
|
** following information to ensure the GNU Lesser General Public License
|
|
|
|
|
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2013-05-07 14:02:08 +02:00
|
|
|
**
|
2015-01-14 18:07:15 +01:00
|
|
|
** In addition, as a special exception, The Qt Company gives you certain additional
|
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
2013-05-07 14:02:08 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-01-30 13:31:11 +01:00
|
|
|
#include "diffeditordocument.h"
|
2013-07-04 22:25:15 +02:00
|
|
|
#include "diffeditorconstants.h"
|
2014-01-31 09:01:49 +01:00
|
|
|
#include "diffeditorcontroller.h"
|
2014-10-24 14:11:12 +02:00
|
|
|
#include "diffeditormanager.h"
|
|
|
|
|
#include "diffeditorreloader.h"
|
2014-06-30 15:04:36 +02:00
|
|
|
#include "diffutils.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2013-07-04 22:25:15 +02:00
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
2014-06-30 15:04:36 +02:00
|
|
|
#include <QFile>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QTextCodec>
|
2013-05-07 14:02:08 +02:00
|
|
|
|
|
|
|
|
namespace DiffEditor {
|
|
|
|
|
|
2014-03-10 13:28:34 +01:00
|
|
|
DiffEditorDocument::DiffEditorDocument() :
|
2014-09-22 18:43:31 +02:00
|
|
|
Core::BaseTextDocument(),
|
2014-07-07 10:23:11 +02:00
|
|
|
m_controller(new DiffEditorController(this))
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
2014-03-05 15:58:12 +01:00
|
|
|
setId(Constants::DIFF_EDITOR_ID);
|
2014-07-23 23:08:04 +03:00
|
|
|
setMimeType(QLatin1String(Constants::DIFF_EDITOR_MIMETYPE));
|
2013-07-12 15:36:29 +02:00
|
|
|
setTemporary(true);
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-02-11 13:00:35 +01:00
|
|
|
DiffEditorController *DiffEditorDocument::controller() const
|
2014-01-31 09:01:49 +01:00
|
|
|
{
|
2014-07-07 10:23:11 +02:00
|
|
|
return m_controller;
|
2014-01-31 09:01:49 +01:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 13:31:11 +01:00
|
|
|
bool DiffEditorDocument::setContents(const QByteArray &contents)
|
2013-07-15 15:14:10 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(contents);
|
2013-07-19 12:37:17 +03:00
|
|
|
return true;
|
2013-07-15 15:14:10 +02:00
|
|
|
}
|
|
|
|
|
|
2014-06-30 15:04:36 +02:00
|
|
|
QString DiffEditorDocument::defaultPath() const
|
|
|
|
|
{
|
2014-07-07 10:23:11 +02:00
|
|
|
return m_controller->workingDirectory();
|
2014-06-30 15:04:36 +02:00
|
|
|
}
|
|
|
|
|
|
2014-01-30 13:31:11 +01:00
|
|
|
bool DiffEditorDocument::save(QString *errorString, const QString &fileName, bool autoSave)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
2014-01-30 13:06:23 +01:00
|
|
|
Q_UNUSED(errorString)
|
|
|
|
|
Q_UNUSED(autoSave)
|
2014-06-30 15:04:36 +02:00
|
|
|
|
2014-11-11 16:27:23 +01:00
|
|
|
const bool ok = write(fileName, format(), m_controller->contents(), errorString);
|
2014-06-30 15:04:36 +02:00
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
|
return false;
|
|
|
|
|
|
2014-11-07 10:30:19 +01:00
|
|
|
m_controller->setReloader(0);
|
|
|
|
|
m_controller->setDescription(QString());
|
|
|
|
|
m_controller->setDescriptionEnabled(false);
|
2014-10-24 14:11:12 +02:00
|
|
|
|
|
|
|
|
DiffEditorManager::removeDocument(this);
|
2014-06-30 15:04:36 +02:00
|
|
|
const QFileInfo fi(fileName);
|
2014-10-23 16:47:20 +02:00
|
|
|
setTemporary(false);
|
2014-12-21 21:54:30 +02:00
|
|
|
setFilePath(Utils::FileName::fromString(fi.absoluteFilePath()));
|
2014-06-30 15:04:36 +02:00
|
|
|
setDisplayName(QString());
|
|
|
|
|
return true;
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-23 16:47:20 +02:00
|
|
|
bool DiffEditorDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
|
|
|
|
Q_UNUSED(type)
|
2014-10-23 16:47:20 +02:00
|
|
|
if (flag == FlagIgnore)
|
|
|
|
|
return true;
|
2014-12-21 21:54:30 +02:00
|
|
|
return open(errorString, filePath().toString());
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-10-23 16:47:20 +02:00
|
|
|
bool DiffEditorDocument::open(QString *errorString, const QString &fileName)
|
2013-05-07 14:02:08 +02:00
|
|
|
{
|
2014-10-23 16:47:20 +02:00
|
|
|
QString patch;
|
|
|
|
|
if (read(fileName, &patch, errorString) != Utils::TextFileFormat::ReadSuccess)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
bool ok = false;
|
2014-10-24 14:11:12 +02:00
|
|
|
QList<FileData> fileDataList = DiffUtils::readPatch(patch, &ok);
|
2014-10-23 16:47:20 +02:00
|
|
|
if (!ok) {
|
|
|
|
|
*errorString = tr("Could not parse patch file \"%1\". "
|
|
|
|
|
"The content is not of unified diff format.")
|
|
|
|
|
.arg(fileName);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const QFileInfo fi(fileName);
|
|
|
|
|
setTemporary(false);
|
2014-12-21 21:54:30 +02:00
|
|
|
setFilePath(Utils::FileName::fromString(fi.absoluteFilePath()));
|
2014-10-23 16:47:20 +02:00
|
|
|
m_controller->setDiffFiles(fileDataList, fi.absolutePath());
|
|
|
|
|
return true;
|
2013-05-07 14:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-12 15:30:06 +01:00
|
|
|
QString DiffEditorDocument::suggestedFileName() const
|
|
|
|
|
{
|
2014-11-13 08:19:10 +01:00
|
|
|
enum { maxSubjectLength = 50 };
|
2014-11-12 15:30:06 +01:00
|
|
|
QString result = QStringLiteral("0001");
|
|
|
|
|
const QString description = m_controller->description();
|
|
|
|
|
if (!description.isEmpty()) {
|
|
|
|
|
// Derive "git format-patch-type" file name from subject.
|
|
|
|
|
const int pos = description.indexOf(QLatin1String("\n\n "));
|
|
|
|
|
const int endPos = pos >= 0 ? description.indexOf(QLatin1Char('\n'), pos + 6) : -1;
|
|
|
|
|
if (endPos > pos) {
|
|
|
|
|
const QChar space(QLatin1Char(' '));
|
|
|
|
|
const QChar dash(QLatin1Char('-'));
|
|
|
|
|
QString subject = description.mid(pos, endPos - pos);
|
|
|
|
|
for (int i = 0; i < subject.size(); ++i) {
|
|
|
|
|
if (!subject.at(i).isLetterOrNumber())
|
|
|
|
|
subject[i] = space;
|
|
|
|
|
}
|
|
|
|
|
subject = subject.simplified();
|
2014-11-13 08:19:10 +01:00
|
|
|
if (subject.size() > maxSubjectLength) {
|
|
|
|
|
const int lastSpace = subject.lastIndexOf(space, maxSubjectLength);
|
|
|
|
|
subject.truncate(lastSpace > 0 ? lastSpace : maxSubjectLength);
|
|
|
|
|
}
|
2014-11-12 15:30:06 +01:00
|
|
|
subject.replace(space, dash);
|
|
|
|
|
result += dash;
|
|
|
|
|
result += subject;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result + QStringLiteral(".patch");
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-13 09:11:56 +01:00
|
|
|
QString DiffEditorDocument::plainText() const
|
|
|
|
|
{
|
|
|
|
|
return m_controller->contents();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-07 14:02:08 +02:00
|
|
|
} // namespace DiffEditor
|