# include<QMainWindow>
# include<QMessageBox>
#include <QDir>
# include"MyMainWindow.h"
#include<stdlib.h>
#include<string.h>
#include<sys/socket.h>
#include<dirent.h>
#include<stdio.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
# include "Client_CFM.h"
# include "client.h"
char CtrlSegBuf[128];//the array to store the designed control segment
char DataRecvBuf[65535];
char DirStr[100]="/home/shl/documents";//the default directory
char *DupDirStr = DirStr;
char RDirStr[100]="/home/shl/documents";
DIR *dp;
QDir currentDir;
MyMainWindow::MyMainWindow()
{
ui.setupUi(this);
QObject::connect(ui.OPENpushButton, SIGNAL(clicked()), this, SLOT(Open()));
QObject::connect(ui.PWDpushButton, SIGNAL(clicked()), this, SLOT(Pwd()));
QObject::connect(ui.RLSpushButton, SIGNAL(clicked()), this, SLOT(Rls()));
QObject::connect(ui.LLSpushButton, SIGNAL(clicked()), this, SLOT(Lls()));
QObject::connect(ui.LCDpushButton, SIGNAL(clicked()), this, SLOT(Lcd()));
QObject::connect(ui.RCDpushButton, SIGNAL(clicked()), this, SLOT(Rcd()));
QObject::connect(ui.GETpushButton, SIGNAL(clicked()), this, SLOT(Get()));
QObject::connect(ui.PUTpushButton, SIGNAL(clicked()), this, SLOT(Put()));
QObject::connect(ui.MPUTpushButton, SIGNAL(clicked()), this, SLOT(Mput()));
QObject::connect(ui.MGETpushButton, SIGNAL(clicked()), this, SLOT(Mget()));
}
void MyMainWindow::Open()
{
QString Str=ui.ServerAddresslineEdit->text();
//char *REGISTERStr;
QByteArray ba = Str.toLatin1();
const char *DomainNameEntered = ba.data();//QString to char *
//String localDir="/home/shl/documents";
char DomainNameAndIP[100];
char file1[]="/home/shl/documents/DNS1.txt";
int fp;
fp=open(file1,O_RDONLY);
int length=read(fp,DomainNameAndIP,sizeof(DomainNameAndIP));
printf("DomainName:%s\n",DomainNameAndIP);
char *DomainnameRegitsered=strtok(DomainNameAndIP," ");
printf("The domain name is:%s\n",DomainnameRegitsered);
char *IPRegistered=strtok(NULL," ");
printf("The IP address is:%s\n",IPRegistered);
//CtrlTranspSend(DomainName,strlen(DomainName)+1);
printf("connectting:..........\n");
if((strcmp(DomainNameEntered,DomainnameRegitsered)==0)||(strcmp(DomainNameEntered,IPRegistered)==0))
{
init();
}
else
{
QMessageBox::information(this, tr("Warning"), tr("Invalid domain name!"), tr("OK"));
printf("\n Domain name unvalid. Please type in the correct Domain name!\n");
}
}
void MyMainWindow::Pwd()
{
int PWD_length=1;
PWDCtrl.CmdType="PWD";
strcpy(PWDCtrl.CtrlInfo,"\0");
dealCtrlStruct(PWDCtrl,CtrlSegBuf);
printf("CtrlSegBuf:%s\n",CtrlSegBuf);
CtrlTranspSend(CtrlSegBuf,sizeof(CtrlSegBuf));
bzero(DataRecvBuf,sizeof(DataRecvBuf));
DataTranspRecv(DataRecvBuf,&PWD_length);
printf("PWD_length:%d\n",PWD_length);
printf("DataRecvBuf:%s\n\n",DataRecvBuf);
bzero(RDirStr,sizeof(RDirStr));
strcpy(RDirStr,DataRecvBuf);
ui.RDlineEdit->setText(DataRecvBuf);
//createFilesTable2();
}
void MyMainWindow::Rls()
{
int RLS_length=1;
RLSCtrl.CmdType="RLS";
strcpy(RLSCtrl.CtrlInfo,"\0");
dealCtrlStruct(RLSCtrl,CtrlSegBuf);
printf("CtrlsegBuf is %s\n",CtrlSegBuf);
CtrlTranspSend(CtrlSegBuf,sizeof(CtrlSegBuf));
while(1)
{
bzero(DataRecvBuf,sizeof(DataRecvBuf));
DataTranspRecv(DataRecvBuf,&RLS_length);
printf("RLS_length:%d\n",RLS_length);
printf("DataRecvBuf:%s\n\n",DataRecvBuf);
if(RLS_length!=TRANSIZE)
{
printf("over............................\n");
break;
}
}
ui.RDDtableWidget->setRowCount(0);
QString path;
printf("RDirStr to RLS :%s\n",RDirStr);
/*int m;
for(m=0;m<100;m++)
{
if(RDirStr[m]!='\0')
printf("RDirStr[m]:%s\n",RDirStr[m]);
else
break;
}
printf("m:%d\n",m);*/
path=QString(RDirStr);
currentDir = QDir(path);
QStringList files;
files = currentDir.entryList(QStringList("*"),
QDir::Files | QDir::NoSymLinks);
//createFilesTable();
for (int i = 0; i < files.size(); ++i) {
QFile file(currentDir.absoluteFilePath(files[i]));
qint64 size = QFileInfo(file).size();
QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
int row = ui.LDDtableWidget->rowCount();
ui.RDDtableWidget->insertRow(row);
ui.RDDtableWidget->setItem(row, 0, fileNameItem);
}
//filesFoundLabel = new QLabel;
}
void MyMainWindow::Lls()
{
printf("Current directory is: %s\n",DirStr);
dp = opendir (DirStr);
struct dirent *ep=readdir (dp);
while(ep != NULL)
{
printf("%s\n",ep->d_name);
ep = readdir (dp);
}
closedir (dp);
ui.LDlineEdit->setText(DirStr);
printf("Current directory is: %s\n\n",DirStr);
createFilesTable();
ui.LDDtableWidget->setRowCount(0);
QString path;
path=QString(DirStr);
//strcpy(path,DirStr);
currentDir = QDir(path);
QStringList files;
files = currentDir.entryList(QStringList("*"),
QDir::Files | QDir::NoSymLinks);
//showFiles(files);
createFilesTable();
for (int i = 0; i < files.size(); ++i) {
QFile file(currentDir.absoluteFilePath(files[i]));
qint64 size = QFileInfo(file).size();
QTableWidgetItem *fileNameItem = new QTableWidgetItem(files[i]);
fileNameItem->setFlags(fileNameItem->flags() ^ Qt::ItemIsEditable);
QTableWidgetItem *sizeItem = new QTableWidgetItem(tr("%1 KB")
.arg(int((size + 1023) / 1024)));
sizeItem->setTextAlignment(Qt::AlignRight | Qt::AlignVCenter);
sizeItem->setFlags(sizeItem->flags() ^ Qt::ItemIsEditable);
int row = ui.LDDtableWidget->rowCount();
ui.LDDtableWidget->insertRow(row);
ui.LDDtableWidget->setItem(row, 0, fileNameItem);
ui.LDDtableWidget->setItem(row, 1, sizeItem);
}
//filesFoundLabel = new QLabel;
//filesFoundLabel->setText(tr("%1 file(s) found").arg(files.size()) +(" (Double click on a file to open it)"));
}
void MyMainWindow::createFilesTable()
{
//ui.LDDtableWidget = new QTableWidget(0, 2);
ui.LDDtableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
QStringList labels;
labels << tr("File Name") << tr("Size");
ui.LDDtableWidget->setHorizontalHeaderLabels(labels);
ui.LDDtableWidget->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
ui.LDDtableWidget->verticalHeader()->hide();
ui.LDDtableWidget->setShowGrid(false);
//connect(LDDtableWidget, SIGNAL(cellActivated(int,int)),this, SLOT(openFileOfItem(int,int)));
}
void MyMainWindow::createFilesTable2()
{
//ui.LDDtableWidget = new QTableWidget(0, 2);
ui.RDDtableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
QStringList labels;
labels << tr("File Name");
ui.RDDtableWidget->setHorizontalHeaderLabels(labels);
ui.RDDtableWidget->horizontalHeader()->setResizeMode(0, QHeaderView::Stretch);
ui.RDDtableWidget->verticalHeader()->hide();
ui.RDDtableWidget->setShowGrid(false);
//connect(LDDtableWidget, SIGNAL(cellActivated(int,int)),this, SLOT(openFileOfItem(int,int)));
}
void MyMainWindow::Lcd()
{
QString Str=ui.LCDlineEdit->text();
QByteArray ba = Str.toLatin1();
const char *LCDStr = ba.data();//QString to char *
printf("DirStr is:%s\n",DirStr);
printf("augment information:%s\n",LCDStr);
if(LCDStr==NULL)
{
printf("%s\n","The command you typed in is invalid.");
QMessageBox::information(this, tr("Warning"), tr("Invalid Command!"), tr("OK"));
}
else if( strcmp(LCDStr,"..")==0 )
{
printf("Previous directory is: %s\n",DirStr);
char *position=strrchr(DirStr,'/');
//printf("position:%d\n",position-DirStr+1);
DirStr[position-DirStr]='\0';
printf("Current directory is: %s\n\n",DirStr);
ui.LDlineEdit->setText(DirStr);
}
else
{
//bzero(Dup,sizeof(Dup));
char *Dup=DirStr;
strcat(Dup,"/");
strcat(Dup,LCDStr);
dp = opendir (Dup);
if (dp != NULL)
{
printf("Current directory is: %s\n\n",DirStr);
ui.LDli
- 1
- 2
前往页