OSDN Git Service

Fixed memberlist format.
authorHiroaki Yamamoto <admin@hysoftware.net>
Mon, 18 Oct 2010 11:07:27 +0000 (20:07 +0900)
committerHiroaki Yamamoto <admin@hysoftware.net>
Mon, 18 Oct 2010 11:07:27 +0000 (20:07 +0900)
Before this version, QHash<QString, AddressAndPort> is used to save memberlist. However, QHash sorts the data.
And so, I used QPair to fix this problem.

definition.h
ui/jasmine_mainwindow.cxx
ui/memberlist.h

index c86bd60..4fa8558 100644 (file)
@@ -8,3 +8,4 @@
 #define default_status_interval 3000
 #define default_buffer_size 1024
 typedef QPair<QHostAddress,quint16> AddressAndPort;
+typedef QPair<QString,AddressAndPort> NameAndAddrPort;
index 89ba54d..a4e9eb1 100644 (file)
@@ -66,14 +66,6 @@ void mainWindow::showEvent(QShowEvent *event){
                this->status->showMessage("<font color=\"#ff0000\">Don't forget almost all features are not implemented!</font>",default_status_interval);
        event->accept();
 }
-bool mainWindow::isInMember(const AddressAndPort &peer,bool matchIPOnly){
-       QList<AddressAndPort> iplist=this->memberList->addressPortList();
-       foreach(AddressAndPort addressPort,iplist){
-               if(matchIPOnly&&peer.first==addressPort.first) return true;
-               else if(peer==addressPort) return true;
-       }
-       return false;
-}
 
 //Client behavior
 void mainWindow::on_sendButton_clicked(){
@@ -299,7 +291,7 @@ bool mainWindow::tcpserver_pending(const tcpSocket &socket){
 #ifdef DEBUG
        qDebug()<<"Pending:"<<socket.peerAddress().toString()<<" Port:"<<socket.peerPort();
 #endif
-       if(!this->isInMember(AddressAndPort(socket.peerAddress(),0),true)) return false;
+       if(this->memberList->isInMember(AddressAndPort(socket.peerAddress(),0),true)<0) return false;
        connect(&socket,
                        SIGNAL(msg_received(const QString &)),
                        SLOT(tcpserver_msg_received(const QString &)));
index 59fcf75..3519088 100644 (file)
@@ -2,6 +2,9 @@
 #include <QtGui>
 #include <QtNetwork>
 #include "../definition.h"
+
+#define AddressIndex 1
+#define NameIndex 0
 class MemberList:virtual public QTableWidget{
        Q_OBJECT
 public:
@@ -13,27 +16,27 @@ public slots:
        QString name(const AddressAndPort&,bool) const;
        int isInMember(const AddressAndPort &peer,bool matchIPOnly)const;
        friend QDataStream &operator<<(QDataStream &out,const MemberList &value){
-               QHash<QString,AddressAndPort> AddressList;
+               QList<NameAndAddrPort> AddressList;
                for(int row=0;row<value.rowCount();row++){
-                       QStringList AddrAndPort=value.item(row,1)->text().split(":");
-                       AddressList.insertMulti(value.item(row,0)->text(),AddressAndPort(QHostAddress(AddrAndPort[0]),(AddrAndPort.count()==2)? AddrAndPort[1].toUShort():default_port));
+                       QStringList AddrAndPort=value.item(row,AddressIndex)->text().split(":");
+                       AddressList<<NameAndAddrPort(value.item(row,NameIndex)->text(),AddressAndPort(QHostAddress(AddrAndPort[0]),(AddrAndPort.count()==2)? AddrAndPort[1].toUShort():default_port));
                }
                out<<AddressList;
                return out;
        }
 
        friend QDataStream &operator>>(QDataStream &in, MemberList &value){
-               QHash<QString,AddressAndPort> AddressList;
+               QList<NameAndAddrPort> AddressList;
                in>>AddressList;
                value.remove_all_contents();
-               for(QHash<QString,AddressAndPort>::iterator index=AddressList.begin();index!=AddressList.end();index++){
-                       QTableWidgetItem *name=new QTableWidgetItem(index.key()),
+               foreach(NameAndAddrPort addressList,AddressList){
+                       QTableWidgetItem *name=new QTableWidgetItem(addressList.first),
                        *AddressPort=new QTableWidgetItem(QString("%1:%2").
-                                                                                         arg(index.value().first.toString()).
-                                                                                         arg(index.value().second));
+                                                                                         arg(addressList.second.first.toString()).
+                                                                                         arg(addressList.second.second));
                        value.insertRow(value.rowCount());
-                       value.setItem(value.rowCount()-1,0,name);
-                       value.setItem(value.rowCount()-1,1,AddressPort);
+                       value.setItem(value.rowCount()-1,NameIndex,name);
+                       value.setItem(value.rowCount()-1,AddressIndex,AddressPort);
                }
                return in;
        }