OSDN Git Service

Added a flag for debug.
authorHiroaki Yamamoto <admin@hysoftware.net>
Sun, 17 Oct 2010 06:40:52 +0000 (15:40 +0900)
committerHiroaki Yamamoto <admin@hysoftware.net>
Sun, 17 Oct 2010 06:40:52 +0000 (15:40 +0900)
CMakeLists.txt
network/tcpnetwork.cxx
network/tcpnetwork.h
ui/jasmine_mainwindow.cxx
ui/memberlist.cxx
ui/memberlist.h
ui/rtfeditor.cxx

index 88109f5..67c3f86 100644 (file)
@@ -1,11 +1,15 @@
-cmake_minimum_required(VERSION 2.6)
+cmake_minimum_required(VERSION 2.8)
 project(jasmine)
 find_package(Qt4 4.5 REQUIRED QtCore QtGui QtNetwork)
 find_package(OpenMP)
 add_definitions(${QT_DEFINITIONS})
 include_directories(${CMAKE_CURRENT_BINARY_DIR} ${QT_INCLUDE_DIR})
 include(${QT_USE_FILE})
-# 
+if(CMAKE_BUILD_TYPE STREQUAL Debug)
+       message(STATUS "Debugging mode is being enabled. This is not recommended for normal use.")
+       add_definitions(-DDEBUG)
+       set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -Wall)
+endif(CMAKE_BUILD_TYPE STREQUAL Debug)
 #No mocs
 set(main main.cxx settings.cxx)
 set(validator
index c667778..ad946db 100644 (file)
@@ -15,7 +15,7 @@ void tcpServer::incomingConnection(int handle){
                delete socket;
                return;
        }
-       emit (emit this->pending(*socket))?this->newConnection():socket->disconnectFromHost();
+       emit (emit this->pending(*socket))?this->newConnection():socket->abort();
 }
 
 tcpSocket::tcpSocket(quint64 buffersize, QObject *parent):QTcpSocket(parent){
@@ -31,7 +31,9 @@ tcpSocket::tcpSocket(const QString &senderName,quint64 buffersize,QObject *paren
 }
 void tcpSocket::read_data(){
        while(this->bytesAvailable()>0){
+#ifdef DEBUG
                qDebug()<<"Server Event Mode:"<<this->event<<"Server Available bytes:"<<this->bytesAvailable();
+#endif
                switch(this->event){
                case tcpSocket::headsize:                this->size_event();            break;
                case tcpSocket::header_receive:  this->header_event();          break;
@@ -76,7 +78,7 @@ void tcpSocket::header_event(){
        this->event=tcpSocket::data;
 }
 void tcpSocket::data_event(){
-       if(this->bytesAvailable()<this->header_data().datasize()) return;
+       if((quint64)this->bytesAvailable()<this->header_data().datasize()) return;
 
        quint64 final_readsize=this->head_data.datasize()%this->buffer_size,
        read_count=(this->head_data.datasize()-final_readsize)/this->buffer_size;
@@ -161,7 +163,7 @@ tcpSocket &tcpSocket::operator<<(const QString &msg){
        tmp_buffer=msg.toUtf8();
        QBuffer memoryStream(&tmp_buffer,this);
        if(!memoryStream.open(QIODevice::ReadOnly)){
-               this->setErrorString("Memory Stream couldn't open.");
+               this->setErrorString(tr("Memory Stream couldn't open."));
                emit this->error(QAbstractSocket::UnknownSocketError);
                return (*this);
        }
@@ -172,7 +174,8 @@ tcpSocket &tcpSocket::operator<<(const QString &msg){
        emit this->sentData();
        return (*this);
 }
-tcpSocket &tcpSocket::operator<<(QFile &file){
+tcpSocket &tcpSocket::operator<<(QFile &src_file){
+       QFile file(src_file.fileName(),this);
        if(!this->state()!=QAbstractSocket::ConnectedState)return (*this);
        this->head_data=header(this->senderName,QFileInfo(file));
        QByteArray tmp_buffer;
@@ -184,8 +187,16 @@ tcpSocket &tcpSocket::operator<<(QFile &file){
        this->write(tmp_buffer);
        if(!this->flush())return (*this);
        tmp_buffer.clear();
-       while(this->write(file.read(this->buffer_size))>0)
+       emit this->file_header_sent();
+       if(!file.open(QIODevice::ReadOnly)){
+               this->setErrorString(tr("The file stream couldn't open."));
+               emit this->error(QAbstractSocket::UnknownSocketError);
+               return (*this);
+       }
+       while(this->write(file.read(this->buffer_size))>0&&!this->check_canceled_then_abort()){
                if(!this->flush())return (*this);
+               emit this->sending_file_progress(file.pos());
+       }
        emit this->sentData();
        return (*this);
 }
index 082bac0..77c882f 100644 (file)
@@ -31,7 +31,7 @@ namespace network{
                tcpSocket(const QString &senderName,quint64 buffersize=default_buffer_size,QObject *parent=NULL);
                QString path_to_save() const;
                structures::header header_data() const;
-
+               //Calling operator<<(QFile), the specified file stream will be copied.
                tcpSocket &operator<<(const QString &),
                                  &operator<<(QFile &);
        signals:
@@ -42,7 +42,10 @@ namespace network{
 
                void file_receive_progress(const quint64 streamPos) const;
                void file_saved() const;
+
                void sentData();
+               void file_header_sent();
+               void sending_file_progress(const quint64 pos);
        private slots:
                void read_data();
                void cancel();
index 6741b73..89ba54d 100644 (file)
@@ -89,8 +89,11 @@ void mainWindow::on_sendButton_clicked(){
        }
 }
 void mainWindow::tcpclient_error(const QAbstractSocket::SocketError &error){
+       Q_UNUSED(error);
        tcpSocket *client=qobject_cast<tcpSocket *>(this->sender());
+#ifdef DEBUG
        qDebug()<<"Error:"<<client->errorString();
+#endif
        client->close();
 }
 
@@ -293,6 +296,9 @@ settings mainWindow::app_setting() const{return this->setting;}
 
 //Implementations for main server.
 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;
        connect(&socket,
                        SIGNAL(msg_received(const QString &)),
@@ -302,13 +308,17 @@ bool mainWindow::tcpserver_pending(const tcpSocket &socket){
        return true;
 }
 void mainWindow::tcpserver_error(const QAbstractSocket::SocketError &error){
+       Q_UNUSED(error);
        tcpSocket *sender=qobject_cast<tcpSocket *>(this->sender());
+#ifdef DEBUG
        qDebug()<<"Receive aborted:"<<sender->errorString();
+#endif
+       sender->close();
 }
 
 void mainWindow::tcpserver_msg_received(const QString &msg){
        tcpSocket *sender=qobject_cast<tcpSocket *>(this->sender());
-       this->receiveText->append(sender->header_data().senderName()+"("+sender->peerAddress().toString()+")"+tr(" says:"));
-       this->receiveText->append(msg+"\n");
-       //this->receiveText->append("<br />");
+       this->receiveText->append(sender->header_data().senderName()+"("+this->memberList->name(AddressAndPort(sender->peerAddress(),0),true)+", "+sender->peerAddress().toString()+")"+tr(" says:"));
+       this->receiveText->append(msg);
+       this->receiveText->append("<br />");
 }
index acfde87..e74f7c2 100644 (file)
@@ -27,10 +27,21 @@ QList<AddressAndPort> MemberList::addressPortList() const{
        }
        return ret;
 }
-QStringList MemberList::name() const{
-       QStringList ret;
-       for(int row=0;row<this->rowCount();row++) ret<<this->item(row,NameIndex)->text();
-       return ret;
+int MemberList::isInMember(const AddressAndPort &peer,bool matchIPOnly)const{
+       for(int row=0;row<this->rowCount();row++){
+               QStringList address_port_str=this->item(row,AddressIndex)->text().split(":");
+               if(address_port_str.size()<2) address_port_str<<QString("%1").arg(default_port);
+               if(address_port_str[1].isEmpty()) address_port_str[1]=QString("%1").arg(default_port);
+               if(matchIPOnly&&address_port_str[0]==peer.first.toString()) return row;
+               else if(address_port_str[0]==peer.first.toString()&&
+                               address_port_str[1]==QString::number(peer.second))
+                       return row;
+       }
+       return -1;
+}
+QString MemberList::name(const AddressAndPort &addressport, bool matchAddressOnly) const{
+       int index=this->isInMember(addressport,matchAddressOnly);
+       return this->item(index,NameIndex)->text();
 }
 void MemberList::remove_selected(){
        while(this->selectedItems().size()>0&&this->selectedRanges()[0].rowCount()>0)
index 1494216..59fcf75 100644 (file)
@@ -10,7 +10,8 @@ public slots:
        void remove_selected();
        void remove_all_contents();
        QList<AddressAndPort> addressPortList() const;
-       QStringList name() const;
+       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;
                for(int row=0;row<value.rowCount();row++){
index 6bccf38..f17b2e9 100644 (file)
@@ -22,7 +22,9 @@ RtfEditor::RtfEditor(QWidget *parent):QWidget(parent){
        this->italic->setCheckable(true);
        this->link->setCheckable(true);
        this->color->setCheckable(true);
+#ifdef DEBUG
        qDebug()<<"Rtf editor has initialized. When you press any key(s), keysym will be output to stdout.";
+#endif
 }
 void RtfEditor::selectionChnged(){
        this->bold->setChecked(this->editor->currentFont().bold());
@@ -108,7 +110,9 @@ void RtfEditor::focusInEvent(QFocusEvent *event){
        event->accept();
 }
 void RtfEditor::keyPressEvent(QKeyEvent *event){
+#ifdef DEBUG
        qDebug()<<hex<<event->modifiers()<<":"<<event->key();
+#endif
        if(event->modifiers()==Qt::ControlModifier&&(event->key()==Qt::Key_Enter||event->key()==Qt::Key_Return)) emit this->sendTriggered();
        event->accept();
 }