LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 <xmlsettingsdialog/datasourceroles.h>
34 #include <xmlsettingsdialog/basesettingsmanager.h>
35 
36 namespace LeechCraft
37 {
38 namespace Util
39 {
40  AddressesModelManager::AddressesModelManager (BaseSettingsManager *bsm, int defaultPort, QObject *parent)
41  : QObject { parent }
42  , Model_ { new QStandardItemModel { this } }
43  , BSM_ { bsm }
44  {
45  Model_->setHorizontalHeaderLabels ({ tr ("Host"), tr ("Port") });
46  Model_->horizontalHeaderItem (0)->setData (DataSources::DataFieldType::Enum,
47  DataSources::DataSourceRole::FieldType);
48  Model_->horizontalHeaderItem (1)->setData (DataSources::DataFieldType::Integer,
49  DataSources::DataSourceRole::FieldType);
50 
51  updateAvailInterfaces ();
52 
53  const auto& addrs = BSM_->Property ("ListenAddresses",
54  QVariant::fromValue (GetLocalAddresses (defaultPort))).value<AddrList_t> ();
55  qDebug () << Q_FUNC_INFO << addrs;
56  for (const auto& addr : addrs)
57  AppendRow (addr);
58  }
59 
61  {
62  qRegisterMetaType<AddrList_t> ("LeechCraft::Util::AddrList_t");
63  qRegisterMetaTypeStreamOperators<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::updateAvailInterfaces ()
104  {
105  QVariantList hosts;
106  for (const auto& addr : QNetworkInterface::allAddresses ())
107  {
108  if (!addr.scopeId ().isEmpty ())
109  continue;
110 
111  QVariantMap map;
112  map ["ID"] = map ["Name"] = addr.toString ();
113  hosts << map;
114  }
115  Model_->horizontalHeaderItem (0)->setData (hosts,
116  DataSources::DataSourceRole::FieldValues);
117  }
118 
119  void AddressesModelManager::addRequested (const QString&, const QVariantList& data)
120  {
121  const auto port = data.value (1).toInt ();
122  if (port < 1024 || port > 65535)
123  return;
124 
125  AppendRow ({ data.value (0).toString (), QString::number (port) });
126  SaveSettings ();
127  }
128 
129  void AddressesModelManager::removeRequested (const QString&, const QModelIndexList& list)
130  {
131  for (const auto& item : list)
132  Model_->removeRow (item.row ());
133 
134  SaveSettings ();
135  emit addressesChanged ();
136  }
137 }
138 }
AddrList_t GetLocalAddresses(int defaultPort)
Returns all local addresses.
Definition: addresses.cpp:38
void removeRequested(const QString &, const QModelIndexList &)
AddressesModelManager(BaseSettingsManager *, int defaultPort, QObject *=0)
QList< QPair< QString, QString > > AddrList_t
Definition: addresses.h:44
void addRequested(const QString &, const QVariantList &)