summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/engine/event.h
blob: 0e05f5fa8f4a6be23e886ca123a4f90f350f17ab (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
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
/*
 * This file is part of the KFTPGrabber project
 *
 * Copyright (C) 2003-2006 by the KFTPGrabber developers
 * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
 *
 * 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
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  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, write to the Free Software
 * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 *
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */

#ifndef KFTPNETWORKEVENT_H
#define KFTPNETWORKEVENT_H

#include <ntqobject.h>
#include <ntqevent.h>
#include <ntqshared.h>

#include "directorylisting.h"

namespace KFTPEngine {

/**
 * Engine reset codes. TODO description of each reset code.
 */
enum ResetCode {
  Ok,
  UserAbort,
  Failed,
  FailedSilently
};

/**
 * Engine error codes. TODO: description of each error code.
 */
enum ErrorCode {
  ConnectFailed,
  LoginFailed,
  PermissionDenied,
  FileNotFound,
  OperationFailed,
  ListFailed,
  FileOpenFailed
};

/**
 * This class is used for event parameter passing between the socket
 * thread and the main thread. It supports multiple parameter types.
 *
 * @author Jernej Kos <kostko@jweb-network.net>
 */
class EventParameter {
public:
    /**
     * Parameter type enum.
     */
    enum Type {
      ParamString,
      ParamUrl,
      ParamDirListing,
      ParamDirTree,
      ParamErrorCode,
      ParamSize,
      ParamData
    };
    
    EventParameter();
    
    /**
     * Constructs a new string parameter.
     *
     * @param string The TQString value
     */
    EventParameter(const TQString &string);
    
    /**
     * Construct a new url parameter.
     *
     * @param url The KURL value
     */
    EventParameter(const KURL &url);
    
    /**
     * Construct a new directory listing parameter.
     *
     * @param listing The DirectoryListing value
     */
    EventParameter(DirectoryListing listing);
    
    /**
     * Construct a new directory tree parameter.
     *
     * @param tree The DirectoryTree value
     */
    EventParameter(DirectoryTree tree);
    
    /**
     * Construct a new error code parameter.
     *
     * @param error The ErrorCode value
     */
    EventParameter(ErrorCode error);
    
    /**
     * Construct a new filesize parameter.
     *
     * @param size The filesize_t value
     */
    EventParameter(filesize_t size);
    
    /**
     * Constructs a new data parameter.
     *
     * @param data A pointer to some data.
     */
    EventParameter(void *data);
    
    /**
     * Returns the parameter's string value.
     *
     * @return Parameter's string value
     */
    TQString asString() const;
    
    /**
     * Returns the parameter's url value.
     *
     * @return Parameter's url value
     */
    KURL asUrl() const;
    
    /**
     * Returns the parameter's directory listing value.
     *
     * @return Parameter's directory listing value
     */
    DirectoryListing asDirectoryListing() const;
    
    /**
     * Returns the parameter's directory tree value.
     *
     * @return Parameter's directory tree value.
     */
    DirectoryTree asDirectoryTree() const;
    
    /**
     * Returns the parameter's error code value.
     *
     * @return Parameter's error code value
     */
    ErrorCode asErrorCode() const;
    
    /**
     * Returns the parameter's filesize value.
     *
     * @return Parameter's filesize value
     */
    filesize_t asFileSize() const;
    
    /**
     * Returns the parameter's boolean value.
     *
     * @return Parameter's boolean value
     */
    bool asBoolean() const;
    
    /**
     * Returns raw parameter data pointer.
     *
     * @return Raw parameter data pointer
     */
    void *asData() const;
private:
    Type m_type;
    
    TQString m_string;
    KURL m_url;
    DirectoryListing m_directoryListing;
    DirectoryTree m_directoryTree;
    ErrorCode m_errorCode;
    filesize_t m_fileSize;
    void *m_data;
};

/**
 * A wakeup event is a special type event used to transfer some response from
 * the GUI to the engine that has been temporarly suspended. After receiving
 * this event, the current command handler's wakeup() method will be called
 * with this event as a parameter.
 *
 * @author Jernej Kos <kostko@jweb-network.net>
 */
class WakeupEvent {
public:
    /**
     * Possible wakeup event types. Each type should subclass this class to
     * provide any custom methods needed.
     */
    enum Type {
      WakeupFileExists,
      WakeupPubkey
    };
    
    /**
     * Constructs a new wakeup event of specified type.
     *
     * @param type Event type
     */
    WakeupEvent(Type type) : m_type(type) {}
private:
    Type m_type;
};

/**
 * A file exists wakeup event that is used to continue pending transfers.
 *
 * @author Jernej Kos <kostko@jweb-network.net>
 */
class FileExistsWakeupEvent : public WakeupEvent {
public:
    /**
     * Possible actions the engine can take.
     */
    enum Action {
      Overwrite,
      Rename,
      Resume,
      Skip
    };
    
    /**
     * Constructs a new file exists wakeup event with Skip action as default.
     */
    FileExistsWakeupEvent() : WakeupEvent(WakeupFileExists), action(Skip) {}
    
    Action action;
    TQString newFileName;
};

/**
 * A public key password request event for SFTP connections.
 *
 * @author Jernej Kos <kostko@jweb-network.net>
 */
class PubkeyWakeupEvent : public WakeupEvent {
public:
    /**
     * Constructs a new public key wakeup event.
     */
    PubkeyWakeupEvent() : WakeupEvent(WakeupPubkey) {}
    
    TQString password;
};

/**
 * This class represents an event that is passed to the EventHandler for
 * processing. It can have multiple EventParameters.
 *
 * @author Jernej Kos <kostko@jweb-network.net>
 */
class Event : public TQCustomEvent {
public:
    enum Type {
      EventMessage,
      EventCommand,
      EventResponse,
      EventMultiline,
      EventRaw,
      EventDirectoryListing,
      EventDisconnect,
      EventError,
      EventConnect,
      EventReady,
      EventState,
      EventScanComplete,
      EventRetrySuccess,
      EventReloadNeeded,
      
      // Transfer events
      EventTransferComplete,
      EventResumeOffset,
      
      // Events that require wakeup events
      EventFileExists,
      EventPubkeyPassword
    };

    /**
     * Construct a new event with a parameter list.
     *
     * @param params Parameter list
     */
    Event(Type type, TQValueList<EventParameter> params);
    ~Event();
    
    /**
     * Return the event's type.
     *
     * @return Event's type
     */
    Type type() { return m_type; }
    
    /**
     * Returns the parameter with a specific index.
     *
     * @param index Parameter's index
     * @return An EventParameter object
     */
    EventParameter getParameter(int index) { return m_params[index]; }
protected:
    Type m_type;
    TQValueList<EventParameter> m_params;
};

class Thread;

/**
 * This class handles events receieved from the thread and passes them
 * on to the GUI as normal TQt signals.
 *
 * @author Jernej Kos <kostko@jweb-network.net>
 */
class EventHandler : public TQObject {
Q_OBJECT
public:
    /**
     * Construct a new event handler.
     *
     * @param thread The thread this event handler belongs to
     */
    EventHandler(Thread *thread);
protected:
    void customEvent(TQCustomEvent *e);
protected:
    Thread *m_thread;
signals:
    void engineEvent(KFTPEngine::Event *event);
    
    void connected();
    void disconnected();
    void gotResponse(const TQString &text);
    void gotRawResponse(const TQString &text);
};

}

#endif