Debian Codegraph @

src/network/login.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22
23 #include <QtWidgets>
24 #include "login.h"
25 #include "host.h"
26 #include "network/igsconnection.h"
27 #include "network/wing.h"
28 #include "network/lgs.h"
29 #include "network/cyberoroconnection.h"
30 #include "network/tygemconnection.h"
31 #include "network/eweiqiconnection.h"
32 #include "network/tomconnection.h"
33 #include "connectionwidget.h"
34
35 const char * connectionTypeString[] = {"IGS", "WING", "LGS", "CyberORO", "Tygem", "eWeiQi", "Tom",
36 "Default", "None", "Unknown",
37 "NNGS", "CTN", "CWS",};
38
39 enum CredentialTableColumn { CRED_PROTOCOL, CRED_HOSTNAME, CRED_PORT, CRED_USERNAME, CRED_PASSWORD, CRED_NUMITEMS };
40
41 CredentialTableModel::CredentialTableModel(QObject *parent)
42 : QAbstractTableModel(parent)
43 {
44 editable = true;
45 loadCredentials();
46 }
47
48 CredentialTableModel::~CredentialTableModel() {}
49
50 int CredentialTableModel::rowCount(const QModelIndex &) const
51 {
52 return credentials.size();
53 }
54
55 int CredentialTableModel::columnCount(const QModelIndex &) const
56 {
57 return CRED_NUMITEMS;
58 }
59
60 QModelIndex CredentialTableModel::index ( int row, int column, const QModelIndex &) const
61 {
62 return createIndex(row,column);
63 }
64
65 QVariant CredentialTableModel::data(const QModelIndex &index, int role) const
66 {
67 if(!index.isValid())
68 return QVariant();
69 if ((role != Qt::DisplayRole) && (role != Qt::EditRole))
70 return QVariant();
71 switch (index.column())
72 {
73 case CRED_PROTOCOL:
74 if (role == Qt::DisplayRole)
75 return QString(connectionTypeString[int(credentials[index.row()].type)]);
76 else
77 return QVariant(int(credentials[index.row()].type));
78 case CRED_HOSTNAME:
79 return credentials[index.row()].hostName;
80 case CRED_PORT:
81 return QVariant(credentials[index.row()].port);
82 case CRED_USERNAME:
83 return credentials[index.row()].userName;
84 case CRED_PASSWORD:
85 if ((role == Qt::DisplayRole) && ( ! credentials[index.row()].password.isEmpty() ))
86 return QString::fromUtf8( "\u2022\u2022\u2022\u2022\u2022" );
87 else
88 return credentials[index.row()].password;
89 default:
90 return QVariant();
91 }
92 }
93
94 QVariant CredentialTableModel::headerData(int section, Qt::Orientation orientation, int role) const
95 {
96 if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
97 switch(section)
98 {
99 case CRED_PROTOCOL:
100 return QVariant("Protocol");
101 case CRED_HOSTNAME:
102 return QVariant("Hostname");
103 case CRED_PORT:
104 return QVariant("Port");
105 case CRED_USERNAME:
106 return QVariant("Username");
107 case CRED_PASSWORD:
108 return QVariant("Password");
109 }
110 return QVariant();
111 }
112
113 Qt::ItemFlags CredentialTableModel::flags(const QModelIndex &) const
114 {
115 if (editable)
116 return Qt::ItemIsEnabled | Qt::ItemIsEditable;
117 else
118 return Qt::ItemIsEnabled;
119 }
120
121 bool CredentialTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
122 {
123 int row = index.row();
124 int column = index.column();
125 if ((row < 0) || (row >= credentials.size()) || (role != Qt::EditRole))
126 return false;
127
128 bool ok;
129 int val;
130 switch (column)
131 {
132 case CRED_PROTOCOL:
133 val = value.toInt(& ok);
134 if ((!ok) || (val<0) || (val >= int(TypeDEFAULT)))
135 return false;
136 credentials[row].type = ConnectionType(val);
137 break;
138 case CRED_HOSTNAME:
139 credentials[row].hostName = value.toString();
140 break;
141 case CRED_PORT:
142 val = value.toInt(& ok);
143 if ((!ok) || (val<0) || (val > 0xFFFF))
144 return false;
145 credentials[row].port = (qint16)val;
146 break;
147 case CRED_USERNAME:
148 credentials[row].userName = value.toString();
149 break;
150 case CRED_PASSWORD:
151 credentials[row].password = value.toString();
152 break;
153 default:
154 return false;
155 }
156 emit dataChanged(index,index);
157 return true;
158 }
159
160 bool CredentialTableModel::removeRows(int row, int count, const QModelIndex &)
161 {
162 beginRemoveRows(QModelIndex(),row,row+count-1);
163 for(;count>0;count--)
164 credentials.removeAt(row);
165 endRemoveRows();
166 return true;
167 }
168
169
170 void CredentialTableModel::addCredential(ConnectionCredentials cred)
171 {
172 int row = credentials.size();
173 beginInsertRows(QModelIndex(),row,row);
174 credentials.append(cred);
175 endInsertRows();
176 }
177
178 void CredentialTableModel::loadCredentials(void)
179 {
180 QSettings settings;
181 int size = settings.beginReadArray("CREDENTIALS");
182 for (int i = 0; i < size; ++i)
183 {
184 settings.setArrayIndex(i);
185 int j = 0;
186 for (;j < int(TypeDEFAULT); j++)
187 {
188 if (settings.value("type").toString() == QString(connectionTypeString[j]))
189 break;
190 }
191 if (j == int(TypeDEFAULT))
192 break;
193 // We should probably do some sanity checks here
194 // although this is all user-changeable in the GUI, so bad values should be handled gracefully
195 credentials.append(ConnectionCredentials((ConnectionType)(j),
196 settings.value("host").toString(),
197 (qint16)(settings.value("port").toInt()),
198 settings.value("user").toString(),
199 settings.value("password").toString()));
200 }
201 settings.endArray();
202 }
203
204 void CredentialTableModel::saveCredentials(void)
205 {
206 QSettings settings;
207 settings.beginWriteArray("CREDENTIALS");
208 for (int i = 0; i < credentials.size(); ++i)
209 {
210 settings.setArrayIndex(i);
211 settings.setValue("type", connectionTypeString[credentials.at(i).type]);
212 settings.setValue("host", credentials.at(i).hostName);
213 settings.setValue("port", credentials.at(i).port);
214 settings.setValue("user", credentials.at(i).userName);
215 settings.setValue("password", credentials.at(i).password);
216 }
217 settings.endArray();
218
219 }
220
221 ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
222 : QStyledItemDelegate(parent)
223 {
224 }
225
226 QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
227 const QStyleOptionViewItem &/* option */,
228 const QModelIndex &/* index */) const
229 {
230 QComboBox *editor = new QComboBox(parent);
231 for (int i=0; i <= (int)TypeDEFAULT; i++)
232 editor->addItem(connectionTypeString[i]);
233 return editor;
234 }
235
236 void ComboBoxDelegate::setEditorData(QWidget *editor,
237 const QModelIndex &index) const
238 {
239 int value = index.model()->data(index, Qt::EditRole).toInt();
240
241 QComboBox *comboBox = static_cast<QComboBox*>(editor);
242 comboBox->setCurrentIndex(value);
243 }
244
245 void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
246 const QModelIndex &index) const
247 {
248 QComboBox *comboBox = static_cast<QComboBox*>(editor);
249
250 model->setData(index, comboBox->currentIndex(), Qt::EditRole);
251 }
252
253 void ComboBoxDelegate::updateEditorGeometry(QWidget *editor,
254 const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
255 {
256 editor->setGeometry(option.rect);
257 }
258
259 LoginDialog::LoginDialog(QWidget *parent)
260 : QDialog(parent)
261 {
262 ui.setupUi(this);
263 newCredMenu = new QMenu("New credentials...",this);
264 newCredMenu->addAction((QAction*)(new NewCredentialsAction(QString("IGS"),ConnectionCredentials(TypeIGS,"igs.joyjoy.net", 7777,"",""),(QObject*)this)));
265 newCredMenu->addAction((QAction*)(new NewCredentialsAction(QString("WING"),ConnectionCredentials(TypeWING,"wing.gr.jp", 1515,"",""),(QObject*)this)));
266 newCredMenu->addAction((QAction*)(new NewCredentialsAction(QString("LGS"),ConnectionCredentials(TypeLGS,"lgs.taiwango.net", 9696,"",""),(QObject*)this)));
267 newCredMenu->addAction((QAction*)(new NewCredentialsAction(QString("CyberORO"),ConnectionCredentials(TypeORO,"211.38.95.221", 7447,"",""),(QObject*)this)));
268 newCredMenu->addAction((QAction*)(new NewCredentialsAction(QString("Tygem"),ConnectionCredentials(TypeTYGEM,"121.189.9.52", 80,"",""),(QObject*)this)));
269 newCredMenu->addAction((QAction*)(new NewCredentialsAction(QString("eWeiQi"),ConnectionCredentials(TypeEWEIQI,"121.189.9.52", 80,"",""),(QObject*)this)));
270 newCredMenu->addAction((QAction*)(new NewCredentialsAction(QString("Tom"),ConnectionCredentials(TypeTOM,"61.135.158.147", 80,"",""),(QObject*)this)));
271 credModel = new CredentialTableModel(this);
272 slot_toggleEditable(); // Sets "editable" to false
273 ui.credentialView->setModel((QAbstractTableModel*)credModel);
274 ui.credentialView->setSelectionMode(QAbstractItemView::SingleSelection);
275 ui.credentialView->setSelectionBehavior(QAbstractItemView::SelectRows);
276 ui.credentialView->setItemDelegateForColumn(0,new ComboBoxDelegate(this));
277 ui.credentialView->setColumnWidth(CRED_PROTOCOL,80);
278 ui.credentialView->setColumnWidth(CRED_PORT,50);
279 connect(ui.editButton, &QPushButton::clicked, this, &LoginDialog::slot_toggleEditable);
280 connect(ui.credentialView, &QTableView::activated, this, &LoginDialog::slot_connect);
281 connect(newCredMenu, &QMenu::triggered, this, &LoginDialog::slot_newCredential);
282 ui.credentialView->setContextMenuPolicy(Qt::CustomContextMenu);
283 connect(ui.credentialView, &QTableView::customContextMenuRequested, this, &LoginDialog::slot_showPopup);
284 }
285
286 LoginDialog::~LoginDialog()
287 {
288 delete newCredMenu;
289 delete credModel;
290 }
291
292 void LoginDialog::slot_connect(QModelIndex index)
293 {
294 if(credModel->editable)
295 return;
296
297 int row = index.row();
298 if ( credModel->credentials.at(row).userName.isEmpty() )
299 {
300 QMessageBox::information(this, tr("Empty Login"), tr("You must enter a username"));
301 return;
302 }
303
304 this->setEnabled(false);
305 /* Here a possibility to cancel the connection
306 * while it is being created could be provided using
307 * void NetworkConnection::slot_cancelConnecting(void) */
308
309 //if(ui.connectPB->isDown()) //wth? unreliable?
310
311 connection = newConnection(credModel->credentials.at(row));
312 connect(connection, &NetworkConnection::stateChanged, this, &LoginDialog::slot_receiveConnectionState);
313 connectionWidget->setNetworkConnection(connection);
314 }
315
316 void LoginDialog::slot_cancel(void)
317 {
318 if(connection)
319 connection->userCanceled();
320 this->setEnabled(true);
321 reject();
322 }
323
324 void LoginDialog::slot_toggleEditable(void)
325 {
326 credModel->editable = !(credModel->editable);
327 if (credModel->editable)
328 {
329 ui.editButton->setText("&Save");
330 ui.editLabel->setText("Right click to edit");
331 } else {
332 ui.editButton->setText("&Edit");
333 ui.editLabel->setText("");
334 ui.editLabel->repaint();
335 credModel->saveCredentials();
336 }
337 }
338
339 void LoginDialog::slot_newCredential(QAction *action)
340 {
341 credModel->addCredential(((NewCredentialsAction*)action)->cred);
342 }
343
344 void LoginDialog::slot_showPopup(const QPoint & iPoint)
345 {
346 if (!credModel->editable)
347 return;
348 popup_item = ui.credentialView->indexAt(iPoint);
349 QMenu menu(ui.credentialView);
350 QAction * deleteAction = new QAction(tr("Delete"), 0);
351 menu.addMenu(newCredMenu);
352 if (popup_item != QModelIndex())
353 {
354 connect(deleteAction, &QAction::triggered, this, &LoginDialog::slot_deleteCredential);
355 menu.addSeparator();
356 menu.addAction(deleteAction);
357 }
358
359 menu.exec(ui.credentialView->mapToGlobal(iPoint));
360 delete deleteAction;
361 }
362
363 void LoginDialog::slot_deleteCredential(void)
364 {
365 credModel->removeRow(popup_item.row());
366 }
367
368 void LoginDialog::slot_receiveConnectionState(ConnectionState newState)
369 {
370 switch(newState)
371 {
372 case Connected:
373 connectionWidget->setupButtons();
374 connectionWidget->setEnabled(true);
375 this->setEnabled(true);
376 this->accept();
377 return;
378 case AuthFailed:
379 QMessageBox::information(this, tr("Bad Login"), tr("Invalid Login"));
380 break;
381 case PassFailed:
382 QMessageBox::information(this, tr("Bad Password"), tr("Invalid Password"));
383 break;
384 case ProtocolError:
385 QMessageBox::information(this, tr("Protocol Error"), tr("This may also result from wrong username or password"));
386 break;
387 case Canceled:
388 break;
389 case AlreadyLoggedIn:
390 QMessageBox::information(this, tr("Already Logged In"), tr("Are you logged in somewhere else?"));
391 break;
392 case ConnectionRefused:
393 QMessageBox::information(this, tr("Connection Refused"), tr("Server may be down"));
394 break;
395 case HostNotFound:
396 case SockTimeout:
397 case UnknownError:
398 QMessageBox::information(this, tr("Can't connect"), tr("Can't connect to host!"));
399 break;
400 default:
401 return; // waiting
402 }
403 // Arrive here only in case of connection error
404 disconnect(connection, &NetworkConnection::stateChanged, this, &LoginDialog::slot_receiveConnectionState);
405 connectionWidget->setNetworkConnection(0);
406 connection->deleteLater();
407 connection = 0;
408 this->setEnabled(true);
409 }
410
411 NetworkConnection * LoginDialog::newConnection(ConnectionCredentials cred)
412 {
413 switch(cred.type)
414 {
415 case TypeIGS:
416 return new IGSConnection(cred);
417 case TypeORO:
418 return new CyberOroConnection(cred);
419 case TypeWING:
420 return new WingConnection(cred);
421 case TypeLGS:
422 return new LGSConnection(cred);
423 case TypeTYGEM:
424 return new TygemConnection(cred);
425 case TypeEWEIQI:
426 return new EWeiQiConnection(cred);
427 case TypeTOM:
428 return new TomConnection(cred);
429 default:
430 qDebug("LoginDialog::newConnection : Bad connection Type");
431 // ERROR handling???
432 return 0;
433 }
434 }