summaryrefslogtreecommitdiffstats
path: root/commands/command_divesite.cpp
blob: 6f558d40861f9e5b784381f265f95cf3dbc2569b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// SPDX-License-Identifier: GPL-2.0

#include "command_divesite.h"
#include "core/divesite.h"
#include "core/subsurface-qt/divelistnotifier.h"
#include "core/qthelper.h"
#include "core/subsurface-string.h"
#include "qt-models/divelocationmodel.h"
#include "qt-models/filtermodels.h"

namespace Command {

// Helper functions to add / remove a set of dive sites

// Add a set of dive sites to the core. The dives that were associated with
// that dive site will be restored to that dive site.
static std::vector<dive_site *> addDiveSites(std::vector<OwningDiveSitePtr> &sites)
{
	std::vector<dive_site *> res;
	QVector<dive *> changedDives;
	res.reserve(sites.size());

	for (OwningDiveSitePtr &ds: sites) {
		// Readd the dives that belonged to this site
		for (int i = 0; i < ds->dives.nr; ++i) {
			// TODO: send dive site changed signal
			struct dive *d = ds->dives.dives[i];
			d->dive_site = ds.get();
			changedDives.push_back(d);
		}

		// Add dive site to core, but remember a non-owning pointer first.
		res.push_back(ds.get());
		int idx = register_dive_site(ds.release()); // Return ownership to backend.
		emit diveListNotifier.diveSiteAdded(res.back(), idx); // Inform frontend of new dive site.
	}

	emit diveListNotifier.divesChanged(changedDives, DiveField::DIVESITE);

	// Clear vector of unused owning pointers
	sites.clear();

	return res;
}

// Remove a set of dive sites. Get owning pointers to them. The dives are set to
// being at no dive site, but the dive site will retain a list of dives, so
// that the dives can be readded to the site on undo.
static std::vector<OwningDiveSitePtr> removeDiveSites(std::vector<dive_site *> &sites)
{
	std::vector<OwningDiveSitePtr> res;
	QVector<dive *> changedDives;
	res.reserve(sites.size());

	for (dive_site *ds: sites) {
		// Reset the dive_site field of the affected dives
		for (int i = 0; i < ds->dives.nr; ++i) {
			struct dive *d = ds->dives.dives[i];
			d->dive_site = nullptr;
			changedDives.push_back(d);
		}

		// Remove dive site from core and take ownership.
		int idx = unregister_dive_site(ds);
		res.emplace_back(ds);
		emit diveListNotifier.diveSiteDeleted(ds, idx); // Inform frontend of removed dive site.
	}

	emit diveListNotifier.divesChanged(changedDives, DiveField::DIVESITE);

	sites.clear();

	return res;
}

AddDiveSite::AddDiveSite(const QString &name)
{
	setText(Command::Base::tr("add dive site"));
	sitesToAdd.emplace_back(alloc_dive_site());
	sitesToAdd.back()->name = copy_qstring(name);
}

bool AddDiveSite::workToBeDone()
{
	return true;
}

void AddDiveSite::redo()
{
	sitesToRemove = addDiveSites(sitesToAdd);
}

void AddDiveSite::undo()
{
	sitesToAdd = removeDiveSites(sitesToRemove);
}

ImportDiveSites::ImportDiveSites(struct dive_site_table *sites, const QString &source)
{
	setText(Command::Base::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 = addDiveSites(sitesToAdd);
}

void ImportDiveSites::undo()
{
	sitesToAdd = removeDiveSites(sitesToRemove);
}

DeleteDiveSites::DeleteDiveSites(const QVector<dive_site *> &sites) : sitesToRemove(std::vector<dive_site *>(sites.begin(),sites.end()))
{
	setText(Command::Base::tr("delete %n dive site(s)", "", sites.size()));
}

bool DeleteDiveSites::workToBeDone()
{
	return !sitesToRemove.empty();
}

void DeleteDiveSites::redo()
{
	sitesToAdd = removeDiveSites(sitesToRemove);
}

void DeleteDiveSites::undo()
{
	sitesToRemove = addDiveSites(sitesToAdd);
}

PurgeUnusedDiveSites::PurgeUnusedDiveSites()
{
	setText(Command::Base::tr("purge unused dive sites"));
	for (int i = 0; i < dive_site_table.nr; ++i) {
		dive_site *ds = dive_site_table.dive_sites[i];
		if (ds->dives.nr == 0)
			sitesToRemove.push_back(ds);
	}
}

bool PurgeUnusedDiveSites::workToBeDone()
{
	return !sitesToRemove.empty();
}

void PurgeUnusedDiveSites::redo()
{
	sitesToAdd = removeDiveSites(sitesToRemove);
}

void PurgeUnusedDiveSites::undo()
{
	sitesToRemove = addDiveSites(sitesToAdd);
}

// Helper function: swap C and Qt string
static void swap(char *&c, QString &q)
{
	QString s = c;
	free(c);
	c = copy_qstring(q);
	q = s;
}

EditDiveSiteName::EditDiveSiteName(dive_site *dsIn, const QString &name) : ds(dsIn),
	value(name)
{
	setText(Command::Base::tr("Edit dive site name"));
}

bool EditDiveSiteName::workToBeDone()
{
	return value != QString(ds->name);
}

void EditDiveSiteName::redo()
{
	swap(ds->name, value);
	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::NAME); // Inform frontend of changed dive site.
}

void EditDiveSiteName::undo()
{
	// Undo and redo do the same
	redo();
}

EditDiveSiteDescription::EditDiveSiteDescription(dive_site *dsIn, const QString &description) : ds(dsIn),
	value(description)
{
	setText(Command::Base::tr("Edit dive site description"));
}

bool EditDiveSiteDescription::workToBeDone()
{
	return value != QString(ds->description);
}

void EditDiveSiteDescription::redo()
{
	swap(ds->description, value);
	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::DESCRIPTION); // Inform frontend of changed dive site.
}

