/
TransProtocolData.cpp
1111 lines (1008 loc) · 41.6 KB
/
TransProtocolData.cpp
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* filename: TransProtocolData.cpp 2009/03/03 */
/*************************************************************************
LuLu Messenger: A LAN Instant Messenger For Chatting and File Exchanging.
Copyright (C) 2008,2009 Wu Weisheng <wwssir@gmail.com>
This file is part of LuLu Messenger.
LuLu Messenger is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LuLu Messenger is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*************************************************************************/
/*************************************************************************
LuLu 信使: 局域网络即时对话与文件交换通讯工具。
著作权所有 (C) 2008,2009 武维生 <wwssir@gmail.com>
此文件是 LuLu 信使源文件的一部分。
LuLu 信使为自由软件;您可依据自由软件基金会所发表的GNU通用公共授权条款,
对本程序再次发布和/或修改;无论您依据的是本授权的第三版,或(您可选的)
任一日后发行的版本。
LuLu 信使是基于使用目的而加以发布,然而不负任何担保责任;亦无对适售性或
特定目的适用性所为的默示性担保。详情请参照GNU通用公共授权。
您应已收到附随于本程序的GNU通用公共授权的副本;如果没有,请参照
<http://www.gnu.org/licenses/>.
*************************************************************************/
// 说明:
// 修改:
#include "TransProtocolData.h"
#include "UserItem.h"
void
ProtocolDataTransfer::run()
{
switch (m_SocketMode)
{
case Slave:
if (accept() == -1)
{
return;
}
break;
case Active:
if (connectTo() == -1)
{
return;
}
break;
default:
return;
}
switch (m_Protocol)
{
case DoSendFile:
sendFile();
break;
case DoRecvFile:
recvFile();
break;
case DoSendDir:
sendDir();
break;
case DoRecvDir:
recvDir();
break;
case DoSendList:
sendList();
break;
case DoRecvList:
recvList();
break;
}
}
int
ProtocolDataTransfer::accept()
{
Address raddr;
if (acceptor_.open(Address(m_hostPort)) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%P|%t) %p\n"),
ACE_TEXT ("Socket open failed")));
emit sigError(tr("Socket open failed"));
return -1;
}
if (acceptor_.accept(sock_, &raddr) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%P|%t) %p\n"),
ACE_TEXT ("Socket accept failed")));
sock_.close();
emit sigError(tr("Socket accept failed"));
return -1;
}
return 0;
}
int
ProtocolDataTransfer::connectTo()
{
if (connector_.connect(sock_,
Address(m_hostPort, m_hostAddress.toAscii().data())) == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("(%P|%t) %p\n"),
ACE_TEXT ("Socket connection failed")));
emit sigError(tr("Socket connection failed"));
return -1;
}
return 0;
}
void
ProtocolDataTransfer::sendFile()
{
QString const& fName = m_fileName;
QFile file(fName);
if (!file.open(QIODevice::ReadOnly))
{
sock_.close();
m_CtrlID = DoError;
emit sigError(tr("Unable to read the file %1: %2")
.arg(fName).arg(file.errorString()));
return;
}
char buf[PayloadSize];
totalBytes = file.size();
emit sigStart();
while (bytesTransfered < totalBytes
&& m_CtrlID == DoStart)
{
int len = file.read(buf, PayloadSize);
bytesTransfered += sock_.send_n(buf, len);
}
file.close();
sock_.close();
acceptor_.close();
if (bytesTransfered == totalBytes)
{
emit sigComplete();
}
else if (m_CtrlID == DoCancel)
{
emit sigError(tr("Canceled"));
}
else
{
emit sigError(tr("Error"));
}
}
void
ProtocolDataTransfer::recvFile()
{
QString const& fName = m_fileName;
QFile file(fName);
if (!file.open(QIODevice::WriteOnly))
{
sock_.close();
m_CtrlID = DoError;
emit sigError(tr("Unable to read the file %1: %2")
.arg(fName).arg(file.errorString()));
return;
}
char buf[PayloadSize];
emit sigStart();
while (bytesTransfered < totalBytes
&& m_CtrlID == DoStart)
{
quint64 left = totalBytes - bytesTransfered;
int len = sock_.recv_n(buf, (left>PayloadSize)?PayloadSize:left);
bytesTransfered += len;
file.write(buf, len);
}
file.close();
sock_.close();
acceptor_.close();
if (bytesTransfered == totalBytes)
{
m_CtrlID = DoFinish;
emit sigComplete();
}
else if (m_CtrlID == DoCancel)
{
file.remove();
emit sigError(tr("Canceled"));
}
else
{
file.remove();
emit sigError(tr("Error"));
}
}
void
ProtocolDataTransfer::sendDir()
{
emit sigListStart();
totalBytes = 0;
QStringList fileList = DataAccess::GetFileList(m_dirName, &totalBytes);
if (m_item)
{
emit sigListReady(fileList);
m_lock.lock();
m_waitCondition.wait(&m_lock);
m_lock.unlock();
}
if (m_CtrlID != DoStart)
{
return;
}
QFileInfo finfo(m_dirName);
QString const& headkey = fileList[0];
QStringList headkeys = ParseFileInfoString(headkey);
if (headkeys.size() < 3)
{
m_CtrlID = DoError;
sock_.close();
acceptor_.close();
emit sigError(tr("Error occurred while parsing file information: %1.").arg(headkey));
return;
}
headkeys[2] = finfo.fileName();
fileList[0] = headkeys.join("$");
QString const& fstr = fileList.join("/*/");
std::string bArray = fstr.toUtf8().data();
emit sigStart();
// list
int tmp = 0;
int siz = bArray.length() + 1;
tmp = sock_.send_n(&siz, sizeof(siz));
if (tmp != sizeof(siz))
{
sock_.close();
emit sigError(tr("Error occurred while sending file list size."));
return;
}
tmp = sock_.send_n(bArray.c_str(), siz);
if (tmp != siz)
{
sock_.close();
emit sigError(tr("Error occurred while sending file list."));
return;
}
fileList.removeAt(0);
// files
char buf[PayloadSize];
while (m_CtrlID == DoStart)
{
if (fileList.isEmpty())
{
m_CtrlID = DoError;
sock_.close();
emit sigError(tr("File list became empty while sending."));
return;
}
QString const& key = fileList.takeAt(0);
QStringList keys = ParseFileInfoString(key);
if (keys.size() < 3)
{
m_CtrlID = DoError;
sock_.close();
acceptor_.close();
emit sigError(tr("Error occurred while parsing file information: %1.").arg(key));
return;
}
if (keys[0] == "DIR")
{
if (updateDirFileItemInfo)
{
emit sigDirFileTransOK(0, keys[2]);
}
continue;
}
else if (keys[0] == "FILE")
{
quint64 bytesSentOfFile = 0;
quint64 fsize = keys[1].toULongLong();
QString const& fname = m_dirName + keys[2];
QFile file(fname);
if (!file.open(QIODevice::ReadOnly))
{
m_CtrlID = DoError;
sock_.close();
emit sigDirFileTransError(tr("Unable to read the file %1: %2")
.arg(fname).arg(file.errorString()), keys[2]);
return;
}
if (updateDirFileItemInfo)
{
emit sigDirFileTransStart(keys[2]);
}
m_currentFileName = keys[2];
m_currentFilePos = 0;
tmp = file.read(buf, PayloadSize);
tmp = sock_.send(buf, tmp);
bytesSentOfFile += tmp;
while (bytesSentOfFile < fsize
&& m_CtrlID == DoStart)
{
tmp = file.read(buf, PayloadSize);
tmp = sock_.send(buf, tmp);
bytesSentOfFile += tmp;
m_currentFilePos = bytesSentOfFile;
}
file.close();
m_currentFilePos = bytesSentOfFile;
bytesTransfered += bytesSentOfFile;
if (bytesSentOfFile < fsize
&& m_CtrlID == DoCancel)
{
goto callend;
}
if (updateDirFileItemInfo)
{
emit sigDirFileTransOK(fsize, keys[2]);
}
if (bytesTransfered == totalBytes)
{
m_CtrlID = DoFinish;
goto callend;
}
}
}
callend:
sock_.close();
acceptor_.close();
if (m_CtrlID == DoFinish)
{
emit sigComplete();
}
else if (m_CtrlID == DoCancel)
{
emit sigError(tr("Canceled"));
}
else
{
emit sigError(tr("Error"));
}
}
void
ProtocolDataTransfer::recvDir()
{
char buf[PayloadSize];
int tmp = 0;
int siz = 0;
emit sigListStart();
// list
tmp = sock_.recv_n(&siz, sizeof(int));
if (tmp != sizeof(int))
{
sock_.close();
emit sigError(tr("Error occurred while receiving file list size."));
return;
}
char* listBuf = new char[siz];
tmp = sock_.recv_n(listBuf, siz);
if (tmp != siz)
{
sock_.close();
delete listBuf;
emit sigError(tr("Error occurred while receiving file list."));
return;
}
QString const& strBuf = QString::fromUtf8(listBuf, siz);
QStringList fileList = strBuf.split("/*/");
if (m_item)
{
emit sigListReady(fileList);
m_lock.lock();
m_waitCondition.wait(&m_lock);
m_lock.unlock();
}
if (m_CtrlID != DoStart)
{
delete listBuf;
return;
}
emit sigStart();
QString const& headkey = fileList.takeAt(0);
QStringList const& headkeys = ParseFileInfoString(headkey);
totalBytes = headkeys[1].toULongLong();
// files
while (m_CtrlID == DoStart)
{
if (fileList.isEmpty())
{
m_CtrlID = DoError;
sock_.close();
delete listBuf;
emit sigError(tr("File list became empty while receiving."));
return;
}
QString const& key = fileList.takeAt(0);
QStringList keys = ParseFileInfoString(key);
if (keys.size() < 3)
{
m_CtrlID = DoError;
sock_.close();
acceptor_.close();
delete listBuf;
emit sigError(tr("Error occurred while parsing file information: %1.").arg(key));
return;
}
QString saveName = m_dirName + keys[2];
if (keys[0] == "DIR")
{
QDir dir;
dir.mkpath(saveName);
if (updateDirFileItemInfo)
{
emit sigDirFileTransOK(0, keys[2]);
}
}
else if (keys[0] == "FILE")
{
quint64 bytesRecvedOfFile = 0;
quint64 fsize = keys[1].toULongLong();
QFile file(saveName);
if (!file.open(QIODevice::WriteOnly))
{
m_CtrlID = DoError;
sock_.close();
acceptor_.close();
emit sigDirFileTransError(tr("Unable to save the file %1: %2")
.arg(saveName).arg(file.errorString()), keys[2]);
delete listBuf;
return;
}
if (updateDirFileItemInfo)
{
emit sigDirFileTransStart(keys[2]);
}
m_currentFileName = keys[2];
m_currentFilePos = 0;
tmp = sock_.recv_n(buf, (fsize)>PayloadSize?PayloadSize:fsize);
file.write(buf, tmp);
bytesRecvedOfFile += tmp;
while (bytesRecvedOfFile < fsize
&& m_CtrlID == DoStart)
{
siz = (fsize-bytesRecvedOfFile)>PayloadSize?PayloadSize:(fsize-bytesRecvedOfFile);
tmp = sock_.recv_n(buf, siz);
file.write(buf, tmp);
bytesRecvedOfFile += tmp;
m_currentFilePos = bytesRecvedOfFile;
}
file.close();
m_currentFilePos = bytesRecvedOfFile;
bytesTransfered += bytesRecvedOfFile;
if (bytesRecvedOfFile < fsize
&& m_CtrlID == DoCancel)
{
file.remove();
goto callend;
}
if (updateDirFileItemInfo)
{
emit sigDirFileTransOK(fsize, keys[2]);
}
if (bytesTransfered == totalBytes)
{
m_CtrlID = DoFinish;
goto callend;
}
}
}
callend:
sock_.close();
acceptor_.close();
if (m_CtrlID == DoFinish)
{
emit sigComplete();
}
else if (m_CtrlID == DoCancel)
{
emit sigError(tr("Canceled"));
}
else
{
emit sigError(tr("Error"));
}
delete listBuf;
}
void
ProtocolDataTransfer::sendList()
{
emit sigStart();
totalBytes = 0;
QStringList fileList = DataAccess::GetFileList(m_dirName, &totalBytes);
QFileInfo finfo(m_dirName);
QString const& headkey = fileList[0];
QStringList headkeys = ParseFileInfoString(headkey);
headkeys[2] = finfo.fileName();
fileList[0] = headkeys.join("$");
QString const& fstr = fileList.join("/*/");
QByteArray const& bArray = fstr.toUtf8();
int tmp = 0;
int siz = bArray.size() + 1;
tmp = sock_.send_n(&siz, sizeof(siz));
if (tmp != sizeof(siz))
{
sock_.close();
emit sigError(tr("Error occurred while sending file list size."));
return;
}
tmp = sock_.send_n(bArray.data(), siz);
if (tmp != siz)
{
sock_.close();
emit sigError(tr("Error occurred while sending file list."));
return;
}
sock_.close();
emit sigError(tr("List OK."));
}
void
ProtocolDataTransfer::recvList()
{
emit sigStart();
int tmp = 0;
int siz = 0;
tmp = sock_.recv_n(&siz, sizeof(int));
if (tmp != sizeof(int))
{
sock_.close();
emit sigError(tr("Error occurred while receiving file list size."));
return;
}
char* buf = new char[siz];
tmp = sock_.recv_n(buf, siz);
if (tmp != siz)
{
sock_.close();
emit sigError(tr("Error occurred while receiving file list."));
return;
}
sock_.close();
QByteArray bArray(buf, siz);
QStringList fileList = QString(bArray).split("/*/");
delete buf;
emit sigError(tr("List OK."));
}
//////////////////////////////////////////////////////////////////////////
ProtocolDataWidget::ProtocolDataWidget(DataAccess& dataAccess, UserItem* uitem, QWidget* parent)
: QTreeWidget(parent)
, m_userItem(uitem)
, m_DataAccess(dataAccess)
{
createActions();
createMenus();
this->setAlternatingRowColors(true);
QTreeWidgetItem* headerItem = new QTreeWidgetItem;
headerItem->setText(COL_NAME, tr("Name"));
headerItem->setText(COL_SIZE, tr("Size"));
headerItem->setText(COL_TYPE, tr("Type"));
headerItem->setText(COL_PERCENT, tr("Percent"));
headerItem->setText(COL_SPEED, tr("Speed"));
headerItem->setText(COL_STATE, tr("State"));
this->setHeaderItem(headerItem);
this->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setColumnWidth(COL_NAME, 180); // File Name
this->setColumnWidth(COL_SIZE, 110); // Size
this->setColumnWidth(COL_TYPE, 40); // Sign
this->setColumnWidth(COL_PERCENT, 50); // Percent
this->setColumnWidth(COL_SPEED, 70); // Speed
this->setColumnWidth(COL_STATE, 60); // State
setWindowTitle(tr("ProtocolData Trans Watcher"));
}
void
ProtocolDataWidget::createActions()
{
actCancelAllItem = new QAction(QIcon(DataAccess::SkinPath() + "/cancel.png"), tr("Cancel &All Item"), this);
connect(actCancelAllItem, SIGNAL(triggered()), this, SLOT(slotCancelAllItem()));
actRemoveFinishedItem = new QAction(QIcon(DataAccess::SkinPath() + "/error.png"), tr("Remove &Finished Item"), this);
connect(actRemoveFinishedItem, SIGNAL(triggered()), this, SLOT(slotRemoveFinishedItem()));
actRemoveCanceledItem = new QAction(QIcon(DataAccess::SkinPath() + "/cancel.png"), tr("Remove &Canceled Item"), this);
connect(actRemoveCanceledItem, SIGNAL(triggered()), this, SLOT(slotRemoveCanceledItem()));
actRemoveErrorItem = new QAction(QIcon(DataAccess::SkinPath() + "/error.png"), tr("Remove &Error Item"), this);
connect(actRemoveErrorItem, SIGNAL(triggered()), this, SLOT(slotRemoveErrorItem()));
}
void
ProtocolDataWidget::createMenus()
{
menuContext = new QMenu(tr("&Context"));
menuContext->addAction(actCancelAllItem);
menuContext->addAction(actRemoveFinishedItem);
menuContext->addAction(actRemoveCanceledItem);
menuContext->addAction(actRemoveErrorItem);
setContextMenuPolicy(Qt::DefaultContextMenu);
}
void
ProtocolDataWidget::slotRemoveFinishedItem()
{
QList<QTreeWidgetItem*> itemList;
for (int i = 0; i < this->topLevelItemCount(); i++)
{
QTreeWidgetItem* root = this->topLevelItem(i);
QString const& tsr = root->data(COL_NAME, TransStateRole).toString();
if (tsr == "FINISH")
{
itemList << this->topLevelItem(i);
}
}
while (itemList.size() > 0)
{
QTreeWidgetItem* item = this->takeTopLevelItem(indexOfTopLevelItem(itemList.takeAt(0)));
delete item;
}
}
void
ProtocolDataWidget::slotRemoveCanceledItem()
{
QList<QTreeWidgetItem*> itemList;
for (int i = 0; i < this->topLevelItemCount(); i++)
{
QTreeWidgetItem* root = this->topLevelItem(i);
QString const& tsr = root->data(COL_NAME, TransStateRole).toString();
if (tsr == "CANCEL")
{
itemList << this->topLevelItem(i);
}
}
while (itemList.size() > 0)
{
QTreeWidgetItem* item = this->takeTopLevelItem(indexOfTopLevelItem(itemList.takeAt(0)));
delete item;
}
}
void
ProtocolDataWidget::slotRemoveErrorItem()
{
QList<QTreeWidgetItem*> itemList;
for (int i = 0; i < this->topLevelItemCount(); i++)
{
QTreeWidgetItem* root = this->topLevelItem(i);
QString const& tsr = root->data(COL_NAME, TransStateRole).toString();
if (tsr == "ERROR")
{
itemList << this->topLevelItem(i);
}
}
while (itemList.size() > 0)
{
QTreeWidgetItem* item = this->takeTopLevelItem(indexOfTopLevelItem(itemList.takeAt(0)));
delete item;
}
}
void
ProtocolDataWidget::slotCancelAllItem()
{
for (int i = 0; i < this->topLevelItemCount(); i++)
{
QTreeWidgetItem* root = this->topLevelItem(i);
this->setCurrentItem(root);
slotCancel();
}
slotRemoveFinishedItem();
slotRemoveCanceledItem();
slotRemoveErrorItem();
}
void
ProtocolDataWidget::onRecv(Message_ptr m)
{
Address from;
if (From const* f = static_cast<From const*>(m->find(From::id)))
{
from = f->address();
}
if (PFileSend const* pfs = static_cast<PFileSend const*>(m->find(IMProto::FileSend)))
{
QString const& sn = QString::fromUtf8(pfs->property_data(IMProto::EventSn));
QString const& kName = QString::fromUtf8(pfs->property_data(IMProto::FileKey));
QString const& fName = QString::fromUtf8(pfs->property_data(IMProto::FileName));
quint64 siz = QString::fromUtf8(pfs->property_data(IMProto::FileSize)).toULongLong();
//u16 port = QString::fromUtf8(pfs->property_data(IMProto::ListenPort)).toUShort();
QTreeWidgetItem* root = new QTreeWidgetItem;
this->addTopLevelItem(root);
m_RecvItemMap.insert(sn, root);
QString const& sizstr = GetFormatedFileSize(siz);
root->setText(COL_NAME, fName);
root->setText(COL_SIZE, sizstr);
root->setText(COL_TYPE, tr("File"));
root->setText(COL_STATE, tr("waiting to receive"));
root->setText(COL_PERCENT, "0%");
root->setData(COL_NAME, FileKeyRole, kName);
root->setData(COL_NAME, FileSizeRole, siz);
root->setData(COL_NAME, DirFileRole, "FILE");
root->setData(COL_NAME, SendRecvRole, "R");
root->setData(COL_NAME, TransStateRole, "WAIT");
root->setData(COL_NAME, TransItemSNRole, sn);
root->setIcon(COL_NAME, QIcon(DataAccess::SkinPath() + "/download-file.png"));
root->setIcon(COL_STATE, QIcon(DataAccess::SkinPath() + "/clock.png"));
emit sigTips(tr("The other request to send %1:%2. Please switch to file transferring window to process.")
.arg(tr("File"))
.arg(fName));
}
else if (PDirFileRecv const* pfr = static_cast<PDirFileRecv const*>(m->find(IMProto::DirFileRecv)))
{
QString const& sn = QString::fromUtf8(pfr->property_data(IMProto::EventSn));
QString const& kName = QString::fromUtf8(pfr->property_data(IMProto::FileKey));
u16 port = QString::fromUtf8(pfr->property_data(IMProto::ListenPort)).toUShort();
QTreeWidgetItem* item = m_SendItemMap.value(sn, 0);
if (!item)
{
//QMessageBox::critical(this, tr("Error"), tr("Unable to find item in file SendItemMap. key: %1.").arg(keys[2]));
return;
}
QString const& pfName = m_userItem->SendingFilesCollection()->value(kName);
if (pfName.isEmpty())
{
//QMessageBox::critical(this, tr("Error"), tr("Unable to find in SendingFilesCollection key: %1.").arg(keys[2]));
return;
}
QFileInfo finfo(pfName);
ProtocolDataTransfer* pdthr = 0;
if (finfo.isFile())
{
pdthr = new ProtocolDataTransfer(item, pfName, 0,
port, m_userItem->IpAddress(),
ProtocolDataTransfer::DoSendFile);
connect(&m_timer, SIGNAL(timeout()), pdthr, SLOT(slotUpdate()));
}
else if (finfo.isDir())
{
pdthr = new ProtocolDataTransfer(item, pfName,
port, m_userItem->IpAddress(),
ProtocolDataTransfer::DoSendDir);
connect(&m_timer, SIGNAL(timeout()), pdthr, SLOT(slotUpdate()));
}
if (!pdthr)
{
return;
}
item->setData(COL_NAME, TransThreadRole, (quint64)pdthr);
if (!m_timer.isActive())
{
m_timer.start(1000);
}
pdthr->start();
}
else if (ProfileWithProperties const* pds = static_cast<ProfileWithProperties const*>(m->find(IMProto::DirSend)))
{
QString const& sn = QString::fromUtf8(pds->property_data(IMProto::EventSn));
QString const& kName = QString::fromUtf8(pds->property_data(IMProto::FileKey));
QString const& dName = QString::fromUtf8(pds->property_data(IMProto::FileName));
quint64 siz = QString::fromUtf8(pds->property_data(IMProto::FileSize)).toULongLong();
//u16 port = QString::fromUtf8(pfs->property_data(IMProto::ListenPort)).toUShort();
QTreeWidgetItem* root = new QTreeWidgetItem;
this->addTopLevelItem(root);
m_RecvItemMap.insert(sn, root);
QString const& sizstr = tr("unknown");
root->setText(COL_NAME, dName);
root->setText(COL_SIZE, sizstr);
root->setText(COL_TYPE, tr("Dir"));
root->setText(COL_STATE, tr("waiting to receive"));
root->setText(COL_PERCENT, "0%");
root->setData(COL_NAME, FileKeyRole, kName);
root->setData(COL_NAME, FileSizeRole, siz);
root->setData(COL_NAME, DirFileRole, "DIR");
root->setData(COL_NAME, SendRecvRole, "R");
root->setData(COL_NAME, TransStateRole, "WAIT");
root->setData(COL_NAME, TransItemSNRole, sn);
root->setIcon(COL_NAME, QIcon(DataAccess::SkinPath() + "/download-folder.png"));
root->setIcon(COL_STATE, QIcon(DataAccess::SkinPath() + "/clock.png"));
emit sigTips(tr("The other request to send %1:%2. Please switch to file transferring window to process.")
.arg(tr("Dir"))
.arg(dName));
}
else if (PDirFileSendCancel const* pfsc = static_cast<PDirFileSendCancel const*>(m->find(IMProto::DirFileSendCancel)))
{
QString const& sn = QString::fromUtf8(pfsc->property_data(IMProto::EventSn));
QTreeWidgetItem* item = m_RecvItemMap.value(sn);
if (!item)
{
//QMessageBox::critical(this, tr("Error"), tr("Unable to find item in file RecvItemMap. key: %1.").arg(keys[2]));
return;
}
QString tips = tr("The other canceled sending %1: %2.").arg(item->text(COL_TYPE)).arg(item->text(COL_NAME));
emit sigTips(tips);
m_RecvItemMap.remove(sn);
QString const& tsr = item->data(COL_NAME, TransStateRole).toString();
if (tsr == "START")
{
ProtocolDataTransfer* t = (ProtocolDataTransfer*)item->data(COL_NAME, TransThreadRole).toULongLong();
disconnect(&m_timer, SIGNAL(timeout()), t, SLOT(slotUpdate()));
t->slotCancel();
}
if (!delItemWhenFinished)
{
item->setData(COL_NAME, TransStateRole, "CANCEL");
item->setIcon(COL_STATE, QIcon(DataAccess::SkinPath() + "/warning.png"));
item->setText(COL_STATE, tr("Canceled"));
}
else
{
this->takeTopLevelItem(this->indexOfTopLevelItem(item));
}
}
else if (ProfileWithProperties const* pfrc = static_cast<ProfileWithProperties const*>(m->find(IMProto::DirFileRecvCancel)))
{
QString const& sn = QString::fromUtf8(pfrc->property_data(IMProto::EventSn));
QTreeWidgetItem* item = m_SendItemMap.value(sn, 0);
if (!item)
{
//QMessageBox::critical(this, tr("Error"), tr("Unable to find item in file SendItemMap. key: %1.").arg(keys[2]));
return;
}
QString tips = tr("The other canceled receiving %1: %2.").arg(item->text(COL_TYPE)).arg(item->text(COL_NAME));
emit sigTips(tips);
m_SendItemMap.remove(sn);
QString const& tsr = item->data(COL_NAME, TransStateRole).toString();
if (tsr == "START")
{
ProtocolDataTransfer* t = (ProtocolDataTransfer*)item->data(COL_NAME, TransThreadRole).toLongLong();
disconnect(&m_timer, SIGNAL(timeout()), t, SLOT(slotUpdate()));
t->slotCancel();
}
if (!delItemWhenFinished)
{
item->setData(COL_NAME, TransStateRole, "CANCEL");
item->setIcon(COL_STATE, QIcon(DataAccess::SkinPath() + "/warning.png"));
item->setText(COL_STATE, tr("Canceled"));
}
else
{
this->takeTopLevelItem(this->indexOfTopLevelItem(item));
}
}
}
QTreeWidgetItem*
ProtocolDataWidget::addItem(const QString& fileInfo, const QString& sn, const QString& kName)
{
QStringList keys = ParseFileInfoString(fileInfo);
quint64 siz = keys[1].toULongLong();
QString state = tr("wait sending");
QString sizstr = GetFormatedFileSize(siz);
QTreeWidgetItem* root = new QTreeWidgetItem;
this->addTopLevelItem(root);
m_SendItemMap.insert(sn, root);
if (keys[0] == "DIR")
{
sizstr = tr("unknown");
}
root->setText(COL_NAME, keys[2]);
root->setText(COL_SIZE, sizstr);
root->setText(COL_TYPE, (keys[0] == "DIR")?tr("Dir"):tr("File"));
root->setText(COL_STATE, state);
root->setText(COL_PERCENT, "0%");
root->setData(COL_NAME, FileKeyRole, kName);
root->setData(COL_NAME, FileSizeRole, siz);
root->setData(COL_NAME, DirFileRole, keys[0]);
root->setData(COL_NAME, SendRecvRole, "S");
root->setData(COL_NAME, TransStateRole, "WAIT");
root->setData(COL_NAME, TransItemSNRole, sn);
if (keys[0] == "DIR")
{
root->setIcon(COL_NAME, QIcon(DataAccess::SkinPath() + "/upload-folder.png"));
}
else
{
root->setIcon(COL_NAME, QIcon(DataAccess::SkinPath() + "/upload-file.png"));
}
root->setIcon(COL_STATE, QIcon(DataAccess::SkinPath() + "/clock.png"));
return root;
}
void
ProtocolDataWidget::slotSave()
{
QTreeWidgetItem* item = this->currentItem();
if (!item)
{
QMessageBox::information(this, tr("Tips"), tr("Please choose the item that you want to operate."));
return;
}
QString const& kName = item->data(COL_NAME, FileKeyRole).toString();
if (m_userItem->SendingFilesCollection()->value(kName, "").isEmpty())
{
QMessageBox::critical(this, tr("Error"), tr("Unable to find key: %1 in ReceivingFilesCollection.").arg(kName));
return;
}
QString const& fType = item->data(COL_NAME, DirFileRole).toString();
QString const& srr = item->data(COL_NAME, SendRecvRole).toString();
QString const& state = item->data(COL_NAME, TransStateRole).toString();
if (srr == "S")
{
QMessageBox::information(this, tr("Tips"), tr("You can't save a send item."));
return;
}
if (state == "WAIT")
{
quint64 siz = item->data(0, FileSizeRole).toULongLong();
quint16 port = DataAccess::genTcpPort();
ProtocolDataTransfer* pdthr = 0;
if (fType == "FILE")
{
QString const &saveName = QFileDialog::getSaveFileName(this, tr("Choose Save File Name"),
m_DataAccess.SaveDir() + "/" + item->text(COL_NAME));
if (saveName.isEmpty())
{
return;
}
QFileInfo finfo(saveName);
m_DataAccess.updateSaveDir(finfo.absolutePath());
pdthr = new ProtocolDataTransfer(item, saveName, siz, port);
connect(&m_timer, SIGNAL(timeout()), pdthr, SLOT(slotUpdate()));
}
else if (fType == "DIR")