File indexing completed on 2024-05-12 04:42:42

0001 /*
0002     SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "attributionutil_p.h"
0008 
0009 #include <KPublicTransport/Attribution>
0010 
0011 #include <QUrl>
0012 
0013 using namespace KPublicTransport;
0014 
0015 static bool attrLessThan(const Attribution &lhs, const Attribution &rhs)
0016 {
0017     const auto nameCmp = QString::compare(lhs.name(), rhs.name(), Qt::CaseInsensitive);
0018     if (nameCmp == 0) {
0019         return QString::compare(lhs.license(), rhs.license(), Qt::CaseInsensitive) < 0;
0020     }
0021     return nameCmp < 0;
0022 }
0023 
0024 static bool attrEqual(const Attribution &lhs, const Attribution &rhs)
0025 {
0026     const auto nameCmp = QString::compare(lhs.name(), rhs.name(), Qt::CaseInsensitive);
0027     if (nameCmp == 0) {
0028         return QString::compare(lhs.license(), rhs.license(), Qt::CaseInsensitive) == 0;
0029     }
0030     return nameCmp == 0;
0031 }
0032 
0033 void AttributionUtil::sort(std::vector<Attribution> &attrs)
0034 {
0035     std::sort(attrs.begin(), attrs.end(), attrLessThan);
0036 }
0037 
0038 void AttributionUtil::merge(std::vector<Attribution> &left, std::vector<Attribution> &&right)
0039 {
0040     if (left.empty()) {
0041         left = std::move(right);
0042         sort(left);
0043     } else {
0044         for (const auto &a : right) {
0045             merge(left, a);
0046         }
0047     }
0048 }
0049 
0050 void AttributionUtil::merge(std::vector<Attribution> &left, const std::vector<Attribution> &right)
0051 {
0052     for (const auto &a : right) {
0053         merge(left, a);
0054     }
0055 }
0056 
0057 void AttributionUtil::merge(std::vector<Attribution> &left, const Attribution &right)
0058 {
0059     if (right.isEmpty()) {
0060         return;
0061     }
0062 
0063     const auto it = std::lower_bound(left.begin(), left.end(), right, attrLessThan);
0064     if (it != left.end() && attrEqual(*it, right)) {
0065         merge(*it, right);
0066     } else {
0067         left.insert(it, right);
0068     }
0069 }
0070 
0071 void AttributionUtil::merge(Attribution &left, const Attribution &right)
0072 {
0073     if (!left.url().isValid() && right.url().isValid()) {
0074         left.setUrl(right.url());
0075     }
0076     if (!left.hasLicense() && right.hasLicense()) {
0077         left.setLicense(right.license());
0078         left.setLicenseUrl(right.licenseUrl());
0079     }
0080 }