locationlistmodel.cpp 4.82 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright 2009  Petri Damstén <damu@iki.fi>
 * Copyright 2016  Friedrich W. H. Kossebau <kossebau@kde.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "locationlistmodel.h"

#include <plasmaweather/weathervalidator.h>

#include <Plasma/DataContainer>
24
#include <Plasma/DataEngine>
25 26 27 28 29 30 31 32 33 34 35 36 37 38

#include <KLocalizedString>

#include <QDebug>

LocationListModel::LocationListModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_validatingInput(false)
    , m_checkedInCount(0)
{
}

QVariant LocationListModel::data(const QModelIndex &index, int role) const
{
39
    if (!index.isValid() || index.row() >= m_locations.size()) {
40 41 42 43
        return QVariant();
    }

    switch (role) {
44
        case Qt::DisplayRole: return nameForListIndex(index.row());
45 46 47 48 49 50 51
    }

    return QVariant();
}

int LocationListModel::rowCount(const QModelIndex &index) const
{
52
    if (!index.isValid()) {
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
        return m_locations.size();
    }

    return 0;
}

bool LocationListModel::isValidatingInput() const
{
    return m_validatingInput;
}

QString LocationListModel::valueForListIndex(int listIndex) const
{
    if (0 <= listIndex && listIndex < m_locations.count()) {
        return m_locations.at(listIndex).value;
    }

    return QString();
}

73 74 75 76 77 78 79 80 81 82 83 84 85
QString LocationListModel::nameForListIndex(int listIndex) const
{
    if (0 <= listIndex && listIndex < m_locations.count()) {
        const LocationItem& item = m_locations.at(listIndex);
        if (!item.weatherService.isEmpty()) {
            return i18nc("A weather station location and the weather service it comes from",
                        "%1 (%2)", item.weatherStation, item.weatherService);
        }
    }

    return QString();
}

86
void LocationListModel::searchLocations(const QString &searchString, const QStringList& services)
87 88 89
{
    m_checkedInCount = 0;

90 91 92
    // reset current validators
    qDeleteAll(m_validators);
    m_validators.clear();
93

94 95
    m_searchString = searchString;

96
    if (!m_validatingInput) {
97 98 99 100 101 102 103 104
        m_validatingInput = true;
        emit validatingInputChanged(true);
    }

    beginResetModel();
    m_locations.clear();
    endResetModel();

105 106 107
    if (searchString.isEmpty()) {
        completeSearch();
        return;
108 109
    }

110
    Plasma::DataEngine* dataengine = dataEngine(QStringLiteral("weather"));
111

112
    const QVariantList plugins = dataengine->containerForSource(QStringLiteral("ions"))->data().values();
113
    for (const QVariant& plugin : plugins) {
114 115
        const QStringList pluginInfo = plugin.toString().split(QLatin1Char('|'));
        if (pluginInfo.count() > 1) {
116 117 118 119
            const QString& ionId = pluginInfo[1];
            if (!services.contains(ionId)) {
                continue;
            }
120 121 122
            //qDebug() << "ion: " << pluginInfo[0] << pluginInfo[1];
            //d->ions.insert(pluginInfo[1], pluginInfo[0]);

123 124 125
            auto* validator = new Plasma::WeatherValidator(this);
            connect(validator, &Plasma::WeatherValidator::error, this, &LocationListModel::validatorError);
            connect(validator, &Plasma::WeatherValidator::finished, this, &LocationListModel::addSources);
126
            validator->setDataEngine(dataengine);
127
            validator->setIon(ionId);
128

129
            m_validators.append(validator);
130 131 132
        }
    }

133
    for (auto* validator : qAsConst(m_validators)) {
134 135
        validator->validate(m_searchString, true);
    }
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
}

void LocationListModel::validatorError(const QString &error)
{
    qDebug() << error;
}

void LocationListModel::addSources(const QMap<QString, QString> &sources)
{
    QMapIterator<QString, QString> it(sources);

    // TODO: be more elaborate and use beginInsertRows() & endInsertRows()
    beginResetModel();

    while (it.hasNext()) {
        it.next();
        const QStringList list = it.value().split(QLatin1Char('|'), QString::SkipEmptyParts);
        if (list.count() > 2) {
154 155
            qDebug() << list;
            m_locations.append(LocationItem(list[2], list[0], it.value()));
156 157 158 159 160 161 162
        }
    }

    endResetModel();

    ++m_checkedInCount;
    if (m_checkedInCount >= m_validators.count()) {
163
        completeSearch();
164 165
    }
}
166 167 168 169 170 171 172 173

void LocationListModel::completeSearch()
{
    m_validatingInput = false;
    const bool success = !m_locations.empty();
    emit locationSearchDone(success, m_searchString);
    emit validatingInputChanged(false);
}