TunnelPane.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. #include "TunnelPane.h"
  2. #include "QMessageBox"
  3. #include "mainwindow.h"
  4. #include "ui_mainwindow.h"
  5. TunnelPane::TunnelPane(TunnelsPageUpdateListener* tunnelsPageUpdateListener_, TunnelConfig* tunnelConfig_, QWidget* wrongInputPane_, QLabel* wrongInputLabel_, MainWindow* mainWindow_):
  6. QObject(),
  7. mainWindow(mainWindow_),
  8. wrongInputPane(wrongInputPane_),
  9. wrongInputLabel(wrongInputLabel_),
  10. tunnelConfig(tunnelConfig_),
  11. tunnelsPageUpdateListener(tunnelsPageUpdateListener_),
  12. gridLayoutWidget_2(nullptr) {}
  13. void TunnelPane::setupTunnelPane(
  14. TunnelConfig* tunnelConfig,
  15. QGroupBox *tunnelGroupBox,
  16. QWidget* gridLayoutWidget_2, QComboBox * tunnelTypeComboBox,
  17. QWidget *tunnelsFormGridLayoutWidget, int tunnelsRow, int height, int h) {
  18. tunnelGroupBox->setGeometry(0, tunnelsFormGridLayoutWidget->height(), gridLayoutWidget_2->width(), h);
  19. tunnelsFormGridLayoutWidget->resize(527, tunnelsFormGridLayoutWidget->height()+h);
  20. QObject::connect(tunnelTypeComboBox, SIGNAL(currentIndexChanged(int)),
  21. this, SLOT(updated()));
  22. this->tunnelGroupBox=tunnelGroupBox;
  23. gridLayoutWidget_2->setObjectName(QStringLiteral("gridLayoutWidget_2"));
  24. this->gridLayoutWidget_2=gridLayoutWidget_2;
  25. tunnelGridLayout = new QVBoxLayout(gridLayoutWidget_2);
  26. tunnelGridLayout->setObjectName(QStringLiteral("tunnelGridLayout"));
  27. tunnelGridLayout->setContentsMargins(5, 5, 5, 5);
  28. tunnelGridLayout->setSpacing(5);
  29. //header
  30. QHBoxLayout *headerHorizontalLayout = new QHBoxLayout();
  31. headerHorizontalLayout->setObjectName(QStringLiteral("headerHorizontalLayout"));
  32. nameLabel = new QLabel(gridLayoutWidget_2);
  33. nameLabel->setObjectName(QStringLiteral("nameLabel"));
  34. headerHorizontalLayout->addWidget(nameLabel);
  35. nameLineEdit = new QLineEdit(gridLayoutWidget_2);
  36. nameLineEdit->setObjectName(QStringLiteral("nameLineEdit"));
  37. const QString& tunnelName=tunnelConfig->getName().c_str();
  38. nameLineEdit->setText(tunnelName);
  39. setGroupBoxTitle(tunnelName);
  40. QObject::connect(nameLineEdit, SIGNAL(textChanged(const QString &)),
  41. this, SLOT(setGroupBoxTitle(const QString &)));
  42. QObject::connect(nameLineEdit, SIGNAL(textChanged(const QString &)),
  43. this, SLOT(updated()));
  44. headerHorizontalLayout->addWidget(nameLineEdit);
  45. headerHorizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
  46. headerHorizontalLayout->addItem(headerHorizontalSpacer);
  47. deletePushButton = new QPushButton(gridLayoutWidget_2);
  48. deletePushButton->setObjectName(QStringLiteral("deletePushButton"));
  49. QObject::connect(deletePushButton, SIGNAL(released()),
  50. this, SLOT(deleteButtonReleased()));//MainWindow::DeleteTunnelNamed(std::string name) {
  51. headerHorizontalLayout->addWidget(deletePushButton);
  52. tunnelGridLayout->addLayout(headerHorizontalLayout);
  53. //type
  54. {
  55. const QString& type = tunnelConfig->getType();
  56. QHBoxLayout * horizontalLayout_ = new QHBoxLayout();
  57. horizontalLayout_->setObjectName(QStringLiteral("horizontalLayout_"));
  58. typeLabel = new QLabel(gridLayoutWidget_2);
  59. typeLabel->setObjectName(QStringLiteral("typeLabel"));
  60. horizontalLayout_->addWidget(typeLabel);
  61. horizontalLayout_->addWidget(tunnelTypeComboBox);
  62. QPushButton * lockButton1 = new QPushButton(gridLayoutWidget_2);
  63. horizontalLayout_->addWidget(lockButton1);
  64. widgetlocks.add(new widgetlock(tunnelTypeComboBox, lockButton1));
  65. this->tunnelTypeComboBox=tunnelTypeComboBox;
  66. QSpacerItem * horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
  67. horizontalLayout_->addItem(horizontalSpacer);
  68. tunnelGridLayout->addLayout(horizontalLayout_);
  69. }
  70. retranslateTunnelForm(*this);
  71. }
  72. void TunnelPane::appendControlsForI2CPParameters(I2CPParameters& i2cpParameters, int& gridIndex) {
  73. {
  74. //number of hops of an inbound tunnel
  75. const QString& inbound_length=i2cpParameters.getInbound_length();
  76. QHBoxLayout *horizontalLayout_2 = new QHBoxLayout();
  77. horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
  78. inbound_lengthLabel = new QLabel(gridLayoutWidget_2);
  79. inbound_lengthLabel->setObjectName(QStringLiteral("inbound_lengthLabel"));
  80. horizontalLayout_2->addWidget(inbound_lengthLabel);
  81. inbound_lengthLineEdit = new QLineEdit(gridLayoutWidget_2);
  82. inbound_lengthLineEdit->setObjectName(QStringLiteral("inbound_lengthLineEdit"));
  83. inbound_lengthLineEdit->setText(inbound_length);
  84. inbound_lengthLineEdit->setMaximumWidth(80);
  85. QObject::connect(inbound_lengthLineEdit, SIGNAL(textChanged(const QString &)),
  86. this, SLOT(updated()));
  87. horizontalLayout_2->addWidget(inbound_lengthLineEdit);
  88. QSpacerItem * horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
  89. horizontalLayout_2->addItem(horizontalSpacer);
  90. tunnelGridLayout->addLayout(horizontalLayout_2);
  91. }
  92. {
  93. //number of hops of an outbound tunnel
  94. const QString& outbound_length=i2cpParameters.getOutbound_length();
  95. QHBoxLayout *horizontalLayout_2 = new QHBoxLayout();
  96. horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
  97. outbound_lengthLabel = new QLabel(gridLayoutWidget_2);
  98. outbound_lengthLabel->setObjectName(QStringLiteral("outbound_lengthLabel"));
  99. horizontalLayout_2->addWidget(outbound_lengthLabel);
  100. outbound_lengthLineEdit = new QLineEdit(gridLayoutWidget_2);
  101. outbound_lengthLineEdit->setObjectName(QStringLiteral("outbound_lengthLineEdit"));
  102. outbound_lengthLineEdit->setText(outbound_length);
  103. outbound_lengthLineEdit->setMaximumWidth(80);
  104. QObject::connect(outbound_lengthLineEdit, SIGNAL(textChanged(const QString &)),
  105. this, SLOT(updated()));
  106. horizontalLayout_2->addWidget(outbound_lengthLineEdit);
  107. QSpacerItem * horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
  108. horizontalLayout_2->addItem(horizontalSpacer);
  109. tunnelGridLayout->addLayout(horizontalLayout_2);
  110. }
  111. {
  112. //number of inbound tunnels
  113. const QString& inbound_quantity=i2cpParameters.getInbound_quantity();
  114. QHBoxLayout *horizontalLayout_2 = new QHBoxLayout();
  115. horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
  116. inbound_quantityLabel = new QLabel(gridLayoutWidget_2);
  117. inbound_quantityLabel->setObjectName(QStringLiteral("inbound_quantityLabel"));
  118. horizontalLayout_2->addWidget(inbound_quantityLabel);
  119. inbound_quantityLineEdit = new QLineEdit(gridLayoutWidget_2);
  120. inbound_quantityLineEdit->setObjectName(QStringLiteral("inbound_quantityLineEdit"));
  121. inbound_quantityLineEdit->setText(inbound_quantity);
  122. inbound_quantityLineEdit->setMaximumWidth(80);
  123. QObject::connect(inbound_quantityLineEdit, SIGNAL(textChanged(const QString &)),
  124. this, SLOT(updated()));
  125. horizontalLayout_2->addWidget(inbound_quantityLineEdit);
  126. QSpacerItem * horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
  127. horizontalLayout_2->addItem(horizontalSpacer);
  128. tunnelGridLayout->addLayout(horizontalLayout_2);
  129. }
  130. {
  131. //number of outbound tunnels
  132. const QString& outbound_quantity=i2cpParameters.getOutbound_quantity();
  133. QHBoxLayout *horizontalLayout_2 = new QHBoxLayout();
  134. horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
  135. outbound_quantityLabel = new QLabel(gridLayoutWidget_2);
  136. outbound_quantityLabel->setObjectName(QStringLiteral("outbound_quantityLabel"));
  137. horizontalLayout_2->addWidget(outbound_quantityLabel);
  138. outbound_quantityLineEdit = new QLineEdit(gridLayoutWidget_2);
  139. outbound_quantityLineEdit->setObjectName(QStringLiteral("outbound_quantityLineEdit"));
  140. outbound_quantityLineEdit->setText(outbound_quantity);
  141. outbound_quantityLineEdit->setMaximumWidth(80);
  142. QObject::connect(outbound_quantityLineEdit, SIGNAL(textChanged(const QString &)),
  143. this, SLOT(updated()));
  144. horizontalLayout_2->addWidget(outbound_quantityLineEdit);
  145. QSpacerItem * horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
  146. horizontalLayout_2->addItem(horizontalSpacer);
  147. tunnelGridLayout->addLayout(horizontalLayout_2);
  148. }
  149. {
  150. //number of ElGamal/AES tags to send
  151. const QString& crypto_tagsToSend=i2cpParameters.getCrypto_tagsToSend();
  152. QHBoxLayout *horizontalLayout_2 = new QHBoxLayout();
  153. horizontalLayout_2->setObjectName(QStringLiteral("horizontalLayout_2"));
  154. crypto_tagsToSendLabel = new QLabel(gridLayoutWidget_2);
  155. crypto_tagsToSendLabel->setObjectName(QStringLiteral("crypto_tagsToSendLabel"));
  156. horizontalLayout_2->addWidget(crypto_tagsToSendLabel);
  157. crypto_tagsToSendLineEdit = new QLineEdit(gridLayoutWidget_2);
  158. crypto_tagsToSendLineEdit->setObjectName(QStringLiteral("crypto_tagsToSendLineEdit"));
  159. crypto_tagsToSendLineEdit->setText(crypto_tagsToSend);
  160. crypto_tagsToSendLineEdit->setMaximumWidth(80);
  161. QObject::connect(crypto_tagsToSendLineEdit, SIGNAL(textChanged(const QString &)),
  162. this, SLOT(updated()));
  163. horizontalLayout_2->addWidget(crypto_tagsToSendLineEdit);
  164. QSpacerItem * horizontalSpacer = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum);
  165. horizontalLayout_2->addItem(horizontalSpacer);
  166. tunnelGridLayout->addLayout(horizontalLayout_2);
  167. }
  168. retranslateI2CPParameters();
  169. }
  170. void TunnelPane::updated() {
  171. std::string oldName=tunnelConfig->getName();
  172. //validate and show red if invalid
  173. hideWrongInputLabel();
  174. if(!mainWindow->applyTunnelsUiToConfigs())return;
  175. tunnelsPageUpdateListener->updated(oldName, tunnelConfig);
  176. }
  177. void TunnelPane::deleteButtonReleased() {
  178. QMessageBox msgBox;
  179. msgBox.setText(QApplication::tr("Are you sure to delete this tunnel?"));
  180. msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  181. msgBox.setDefaultButton(QMessageBox::Cancel);
  182. int ret = msgBox.exec();
  183. switch (ret) {
  184. case QMessageBox::Ok:
  185. // OK was clicked
  186. hideWrongInputLabel();
  187. tunnelsPageUpdateListener->needsDeleting(tunnelConfig->getName());
  188. break;
  189. case QMessageBox::Cancel:
  190. // Cancel was clicked
  191. return;
  192. }
  193. }
  194. /*
  195. const char I2P_TUNNELS_SECTION_TYPE_CLIENT[] = "client";
  196. const char I2P_TUNNELS_SECTION_TYPE_SERVER[] = "server";
  197. const char I2P_TUNNELS_SECTION_TYPE_HTTP[] = "http";
  198. const char I2P_TUNNELS_SECTION_TYPE_IRC[] = "irc";
  199. const char I2P_TUNNELS_SECTION_TYPE_UDPCLIENT[] = "udpclient";
  200. const char I2P_TUNNELS_SECTION_TYPE_UDPSERVER[] = "udpserver";
  201. const char I2P_TUNNELS_SECTION_TYPE_SOCKS[] = "socks";
  202. const char I2P_TUNNELS_SECTION_TYPE_WEBSOCKS[] = "websocks";
  203. const char I2P_TUNNELS_SECTION_TYPE_HTTPPROXY[] = "httpproxy";
  204. */
  205. QString TunnelPane::readTunnelTypeComboboxData() {
  206. return tunnelTypeComboBox->currentData().toString();
  207. }
  208. i2p::data::SigningKeyType TunnelPane::readSigTypeComboboxUI(QComboBox* sigTypeComboBox) {
  209. return (i2p::data::SigningKeyType) sigTypeComboBox->currentData().toInt();
  210. }
  211. void TunnelPane::deleteTunnelForm() {
  212. widgetlocks.deleteListeners();
  213. }
  214. void TunnelPane::highlightWrongInput(QString warningText, QWidget* controlWithWrongInput) {
  215. wrongInputPane->setVisible(true);
  216. wrongInputLabel->setText(warningText);
  217. mainWindow->adjustSizesAccordingToWrongLabel();
  218. if(controlWithWrongInput){
  219. mainWindow->ui->tunnelsScrollArea->ensureWidgetVisible(controlWithWrongInput);
  220. controlWithWrongInput->setFocus();
  221. }
  222. mainWindow->showTunnelsPage();
  223. }
  224. void TunnelPane::hideWrongInputLabel() const {
  225. wrongInputPane->setVisible(false);
  226. mainWindow->adjustSizesAccordingToWrongLabel();
  227. }