~mateo-salta/nitroshare/nitroshare

« back to all changes in this revision

Viewing changes to src/file/CFileSender.cpp

  • Committer: Nathan Osman
  • Date: 2012-06-28 03:33:35 UTC
  • Revision ID: admin@quickmediasolutions.com-20120628033335-w6t0h1ltwr21fv6b
Implemented progress of individual file transfers.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
CFileSender::CFileSender(QObject * parent, QStringList filenames)
27
27
    : CBasicSocket(parent), m_state(WaitingForConnection),
28
 
      m_total_uncompressed(0), m_current_uncompressed(0)
 
28
      m_total_uncompressed_bytes(0), m_total_uncompressed_bytes_so_far(0)
29
29
{
30
 
    connect(this, SIGNAL(connected()),      SLOT(OnConnected()));
31
 
    connect(this, SIGNAL(Data(QByteArray)), SLOT(OnData(QByteArray)));
 
30
    connect(this, SIGNAL(bytesWritten(qint64)), SLOT(OnBytesWritten(qint64)));
 
31
    connect(this, SIGNAL(connected()),          SLOT(OnConnected()));
 
32
    connect(this, SIGNAL(Data(QByteArray)),     SLOT(OnData(QByteArray)));
32
33
 
33
34
    /* Immediately begin preparing the headers for the transfer. */
34
35
    PrepareHeaders(filenames);
48
49
    connectToHost(machine.address, machine.port);
49
50
}
50
51
 
 
52
void CFileSender::OnBytesWritten(qint64 amount)
 
53
{
 
54
    m_current_file_bytes_so_far += amount;
 
55
 
 
56
    /* Determine what percentage of the file has been transferred so far. */
 
57
    double percent_transferred = CalculateProgress(m_current_file_bytes_so_far, m_current_file_transfer_bytes);
 
58
 
 
59
    /* Next, determine what percentage of the total transfer this file represents. */
 
60
    double percent_of_total = CalculateProgress(m_current_file_uncompressed_bytes, m_total_uncompressed_bytes);
 
61
 
 
62
    /* Lastly add that amount to what has been transferred so far for the final number. */
 
63
    double progress = CalculateProgress(m_total_uncompressed_bytes_so_far, m_total_uncompressed_bytes);
 
64
 
 
65
    emit Progress((progress + percent_transferred * percent_of_total) * 100.0);
 
66
 
 
67
    /* If we've reached the end of the current file, then add the _uncompressed_
 
68
       size of this file to the total. */
 
69
    if(m_current_file_bytes_so_far >= m_current_file_transfer_bytes)
 
70
        m_total_uncompressed_bytes_so_far += m_current_file_uncompressed_bytes;
 
71
}
 
72
 
51
73
void CFileSender::OnConnected()
52
74
{
53
75
    qDebug() << "Connection with remote server established.";
128
150
        m_headers.append(header);
129
151
 
130
152
        /* Add the uncompressed size to our running total. */
131
 
        m_total_uncompressed += info.size();
 
153
        m_total_uncompressed_bytes += info.size();
132
154
    }
133
155
}
134
156
 
138
160
 
139
161
    /* Now we actually begin the transmission of the file. */
140
162
    QByteArray contents;
141
 
    qint64 uncompressed_size;
142
 
 
143
 
    if(header->GetContents(contents, uncompressed_size))
 
163
    if(header->GetContents(contents, m_current_file_uncompressed_bytes))
144
164
    {
145
165
        /* Calculate the checksum if requested. */
146
166
        if(header->GetChecksum())
151
171
            qDebug() << "Calculated CRC:" << crc;
152
172
        }
153
173
 
 
174
        /* Reset the transfer statistics. */
 
175
        m_current_file_bytes_so_far   = 0;
 
176
        m_current_file_transfer_bytes = contents.size();
 
177
 
154
178
        /* Send the actual data. */
155
179
        SendData(contents);
156
 
 
157
 
        /* Emit a signal indicating progress. */
158
 
        m_current_uncompressed += uncompressed_size;
159
 
        emit Progress((double)m_current_uncompressed / (double)m_total_uncompressed * 100.0);
160
180
    }
161
181
    else
162
182
        disconnectFromHost();