void EditDiveSiteDescription::undo()
{
	// Undo and redo do the same
	redo();
}

EditDiveSiteNotes::EditDiveSiteNotes(dive_site *dsIn, const QString &notes) : ds(dsIn),
	value(notes)
{
	setText(Command::Base::tr("Edit dive site notes"));
}

bool EditDiveSiteNotes::workToBeDone()
{
	return value != QString(ds->notes);
}

void EditDiveSiteNotes::redo()
{
	swap(ds->notes, value);
	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::NOTES); // Inform frontend of changed dive site.
}

void EditDiveSiteNotes::undo()
{
	// Undo and redo do the same
	redo();
}

EditDiveSiteCountry::EditDiveSiteCountry(dive_site *dsIn, const QString &country) : ds(dsIn),
	value(country)
{
	setText(Command::Base::tr("Edit dive site country"));
}

bool EditDiveSiteCountry::workToBeDone()
{
	return !same_string(qPrintable(value), taxonomy_get_country(&ds->taxonomy));
}

void EditDiveSiteCountry::redo()
{
	QString old = taxonomy_get_country(&ds->taxonomy);
	taxonomy_set_country(&ds->taxonomy, copy_qstring(value), taxonomy_origin::GEOMANUAL);
	value = old;
	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::TAXONOMY); // Inform frontend of changed dive site.
}

void EditDiveSiteCountry::undo()
{
	// Undo and redo do the same
	redo();
}

EditDiveSiteLocation::EditDiveSiteLocation(dive_site *dsIn, const location_t location) : ds(dsIn),
	value(location)
{
	setText(Command::Base::tr("Edit dive site location"));
}

bool EditDiveSiteLocation::workToBeDone()
{
	bool ok = has_location(&value);
	bool old_ok = has_location(&ds->location);
	if (ok != old_ok)
		return true;
	return ok && !same_location(&value, &ds->location);
}

void EditDiveSiteLocation::redo()
{
	std::swap(value, ds->location);
	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::LOCATION); // Inform frontend of changed dive site.
}

void EditDiveSiteLocation::undo()
{
	// Undo and redo do the same
	redo();
}

EditDiveSiteTaxonomy::EditDiveSiteTaxonomy(dive_site *dsIn, taxonomy_data &taxonomy) : ds(dsIn),
	value(taxonomy)
{
	// We did a dumb copy. Erase the source to remove double references to strings.
	memset(&taxonomy, 0, sizeof(taxonomy));
	setText(Command::Base::tr("Edit dive site taxonomy"));
}

