Commit 2489ddc5 authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

fix --tempfile handling

fixes the handling both for Kate & KWrite

simplifies it to just deleting the files on application exit

this avoids any complex bookkeeping and ensures we really just
delete 1:1 the files passed to the command line that got the

--tempfile

option

BUG: 442979
parent 5845bfde
......@@ -136,6 +136,13 @@ KateApp::~KateApp()
// mainwindow itself calls KateApp::removeMainWindow(this)
delete m_mainWindows[0];
}
/**
* cleanup --tempfile files
*/
for (const auto &file : std::as_const(m_tempFilesToDelete)) {
QFile::remove(file);
}
}
KateApp *KateApp::self()
......@@ -392,6 +399,13 @@ bool KateApp::isOnActivity(const QString &activity)
KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile, bool activateView, KTextEditor::Cursor c)
{
// temporary file handling
// ensure we will delete the local file we opened via --tempfile at end of program
if (isTempFile && !url.isEmpty() && url.isLocalFile() && QFile::exists(url.toLocalFile())) {
// register for deletion on program exit
m_tempFilesToDelete.push_back(url.toLocalFile());
}
KateMainWindow *mainWindow = activeKateMainWindow();
if (!mainWindow) {
......
......@@ -466,6 +466,11 @@ private:
*/
KUserFeedback::Provider m_userFeedbackProvider;
#endif
/**
* files opened via --tempfile, must be deleted on program exit
*/
QStringList m_tempFilesToDelete;
};
#endif
......@@ -131,18 +131,17 @@ KTextEditor::Document *KateDocManager::findDocument(const QUrl &url) const
return nullptr;
}
std::vector<KTextEditor::Document *>
KateDocManager::openUrls(const QList<QUrl> &urls, const QString &encoding, bool isTempFile, const KateDocumentInfo &docInfo)
std::vector<KTextEditor::Document *> KateDocManager::openUrls(const QList<QUrl> &urls, const QString &encoding, const KateDocumentInfo &docInfo)
{
std::vector<KTextEditor::Document *> docs;
docs.reserve(urls.size());
for (const QUrl &url : urls) {
docs.push_back(openUrl(url, encoding, isTempFile, docInfo));
docs.push_back(openUrl(url, encoding, docInfo));
}
return docs;
}
KTextEditor::Document *KateDocManager::openUrl(const QUrl &url, const QString &encoding, bool isTempFile, const KateDocumentInfo &docInfo)
KTextEditor::Document *KateDocManager::openUrl(const QUrl &url, const QString &encoding, const KateDocumentInfo &docInfo)
{
// special handling: if only one unmodified empty buffer in the list,
// keep this buffer in mind to close it after opening the new url
......@@ -180,17 +179,6 @@ KTextEditor::Document *KateDocManager::openUrl(const QUrl &url, const QString &e
}
}
//
// if needed, register as temporary file
//
if (isTempFile && u.isLocalFile()) {
QFileInfo fi(u.toLocalFile());
if (fi.exists()) {
m_tempFiles[doc] = std::make_pair(u, fi.lastModified());
qCDebug(LOG_KATE) << "temporary file will be deleted after use unless modified: " << u;
}
}
return doc;
}
......@@ -212,28 +200,6 @@ bool KateDocManager::closeDocuments(const QList<KTextEditor::Document *> documen
break;
}
if (closeUrl) {
auto it = m_tempFiles.find(doc);
if (it != m_tempFiles.end()) {
const auto [url, lastMod] = it->second;
QFileInfo fi(url.toLocalFile());
if (fi.lastModified() <= lastMod
|| KMessageBox::questionYesNo(KateApp::self()->activeKateMainWindow(),
i18n("The supposedly temporary file %1 has been modified. "
"Do you want to delete it anyway?",
url.url(QUrl::PreferLocalFile)),
i18n("Delete File?"))
== KMessageBox::Yes) {
KIO::del(url, KIO::HideProgressInfo);
qCDebug(LOG_KATE) << "Deleted temporary file " << url;
m_tempFiles.erase(it);
} else {
m_tempFiles.erase(it);
qCDebug(LOG_KATE) << "The supposedly temporary file " << url.url() << " have been modified since loaded, and has not been deleted.";
}
}
}
KateApp::self()->emitDocumentClosed(QString::number(reinterpret_cast<qptrdiff>(doc)));
// document will be deleted, soon
......
......@@ -58,11 +58,10 @@ public:
return m_docList;
}
KTextEditor::Document *
openUrl(const QUrl &, const QString &encoding = QString(), bool isTempFile = false, const KateDocumentInfo &docInfo = KateDocumentInfo());
KTextEditor::Document *openUrl(const QUrl &, const QString &encoding = QString(), const KateDocumentInfo &docInfo = KateDocumentInfo());
std::vector<KTextEditor::Document *>
openUrls(const QList<QUrl> &, const QString &encoding = QString(), bool isTempFile = false, const KateDocumentInfo &docInfo = KateDocumentInfo());
openUrls(const QList<QUrl> &, const QString &encoding = QString(), const KateDocumentInfo &docInfo = KateDocumentInfo());
bool closeDocument(KTextEditor::Document *, bool closeUrl = true);
bool closeDocuments(const QList<KTextEditor::Document *> documents, bool closeUrl = true);
......@@ -178,9 +177,6 @@ private:
bool m_saveMetaInfos;
int m_daysMetaInfos;
typedef std::pair<QUrl, QDateTime> TPair;
std::unordered_map<KTextEditor::Document *, TPair> m_tempFiles;
private Q_SLOTS:
void documentOpened();
};
......
......@@ -669,7 +669,7 @@ void KateMainWindow::slotFileClose()
void KateMainWindow::slotOpenDocument(const QUrl &url)
{
m_viewManager->openUrl(url, QString(), true, false);
m_viewManager->openUrl(url, QString(), true);
}
void KateMainWindow::readOptions()
......@@ -1235,7 +1235,8 @@ void KateMainWindow::queueModifiedOnDisc(KTextEditor::Document *doc)
s_modOnHdDialog = new KateMwModOnHdDialog(list, this);
m_modignore = true;
connect(s_modOnHdDialog, &KateMwModOnHdDialog::requestOpenDiffDocument, this, [this](const QUrl &url) {
viewManager()->openUrl(url, QString(), true, true);
// use open with isTempFile == true
KateApp::self()->openUrl(url, QString(), true);
});
// Someone modified a doc outside and now we are here
......
......@@ -289,7 +289,7 @@ void KateViewManager::slotDocumentOpen()
// activate view of last opened document
KateDocumentInfo docInfo;
docInfo.openedByUser = true;
if (KTextEditor::Document *lastID = openUrls(urls, QString(), false, docInfo)) {
if (KTextEditor::Document *lastID = openUrls(urls, QString(), docInfo)) {
activateView(lastID);
}
}
......@@ -323,11 +323,12 @@ void KateViewManager::slotDocumentClose()
}
}
KTextEditor::Document *KateViewManager::openUrl(const QUrl &url, const QString &encoding, bool activate, bool isTempFile, const KateDocumentInfo &docInfo)
KTextEditor::Document *
KateViewManager::openUrl(const QUrl &url, const QString &encoding, bool activate, bool ignoreForRecentFiles, const KateDocumentInfo &docInfo)
{
KTextEditor::Document *doc = KateApp::self()->documentManager()->openUrl(url, encoding, isTempFile, docInfo);
KTextEditor::Document *doc = KateApp::self()->documentManager()->openUrl(url, encoding, docInfo);
if (!isTempFile) {
if (!ignoreForRecentFiles) {
m_mainWindow->addRecentOpenedFile(doc->url());
}
......@@ -338,16 +339,9 @@ KTextEditor::Document *KateViewManager::openUrl(const QUrl &url, const QString &
return doc;
}
KTextEditor::Document *KateViewManager::openUrls(const QList<QUrl> &urls, const QString &encoding, bool isTempFile, const KateDocumentInfo &docInfo)
KTextEditor::Document *KateViewManager::openUrls(const QList<QUrl> &urls, const QString &encoding, const KateDocumentInfo &docInfo)
{
const std::vector<KTextEditor::Document *> docs = KateApp::self()->documentManager()->openUrls(urls, encoding, isTempFile, docInfo);
if (!isTempFile) {
for (const KTextEditor::Document *doc : docs) {
m_mainWindow->addRecentOpenedFile(doc->url());
}
}
const std::vector<KTextEditor::Document *> docs = KateApp::self()->documentManager()->openUrls(urls, encoding, docInfo);
return docs.empty() ? nullptr : docs.back();
}
......
......@@ -59,11 +59,13 @@ public:
/* restore it */
void restoreViewConfiguration(const KConfigGroup &group);
KTextEditor::Document *
openUrl(const QUrl &url, const QString &encoding, bool activate = true, bool isTempFile = false, const KateDocumentInfo &docInfo = KateDocumentInfo());
KTextEditor::Document *openUrl(const QUrl &url,
const QString &encoding,
bool activate = true,
bool ignoreForRecentFiles = false,
const KateDocumentInfo &docInfo = KateDocumentInfo());
KTextEditor::Document *
openUrls(const QList<QUrl> &url, const QString &encoding, bool isTempFile = false, const KateDocumentInfo &docInfo = KateDocumentInfo());
KTextEditor::Document *openUrls(const QList<QUrl> &url, const QString &encoding, const KateDocumentInfo &docInfo = KateDocumentInfo());
KTextEditor::View *openUrlWithView(const QUrl &url, const QString &encoding);
......
Supports Markdown
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