QTfrontend/ui/page/pagenetserver.cpp
branchwebgl
changeset 8833 c13ebed437cb
parent 8749 1b9f026e9fc6
child 8785 b26bc69022f7
equal deleted inserted replaced
8450:404ddce27b23 8833:c13ebed437cb
    22 #include <QPushButton>
    22 #include <QPushButton>
    23 #include <QGroupBox>
    23 #include <QGroupBox>
    24 #include <QLabel>
    24 #include <QLabel>
    25 #include <QLineEdit>
    25 #include <QLineEdit>
    26 #include <QSpinBox>
    26 #include <QSpinBox>
       
    27 #include <QTcpSocket>
       
    28 #include <QHostAddress>
       
    29 #include <QClipboard>
    27 
    30 
    28 #include "pagenetserver.h"
    31 #include "pagenetserver.h"
       
    32 #include "hwconsts.h"
       
    33 #include "HWApplication.h"
    29 
    34 
    30 QLayout * PageNetServer::bodyLayoutDefinition()
    35 QLayout * PageNetServer::bodyLayoutDefinition()
    31 {
    36 {
    32     QVBoxLayout * pageLayout = new QVBoxLayout();
    37     QVBoxLayout * pageLayout = new QVBoxLayout();
    33 
    38 
    57     labelPort = new QLabel(gb);
    62     labelPort = new QLabel(gb);
    58     labelPort->setText(QLabel::tr("Server port:"));
    63     labelPort->setText(QLabel::tr("Server port:"));
    59     gbLayout->addWidget(labelPort, 1, 0);
    64     gbLayout->addWidget(labelPort, 1, 0);
    60 
    65 
    61     sbPort = new QSpinBox(gb);
    66     sbPort = new QSpinBox(gb);
    62     sbPort->setMinimum(0);
    67     sbPort->setMinimum(1024);
    63     sbPort->setMaximum(65535);
    68     sbPort->setMaximum(65535);
    64     gbLayout->addWidget(sbPort, 1, 1);
    69     gbLayout->addWidget(sbPort, 1, 1);
    65 
    70 
    66     BtnDefault = new QPushButton(gb);
    71     BtnDefault = new QPushButton(gb);
    67     BtnDefault->setText(QPushButton::tr("default"));
    72     BtnDefault->setMinimumWidth(50);
       
    73     BtnDefault->setText(QPushButton::tr("Reset"));
       
    74     BtnDefault->setWhatsThis(QPushButton::tr("Set the default server port for Hedgewars"));
    68     gbLayout->addWidget(BtnDefault, 1, 2);
    75     gbLayout->addWidget(BtnDefault, 1, 2);
       
    76 
       
    77     BtnShare = new QPushButton(gb);
       
    78     BtnShare->setText(QPushButton::tr("Invite your friends to your server in just 1 click!"));
       
    79     BtnShare->setWhatsThis(QPushButton::tr("Click to copy your unique server URL in your clipboard. Send this link to your friends ands and they will be able to join you."));
       
    80     gbLayout->addWidget(BtnShare, 2, 1);
    69 
    81 
    70     return pageLayout;
    82     return pageLayout;
    71 }
    83 }
    72 
    84 
    73 QLayout * PageNetServer::footerLayoutDefinition()
    85 QLayout * PageNetServer::footerLayoutDefinition()
    74 {
    86 {
    75     QHBoxLayout * bottomLayout = new QHBoxLayout();
    87     QHBoxLayout * bottomLayout = new QHBoxLayout();
    76 
    88 
    77     BtnStart = formattedButton(QPushButton::tr("Start"));
    89     BtnStart = formattedButton(QPushButton::tr("Start"));
       
    90     BtnStart->setWhatsThis(QPushButton::tr("Start private server"));
    78     BtnStart->setMinimumWidth(180);
    91     BtnStart->setMinimumWidth(180);
    79 
    92 
    80     bottomLayout->addStretch();
    93     bottomLayout->addStretch();
    81     bottomLayout->addWidget(BtnStart);
    94     bottomLayout->addWidget(BtnStart);
    82 
    95 
    84 }
    97 }
    85 
    98 
    86 void PageNetServer::connectSignals()
    99 void PageNetServer::connectSignals()
    87 {
   100 {
    88     connect(BtnDefault, SIGNAL(clicked()), this, SLOT(setDefaultPort()));
   101     connect(BtnDefault, SIGNAL(clicked()), this, SLOT(setDefaultPort()));
       
   102     connect(BtnShare, SIGNAL(clicked()), this, SLOT(copyUrl()));
    89 }
   103 }
    90 
   104 
    91 PageNetServer::PageNetServer(QWidget* parent) : AbstractPage(parent)
   105 PageNetServer::PageNetServer(QWidget* parent) : AbstractPage(parent)
    92 {
   106 {
    93     initPage();
   107     initPage();
    94 }
   108 }
    95 
   109 
    96 void PageNetServer::setDefaultPort()
   110 void PageNetServer::setDefaultPort()
    97 {
   111 {
    98     sbPort->setValue(46631);
   112     sbPort->setValue(NETGAME_DEFAULT_PORT);
    99 }
   113 }
       
   114 
       
   115 // This function assumes that the user wants to share his server while connected to
       
   116 // the Internet and that he/she is using direct access (eg no NATs). To determine the
       
   117 // IP we briefly connect to Hedgewars website and fallback to user intervention 
       
   118 // after 4 seconds of timeout.
       
   119 void PageNetServer::copyUrl()
       
   120 {
       
   121     QString address = "hwplay://";
       
   122 
       
   123     QTcpSocket socket;
       
   124     socket.connectToHost("www.hedgewars.org", 80);
       
   125     if (socket.waitForConnected(4000))
       
   126         address += socket.localAddress().toString();
       
   127     else
       
   128         address += "<" + tr("Insert your address here") + ">";
       
   129 
       
   130     if (sbPort->value() != NETGAME_DEFAULT_PORT)
       
   131         address += ":" + QString::number(sbPort->value());
       
   132 
       
   133     QClipboard *clipboard = HWApplication::clipboard();
       
   134     clipboard->setText(address);
       
   135     qDebug() << address << "copied to clipboard";
       
   136 }
       
   137