summaryrefslogtreecommitdiffstats
path: root/kget/slave.cpp
blob: 60933dcda8d9743ca0009ef0ec3dee72e09e1629 (plain)
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
/***************************************************************************
*                                slave.cpp
*                             -------------------
*
*    Revision     : $Id$
*    begin        : Tue Jan 29 2002
*    copyright    : (C) 2002 by Patrick Charbonnier
*                 : Based On Caitoo v.0.7.3 (c) 1998 - 2000, Matej Koss
*    email        : [email protected]
*
****************************************************************************/

/***************************************************************************
 *
 *   This program 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 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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.
 *
 ***************************************************************************/

#include <tdeapplication.h>

#include "getfilejob.h"
#include "slave.h"
#include "slaveevent.h"
#include "transfer.h"

#include <assert.h>

Slave::Slave(Transfer * _parent, const KURL & _src, const KURL & _dest)
    : TQObject(),
      TQThread()
{
    // FIXME
    // KGet uses an unconventional threading model that relies on TDEIO slave execution from the main GUI thread
    disableThreadPostedEvents(true);

    mDebug << ">>>>Entering" << endl;
    copyjob = NULL;
    m_src = _src;
    m_dest = _dest;
    m_parent = _parent;

    nPendingCommand = 0;

    mDebug << ">>>>Leaving" << endl;
}

Slave::~Slave()
{}

void Slave::Op(SlaveCommand _cmd)
{
    mDebugIn << " _cmd = " << _cmd << endl;

    if ( !running() ) { // start on demand
        start();
        moveToThread(this);
    }

    mutex.lock();
    stack.push(_cmd);
    nPendingCommand++;
    worker.wakeOne();
    mutex.unlock();

    mDebugOut << endl;
}

/** No descriptions */
void Slave::PostMessage(SlaveResult _event, TQ_ULLONG _data)
{
    SlaveEvent *e1 = new SlaveEvent(m_parent, _event, _data);

    TQApplication::postEvent(kapp->mainWidget(), (TQEvent *) e1);
}

void Slave::PostMessage(SlaveResult _event, const TQString & _msg)
{
    SlaveEvent *e1 = new SlaveEvent(m_parent, _event, _msg);

    TQApplication::postEvent(kapp->mainWidget(), (TQEvent *) e1);
    mDebug << "Msg:" << "_msg = " << _msg << endl;
}

void Slave::InfoMessage(const TQString & _msg)
{
    SlaveEvent *e1 = new SlaveEvent(m_parent, SLV_INFO, _msg);

    TQApplication::postEvent(kapp->mainWidget(), (TQEvent *) e1);
    mDebug << "Infor Msg:" << "_msg = " << _msg << endl;
}

void Slave::run()
{
    mDebugIn << endl;

    SlaveCommand cmd;
    bool running = true;

    while (running)
    {
        if (!nPendingCommand) {
            worker.wait();
        }
        switch (cmd = fetch_cmd())
        {
            case RESTART:
                // fall through
            case RETR:
                mDebug << " FETCHED COMMAND       RETR" << endl;
                PostMessage(SLV_RESUMED);
                break;

            case RETR_CACHE:
                mDebug << " FETCHED COMMAND       RETR_CACHE" << endl;
                break;

            case PAUSE:
                mDebug << " FETCHED COMMAND       PAUSE" << endl;
                PostMessage(SLV_PAUSED);
                break;

            case KILL:
                mDebug << " FETCHED COMMAND      KILL" << endl;
                running = false;
                // no message posted
                break;

            case REMOVE:
                mDebug << " FETCHED COMMAND       REMOVE" << endl;
                running = false;
                PostMessage(SLV_REMOVED);
                break;

            case SCHEDULE:
                mDebug << " FETCHED COMMAND       SCHEDULE" << endl;
                PostMessage(SLV_SCHEDULED);
                break;

            case DELAY:
                mDebug << " FETCHED COMMAND       DELAY" << endl;
                PostMessage(SLV_DELAYED);
                break;

            case NOOP:
                mDebug << "FETCHED COMMAND        NOOP, i.e. empty stack" << endl;
                running = false;
                break;

            default:
                mDebug << " UNKNOWN COMMAND DIE...." << endl;
                assert(0);
        }
    }

    assert(!copyjob);
    mDebugOut << endl;
}


Slave::SlaveCommand Slave::fetch_cmd()
{
    mutex.lock();
    SlaveCommand cmd = NOOP;
    if ( !stack.isEmpty() )
    {
        nPendingCommand--;
        cmd = stack.pop();
    }
    mutex.unlock();
    return cmd;
}


void Slave::Connect()
{
    mDebugIn << endl;

    connect(copyjob, TQT_SIGNAL(canceled(TDEIO::Job *)), TQT_SLOT(slotCanceled(TDEIO::Job *)));
    connect(copyjob, TQT_SIGNAL(connected(TDEIO::Job *)), TQT_SLOT(slotConnected(TDEIO::Job *)));
    connect(copyjob, TQT_SIGNAL(result(TDEIO::Job *)), TQT_SLOT(slotResult(TDEIO::Job *)));

    connect(copyjob, TQT_SIGNAL(totalSize(TDEIO::Job *, TDEIO::filesize_t)), TQT_SLOT(slotTotalSize(TDEIO::Job *, TDEIO::filesize_t)));

    connect(copyjob, TQT_SIGNAL(processedSize(TDEIO::Job *, TDEIO::filesize_t)), TQT_SLOT(slotProcessedSize(TDEIO::Job *, TDEIO::filesize_t)));

    connect(copyjob, TQT_SIGNAL(speed(TDEIO::Job *, unsigned long)), TQT_SLOT(slotSpeed(TDEIO::Job *, unsigned long)));

    connect(copyjob, TQT_SIGNAL(infoMessage(TDEIO::Job *, const TQString &)), TQT_SLOT(slotInfoMessage(TDEIO::Job *, const TQString &)));

    mDebugOut << endl;
}


void Slave::slotCanceled(TDEIO::Job *)
{
    mDebugIn << endl;


    mDebugOut << endl;
}

void Slave::slotConnected(TDEIO::Job *)
{
    mDebugIn << endl;


    mDebugOut << endl;
}

void Slave::slotResult(TDEIO::Job * job)
{
    mDebugIn << endl;

    assert(copyjob == job);
    copyjob=0L;
    TDEIO::Error error=TDEIO::Error(job->error());
    if (!error) {
        PostMessage(SLV_FINISHED);
    }
    else {
        if (m_parent->getMode() == Transfer::MD_NEW)
        {
            PostMessage(SLV_NOTINCACHE);
            return;
        }
        TQString tmsg="<font color=\"red\"> <b>" + job->errorString() + \
                "</b></font>";
        InfoMessage(tmsg);
        if (m_parent->retryOnError() && \
            ((error==TDEIO::ERR_COULD_NOT_LOGIN) || (error==TDEIO::ERR_SERVER_TIMEOUT))) {
            //Timeout or login error
            PostMessage(SLV_ERROR);
        }
        else if (m_parent->retryOnBroken() && (error==TDEIO::ERR_CONNECTION_BROKEN)) {
            // Connection Broken
            PostMessage(SLV_BROKEN);
        }
        else {
            job->showErrorDialog();
            PostMessage(SLV_DELAYED);
        }
    }
    mDebugOut << endl;
}


void Slave::slotSpeed(TDEIO::Job *, unsigned long lSpeed)
{
    // mDebugIn<<endl;
    PostMessage(SLV_PROGRESS_SPEED, lSpeed);
    // mDebugOut<<endl;

}

void Slave::slotTotalSize(TDEIO::Job *, TDEIO::filesize_t _total_size)
{
    mDebugIn << "= " << _total_size << endl;

    if ((int)_total_size == 0)//shouldn't happen, but does
        return;

    if (m_parent->getMode() != Transfer::MD_NEW)
    {
    PostMessage(SLV_CAN_RESUME, copyjob->getCanResume());
    PostMessage(SLV_CONNECTED);
    }
    else
        InfoMessage("checking if file is in cache...yes");
    PostMessage(SLV_TOTAL_SIZE, _total_size);
    mDebugOut << endl;
}

void Slave::slotProcessedSize(TDEIO::Job *, TDEIO::filesize_t _processed_size)
{
    // mDebugIn<<endl;
    PostMessage(SLV_PROGRESS_SIZE, _processed_size);

    // mDebugOut<<endl;
}

void Slave::slotInfoMessage(TDEIO::Job *, const TQString & _msg)
{
    mDebugIn << "MSG=" << _msg << endl;
    InfoMessage(_msg);
    mDebugOut << endl;
}

#include "slave.moc"