diff mainwindow.cpp @ 24:bb93f0593aeb

Fixed findMyIp().
author Jari Korhonen <jtkorhonen@gmail.com>
date Wed, 19 May 2010 22:57:05 +0300
parents 465fbf9ad5a9
children eba979148fb3 ee34bd26751e
line wrap: on
line diff
--- a/mainwindow.cpp	Tue May 18 23:56:55 2010 +0300
+++ b/mainwindow.cpp	Wed May 19 22:57:05 2010 +0300
@@ -43,6 +43,7 @@
 #include <QtGui>
 #include <QStringList>
 #include <QDir>
+#include <QNetworkInterface>
 #include <QHostAddress>
 #include <QHostInfo>
 
@@ -546,36 +547,31 @@
 }
 
 
-QString MainWindow::findMyIp(bool& isAddrIpV6)
+QString MainWindow::findMyIp()
 {
-    QString ret;
-    QHostInfo info = QHostInfo::fromName(QHostInfo::localHostName());
-    QList <QHostAddress> ipList = info.addresses();
+    QString ipAddr;
+
+    QList <QHostAddress> ipList = QNetworkInterface::allAddresses();
 
 
     if (!ipList.isEmpty())
     {
-        QHostAddress addr = ipList.at(0);
+        for(int i = 0; i < ipList.size(); i++)
+        {
+            QHostAddress addr = ipList.at(i);
 
-        if (QAbstractSocket::IPv6Protocol == addr.protocol())
-        {
-            isAddrIpV6 = true;
+            if (QAbstractSocket::IPv4Protocol == addr.protocol())
+            {
+                if (addr != QHostAddress::LocalHost)
+                {
+                    return addr.toString();
+                }
+            }
         }
-        else
-        {
-            isAddrIpV6 = false;
-        }
-
-        ret = addr.toString();
-    }
-    else
-    {
-        //This won't help your teammate much, but is funny ;-)
-        ret = "127.0.0.1";
-        isAddrIpV6 = false;
     }
 
-    return ret;
+    //This won't help your teammate much but is funny ;-)
+    return "127.0.0.1";
 }
 
 void MainWindow::hgServe()
@@ -584,19 +580,10 @@
     {
         QStringList params;
         QString msg;
-        bool isIpv6;
 
-        QString addr = findMyIp(isIpv6);
-        if (isIpv6)
-        {
-            QTextStream(&msg) << "Server running on http://[" << addr << "]:8000/";
-            params << "serve" << "--ipv6";
-        }
-        else
-        {
-            QTextStream(&msg) << "Server running on http://" << addr << ":8000/";
-            params << "serve";
-        }
+        QString addr = findMyIp();
+        QTextStream(&msg) << "Server running on http://" << addr << ":8000/";
+        params << "serve";
 
         runner -> startProc(getHgBinaryName(), workFolderPath, params, false);
         runningAction = ACT_SERVE;