网盘——进入文件夹

本文主要讲解网盘的文件操作中进入文件夹的部分,具体实现步骤如下:

1、具体步骤如下:

A、客户端发送进入文件夹的请求(该请求包含目录信息以及要进入的文件夹名字)

B、服务器收到该请求之后,服务器首先判断路径是否正确。

正确:则进入到该路径中,将新文件夹中的文件信息发送给客户端

不正确:回复失败给客户端

C、客户端接收来自服务器的回复信息并显示

2、代码实现

2.1、添加协议,进入文件夹的消息类型

 ENUM_MSG_TYPE_ENTER_DIR_REQUEST,  //进入文件夹请求
ENUM_MSG_TYPE_ENTER_DIR_RESPOND,  //进入文件夹回复

2.2、添加进入文件夹的槽函数

    //进入文件夹
void enterDir(const QModelIndex &index);

2.3、关联connect信号,doubleClicked双击

    //关联进入文件夹信号槽  doubleClicked双击
   connect(m_pBookListW,SIGNAL(doubleClicked(QModelIndex)),this, SLOT(enterDir(QModelIndex)));

2.4、添加槽函数定义

void Book::enterDir(const QModelIndex &index)
{
    //通过index获得双击的选项上面的内容
    QString strDirName = index.data().toString();
    //测试打印    
    //qDebug()<<strDirName;
    QString strCurPath = TcpClient::getinstance().curPath();
    PDU *pdu =mkPDU(strCurPath.size()+1);
    pdu->uiMsgType=ENUM_MSG_TYPE_ENTER_DIR_REQUEST;
    strncpy(pdu->caData,strDirName.toStdString().c_str(),strDirName.size());
    memcpy(pdu->caMsg,strCurPath.toStdString().c_str(),strCurPath.size());
    //发送给服务器
    TcpClient::getinstance().getTcpSocket().write((char*)pdu,pdu->uiPDULen);
    free(pdu);
    pdu=NULL;
}

2.5、在服务器端写处理

2.6、添加进入文件夹的case

case ENUM_MSG_TYPE_ENTER_DIR_REQUEST:
    {
        char caEnterName[32] = {'\0'};
        strncpy(caEnterName,pdu->caData,32);
        //产生一块空间,将传过来的路径拷贝出来
        char *pPath = new char[pdu->uiMsgLen];
        memcpy(pPath,pdu->caMsg,pdu->uiMsgLen);
        
        QString strPath = QString("%1/%2").arg(pPath).arg(caEnterName);
        qDebug()<<strPath;
        
        //判断是路径还是唱常规文件
        QFileInfo fileInfo(strPath);
        PDU *respdu =NULL;
        //判断类型
        if(fileInfo.isDir())
        {
            QDir dir(strPath);
            QFileInfoList fileInfoList = dir.entryInfoList();
            
            //产生pdu
            int iFileCount = fileInfoList.size();
            PDU *respdu = mkPDU(sizeof (FileInfo)*iFileCount);
            respdu->uiMsgType = ENUM_MSG_TYPE_FLUSH_DIR_RESPOND;
            FileInfo *pFileInfo =NULL;
            QString strFileName;
            for(int i=0;i<iFileCount;i++)
            {
                //拷贝进去,跳到下一个结构体
                pFileInfo = (FileInfo*)(respdu->caMsg)+i;
                strFileName = fileInfoList[i].fileName();
                
                memcpy(pFileInfo->caFileName,strFileName.toStdString().c_str(),strFileName.size());
                //判断类型
                if(fileInfoList[i].isDir())
                {
                    pFileInfo->iFileType=0;//表示是个文件夹
                }
                else if(fileInfoList[i].isFile()) {
                    pFileInfo->iFileType =1;//常规文件
                }
            }
            //发送
            write((char*)respdu, respdu->uiPDULen);
            free(respdu);
            respdu =NULL;
        }
        else if(fileInfo.isFile()) {
            respdu=mkPDU(0);
            respdu->uiMsgType=ENUM_MSG_TYPE_ENTER_DIR_RESPOND;
            strcpy(respdu->caData,ENTERE_DIR_FAILED);
            
            write((char*)respdu, respdu->uiPDULen);
            free(respdu);
            respdu =NULL;
        }
        break;
    }

2.7、客户端接收

        //---------------------进入文件夹------------------------
    case ENUM_MSG_TYPE_ENTER_DIR_RESPOND:
    {
        QMessageBox::information(this,"进入文件夹",pdu->caData);
        break;
    }

现在还是有点问题的,当我们进入文件夹的时候,当前的文件夹strCurPath应该更新的

2.8、添加清楚进入文件夹的函数

void Book::clearEnterDir()
{
    m_strEnterDir.clear();
}
     //---------------------进入文件夹------------------------
    case ENUM_MSG_TYPE_ENTER_DIR_RESPOND:
    {
        OpeWidget::getInstance().getBook()->clearEnterDir();
        QMessageBox::information(this,"进入文件夹",pdu->caData);
        break;
    }

3、测试

3.1、点击常规文件

3.2、点击文件夹,直接进入

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值