Commit ddcf1742 authored by Waqar Ahmed's avatar Waqar Ahmed Committed by Christoph Cullmann
Browse files

Refactor temporary file opening code



This was duplicated everywhere so I just moved it to one function.
Signed-off-by: Waqar Ahmed's avatarWaqar Ahmed <waqar.17a@gmail.com>
parent 5013d168
......@@ -31,6 +31,7 @@
#include <KLocalizedString>
#include <KMessageBox>
#include <QPointer>
#include <KTextEditor/ConfigInterface>
#include <KTextEditor/Editor>
......@@ -147,7 +148,7 @@ KTextEditor::MainWindow *GitWidget::mainWindow()
std::vector<GitWidget::TempFileViewPair> *GitWidget::tempFilesVector()
{
return &m_filesOpenAtHEAD;
return &m_tempFiles;
}
void GitWidget::getStatus(bool untracked, bool submodules)
......@@ -246,43 +247,11 @@ void GitWidget::openAtHEAD(const QString &file)
disconnect(&git, &QProcess::finished, nullptr, nullptr);
connect(&git, &QProcess::finished, this, [this, file](int exitCode, QProcess::ExitStatus es) {
// sever connection
disconnect(&git, &QProcess::finished, nullptr, nullptr);
if (es != QProcess::NormalExit || exitCode != 0) {
sendMessage(i18n("Failed to open file at HEAD. Error:\n%1", QString::fromUtf8(git.readAllStandardError())), true);
} else {
std::unique_ptr<QTemporaryFile> f(new QTemporaryFile);
QFileInfo fi(file);
f->setFileTemplate(QString(QStringLiteral("XXXXXX - (HEAD) - %1").arg(fi.fileName())));
if (!f->open()) {
return;
}
auto xx = git.readAllStandardOutput();
f->write(xx);
f->flush();
const QUrl tempFileUrl(QUrl::fromLocalFile(f->fileName()));
auto v = m_mainWin->openUrl(tempFileUrl);
if (!v || !v->document()) {
return;
}
TempFileViewPair tfvp = std::make_pair(std::move(f), v);
m_filesOpenAtHEAD.push_back(std::move(tfvp));
// close temp on document close
auto clearTemp = [this](KTextEditor::Document *document) {
m_filesOpenAtHEAD.erase(std::remove_if(m_filesOpenAtHEAD.begin(),
m_filesOpenAtHEAD.end(),
[document](const GitWidget::TempFileViewPair &tf) {
if (tf.second && tf.second->document() == document) {
return true;
}
return false;
}),
m_filesOpenAtHEAD.end());
};
connect(v->document(), &KTextEditor::Document::aboutToClose, this, clearTemp);
openTempFile(QFileInfo(file).fileName(), QStringLiteral("XXXXXX - (HEAD) - %1"));
}
});
......@@ -306,42 +275,11 @@ void GitWidget::showDiff(const QString &file, bool staged)
disconnect(&git, &QProcess::finished, nullptr, nullptr);
connect(&git, &QProcess::finished, this, [this, file](int exitCode, QProcess::ExitStatus es) {
// sever connection
disconnect(&git, &QProcess::finished, nullptr, nullptr);
if (es != QProcess::NormalExit || exitCode != 0) {
sendMessage(i18n("Failed to get Diff of file. Error:\n%1", QString::fromUtf8(git.readAllStandardError())), true);
} else {
std::unique_ptr<QTemporaryFile> f(new QTemporaryFile);
QFileInfo fi(file);
f->setFileTemplate(QString(QStringLiteral("XXXXXX %1.diff").arg(fi.fileName())));
if (!f->open()) {
return;
}
f->write(git.readAllStandardOutput());
f->flush();
const QUrl tempFileUrl(QUrl::fromLocalFile(f->fileName()));
auto v = m_mainWin->openUrl(tempFileUrl);
if (!v || !v->document()) {
return;
}
TempFileViewPair tfvp = std::make_pair(std::move(f), v);
m_filesOpenAtHEAD.push_back(std::move(tfvp));
// close temp on document close
auto clearTemp = [this](KTextEditor::Document *document) {
m_filesOpenAtHEAD.erase(std::remove_if(m_filesOpenAtHEAD.begin(),
m_filesOpenAtHEAD.end(),
[document](const GitWidget::TempFileViewPair &tf) {
if (tf.second && tf.second->document() == document) {
return true;
}
return false;
}),
m_filesOpenAtHEAD.end());
};
connect(v->document(), &KTextEditor::Document::aboutToClose, this, clearTemp);
openTempFile(QFileInfo(file).fileName(), QStringLiteral("XXXXXX %1.diff"));
}
});
......@@ -545,6 +483,46 @@ QMenu *GitWidget::stashMenu()
return menu;
}
void GitWidget::clearTempFile(KTextEditor::Document *document)
{
m_tempFiles.erase(std::remove_if(m_tempFiles.begin(),
m_tempFiles.end(),
[document](const GitWidget::TempFileViewPair &tf) {
if (tf.second && tf.second->document() == document) {
return true;
}
return false;
}),
m_tempFiles.end());
}
void GitWidget::openTempFile(const QString &file, const QString &templatString)
{
std::unique_ptr<QTemporaryFile> f(new QTemporaryFile);
if (!templatString.isEmpty() && !file.isEmpty()) {
f->setFileTemplate(templatString.arg(file));
} else if (!templatString.isEmpty() && file.isEmpty()) {
f->setFileTemplate(templatString);
}
if (!f->open()) {
qWarning() << "Gitwidget: Failed to open temp file";
return;
}
qWarning() << "Reading output";
f->write(git.readAllStandardOutput());
f->flush();
const QUrl tempFileUrl(QUrl::fromLocalFile(f->fileName()));
QPointer<KTextEditor::View> v = m_mainWin->openUrl(tempFileUrl);
if (!v || !v->document()) {
return;
}
TempFileViewPair tfvp = std::make_pair(std::move(f), v);
m_tempFiles.push_back(std::move(tfvp));
connect(v->document(), &KTextEditor::Document::aboutToClose, this, &GitWidget::clearTempFile);
}
static KMessageBox::ButtonCode confirm(GitWidget *_this, const QString &text)
{
return KMessageBox::questionYesNo(_this, text, {}, KStandardGuiItem::yes(), KStandardGuiItem::no(), {}, KMessageBox::Dangerous);
......
......@@ -28,6 +28,7 @@ namespace KTextEditor
{
class MainWindow;
class View;
class Document;
}
class GitWidget : public QWidget
......@@ -42,8 +43,9 @@ public:
QProcess *gitprocess();
KTextEditor::MainWindow *mainWindow();
using TempFileViewPair = std::pair<std::unique_ptr<QTemporaryFile>, KTextEditor::View *>;
using TempFileViewPair = std::pair<std::unique_ptr<QTemporaryFile>, QPointer<KTextEditor::View>>;
std::vector<TempFileViewPair> *tempFilesVector();
void openTempFile(const QString &file, const QString &templatString);
private:
QToolButton *m_menuBtn;
......@@ -58,7 +60,7 @@ private:
QString m_commitMessage;
KTextEditor::MainWindow *m_mainWin;
QMenu *m_gitMenu;
std::vector<TempFileViewPair> m_filesOpenAtHEAD;
std::vector<TempFileViewPair> m_tempFiles;
KateProjectPluginView *m_pluginView;
void buildMenu();
......@@ -78,9 +80,13 @@ private:
void treeViewContextMenuEvent(QContextMenuEvent *e);
void selectedContextMenu(QContextMenuEvent *e);
Q_SLOT void gitStatusReady(int exit, QProcess::ExitStatus);
Q_SLOT void parseStatusReady();
Q_SLOT void opencommitChangesDialog();
public Q_SLOTS:
void clearTempFile(KTextEditor::Document *document);
private Q_SLOTS:
void gitStatusReady(int exit, QProcess::ExitStatus);
void parseStatusReady();
void opencommitChangesDialog();
// signals
public:
......
......@@ -336,7 +336,7 @@ void StashDialog::stash(bool keepIndex, bool includeUntracked)
}
});
git->setArguments(args);
git->start();
git->start(QProcess::ReadOnly);
}
void StashDialog::getStashList()
......@@ -347,7 +347,7 @@ void StashDialog::getStashList()
}
git->setArguments({QStringLiteral("stash"), QStringLiteral("list")});
git->start();
git->start(QProcess::ReadOnly);
QList<QByteArray> stashList;
if (git->waitForStarted() && git->waitForFinished(-1)) {
......@@ -414,7 +414,7 @@ void StashDialog::popStash(const QByteArray &index, const QString &command)
}
});
git->setArguments(args);
git->start();
git->start(QProcess::ReadOnly);
}
void StashDialog::applyStash(const QByteArray &index)
......@@ -443,46 +443,16 @@ void StashDialog::showStash(const QByteArray &index)
QStringList args{QStringLiteral("stash"), QStringLiteral("show"), QStringLiteral("-p"), QString::fromUtf8(index)};
connect(git, &QProcess::finished, gitWidget, [gitWidget](int exitCode, QProcess::ExitStatus es) {
// sever connection
disconnect(gitWidget->gitprocess(), &QProcess::finished, nullptr, nullptr);
if (es != QProcess::NormalExit || exitCode != 0) {
gitWidget->sendMessage(i18n("Failed to get Diff of file. Error:\n%1", QString::fromUtf8(gitWidget->gitprocess()->readAllStandardError())), true);
gitWidget->sendMessage(i18n("Show stash failed. Error:\n%1", QString::fromUtf8(gitWidget->gitprocess()->readAllStandardError())), true);
} else {
std::unique_ptr<QTemporaryFile> f(new QTemporaryFile);
f->setFileTemplate(QString(QStringLiteral("XXXXXX.diff")));
if (!f->open()) {
return;
}
f->write(gitWidget->gitprocess()->readAllStandardOutput());
f->flush();
const QUrl tempFileUrl(QUrl::fromLocalFile(f->fileName()));
auto v = gitWidget->mainWindow()->openUrl(tempFileUrl);
if (!v || !v->document()) {
return;
}
std::pair<std::unique_ptr<QTemporaryFile>, KTextEditor::View *> tf = std::make_pair(std::move(f), v);
gitWidget->tempFilesVector()->push_back(std::move(tf));
// close temp on document close
auto clearTemp = [gitWidget](KTextEditor::Document *document) {
auto tempFiles = gitWidget->tempFilesVector();
tempFiles->erase(std::remove_if(tempFiles->begin(),
tempFiles->end(),
[document](const GitWidget::TempFileViewPair &tf) {
if (tf.second->document() == document) {
return true;
}
return false;
}),
tempFiles->end());
};
connect(v->document(), &KTextEditor::Document::aboutToClose, gitWidget, clearTemp);
gitWidget->openTempFile(QString(), QStringLiteral("XXXXXX.diff"));
}
});
git->setArguments(args);
git->start();
git->start(QProcess::ReadOnly);
}
void StashDialog::updateViewGeometry()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment