Changeset View
Changeset View
Standalone View
Standalone View
kmymoney/converter/mymoneytemplate.cpp
Show All 27 Lines | |||||
28 | 28 | | |||
29 | // ---------------------------------------------------------------------------- | 29 | // ---------------------------------------------------------------------------- | ||
30 | // KDE Includes | 30 | // KDE Includes | ||
31 | 31 | | |||
32 | #include <KLocalizedString> | 32 | #include <KLocalizedString> | ||
33 | #include <KMessageBox> | 33 | #include <KMessageBox> | ||
34 | #include <QTemporaryFile> | 34 | #include <QTemporaryFile> | ||
35 | #include <KXmlGuiWindow> | 35 | #include <KXmlGuiWindow> | ||
36 | #include <KIO/StoredTransferJob> | ||||
37 | #include <KJobWidgets> | ||||
36 | 38 | | |||
37 | // ---------------------------------------------------------------------------- | 39 | // ---------------------------------------------------------------------------- | ||
38 | // Project Includes | 40 | // Project Includes | ||
39 | 41 | | |||
40 | #include "kmymoneyutils.h" | 42 | #include "kmymoneyutils.h" | ||
41 | #include "mymoneyfile.h" | 43 | #include "mymoneyfile.h" | ||
42 | #include "mymoneyaccount.h" | 44 | #include "mymoneyaccount.h" | ||
43 | #include "mymoneyexception.h" | 45 | #include "mymoneyexception.h" | ||
Show All 14 Lines | |||||
58 | 60 | | |||
59 | MyMoneyTemplate::~MyMoneyTemplate() | 61 | MyMoneyTemplate::~MyMoneyTemplate() | ||
60 | { | 62 | { | ||
61 | } | 63 | } | ||
62 | 64 | | |||
63 | bool MyMoneyTemplate::loadTemplate(const QUrl &url) | 65 | bool MyMoneyTemplate::loadTemplate(const QUrl &url) | ||
64 | { | 66 | { | ||
65 | QString filename; | 67 | QString filename; | ||
66 | 68 | bool downloadedFile = false; | |||
67 | if (!url.isValid()) { | 69 | if (!url.isValid()) { | ||
68 | qDebug("Invalid template URL '%s'", qPrintable(url.url())); | 70 | qDebug("Invalid template URL '%s'", qPrintable(url.url())); | ||
69 | return false; | 71 | return false; | ||
70 | } | 72 | } | ||
71 | 73 | | |||
72 | m_source = url; | 74 | m_source = url; | ||
73 | if (url.isLocalFile()) { | 75 | if (url.isLocalFile()) { | ||
74 | filename = url.toLocalFile(); | 76 | filename = url.toLocalFile(); | ||
75 | 77 | | |||
76 | } else { | 78 | } else { | ||
77 | bool rc = false; | 79 | bool rc = false; | ||
78 | // TODO: port to kf5 | 80 | downloadedFile = true; | ||
79 | //rc = KIO::NetAccess::download(url, filename, KMyMoneyUtils::mainWindow()); | 81 | KIO::StoredTransferJob *transferjob = KIO::storedGet (url); | ||
82 | KJobWidgets::setWindow(transferjob, KMyMoneyUtils::mainWindow()); | ||||
83 | rc = transferjob->exec(); | ||||
80 | if (!rc) { | 84 | if (! rc) { | ||
81 | KMessageBox::detailedError(KMyMoneyUtils::mainWindow(), | 85 | KMessageBox::detailedError(KMyMoneyUtils::mainWindow(), | ||
82 | i18n("Error while loading file '%1'.", url.url()), | 86 | i18n("Error while loading file '%1'.", url.url()), | ||
83 | // TODO: port to kf5 | 87 | transferjob->errorString(), | ||
84 | QString(),//KIO::NetAccess::lastErrorString(), | | |||
85 | i18n("File access error")); | 88 | i18n("File access error")); | ||
86 | return false; | 89 | return false; | ||
87 | } | 90 | } | ||
91 | QTemporaryFile file; | ||||
92 | file.setAutoRemove(false); | ||||
93 | file.open(); | ||||
94 | file.write(transferjob->data()); | ||||
95 | filename = file.fileName(); | ||||
96 | file.close(); | ||||
88 | } | 97 | } | ||
89 | 98 | | |||
90 | bool rc = true; | 99 | bool rc = true; | ||
91 | QFile file(filename); | 100 | QFile file(filename); | ||
92 | QFileInfo info(file); | 101 | QFileInfo info(file); | ||
93 | if (!info.isFile()) { | 102 | if (!info.isFile()) { | ||
94 | QString msg = i18n("<p><b>%1</b> is not a template file.</p>", filename); | 103 | QString msg = i18n("<p><b>%1</b> is not a template file.</p>", filename); | ||
95 | KMessageBox::error(KMyMoneyUtils::mainWindow(), msg, i18n("Filetype Error")); | 104 | KMessageBox::error(KMyMoneyUtils::mainWindow(), msg, i18n("Filetype Error")); | ||
Show All 11 Lines | 115 | } else { | |||
107 | rc = loadDescription(); | 116 | rc = loadDescription(); | ||
108 | } | 117 | } | ||
109 | file.close(); | 118 | file.close(); | ||
110 | } else { | 119 | } else { | ||
111 | KMessageBox::sorry(KMyMoneyUtils::mainWindow(), i18n("File '%1' not found.", filename)); | 120 | KMessageBox::sorry(KMyMoneyUtils::mainWindow(), i18n("File '%1' not found.", filename)); | ||
112 | rc = false; | 121 | rc = false; | ||
113 | } | 122 | } | ||
114 | 123 | | |||
115 | // if a temporary file was constructed by NetAccess::download, | 124 | // if a temporary file was downloaded, then it will be removed | ||
116 | // then it will be removed with the next call. Otherwise, it | 125 | // with the next call. Otherwise, it stays untouched on the local | ||
117 | // stays untouched on the local filesystem | 126 | // filesystem. | ||
118 | // TODO: port to kf5 | 127 | if (downloadedFile) { | ||
119 | //KIO::NetAccess::removeTempFile(filename); | 128 | QFile::remove(filename); | ||
129 | } | ||||
120 | return rc; | 130 | return rc; | ||
121 | } | 131 | } | ||
122 | 132 | | |||
123 | bool MyMoneyTemplate::loadDescription() | 133 | bool MyMoneyTemplate::loadDescription() | ||
124 | { | 134 | { | ||
125 | int validMask = 0x00; | 135 | int validMask = 0x00; | ||
126 | const int validAccount = 0x01; | 136 | const int validAccount = 0x01; | ||
127 | const int validTitle = 0x02; | 137 | const int validTitle = 0x02; | ||
▲ Show 20 Lines • Show All 329 Lines • ▼ Show 20 Line(s) | 465 | if (qfile.open(QIODevice::WriteOnly)) { | |||
457 | if (!qfile.commit()) { | 467 | if (!qfile.commit()) { | ||
458 | throw MYMONEYEXCEPTION(i18n("Unable to write changes to '%1'", filename)); | 468 | throw MYMONEYEXCEPTION(i18n("Unable to write changes to '%1'", filename)); | ||
459 | } | 469 | } | ||
460 | } else { | 470 | } else { | ||
461 | throw MYMONEYEXCEPTION(i18n("Unable to write changes to '%1'", filename)); | 471 | throw MYMONEYEXCEPTION(i18n("Unable to write changes to '%1'", filename)); | ||
462 | } | 472 | } | ||
463 | } else { | 473 | } else { | ||
464 | QTemporaryFile tmpfile; | 474 | QTemporaryFile tmpfile; | ||
475 | tmpfile.open(); | ||||
465 | QSaveFile qfile(tmpfile.fileName()); | 476 | QSaveFile qfile(tmpfile.fileName()); | ||
466 | if (qfile.open(QIODevice::WriteOnly)) { | 477 | if (qfile.open(QIODevice::WriteOnly)) { | ||
467 | saveToLocalFile(&qfile); | 478 | saveToLocalFile(&qfile); | ||
468 | if (!qfile.commit()) { | 479 | if (!qfile.commit()) { | ||
469 | throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.url())); | 480 | throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.toDisplayString())); | ||
470 | } | 481 | } | ||
471 | } else { | 482 | } else { | ||
472 | throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.url())); | 483 | throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.toDisplayString())); | ||
473 | } | 484 | } | ||
474 | // TODO: port to kf5 | 485 | int permission = -1; | ||
475 | //if (!KIO::NetAccess::upload(tmpfile.fileName(), url, 0)) | 486 | QFile file(tmpfile.fileName()); | ||
476 | // throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'", url.url())); | 487 | file.open(QIODevice::ReadOnly); | ||
488 | KIO::StoredTransferJob *putjob = KIO::storedPut(file.readAll(), url, permission, KIO::JobFlag::Overwrite); | ||||
489 | if (!putjob->exec()) { | ||||
490 | throw MYMONEYEXCEPTION(i18n("Unable to upload to '%1'.<br />%2", url.toDisplayString(), putjob->errorString())); | ||||
491 | } | ||||
492 | file.close(); | ||||
477 | } | 493 | } | ||
478 | return true; | 494 | return true; | ||
479 | } | 495 | } | ||
480 | 496 | | |||
481 | bool MyMoneyTemplate::saveToLocalFile(QSaveFile* qfile) | 497 | bool MyMoneyTemplate::saveToLocalFile(QSaveFile* qfile) | ||
482 | { | 498 | { | ||
483 | QTextStream stream(qfile); | 499 | QTextStream stream(qfile); | ||
484 | stream.setCodec("UTF-8"); | 500 | stream.setCodec("UTF-8"); | ||
485 | stream << m_doc.toString(); | 501 | stream << m_doc.toString(); | ||
486 | stream.flush(); | 502 | stream.flush(); | ||
487 | 503 | | |||
488 | return true; | 504 | return true; | ||
489 | } | 505 | } |