diff --git a/kmymoney/CMakeLists.txt b/kmymoney/CMakeLists.txt --- a/kmymoney/CMakeLists.txt +++ b/kmymoney/CMakeLists.txt @@ -132,6 +132,7 @@ KF5::Archive KF5::ConfigGui KF5::WidgetsAddons + KF5::KIOCore KF5::CoreAddons KChart $<$:Qt5::Test> diff --git a/kmymoney/converter/mymoneytemplate.cpp b/kmymoney/converter/mymoneytemplate.cpp --- a/kmymoney/converter/mymoneytemplate.cpp +++ b/kmymoney/converter/mymoneytemplate.cpp @@ -33,6 +33,8 @@ #include #include #include +#include +#include // ---------------------------------------------------------------------------- // Project Includes @@ -63,7 +65,7 @@ bool MyMoneyTemplate::loadTemplate(const QUrl &url) { QString filename; - + bool downloadedFile = false; if (!url.isValid()) { qDebug("Invalid template URL '%s'", qPrintable(url.url())); return false; @@ -75,16 +77,23 @@ } else { bool rc = false; - // TODO: port to kf5 - //rc = KIO::NetAccess::download(url, filename, KMyMoneyUtils::mainWindow()); - if (!rc) { - KMessageBox::detailedError(KMyMoneyUtils::mainWindow(), + downloadedFile = true; + KIO::StoredTransferJob *transferjob = KIO::storedGet (url); + KJobWidgets::setWindow(transferjob, KMyMoneyUtils::mainWindow()); + rc = transferjob->exec(); + if (! rc) { + KMessageBox::detailedError(KMyMoneyUtils::mainWindow(), i18n("Error while loading file '%1'.", url.url()), - // TODO: port to kf5 - QString(),//KIO::NetAccess::lastErrorString(), + transferjob->errorString(), i18n("File access error")); - return false; + return false; } + QTemporaryFile file; + file.setAutoRemove(false); + file.open(); + file.write(transferjob->data()); + filename = file.fileName(); + file.close(); } bool rc = true; @@ -112,11 +121,12 @@ rc = false; } - // if a temporary file was constructed by NetAccess::download, - // then it will be removed with the next call. Otherwise, it - // stays untouched on the local filesystem - // TODO: port to kf5 - //KIO::NetAccess::removeTempFile(filename); + // if a temporary file was downloaded, then it will be removed + // with the next call. Otherwise, it stays untouched on the local + // filesystem. + if (downloadedFile) { + QFile::remove(filename); + } return rc; } @@ -462,18 +472,24 @@ } } else { QTemporaryFile tmpfile; + tmpfile.open(); QSaveFile qfile(tmpfile.fileName()); if (qfile.open(QIODevice::WriteOnly)) { saveToLocalFile(&qfile); if (!qfile.commit()) { - throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.url())); + throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.toDisplayString())); } } else { - throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.url())); + throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.toDisplayString())); } - // TODO: port to kf5 - //if (!KIO::NetAccess::upload(tmpfile.fileName(), url, 0)) - // throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.url())); + int permission = -1; + QFile file(tmpfile.fileName()); + file.open(QIODevice::ReadOnly); + KIO::StoredTransferJob *putjob = KIO::storedPut(file.readAll(), url, permission, KIO::JobFlag::Overwrite); + if (!putjob->exec()) { + throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'.
%2", url.toDisplayString(), putjob->errorString())); + } + file.close(); } return true; } diff --git a/kmymoney/kmymoney.cpp b/kmymoney/kmymoney.cpp --- a/kmymoney/kmymoney.cpp +++ b/kmymoney/kmymoney.cpp @@ -1467,6 +1467,7 @@ return result; } + void KMyMoneyApp::slotFileOpenRecent(const QUrl &url) { KMSTATUS(i18n("Loading file...")); @@ -1527,8 +1528,8 @@ } delete dialog; } - // TODO: port KF5 (NetAccess) - if ((newurl.scheme() == QLatin1String("sql")) || (newurl.isValid() /*&& KIO::NetAccess::exists(newurl, KIO::NetAccess::SourceSide, this)*/)) { + + if (newurl.scheme() == QLatin1String("sql") || KMyMoneyUtils::fileExists(newurl)) { slotFileClose(); if (!d->m_myMoneyView->fileOpen()) { try { @@ -2249,11 +2250,10 @@ // check if the file exists and warn the user bool reallySaveFile = true; - // TODO: port KF5 (NetAccess) - //if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, this)) { - // if (KMessageBox::warningYesNo(this, QLatin1String("") + i18n("The file %1 already exists. Do you really want to overwrite it?", url.toDisplayString(QUrl::PreferLocalFile)) + QLatin1String(""), i18n("File already exists")) != KMessageBox::Yes) - // reallySaveFile = false; - //} + if (KMyMoneyUtils::fileExists(url)) { + if (KMessageBox::warningYesNo(this, QLatin1String("") + i18n("The file %1 already exists. Do you really want to overwrite it?", url.toDisplayString(QUrl::PreferLocalFile)) + QLatin1String(""), i18n("File already exists")) != KMessageBox::Yes) + reallySaveFile = false; + } return reallySaveFile; } diff --git a/kmymoney/kmymoneyutils.h b/kmymoney/kmymoneyutils.h --- a/kmymoney/kmymoneyutils.h +++ b/kmymoney/kmymoneyutils.h @@ -28,7 +28,7 @@ #include #include - +#include // ---------------------------------------------------------------------------- // KDE Headers @@ -366,6 +366,13 @@ * This method deletes security and associated price list but asks beforehand. */ static void deleteSecurity(const MyMoneySecurity &security, QWidget *parent = nullptr); + + + /** + * Check whether the url links to an existing file or not + * @returns whether the file exists or not + */ + static bool fileExists(const QUrl &url); }; #endif diff --git a/kmymoney/kmymoneyutils.cpp b/kmymoney/kmymoneyutils.cpp --- a/kmymoney/kmymoneyutils.cpp +++ b/kmymoney/kmymoneyutils.cpp @@ -48,6 +48,7 @@ #include #include #include +#include // ---------------------------------------------------------------------------- // Project Includes @@ -648,3 +649,18 @@ } } } + +bool KMyMoneyUtils::fileExists(const QUrl &url) +{ + bool fileExists = false; + if (url.isValid()) { + short int detailLevel = 0; // Lowest level: file/dir/symlink/none + KIO::StatJob* statjob = KIO::stat(url, KIO::StatJob::SourceSide, detailLevel); + bool noerror = statjob->exec(); + if (noerror) { + // We want a file + fileExists = !statjob->statResult().isDir(); + } + } + return fileExists; +} diff --git a/kmymoney/plugins/csvexport/csvexporterplugin.cpp b/kmymoney/plugins/csvexport/csvexporterplugin.cpp --- a/kmymoney/plugins/csvexport/csvexporterplugin.cpp +++ b/kmymoney/plugins/csvexport/csvexporterplugin.cpp @@ -29,6 +29,8 @@ #include #include +#include +#include // ---------------------------------------------------------------------------- // Project Includes @@ -74,17 +76,24 @@ bool CsvExporterPlugin::okToWriteFile(const QUrl &url) { - Q_UNUSED(url) - // check if the file exists and warn the user bool reallySaveFile = true; - // TODO: port this to KF5 (NetAccess) -#if 0 - if (KIO::NetAccess::exists(url, KIO::NetAccess::SourceSide, 0)) { + bool fileExists = false; + + if (url.isValid()) { + short int detailLevel = 0; // Lowest level: file/dir/symlink/none + KIO::StatJob* statjob = KIO::stat(url, KIO::StatJob::SourceSide, detailLevel); + bool noerror = statjob->exec(); + if (noerror) { + // We want a file + fileExists = !statjob->statResult().isDir(); + } + } + + if (fileExists) { if (KMessageBox::warningYesNo(0, QString("") + i18n("The file %1 already exists. Do you really want to overwrite it?", url.toDisplayString(QUrl::PreferLocalFile)) + QString(""), i18n("File already exists")) != KMessageBox::Yes) reallySaveFile = false; } -#endif return reallySaveFile; } diff --git a/kmymoney/plugins/qif/import/kimportdlg.cpp b/kmymoney/plugins/qif/import/kimportdlg.cpp --- a/kmymoney/plugins/qif/import/kimportdlg.cpp +++ b/kmymoney/plugins/qif/import/kimportdlg.cpp @@ -34,10 +34,12 @@ // KDE Headers #include -#include +#include #include #include #include +#include +#include // ---------------------------------------------------------------------------- // Project Headers @@ -45,7 +47,7 @@ #include "kmymoneyutils.h" #include "mymoneyfile.h" #include "mymoneyaccount.h" -#include + #include "../config/mymoneyqifprofile.h" #include @@ -138,10 +140,18 @@ /** Make sure the text input is ok */ void KImportDlg::slotFileTextChanged(const QString& text) { - // TODO: port to kf5 - Q_UNUSED(text) -#if 0 - if (!text.isEmpty() && KIO::NetAccess::exists(file(), KIO::NetAccess::SourceSide, KMyMoneyUtils::mainWindow())) { + bool fileExists = false; + if (file().isValid()) { + Q_CONSTEXPR short int detailLevel = 0; // it's a file or a directory or a symlink, or it doesn't exist + KIO::StatJob* statjob = KIO::stat(file(), KIO::StatJob::SourceSide, detailLevel); + bool noerror = statjob->exec(); + if (noerror) { + // We want a file + fileExists = !statjob->statResult().isDir(); + } + } + + if (!text.isEmpty() && fileExists) { // m_qcomboboxDateFormat->setEnabled(true); m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true); m_qlineeditFile->setText(text); @@ -149,7 +159,6 @@ // m_qcomboboxDateFormat->setEnabled(false); m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); } -#endif } void KImportDlg::loadProfiles(const bool selectLast) diff --git a/kmymoney/views/kmymoneyview.cpp b/kmymoney/views/kmymoneyview.cpp --- a/kmymoney/views/kmymoneyview.cpp +++ b/kmymoney/views/kmymoneyview.cpp @@ -45,6 +45,8 @@ #include #include #include +#include +#include #ifdef KF5Activities_FOUND #include @@ -450,7 +452,7 @@ void KMyMoneyView::slotAccountTreeViewChanged(const eAccountsModel::Column column, const bool show) { QVector proxyModels {m_institutionsView->getProxyModel(), m_accountsView->getProxyModel(), - m_categoriesView->getProxyModel(), m_budgetView->getProxyModel()}; + m_categoriesView->getProxyModel(), m_budgetView->getProxyModel()}; for (auto i = proxyModels.count() - 1; i >= 0; --i) { // weed out unloaded views if (!proxyModels.at(i)) @@ -801,7 +803,7 @@ bool KMyMoneyView::readFile(const QUrl &url) { QString filename; - + bool downloadedFile = false; m_fileOpen = false; bool isEncrypted = false; @@ -830,16 +832,22 @@ if (url.isLocalFile()) { filename = url.toLocalFile(); } else { - // TODO: port to kf5 (NetAccess) -#if 0 - if (!KIO::NetAccess::download(url, filename, 0)) { - KMessageBox::detailedError(this, + downloadedFile = true; + KIO::StoredTransferJob *transferjob = KIO::storedGet (url); + KJobWidgets::setWindow(transferjob, this); + if (! transferjob->exec()) { + KMessageBox::detailedError(this, i18n("Error while loading file '%1'.", url.url()), - KIO::NetAccess::lastErrorString(), + transferjob->errorString(), i18n("File access error")); - return false; + return false; } -#endif + QTemporaryFile file; + file.setAutoRemove(false); + file.open(); + file.write(transferjob->data()); + filename = file.fileName(); + file.close(); } // let's glimps into the file to figure out, if it's one @@ -1018,11 +1026,13 @@ } catch (const MyMoneyException &) { } - // if a temporary file was constructed by NetAccess::download, - // then it will be removed with the next call. Otherwise, it - // stays untouched on the local filesystem - // TODO: port to KF5 (NetAccess) - //KIO::NetAccess::removeTempFile(filename); + // if a temporary file was downloaded, then it will be removed + // with the next call. Otherwise, it stays untouched on the local + // filesystem. + if (downloadedFile) { + QFile::remove(filename); + } + return initializeStorage(); } @@ -1443,9 +1453,15 @@ tmpfile.open(); // to obtain the name tmpfile.close(); saveToLocalFile(tmpfile.fileName(), storageWriter.get(), plaintext, keyList); - // TODO: port to kf5 - //if (!KIO::NetAccess::upload(tmpfile.fileName(), url, 0)) - // throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.toDisplayString())); + + Q_CONSTEXPR int permission = -1; + QFile file(tmpfile.fileName()); + file.open(QIODevice::ReadOnly); + KIO::StoredTransferJob *putjob = KIO::storedPut(file.readAll(), url, permission, KIO::JobFlag::Overwrite); + if (!putjob->exec()) { + throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'.
%2", url.toDisplayString(), putjob->errorString())); + } + file.close(); } m_fileType = KmmXML; } catch (const MyMoneyException &e) {