QTfrontend/ui/page/pagenetserver.cpp
branchwebgl
changeset 8833 c13ebed437cb
parent 8749 1b9f026e9fc6
child 8785 b26bc69022f7
--- a/QTfrontend/ui/page/pagenetserver.cpp	Wed Feb 20 02:21:58 2013 +0100
+++ b/QTfrontend/ui/page/pagenetserver.cpp	Tue Apr 02 21:00:57 2013 +0200
@@ -24,8 +24,13 @@
 #include <QLabel>
 #include <QLineEdit>
 #include <QSpinBox>
+#include <QTcpSocket>
+#include <QHostAddress>
+#include <QClipboard>
 
 #include "pagenetserver.h"
+#include "hwconsts.h"
+#include "HWApplication.h"
 
 QLayout * PageNetServer::bodyLayoutDefinition()
 {
@@ -59,14 +64,21 @@
     gbLayout->addWidget(labelPort, 1, 0);
 
     sbPort = new QSpinBox(gb);
-    sbPort->setMinimum(0);
+    sbPort->setMinimum(1024);
     sbPort->setMaximum(65535);
     gbLayout->addWidget(sbPort, 1, 1);
 
     BtnDefault = new QPushButton(gb);
-    BtnDefault->setText(QPushButton::tr("default"));
+    BtnDefault->setMinimumWidth(50);
+    BtnDefault->setText(QPushButton::tr("Reset"));
+    BtnDefault->setWhatsThis(QPushButton::tr("Set the default server port for Hedgewars"));
     gbLayout->addWidget(BtnDefault, 1, 2);
 
+    BtnShare = new QPushButton(gb);
+    BtnShare->setText(QPushButton::tr("Invite your friends to your server in just 1 click!"));
+    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."));
+    gbLayout->addWidget(BtnShare, 2, 1);
+
     return pageLayout;
 }
 
@@ -75,6 +87,7 @@
     QHBoxLayout * bottomLayout = new QHBoxLayout();
 
     BtnStart = formattedButton(QPushButton::tr("Start"));
+    BtnStart->setWhatsThis(QPushButton::tr("Start private server"));
     BtnStart->setMinimumWidth(180);
 
     bottomLayout->addStretch();
@@ -86,6 +99,7 @@
 void PageNetServer::connectSignals()
 {
     connect(BtnDefault, SIGNAL(clicked()), this, SLOT(setDefaultPort()));
+    connect(BtnShare, SIGNAL(clicked()), this, SLOT(copyUrl()));
 }
 
 PageNetServer::PageNetServer(QWidget* parent) : AbstractPage(parent)
@@ -95,5 +109,29 @@
 
 void PageNetServer::setDefaultPort()
 {
-    sbPort->setValue(46631);
+    sbPort->setValue(NETGAME_DEFAULT_PORT);
 }
+
+// This function assumes that the user wants to share his server while connected to
+// the Internet and that he/she is using direct access (eg no NATs). To determine the
+// IP we briefly connect to Hedgewars website and fallback to user intervention 
+// after 4 seconds of timeout.
+void PageNetServer::copyUrl()
+{
+    QString address = "hwplay://";
+
+    QTcpSocket socket;
+    socket.connectToHost("www.hedgewars.org", 80);
+    if (socket.waitForConnected(4000))
+        address += socket.localAddress().toString();
+    else
+        address += "<" + tr("Insert your address here") + ">";
+
+    if (sbPort->value() != NETGAME_DEFAULT_PORT)
+        address += ":" + QString::number(sbPort->value());
+
+    QClipboard *clipboard = HWApplication::clipboard();
+    clipboard->setText(address);
+    qDebug() << address << "copied to clipboard";
+}
+