QTfrontend/ui/page/pagenetserver.cpp
changeset 8749 1b9f026e9fc6
parent 8748 fbd7f0b21e12
child 8785 b26bc69022f7
equal deleted inserted replaced
8748:fbd7f0b21e12 8749:1b9f026e9fc6
    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"
    29 #include "hwconsts.h"
    32 #include "hwconsts.h"
       
    33 #include "HWApplication.h"
    30 
    34 
    31 QLayout * PageNetServer::bodyLayoutDefinition()
    35 QLayout * PageNetServer::bodyLayoutDefinition()
    32 {
    36 {
    33     QVBoxLayout * pageLayout = new QVBoxLayout();
    37     QVBoxLayout * pageLayout = new QVBoxLayout();
    34 
    38 
    68     BtnDefault->setMinimumWidth(50);
    72     BtnDefault->setMinimumWidth(50);
    69     BtnDefault->setText(QPushButton::tr("Reset"));
    73     BtnDefault->setText(QPushButton::tr("Reset"));
    70     BtnDefault->setWhatsThis(QPushButton::tr("Set the default server port for Hedgewars"));
    74     BtnDefault->setWhatsThis(QPushButton::tr("Set the default server port for Hedgewars"));
    71     gbLayout->addWidget(BtnDefault, 1, 2);
    75     gbLayout->addWidget(BtnDefault, 1, 2);
    72 
    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);
       
    81 
    73     return pageLayout;
    82     return pageLayout;
    74 }
    83 }
    75 
    84 
    76 QLayout * PageNetServer::footerLayoutDefinition()
    85 QLayout * PageNetServer::footerLayoutDefinition()
    77 {
    86 {
    88 }
    97 }
    89 
    98 
    90 void PageNetServer::connectSignals()
    99 void PageNetServer::connectSignals()
    91 {
   100 {
    92     connect(BtnDefault, SIGNAL(clicked()), this, SLOT(setDefaultPort()));
   101     connect(BtnDefault, SIGNAL(clicked()), this, SLOT(setDefaultPort()));
       
   102     connect(BtnShare, SIGNAL(clicked()), this, SLOT(copyUrl()));
    93 }
   103 }
    94 
   104 
    95 PageNetServer::PageNetServer(QWidget* parent) : AbstractPage(parent)
   105 PageNetServer::PageNetServer(QWidget* parent) : AbstractPage(parent)
    96 {
   106 {
    97     initPage();
   107     initPage();
    99 
   109 
   100 void PageNetServer::setDefaultPort()
   110 void PageNetServer::setDefaultPort()
   101 {
   111 {
   102     sbPort->setValue(NETGAME_DEFAULT_PORT);
   112     sbPort->setValue(NETGAME_DEFAULT_PORT);
   103 }
   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