add one button to improve sharing of hwplay:// schemes
authorkoda
Sun, 17 Mar 2013 22:02:57 +0100
changeset 8749 1b9f026e9fc6
parent 8748 fbd7f0b21e12
child 8750 d9e57426e759
child 8751 4609823efc94
add one button to improve sharing of hwplay:// schemes
QTfrontend/ui/page/pagenetserver.cpp
QTfrontend/ui/page/pagenetserver.h
--- a/QTfrontend/ui/page/pagenetserver.cpp	Sun Mar 17 20:50:02 2013 +0100
+++ b/QTfrontend/ui/page/pagenetserver.cpp	Sun Mar 17 22:02:57 2013 +0100
@@ -24,9 +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()
 {
@@ -70,6 +74,11 @@
     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;
 }
 
@@ -90,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)
@@ -101,3 +111,27 @@
 {
     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";
+}
+
--- a/QTfrontend/ui/page/pagenetserver.h	Sun Mar 17 20:50:02 2013 +0100
+++ b/QTfrontend/ui/page/pagenetserver.h	Sun Mar 17 22:02:57 2013 +0100
@@ -30,6 +30,7 @@
 
         QPushButton *BtnStart;
         QPushButton *BtnDefault;
+        QPushButton *BtnShare;
         QLabel *labelSD;
         QLineEdit *leServerDescr;
         QLabel *labelPort;
@@ -42,6 +43,7 @@
 
     private slots:
         void setDefaultPort();
+        void copyUrl();
 };
 
 #endif