LeechCraft 0.6.70-17609-g3dde4097dd
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 <xmlsettingsdialog/datasourceroles.h>
13#include <xmlsettingsdialog/basesettingsmanager.h>
14
15namespace LC::Util
16{
17 namespace
18 {
19 auto GetAddrInfos ()
20 {
21 const auto& addrs = QNetworkInterface::allAddresses ();
22 QList<DataSources::EnumValueInfo> hosts;
23 hosts.reserve (addrs.size ());
24 for (const auto& addr : addrs)
25 {
26 if (!addr.scopeId ().isEmpty ())
27 continue;
28
29 const auto& str = addr.toString ();
30 hosts.push_back ({
31 .Name_ = str,
32 .UserData_ = str
33 });
34 }
35 return hosts;
36 }
37 }
38
39 AddressesModelManager::AddressesModelManager (BaseSettingsManager *bsm, int defaultPort, QObject *parent)
40 : QObject { parent }
41 , Model_ { new QStandardItemModel { this } }
42 , BSM_ { bsm }
43 {
44 Model_->setHorizontalHeaderLabels ({ tr ("Host"), tr ("Port") });
45
46 using namespace DataSources;
47
48 const auto hostHeader = Model_->horizontalHeaderItem (0);
49 hostHeader->setData (DataFieldType::Enum, DataSourceRole::FieldType);
50 hostHeader->setData (QVariant::fromValue<EnumValueInfoGenerator> (GetAddrInfos), DataSourceRole::FieldValuesGenerator);
51
52 Model_->horizontalHeaderItem (1)->setData (DataFieldType::Integer, DataSourceRole::FieldType);
53
54 const auto& addrs = BSM_->Property ("ListenAddresses",
55 QVariant::fromValue (GetLocalAddresses (defaultPort))).value<AddrList_t> ();
56 qDebug () << Q_FUNC_INFO << addrs;
57 for (const auto& addr : addrs)
58 AppendRow (addr);
59 }
60
62 {
63 qRegisterMetaType<AddrList_t> ("LC::Util::AddrList_t");
64 }
65
66 QAbstractItemModel* AddressesModelManager::GetModel () const
67 {
68 return Model_;
69 }
70
72 {
73 AddrList_t addresses;
74 for (auto i = 0; i < Model_->rowCount (); ++i)
75 {
76 auto hostItem = Model_->item (i, 0);
77 auto portItem = Model_->item (i, 1);
78 addresses.push_back ({ hostItem->text (), portItem->text () });
79 }
80 return addresses;
81 }
82
83 void AddressesModelManager::SaveSettings () const
84 {
85 BSM_->setProperty ("ListenAddresses",
86 QVariant::fromValue (GetAddresses ()));
87 }
88
89 void AddressesModelManager::AppendRow (const QPair<QString, QString>& pair)
90 {
92 {
93 new QStandardItem { pair.first },
94 new QStandardItem { pair.second }
95 };
96 for (const auto item : items)
97 item->setEditable (false);
98 Model_->appendRow (items);
99
100 emit addressesChanged ();
101 }
102
103 void AddressesModelManager::addRequested (const QString&, const QVariantList& data)
104 {
105 const auto port = data.value (1).toInt ();
106 if (port < 1024 || port > 65535)
107 return;
108
109 AppendRow ({ data.value (0).toString (), QString::number (port) });
110 SaveSettings ();
111 }
112
113 void AddressesModelManager::removeRequested (const QString&, const QModelIndexList& list)
114 {
115 for (const auto& item : list)
116 Model_->removeRow (item.row ());
117
118 SaveSettings ();
119 emit addressesChanged ();
120 }
121}
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.
AddrList_t GetLocalAddresses(int defaultPort)
Returns all local addresses.
Definition addresses.cpp:15
QList< QPair< QString, QString > > AddrList_t
Definition addresses.h:21