-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.cpp
More file actions
483 lines (414 loc) · 14.5 KB
/
Copy pathbook.cpp
File metadata and controls
483 lines (414 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#include "book.h"
#include "tcpclient.h"
#include <QInputDialog>
#include <QMessageBox>
#include <QFileDialog>
#include "opewidget.h"
#include "sharefile.h"
Book::Book(QWidget *parent) : QWidget(parent)
{
m_strEnterDir.clear();
m_bDownload = false;
m_pTimer = new QTimer;
m_pBookListW = new QListWidget;
m_pReturnPB = new QPushButton("返回");
m_pCreateDirPB = new QPushButton("创建文件夹");
m_pDelDirPB = new QPushButton("删除文件夹");
m_pRenamePB = new QPushButton("重命名文件");
m_pFlushFilePB = new QPushButton("刷新文件");
QVBoxLayout *pDirVBL = new QVBoxLayout;
pDirVBL->addWidget(m_pReturnPB);
pDirVBL->addWidget(m_pCreateDirPB);
pDirVBL->addWidget(m_pDelDirPB);
pDirVBL->addWidget(m_pRenamePB);
pDirVBL->addWidget(m_pFlushFilePB);
m_pUploadPB = new QPushButton("上传文件");
m_pDownLoadPB = new QPushButton("下载文件");
m_pDelFilePB = new QPushButton("删除文件");
m_pShareFilePB = new QPushButton("共享文件");
m_pMoveFilePB = new QPushButton("移动文件");
m_pSelectDirPB = new QPushButton("目标目录");
m_pSelectDirPB->setEnabled(false);
QVBoxLayout *pFileVBL = new QVBoxLayout;
pFileVBL->addWidget(m_pUploadPB);
pFileVBL->addWidget(m_pDownLoadPB);
pFileVBL->addWidget(m_pDelFilePB);
pFileVBL->addWidget(m_pShareFilePB);
pFileVBL->addWidget(m_pMoveFilePB);
pFileVBL->addWidget(m_pSelectDirPB);
QHBoxLayout *pMain = new QHBoxLayout;
pMain->addWidget(m_pBookListW);
pMain->addLayout(pDirVBL);
pMain->addLayout(pFileVBL);
setLayout(pMain);
connect(m_pCreateDirPB, SIGNAL(clicked(bool))
, this, SLOT(createDir()));
connect(m_pFlushFilePB, SIGNAL(clicked(bool))
, this, SLOT(flushFile()));
connect(m_pDelDirPB, SIGNAL(clicked(bool))
, this, SLOT(delDir()));
connect(m_pRenamePB, SIGNAL(clicked(bool))
, this, SLOT(renameFile()));
connect(m_pBookListW, SIGNAL(doubleClicked(QModelIndex))
, this, SLOT(enterDir(QModelIndex)));
connect(m_pReturnPB, SIGNAL(clicked(bool))
, this, SLOT(returnPre()));
connect(m_pDelFilePB, SIGNAL(clicked(bool))
, this, SLOT(delRegFile()));
connect(m_pUploadPB, SIGNAL(clicked(bool))
, this, SLOT(uploadFile()));
connect(m_pTimer, SIGNAL(timeout())
, this, SLOT(uploadFileData()));
connect(m_pDownLoadPB, SIGNAL(clicked(bool))
, this, SLOT(downloadFile()));
connect(m_pShareFilePB, SIGNAL(clicked(bool))
, this, SLOT(shareFile()));
connect(m_pMoveFilePB, SIGNAL(clicked(bool))
, this, SLOT(moveFile()));
connect(m_pSelectDirPB, SIGNAL(clicked(bool))
, this, SLOT(selectDestDir()));
}
void Book::updateFileList(const PDU *pdu)
{
if (NULL == pdu)
{
return;
}
QListWidgetItem *pItemTmp = NULL;
int row = m_pBookListW->count();
while (m_pBookListW->count()>0)
{
pItemTmp = m_pBookListW->item(row-1);
m_pBookListW->removeItemWidget(pItemTmp);
delete pItemTmp;
row = row-1;
}
FileInfo *pFileInfo = NULL;
int iCount = pdu->uiMsgLen/sizeof(FileInfo);
for (int i=0; i<iCount; i++)
{
pFileInfo = (FileInfo*)(pdu->caMsg)+i;
qDebug() << pFileInfo->caFileName << pFileInfo->iFileType;
QListWidgetItem *pItem = new QListWidgetItem;
if (0 == pFileInfo->iFileType)
{
pItem->setIcon(QIcon(QPixmap(":/map/dir.jpg")));
}
else if (1 == pFileInfo->iFileType)
{
pItem->setIcon(QIcon(QPixmap(":/map/reg.jpg")));
}
pItem->setText(pFileInfo->caFileName);
m_pBookListW->addItem(pItem);
}
}
void Book::clearEnterDir()
{
m_strEnterDir.clear();
}
QString Book::enterDir()
{
return m_strEnterDir;
}
void Book::setDownloadStatus(bool status)
{
m_bDownload = status;
}
bool Book::getDownloadStatus()
{
return m_bDownload;
}
QString Book::getSaveFilePath()
{
return m_strSaveFilePath;
}
QString Book::getShareFileName()
{
return m_strShareFileName;
}
void Book::createDir()
{
QString strNewDir = QInputDialog::getText(this, "新建文件夹", "新文件夹名字");
if (!strNewDir.isEmpty())
{
if (strNewDir.size()>32)
{
QMessageBox::warning(this, "新建文件夹", "新文件夹名字不能超过32个字符");
}
else
{
QString strName = TcpClient::getInstance().loginName();
QString strCurPath = TcpClient::getInstance().curPath();
PDU *pdu = mkPDU(strCurPath.size()+1);
pdu->uiMsgType = ENUM_MSG_TYPE_CREATE_DIR_REQUEST;
strncpy(pdu->caData, strName.toStdString().c_str(), strName.size());
strncpy(pdu->caData+32, strNewDir.toStdString().c_str(), strNewDir.size());
memcpy(pdu->caMsg, strCurPath.toStdString().c_str(), strCurPath.size());
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
free(pdu);
pdu = NULL;
}
}
else
{
QMessageBox::warning(this, "新建文件夹", "新文件夹名字不能为空");
}
}
void Book::flushFile()
{
QString strCurPath = TcpClient::getInstance().curPath();
PDU *pdu = mkPDU(strCurPath.size()+1);
pdu->uiMsgType = ENUM_MSG_TYPE_FLUSH_FILE_REQUEST;
strncpy((char*)(pdu->caMsg), strCurPath.toStdString().c_str(), strCurPath.size());
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
free(pdu);
pdu = NULL;
}
void Book::delDir()
{
QString strCurPath = TcpClient::getInstance().curPath();
QListWidgetItem *pItem = m_pBookListW->currentItem();
if (NULL == pItem)
{
QMessageBox::warning(this, "删除文件", "请选择要删除的文件");
}
else
{
QString strDelName = pItem->text();
PDU *pdu = mkPDU(strCurPath.size()+1);
pdu->uiMsgType = ENUM_MSG_TYPE_DEL_DIR_REQUEST;
strncpy(pdu->caData, strDelName.toStdString().c_str(), strDelName.size());
memcpy(pdu->caMsg, strCurPath.toStdString().c_str(), strCurPath.size());
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
free(pdu);
pdu = NULL;
}
}
void Book::renameFile()
{
QString strCurPath = TcpClient::getInstance().curPath();
QListWidgetItem *pItem = m_pBookListW->currentItem();
if (NULL == pItem)
{
QMessageBox::warning(this, "重命名文件", "请选择要重命名的文件");
}
else
{
QString strOldName = pItem->text();
QString strNewName = QInputDialog::getText(this, "重命名文件", "请输入新的文件名");
if (!strNewName.isEmpty())
{
PDU *pdu = mkPDU(strCurPath.size()+1);
pdu->uiMsgType = ENUM_MSG_TYPE_RENAME_FILE_REQUEST;
strncpy(pdu->caData, strOldName.toStdString().c_str(), strOldName.size());
strncpy(pdu->caData+32, strNewName.toStdString().c_str(), strNewName.size());
memcpy(pdu->caMsg, strCurPath.toStdString().c_str(), strCurPath.size());
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
free(pdu);
pdu = NULL;
}
else
{
QMessageBox::warning(this, "重命名文件", "新文件名不能为空");
}
}
}
void Book::enterDir(const QModelIndex &index)
{
QString strDirName = index.data().toString();
m_strEnterDir = 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;
}
void Book::returnPre()
{
QString strCurPath = TcpClient::getInstance().curPath();
QString strRootPath = "./"+TcpClient::getInstance().loginName();
qDebug() << strCurPath << strRootPath;
if (strCurPath == strRootPath)
{
QMessageBox::warning(this, "返回", "返回失败:已经在最开始的文件夹目录中");
}
else
{ // "./aa/bb/cc" --> "./aa/bb"
int index = strCurPath.lastIndexOf('/');
strCurPath.remove(index, strCurPath.size()-index);
qDebug () << "return --> " << strCurPath;
TcpClient::getInstance().setCurPath(strCurPath);
clearEnterDir();
flushFile();
}
}
void Book::delRegFile()
{
QString strCurPath = TcpClient::getInstance().curPath();
QListWidgetItem *pItem = m_pBookListW->currentItem();
if (NULL == pItem)
{
QMessageBox::warning(this, "删除文件", "请选择要删除的文件");
}
else
{
QString strDelName = pItem->text();
PDU *pdu = mkPDU(strCurPath.size()+1);
pdu->uiMsgType = ENUM_MSG_TYPE_DEL_FILE_REQUEST;
strncpy(pdu->caData, strDelName.toStdString().c_str(), strDelName.size());
memcpy(pdu->caMsg, strCurPath.toStdString().c_str(), strCurPath.size());
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
free(pdu);
pdu = NULL;
}
}
void Book::uploadFile()
{
m_strUploadFilePath = QFileDialog::getOpenFileName();
qDebug() << m_strUploadFilePath;
if (!m_strUploadFilePath.isEmpty())
{
// aa/bb/cc 5 8
int index = m_strUploadFilePath.lastIndexOf('/');
QString strFileName = m_strUploadFilePath.right(m_strUploadFilePath.size()-index-1);
qDebug() << strFileName;
QFile file(m_strUploadFilePath);
qint64 fileSize = file.size(); //获得文件大小
QString strCurPath = TcpClient::getInstance().curPath();
PDU *pdu = mkPDU(strCurPath.size()+1);
pdu->uiMsgType = ENUM_MSG_TYPE_UPLOAD_FILE_REQUEST;
memcpy(pdu->caMsg, strCurPath.toStdString().c_str(), strCurPath.size());
sprintf(pdu->caData, "%s %lld", strFileName.toStdString().c_str(), fileSize);
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
free(pdu);
pdu = NULL;
m_pTimer->start(1000);
}
else
{
QMessageBox::warning(this, "上传文件", "上传文件名字不能为空");
}
}
void Book::uploadFileData()
{
m_pTimer->stop();
QFile file(m_strUploadFilePath);
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::warning(this, "上传文件", "打开文件失败");
return;
}
char *pBuffer = new char[4096];
qint64 ret = 0;
while (true)
{
ret = file.read(pBuffer, 4096);
if (ret > 0 && ret <= 4096)
{
TcpClient::getInstance().getTcpSocket().write(pBuffer, ret);
}
else if (0 == ret)
{
break;
}
else
{
QMessageBox::warning(this, "上传文件", "上传文件失败:读文件失败");
break;
}
}
file.close();
delete []pBuffer;
pBuffer = NULL;
}
void Book::downloadFile()
{
QListWidgetItem *pItem = m_pBookListW->currentItem();
if (NULL == pItem)
{
QMessageBox::warning(this, "下载文件", "请选择要下载的文件");
}
else
{
QString strSaveFilePath = QFileDialog::getSaveFileName();
if (strSaveFilePath.isEmpty())
{
QMessageBox::warning(this, "下载文件", "请指定要保存的位置");
m_strSaveFilePath.clear();
}
else
{
m_strSaveFilePath = strSaveFilePath;
}
QString strCurPath = TcpClient::getInstance().curPath();
PDU *pdu = mkPDU(strCurPath.size()+1);
pdu->uiMsgType = ENUM_MSG_TYPE_DOWNLOAD_FILE_REQUEST;
QString strFileName = pItem->text();
strcpy(pdu->caData, strFileName.toStdString().c_str());
memcpy(pdu->caMsg, strCurPath.toStdString().c_str(), strCurPath.size());
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
}
}
void Book::shareFile()
{
QListWidgetItem *pItem = m_pBookListW->currentItem();
if (NULL == pItem)
{
QMessageBox::warning(this, "分享文件", "请选择要分享的文件");
return;
}
else
{
m_strShareFileName = pItem->text();
}
Friend *pFriend = OpeWidget::getInstance().getFriend();
QListWidget *pFriendList = pFriend->getFriendList();
ShareFile::getInstance().updateFriend(pFriendList);
if (ShareFile::getInstance().isHidden())
{
ShareFile::getInstance().show();
}
}
void Book::moveFile()
{
QListWidgetItem *pCurItem = m_pBookListW->currentItem();
if (NULL != pCurItem)
{
m_strMoveFileName = pCurItem->text();
QString strCutPath = TcpClient::getInstance().curPath();
m_strMoveFilePath = strCutPath+'/'+m_strMoveFileName;
m_pSelectDirPB->setEnabled(true);
}
else
{
QMessageBox::warning(this, "移动文件", "请选择要移动的文件");
}
}
void Book::selectDestDir()
{
QListWidgetItem *pCurItem = m_pBookListW->currentItem();
if (NULL != pCurItem)
{
QString strDestDir = pCurItem->text();
QString strCutPath = TcpClient::getInstance().curPath();
m_strDestDir = strCutPath+'/'+strDestDir;
int srcLen = m_strMoveFilePath.size();
int destLen = m_strDestDir.size();
PDU *pdu = mkPDU(srcLen+destLen+2);
pdu->uiMsgType = ENUM_MSG_TYPE_MOVE_FILE_REQUEST;
sprintf(pdu->caData, "%d %d %s", srcLen, destLen, m_strMoveFileName.toStdString().c_str());
memcpy(pdu->caMsg, m_strMoveFilePath.toStdString().c_str(), srcLen);
memcpy((char*)(pdu->caMsg)+(srcLen+1), m_strDestDir.toStdString().c_str(), destLen);
TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
free(pdu);
pdu = NULL;
}
else
{
QMessageBox::warning(this, "移动文件", "请选择要移动的文件");
}
m_pSelectDirPB->setEnabled(false);
}