LeechCraft 0.6.70-16373-g319c272718
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 ();
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 qRegisterMetaTypeStreamOperators<AddrList_t> ();
65 }
66
67 QAbstractItemModel* AddressesModelManager::GetModel () const
68 {
69 return Model_;
70 }
71
73 {
74 AddrList_t addresses;
75 for (auto i = 0; i < Model_->rowCount (); ++i)
76 {
77 auto hostItem = Model_->item (i, 0);
78 auto portItem = Model_->item (i, 1);
79 addresses.push_back ({ hostItem->text (), portItem->text () });
80 }
81 return addresses;
82 }
83
84 void AddressesModelManager::SaveSettings () const
85 {
86 BSM_->setProperty ("ListenAddresses",
87 QVariant::fromValue (GetAddresses ()));
88 }
89
90 void AddressesModelManager::AppendRow (const QPair<QString, QString>& pair)
91 {
93 {
94 new QStandardItem { pair.first },
95 new QStandardItem { pair.second }
96 };
97 for (const auto item : items)
98 item->setEditable (false);
99 Model_->appendRow (items);
100
101 emit addressesChanged ();
102 }
103
104 void AddressesModelManager::addRequested (const QString&, const QVariantList& data)
105 {
106 const auto port = data.value (1).toInt ();
107 if (port < 1024 || port > 65535)
108 return;
109
110 AppendRow ({ data.value (0).toString (), QString::number (port) });
111 SaveSettings ();
112 }
113
114 void AddressesModelManager::removeRequested (const QString&, const QModelIndexList& list)
115 {
116 for (const auto& item : list)
117 Model_->removeRow (item.row ());
118
119 SaveSettings ();
120 emit addressesChanged ();
121 }
122}
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