forked from KangLin/RabbitIm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileTransferQXmpp.cpp
142 lines (121 loc) · 3.47 KB
/
FileTransferQXmpp.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
#include "FileTransferQXmpp.h"
#include "Global/Global.h"
#include "qxmpp/QXmppUtils.h"
CFileTransferQXmpp::CFileTransferQXmpp(QXmppTransferJob *pJob, QObject *parent) :
CFileTransfer(parent)
{
m_pJob = pJob;
m_DoneSize = 0;
m_localFile = pJob->localFileUrl();
m_szFileName = pJob->fileName();
m_nFileSize = pJob->fileSize();
m_Direction = (Direction)pJob->direction();
m_State = (State)pJob->state();
m_Error = (Error)pJob->error();
m_szId = QXmppUtils::jidToBareJid(pJob->jid());
bool check = false;
check = connect(pJob, SIGNAL(error(QXmppTransferJob::Error)),
SLOT(slotError(QXmppTransferJob::Error)));
Q_ASSERT(check);
check = connect(pJob, SIGNAL(stateChanged(QXmppTransferJob::State)),
SLOT(slotStateChanged(QXmppTransferJob::State)));
Q_ASSERT(check);
check = connect(pJob, SIGNAL(progress(qint64,qint64)),
SLOT(slotProgress(qint64,qint64)));
Q_ASSERT(check);
}
CFileTransferQXmpp::~CFileTransferQXmpp()
{
LOG_MODEL_DEBUG("CFileTransferQXmpp", "CFileTransferQXmpp::~CFileTransferQXmpp");
}
int CFileTransferQXmpp::Accept(const QString &szFile)
{
int nRet = 0;
if(m_pJob)
m_pJob->accept(szFile);
m_localFile = QUrl::fromLocalFile(szFile);
return nRet;
}
int CFileTransferQXmpp::Abort()
{
if(m_pJob)
m_pJob->abort();
return 0;
}
QUrl CFileTransferQXmpp::GetLocalFileUrl()
{
if(m_pJob)
m_pJob->localFileUrl();
return m_localFile;
}
QString CFileTransferQXmpp::GetFile()
{
return m_szFileName;
}
qint64 CFileTransferQXmpp::GetFileSize()
{
return m_nFileSize;
}
CFileTransfer::Direction CFileTransferQXmpp::GetDirection()
{
return m_Direction;
}
QString CFileTransferQXmpp::GetDescription()
{
if(m_pJob)
return m_pJob->fileInfo().description();
return QString();
}
CFileTransfer::State CFileTransferQXmpp::GetState()
{
if(m_pJob)
return (State)m_pJob->state();
return m_State;
}
CFileTransfer::Error CFileTransferQXmpp::GetError()
{
if(m_pJob)
return (Error)m_pJob->error();
return m_Error;
}
qint64 CFileTransferQXmpp::GetSpeed()
{
if(m_pJob)
return m_pJob->speed();
return 0;
}
qint64 CFileTransferQXmpp::GetDoneSize()
{
return m_DoneSize;
}
void CFileTransferQXmpp::slotError(QXmppTransferJob::Error error)
{
LOG_MODEL_DEBUG("CFileTransferQXmpp", "Error:%d", error);
m_Error = (Error)error;
emit sigUpdate();
if(NoError != error)
emit sigFinished(GetId(), GetFileTranserId());
}
void CFileTransferQXmpp::slotProgress(qint64 done, qint64 total)
{
m_DoneSize = done;
QDateTime now = QDateTime::currentDateTime();
if (m_LastUpdateTime.secsTo(now) < 1) //update every 1s
return;
m_LastUpdateTime = now;
if(total != m_pJob->fileSize())
{
LOG_MODEL_ERROR("CFileTransferQXmpp", "file size is equet:total:%d;fileSize:%d", total, m_pJob->fileSize());
}
emit sigUpdate();
}
void CFileTransferQXmpp::slotStateChanged(QXmppTransferJob::State state)
{
LOG_MODEL_DEBUG("CFileTransferQXmpp", "state:%d", state);
m_State = (State) state;
if(TransferState == m_State)
m_LastUpdateTime = QDateTime::currentDateTime();
emit sigUpdate();
if(FinishedState == m_State)
emit sigFinished(GetId(), GetFileTranserId());
}