blob: 17c1714db137802683f5c7934b95824f7ad4e284 [file] [log] [blame]
Qi Zhaobd19e072017-03-05 15:12:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017, Regents of the University of California,
4 *
5 * This file is part of NFD Control Center. See AUTHORS.md for complete list of NFD
6 * authors and contributors.
7 *
8 * NFD Control Center is free software: you can redistribute it and/or modify it under the
9 * terms of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NFD Control Center is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
14 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with NFD
17 * Control Center, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "channel-status.hpp"
21#include "channel-status.moc"
22
23namespace ndn {
24
25ChannelStatusModel::ChannelStatusModel(QObject* parent/* = 0*/)
26 : QAbstractListModel(parent)
27{
28 connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::ChannelStatus>)), this,
29 SLOT(updateStatus(std::vector<ndn::nfd::ChannelStatus>)),
30 Qt::QueuedConnection);
31}
32
33int
34ChannelStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
35{
36 return m_items.count();
37}
38
39void
40ChannelStatusModel::addItem(const ChannelStatusItem& item)
41{
42 beginInsertRows(QModelIndex(), rowCount(), rowCount());
43 m_items << item;
44 endInsertRows();
45}
46
47QVariant
48ChannelStatusModel::data(const QModelIndex& index, int role) const
49{
50 if (index.row() < 0 || index.row() >= m_items.count()) {
51 return QVariant();
52 }
53
54 const ChannelStatusItem& item = m_items.at(index.row());
55 if (role == ChannelRole) {
56 return item.channel();
57 }
58
59 return QVariant();
60}
61
62QHash<int, QByteArray>
63ChannelStatusModel::roleNames() const
64{
65 QHash<int, QByteArray> roles;
66 roles[ChannelRole] = "channel";
67 return roles;
68}
69
70void
71ChannelStatusModel::clear()
72{
73 beginResetModel();
74 m_items.clear();
75 endResetModel();
76}
77
78void
79ChannelStatusModel::updateStatus(std::vector<ndn::nfd::ChannelStatus> status)
80{
81 beginResetModel();
82 m_items.clear();
83 for (auto const& channel : status) {
84 addItem(ChannelStatusItem(QString::fromStdString(channel.getLocalUri())));
85 }
86 endResetModel();
87}
88
89void
90ChannelStatusModel::onTimeout()
91{
92 std::cerr << "Request timed out (should not really happen)" << std::endl;
93}
94
95} // namespace ndn