void Widget::hasPendingFile(QString usrname, QString srvaddr, QString clntaddr, QString filename) { QString ipAddr = getIP(); if(ipAddr == clntaddr) { int btn = QMessageBox::information(this, tr("接收文件"), tr("来自%1(%2)的文件:%3,是否接收?") .arg(usrname) .arg(srvaddr) .arg(filename), QMessageBox::Yes, QMessageBox::No); if(btn == QMessageBox::Yes) { QString name = QFileDialog::getSaveFileName(0, tr("保存文件"), filename); if(!name.isEmpty()) { Client *clnt = new Client(this); clnt->setFileName(name); clnt->setHostAddr(QHostAddress(srvaddr)); clnt->show(); } } else { sndMsg(Refuse, srvaddr); } } }
qChat::qChat(QWidget *parent) : QWidget(parent), ui(new Ui::qChat) { ui->setupUi(this); /* server parameters */ _host = "achat.lorenzobianconi.net"; _port = 9999; _nick = getHostname() + QString("@") + QHostInfo::localHostName(); _sock = new QTcpSocket(this); _connecTimer = new QTimer(this); _ws = CLIENT_NOT_AUTHENTICATED; _attempt = 1; tableFormat.setBorder(0); connect(ui->msgEdit, SIGNAL(returnPressed()), this, SLOT(sndMsg())); connect(_sock, SIGNAL(connected()), this, SLOT(clientAuth())); connect(_sock, SIGNAL(readyRead()), this, SLOT(getMsg())); connect(_sock, SIGNAL(error(QAbstractSocket::SocketError)), this, SLOT(displayError(QAbstractSocket::SocketError))); connect(_connecTimer, SIGNAL(timeout()), this, SLOT(try_connect())); _sock->connectToHost(_host, _port); _connecTimer->setSingleShot(true); }
Server::Server(QWidget *parent) : QDialog(parent), ui(new Ui::Server) { ui->setupUi(this); //将对话框大小固定为400*207 setFixedSize(400, 207); tPort = 5555; tSrv = new QTcpServer(this); connect(tSrv, SIGNAL(newConnection()), this, SLOT(sndMsg())); initSrv(); }
//用来处理新用户的加入 void Widget::usrEnter(QString usrname, QString ipaddr) { bool isEmpty = ui->usrTblWidget->findItems(usrname, Qt::MatchExactly).isEmpty(); if(isEmpty) { QTableWidgetItem *usr = new QTableWidgetItem(usrname); QTableWidgetItem *ip = new QTableWidgetItem(ipaddr); ui->usrTblWidget->insertRow(0); ui->usrTblWidget->setItem(0, 0, usr); ui->usrTblWidget->setItem(0, 1, ip); ui->msgBrowser->setTextColor(Qt::gray); ui->msgBrowser->setCurrentFont(QFont("Times New Roman", 10)); ui->msgBrowser->append(tr("%1 在线!").arg(usrname)); ui->usrNumLbl->setText(tr("在线人数: %1").arg(ui->usrTblWidget->rowCount())); sndMsg(UsrEnter); } }
Widget::Widget(QWidget *parent, QString usrname) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); uName = usrname; udpSocket = new QUdpSocket(this); port = 23232; udpSocket->bind(port, QUdpSocket::ShareAddress | QUdpSocket::ReuseAddressHint); connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDategrams())); sndMsg(UsrEnter); srv = new Server(this); connect(srv, SIGNAL(sndFileName(QString)), this, SLOT(getFileName(QString))); connect(ui->msgTxtEdit, SIGNAL(currentCharFormatChanged(QTextCharFormat)), this, SLOT(curFmtChanged(const QTextCharFormat))); }
void Widget::closeEvent(QCloseEvent *e) { sndMsg(UsrLeft); QWidget::closeEvent(e); }
void Widget::getFileName(QString name) { fileName = name; //首先获取文件名, sndMsg(FileName); //然后发送FileName类型的UDP广播 }
void Widget::on_sendBtn_clicked() { sndMsg(Msg); }
/*********************************************************** * CheckFromServer ***********************************************************/ status_t HDaemonApp::CheckFromServer(entry_ref &ref) { // Load account setting BFile file; BMessage msg; BEntry entry(&ref); char name[B_FILE_NAME_LENGTH+1]; entry.GetName(name); PRINT(("Account Name:%s\n",name)); if(file.SetTo(&entry,B_READ_ONLY) != B_OK) return B_ERROR; fAccountName = name; msg.Unflatten(&file); const char* password,*port; if(msg.FindString("pop_host",&fHost) != B_OK) return B_ERROR; if(msg.FindString("pop_port",&port) != B_OK) return B_ERROR; fPort = atoi(port); if(msg.FindString("pop_user",&fLogin) != B_OK) return B_ERROR; if(msg.FindInt16("protocol_type",&fProtocolType) != B_OK) fProtocolType = 0; if(msg.FindInt16("retrieve",&fRetrievingType) != B_OK) fRetrievingType = 0; if(fRetrievingType == 2) msg.FindInt32("delete_day",&fDeleteDays); else fDeleteDays = 0; if(msg.FindString("uidl",&fUidl) != B_OK) fUidl= ""; if(msg.FindString("pop_password",&password) != B_OK) return B_ERROR; int32 len = strlen(password); fPassword = ""; for(int32 k = 0;k < len;k++) fPassword += (char)(255-password[k]); if(fPopClient) fPopClient->PostMessage(B_QUIT_REQUESTED); fPopClient = new PopClient(NULL,this); if(fPopClient->Lock()) { fPopClient->InitBlackList(); fPopClient->Unlock(); } fDeleteMails.MakeEmpty(); fDeleteMails.what = H_DELETE_MESSAGE; fGotMails = false; BMessage sndMsg(H_CONNECT_MESSAGE); sndMsg.AddString("address",fHost); sndMsg.AddInt16("port",fPort); fPopClient->PostMessage(&sndMsg); return B_OK; }
void Widget::getFileName(QString name) { fileName = name; sndMsg(FileName); }