LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "addressesmodelmanager.h"
31 #include <QStandardItemModel>
32 #include <QNetworkInterface>
33 #include <QNetworkConfigurationManager>
34 #include <xmlsettingsdialog/datasourceroles.h>
35 #include <xmlsettingsdialog/basesettingsmanager.h>
36 
37 namespace LC
38 {
39 namespace Util
40 {
41  AddressesModelManager::AddressesModelManager (BaseSettingsManager *bsm, int defaultPort, QObject *parent)
42  : QObject { parent }
43  , Model_ { new QStandardItemModel { this } }
44  , BSM_ { bsm }
45  {
46  Model_->setHorizontalHeaderLabels ({ tr ("Host"), tr ("Port") });
47  Model_->horizontalHeaderItem (0)->setData (DataSources::DataFieldType::Enum,
48  DataSources::DataSourceRole::FieldType);
49  Model_->horizontalHeaderItem (1)->setData (DataSources::DataFieldType::Integer,
50  DataSources::DataSourceRole::FieldType);
51 
52  const auto confManager = new QNetworkConfigurationManager { this };
53  connect (confManager,
54  SIGNAL (configurationAdded (QNetworkConfiguration)),
55  this,
56  SLOT (updateAvailInterfaces ()));
57  connect (confManager,
58  SIGNAL (configurationRemoved (QNetworkConfiguration)),
59  this,
60  SLOT (updateAvailInterfaces ()));
61  connect (confManager,
62  SIGNAL (configurationChanged (QNetworkConfiguration)),
63  this,
64  SLOT (updateAvailInterfaces ()));
65 
66  updateAvailInterfaces ();
67 
68  const auto& addrs = BSM_->Property ("ListenAddresses",
69  QVariant::fromValue (GetLocalAddresses (defaultPort))).value<AddrList_t> ();
70  qDebug () << Q_FUNC_INFO << addrs;
71  for (const auto& addr : addrs)
72  AppendRow (addr);
73  }
74 
76  {
77  qRegisterMetaType<AddrList_t> ("LC::Util::AddrList_t");
78  qRegisterMetaTypeStreamOperators<AddrList_t> ();
79  }
80 
81  QAbstractItemModel* AddressesModelManager::GetModel () const
82  {
83  return Model_;
84  }
85 
87  {
88  AddrList_t addresses;
89  for (auto i = 0; i < Model_->rowCount (); ++i)
90  {
91  auto hostItem = Model_->item (i, 0);
92  auto portItem = Model_->item (i, 1);
93  addresses.push_back ({ hostItem->text (), portItem->text () });
94  }
95  return addresses;
96  }
97 
98  void AddressesModelManager::SaveSettings () const
99  {
100  BSM_->setProperty ("ListenAddresses",
101  QVariant::fromValue (GetAddresses ()));
102  }
103 
104  void AddressesModelManager::AppendRow (const QPair<QString, QString>& pair)
105  {
107  {
108  new QStandardItem { pair.first },
109  new QStandardItem { pair.second }
110  };
111  for (const auto item : items)
112  item->setEditable (false);
113  Model_->appendRow (items);
114 
115  emit addressesChanged ();
116  }
117 
118  void AddressesModelManager::updateAvailInterfaces ()
119  {
120  QVariantList hosts;
121  for (const auto& addr : QNetworkInterface::allAddresses ())
122  {
123  if (!addr.scopeId ().isEmpty ())
124  continue;
125 
126  QVariantMap map;
127  map ["ID"] = map ["Name"] = addr.toString ();
128  hosts << map;
129  }
130  Model_->horizontalHeaderItem (0)->setData (hosts,
131  DataSources::DataSourceRole::FieldValues);
132  }
133 
134  void AddressesModelManager::addRequested (const QString&, const QVariantList& data)
135  {
136  const auto port = data.value (1).toInt ();
137  if (port < 1024 || port > 65535)
138  return;
139 
140  AppendRow ({ data.value (0).toString (), QString::number (port) });
141  SaveSettings ();
142  }
143 
144  void AddressesModelManager::removeRequested (const QString&, const QModelIndexList& list)
145  {
146  for (const auto& item : list)
147  Model_->removeRow (item.row ());
148 
149  SaveSettings ();
150  emit addressesChanged ();
151  }
152 }
153 }
QList< QPair< QString, QString > > AddrList_t
Definition: addresses.h:44
QAbstractItemModel * GetModel() const
Returns the managed model.
AddrList_t GetLocalAddresses(int defaultPort)
Returns all local addresses.
Definition: addresses.cpp:38
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.
void removeRequested(const QString &property, const QModelIndexList &list)
Invoked by XML settings dialog to remove some 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.
Definition: constants.h:35