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 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
#include "roamingzone.hh"
#include "channel.hh"
#include <QSet>
#include "config.hh"
/* ********************************************************************************************* *
* Implementation of RoamingZone
* ********************************************************************************************* */
RoamingZone::RoamingZone(QObject *parent)
: ConfigObject("roam", parent), _channel()
{
// pass...
}
RoamingZone::RoamingZone(const QString &name, QObject *parent)
: ConfigObject(name, parent), _channel()
{
// pass...
}
RoamingZone &
RoamingZone::operator =(const RoamingZone &other) {
copy(other);
return *this;
}
ConfigItem *
RoamingZone::clone() const {
RoamingZone *z = new RoamingZone();
if (! z->copy(*this)) {
z->deleteLater();
return nullptr;
}
return z;
}
int
RoamingZone::count() const {
return _channel.count();
}
void
RoamingZone::clear() {
_channel.clear();
}
bool
RoamingZone::contains(const RoamingChannel *ch) const {
for (int i=0; i<count(); i++)
if (ch == channel(i))
return true;
return false;
}
RoamingChannel*
RoamingZone::channel(int idx) const {
if ((idx < 0) || (idx >= count()))
return nullptr;
return _channel.get(idx)->as<RoamingChannel>();
}
int
RoamingZone::addChannel(RoamingChannel* ch, int row) {
row = _channel.add(ch, row);
if (0 > row)
return row;
emit modified(this);
return row;
}
bool
RoamingZone::remChannel(int row) {
return _channel.del(_channel.get(row));
}
bool
RoamingZone::remChannel(RoamingChannel* ch) {
return _channel.del(ch);
}
const RoamingChannelRefList *
RoamingZone::channels() const {
return &_channel;
}
RoamingChannelRefList*
RoamingZone::channels() {
return &_channel;
}
bool
RoamingZone::link(const YAML::Node &node, const Context &ctx, const ErrorStack &err) {
// First, run default link
if (! ConfigObject::link(node, ctx, err))
return false;
// Handle channel references separately
if (! node["channels"])
return true;
// check type
if (! node["channels"].IsSequence()) {
errMsg(err) << node["channels"].Mark().line << ":" << node["channels"].Mark().column
<< ": Cannot link 'channels' of 'RoamingZone': Expected sequence.";
return false;
}
YAML::Node lst = node["channels"];
for (YAML::const_iterator it=lst.begin(); it!=lst.end(); it++) {
if (! it->IsScalar()) {
errMsg(err) << it->Mark().line << ":" << it->Mark().column
<< ": Cannot link 'channels' of 'RoamingZone': Expected ID string.";
return false;
}
QString id = QString::fromStdString(it->as<std::string>());
if (! ctx.contains(id)) {
errMsg(err) << it->Mark().line << ":" << it->Mark().column
<< ": Cannot link 'channels' of 'RoamingZone': Reference '"
<< id << "' not defined.";
return false;
}
// Handle referenced object (either DMR channel or roaming channel)
ConfigObject *obj = ctx.getObj(id);
if (obj->is<DMRChannel>()) {
RoamingChannel *rch = RoamingChannel::fromDMRChannel(obj->as<DMRChannel>());
config()->roamingChannels()->add(rch);
addChannel(rch);
} else if (obj->is<RoamingChannel>()) {
addChannel(obj->as<RoamingChannel>());
} else {
errMsg(err) << it->Mark().line << ":" << it->Mark().column
<< ": Cannot link 'channels' of 'RoamingZone': "
<< "Cannot add reference to '" << id << "' to list. "
<< "Not a roaming channel.";
return false;
}
}
return true;
}
bool
RoamingZone::populate(YAML::Node &node, const Context &context, const ErrorStack &err) {
if (! ConfigObject::populate(node, context, err))
return false;
// Serialize list of channel references.
for (int i=0; i<count(); i++) {
if (! context.contains(channel(i))) {
errMsg(err) << "Cannot store reference to roaming channel '" << channel(i)->name()
<< "': No ID assigned.";
return false;
}
node["channels"].push_back(context.getId(channel(i)).toStdString());
}
return true;
}
/* ********************************************************************************************* *
* Implementation of DefaultRoamingZone
* ********************************************************************************************* */
DefaultRoamingZone *DefaultRoamingZone::_instance = nullptr;
DefaultRoamingZone::DefaultRoamingZone(QObject *parent)
: RoamingZone(tr("[Default]"), parent)
{
// pass...
}
DefaultRoamingZone *
DefaultRoamingZone::get() {
if (nullptr == _instance)
_instance = new DefaultRoamingZone();
return _instance;
}
/* ********************************************************************************************* *
* Implementation of RoamingZoneList
* ********************************************************************************************* */
RoamingZoneList::RoamingZoneList(QObject *parent)
: ConfigObjectList(RoamingZone::staticMetaObject, parent)
{
// pass...
}
RoamingZone *
RoamingZoneList::zone(int idx) const {
if (ConfigItem *obj = get(idx))
return obj->as<RoamingZone>();
return nullptr;
}
int
RoamingZoneList::add(ConfigObject *obj, int row, bool unique) {
if (obj && obj->is<RoamingZone>())
return ConfigObjectList::add(obj, row, unique);
return -1;
}
ConfigItem *
RoamingZoneList::allocateChild(const YAML::Node &node, ConfigItem::Context &ctx, const ErrorStack &err) {
Q_UNUSED(ctx)
if (! node)
return nullptr;
if (! node.IsMap()) {
errMsg(err) << node.Mark().line << ":" << node.Mark().column
<< ": Cannot create roaming zone: Expected object.";
return nullptr;
}
return new RoamingZone();
}
|