tripgroup.cpp 2.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
    Copyright (C) 2018 Volker Krause <vkrause@kde.org>

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU General Public License
15
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
16 17 18 19
*/

#include "tripgroup.h"
#include "logging.h"
20 21
#include "reservationmanager.h"
#include "tripgroupmanager.h"
22

23 24 25
#include <KItinerary/SortUtil>

#include <QDateTime>
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
#include <QDebug>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>

TripGroup::TripGroup() = default;

TripGroup::TripGroup(TripGroupManager *mgr)
    : m_mgr(mgr)
{
}

TripGroup::~TripGroup() = default;

QString TripGroup::name() const
{
    return m_name;
}

void TripGroup::setName(const QString &name)
{
    m_name = name;
}

QVector<QString> TripGroup::elements() const
{
    return m_elements;
}

56 57 58 59 60
void TripGroup::setElements(const QVector<QString> &elems)
{
    m_elements = elems;
}

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
bool TripGroup::load(const QString &path)
{
    QFile f(path);
    if (!f.open(QFile::ReadOnly)) {
        qCWarning(Log) << "Failed to open trip group file:" << path << f.errorString();
        return false;
    }

    const auto doc = QJsonDocument::fromJson(f.readAll());
    const auto obj = doc.object();
    m_name = obj.value(QLatin1String("name")).toString();
    const auto elems = obj.value(QLatin1String("elements")).toArray();
    m_elements.clear();
    m_elements.reserve(elems.size());
    for (const auto &v : elems) {
        m_elements.push_back(v.toString());
    }

79
    return elems.size() >= 2;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
}

void TripGroup::store(const QString &path) const
{
    QFile f(path);
    if (!f.open(QFile::WriteOnly)) {
        qCWarning(Log) << "Failed to open trip group file:" << path << f.errorString();
        return;
    }

    QJsonObject obj;
    obj.insert(QLatin1String("name"), m_name);
    QJsonArray elems;
    std::copy(m_elements.begin(), m_elements.end(), std::back_inserter(elems));
    obj.insert(QLatin1String("elements"), elems);
    f.write(QJsonDocument(obj).toJson());
}
97 98 99 100 101 102 103 104 105 106 107 108

QDateTime TripGroup::beginDateTime() const
{
    const auto res = m_mgr->m_resMgr->reservation(m_elements.at(0));
    return KItinerary::SortUtil::startDateTime(res);
}

QDateTime TripGroup::endDateTime() const
{
    const auto res = m_mgr->m_resMgr->reservation(m_elements.constLast());
    return KItinerary::SortUtil::endtDateTime(res);
}