Commit 745617da authored by Christoph Cullmann's avatar Christoph Cullmann 🐮
Browse files

delete temp files earlier again

cleanup the token handling for blocking instances
delete temp files inside that handling early

e.g.

kate -b --tempfile test.txt

test.txt will be gone when the kate client instance
terminates
parent 2489ddc5
Pipeline #161634 passed with stage
in 5 minutes and 25 seconds
......@@ -136,13 +136,6 @@ 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()
......@@ -378,11 +371,6 @@ KateStashManager *KateApp::stashManager()
return &m_stashManager;
}
bool KateApp::openUrl(const QUrl &url, const QString &encoding, bool isTempFile)
{
return openDocUrl(url, encoding, isTempFile);
}
bool KateApp::isOnActivity(const QString &activity)
{
for (const auto window : qAsConst(m_mainWindows)) {
......@@ -401,10 +389,8 @@ KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encod
{
// 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());
}
// we can only do this properly for local files
isTempFile = (isTempFile && !url.isEmpty() && url.isLocalFile() && QFile::exists(url.toLocalFile()));
KateMainWindow *mainWindow = activeKateMainWindow();
......@@ -433,9 +419,37 @@ KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encod
KMessageBox::sorry(mainWindow, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", url.url()));
}
// document was successfully opened, ensure we will handle destroy properly
if (doc) {
// connect to slot & register the temp file handling if needed
connect(doc, &QObject::destroyed, this, &KateApp::openDocUrlDocumentDestroyed);
if (isTempFile) {
m_tempFilesToDelete[doc].push_back(url.toLocalFile());
}
}
// unable to open document, directly dispose of the temporary file
else if (isTempFile) {
QFile::remove(url.toLocalFile());
}
return doc;
}
void KateApp::openDocUrlDocumentDestroyed(QObject *document)
{
// do we need to kill the temporary files for this document?
if (const auto tempFilesIt = m_tempFilesToDelete.find(document); tempFilesIt != m_tempFilesToDelete.end()) {
for (const auto &file : tempFilesIt.value()) {
QFile::remove(file);
}
m_tempFilesToDelete.erase(tempFilesIt);
}
// emit token signal to unblock remove blocking instances
m_adaptor.emitDocumentClosed(QString::number(reinterpret_cast<qptrdiff>(document)));
}
KTextEditor::Cursor KateApp::cursorFromArgs()
{
int line = -1;
......@@ -571,11 +585,6 @@ KateMainWindow *KateApp::mainWindow(int n)
return nullptr;
}
void KateApp::emitDocumentClosed(const QString &token)
{
m_adaptor.emitDocumentClosed(token);
}
KTextEditor::Plugin *KateApp::plugin(const QString &name)
{
return m_pluginManager.plugin(name);
......
......@@ -227,16 +227,6 @@ public:
* some stuff for the dcop API
*/
public:
/**
* open url with given encoding
* used by kate if --use given
* @param url filename
* @param encoding encoding name
* @param isTempFile whether the file is temporary
* @return success
*/
bool openUrl(const QUrl &url, const QString &encoding, bool isTempFile);
/**
* checks if the current instance is in a given activity
* @param activity activity to check
......@@ -244,11 +234,19 @@ public:
*/
bool isOnActivity(const QString &activity);
/**
* open url with given encoding
* used by kate if --use given
* @param url filename
* @param encoding encoding name
* @param isTempFile whether the file is temporary
* @param activateView activate the view of the opened document?
* @param c cursor position to set if not invalid
* @return opened document or nullptr
*/
KTextEditor::Document *
openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile, bool activateView = true, KTextEditor::Cursor c = KTextEditor::Cursor::invalid());
void emitDocumentClosed(const QString &token);
/**
* position cursor in current active view
* will clear selection
......@@ -417,6 +415,13 @@ protected:
*/
bool eventFilter(QObject *obj, QEvent *event) override;
private Q_SLOTS:
/**
* Handle destroy of documents created via openDocUrl.
* Needed to e.g. emit token signal and cleanup temporary files.
*/
void openDocUrlDocumentDestroyed(QObject *document);
private:
/**
* kate's command line args
......@@ -428,6 +433,13 @@ private:
*/
const ApplicationMode m_mode;
/**
* files opened via --tempfile, must be deleted on program exit
* need to be constructed before document manager as the destructor of that might
* access it via the destroyed signal of the document
*/
QHash<QObject *, QStringList> m_tempFilesToDelete;
/**
* known main windows
*/
......@@ -466,11 +478,6 @@ private:
*/
KUserFeedback::Provider m_userFeedbackProvider;
#endif
/**
* files opened via --tempfile, must be deleted on program exit
*/
QStringList m_tempFilesToDelete;
};
#endif
......@@ -49,14 +49,14 @@ void KateAppAdaptor::activate(const QString &token)
bool KateAppAdaptor::openUrl(const QString &url, const QString &encoding)
{
return m_app->openUrl(QUrl(url), encoding, false);
return m_app->openDocUrl(QUrl(url), encoding, false);
}
bool KateAppAdaptor::openUrl(const QString &url, const QString &encoding, bool isTempFile)
{
qCDebug(LOG_KATE) << "openURL";
return m_app->openUrl(QUrl(url), encoding, isTempFile);
return m_app->openDocUrl(QUrl(url), encoding, isTempFile);
}
bool KateAppAdaptor::isOnActivity(const QString &activity)
......
......@@ -200,8 +200,6 @@ bool KateDocManager::closeDocuments(const QList<KTextEditor::Document *> documen
break;
}
KateApp::self()->emitDocumentClosed(QString::number(reinterpret_cast<qptrdiff>(doc)));
// document will be deleted, soon
Q_EMIT documentWillBeDeleted(doc);
......
......@@ -1236,7 +1236,7 @@ void KateMainWindow::queueModifiedOnDisc(KTextEditor::Document *doc)
m_modignore = true;
connect(s_modOnHdDialog, &KateMwModOnHdDialog::requestOpenDiffDocument, this, [this](const QUrl &url) {
// use open with isTempFile == true
KateApp::self()->openUrl(url, QString(), true);
KateApp::self()->openDocUrl(url, QString(), true);
});
// Someone modified a doc outside and now we are here
......
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