EditDiveSiteTaxonomy::~EditDiveSiteTaxonomy()
{
	free_taxonomy(&value);
}

bool EditDiveSiteTaxonomy::workToBeDone()
{
	// TODO: Apparently we have no way of comparing taxonomies?
	return true;
}

void EditDiveSiteTaxonomy::redo()
{
	std::swap(value, ds->taxonomy);
	emit diveListNotifier.diveSiteChanged(ds, LocationInformationModel::TAXONOMY); // Inform frontend of changed dive site.
}

void EditDiveSiteTaxonomy::undo()
{
	// Undo and redo do the same
	redo();
}

MergeDiveSites::MergeDiveSites(dive_site *dsIn, const QVector<dive_site *> &sites) : ds(dsIn)
{
	setText(Command::Base::tr("merge dive sites"));
	sitesToRemove.reserve(sites.size());
	for (dive_site *site: sites) {
		if (site != ds)
			sitesToRemove.push_back(site);
	}
}

bool MergeDiveSites::workToBeDone()
{
	return !sitesToRemove.empty();
}

void MergeDiveSites::redo()
{
	// First, remove all dive sites
	sitesToAdd = removeDiveSites(sitesToRemove);

	// Remember which dives changed so that we can send a single dives-edited signal
	QVector<dive *> divesChanged;

	// The dives of the above dive sites were reset to no dive sites.
	// Add them to the merged-into dive site. Thankfully, we remember
	// the dives in the sitesToAdd vector.
	for (const OwningDiveSitePtr &site: sitesToAdd) {
		for (int i = 0; i < site->dives.nr; ++i) {
			add_dive_to_dive_site(site->dives.dives[i], ds);
			divesChanged.push_back(site->dives.dives[i]);
		}
	}
	emit diveListNotifier.divesChanged(divesChanged, DiveField::DIVESITE);
}

void MergeDiveSites::undo()
{
	// Remember which dives changed so that we can send a single dives-edited signal
	QVector<dive *> divesChanged;

	// Before readding the dive sites, unregister the corresponding dives so that they can be
	// readded to their old dive sites.
	for (const OwningDiveSitePtr &site: sitesToAdd) {
		for (int i = 0; i < site->dives.nr; ++i) {
			unregister_dive_from_dive_site(site->dives.dives[i]);
			divesChanged.push_back(site->dives.dives[i]);
		}
	}

	sitesToRemove = addDiveSites(sitesToAdd);

	emit diveListNotifier.divesChanged(divesChanged, DiveField::DIVESITE);
}

ApplyGPSFixes::ApplyGPSFixes(const std::vector<DiveAndLocation> &fixes)
{
	setText(Command::Base::tr("apply GPS fixes"));

	for (const DiveAndLocation &dl: fixes) {
		struct dive_site *ds = dl.d->dive_site;
		if (ds) {
			// Arbitrary choice: if we find multiple fixes for the same dive, we use the first one.
			if (std::find_if(siteLocations.begin(), siteLocations.end(),
					 [ds] (const SiteAndLocation &sl) { return sl.ds == ds; }) == siteLocations.end()) {
				siteLocations.push_back({ ds, dl.location });
			}
		} else {
			ds = create_dive_site(qPrintable(dl.name), &dive_site_table);
			ds->location = dl.location;
			add_dive_to_dive_site(dl.d, ds);
			dl.d->dive_site = nullptr; // This will be set on redo()
			sitesToAdd.emplace_back(ds);
		}
	}
}

bool ApplyGPSFixes::workToBeDone()
{
	return !sitesToAdd.empty() || !siteLocations.empty();
}

void ApplyGPSFixes::editDiveSites()
{
	for (SiteAndLocation &sl: siteLocations) {
		std::swap(sl.location, sl.ds->location);
		emit diveListNotifier.diveSiteChanged(sl.ds, LocationInformationModel::LOCATION); // Inform frontend of changed dive site.
	}
}

void ApplyGPSFixes::redo()
{
	sitesToRemove = addDiveSites(sitesToAdd);
	editDiveSites();
}

void ApplyGPSFixes::undo()
{
	sitesToAdd = removeDiveSites(sitesToRemove);
	editDiveSites();
}

} // namespace Command