diff --git a/desktop-widgets/command.cpp b/desktop-widgets/command.cpp index ccfcbc524..00bb49872 100644 --- a/desktop-widgets/command.cpp +++ b/desktop-widgets/command.cpp @@ -120,6 +120,11 @@ void addDiveSite(const QString &name) execute(new AddDiveSite(name)); } +void importDiveSites(struct dive_site_table *sites, const QString &source) +{ + execute(new ImportDiveSites(sites, source)); +} + void mergeDiveSites(dive_site *ds, const QVector &sites) { execute(new MergeDiveSites(ds, sites)); diff --git a/desktop-widgets/command.h b/desktop-widgets/command.h index 9e08b9a5e..8e8157acf 100644 --- a/desktop-widgets/command.h +++ b/desktop-widgets/command.h @@ -48,6 +48,7 @@ void editDiveSiteCountry(dive_site *ds, const QString &value); void editDiveSiteLocation(dive_site *ds, location_t value); void editDiveSiteTaxonomy(dive_site *ds, taxonomy_data &value); // value is consumed (i.e. will be erased after call)! void addDiveSite(const QString &name); +void importDiveSites(struct dive_site_table *sites, const QString &source); void mergeDiveSites(dive_site *ds, const QVector &sites); void purgeUnusedDiveSites(); diff --git a/desktop-widgets/command_divesite.cpp b/desktop-widgets/command_divesite.cpp index e18a7f3c1..386159a7a 100644 --- a/desktop-widgets/command_divesite.cpp +++ b/desktop-widgets/command_divesite.cpp @@ -100,6 +100,42 @@ void AddDiveSite::undo() sitesToAdd = std::move(removeDiveSites(sitesToRemove)); } +ImportDiveSites::ImportDiveSites(struct dive_site_table *sites, const QString &source) +{ + setText(tr("import dive sites from %1").arg(source)); + + for (int i = 0; i < sites->nr; ++i) { + struct dive_site *new_ds = sites->dive_sites[i]; + + // Don't import dive sites that already exist. Currently we only check for + // the same name. We might want to be smarter here and merge dive site data, etc. + struct dive_site *old_ds = get_same_dive_site(new_ds); + if (old_ds) { + free_dive_site(new_ds); + continue; + } + sitesToAdd.emplace_back(new_ds); + } + + // All site have been consumed + sites->nr = 0; +} + +bool ImportDiveSites::workToBeDone() +{ + return !sitesToAdd.empty(); +} + +void ImportDiveSites::redo() +{ + sitesToRemove = std::move(addDiveSites(sitesToAdd)); +} + +void ImportDiveSites::undo() +{ + sitesToAdd = std::move(removeDiveSites(sitesToRemove)); +} + DeleteDiveSites::DeleteDiveSites(const QVector &sites) : sitesToRemove(sites.toStdVector()) { setText(tr("delete %n dive site(s)", "", sites.size())); diff --git a/desktop-widgets/command_divesite.h b/desktop-widgets/command_divesite.h index 9661bbffd..3fcf085ae 100644 --- a/desktop-widgets/command_divesite.h +++ b/desktop-widgets/command_divesite.h @@ -21,10 +21,26 @@ private: // Note: we only add one dive site. Nevertheless, we use vectors so that we // can reuse the dive site deletion code. - // For redo + // For undo std::vector sitesToRemove; + // For redo + std::vector sitesToAdd; +}; + +class ImportDiveSites : public Base { +public: + // Note: the dive site table is consumed after the call it will be empty. + ImportDiveSites(struct dive_site_table *sites, const QString &source); +private: + bool workToBeDone() override; + void undo() override; + void redo() override; + // For undo + std::vector sitesToRemove; + + // For redo std::vector sitesToAdd; };