comparison 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
comparison
equal deleted inserted replaced
23:16be237acf13 24:bb93f0593aeb
41 *************************************************************************************/ 41 *************************************************************************************/
42 42
43 #include <QtGui> 43 #include <QtGui>
44 #include <QStringList> 44 #include <QStringList>
45 #include <QDir> 45 #include <QDir>
46 #include <QNetworkInterface>
46 #include <QHostAddress> 47 #include <QHostAddress>
47 #include <QHostInfo> 48 #include <QHostInfo>
48 49
49 #include "mainwindow.h" 50 #include "mainwindow.h"
50 #include "settingsdialog.h" 51 #include "settingsdialog.h"
544 runningAction = ACT_PUSH; 545 runningAction = ACT_PUSH;
545 } 546 }
546 } 547 }
547 548
548 549
549 QString MainWindow::findMyIp(bool& isAddrIpV6) 550 QString MainWindow::findMyIp()
550 { 551 {
551 QString ret; 552 QString ipAddr;
552 QHostInfo info = QHostInfo::fromName(QHostInfo::localHostName()); 553
553 QList <QHostAddress> ipList = info.addresses(); 554 QList <QHostAddress> ipList = QNetworkInterface::allAddresses();
554 555
555 556
556 if (!ipList.isEmpty()) 557 if (!ipList.isEmpty())
557 { 558 {
558 QHostAddress addr = ipList.at(0); 559 for(int i = 0; i < ipList.size(); i++)
559 560 {
560 if (QAbstractSocket::IPv6Protocol == addr.protocol()) 561 QHostAddress addr = ipList.at(i);
561 { 562
562 isAddrIpV6 = true; 563 if (QAbstractSocket::IPv4Protocol == addr.protocol())
563 } 564 {
564 else 565 if (addr != QHostAddress::LocalHost)
565 { 566 {
566 isAddrIpV6 = false; 567 return addr.toString();
567 } 568 }
568 569 }
569 ret = addr.toString(); 570 }
570 } 571 }
571 else 572
572 { 573 //This won't help your teammate much but is funny ;-)
573 //This won't help your teammate much, but is funny ;-) 574 return "127.0.0.1";
574 ret = "127.0.0.1";
575 isAddrIpV6 = false;
576 }
577
578 return ret;
579 } 575 }
580 576
581 void MainWindow::hgServe() 577 void MainWindow::hgServe()
582 { 578 {
583 if (runningAction == ACT_NONE) 579 if (runningAction == ACT_NONE)
584 { 580 {
585 QStringList params; 581 QStringList params;
586 QString msg; 582 QString msg;
587 bool isIpv6; 583
588 584 QString addr = findMyIp();
589 QString addr = findMyIp(isIpv6); 585 QTextStream(&msg) << "Server running on http://" << addr << ":8000/";
590 if (isIpv6) 586 params << "serve";
591 {
592 QTextStream(&msg) << "Server running on http://[" << addr << "]:8000/";
593 params << "serve" << "--ipv6";
594 }
595 else
596 {
597 QTextStream(&msg) << "Server running on http://" << addr << ":8000/";
598 params << "serve";
599 }
600 587
601 runner -> startProc(getHgBinaryName(), workFolderPath, params, false); 588 runner -> startProc(getHgBinaryName(), workFolderPath, params, false);
602 runningAction = ACT_SERVE; 589 runningAction = ACT_SERVE;
603 590
604 QMessageBox::information(this, "Serve", msg, QMessageBox::Close); 591 QMessageBox::information(this, "Serve", msg, QMessageBox::Close);