Statistics
| Revision:

root / trunk / server_qt / nntpgrab_qt.cpp @ 1919

History | View | Annotate | Download (12.7 KB)

1
#include 
2
#ifdef WIN32
3
#include 
4
#endif
5

                
6
#include "nntpgrab_qt.h"
7
#include "nntpgrab_utils.h"
8

                
9
QNNTPGrabThread::QNNTPGrabThread(NntpgrabCore *core)
10
{
11
    this->core = core;
12
}
13

                
14
QNNTPGrabThread::~QNNTPGrabThread()
15
{
16
    if (this->isRunning()) {
17
        ng_event_handler_loop_quit();
18
        this->wait();
19
    }
20
}
21

                
22
void QNNTPGrabThread::run()
23
{
24
    ng_event_handler_loop_run();
25
}
26

                
27
QNNTPGrab::QNNTPGrab()
28
{
29
    core = NULL;
30
    qRegisterMetaType("QNNTPGrab::NNTPDisconnectType");
31
    qRegisterMetaType("QNNTPGrab::TaskState");
32
    qRegisterMetaType("QNNTPGrab::LogLevel");
33
    qRegisterMetaType("QNNTPGrab::SchedularState");
34
    qRegisterMetaType >("QList");
35
}
36

                
37
QNNTPGrab::~QNNTPGrab()
38
{
39
    if (core) {
40
        nntpgrab_core_destroy(core);
41
        core = NULL;
42
    }
43
}
44

                
45
bool QNNTPGrab::init(char **errmsg, char **warnings)
46
{
47
    nntpgrab_utils_perform_base_initialization();
48

                
49
    core = nntpgrab_core_new();
50

                
51
    if (!nntpgrab_core_init(core, NNTPGRAB_API_VERSION, errmsg, warnings)) {
52
        nntpgrab_core_destroy(core);
53
        core = NULL;
54
        return false;
55
    }
56

                
57
    nntpgrab_core_set_emit_log_messages(core, TRUE);
58

                
59
    ng_signal_connect(core, "config_changed", NG_CALLBACK(configChangedWrapper), this);
60
    ng_signal_connect(core, "all_downloads_completed", NG_CALLBACK(allDownloadsCompletedWrapper), this);
61
    ng_signal_connect(core, "part_download_start", NG_CALLBACK(partDownloadStartWrapper), this);
62
    ng_signal_connect(core, "part_done", NG_CALLBACK(partDoneWrapper), this);
63
    ng_signal_connect(core, "part_failed", NG_CALLBACK(partFailedWrapper), this);
64
    ng_signal_connect(core, "traffic_monitor_update", NG_CALLBACK(trafficMonitorUpdateWrapper), this);
65
    ng_signal_connect(core, "part_progress_update", NG_CALLBACK(partProgressUpdateWrapper), this);
66
    ng_signal_connect(core, "collection_added", NG_CALLBACK(collectionAddedWrapper), this);
67
    ng_signal_connect(core, "collection_modified", NG_CALLBACK(collectionModifiedWrapper), this);
68
    ng_signal_connect(core, "collection_removed", NG_CALLBACK(collectionRemovedWrapper), this);
69
    ng_signal_connect(core, "file_added", NG_CALLBACK(fileAddedWrapper), this);
70
    ng_signal_connect(core, "file_removed", NG_CALLBACK(fileRemovedWrapper), this);
71
    ng_signal_connect(core, "file_download_state_update", NG_CALLBACK(fileDownloadStateUpdateWrapper), this);
72
    ng_signal_connect(core, "file_state_changed", NG_CALLBACK(fileStateChangedWrapper), this);
73
    ng_signal_connect(core, "connection_connecting", NG_CALLBACK(connectionConnectingWrapper), this);
74
    ng_signal_connect(core, "connection_connected", NG_CALLBACK(connectionConnectedWrapper), this);
75
    ng_signal_connect(core, "connection_disconnect", NG_CALLBACK(connectionDisconnectWrapper), this);
76
    ng_signal_connect(core, "schedular_state_changed", NG_CALLBACK(schedularStateChangedWrapper), this);
77
    ng_signal_connect(core, "log_message", NG_CALLBACK(logMessageWrapper), this);
78
    ng_signal_connect(core, "task_moved", NG_CALLBACK(fileMovedWrapper), this);
79
    ng_signal_connect(core, "collection_moved", NG_CALLBACK(collectionMovedWrapper), this);
80
    ng_signal_connect(core, "collection_downloaded", NG_CALLBACK(collectionDownloadedWrapper), this);
81
    ng_signal_connect(core, "plugin_loaded", NG_CALLBACK(pluginLoadedWrapper), this);
82
    ng_signal_connect(core, "plugin_unloaded", NG_CALLBACK(pluginUnloadedWrapper), this);
83
    ng_signal_connect(core, "plugin_event", NG_CALLBACK(pluginEventWrapper), this);
84

                
85
    /* If Qt is compiled with GLib support then there's already an GLib main loop active */
86
    /* If this isn't the case, start one in a seperate thread */
87
    if (ng_event_handler_loop_depth() == 0) {
88
        this->callbacks_thread = new QNNTPGrabThread(core);
89
        this->callbacks_thread->start();
90
    }
91

                
92
    return true;
93
}
94

                
95
void QNNTPGrab::configChangedWrapper(NntpgrabCore *core, void *data)
96
{
97
    Q_UNUSED(core);
98

                
99
    emit ((QNNTPGrab*) data)->configChanged();
100
}
101

                
102
void QNNTPGrab::allDownloadsCompletedWrapper(NntpgrabCore *core, void *data)
103
{
104
    Q_UNUSED(core);
105

                
106
    emit ((QNNTPGrab*) data)->allDownloadsCompleted();
107
}
108

                
109
void QNNTPGrab::partDownloadStartWrapper(NntpgrabCore *core, const char *servername, int conn_id, const char *collection_name, const char *subject, int part_num, void *data)
110
{
111
    Q_UNUSED(core);
112

                
113
    emit ((QNNTPGrab*) data)->partDownloadStart(QString(servername), conn_id, QString(collection_name), QString(subject), part_num);
114
}
115

                
116
void QNNTPGrab::partDoneWrapper(NntpgrabCore *core, const char *servername, int conn_id, const char *collection_name, const char *subject, int part_num, int size, void *data)
117
{
118
    Q_UNUSED(core);
119

                
120
    emit ((QNNTPGrab*) data)->partDone(QString(servername), conn_id, QString(collection_name), QString(subject), part_num, size);
121
}
122

                
123
void QNNTPGrab::partFailedWrapper(NntpgrabCore *core, const char *servername, int conn_id, const char *collection_name, const char *subject, int part_num,int size, bool all_servers_tried, void *data)
124
{
125
    Q_UNUSED(core);
126

                
127
    emit ((QNNTPGrab*) data)->partFailed(QString(servername), conn_id, QString(collection_name), QString(subject), part_num, size, all_servers_tried);
128
}
129

                
130
void QNNTPGrab::trafficMonitorUpdateWrapper(NntpgrabCore *core, int bytes_received1, int bytes_received2, int bytes_received3, int bytes_received4, int bytes_received5, int bytes_received6, int bytes_received7, int bytes_received8, int bytes_received9, int bytes_received10, nguint64 stamp, double average, void *data)
131
{
132
    int bytes_received[10];
133

                
134
    Q_UNUSED(core);
135

                
136
    bytes_received[0] = bytes_received1;
137
    bytes_received[1] = bytes_received2;
138
    bytes_received[2] = bytes_received3;
139
    bytes_received[3] = bytes_received4;
140
    bytes_received[4] = bytes_received5;
141
    bytes_received[5] = bytes_received6;
142
    bytes_received[6] = bytes_received7;
143
    bytes_received[7] = bytes_received8;
144
    bytes_received[8] = bytes_received9;
145
    bytes_received[9] = bytes_received10;
146

                
147
    emit ((QNNTPGrab*) data)->trafficMonitorUpdate(bytes_received, stamp, average);
148
}
149

                
150
void QNNTPGrab::partProgressUpdateWrapper(NntpgrabCore *core, const char *servername, int conn_id, const char *collection_name, const char *subject, int part_num, int bytes_downloaded, int bytes_total, void *data)
151
{
152
    Q_UNUSED(core);
153

                
154
    emit ((QNNTPGrab*) data)->partProgressUpdate(QString(servername), conn_id, QString(collection_name), QString(subject), part_num, bytes_downloaded, bytes_total);
155
}
156

                
157
void QNNTPGrab::collectionAddedWrapper(NntpgrabCore *core, const char *collection_name, const char *poster, void *data)
158
{
159
    Q_UNUSED(core);
160

                
161
    emit ((QNNTPGrab*) data)->collectionAdded(QString(collection_name), QString(poster));
162
}
163

                
164
void QNNTPGrab::collectionModifiedWrapper(NntpgrabCore *core, const char *collection_name, const char *poster, void *data)
165
{
166
    Q_UNUSED(core);
167

                
168
    emit ((QNNTPGrab*) data)->collectionModified(QString(collection_name), QString(poster));
169
}
170

                
171
void QNNTPGrab::collectionRemovedWrapper(NntpgrabCore *core, const char *collection_name, void *data)
172
{
173
    Q_UNUSED(core);
174

                
175
    emit ((QNNTPGrab*) data)->collectionRemoved(QString(collection_name));
176
}
177

                
178
void QNNTPGrab::fileAddedWrapper(NntpgrabCore *core, const char *collection_name, const char *subject, const char *poster, nguint64 stamp, nguint64 file_size, nguint64 total_size, nguint64 total_size_remaining, TaskState state, int num_parts, NGList *groups, void *data)
179
{
180
    Q_UNUSED(core);
181

                
182
    /* FIXME: transform groups into a Qt friendly data type */
183
    emit ((QNNTPGrab*) data)->fileAdded(QString(collection_name), QString(subject), QString(poster), (time_t) stamp, file_size, total_size, total_size_remaining, state, num_parts, groups);
184
}
185

                
186
void QNNTPGrab::fileRemovedWrapper(NntpgrabCore *core, const char *collection_name, const char *subject, nguint64 total_size, nguint64 total_size_remaining, void *data)
187
{
188
    Q_UNUSED(core);
189

                
190
    emit ((QNNTPGrab*) data)->fileRemoved(QString(collection_name), QString(subject), total_size, total_size_remaining);
191
}
192

                
193
void QNNTPGrab::fileDownloadStateUpdateWrapper(NntpgrabCore *core, const char *collection_name, const char *subject, int num_parts_total, int num_parts_done, int num_parts_failed, nguint64 file_size, nguint64 file_size_remaining, nguint64 total_size, nguint64 total_size_remaining, void *data)
194
{
195
    Q_UNUSED(core);
196

                
197
    emit ((QNNTPGrab*) data)->fileDownloadStateUpdate(QString(collection_name), QString(subject), num_parts_total, num_parts_done, num_parts_failed, file_size, file_size_remaining, total_size, total_size_remaining);
198
}
199

                
200
void QNNTPGrab::fileStateChangedWrapper(NntpgrabCore *core, const char *collection_name, const char *subject, const char *real_filename, TaskState old_state, TaskState new_state, nguint64 file_size_remaining, nguint64 total_size, nguint64 total_size_remaining, void *data)
201
{
202
    Q_UNUSED(core);
203

                
204
    emit ((QNNTPGrab*) data)->fileStateChanged(QString(collection_name), QString(subject), QString(real_filename), (QNNTPGrab::TaskState) old_state, (QNNTPGrab::TaskState) new_state, file_size_remaining, total_size, total_size_remaining);
205
}
206

                
207
void QNNTPGrab::connectionConnectingWrapper(NntpgrabCore *core, const char *servername, int conn_id, void *data)
208
{
209
    Q_UNUSED(core);
210

                
211
    emit ((QNNTPGrab*) data)->connectionConnecting(QString(servername), conn_id);
212
}
213

                
214
void QNNTPGrab::connectionConnectedWrapper(NntpgrabCore *core, const char *servername, int conn_id, const char *welcome_msg, void *data)
215
{
216
    Q_UNUSED(core);
217

                
218
    emit ((QNNTPGrab*) data)->connectionConnected(QString(servername), conn_id, welcome_msg);
219
}
220

                
221
void QNNTPGrab::connectionDisconnectWrapper(NntpgrabCore *core, const char *servername, int conn_id, NNTPDisconnectType disconnect_type, const char *reason, void *data)
222
{
223
    Q_UNUSED(core);
224

                
225
    emit ((QNNTPGrab*) data)->connectionDisconnect(QString(servername), conn_id, disconnect_type, QString(reason));
226
}
227

                
228
void QNNTPGrab::logMessageWrapper(NntpgrabCore *core, const char *component, NGLogLevel log_level, const char *msg, void *data)
229
{
230
    Q_UNUSED(core);
231

                
232
    emit ((QNNTPGrab*) data)->logMessage(QString(component), (QNNTPGrab::LogLevel) log_level, QString(msg));
233
}
234

                
235
void QNNTPGrab::schedularStateChangedWrapper(NntpgrabCore *core, NGSchedularState new_state, const char *reason, void *data)
236
{
237
    Q_UNUSED(core);
238

                
239
    emit ((QNNTPGrab*) data)->schedularStateChanged((QNNTPGrab::SchedularState) new_state, QString(reason));
240
}
241

                
242
void QNNTPGrab::fileMovedWrapper(NntpgrabCore *core, const char *orig_collection_name, const char *subject, const char *new_collection_name, int old_position, int new_position, void *data)
243
{
244
    Q_UNUSED(core);
245

                
246
    emit ((QNNTPGrab*) data)->fileMoved(QString(orig_collection_name), QString(subject), QString(new_collection_name), old_position, new_position);
247
}
248

                
249
void QNNTPGrab::collectionMovedWrapper(NntpgrabCore *core, const char *collection_name, int old_position, int new_position, void *data)
250
{
251
    Q_UNUSED(core);
252

                
253
    emit ((QNNTPGrab*) data)->collectionMoved(QString(collection_name), old_position, new_position);
254
}
255

                
256
void QNNTPGrab::collectionDownloadedWrapper(NntpgrabCore *core, const char *collection_name, void *data)
257
{
258
    Q_UNUSED(core);
259

                
260
    emit ((QNNTPGrab*) data)->collectionDownloaded(QString(collection_name));
261
}
262

                
263
void QNNTPGrab::pluginLoadedWrapper(NntpgrabCore *core, const char *plugin_name, bool is_persistent, void *data)
264
{
265
    Q_UNUSED(core);
266

                
267
    emit ((QNNTPGrab*) data)->pluginLoaded(QString(plugin_name), is_persistent);
268
}
269

                
270
void QNNTPGrab::pluginUnloadedWrapper(NntpgrabCore *core, const char *plugin_name, void *data)
271
{
272
    Q_UNUSED(core);
273

                
274
    emit ((QNNTPGrab*) data)->pluginUnloaded(QString(plugin_name));
275
}
276

                
277
void QNNTPGrab::pluginEventWrapper(NntpgrabCore *core, const char *plugin_name, const char *event_name, const char **values, void *data)
278
{
279
    int i;
280
    QList list_values;
281

                
282
    Q_UNUSED(core);
283

                
284
    i = 0;
285
    while (values[i]) {
286
        list_values.append(QString(values[i]));
287
        i++;
288
    }
289

                
290
    emit ((QNNTPGrab*) data)->pluginEvent(QString(plugin_name), QString(event_name), list_values);
291
}
292

                
293
NGConfigOpts QNNTPGrab::configGetOpts()
294
{
295
    return nntpgrab_core_config_get_opts(this->core);
296
}
297

                
298
QNNTPGrab::SchedularState QNNTPGrab::getSchedularState()
299
{
300
    return (QNNTPGrab::SchedularState) nntpgrab_core_schedular_get_state(this->core);
301
}
302

                
303
bool QNNTPGrab::startSchedular()
304
{
305
    return nntpgrab_core_schedular_start(this->core);
306
}
307

                
308
bool QNNTPGrab::stopSchedular(bool wait)
309
{
310
    return nntpgrab_core_schedular_stop(this->core, wait);
311
}
312

                
313
bool QNNTPGrab::loadPlugin(QString plugin_name, QString *errmsg)
314
{
315
    bool ret;
316
    char *errmsg_tmp = NULL;
317

                
318
    ret = nntpgrab_core_plugins_load_plugin(this->core, plugin_name.toAscii(), &errmsg_tmp);
319
    if (!ret) {
320
        errmsg->clear();
321
        errmsg->append(errmsg_tmp);
322
    }
323

                
324
    return ret;
325
}
326

                
327
bool QNNTPGrab::startEmbeddedServer(int port, char **errmsg)
328
{
329
    return nntpgrab_core_embedded_server_start(this->core, port, errmsg);
330
}