10#include "komparemodellist.h"
18#include <QTemporaryFile>
20#include <QMimeDatabase>
22#include <KActionCollection>
24#include <KIO/UDSEntry>
26#include <KIO/MkdirJob>
27#include <KIO/FileCopyJob>
28#include <KLocalizedString>
29#include <KStandardAction>
31#include <komparediffdebug.h>
33#include "kompareprocess.h"
38#if KOMPAREDIFF2_BUILD_DEPRECATED_SINCE(5, 4)
42 Q_UNUSED(widgetForKIO)
48 m_diffProcess(nullptr),
49 m_diffSettings(diffSettings),
51 m_selectedModel(nullptr),
52 m_selectedDifference(nullptr),
56 m_isReadWrite(supportReadWrite)
58 qCDebug(LIBKOMPAREDIFF2) <<
"Show me the arguments: " << diffSettings <<
", " <<
parent <<
", " << name;
60 if (supportReadWrite) {
61 m_applyDifference = m_actionCollection->
addAction(QStringLiteral(
"difference_apply"),
this, &KompareModelList::slotActionApplyDifference);
63 m_applyDifference->
setText(
i18nc(
"@action",
"&Apply Difference"));
65 m_unApplyDifference = m_actionCollection->
addAction(QStringLiteral(
"difference_unapply"),
this, &KompareModelList::slotActionUnApplyDifference);
67 m_unApplyDifference->
setText(
i18nc(
"@action",
"Un&apply Difference"));
69 m_applyAll = m_actionCollection->
addAction(QStringLiteral(
"difference_applyall"),
this, &KompareModelList::slotActionApplyAllDifferences);
73 m_unapplyAll = m_actionCollection->
addAction(QStringLiteral(
"difference_unapplyall"),
this, &KompareModelList::slotActionUnapplyAllDifferences);
78 m_applyDifference =
nullptr;
79 m_unApplyDifference =
nullptr;
81 m_unapplyAll =
nullptr;
83 m_previousFile = m_actionCollection->
addAction(QStringLiteral(
"difference_previousfile"),
this, &KompareModelList::slotPreviousModel);
85 m_previousFile->
setText(
i18nc(
"@action",
"P&revious File"));
87 m_nextFile = m_actionCollection->
addAction(QStringLiteral(
"difference_nextfile"),
this, &KompareModelList::slotNextModel);
91 m_previousDifference = m_actionCollection->
addAction(QStringLiteral(
"difference_previous"),
this, &KompareModelList::slotPreviousDifference);
93 m_previousDifference->
setText(
i18nc(
"@action",
"&Previous Difference"));
95 m_nextDifference = m_actionCollection->
addAction(QStringLiteral(
"difference_next"),
this, &KompareModelList::slotNextDifference);
97 m_nextDifference->
setText(
i18nc(
"@action",
"&Next Difference"));
102 if (supportReadWrite) {
109 updateModelListActions();
112KompareModelList::~KompareModelList()
114 m_selectedModel =
nullptr;
115 m_selectedDifference =
nullptr;
120bool KompareModelList::isDirectory(
const QString& url)
const
129bool KompareModelList::isDiff(
const QString& mimeType)
const
137bool KompareModelList::compare()
141 bool sourceIsDirectory = isDirectory(m_info->localSource);
142 bool destinationIsDirectory = isDirectory(m_info->localDestination);
144 if (sourceIsDirectory && destinationIsDirectory)
146 m_info->mode = Kompare::ComparingDirs;
147 result = compare(m_info->mode);
149 else if (!sourceIsDirectory && !destinationIsDirectory)
151 QFile sourceFile(m_info->localSource);
156 qCDebug(LIBKOMPAREDIFF2) <<
"Mimetype source : " << sourceMimeType;
158 QFile destinationFile(m_info->localDestination);
161 destinationFile.close();
162 qCDebug(LIBKOMPAREDIFF2) <<
"Mimetype destination: " << destinationMimeType;
165 if (!isDiff(sourceMimeType) && isDiff(destinationMimeType))
167 qCDebug(LIBKOMPAREDIFF2) <<
"Blending destination into source...";
168 m_info->mode = Kompare::BlendingFile;
169 result = openFileAndDiff();
171 else if (isDiff(sourceMimeType) && !isDiff(destinationMimeType))
173 qCDebug(LIBKOMPAREDIFF2) <<
"Blending source into destination...";
174 m_info->mode = Kompare::BlendingFile;
176 m_info->swapSourceWithDestination();
179 result = openFileAndDiff();
183 qCDebug(LIBKOMPAREDIFF2) <<
"Comparing source with destination";
184 m_info->mode = Kompare::ComparingFiles;
185 result = compare(m_info->mode);
188 else if (sourceIsDirectory && !destinationIsDirectory)
190 m_info->mode = Kompare::BlendingDir;
191 result = openDirAndDiff();
195 m_info->mode = Kompare::BlendingDir;
197 m_info->swapSourceWithDestination();
200 result = openDirAndDiff();
210 m_diffProcess =
new KompareProcess(m_diffSettings, Kompare::Custom, m_info->localSource, m_info->localDestination,
QString(), mode);
211 m_diffProcess->setEncoding(m_encoding);
213 connect(m_diffProcess, &KompareProcess::diffHasFinished,
214 this, &KompareModelList::slotDiffProcessFinished);
217 m_diffProcess->start();
225 for (uint i = 0; i < count; ++i)
238 return from.
mid(position + 1);
241void KompareModelList::setDepthAndApplied()
245 for (
DiffModel* model : splicedModelList) {
246 model->setSourceFile(lstripSeparators(model->source(), m_info->depth));
247 model->setDestinationFile(lstripSeparators(model->destination(), m_info->depth));
248 model->applyAllDifferences(m_info->applied);
252bool KompareModelList::openFileAndDiff()
256 if (parseDiffOutput(readFile(m_info->localDestination)) != 0)
258 Q_EMIT error(
i18n(
"<qt>No models or no differences, this file: <b>%1</b>, is not a valid diff file.</qt>", m_info->destination.
url()));
262 setDepthAndApplied();
264 if (!blendOriginalIntoModelList(m_info->localSource))
266 qCDebug(LIBKOMPAREDIFF2) <<
"Oops cant blend original file into modellist : " << m_info->localSource;
267 Q_EMIT error(
i18n(
"<qt>There were problems applying the diff <b>%1</b> to the file <b>%2</b>.</qt>", m_info->destination.
url(), m_info->source.
url()));
271 updateModelListActions();
277bool KompareModelList::openDirAndDiff()
281 if (parseDiffOutput(readFile(m_info->localDestination)) != 0)
283 Q_EMIT error(
i18n(
"<qt>No models or no differences, this file: <b>%1</b>, is not a valid diff file.</qt>", m_info->destination.
url()));
287 setDepthAndApplied();
290 if (!blendOriginalIntoModelList(m_info->localSource))
293 qCDebug(LIBKOMPAREDIFF2) <<
"Oops cant blend original dir into modellist : " << m_info->localSource;
294 Q_EMIT error(
i18n(
"<qt>There were problems applying the diff <b>%1</b> to the folder <b>%2</b>.</qt>", m_info->destination.
url(), m_info->source.
url()));
298 updateModelListActions();
309 saveDestination(m_selectedModel);
315bool KompareModelList::saveDestination(
DiffModel* model)
317 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::saveDestination: ";
320 if (!model->hasUnsavedChanges())
326 Q_EMIT error(
i18n(
"Could not open a temporary file."));
334 DiffHunkListConstIterator hunkIt = model->hunks()->
constBegin();
335 DiffHunkListConstIterator hEnd = model->hunks()->
constEnd();
337 for (; hunkIt != hEnd; ++hunkIt)
341 DifferenceListConstIterator diffIt = hunk->differences().
constBegin();
342 DifferenceListConstIterator dEnd = hunk->differences().
constEnd();
345 for (; diffIt != dEnd; ++diffIt)
348 if (!diff->applied())
350 DifferenceStringListConstIterator stringIt = diff->destinationLines().begin();
351 DifferenceStringListConstIterator sEnd = diff->destinationLines().end();
352 for (; stringIt != sEnd; ++stringIt)
354 list.
append((*stringIt)->string());
359 DifferenceStringListConstIterator stringIt = diff->sourceLines().begin();
360 DifferenceStringListConstIterator sEnd = diff->sourceLines().end();
361 for (; stringIt != sEnd; ++stringIt)
374 Q_EMIT error(
i18n(
"<qt>Could not write to the temporary file <b>%1</b>, deleting it.</qt>", temp.
fileName()));
381 Q_EMIT error(
i18n(
"<qt>Could not write to the temporary file <b>%1</b>, deleting it.</qt>", temp.
fileName()));
389 if (m_info->mode == Kompare::ComparingDirs)
393 qCDebug(LIBKOMPAREDIFF2) <<
"Tempfilename (save) : " << temp.
fileName();
394 qCDebug(LIBKOMPAREDIFF2) <<
"Model->path+file : " << model->destinationPath() << model->destinationFile();
395 qCDebug(LIBKOMPAREDIFF2) <<
"info->localdest : " << m_info->localDestination;
396 QString tmp = model->destinationPath();
398 tmp.
remove(0, m_info->localDestination.
size());
399 qCDebug(LIBKOMPAREDIFF2) <<
"DestinationURL : " << m_info->destination;
400 qCDebug(LIBKOMPAREDIFF2) <<
"tmp : " << tmp;
402 QUrl fullDestinationPath = m_info->destination;
403 fullDestinationPath.
setPath(fullDestinationPath.
path() + tmp);
404 qCDebug(LIBKOMPAREDIFF2) <<
"fullDestinationPath : " << fullDestinationPath;
406 if (!statJob->
exec())
410 if (!mkdirJob->
exec())
412 Q_EMIT error(
i18n(
"<qt>Could not create destination directory <b>%1</b>.\nThe file has not been saved.</qt>", fullDestinationPath.
path()));
416 fullDestinationPath.
setPath(fullDestinationPath.
path() + model->destinationFile());
418 result = copyJob->
exec();
422 qCDebug(LIBKOMPAREDIFF2) <<
"Tempfilename : " << temp.
fileName();
423 qCDebug(LIBKOMPAREDIFF2) <<
"DestinationURL : " << m_info->destination;
426 int permissions = -1;
428 result = statJob->
exec();
433 result = copyJob->
exec();
434 qCDebug(LIBKOMPAREDIFF2) <<
"true or false?" << result;
440 Q_EMIT error(
i18n(
"<qt>Could not upload the temporary file to the destination location <b>%1</b>. The temporary file is still available under: <b>%2</b>. You can manually copy it to the right place.</qt>", m_info->destination.
url(), temp.
fileName()));
452 DifferenceListConstIterator diffIt = model->differences()->
constBegin();
453 DifferenceListConstIterator endIt = model->differences()->
constEnd();
455 for (; diffIt != endIt; ++diffIt)
457 (*diffIt)->setUnsaved(
false);
464bool KompareModelList::saveAll()
466 if (modelCount() == 0)
469 DiffModelListIterator it = m_models->
begin();
470 DiffModelListIterator
end = m_models->
end();
471 for (; it !=
end; ++it)
473 if (!saveDestination(*it))
480void KompareModelList::setEncoding(
const QString& encoding)
482 m_encoding = encoding;
485 m_textCodec = QTextCodec::codecForLocale();
489 qCDebug(LIBKOMPAREDIFF2) <<
"Encoding : " << encoding;
490 m_textCodec = QTextCodec::codecForName(encoding.
toUtf8());
491 qCDebug(LIBKOMPAREDIFF2) <<
"TextCodec: " << m_textCodec;
493 m_textCodec = QTextCodec::codecForLocale();
495 qCDebug(LIBKOMPAREDIFF2) <<
"TextCodec: " << m_textCodec;
498void KompareModelList::setReadWrite(
bool isReadWrite)
500 if (m_isReadWrite == isReadWrite)
503 m_isReadWrite = isReadWrite;
504 updateModelListActions();
507bool KompareModelList::isReadWrite()
const
509 return m_isReadWrite;
512void KompareModelList::slotDiffProcessFinished(
bool success)
517 if (parseDiffOutput(m_diffProcess->diffOutput()) != 0)
519 Q_EMIT error(
i18n(
"Could not parse diff output."));
523 if (m_info->mode != Kompare::ShowingDiff)
525 qCDebug(LIBKOMPAREDIFF2) <<
"Blend this crap please and do not give me any conflicts...";
526 blendOriginalIntoModelList(m_info->localSource);
528 updateModelListActions();
535 Q_EMIT error(
i18n(
"The files are identical."));
539 Q_EMIT error(m_diffProcess->stdErr());
543 m_diffProcess =
nullptr;
546void KompareModelList::slotDirectoryChanged(
const QString& )
549 qCDebug(LIBKOMPAREDIFF2) <<
"Yippie directories are being watched !!! :)";
553 m_diffProcess->start();
557void KompareModelList::slotFileChanged(
const QString& )
560 qCDebug(LIBKOMPAREDIFF2) <<
"Yippie files are being watched !!! :)";
564 m_diffProcess->start();
570 QString contents = fileContents;
577 const char split =
'\r';
579 const char split =
'\n';
587 if (contents.
length() > oldpos)
599 QFile file(fileName);
602 qCDebug(LIBKOMPAREDIFF2) <<
"Codec = " << m_textCodec;
604 m_textCodec = QTextCodec::codecForLocale();
605 std::unique_ptr<QTextDecoder> decoder(m_textCodec->makeDecoder());
608 while (!file.atEnd()) {
610 const auto len = file.read(buffer, 4096);
611 contents += decoder->toUnicode(buffer, len);
619bool KompareModelList::openDiff(
const QString& diffFile)
621 qCDebug(LIBKOMPAREDIFF2) <<
"Stupid :) Url = " << diffFile;
626 QString diff = readFile(diffFile);
632 if (parseDiffOutput(diff) != 0)
634 Q_EMIT error(
i18n(
"Could not parse diff output."));
638 updateModelListActions();
646bool KompareModelList::parseAndOpenDiff(
const QString& diff)
652 if (parseDiffOutput(diff) != 0)
654 Q_EMIT error(
i18n(
"Could not parse diff output."));
658 updateModelListActions();
665QString KompareModelList::recreateDiff()
const
669 DiffModelListConstIterator modelIt = m_models->
constBegin();
670 DiffModelListConstIterator mEnd = m_models->
constEnd();
672 for (; modelIt != mEnd; ++modelIt)
674 diff += (*modelIt)->recreateDiff();
681 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::saveDiff: ";
684 m_diffURL =
QUrl(url);
686 if (!m_diffTemp->
open()) {
687 Q_EMIT error(
i18n(
"Could not open a temporary file."));
690 m_diffTemp =
nullptr;
694 m_diffProcess =
new KompareProcess(diffSettings, Kompare::Custom, m_info->localSource, m_info->localDestination, directory);
695 m_diffProcess->setEncoding(m_encoding);
697 connect(m_diffProcess, &KompareProcess::diffHasFinished,
698 this, &KompareModelList::slotWriteDiffOutput);
701 m_diffProcess->start();
705void KompareModelList::slotWriteDiffOutput(
bool success)
707 qCDebug(LIBKOMPAREDIFF2) <<
"Success = " << success;
713 stream << m_diffProcess->diffOutput();
719 Q_EMIT error(
i18n(
"Could not write to the temporary file."));
732 m_diffTemp =
nullptr;
734 delete m_diffProcess;
735 m_diffProcess =
nullptr;
742 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::slotSelectionChanged( " << model <<
", " << diff <<
" )";
746 m_selectedModel =
const_cast<DiffModel*
>(model);
747 m_modelIndex = m_models->
indexOf(m_selectedModel);
748 qCDebug(LIBKOMPAREDIFF2) <<
"m_modelIndex = " << m_modelIndex;
749 m_selectedDifference =
const_cast<Difference*
>(diff);
751 m_selectedModel->setSelectedDifference(m_selectedDifference);
755 if (!setSelectedModel(m_selectedModel))
758 m_selectedModel = firstModel();
759 m_selectedDifference = m_selectedModel->firstDifference();
761 else if (!m_selectedModel->setSelectedDifference(m_selectedDifference))
764 m_selectedDifference = m_selectedModel->firstDifference();
767 Q_EMIT setSelection(model, diff);
768 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
770 updateModelListActions();
777 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::slotSelectionChanged( " << diff <<
" )";
780 m_selectedDifference =
const_cast<Difference*
>(diff);
782 if (!m_selectedModel->setSelectedDifference(m_selectedDifference))
785 m_selectedDifference = m_selectedModel->firstDifference();
788 Q_EMIT setSelection(diff);
789 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
791 updateModelListActions();
794void KompareModelList::slotPreviousModel()
796 if ((m_selectedModel = prevModel()) !=
nullptr)
798 m_selectedDifference = m_selectedModel->firstDifference();
802 m_selectedModel = firstModel();
803 m_selectedDifference = m_selectedModel->firstDifference();
806 Q_EMIT setSelection(m_selectedModel, m_selectedDifference);
807 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
808 updateModelListActions();
811void KompareModelList::slotNextModel()
813 if ((m_selectedModel = nextModel()) !=
nullptr)
815 m_selectedDifference = m_selectedModel->firstDifference();
819 m_selectedModel = lastModel();
820 m_selectedDifference = m_selectedModel->firstDifference();
823 Q_EMIT setSelection(m_selectedModel, m_selectedDifference);
824 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
825 updateModelListActions();
830 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::firstModel()";
832 qCDebug(LIBKOMPAREDIFF2) <<
"m_modelIndex = " << m_modelIndex;
834 m_selectedModel = m_models->
first();
836 return m_selectedModel;
841 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::lastModel()";
842 m_modelIndex = m_models->
count() - 1;
843 qCDebug(LIBKOMPAREDIFF2) <<
"m_modelIndex = " << m_modelIndex;
845 m_selectedModel = m_models->
last();
847 return m_selectedModel;
852 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::prevModel()";
853 if (m_modelIndex > 0 && --m_modelIndex < m_models->count())
855 qCDebug(LIBKOMPAREDIFF2) <<
"m_modelIndex = " << m_modelIndex;
856 m_selectedModel = (*m_models)[ m_modelIndex ];
860 m_selectedModel =
nullptr;
862 qCDebug(LIBKOMPAREDIFF2) <<
"m_modelIndex = " << m_modelIndex;
865 return m_selectedModel;
870 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::nextModel()";
871 if (++m_modelIndex < m_models->count())
873 qCDebug(LIBKOMPAREDIFF2) <<
"m_modelIndex = " << m_modelIndex;
874 m_selectedModel = (*m_models)[ m_modelIndex ];
878 m_selectedModel =
nullptr;
880 qCDebug(LIBKOMPAREDIFF2) <<
"m_modelIndex = " << m_modelIndex;
883 return m_selectedModel;
888 return m_actionCollection;
891void KompareModelList::slotPreviousDifference()
893 qCDebug(LIBKOMPAREDIFF2) <<
"slotPreviousDifference called";
894 if ((m_selectedDifference = m_selectedModel->prevDifference()) !=
nullptr)
896 Q_EMIT setSelection(m_selectedDifference);
897 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
898 updateModelListActions();
902 qCDebug(LIBKOMPAREDIFF2) <<
"**** no previous difference... ok lets find the previous model...";
904 if ((m_selectedModel = prevModel()) !=
nullptr)
906 m_selectedDifference = m_selectedModel->lastDifference();
908 Q_EMIT setSelection(m_selectedModel, m_selectedDifference);
909 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
910 updateModelListActions();
915 qCDebug(LIBKOMPAREDIFF2) <<
"**** !!! No previous model, ok backup plan activated...";
918 m_selectedModel = firstModel();
919 m_selectedDifference = m_selectedModel->firstDifference();
921 Q_EMIT setSelection(m_selectedModel, m_selectedDifference);
922 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
923 updateModelListActions();
926void KompareModelList::slotNextDifference()
928 qCDebug(LIBKOMPAREDIFF2) <<
"slotNextDifference called";
929 if ((m_selectedDifference = m_selectedModel->nextDifference()) !=
nullptr)
931 Q_EMIT setSelection(m_selectedDifference);
932 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
933 updateModelListActions();
937 qCDebug(LIBKOMPAREDIFF2) <<
"**** no next difference... ok lets find the next model...";
939 if ((m_selectedModel = nextModel()) !=
nullptr)
941 m_selectedDifference = m_selectedModel->firstDifference();
943 Q_EMIT setSelection(m_selectedModel, m_selectedDifference);
944 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
945 updateModelListActions();
949 qCDebug(LIBKOMPAREDIFF2) <<
"**** !!! No next model, ok backup plan activated...";
952 m_selectedModel = lastModel();
953 m_selectedDifference = m_selectedModel->lastDifference();
955 Q_EMIT setSelection(m_selectedModel, m_selectedDifference);
956 Q_EMIT setStatusBarModelInfo(findModel(m_selectedModel), m_selectedModel->findDifference(m_selectedDifference), modelCount(), differenceCount(), m_selectedModel->appliedCount());
957 updateModelListActions();
960void KompareModelList::slotApplyDifference(
bool apply)
962 m_selectedModel->applyDifference(apply);
963 Q_EMIT applyDifference(apply);
966void KompareModelList::slotApplyAllDifferences(
bool apply)
968 m_selectedModel->applyAllDifferences(apply);
969 Q_EMIT applyAllDifferences(apply);
972int KompareModelList::parseDiffOutput(
const QString& diff)
974 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::parseDiffOutput";
979 Parser* parser =
new Parser(
this);
980 bool malformed =
false;
981 m_models = parser->parse(diffLines, &malformed);
983 m_info->generator = parser->generator();
984 m_info->format = parser->format();
992 qCDebug(LIBKOMPAREDIFF2) <<
"Malformed diff";
993 Q_EMIT error(
i18n(
"The diff is malformed. Some lines could not be parsed and will not be displayed in the diff view."));
996 m_selectedModel = firstModel();
997 qCDebug(LIBKOMPAREDIFF2) <<
"Ok there are differences...";
998 m_selectedDifference = m_selectedModel->firstDifference();
999 Q_EMIT setStatusBarModelInfo(0, 0, modelCount(), differenceCount(), 0);
1004 qCDebug(LIBKOMPAREDIFF2) <<
"Now i'll be damned, there should be models here !!!";
1011bool KompareModelList::blendOriginalIntoModelList(
const QString& localURL)
1013 qCDebug(LIBKOMPAREDIFF2) <<
"Hurrah we are blending...";
1016 bool result =
false;
1023 qCDebug(LIBKOMPAREDIFF2) <<
"Blend Dir";
1025 DiffModelListIterator modelIt = m_models->
begin();
1026 DiffModelListIterator mEnd = m_models->
end();
1027 for (; modelIt != mEnd; ++modelIt)
1030 qCDebug(LIBKOMPAREDIFF2) <<
"Model : " << model;
1031 QString filename = model->source();
1037 qCDebug(LIBKOMPAREDIFF2) <<
"Reading from: " << filename;
1038 fileContents = readFile(filename);
1039 result = blendFile(model, fileContents);
1043 qCDebug(LIBKOMPAREDIFF2) <<
"File " << filename <<
" does not exist !";
1044 qCDebug(LIBKOMPAREDIFF2) <<
"Assume empty file !";
1046 result = blendFile(model, fileContents);
1049 qCDebug(LIBKOMPAREDIFF2) <<
"End of Blend Dir";
1051 else if (fi.isFile())
1053 qCDebug(LIBKOMPAREDIFF2) <<
"Blend File";
1054 qCDebug(LIBKOMPAREDIFF2) <<
"Reading from: " << localURL;
1055 fileContents = readFile(localURL);
1057 result = blendFile((*m_models)[ 0 ], fileContents);
1058 qCDebug(LIBKOMPAREDIFF2) <<
"End of Blend File";
1064bool KompareModelList::blendFile(
DiffModel* model,
const QString& fileContents)
1068 qCDebug(LIBKOMPAREDIFF2) <<
"**** model is null :(";
1072 model->setBlended(
true);
1074 int srcLineNo = 1, destLineNo = 1;
1080 qCDebug(LIBKOMPAREDIFF2) <<
"Hunks in hunklist: " << hunks->
count();
1081 DiffHunkListIterator hunkIt = hunks->
begin();
1091 for (; hunkIt != hunks->
end(); ++hunkIt)
1095 if (srcLineNo < hunk->sourceLineNumber())
1097 newHunk =
new DiffHunk(srcLineNo, destLineNo,
QString(), DiffHunk::AddedByBlend);
1099 hunkIt = ++hunks->
insert(hunkIt, newHunk);
1101 newDiff =
new Difference(srcLineNo, destLineNo,
1102 Difference::Unchanged);
1104 newHunk->add(newDiff);
1106 while (srcLineNo < hunk->sourceLineNumber() && linesIt != lEnd)
1108 newDiff->addSourceLine(*linesIt);
1109 newDiff->addDestinationLine(*linesIt);
1117 int size = hunk->sourceLineCount();
1126 destLineNo += hunk->destinationLineCount();
1129 if (linesIt != lEnd)
1131 newHunk =
new DiffHunk(srcLineNo, destLineNo,
QString(), DiffHunk::AddedByBlend);
1133 model->addHunk(newHunk);
1135 newDiff =
new Difference(srcLineNo, destLineNo, Difference::Unchanged);
1137 newHunk->add(newDiff);
1139 while (linesIt != lEnd)
1141 newDiff->addSourceLine(*linesIt);
1142 newDiff->addDestinationLine(*linesIt);
1148 DifferenceListIterator diffIt = hunkDiffList.
begin();
1149 DifferenceListIterator dEnd = hunkDiffList.
end();
1150 qCDebug(LIBKOMPAREDIFF2) <<
"Number of differences in hunkDiffList = " << diffList.count();
1152 DifferenceListIterator tempIt;
1155 for (; diffIt != dEnd; ++diffIt)
1158 qCDebug(LIBKOMPAREDIFF2) <<
"*(Diff it) = " << diff;
1161 if (srcLineNo < diff->sourceLineNumber())
1163 newDiff =
new Difference(srcLineNo, destLineNo,
1164 Difference::Unchanged | Difference::AddedByBlend);
1165 newHunk->add(newDiff);
1166 while (srcLineNo < diff->sourceLineNumber() && linesIt != lEnd)
1169 newDiff->addSourceLine(*linesIt);
1170 newDiff->addDestinationLine(*linesIt);
1177 switch (diff->type())
1179 case Difference::Unchanged:
1180 qCDebug(LIBKOMPAREDIFF2) <<
"Unchanged";
1181 for (
int i = 0; i < diff->sourceLineCount(); ++i)
1183 if (linesIt != lEnd && *linesIt != diff->sourceLineAt(i)->string())
1185 qCDebug(LIBKOMPAREDIFF2) <<
"Conflict: SourceLine = " << srcLineNo <<
": " << *linesIt;
1186 qCDebug(LIBKOMPAREDIFF2) <<
"Conflict: DiffLine = " << diff->sourceLineNumber() + i <<
": " << diff->sourceLineAt(i)->string();
1189 diff->sourceLineAt(i)->setConflictString(*linesIt);
1190 diff->setConflict(
true);
1201 diffList.remove(tempIt);
1205 case Difference::Change:
1206 qCDebug(LIBKOMPAREDIFF2) <<
"Change";
1210 for (
int i = 0; i < diff->sourceLineCount(); ++i)
1212 if (linesIt != lEnd && *linesIt != diff->sourceLineAt(i)->string())
1214 qCDebug(LIBKOMPAREDIFF2) <<
"Conflict: SourceLine = " << srcLineNo <<
": " << *linesIt;
1215 qCDebug(LIBKOMPAREDIFF2) <<
"Conflict: DiffLine = " << diff->sourceLineNumber() + i <<
": " << diff->sourceLineAt(i)->string();
1218 diff->sourceLineAt(i)->setConflictString(*linesIt);
1219 diff->setConflict(
true);
1226 destLineNo += diff->destinationLineCount();
1230 diffList.remove(tempIt);
1232 newModel->addDiff(diff);
1235 case Difference::Insert:
1236 qCDebug(LIBKOMPAREDIFF2) <<
"Insert";
1237 destLineNo += diff->destinationLineCount();
1240 diffList.remove(tempIt);
1242 newModel->addDiff(diff);
1244 case Difference::Delete:
1245 qCDebug(LIBKOMPAREDIFF2) <<
"Delete";
1246 qCDebug(LIBKOMPAREDIFF2) <<
"Number of lines in Delete: " << diff->sourceLineCount();
1247 for (
int i = 0; i < diff->sourceLineCount(); ++i)
1249 if (linesIt != lEnd && *linesIt != diff->sourceLineAt(i)->string())
1251 qCDebug(LIBKOMPAREDIFF2) <<
"Conflict: SourceLine = " << srcLineNo <<
": " << *linesIt;
1252 qCDebug(LIBKOMPAREDIFF2) <<
"Conflict: DiffLine = " << diff->sourceLineNumber() + i <<
": " << diff->sourceLineAt(i)->string();
1255 diff->sourceLineAt(i)->setConflictString(*linesIt);
1256 diff->setConflict(
true);
1267 diffList.remove(tempIt);
1269 newModel->addDiff(diff);
1272 qCDebug(LIBKOMPAREDIFF2) <<
"****, some diff type we do not know about ???";
1289m_selectedModel = firstModel();
1291m_selectedDifference = m_selectedModel->firstDifference();
1296void KompareModelList::show()
1298 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::Show Number of models = " << m_models->
count();
1299 Q_EMIT modelsChanged(m_models);
1300 Q_EMIT setSelection(m_selectedModel, m_selectedDifference);
1303void KompareModelList::clear()
1308 Q_EMIT modelsChanged(m_models);
1311void KompareModelList::refresh()
1314 compare(m_info->mode);
1317void KompareModelList::swap()
1320 if (m_info->mode == Kompare::ComparingFiles)
1321 compare(m_info->mode);
1322 else if (m_info->mode == Kompare::ComparingDirs)
1323 compare(m_info->mode);
1326bool KompareModelList::hasUnsavedChanges()
const
1328 if (modelCount() == 0)
1331 DiffModelListConstIterator modelIt = m_models->
constBegin();
1332 DiffModelListConstIterator endIt = m_models->
constEnd();
1334 for (; modelIt != endIt; ++modelIt)
1336 if ((*modelIt)->hasUnsavedChanges())
1342int KompareModelList::modelCount()
const
1344 return m_models ? m_models->
count() : 0;
1347int KompareModelList::differenceCount()
const
1349 return m_selectedModel ? m_selectedModel->differenceCount() : -1;
1352int KompareModelList::appliedCount()
const
1354 return m_selectedModel ? m_selectedModel->appliedCount() : -1;
1357void KompareModelList::slotKompareInfo(
struct Kompare::Info* info)
1362bool KompareModelList::setSelectedModel(
DiffModel* model)
1364 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::setSelectedModel( " << model <<
" )";
1366 if (model != m_selectedModel)
1370 qCDebug(LIBKOMPAREDIFF2) <<
"m_selectedModel (was) = " << m_selectedModel;
1371 m_modelIndex = m_models->
indexOf(model);
1372 qCDebug(LIBKOMPAREDIFF2) <<
"m_selectedModel (is) = " << m_selectedModel;
1373 m_selectedModel = model;
1376 updateModelListActions();
1381void KompareModelList::updateModelListActions()
1383 if (m_models && m_selectedModel && m_selectedDifference)
1385 if (m_isReadWrite && m_save)
1387 if (m_selectedModel->appliedCount() != m_selectedModel->differenceCount())
1392 if (m_selectedModel->appliedCount() != 0)
1397 m_applyDifference->
setEnabled(m_selectedDifference->applied() ==
false);
1398 m_unApplyDifference->
setEnabled(m_selectedDifference->applied() ==
true);
1399 m_save->
setEnabled(m_selectedModel->hasUnsavedChanges());
1412 m_previousDifference->
setEnabled(hasPrevDiff());
1432bool KompareModelList::hasPrevModel()
const
1434 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::hasPrevModel()";
1436 if (m_modelIndex > 0)
1447bool KompareModelList::hasNextModel()
const
1449 qCDebug(LIBKOMPAREDIFF2) <<
"KompareModelList::hasNextModel()";
1451 if (m_modelIndex < (m_models->
count() - 1))
1461bool KompareModelList::hasPrevDiff()
const
1464 int index = m_selectedModel->diffIndex();
1483bool KompareModelList::hasNextDiff()
const
1486 int index = m_selectedModel->diffIndex();
1488 if (index < (m_selectedModel->differenceCount() - 1))
1505void KompareModelList::slotActionApplyDifference()
1507 if (!m_selectedDifference->applied())
1508 slotApplyDifference(
true);
1509 slotNextDifference();
1510 updateModelListActions();
1513void KompareModelList::slotActionUnApplyDifference()
1515 if (m_selectedDifference->applied())
1516 slotApplyDifference(
false);
1517 slotPreviousDifference();
1518 updateModelListActions();
1521void KompareModelList::slotActionApplyAllDifferences()
1523 slotApplyAllDifferences(
true);
1524 updateModelListActions();
1527void KompareModelList::slotActionUnapplyAllDifferences()
1529 slotApplyAllDifferences(
false);
1530 updateModelListActions();
1533#include "moc_komparemodellist.cpp"
A model describing the differences between two files.
void slotSaveDestination()
Save the currently selected destination in a multi-file diff, or the single destination if a single f...
KompareModelList(DiffSettings *diffSettings, QWidget *widgetForKIO, QObject *parent, const char *name=nullptr, bool supportReadWrite=true)
QAction * addAction(const QString &name, const QObject *receiver=nullptr, const char *member=nullptr)
static void setDefaultShortcut(QAction *action, const QKeySequence &shortcut)
const UDSEntry & statResult() const
long long numberValue(uint field, long long defaultValue=0) const
Q_SCRIPTABLE CaptureState status()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
KIOCORE_EXPORT MkdirJob * mkdir(const QUrl &url, int permissions=-1)
KIOCORE_EXPORT StatJob * stat(const QUrl &url, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT FileCopyJob * file_copy(const QUrl &src, const QUrl &dest, int permissions=-1, JobFlags flags=DefaultFlags)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
QAction * save(const QObject *recvr, const char *slot, QObject *parent)
const QList< QKeySequence > & end()
QString name(StandardShortcut id)
void setIcon(const QIcon &icon)
void setText(const QString &text)
QString filePath(const QString &fileName) const const
virtual void close() override
FileError error() const const
QIcon fromTheme(const QString &name)
void append(QList< T > &&value)
const_iterator constBegin() const const
const_iterator constEnd() const const
bool contains(const AT &value) const const
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
iterator insert(const_iterator before, parameter_type value)
QMimeType mimeTypeForData(QIODevice *device) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
QObject * sender() const const
QProcess::ExitStatus exitStatus() const const
int compare(QLatin1StringView s1, const QString &s2, Qt::CaseSensitivity cs)
qsizetype indexOf(QChar ch, qsizetype from, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const
QString & remove(QChar ch, Qt::CaseSensitivity cs)
QString right(qsizetype n) const const
qsizetype size() const const
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QByteArray toUtf8() const const
void truncate(qsizetype position)
QString join(QChar separator) const const
virtual QString fileName() const const override
void setAutoRemove(bool b)
QUrl fromLocalFile(const QString &localFile)
QString path(ComponentFormattingOptions options) const const
void setPath(const QString &path, ParsingMode mode)
QString url(FormattingOptions options) const const