KPublicTransport

attributionutil.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "attributionutil_p.h"
8 
9 #include <KPublicTransport/Attribution>
10 
11 #include <QUrl>
12 
13 using namespace KPublicTransport;
14 
15 static bool attrLessThan(const Attribution &lhs, const Attribution &rhs)
16 {
17  const auto nameCmp = QString::compare(lhs.name(), rhs.name(), Qt::CaseInsensitive);
18  if (nameCmp == 0) {
19  return QString::compare(lhs.license(), rhs.license(), Qt::CaseInsensitive) < 0;
20  }
21  return nameCmp < 0;
22 }
23 
24 static bool attrEqual(const Attribution &lhs, const Attribution &rhs)
25 {
26  const auto nameCmp = QString::compare(lhs.name(), rhs.name(), Qt::CaseInsensitive);
27  if (nameCmp == 0) {
28  return QString::compare(lhs.license(), rhs.license(), Qt::CaseInsensitive) == 0;
29  }
30  return nameCmp == 0;
31 }
32 
33 void AttributionUtil::sort(std::vector<Attribution> &attrs)
34 {
35  std::sort(attrs.begin(), attrs.end(), attrLessThan);
36 }
37 
38 void AttributionUtil::merge(std::vector<Attribution> &left, std::vector<Attribution> &&right)
39 {
40  if (left.empty()) {
41  left = std::move(right);
42  sort(left);
43  } else {
44  for (const auto &a : right) {
45  merge(left, a);
46  }
47  }
48 }
49 
50 void AttributionUtil::merge(std::vector<Attribution> &left, const std::vector<Attribution> &right)
51 {
52  for (const auto &a : right) {
53  merge(left, a);
54  }
55 }
56 
57 void AttributionUtil::merge(std::vector<Attribution> &left, const Attribution &right)
58 {
59  if (right.isEmpty()) {
60  return;
61  }
62 
63  const auto it = std::lower_bound(left.begin(), left.end(), right, attrLessThan);
64  if (it != left.end() && attrEqual(*it, right)) {
65  merge(*it, right);
66  } else {
67  left.insert(it, right);
68  }
69 }
70 
71 void AttributionUtil::merge(Attribution &left, const Attribution &right)
72 {
73  if (!left.url().isValid() && right.url().isValid()) {
74  left.setUrl(right.url());
75  }
76  if (!left.hasLicense() && right.hasLicense()) {
77  left.setLicense(right.license());
78  left.setLicenseUrl(right.licenseUrl());
79  }
80 }
QString license
Name of the license for the provided data.
Definition: attribution.h:36
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
bool isEmpty() const
Returns true if this is an empty or default-constructed Attribution object.
Definition: attribution.cpp:33
bool hasLicense
true if there is license information for this attribution object.
Definition: attribution.h:41
CaseInsensitive
QUrl licenseUrl
Link to the license or terms and conditions text.
Definition: attribution.h:38
QUrl url
Link to the entity providing the data.
Definition: attribution.h:34
Copyright and license information about the provided data.
Definition: attribution.h:28
bool isValid() const const
QString name
Name of the entity providing the data.
Definition: attribution.h:32
int compare(const QString &other, Qt::CaseSensitivity cs) const const
QVariant merge(const QVariant &lhs, const QVariant &rhs)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.