file: get remote file success.
This commit is contained in:
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -158,6 +158,7 @@
|
||||
"hash_map": "cpp",
|
||||
"qreadwritelock": "cpp",
|
||||
"qdatastream": "cpp",
|
||||
"qhostaddress": "cpp"
|
||||
"qhostaddress": "cpp",
|
||||
"qthread": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
#include "ClientCore.h"
|
||||
|
||||
#include <InfoMsg.h>
|
||||
#include <InfoPack.hpp>
|
||||
#include <QDebug>
|
||||
|
||||
ClientCore::ClientCore(QObject* parent) : QObject(parent)
|
||||
@@ -66,6 +64,41 @@ void ClientCore::UseFrame(QSharedPointer<FrameBuffer> frame)
|
||||
qInfo() << QString(tr("own id: %1")).arg(ownID_);
|
||||
break;
|
||||
}
|
||||
case FrameBufferType::FBT_CLI_ANS_DIRFILE: {
|
||||
DirFileInfoVec info = infoUnpack<DirFileInfoVec>(frame->data);
|
||||
fileCall_(info);
|
||||
break;
|
||||
}
|
||||
case FrameBufferType::FBT_CLI_ASK_DIRFILE: {
|
||||
DirFileInfoVec vec;
|
||||
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
|
||||
vec.root = info.msg;
|
||||
if (!localFile_.GetDirFile(info.msg, vec)) {
|
||||
qWarning() << QString(tr("get dir file failed use %1")).arg(info.msg);
|
||||
return;
|
||||
}
|
||||
if (!Send<DirFileInfoVec>(vec, FBT_CLI_ANS_DIRFILE, frame->fid)) {
|
||||
qCritical() << QString(tr("send dir file result failed."));
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FrameBufferType::FBT_CLI_ASK_HOME: {
|
||||
InfoMsg info;
|
||||
info.msg = Util::GetUserHome();
|
||||
auto data = infoPack<InfoMsg>(info);
|
||||
if (!Send<InfoMsg>(info, FBT_CLI_ANS_HOME, frame->fid)) {
|
||||
qCritical() << QString(tr("send home failed."));
|
||||
return;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case FrameBufferType::FBT_CLI_ANS_HOME: {
|
||||
InfoMsg info = infoUnpack<InfoMsg>(frame->data);
|
||||
qInfo() << QString(tr("home: %1")).arg(info.msg);
|
||||
pathCall_(info.msg);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
qWarning() << QString(tr("unknown frame type: %1")).arg(frame->type);
|
||||
break;
|
||||
@@ -115,6 +148,7 @@ void ClientCore::SetFileCall(const std::function<void(const DirFileInfoVec& file
|
||||
|
||||
void ClientCore::SetRemoteID(const QString& id)
|
||||
{
|
||||
remoteID_ = id;
|
||||
}
|
||||
|
||||
QString ClientCore::GetRemoteID()
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
|
||||
#include <InfoClient.h>
|
||||
#include <InfoDirFile.h>
|
||||
#include <InfoMsg.h>
|
||||
#include <InfoPack.hpp>
|
||||
#include <LocalFile.h>
|
||||
#include <Protocol.h>
|
||||
#include <QDataStream>
|
||||
#include <QHostAddress>
|
||||
@@ -24,6 +27,14 @@ public:
|
||||
void Disconnect();
|
||||
bool Send(QSharedPointer<FrameBuffer> frame);
|
||||
bool Send(const char* data, qint64 len);
|
||||
template <typename T> bool Send(const T& info, FrameBufferType type, const QString& tid)
|
||||
{
|
||||
auto f = QSharedPointer<FrameBuffer>::create();
|
||||
f->tid = tid;
|
||||
f->data = infoPack<T>(info);
|
||||
f->type = type;
|
||||
return Send(f);
|
||||
}
|
||||
|
||||
private:
|
||||
void onReadyRead();
|
||||
@@ -46,6 +57,8 @@ public:
|
||||
QTcpSocket* socket_;
|
||||
QByteArray recvBuffer_;
|
||||
|
||||
LocalFile localFile_;
|
||||
|
||||
std::function<void(const QString& path)> pathCall_;
|
||||
std::function<void(const InfoClientVec& clients)> clientsCall_;
|
||||
std::function<void(const DirFileInfoVec& files)> fileCall_;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "RemoteFile.h"
|
||||
|
||||
#include <InfoPack.hpp>
|
||||
|
||||
#include "LocalFile.h"
|
||||
|
||||
void RemoteFile::setClientCore(ClientCore* cliCore)
|
||||
@@ -13,19 +14,12 @@ void RemoteFile::setClientCore(ClientCore* cliCore)
|
||||
bool RemoteFile::GetHome()
|
||||
{
|
||||
InfoMsg info;
|
||||
auto frame = QSharedPointer<FrameBuffer>::create();
|
||||
frame->data = infoPack(info);
|
||||
frame->type = FBT_CLI_ASK_HOME;
|
||||
frame->tid = cliCore_->GetRemoteID();
|
||||
return cliCore_->Send(frame);
|
||||
return cliCore_->Send<InfoMsg>(info, FBT_CLI_ASK_HOME, cliCore_->GetRemoteID());
|
||||
}
|
||||
|
||||
bool RemoteFile::GetDirFile(const QString& dir)
|
||||
{
|
||||
InfoMsg info;
|
||||
auto frame = QSharedPointer<FrameBuffer>::create();
|
||||
frame->data = infoPack(info);
|
||||
frame->type = FBT_CLI_ASK_DIRFILE;
|
||||
frame->tid = cliCore_->GetRemoteID();
|
||||
return cliCore_->Send(frame);
|
||||
info.msg = dir;
|
||||
return cliCore_->Send<InfoMsg>(info, FBT_CLI_ASK_DIRFILE, cliCore_->GetRemoteID());
|
||||
}
|
||||
@@ -148,17 +148,7 @@ void FileManager::ShowFile(const DirFileInfoVec& info)
|
||||
ui->tableWidget->setItem(i, 4, item);
|
||||
}
|
||||
ui->tableWidget->resizeColumnToContents(0);
|
||||
|
||||
if (info.vec.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString fp = info.vec[0].fullPath;
|
||||
QDir dir(fp);
|
||||
dir.cdUp();
|
||||
fp = dir.path();
|
||||
curRoot_ = fp;
|
||||
|
||||
curRoot_ = info.root;
|
||||
ShowPath(curRoot_);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,6 +40,12 @@ QSharedPointer<FrameBuffer> Protocol::ParseBuffer(QByteArray& buffer)
|
||||
stream.readRawData(fidBytes.data(), 32);
|
||||
stream.readRawData(tidBytes.data(), 32);
|
||||
|
||||
auto b2id = [](const QByteArray& binary, int fixLen) {
|
||||
const int nullPos = binary.indexOf('\0');
|
||||
const int len = (nullPos >= 0) ? qMin(nullPos, fixLen) : fixLen;
|
||||
return QString::fromUtf8(binary.constData(), len);
|
||||
};
|
||||
|
||||
quint32 dataLen;
|
||||
stream >> dataLen;
|
||||
|
||||
@@ -60,8 +66,8 @@ QSharedPointer<FrameBuffer> Protocol::ParseBuffer(QByteArray& buffer)
|
||||
}
|
||||
auto frame = QSharedPointer<FrameBuffer>::create();
|
||||
frame->type = type;
|
||||
frame->fid = QString::fromUtf8(fidBytes).trimmed();
|
||||
frame->tid = QString::fromUtf8(tidBytes).trimmed();
|
||||
frame->fid = b2id(fidBytes, 32);
|
||||
frame->tid = b2id(tidBytes, 32);
|
||||
frame->data = data;
|
||||
buffer.remove(0, headerPos + totalFrameSize);
|
||||
return frame;
|
||||
|
||||
@@ -33,6 +33,7 @@ QDataStream& operator>>(QDataStream& data, DirFileInfo& info);
|
||||
|
||||
struct DirFileInfoVec {
|
||||
QVector<DirFileInfo> vec;
|
||||
QString root;
|
||||
|
||||
void serialize(QDataStream& data) const
|
||||
{
|
||||
@@ -40,6 +41,7 @@ struct DirFileInfoVec {
|
||||
for (const auto& info : vec) {
|
||||
data << info;
|
||||
}
|
||||
data << root;
|
||||
}
|
||||
void deserialize(QDataStream& data)
|
||||
{
|
||||
@@ -49,6 +51,7 @@ struct DirFileInfoVec {
|
||||
for (quint32 i = 0; i < size; ++i) {
|
||||
data >> vec[i];
|
||||
}
|
||||
data >> root;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,16 +1,61 @@
|
||||
#include "LocalFile.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
|
||||
bool LocalFile::GetHome()
|
||||
{
|
||||
return false;
|
||||
auto home = Util::GetUserHome();
|
||||
pathCall_(home);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LocalFile::GetDirFile(const QString& dir)
|
||||
{
|
||||
return false;
|
||||
DirFileInfoVec vec;
|
||||
if (!GetDirFile(dir, vec)) {
|
||||
return false;
|
||||
}
|
||||
fileCall_(vec);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool LocalFile::GetDirFile(const QString& dir, DirFileInfoVec& vec)
|
||||
{
|
||||
return false;
|
||||
vec.vec.clear();
|
||||
|
||||
QDir qdir(dir);
|
||||
if (!qdir.exists()) {
|
||||
err_ = tr("Path does not exist");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!qdir.isReadable()) {
|
||||
err_ = tr("Directory is not readable");
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto entries = qdir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
||||
|
||||
for (const auto& entry : entries) {
|
||||
DirFileInfo info;
|
||||
info.fullPath = entry.absoluteFilePath();
|
||||
info.name = entry.fileName();
|
||||
|
||||
if (entry.isDir()) {
|
||||
info.type = Dir;
|
||||
info.size = 0;
|
||||
} else if (entry.isFile()) {
|
||||
info.type = File;
|
||||
info.size = entry.size();
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
info.lastModified = entry.lastModified().toMSecsSinceEpoch();
|
||||
vec.vec.push_back(info);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QMutex>
|
||||
#include <QStandardPaths>
|
||||
#include <iostream>
|
||||
#include <spdlog/fmt/bundled/color.h>
|
||||
#include <spdlog/fmt/fmt.h>
|
||||
@@ -21,6 +23,16 @@ Util::Util()
|
||||
{
|
||||
}
|
||||
|
||||
QString Util::GetUserHome()
|
||||
{
|
||||
QString homePath = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);
|
||||
if (homePath.isEmpty()) {
|
||||
qWarning() << "Failed to get user home directory";
|
||||
homePath = QDir::homePath();
|
||||
}
|
||||
return homePath;
|
||||
}
|
||||
|
||||
void Util::InitLogger(const QString& logPath, const QString& mark)
|
||||
{
|
||||
auto file_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(logPath.toStdString(), 1024 * 1024 * 50, 3);
|
||||
|
||||
@@ -11,12 +11,13 @@ public:
|
||||
Util();
|
||||
|
||||
public:
|
||||
static QString GetUserHome();
|
||||
static void InitLogger(const QString& logPath, const QString& mark);
|
||||
static void ConsoleMsgHander(QtMsgType type, const QMessageLogContext& context, const QString& msg);
|
||||
};
|
||||
|
||||
class DirFileHelper
|
||||
{
|
||||
class DirFileHelper : public QObject {
|
||||
Q_OBJECT
|
||||
public:
|
||||
DirFileHelper() = default;
|
||||
virtual ~DirFileHelper() = default;
|
||||
|
||||
Reference in New Issue
Block a user