i2pd/qt/i2pd_qt/mainwindow.cpp

266 lines
9.4 KiB
C++
Raw Normal View History

2016-06-14 03:52:17 +03:00
#include "mainwindow.h"
2017-02-01 22:09:57 +03:00
#include "ui_mainwindow.h"
#include <QMessageBox>
2016-06-24 22:37:59 +03:00
#include <QTimer>
#include "RouterContext.h"
#ifndef ANDROID
#include <QtDebug>
#endif
2017-01-28 11:01:34 +03:00
#include <QScrollBar>
2016-06-14 03:52:17 +03:00
MainWindow::MainWindow(QWidget *parent) :
2017-02-01 22:09:57 +03:00
QMainWindow(parent),
ui(new Ui::MainWindow)
2016-06-26 15:32:36 +03:00
#ifndef ANDROID
2016-06-27 01:49:11 +03:00
,quitting(false)
2016-06-26 15:32:36 +03:00
#endif
2016-06-14 03:52:17 +03:00
{
2017-02-01 22:09:57 +03:00
ui->setupUi(this);
2017-02-01 22:03:09 +03:00
//TODO handle resizes and change the below into resize() call
2017-01-28 11:01:34 +03:00
setFixedSize(width(), 480);
onResize();
ui->stackedWidget->setCurrentIndex(0);
ui->settingsScrollArea->resize(ui->settingsContentsGridLayout->sizeHint().width()+10,ui->settingsScrollArea->height());
QScrollBar* const barSett = ui->settingsScrollArea->verticalScrollBar();
//QSize szSettContents = ui->settingsContentsGridLayout->minimumSize();
int w = 683;
int h = 3000;
ui->settingsContents->setFixedSize(w, h);
ui->settingsContents->resize(w, h);
//ui->settingsContents->adjustSize();
/*
QPalette pal(palette());
pal.setColor(QPalette::Background, Qt::red);
ui->settingsContents->setAutoFillBackground(true);
ui->settingsContents->setPalette(pal);
*/
//ui->settingsScrollArea->adjustSize();
ui->tunnelsScrollAreaWidgetContents->setFixedSize(
ui->tunnelsScrollArea->width() - barSett->width(), 0);
#ifndef ANDROID
createActions();
createTrayIcon();
#endif
2017-01-28 11:01:34 +03:00
QObject::connect(ui->statusPagePushButton, SIGNAL(released()), this, SLOT(showStatusPage()));
QObject::connect(ui->settingsPagePushButton, SIGNAL(released()), this, SLOT(showSettingsPage()));
QObject::connect(ui->tunnelsPagePushButton, SIGNAL(released()), this, SLOT(showTunnelsPage()));
QObject::connect(ui->restartPagePushButton, SIGNAL(released()), this, SLOT(showRestartPage()));
QObject::connect(ui->quitPagePushButton, SIGNAL(released()), this, SLOT(showQuitPage()));
2017-02-01 22:09:57 +03:00
QObject::connect(ui->fastQuitPushButton, SIGNAL(released()), this, SLOT(handleQuitButton()));
QObject::connect(ui->gracefulQuitPushButton, SIGNAL(released()), this, SLOT(handleGracefulQuitButton()));
2017-02-01 22:03:09 +03:00
initFileChooser(configFileLineEdit, configFileBrowsePushButton);
initFileChooser(tunnelsConfigFileLineEdit, tunnelsConfigFileBrowsePushButton);
initFileChooser(pidFileLineEdit, pidFileBrowsePushButton);
initFileChooser(logFileLineEdit, logFileBrowsePushButton);
initFileChooser(httpProxyKeyFileLineEdit, httpProxyKeyFilePushButton);
initFileChooser(socksProxyKeyFileLineEdit, socksProxyKeyFilePushButton);
initFileChooser(i2pControlCertFileLineEdit, i2pControlCertFileBrowsePushButton);
initFileChooser(i2pControlKeyFileLineEdit, i2pControlKeyFileBrowsePushButton);
initFileChooser(reseedFileLineEdit, reseedFileBrowsePushButton);
initFolderChooser(dataFolderLineEdit, dataFolderBrowsePushButton);
initCombobox(logLevelComboBox);
initIPAddressBox(routerExternalHostLineEdit, tr("Router external address -> Host"));
initTCPPortBox(routerExternalPortLineEdit, tr("Router external address -> Port"));
initCheckBox(ipv6CheckBox);
initCheckBox(notransitCheckBox);
initCheckBox(floodfillCheckBox);
initIntegerBox(bandwidthLineEdit);
initStringBox(familyLineEdit);
initIntegerBox(netIdLineEdit);
initCheckBox(insomniaCheckBox);
initCheckBox(webconsoleEnabledCheckBox);
initIPAddressBox(webconsoleAddrLineEdit, tr("HTTP webconsole -> IP address"));
initTCPPortBox(webconsolePortLineEdit, tr("HTTP webconsole -> Port"));
initCheckBox(webconsoleBasicAuthCheckBox);
initStringBox(webconsoleUserNameLineEditBasicAuth);
initStringBox(webconsolePasswordLineEditBasicAuth);
initCheckBox(httpProxyEnabledCheckBox);
initIPAddressBox(httpProxyAddressLineEdit, tr("HTTP proxy -> IP address"));
initTCPPortBox(httpProxyPortLineEdit, tr("HTTP proxy -> Port"));
initIntegerBox(httpProxyInboundTunnelsLenLineEdit);
initIntegerBox(httpProxyInboundTunnQuantityLineEdit);
initIntegerBox(httpProxyOutBoundTunnLenLineEdit);
initIntegerBox(httpProxyOutboundTunnQuantityLineEdit);
initCheckBox(socksProxyEnabledCheckBox);
initIPAddressBox(socksProxyAddressLineEdit, tr("Socks proxy -> IP address"));
initTCPPortBox(socksProxyPortLineEdit, tr("Socks proxy -> Port"));
initIntegerBox(socksProxyInboundTunnelsLenLineEdit);
initIntegerBox(socksProxyInboundTunnQuantityLineEdit);
initIntegerBox(socksProxyOutBoundTunnLenLineEdit);
initIntegerBox(socksProxyOutboundTunnQuantityLineEdit);
initIPAddressBox(outproxyAddressLineEdit, tr("Socks proxy -> Outproxy address"));
initTCPPortBox(outproxyPortLineEdit, tr("Socks proxy -> Outproxy port"));
initCheckBox(socksProxyEnabledCheckBox);
initIPAddressBox(socksProxyAddressLineEdit, tr("Socks proxy -> IP address"));
initTCPPortBox(socksProxyPortLineEdit, tr("Socks proxy -> Port"));
initCheckBox(socksProxyEnabledCheckBox);
initIPAddressBox(socksProxyAddressLineEdit, tr("Socks proxy -> IP address"));
initTCPPortBox(socksProxyPortLineEdit, tr("Socks proxy -> Port"));
initCheckBox(socksProxyEnabledCheckBox);
initIPAddressBox(socksProxyAddressLineEdit, tr("Socks proxy -> IP address"));
initTCPPortBox(socksProxyPortLineEdit, tr("Socks proxy -> Port"));
initCheckBox(socksProxyEnabledCheckBox);
initIPAddressBox(socksProxyAddressLineEdit, tr("Socks proxy -> IP address"));
initTCPPortBox(socksProxyPortLineEdit, tr("Socks proxy -> Port"));
loadAllConfigs();
#ifndef ANDROID
QObject::connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
setIcon();
trayIcon->show();
#endif
//QMetaObject::connectSlotsByName(this);
}
2017-01-28 11:01:34 +03:00
void MainWindow::showStatusPage(){ui->stackedWidget->setCurrentIndex(0);}
void MainWindow::showSettingsPage(){ui->stackedWidget->setCurrentIndex(1);}
void MainWindow::showTunnelsPage(){ui->stackedWidget->setCurrentIndex(2);}
void MainWindow::showRestartPage(){ui->stackedWidget->setCurrentIndex(3);}
void MainWindow::showQuitPage(){ui->stackedWidget->setCurrentIndex(4);}
2017-02-01 22:03:09 +03:00
//TODO
2017-01-28 11:01:34 +03:00
void MainWindow::resizeEvent(QResizeEvent *event)
{
QMainWindow::resizeEvent(event);
onResize();
}
2017-02-01 22:03:09 +03:00
//TODO
2017-01-28 11:01:34 +03:00
void MainWindow::onResize()
{
if(isVisible()){
ui->horizontalLayoutWidget->resize(ui->horizontalLayoutWidget->width(), height());
//status
ui->statusPage->resize(ui->statusPage->width(), height());
//tunnels
ui->tunnelsPage->resize(ui->tunnelsPage->width(), height());
ui->verticalLayoutWidget_6->resize(ui->verticalLayoutWidget_6->width(), height()-20);
/*ui->tunnelsScrollArea->resize(ui->tunnelsScrollArea->width(),
ui->verticalLayoutWidget_6->height()-ui->label_5->height());*/
}
}
#ifndef ANDROID
void MainWindow::createActions() {
toggleWindowVisibleAction = new QAction(tr("&Toggle the window"), this);
connect(toggleWindowVisibleAction, SIGNAL(triggered()), this, SLOT(toggleVisibilitySlot()));
//quitAction = new QAction(tr("&Quit"), this);
//connect(quitAction, SIGNAL(triggered()), QApplication::instance(), SLOT(quit()));
}
void MainWindow::toggleVisibilitySlot() {
setVisible(!isVisible());
}
void MainWindow::createTrayIcon() {
trayIconMenu = new QMenu(this);
trayIconMenu->addAction(toggleWindowVisibleAction);
//trayIconMenu->addSeparator();
//trayIconMenu->addAction(quitAction);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
}
void MainWindow::setIcon() {
QIcon icon(":/images/icon.png");
trayIcon->setIcon(icon);
setWindowIcon(icon);
trayIcon->setToolTip(QApplication::translate("MainWindow", "i2pd", 0));
}
void MainWindow::iconActivated(QSystemTrayIcon::ActivationReason reason) {
switch (reason) {
case QSystemTrayIcon::Trigger:
case QSystemTrayIcon::DoubleClick:
case QSystemTrayIcon::MiddleClick:
setVisible(!isVisible());
break;
default:
qDebug() << "MainWindow::iconActivated(): unknown reason: " << reason << endl;
break;
}
}
void MainWindow::closeEvent(QCloseEvent *event) {
if(quitting){ QMainWindow::closeEvent(event); return; }
if (trayIcon->isVisible()) {
QMessageBox::information(this, tr("i2pd"),
tr("The program will keep running in the "
"system tray. To gracefully terminate the program, "
"choose <b>Graceful Quit</b> at the main i2pd window."));
hide();
event->ignore();
}
}
#endif
void MainWindow::handleQuitButton() {
qDebug("Quit pressed. Hiding the main window");
#ifndef ANDROID
quitting=true;
#endif
close();
QApplication::instance()->quit();
2016-06-14 03:52:17 +03:00
}
2016-06-24 22:37:59 +03:00
void MainWindow::handleGracefulQuitButton() {
qDebug("Graceful Quit pressed.");
2017-01-28 11:01:34 +03:00
/*
2017-02-01 22:09:57 +03:00
ui->gracefulQuitPushButton->setText(QApplication::translate("MainWindow", "Graceful quit is in progress", 0));
ui->gracefulQuitPushButton->setEnabled(false);
ui->gracefulQuitPushButton->adjustSize();
ui->quitPage->adjustSize();
i2p::context.SetAcceptsTunnels (false); // stop accpting tunnels
2017-01-28 11:01:34 +03:00
QTimer::singleShot(10*60*1000//millis
, this, SLOT(handleGracefulQuitTimerEvent()));
*/
2016-06-24 22:37:59 +03:00
}
void MainWindow::handleGracefulQuitTimerEvent() {
qDebug("Hiding the main window");
#ifndef ANDROID
quitting=true;
#endif
2016-06-24 22:37:59 +03:00
close();
qDebug("Performing quit");
QApplication::instance()->quit();
}
2016-06-14 03:52:17 +03:00
MainWindow::~MainWindow()
{
qDebug("Destroying main window");
//QMessageBox::information(0, "Debug", "mw destructor 1");
//delete ui;
//QMessageBox::information(0, "Debug", "mw destructor 2");
2016-06-14 03:52:17 +03:00
}