File: gxmlModel.cpp

package info (click to toggle)
plasma-gmailfeed 2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 280 kB
  • sloc: cpp: 459; xml: 19; sh: 6; makefile: 3
file content (186 lines) | stat: -rw-r--r-- 6,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/****************************************************************************
 *  Copyright (c) 2024 Anthony Vital <anthony.vital@gmail.com>              *
 *                                                                          *
 *  This file is part of Gmail Feed.                                        *
 *                                                                          *
 *  Gmail Feed 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 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  Gmail Feed 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 Gmail Feed.  If not, see <http://www.gnu.org/licenses/>.     *
 ****************************************************************************/

#include "gxmlModel.h"

#include <QXmlStreamReader>
#include <QtConcurrent>

GxmlModel::GxmlModel(QObject *parent)
    : QAbstractListModel(parent)
    , m_status(Status::Null)
    , m_fullcount(0)
{
    connect(&m_watcher, &QFutureWatcher<Data>::finished, this, &GxmlModel::xmlParsed);
}

GxmlModel::~GxmlModel()
{
}

QHash<int, QByteArray> GxmlModel::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[AuthorRole] = "author";
    roles[TitleRole] = "title";
    roles[LinkRole] = "link";
    return roles;
}


int GxmlModel::rowCount(const QModelIndex & parent) const
{
    Q_UNUSED(parent)
    return m_data.size();
}

QVariant GxmlModel::data(const QModelIndex & index, int role) const
{
    if (!index.isValid()) {
        return QVariant();
    }

    if (index.row() >= m_data.size() || index.row() < 0) {
        return QVariant();
    }

    switch (role) {
        case AuthorRole:
            return m_data.at(index.row())["author"];
        case TitleRole:
            return m_data.at(index.row())["title"];
        case LinkRole:
            return m_data.at(index.row())["link"];
        case IdRole:
            return m_data.at(index.row())["id"];
        default:
            return QVariant();
    }
}

void GxmlModel::setXml(QString xml)
{
    if (m_status != Status::Ready) {
        m_status = Status::Loading;
        Q_EMIT(statusChanged());
    }
    QFuture<GMailData> future = QtConcurrent::run(&GxmlModel::parseXml, this, xml);
    m_watcher.setFuture(future);
}

GMailData GxmlModel::parseXml(QString xml)
{
    bool parsingMessage = false;
    bool parsingAuthor = false;
    int fullcount = -1;
    QString title;
    QString author;
    QString link;
    QString id;
    QXmlStreamReader reader(xml.toUtf8());
    if (reader.hasError()) {
        m_errorString = reader.errorString();
        m_status = Status::Error;
        Q_EMIT(statusChanged());
        return GMailData();
    }

    QList<QString> oldData;
    Q_FOREACH(auto item, m_data) {
        oldData.append(item["id"]);
    }
    int newMessagesCount = 0;
    QMap<QString, QString> firstNewMessage;

    Data newData;
    while (!reader.atEnd()) {
        if (reader.isStartElement()) {
            if (reader.name() == QLatin1String("entry")) {
                parsingMessage = true;
            } else if (reader.name() == QLatin1String("fullcount")) {
                fullcount = reader.readElementText().toInt();
            } else if (parsingMessage && reader.name() == QLatin1String("author")) {
                parsingAuthor = true;
            } else if (parsingAuthor && reader.name() == QLatin1String("name")) {
                author = reader.readElementText();
            } else if (parsingMessage && reader.name() == QLatin1String("title")) {
                title = reader.readElementText();
            } else if (parsingMessage && reader.name() == QLatin1String("link")) {
                link = reader.attributes().value("href").toString();
            } else if (parsingMessage && reader.name() == QLatin1String("id")) {
                id = reader.readElementText();
            }
        } else if (reader.isEndElement()) {
            if (reader.name() == QLatin1String("name")) {
                parsingAuthor = false;
            } else if (reader.name() == QLatin1String("entry")) {
                parsingMessage = false;
                QMap<QString, QString> item;
                item["author"] = author;
                item["title"] = title;
                item["link"] = link;
                item["id"] = id;
                newData.append(item);
                if (!oldData.contains(id)) {
                    if (newMessagesCount == 0) {
                       firstNewMessage = item;
                    }
                    newMessagesCount++;
                }
            }
        }
        reader.readNext();
    }

    if (newMessagesCount == 1) {
        Q_EMIT(newMessage(firstNewMessage["author"], firstNewMessage["title"]));
    } else if (newMessagesCount > 1) {
        Q_EMIT(newMessages(newMessagesCount));
    }

    m_xml = xml;
    Q_EMIT(xmlChanged());

    GMailData result;
    result.data = newData;
    result.fullCount = fullcount > -1 ? fullcount : newData.count();
    result.isValid = true;

    return result;
}

void GxmlModel::xmlParsed()
{
    GMailData result = m_watcher.result();
    if (result.isValid) {
        beginResetModel();
        m_data = result.data;
        endResetModel();

        Q_EMIT(countChanged());

        m_fullcount = result.fullCount;
        Q_EMIT(fullcountChanged());

        if (m_status != Status::Error) {
            m_status = Status::Ready;
            Q_EMIT(statusChanged());
        }
    }
}