blob: f970120385c508542bf4f3ced703cc56a85d0fee [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 "strategy-status.hpp"
21#include "strategy-status.moc"
22
23namespace ndn {
24
25StrategyStatusModel::StrategyStatusModel(QObject* parent/* = 0*/)
26 : QAbstractListModel(parent)
27{
28 connect(this, SIGNAL(onDataReceived(std::vector<ndn::nfd::StrategyChoice>)), this,
29 SLOT(updateStatus(std::vector<ndn::nfd::StrategyChoice>)),
30 Qt::QueuedConnection);
31}
32
33int
34StrategyStatusModel::rowCount(const QModelIndex& parent/* = QModelIndex()*/) const
35{
36 return m_items.count();
37}
38
39void
40StrategyStatusModel::addItem(const StrategyStatusItem& item)
41{
42 beginInsertRows(QModelIndex(), rowCount(), rowCount());
43 m_items << item;
44 endInsertRows();
45}
46
47QVariant
48StrategyStatusModel::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 StrategyStatusItem& item = m_items.at(index.row());
55 if (role == PrefixRole) {
56 return item.prefix();
57 }
58 else if (role == StrategyRole) {
59 return item.strategy();
60 }
61
62 return QVariant();
63}
64
65QHash<int, QByteArray>
66StrategyStatusModel::roleNames() const
67{
68 QHash<int, QByteArray> roles;
69 roles[PrefixRole] = "prefix";
70 roles[StrategyRole] = "strategy";
71 return roles;
72}
73
74void
75StrategyStatusModel::clear()
76{
77 beginResetModel();
78 m_items.clear();
79 endResetModel();
80}
81
82void
83StrategyStatusModel::updateStatus(std::vector<ndn::nfd::StrategyChoice> status)
84{
85 beginResetModel();
86 m_items.clear();
87 for (auto const& strategy : status) {
88 addItem(StrategyStatusItem(QString::fromStdString(strategy.getName().toUri()),
89 QString::fromStdString(strategy.getStrategy().toUri())));
90 }
91 endResetModel();
92}
93
94void
95StrategyStatusModel::onTimeout()
96{
97 std::cerr << "Request timed out (should not really happen)" << std::endl;
98}
99
100} // namespace ndn