Commit 2baca132 authored by Volker Krause's avatar Volker Krause
Browse files

Forward information whether importing from a bundle file worked

Will be needed to improve user feedback, and to auto-detect whether we
are importing a bundle file or a DIVOC certificate.
parent ea8264a3
......@@ -460,19 +460,19 @@ void ApplicationController::exportTripToGpx(const QString &tripGroupId, const QU
Q_EMIT infoMessage(i18n("Export completed."));
}
void ApplicationController::importBundle(const QUrl &url)
bool ApplicationController::importBundle(const QUrl &url)
{
KItinerary::File f(FileHelper::toLocalFile(url));
if (!f.open(File::Read)) {
qCWarning(Log) << "Failed to open bundle file:" << url << f.errorString();
Q_EMIT infoMessage(i18n("Import failed: %1", f.errorString()));
return;
return false;
}
importBundle(&f);
return importBundle(&f);
}
void ApplicationController::importBundle(const QByteArray &data)
bool ApplicationController::importBundle(const QByteArray &data)
{
QBuffer buffer;
buffer.setData(data);
......@@ -481,34 +481,32 @@ void ApplicationController::importBundle(const QByteArray &data)
if (!f.open(File::Read)) {
qCWarning(Log) << "Failed to open bundle data:" << f.errorString();
Q_EMIT infoMessage(i18n("Import failed: %1", f.errorString()));
return;
return false;
}
importBundle(&f);
return importBundle(&f);
}
void ApplicationController::importBundle(KItinerary::File *file)
bool ApplicationController::importBundle(KItinerary::File *file)
{
Importer importer(file);
int count = 0;
{
QSignalBlocker blocker(this); // suppress infoMessage()
importer.importReservations(m_resMgr);
importer.importPasses(m_pkPassMgr);
importer.importDocuments(m_docMgr);
importer.importFavoriteLocations(m_favLocModel);
importer.importTransfers(m_resMgr, m_transferMgr);
importer.importHealthCertificates(m_healthCertMgr);
importer.importLiveData(m_liveDataMgr);
importer.importSettings();
count += importer.importReservations(m_resMgr);
count += importer.importPasses(m_pkPassMgr);
count += importer.importDocuments(m_docMgr);
count += importer.importFavoriteLocations(m_favLocModel);
count += importer.importTransfers(m_resMgr, m_transferMgr);
count += importer.importHealthCertificates(m_healthCertMgr);
count += importer.importLiveData(m_liveDataMgr);
count += importer.importSettings();
}
// favorite locations
auto favLocs = FavoriteLocation::fromJson(QJsonDocument::fromJson(file->customData(QStringLiteral("org.kde.itinerary/favorite-locations"), QStringLiteral("locations"))).array());
if (!favLocs.empty()) {
m_favLocModel->setFavoriteLocations(std::move(favLocs));
if (count > 0) {
Q_EMIT infoMessage(i18n("Import completed."));
}
Q_EMIT infoMessage(i18n("Import completed."));
return count > 0;
}
QVector<QString> ApplicationController::importReservationOrHealthCertificate(const QByteArray &data, const QString &fileName)
......
......@@ -83,9 +83,9 @@ private:
bool hasClipboardContent() const;
void importLocalFile(const QUrl &url);
void importBundle(const QUrl &url);
void importBundle(const QByteArray &data);
void importBundle(KItinerary::File *file);
bool importBundle(const QUrl &url);
bool importBundle(const QByteArray &data);
bool importBundle(KItinerary::File *file);
QVector<QString> importReservationOrHealthCertificate(const QByteArray &data, const QString &fileName = {});
bool importHealthCertificateRecursive(const KItinerary::ExtractorDocumentNode &node);
void importPass(const QString &passId);
......
......@@ -26,12 +26,13 @@
#include <QSettings>
#include <QTemporaryFile>
static void copySettings(const QSettings *from, QSettings *to)
static int copySettings(const QSettings *from, QSettings *to)
{
const auto keys = from->allKeys();
for (const auto &key : keys) {
to->setValue(key, from->value(key));
}
return keys.size();
}
Exporter::Exporter(KItinerary::File *file)
......@@ -148,59 +149,68 @@ Importer::Importer(const KItinerary::File *file)
{
}
void Importer::importReservations(ReservationManager *resMgr)
int Importer::importReservations(ReservationManager *resMgr)
{
const auto resIds = m_file->reservations();
for (const auto &resId : resIds) {
resMgr->addReservation(m_file->reservation(resId));
}
return resIds.size();
}
void Importer::importPasses(PkPassManager *pkPassMgr)
int Importer::importPasses(PkPassManager *pkPassMgr)
{
const auto passIds = m_file->passes();
for (const auto &passId : passIds) {
pkPassMgr->importPassFromData(m_file->passData(passId));
}
return passIds.size();
}
void Importer::importDocuments(DocumentManager* docMgr)
int Importer::importDocuments(DocumentManager* docMgr)
{
const auto docIds = m_file->documents();
for (const auto &docId : docIds) {
docMgr->addDocument(docId, m_file->documentInfo(docId), m_file->documentData(docId));
}
return docIds.size();
}
void Importer::importTransfers(const ReservationManager *resMgr, TransferManager *transferMgr)
int Importer::importTransfers(const ReservationManager *resMgr, TransferManager *transferMgr)
{
int count = 0;
const auto transferDomain = QStringLiteral("org.kde.itinerary/transfers");
for (const auto &batchId : resMgr->batches()) {
auto t = Transfer::fromJson(QJsonDocument::fromJson(m_file->customData(transferDomain, Transfer::identifier(batchId, Transfer::Before))).object());
transferMgr->importTransfer(t);
count += t.state() != Transfer::Discarded ? 1 : 0;
t = Transfer::fromJson(QJsonDocument::fromJson(m_file->customData(transferDomain, Transfer::identifier(batchId, Transfer::After))).object());
transferMgr->importTransfer(t);
count += t.state() != Transfer::Discarded ? 1 : 0;
}
return count;
}
void Importer::importFavoriteLocations(FavoriteLocationModel *favLocModel)
int Importer::importFavoriteLocations(FavoriteLocationModel *favLocModel)
{
auto favLocs = FavoriteLocation::fromJson(QJsonDocument::fromJson(m_file->customData(QStringLiteral("org.kde.itinerary/favorite-locations"), QStringLiteral("locations"))).array());
if (!favLocs.empty()) {
favLocModel->setFavoriteLocations(std::move(favLocs));
}
return favLocs.size();
}
void Importer::importHealthCertificates(HealthCertificateManager *healthCertMgr)
int Importer::importHealthCertificates(HealthCertificateManager *healthCertMgr)
{
const auto domain = QStringLiteral("org.kde.itinerary/health-certificates");
const auto certIds = m_file->listCustomData(domain);
for (const auto &certId : certIds) {
healthCertMgr->importCertificate(m_file->customData(domain, certId));
}
return certIds.size();
}
void Importer::importLiveData(LiveDataManager *liveDataMgr)
int Importer::importLiveData(LiveDataManager *liveDataMgr)
{
const auto ids = m_file->listCustomData(QStringLiteral("org.kde.itinerary/live-data"));
for (const auto &id : ids) {
......@@ -217,19 +227,25 @@ void Importer::importLiveData(LiveDataManager *liveDataMgr)
ld.store(id);
liveDataMgr->importData(id, std::move(ld));
}
return ids.size();
}
void Importer::importSettings()
int Importer::importSettings()
{
const auto iniData = m_file->customData(QStringLiteral("org.kde.itinerary/settings"), QStringLiteral("settings.ini"));
if (iniData.isEmpty()) {
return 0;
}
QTemporaryFile tmp;
if (!tmp.open()) {
qCWarning(Log) << "Failed to open temporary file:" << tmp.errorString();
return;
return 0;
}
tmp.write(m_file->customData(QStringLiteral("org.kde.itinerary/settings"), QStringLiteral("settings.ini")));
tmp.write(iniData);
tmp.close();
QSettings settings;
const QSettings backup(tmp.fileName(), QSettings::IniFormat);
copySettings(&backup, &settings);
return copySettings(&backup, &settings);
}
......@@ -44,14 +44,14 @@ class Importer
public:
explicit Importer(const KItinerary::File *file);
void importReservations(ReservationManager *resMgr);
void importPasses(PkPassManager *pkPassMgr);
void importDocuments(DocumentManager *docMgr);
void importTransfers(const ReservationManager *resMgr, TransferManager *transferMgr);
void importFavoriteLocations(FavoriteLocationModel *favLocModel);
void importHealthCertificates(HealthCertificateManager *healthCertMgr);
void importLiveData(LiveDataManager *liveDataMgr);
void importSettings();
int importReservations(ReservationManager *resMgr);
int importPasses(PkPassManager *pkPassMgr);
int importDocuments(DocumentManager *docMgr);
int importTransfers(const ReservationManager *resMgr, TransferManager *transferMgr);
int importFavoriteLocations(FavoriteLocationModel *favLocModel);
int importHealthCertificates(HealthCertificateManager *healthCertMgr);
int importLiveData(LiveDataManager *liveDataMgr);
int importSettings();
private:
const KItinerary::File *m_file;
......
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