# HG changeset patch # User koda # Date 1363554177 -3600 # Node ID 1b9f026e9fc6717faa7d0110a0cbceef522649ff # Parent fbd7f0b21e124c2dd4f4896367c4f91bd5fb6238 add one button to improve sharing of hwplay:// schemes diff -r fbd7f0b21e12 -r 1b9f026e9fc6 QTfrontend/ui/page/pagenetserver.cpp --- 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 #include #include +#include +#include +#include #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"; +} + diff -r fbd7f0b21e12 -r 1b9f026e9fc6 QTfrontend/ui/page/pagenetserver.h --- 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