LeechCraft 0.6.70-14794-g33744ae6ce
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
addressesmodelmanager.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
10#include <QStandardItemModel>
11#include <QNetworkInterface>
12#include <QNetworkConfigurationManager>
13#include <xmlsettingsdialog/datasourceroles.h>
14#include <xmlsettingsdialog/basesettingsmanager.h>
15
16namespace LC
17{
18namespace Util
19{
20 AddressesModelManager::AddressesModelManager (BaseSettingsManager *bsm, int defaultPort, QObject *parent)
21 : QObject { parent }
22 , Model_ { new QStandardItemModel { this } }
23 , BSM_ { bsm }
24 {
25 Model_->setHorizontalHeaderLabels ({ tr ("Host"), tr ("Port") });
26 Model_->horizontalHeaderItem (0)->setData (DataSources::DataFieldType::Enum,
27 DataSources::DataSourceRole::FieldType);
28 Model_->horizontalHeaderItem (1)->setData (DataSources::DataFieldType::Integer,
29 DataSources::DataSourceRole::FieldType);
30
31 const auto confManager = new QNetworkConfigurationManager { this };
32 connect (confManager,
33 SIGNAL (configurationAdded (QNetworkConfiguration)),
34 this,
35 SLOT (updateAvailInterfaces ()));
36 connect (confManager,
37 SIGNAL (configurationRemoved (QNetworkConfiguration)),
38 this,
39 SLOT (updateAvailInterfaces ()));
40 connect (confManager,
41 SIGNAL (configurationChanged (QNetworkConfiguration)),
42 this,
43 SLOT (updateAvailInterfaces ()));
44
45 updateAvailInterfaces ();
46
47 const auto& addrs = BSM_->Property ("ListenAddresses",
48 QVariant::fromValue (GetLocalAddresses (defaultPort))).value<AddrList_t> ();
49 qDebug () << Q_FUNC_INFO << addrs;
50 for (const auto& addr : addrs)
51 AppendRow (addr);
52 }
53
55 {
56 qRegisterMetaType<AddrList_t> ("LC::Util::AddrList_t");
57 qRegisterMetaTypeStreamOperators<AddrList_t> ();
58 }
59
60 QAbstractItemModel* AddressesModelManager::GetModel () const
61 {
62 return Model_;
63 }
64
66 {
67 AddrList_t addresses;
68 for (auto i = 0; i < Model_->rowCount (); ++i)
69 {
70 auto hostItem = Model_->item (i, 0);
71 auto portItem = Model_->item (i, 1);
72 addresses.push_back ({ hostItem->text (), portItem->text () });
73 }
74 return addresses;
75 }
76
77 void AddressesModelManager::SaveSettings () const
78 {
79 BSM_->setProperty ("ListenAddresses",
80 QVariant::fromValue (GetAddresses ()));
81 }
82
83 void AddressesModelManager::AppendRow (const QPair<QString, QString>& pair)
84 {
86 {
87 new QStandardItem { pair.first },
88 new QStandardItem { pair.second }
89 };
90 for (const auto item : items)
91 item->setEditable (false);
92 Model_->appendRow (items);
93
94 emit addressesChanged ();
95 }
96
97 void AddressesModelManager::updateAvailInterfaces ()
98 {
99 QVariantList hosts;
100 for (const auto& addr : QNetworkInterface::allAddresses ())
101 {
102 if (!addr.scopeId ().isEmpty ())
103 continue;
104
105 QVariantMap map;
106 map ["ID"] = map ["Name"] = addr.toString ();
107 hosts << map;
108 }
109 Model_->horizontalHeaderItem (0)->setData (hosts,
110 DataSources::DataSourceRole::FieldValues);
111 }
112
113 void AddressesModelManager::addRequested (const QString&, const QVariantList& data)
114 {
115 const auto port = data.value (1).toInt ();
116 if (port < 1024 || port > 65535)
117 return;
118
119 AppendRow ({ data.value (0).toString (), QString::number (port) });
120 SaveSettings ();
121 }
122
123 void AddressesModelManager::removeRequested (const QString&, const QModelIndexList& list)
124 {
125 for (const auto& item : list)
126 Model_->removeRow (item.row ());
127
128 SaveSettings ();
129 emit addressesChanged ();
130 }
131}
132}
void addressesChanged()
Notifies about the changes in the selected interfaces list.
AddrList_t GetAddresses() const
Returns the list of addresses of interfaces selected by the user.
void addRequested(const QString &property, const QVariantList &list)
Invoked by XML settings dialog to add new user-selected items.
static void RegisterTypes()
Registers the types used for storage in Qt metasystem.
AddressesModelManager(BaseSettingsManager *bsm, int defaultPort, QObject *parent=nullptr)
Constructs the model manager.
QAbstractItemModel * GetModel() const
Returns the managed model.
void removeRequested(const QString &property, const QModelIndexList &list)
Invoked by XML settings dialog to remove some user-selected items.
char * toString(const char *name, const T &t)
Definition: common.h:55
AddrList_t GetLocalAddresses(int defaultPort)
Returns all local addresses.
Definition: addresses.cpp:15
Definition: constants.h:15