comparison mainwindow.cpp @ 28:90ffef5f0494

Another attempt to list hg serve ip addresses ;-)
author Jari Korhonen <jtkorhonen@gmail.com>
date Fri, 28 May 2010 22:34:44 +0300
parents 6e8cd5ffa4df
children fad897ed9894
comparison
equal deleted inserted replaced
27:6e8cd5ffa4df 28:90ffef5f0494
546 } 546 }
547 } 547 }
548 548
549 549
550 550
551 void MainWindow::listAllUpAddresses() 551 QString MainWindow::listAllUpIpV4Addresses()
552 { 552 {
553 QString ret;
553 QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); 554 QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces();
554 555
555 for (int i = 0; i < ifaces.count(); i++) 556 for (int i = 0; i < ifaces.count(); i++)
556 { 557 {
557 QNetworkInterface iface = ifaces.at(i); 558 QNetworkInterface iface = ifaces.at(i);
558 559
560 if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack))
561 {
562 for (int j=0; j<iface.addressEntries().count(); j++)
563 {
564 QHostAddress tmp = iface.addressEntries().at(j).ip();
565 if (QAbstractSocket::IPv4Protocol == tmp.protocol())
566 {
567 if (!ret.isEmpty())
568 {
569 ret += " ";
570 }
571 ret += tmp.toString();
572 }
573 }
574 }
575 }
576 return ret;
577 }
578
579
580 void MainWindow::hgServe()
581 {
582 if (runningAction == ACT_NONE)
583 {
584 QStringList params;
559 QString msg; 585 QString msg;
560 586
561 QTextStream(&msg) << "name: " << iface.name() << endl << "mac: " << iface.hardwareAddress() << endl; 587 QString addrs = listAllUpIpV4Addresses();
562 QMessageBox::information(this, "interface", msg); 588 QTextStream(&msg) << "Server running on address(es) (" << addrs << "), port 8000";
563 589 params << "serve";
564 if (iface.flags().testFlag(QNetworkInterface::IsUp) && !iface.flags().testFlag(QNetworkInterface::IsLoopBack))
565 {
566 // this loop is important
567 for (int j=0; j<iface.addressEntries().count(); j++)
568 {
569 QMessageBox::information(this, iface.name(), iface.addressEntries().at(j).ip().toString());
570 }
571 }
572 }
573 }
574
575
576 QString MainWindow::findMyIp()
577 {
578 listAllUpAddresses();
579
580 QList <QHostAddress> ipList = QNetworkInterface::allAddresses();
581
582
583 if (!ipList.isEmpty())
584 {
585 for(int i = 0; i < ipList.size(); i++)
586 {
587 QHostAddress addr = ipList.at(i);
588
589 if (QAbstractSocket::IPv4Protocol == addr.protocol())
590 {
591 if (addr != QHostAddress::LocalHost)
592 {
593 return addr.toString();
594 }
595 }
596 }
597 }
598
599 //This won't help your teammate much but is funny ;-)
600 return "127.0.0.1";
601 }
602
603 void MainWindow::hgServe()
604 {
605 if (runningAction == ACT_NONE)
606 {
607 QStringList params;
608 QString msg;
609
610 QString addr = findMyIp();
611 QTextStream(&msg) << "Server running on http://" << addr << ":8000/";
612 params << "serve" << "--address" << addr << "--port" << "8000";
613 590
614 runner -> startProc(getHgBinaryName(), workFolderPath, params, false); 591 runner -> startProc(getHgBinaryName(), workFolderPath, params, false);
615 runningAction = ACT_SERVE; 592 runningAction = ACT_SERVE;
616 593
617 QMessageBox::information(this, "Serve", msg, QMessageBox::Close); 594 QMessageBox::information(this, "Serve", msg, QMessageBox::Close);