void OsmAnd::FavoriteLocationsCollection_P::clearFavoriteLocations()
{
	QWriteLocker scopedLocker(&_collectionLock);

	doClearFavoriteLocations();

	notifyCollectionChanged();
}
std::shared_ptr<OsmAnd::IFavoriteLocation> OsmAnd::FavoriteLocationsCollection_P::createFavoriteLocation(const PointI position, const QString& title, const QString& group, const ColorRGB color)
{
	QWriteLocker scopedLocker(&_collectionLock);

	std::shared_ptr<FavoriteLocation> newItem(new FavoriteLocation(_containerLink, position, title, group, color));
	_collection.insert(newItem.get(), newItem);

	notifyCollectionChanged();

	return newItem;
}
bool OsmAnd::FavoriteLocationsCollection_P::removeFavoriteLocation(const std::shared_ptr<IFavoriteLocation>& favoriteLocation)
{
	QWriteLocker scopedLocker(&_collectionLock);

	const auto item = std::static_pointer_cast<FavoriteLocation>(favoriteLocation);
	item->detach();
	const auto removedCount = _collection.remove(item.get());
	const auto wasRemoved = (removedCount > 0);

	if (wasRemoved)
		notifyCollectionChanged();

	return wasRemoved;
}
bool OsmAnd::FavoriteLocationsGpxCollection_P::loadFrom(QXmlStreamReader& xmlReader)
{
	std::shared_ptr< FavoriteLocation > newItem;
	QList< std::shared_ptr< FavoriteLocation > > newItems;

	while (!xmlReader.atEnd() && !xmlReader.hasError())
	{
		xmlReader.readNext();
		const auto tagName = xmlReader.name();
		if (xmlReader.isStartElement())
		{
			if (tagName == QLatin1String("wpt"))
			{
				if (newItem)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: unpaired <wpt>");
					return false;
				}

				bool ok = true;
				const double lat = xmlReader.attributes().value(QLatin1String("lat")).toDouble(&ok);
				if (!ok)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: invalid latitude");
					return false;
				}
				const double lon = xmlReader.attributes().value(QLatin1String("lon")).toDouble(&ok);
				if (!ok)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: invalid longitude");
					return false;
				}

				newItem.reset(new FavoriteLocation(LatLon(lat, lon)));
			}
			else if (tagName == QLatin1String("name"))
			{
				if (!newItem)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: unpaired <name>");
					return false;
				}

				newItem->setTitle(xmlReader.readElementText());
			}
			else if (tagName == QLatin1String("category"))
			{
				if (!newItem)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: unpaired <category>");
					return false;
				}

                const auto& group = xmlReader.readElementText();
                if (!group.isEmpty())
                    newItem->setGroup(group);
			}
			else if (tagName == QLatin1String("color"))
			{
				if (!newItem)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: unpaired <color>");
					return false;
				}

				bool ok = false;
				const auto color = Utilities::parseColor(xmlReader.readElementText(), ColorARGB(), &ok);
				if (!ok)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: invalid color");
					continue;
				}

				newItem->setColor(static_cast<ColorRGB>(color));
			}
            else if (tagName == QLatin1String("color"))
            {
                if (!newItem)
                {
                    LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: unexpected <hidden/>");
                    return false;
                }

                newItem->setIsHidden(true);
            }
		}
		else if (xmlReader.isEndElement())
		{
			if (tagName == QLatin1String("wpt"))
			{
				if (!newItem)
				{
					LogPrintf(LogSeverityLevel::Warning, "Malformed favorites GPX file: unpaired </wpt>");
					return false;
				}

				newItems.push_back(newItem);
				newItem.reset();
			}
		}
	}
	if (xmlReader.hasError())
	{
		LogPrintf(
            LogSeverityLevel::Warning,
            "XML error: %s (%" PRIi64 ", %" PRIi64 ")",
            qPrintable(xmlReader.errorString()),
            xmlReader.lineNumber(),
            xmlReader.columnNumber());
		return false;
	}

	{
		QWriteLocker scopedLocker(&_collectionLock);

		doClearFavoriteLocations();
		appendFrom(newItems);

		notifyCollectionChanged();
	}

	return true;
}