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; } @@ -466,14 +476,16 @@ 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())); + } + int permission = -1; + KIO::StoredTransferJob *putjob = KIO::storedPut(&tmpfile, url, permission); + if (!putjob->exec()) { + 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())); } return true; } diff --git a/kmymoney/kmymoney.h b/kmymoney/kmymoney.h --- a/kmymoney/kmymoney.h +++ b/kmymoney/kmymoney.h @@ -1325,6 +1325,13 @@ */ bool payeeReassign(int type); + /** + * Check whether the url links to an existing file or not + * @returns whether the file exists or not + */ + bool fileExists(const QUrl &url); + + Q_SIGNALS: /** * This signal is emitted when a new file is loaded. In the case file diff --git a/kmymoney/kmymoney.cpp b/kmymoney/kmymoney.cpp --- a/kmymoney/kmymoney.cpp +++ b/kmymoney/kmymoney.cpp @@ -68,6 +68,7 @@ #include #include #include +#include #ifdef KF5Holidays_FOUND #include #include @@ -1467,6 +1468,21 @@ return result; } +bool KMyMoneyApp::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; +} + void KMyMoneyApp::slotFileOpenRecent(const QUrl &url) { KMSTATUS(i18n("Loading file...")); @@ -1527,8 +1543,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") || fileExists(newurl)) { slotFileClose(); if (!d->m_myMoneyView->fileOpen()) { try { @@ -2247,11 +2263,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 (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/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 @@ -79,12 +81,20 @@ // 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()) { + short int detailLevel = 0; // Lowest level: file/dir/symlink/none + 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,24 @@ 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(); + qDebug() << file.fileName(); + qDebug() << transferjob->data(); + qDebug() << file.write(transferjob->data()); + filename = file.fileName(); + file.close(); } // let's glimps into the file to figure out, if it's one @@ -1018,11 +1028,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 +1455,12 @@ 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())); + + int permission = -1; + KIO::StoredTransferJob *putjob = KIO::storedPut(&tmpfile, url, permission); + if (!putjob->exec()) { + throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.toDisplayString())); + } } m_fileType = KmmXML; } catch (const MyMoneyException &e) {