玉兔远程控制 0.1.0-bate8
载入中...
搜索中...
未找到
DlgSettings.cpp
1// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2// Author: Kang Lin <kl222@126.com>
3
4#include <QFileDialog>
5#include <QLoggingCategory>
6#include <QStandardItem>
7#include <QNetworkInterface>
8#include <QMenu>
9#include <QInputDialog>
10#include "DlgSettings.h"
11#include "ui_DlgSettings.h"
12
13static Q_LOGGING_CATEGORY(log, "FtpServer.DlgSettings")
14CDlgSettings::CDlgSettings(QSharedPointer<CParameterFtpServer> para, QWidget *parent)
15 : QDialog(parent)
16 , ui(new Ui::CDlgSettings)
17 , m_Para(para)
18{
19 bool check = false;
20 ui->setupUi(this);
21 Q_ASSERT(m_Para);
22
23 para->m_Net.SetHost(tr("Use the following \"Enable listen at all network interface\""));
24 ui->wNet->SetParameter(&para->m_Net);
25
26 ui->leRoot->setText(m_Para->GetRoot());
27 ui->cbAnonmousLogin->setChecked(m_Para->GetAnonymousLogin());
28 ui->cbReadOnly->setChecked(m_Para->GetReadOnly());
29 ui->sbConnectCount->setValue(m_Para->GetConnectCount());
30 ui->sbConnectCount->setToolTip(tr("-1: Enable all\n 0: Disable all\n>0: Connect count"));
31
32 ui->cbListenAll->setChecked(m_Para->GetListenAll());
33 ui->lvListen->setModel(&m_ModelNetWorkInterface);
34 foreach(auto iface, QNetworkInterface::allInterfaces()) {
35 qDebug(log) << iface;
36 auto entry = iface.addressEntries();
37 if(iface.flags() & QNetworkInterface::IsLoopBack)
38 continue;
39 /*if(!(iface.flags() & QNetworkInterface::CanBroadcast))
40 continue;//*/
41 foreach(auto e, entry) {
42 if(!e.broadcast().isNull()) {
43 QStandardItem* item = new QStandardItem(e.ip().toString());
44 item->setCheckable(true);
45 m_ModelNetWorkInterface.appendRow(item);
46 }
47 }
48 }
49 foreach(auto ip, m_Para->GetListen()) {
50 for (int row = 0; row < m_ModelNetWorkInterface.rowCount(); row++) {
51 QModelIndex index = m_ModelNetWorkInterface.index(row, 0);
52 QString szIp = m_ModelNetWorkInterface.data(index).toString();
53 if (szIp == ip) {
54 m_ModelNetWorkInterface.item(row)->setCheckState(Qt::Checked);
55 break;
56 }
57 }
58 }
59
60 m_szFilteListPrompt = tr("The IP address and the netmask must be separated by a slash (/).") + "\n\n"
61 + tr("ag:") + "\n"
62 + "- 123.123.123.123/n " + tr("where n is any value between 0 and 32") + "\n"
63 + "- 123.123.123.123/255.255.255.255" + "\n"
64 + "- <ipv6-address>/n " + tr("where n is any value between 0 and 128") + "\n\n"
65 + tr("For IP version 4, accepts as well missing trailing components") + "\n"
66 + tr("(i.e., less than 4 octets, like \"192.168.1\"), followed or not by a dot. ") + "\n"
67 + tr("If the netmask is also missing in that case,") + "\n"
68 + tr("it is set to the number of octets actually passed") + "\n"
69 + tr("(in the example above, it would be 24, for 3 octets).") + "\n\n"
70 + tr("Add IP address and the netmask:");
71
72 ui->lvBlacklist->setModel(&m_ModelBlack);
73 check = connect(ui->lvBlacklist, SIGNAL(customContextMenuRequested(const QPoint&)),
74 this, SLOT(slotBlackListContextMenuRequested(const QPoint&)));
75 Q_ASSERT(check);
76 foreach (auto ip, m_Para->GetBlacklist()) {
77 m_ModelBlack.appendRow(new QStandardItem(ip));
78 }
79 ui->lvWhtelist->setModel(&m_ModelWhite);
80 check = connect(ui->lvWhtelist, SIGNAL(customContextMenuRequested(const QPoint&)),
81 this, SLOT(slotWhiteListContextMenuRequested(const QPoint&)));
82 foreach (auto ip, m_Para->GetWhitelist()) {
83 m_ModelWhite.appendRow(new QStandardItem(ip));
84 }
85 Q_ASSERT(check);
86}
87
88CDlgSettings::~CDlgSettings()
89{
90 delete ui;
91}
92
93void CDlgSettings::on_pbRoot_clicked()
94{
95 QString szDir = QFileDialog::getExistingDirectory(this, QString(), ui->leRoot->text());
96 if(szDir.isEmpty())
97 return;
98 ui->leRoot->setText(szDir);
99}
100
101void CDlgSettings::accept()
102{
103 ui->wNet->Accept();
104
105 m_Para->SetRoot(ui->leRoot->text());
106 m_Para->SetAnonymousLogin(ui->cbAnonmousLogin->isChecked());
107 m_Para->SetReadOnly(ui->cbReadOnly->isChecked());
108 m_Para->SetConnectCount(ui->sbConnectCount->value());
109
110 m_Para->SetListenAll(ui->cbListenAll->isChecked());
111 QStringList lstInterface;
112 for (int row = 0; row < m_ModelNetWorkInterface.rowCount(); row++) {
113 auto item = m_ModelNetWorkInterface.item(row);
114 if(item->checkState() == Qt::Checked) {
115 lstInterface << item->text();
116 }
117 }
118 m_Para->SetListen(lstInterface);
119
120 QStringList lstBlack;
121 for (int i = 0; i < m_ModelBlack.rowCount(); ++i) {
122 lstBlack << m_ModelBlack.item(i)->text();
123 }
124 m_Para->SetBlacklist(lstBlack);
125 QStringList lstWhite;
126 for (int i = 0; i < m_ModelWhite.rowCount(); ++i) {
127 lstWhite << m_ModelWhite.item(i)->text();
128 }
129 m_Para->SetWhitelist(lstWhite);
130
131 QDialog::accept();
132}
133
134void CDlgSettings::on_cbAnonmousLogin_checkStateChanged(const Qt::CheckState &arg1)
135{
136 bool bEnable = (Qt::Checked != arg1);
137}
138
139void CDlgSettings::on_cbListenAll_checkStateChanged(const Qt::CheckState &arg1)
140{
141 ui->lvListen->setEnabled(Qt::Checked != arg1);
142}
143
144void CDlgSettings::slotWhiteListContextMenuRequested(const QPoint& pos)
145{
146 QMenu m;
147 QItemSelectionModel* pSelect = ui->lvWhtelist->selectionModel();
148 QModelIndexList lstIndex = pSelect->selectedRows();
149 m.addAction(tr("Add"), this, SLOT(on_pbAddWhitelist_clicked()));
150 if(!lstIndex.isEmpty()) {
151 m.addAction(tr("Delete"), this, SLOT(on_pbDeleteWhitelist_clicked()));
152 }
153
154 QPoint p = ui->lvWhtelist->mapToGlobal(pos);
155 m.exec(p);
156}
157
158void CDlgSettings::slotBlackListContextMenuRequested(const QPoint& pos)
159{
160 QMenu m;
161 QItemSelectionModel* pSelect = ui->lvBlacklist->selectionModel();
162 QModelIndexList lstIndex = pSelect->selectedRows();
163 m.addAction(tr("Add"), this, SLOT(on_pbAddBlacklist_clicked()));
164 if(!lstIndex.isEmpty()) {
165 m.addAction(tr("Delete"), this, SLOT(on_pbDeleteBlacklist_clicked()));
166 }
167
168 QPoint p = ui->lvBlacklist->mapToGlobal(pos);
169 m.exec(p);
170}
171
172void CDlgSettings::on_pbAddWhitelist_clicked()
173{
174 QString szIp = QInputDialog::getText(this, tr("Add whilte list"), m_szFilteListPrompt);
175 QStandardItem* item = new QStandardItem(szIp);
176 m_ModelWhite.appendRow(item);
177}
178
179void CDlgSettings::on_pbDeleteWhitelist_clicked()
180{
181 QItemSelectionModel* pSelect = ui->lvBlacklist->selectionModel();
182 QModelIndexList lstIndex = pSelect->selectedRows();
183 foreach(auto idx, lstIndex) {
184 m_ModelWhite.removeRow(idx.row());
185 }
186}
187
188void CDlgSettings::on_pbAddBlacklist_clicked()
189{
190 QString szIp = QInputDialog::getText(this, tr("Add black list"), m_szFilteListPrompt);
191 QStandardItem* item = new QStandardItem(szIp);
192 m_ModelBlack.appendRow(item);
193}
194
195void CDlgSettings::on_pbDeleteBlacklist_clicked()
196{
197 QItemSelectionModel* pSelect = ui->lvBlacklist->selectionModel();
198 if(!pSelect) return;
199 QModelIndexList lstIndex = pSelect->selectedRows();
200 foreach(auto idx, lstIndex) {
201 m_ModelBlack.removeRow(idx.row());
202 }
203}