Revision 1776

trunk/client/gui_qt/WidgetConfigDisplay.cpp (revision 1776)
16 16
    if (opts.getShowColumnProgress()) {
17 17
        ui->chkShowColumnProgress->setChecked(true);
18 18
    } else {
19
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_STATE_STR);
19
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_STATE_STR);
20 20
    }
21 21

                
22 22
    if (opts.getShowColumnSubject()) {
23 23
        ui->chkShowColumnSubject->setChecked(true);
24 24
    } else {
25
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_SUBJECT);
25
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_SUBJECT);
26 26
    }
27 27

                
28 28
    if (opts.getShowColumnPoster()) {
29 29
        ui->chkShowColumnPoster->setChecked(true);
30 30
    } else {
31
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_POSTER);
31
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_POSTER);
32 32
    }
33 33

                
34 34
    if (opts.getShowColumnNewsgroups()) {
35 35
        ui->chkShowColumnNewsgroups->setChecked(true);
36 36
    } else {
37
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_GROUPS);
37
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_GROUPS);
38 38
    }
39 39

                
40 40
    if (opts.getShowColumnFilesize()) {
41 41
        ui->chkShowColumnFileSize->setChecked(true);
42 42
    } else {
43
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_FILE_SIZE_STR);
43
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_FILE_SIZE_STR);
44 44
    }
45 45

                
46 46
    if (opts.getShowColumnFilesizeRemaining()) {
47 47
        ui->chkShowColumnFileSizeRemaining->setChecked(true);
48 48
    } else {
49
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_FILE_SIZE_REMAINING_STR);
49
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_FILE_SIZE_REMAINING_STR);
50 50
    }
51 51

                
52 52
    if (opts.getShowColumnStamp()) {
53 53
        ui->chkShowColumnStamp->setChecked(true);
54 54
    } else {
55
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_STAMP);
55
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_STAMP);
56 56
    }
57 57

                
58 58
    if (opts.getShowColumnTimeRemaining()) {
59 59
        ui->chkShowColumnTimeRemaining->setChecked(true);
60 60
    } else {
61
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_ESTIMATED_TIME_REMAINING_STR);
61
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_ESTIMATED_TIME_REMAINING_STR);
62 62
    }
63 63

                
64 64
    if (opts.getShowColumnTimeToFinish()) {
65 65
        ui->chkShowColumnTimeToFinish->setChecked(true);
66 66
    } else {
67
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_ESTIMATED_TIME_TO_FINISH_STR);
67
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_ESTIMATED_TIME_TO_FINISH_STR);
68 68
    }
69 69

                
70 70
    if (opts.getShowTrayIcon()) {
... ...
113 113
{
114 114
    opts.setShowColumnProgress(toggled);
115 115
    if (toggled) {
116
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_STATE_STR);
116
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_STATE_STR);
117 117
    } else {
118
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_STATE_STR);
118
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_STATE_STR);
119 119
    }
120 120
}
121 121

                
... ...
123 123
{
124 124
    opts.setShowColumnSubject(toggled);
125 125
    if (toggled) {
126
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_SUBJECT);
126
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_SUBJECT);
127 127
    } else {
128
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_SUBJECT);
128
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_SUBJECT);
129 129
    }
130 130
}
131 131

                
... ...
133 133
{
134 134
    opts.setShowColumnPoster(toggled);
135 135
    if (toggled) {
136
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_POSTER);
136
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_POSTER);
137 137
    } else {
138
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_POSTER);
138
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_POSTER);
139 139
    }
140 140
}
141 141

                
... ...
143 143
{
144 144
    opts.setShowColumnNewsgroups(toggled);
145 145
    if (toggled) {
146
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_GROUPS);
146
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_GROUPS);
147 147
    } else {
148
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_GROUPS);
148
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_GROUPS);
149 149
    }
150 150
}
151 151

                
... ...
153 153
{
154 154
    opts.setShowColumnFilesize(toggled);
155 155
    if (toggled) {
156
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_FILE_SIZE_STR);
156
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_FILE_SIZE_STR);
157 157
    } else {
158
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_FILE_SIZE_STR);
158
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_FILE_SIZE_STR);
159 159
    }
160 160
}
161 161

                
... ...
163 163
{
164 164
    opts.setShowColumnFilesizeRemaining(toggled);
165 165
    if (toggled) {
166
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_FILE_SIZE_REMAINING_STR);
166
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_FILE_SIZE_REMAINING_STR);
167 167
    } else {
168
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_FILE_SIZE_REMAINING_STR);
168
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_FILE_SIZE_REMAINING_STR);
169 169
    }
170 170
}
171 171

                
... ...
173 173
{
174 174
    opts.setShowColumnStamp(toggled);
175 175
    if (toggled) {
176
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_STAMP);
176
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_STAMP);
177 177
    } else {
178
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_STAMP);
178
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_STAMP);
179 179
    }
180 180
}
181 181

                
... ...
183 183
{
184 184
    opts.setShowColumnTimeRemaining(toggled);
185 185
    if (toggled) {
186
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_ESTIMATED_TIME_REMAINING_STR);
186
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_ESTIMATED_TIME_REMAINING_STR);
187 187
    } else {
188
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_ESTIMATED_TIME_REMAINING_STR);
188
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_ESTIMATED_TIME_REMAINING_STR);
189 189
    }
190 190
}
191 191

                
... ...
193 193
{
194 194
    opts.setShowColumnTimeToFinish(toggled);
195 195
    if (toggled) {
196
        widgetDownloadQueue->showField(WidgetDownloadQueue::FIELD_ESTIMATED_TIME_TO_FINISH_STR);
196
        widgetDownloadQueue->showField(DownloadQueueItem::FIELD_ESTIMATED_TIME_TO_FINISH_STR);
197 197
    } else {
198
        widgetDownloadQueue->hideField(WidgetDownloadQueue::FIELD_ESTIMATED_TIME_TO_FINISH_STR);
198
        widgetDownloadQueue->hideField(DownloadQueueItem::FIELD_ESTIMATED_TIME_TO_FINISH_STR);
199 199
    }
200 200
}
201 201

                
trunk/client/gui_qt/WidgetDownloadQueue.cpp (revision 1776)
17 17

                
18 18
    ui->setupUi(this);
19 19

                
20
    ui->treeWidget->header()->setResizeMode(QHeaderView::ResizeToContents);
20
    ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
21 21

                
22
    this->model = new DownloadQueueModel(glue);
23
    ui->treeView->setModel(this->model);
24

                
22 25
    progressbarDelegate = new ProgressBarDelegateDownloadQueue(this);
23
    ui->treeWidget->setItemDelegateForColumn(FIELD_STATE_STR, progressbarDelegate);
26
    ui->treeView->setItemDelegateForColumn(DownloadQueueItem::FIELD_STATE_STR, progressbarDelegate);
24 27

                
25 28
    /* Connect the signal handlers for the download queue widgets */
26 29
    connect(ui->actionMoveSelectedItemsToTheTop, SIGNAL(triggered(void)), SLOT(btnQueueMoveToTop_clicked(void)));
... ...
28 31
    connect(ui->actionMoveSelectedItemsDown, SIGNAL(triggered(void)), SLOT(btnQueueMoveDown_clicked(void)));
29 32
    connect(ui->actionMoveSelectedItemsToTheBottom, SIGNAL(triggered(void)), SLOT(btnQueueMoveToBottom_clicked(void)));
30 33

                
31
    connect(ui->treeWidget, SIGNAL(itemSelectionChanged(void)), SLOT(onItemSelectionChanged(void)));
34
    connect(ui->treeView, SIGNAL(itemSelectionChanged(void)), SLOT(onItemSelectionChanged(void)));
32 35

                
33
    connect(glue, SIGNAL(fileDownloadStateUpdate(QString,QString,int,int,int,quint64,quint64,quint64,quint64)), SLOT(onFileDownloadStateUpdate(QString,QString,int,int,int,quint64,quint64,quint64,quint64)), Qt::QueuedConnection);
34
    connect(glue, SIGNAL(fileStateChanged(QString,QString,QString,QNNTPGrabGlue::TaskState,QNNTPGrabGlue::TaskState)), SLOT(onFileStateChanged(QString,QString,QString,QNNTPGrabGlue::TaskState,QNNTPGrabGlue::TaskState)), Qt::QueuedConnection);
35

                
36
    connect(glue, SIGNAL(fileAdded(QString,QString,QString,QDateTime,quint64,quint64,quint64,QNNTPGrabGlue::TaskState,int,QList)), SLOT(onFileAdded(QString,QString,QString,QDateTime,quint64,quint64,quint64,QNNTPGrabGlue::TaskState,int,QList)), Qt::QueuedConnection);
37
    connect(glue, SIGNAL(fileRemoved(QString,QString,quint64,quint64)), SLOT(onFileRemoved(QString,QString,quint64,quint64)), Qt::QueuedConnection);
38
    connect(glue, SIGNAL(fileMoved(QString,QString,QString,int,int)), SLOT(onFileMoved(QString,QString,QString,int,int)), Qt::QueuedConnection);
39

                
40
    connect(glue, SIGNAL(collectionAdded(QString,QString)), SLOT(onCollectionAdded(QString,QString)), Qt::QueuedConnection);
41
    connect(glue, SIGNAL(collectionRemoved(QString)), SLOT(onCollectionRemoved(QString)), Qt::QueuedConnection);
42
    connect(glue, SIGNAL(collectionModified(QString,QString)), SLOT(onCollectionModified(QString,QString)), Qt::QueuedConnection);
43
    connect(glue, SIGNAL(collectionMoved(QString,int,int)), SLOT(onCollectionMoved(QString,int,int)), Qt::QueuedConnection);
44

                
45
    connect(glue, SIGNAL(trafficMonitorUpdate(int,int,int,int,int,int,int,int,int,int,QDateTime,double)), SLOT(onTrafficMonitorUpdate(int,int,int,int,int,int,int,int,int,int,QDateTime,double)), Qt::QueuedConnection);
46

                
47 36
    connect(ui->actionMarkItemOptional, SIGNAL(triggered()), SLOT(actionMarkItemOptional_activated()));
48 37
    connect(ui->actionRestartSelectedItems, SIGNAL(triggered()), SLOT(actionRestartSelectedItems_activated()));
49 38
    connect(actionRemoveSelectedFiles, SIGNAL(triggered()), SLOT(actionRemoveSelectedFiles_activated()));
50 39
    connect(ui->actionOpenDownloadFolder, SIGNAL(triggered()), SLOT(actionOpenDownloadFolder_activated()));
51 40
    connect(ui->actionExecuteSelectedItem, SIGNAL(triggered()), SLOT(actionExecuteSelectedItem_activated()));
52 41

                
53
    ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
54
    connect(ui->treeWidget, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
42
    ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
43
    connect(ui->treeView, SIGNAL(customContextMenuRequested(const QPoint &)), SLOT(showContextMenu(const QPoint &)));
55 44

                
56 45
    /* Add custom fields to the list */
57
    ui->treeWidget->setColumnCount(NUM_FIELDS);
58
    ui->treeWidget->setColumnHidden(FIELD_PROGRESS, true);
59
    ui->treeWidget->setColumnHidden(FIELD_FILE_SIZE, true);
60
    ui->treeWidget->setColumnHidden(FIELD_FILE_SIZE_REMAINING, true);
61
    ui->treeWidget->setColumnHidden(FIELD_REAL_FILENAME, true);
46
    //ui->treeView->setColumnCount(DownloadQueueItem::NUM_FIELDS);
47
    ui->treeView->setColumnHidden(DownloadQueueItem::FIELD_PROGRESS, true);
48
    ui->treeView->setColumnHidden(DownloadQueueItem::FIELD_FILE_SIZE, true);
49
    ui->treeView->setColumnHidden(DownloadQueueItem::FIELD_FILE_SIZE_REMAINING, true);
50
    ui->treeView->setColumnHidden(DownloadQueueItem::FIELD_REAL_FILENAME, true);
62 51

                
63
    /* Retrieve the download queue */
64
    QList collections = glue->schedularGetAllTasks();
65 52

                
66
    int i;
67
    for (i = 0; i < collections.count(); i++) {
68
        QNNTPGrabGlue::Collection collection = collections[i];
69

                
70
        QTreeWidgetItem *collection_item = new QTreeWidgetItem();
71

                
72
        int j;
73
        for (j = 0; j < collection.files.count(); j++) {
74
            QNNTPGrabGlue::File file = collection.files[j];
75

                
76
            QTreeWidgetItem *file_item = new QTreeWidgetItem(collection_item);
77
            file_item->setText(FIELD_SUBJECT, file.subject);
78
            file_item->setText(FIELD_POSTER, file.poster);
79
            file_item->setData(FIELD_FILE_SIZE, Qt::UserRole, QVariant(file.file_size));
80
            file_item->setText(FIELD_FILE_SIZE_STR, glue->utilsCalculateFileSize(file.file_size));
81
            file_item->setData(FIELD_FILE_SIZE_REMAINING, Qt::UserRole, QVariant(file.file_size_remaining));
82
            file_item->setText(FIELD_FILE_SIZE_REMAINING_STR, glue->utilsCalculateFileSize(file.file_size_remaining));
83
            file_item->setText(FIELD_STAMP, file.stamp.toString());
84
            file_item->setText(FIELD_REAL_FILENAME, file.filename);
85

                
86
            int k;
87
            QString groups;
88
            for (k = 0; k < file.groups.count(); k++) {
89
                if (groups.isEmpty()) {
90
                    groups = file.groups[k];
91
                } else {
92
                    groups += QString(",") + file.groups[k];
93
                }
94
            }
95
            file_item->setText(FIELD_GROUPS, groups);
96

                
97
            int progress;
98
            QString state_str;
99

                
100
            if (file.status == QNNTPGrabGlue::TASK_STATE_SKIPPED) {
101
                progress = 0;
102
            } else {
103
                progress = calculateProgress(file.file_size, file.file_size_remaining);
104
            }
105

                
106
            switch(file.status) {
107
                case QNNTPGrabGlue::TASK_STATE_WAITING_FOR_DOWNLOAD:
108
                case QNNTPGrabGlue::TASK_STATE_DOWNLOADING:
109
                    state_str = "";
110
                    break;
111

                
112
                case QNNTPGrabGlue::TASK_STATE_WAITING_FOR_DECODE:
113
                    state_str = tr("Waiting");
114
                    break;
115

                
116
                case QNNTPGrabGlue::TASK_STATE_DECODING:
117
                    state_str = tr("Decoding");
118
                    break;
119

                
120
                case QNNTPGrabGlue::TASK_STATE_FINISHED_COMPLETE:
121
                    state_str = tr("Done");
122
                    break;
123

                
124
                case QNNTPGrabGlue::TASK_STATE_FINISHED_INCOMPLETE:
125
                    state_str = tr("Incomplete");
126
                    break;
127

                
128
                case QNNTPGrabGlue::TASK_STATE_FINISHED_NO_PARTS_AVAIL:
129
                    state_str = tr("Not available");
130
                    break;
131

                
132
                case QNNTPGrabGlue::TASK_STATE_SKIPPED:
133
                    state_str = tr("Skipped");
134
                    break;
135

                
136
                default:
137
                    state_str = "";
138
                    break;
139
            };
140

                
141
            file_item->setText(FIELD_PROGRESS, QString("%1").arg(progress));
142
            file_item->setText(FIELD_STATE_STR, state_str);
143
        }
144

                
145
        collection_item->setText(FIELD_PROGRESS, QString("%1").arg(calculateProgress(collection.total_size, collection.total_size_remaining)));
146
        collection_item->setText(FIELD_SUBJECT, collection.collection_name);
147
        collection_item->setText(FIELD_POSTER, collection.poster);
148
        collection_item->setData(FIELD_FILE_SIZE, Qt::UserRole, collection.total_size);
149
        collection_item->setText(FIELD_FILE_SIZE_STR, glue->utilsCalculateFileSize(collection.total_size));
150
        collection_item->setData(FIELD_FILE_SIZE_REMAINING, Qt::UserRole, collection.total_size_remaining);
151
        collection_item->setText(FIELD_FILE_SIZE_REMAINING_STR, glue->utilsCalculateFileSize(collection.total_size_remaining));
152

                
153
        ui->treeWidget->addTopLevelItem(collection_item);
154
    }
155

                
156
    ui->treeWidget->resizeColumnToContents(FIELD_PROGRESS);
53
    //ui->treeWidget->resizeColumnToContents(FIELD_PROGRESS);
157 54
}
158 55

                
159 56
WidgetDownloadQueue::~WidgetDownloadQueue()
... ...
216 113
    QList list_collections;
217 114
    MoveItemData move_data;
218 115

                
219
    for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++) {
220
        collection_item = ui->treeWidget->topLevelItem(i);
116
#if 0
117
    for (int i = 0; i < ui->treeView->topLevelItemCount(); i++) {
118
        collection_item = ui->treeView->topLevelItem(i);
221 119

                
222 120
        if (collection_item->isSelected()) {
223 121
            if (i == ui->treeWidget->topLevelItemCount() - 1 && direction == MOVE_DIRECTION_DOWN) {
... ...
267 165
            }
268 166
        }
269 167
    }
168
#endif
270 169

                
271 170
    if (list_collections.count() > 0) {
272 171
        Q_FOREACH(move_data, list_collections) {
273
            doMoveItem(move_data.item->data(FIELD_SUBJECT, 0).toString(), NULL, calculateNewPosition(move_data.position, direction));
172
            doMoveItem(move_data.item->data(DownloadQueueItem::FIELD_SUBJECT, 0).toString(), NULL, calculateNewPosition(move_data.position, direction));
274 173
        }
275 174
    }
276 175
}
... ...
295 194
    processSelectionAndMoveItems(MOVE_DIRECTION_BOTTOM);
296 195
}
297 196

                
298
int WidgetDownloadQueue::calculateProgress(quint64 size, quint64 size_remaining)
299
{
300
    if (size == 0) {        /* Prevent a divide by zero */
301
        return 0;
302
    }
303

                
304
    int ret = (((double)size - size_remaining) / size) * 100;
305

                
306
    if (ret < 0 || ret > 100) {
307
        qWarning("Invalid progress detected, size = %lli, size_remaining = %lli", size, size_remaining);
308
    }
309

                
310
    return qMin(100, ret);
311
}
312

                
313
void WidgetDownloadQueue::onFileDownloadStateUpdate(QString collection_name, QString subject, int num_parts_total, int num_parts_done,
314
                                                    int num_parts_failed, quint64 file_size, quint64 file_size_remaining,
315
                                                    quint64 total_size, quint64 total_size_remaining)
316
{
317
    Q_UNUSED(num_parts_total);
318
    Q_UNUSED(num_parts_done);
319
    Q_UNUSED(num_parts_failed);
320
    QTreeWidgetItem *collection_node;
321
    QTreeWidgetItem *subject_node;
322

                
323
    collection_node = getTreeWidgetByCollection(collection_name);
324
    subject_node = getTreeWidgetBySubject(collection_node, subject);
325
    if (subject_node == NULL) {
326
        qWarning("onFileDownloadStateUpdate: Subject '%s' from collection '%s' could not found", collection_name.toUtf8().data(), subject.toUtf8().data());
327
        return;
328
    }
329

                
330
    collection_node->setText(FIELD_PROGRESS, QString("%1").arg(calculateProgress(total_size, total_size_remaining)));
331
    collection_node->setText(FIELD_STATE_STR, collection_node->text(FIELD_STATE_STR));    /* Force redraw */
332
    collection_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(total_size_remaining));
333
    collection_node->setText(FIELD_FILE_SIZE_REMAINING_STR, glue->utilsCalculateFileSize(total_size_remaining));
334

                
335
    subject_node->setText(FIELD_PROGRESS, QString("%1").arg(calculateProgress(file_size, file_size_remaining)));
336
    subject_node->setText(FIELD_STATE_STR, subject_node->text(FIELD_STATE_STR));    /* Force redraw */
337
    subject_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(file_size_remaining));
338
    subject_node->setText(FIELD_FILE_SIZE_REMAINING_STR, glue->utilsCalculateFileSize(file_size_remaining));
339
}
340

                
341
void WidgetDownloadQueue::onFileStateChanged(QString collection_name, QString subject, QString real_filename, QNNTPGrabGlue::TaskState old_state, QNNTPGrabGlue::TaskState new_state)
342
{
343
    Q_UNUSED(old_state);
344

                
345
    QTreeWidgetItem *collection_node;
346
    QTreeWidgetItem *subject_node;
347

                
348
    collection_node = getTreeWidgetByCollection(collection_name);
349
    subject_node = getTreeWidgetBySubject(collection_node, subject);
350

                
351
    if (!subject_node) {
352
        qWarning("onFileStateChanged: Subject '%s' from collection '%s' could not be found", subject.toUtf8().data(), collection_name.toUtf8().data());
353
        return;
354
    }
355

                
356
    quint64 file_size;
357
    quint64 file_size_remaining;
358
    QString size_remaining;
359
    quint64 total_size;
360
    quint64 total_size_remaining;
361
    int progress;
362

                
363
    switch(new_state) {
364
        case QNNTPGrabGlue::TASK_STATE_WAITING_FOR_DOWNLOAD:
365
            /* The task was restarted */
366

                
367
            /* Update the row itself */
368
            file_size = subject_node->data(FIELD_FILE_SIZE, Qt::DisplayRole).toLongLong();
369
            file_size_remaining = subject_node->data(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole).toLongLong();
370

                
371
            size_remaining = glue->utilsCalculateFileSize(file_size);
372

                
373
            subject_node->setText(FIELD_PROGRESS, "0");
374
            subject_node->setText(FIELD_STATE_STR, "");
375
            subject_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(file_size));
376
            subject_node->setText(FIELD_FILE_SIZE_REMAINING_STR, size_remaining);
377

                
378
            /* Update the parent */
379
            total_size = collection_node->data(FIELD_FILE_SIZE, Qt::DisplayRole).toLongLong();
380
            total_size_remaining = collection_node->data(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole).toLongLong();
381

                
382
            total_size_remaining += file_size - file_size_remaining;
383

                
384
            progress = calculateProgress(total_size, total_size_remaining);
385
            size_remaining = glue->utilsCalculateFileSize(total_size_remaining);
386

                
387
            collection_node->setText(FIELD_PROGRESS, QString("%1").arg(progress));
388
            collection_node->setText(FIELD_STATE_STR, collection_node->text(FIELD_STATE_STR));    /* Force redraw */
389
            collection_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(total_size_remaining));
390
            collection_node->setText(FIELD_FILE_SIZE_REMAINING_STR, size_remaining);
391

                
392
            break;
393
        case QNNTPGrabGlue::TASK_STATE_DOWNLOADING:
394
            subject_node->setText(FIELD_PROGRESS, "0");
395
            subject_node->setText(FIELD_STATE_STR, "");     /* The state message should be empty. In that case the value of the progress bar will be shown */
396
            break;
397
        case QNNTPGrabGlue::TASK_STATE_WAITING_FOR_DECODE:
398
            size_remaining = glue->utilsCalculateFileSize(0);
399
            subject_node->setText(FIELD_PROGRESS, "100");
400
            subject_node->setText(FIELD_FILE_SIZE_REMAINING_STR, size_remaining);
401
            subject_node->setText(FIELD_STATE_STR, tr("Waiting"));
402
            break;
403
        case QNNTPGrabGlue::TASK_STATE_DECODING:
404
            subject_node->setText(FIELD_PROGRESS, "100");
405
            subject_node->setText(FIELD_STATE_STR, tr("Decoding"));
406
            break;
407
        case QNNTPGrabGlue::TASK_STATE_FINISHED_COMPLETE:
408
            Q_ASSERT(real_filename != NULL);
409

                
410
            subject_node->setText(FIELD_STATE_STR, tr("Done"));
411
            subject_node->setText(FIELD_REAL_FILENAME, real_filename);
412
            break;
413
        case QNNTPGrabGlue::TASK_STATE_FINISHED_INCOMPLETE:
414
            Q_ASSERT(real_filename != NULL);
415

                
416
            subject_node->setText(FIELD_STATE_STR, tr("Incomplete"));
417
            subject_node->setText(FIELD_REAL_FILENAME, real_filename);
418
            break;
419
        case QNNTPGrabGlue::TASK_STATE_FINISHED_NO_PARTS_AVAIL:
420
            subject_node->setText(FIELD_STATE_STR, tr("Not available"));
421
            break;
422
        case QNNTPGrabGlue::TASK_STATE_SKIPPED:
423
            subject_node->setText(FIELD_STATE_STR, tr("Skipped"));
424
            subject_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayPropertyRole, QVariant(0));
425
            subject_node->setText(FIELD_FILE_SIZE_REMAINING_STR, "");
426
            break;
427
    }
428

                
429
    ui->treeWidget->resizeColumnToContents(FIELD_PROGRESS);
430
}
431

                
432
void WidgetDownloadQueue::onFileAdded(QString collection_name, QString subject, QString poster, QDateTime stamp, quint64 file_size, quint64 total_size, quint64 total_size_remaining, QNNTPGrabGlue::TaskState state, int num_parts, QList groups)
433
{
434
    Q_UNUSED(num_parts);
435

                
436
    QTreeWidgetItem *collection_node =  NULL;
437

                
438
    collection_node = getTreeWidgetByCollection(collection_name);
439
    if (collection_node == NULL) {
440
        qWarning("file added: Collection %s could not be found", collection_name.toUtf8().data());
441
        return;
442
    }
443

                
444
    QTreeWidgetItem *file_node =  new QTreeWidgetItem();
445
    file_node->setText(FIELD_SUBJECT, subject);
446
    file_node->setText(FIELD_POSTER, poster);
447
    file_node->setData(FIELD_FILE_SIZE, Qt::DisplayRole, QVariant(file_size));
448
    file_node->setText(FIELD_FILE_SIZE_STR, glue->utilsCalculateFileSize(file_size));
449
    file_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayPropertyRole, QVariant(file_size));
450
    file_node->setText(FIELD_FILE_SIZE_REMAINING_STR, glue->utilsCalculateFileSize(file_size));
451
    file_node->setText(FIELD_STAMP, stamp.toString());
452

                
453
    int i;
454
    QString groups_str;
455
    for (i = 0; i < groups.count(); i++) {
456
        if (groups_str.isEmpty()) {
457
            groups_str = groups[i];
458
        } else {
459
            groups_str += QString(",") + groups[i];
460
        }
461
    }
462
    file_node->setText(FIELD_GROUPS, groups_str);
463

                
464
    if (state == QNNTPGrabGlue::TASK_STATE_SKIPPED) {
465
        file_node->setText(FIELD_STATE_STR, tr("Skipped"));
466
        file_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(0));
467
        file_node->setText(FIELD_FILE_SIZE_REMAINING_STR, "");
468
    }
469

                
470
    /* Update the parent */
471
    collection_node->addChild(file_node);
472

                
473
    QString size = glue->utilsCalculateFileSize(total_size);
474
    QString size_remaining = glue->utilsCalculateFileSize(total_size_remaining);
475

                
476
    int progress = calculateProgress(total_size, total_size_remaining);
477

                
478
    collection_node->setData(FIELD_FILE_SIZE, Qt::DisplayRole, QVariant(total_size));
479
    collection_node->setText(FIELD_FILE_SIZE_STR, size);
480
    collection_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(total_size_remaining));
481
    collection_node->setText(FIELD_FILE_SIZE_REMAINING_STR, size_remaining);
482
    collection_node->setText(FIELD_PROGRESS, QString("%1").arg(progress));
483
    collection_node->setText(FIELD_STATE_STR, collection_node->text(FIELD_STATE_STR));    /* Force redraw */
484
}
485

                
486
void WidgetDownloadQueue::onFileRemoved(QString collection_name, QString subject, quint64 total_size, quint64 total_size_remaining)
487
{
488
    QTreeWidgetItem *collection_node;
489
    QTreeWidgetItem *subject_node;
490

                
491
    collection_node = getTreeWidgetByCollection(collection_name);
492
    subject_node = getTreeWidgetBySubject(collection_node, subject);
493
    if (subject_node == NULL) {
494
        qWarning("onFileRemoved: Subject '%s' in collection '%s' could not be found", subject.toUtf8().data(), collection_name.toUtf8().data());
495
        return;
496
    }
497

                
498
    QString size = glue->utilsCalculateFileSize(total_size);
499
    QString size_remaining = glue->utilsCalculateFileSize(total_size_remaining);
500

                
501
    collection_node->setData(FIELD_FILE_SIZE, Qt::DisplayRole, QVariant(total_size));
502
    collection_node->setText(FIELD_FILE_SIZE_STR, size);
503
    collection_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(total_size_remaining));
504
    collection_node->setText(FIELD_FILE_SIZE_REMAINING_STR, size_remaining);
505
    collection_node->setText(FIELD_PROGRESS, QString("%1").arg(calculateProgress(total_size, total_size_remaining)));
506
    collection_node->setText(FIELD_STATE_STR, collection_node->text(FIELD_STATE_STR));    /* Force redraw */
507

                
508
    delete subject_node;
509
}
510

                
511
void WidgetDownloadQueue::onFileMoved(QString orig_collection_name, QString subject, QString new_collection_name, int old_position, int new_position)
512
{
513
    Q_UNUSED(subject);
514
    Q_UNUSED(new_collection_name);
515

                
516
    bool was_selected;
517
    QTreeWidgetItem *subject_node;
518
    QTreeWidgetItem *collection_node = getTreeWidgetByCollection(orig_collection_name);
519

                
520
    qDebug("file moved: old_position: %i, new position: %i", old_position, new_position);
521

                
522
    if (collection_node == NULL) {
523
        qWarning("onFileMoved: Collection '%s' not found", orig_collection_name.toUtf8().data());
524
        return;
525
    }
526

                
527
    was_selected = collection_node->child(old_position)->isSelected();
528
    subject_node = collection_node->takeChild(old_position);
529

                
530
    if (new_position == -1) {
531
        collection_node->addChild(subject_node);
532
    } else {
533
        collection_node->insertChild(new_position, subject_node);
534
    }
535

                
536
    if (was_selected) {
537
        subject_node->setSelected(true);
538
    }
539
}
540

                
541
void WidgetDownloadQueue::onCollectionAdded(QString collection_name, QString poster)
542
{
543
    qDebug("Collection added");
544
    QTreeWidgetItem *collection_node =  new QTreeWidgetItem();
545
    collection_node->setText(FIELD_SUBJECT, collection_name);
546
    collection_node->setText(FIELD_POSTER, poster);
547
    ui->treeWidget->addTopLevelItem(collection_node);
548
}
549

                
550
void WidgetDownloadQueue::onCollectionRemoved(QString collection_name)
551
{
552
    qDebug("collection removed: %s", collection_name.toUtf8().data());
553
    QTreeWidgetItem *collection_node =  NULL;
554
    collection_node = getTreeWidgetByCollection(collection_name);
555

                
556
    if (collection_node == NULL) {
557
        qWarning("collectionmodified: Collection %s not found", collection_name.toUtf8().data());
558
        return;
559
    }
560

                
561
    delete collection_node;
562
}
563

                
564
void WidgetDownloadQueue::onCollectionModified(QString collection_name, QString poster)
565
{
566
    qDebug("Collection modified");
567
    QTreeWidgetItem *collection_node =  NULL;
568
    collection_node = getTreeWidgetByCollection(collection_name);
569

                
570
    if (collection_node == NULL) {
571
        qWarning("collectionmodified: Collection %s not found", collection_name.toUtf8().data());
572
        return;
573
    }
574

                
575
    collection_node->setText(FIELD_POSTER, poster);
576
}
577

                
578
void WidgetDownloadQueue::onCollectionMoved(QString collection_name, int old_position, int new_position)
579
{
580
    qDebug("collection moved: old_position: %i, new position: %i", old_position, new_position);
581
    bool was_selected;
582
    bool was_expanded;
583
    QTreeWidgetItem *collection_node;
584

                
585
    was_selected = ui->treeWidget->topLevelItem(old_position)->isSelected();
586
    was_expanded = ui->treeWidget->topLevelItem(old_position)->isExpanded();
587
    collection_node = ui->treeWidget->takeTopLevelItem(old_position);
588

                
589
    if (collection_node->text(FIELD_SUBJECT).compare(collection_name) != 0) {
590
        qWarning("onCollectionMoved: %s != %s", collection_name.toUtf8().data(), collection_node->text(FIELD_SUBJECT).toUtf8().data());
591
        return;
592
    }
593

                
594
    if (new_position == -1) { // To bottom
595
        ui->treeWidget->addTopLevelItem(collection_node);
596
    } else {
597
        ui->treeWidget->insertTopLevelItem(new_position, collection_node);
598
    }
599

                
600
    if (was_selected) {
601
        collection_node->setSelected(true);
602
    }
603

                
604
    if (was_expanded) {
605
        collection_node->setExpanded(true);
606
    }
607
}
608

                
609
void WidgetDownloadQueue::onTrafficMonitorUpdate(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, QDateTime stamp, double average)
610
{
611
    Q_UNUSED(stamp);
612
    Q_UNUSED(average);
613

                
614
    static int times_been_here = 10;
615
    bool no_data_received = FALSE;
616
    quint64 total_estimated_time_remaining;
617

                
618
    if (ui->treeWidget->children().count() == 0) {
619
        return;
620
    }
621

                
622
    if (bytes_received1 == 0 && bytes_received2 == 0 && bytes_received3 == 0 && bytes_received4 == 0 && bytes_received5 == 0 &&
623
        bytes_received6 == 0 && bytes_received7 == 0 && bytes_received8 == 0 && bytes_received9 == 0 && bytes_received10 == 0) {
624
            no_data_received = TRUE;
625
    }
626

                
627
    /* Only update on startup, on every 3 seconds, and when there has no data been received in the last 10 seconds */
628
    if (times_been_here < 3 && !no_data_received) {
629
        times_been_here += 1;
630
        return;
631
    }
632

                
633
    if (no_data_received) {
634
        times_been_here = 3;    /* update the time remaining the next time we get here */
635
    } else {
636
        times_been_here = 0;    /* perform the next update in 10 seconds */
637
    }
638

                
639
    total_estimated_time_remaining = 0;
640

                
641
    for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++) {
642
        QTreeWidgetItem *collection_item;
643
        int estimated_time_remaining;
644
        quint64 file_size;
645

                
646
        collection_item = ui->treeWidget->topLevelItem(i);
647
        file_size = collection_item->data(FIELD_FILE_SIZE_REMAINING, Qt::UserRole).toULongLong();
648
        estimated_time_remaining = glue->utilsCalculateEstimatedTimeRemaining(bytes_received1, bytes_received2, bytes_received3, bytes_received4, bytes_received5,
649
                                                                              bytes_received6, bytes_received7, bytes_received8, bytes_received9, bytes_received10, file_size);
650

                
651
        if (estimated_time_remaining == -1) {
652
            /* Not enough data received to make an estimation */
653
            return;
654
        }
655

                
656
        if (estimated_time_remaining == 0) {
657
            /* Collection is already downloaded */
658
            collection_item->setText(FIELD_ESTIMATED_TIME_REMAINING_STR, "");
659
            collection_item->setText(FIELD_ESTIMATED_TIME_TO_FINISH_STR, "");
660
        } else {
661
            collection_item->setText(FIELD_ESTIMATED_TIME_REMAINING_STR, glue->utilsGetReadableTimeRemaining(total_estimated_time_remaining + estimated_time_remaining));
662
            collection_item->setText(FIELD_ESTIMATED_TIME_TO_FINISH_STR, glue->utilsGetReadableFinishedTime(total_estimated_time_remaining + estimated_time_remaining));
663
        }
664

                
665
        total_estimated_time_remaining += estimated_time_remaining;
666

                
667
/* Disabled for now as it causes too much load */
668 197
#if 0
669
        /* Walk through all the children */
670
        for (int j = 0; j < collection_item->childCount(); j++) {
671
            QTreeWidgetItem *subject_item = collection_item->child(j);
672

                
673
            file_size = subject_item->data(FIELD_FILE_SIZE_REMAINING, Qt::UserRole).toLongLong();
674
            if (file_size == 0) {
675
                /* File is already downloaded */
676
                subject_item->setText(FIELD_ESTIMATED_TIME_REMAINING_STR, "");
677
                subject_item->setText(FIELD_ESTIMATED_TIME_TO_FINISH_STR, "");
678
            } else {
679
                estimated_time_remaining = glue->utilsCalculateEstimatedTimeRemaining(bytes_received1, bytes_received2, bytes_received3, bytes_received4, bytes_received5,
680
                                                                                      bytes_received6, bytes_received7, bytes_received8, bytes_received9, bytes_received10, file_size);
681

                
682
                subject_item->setText(FIELD_ESTIMATED_TIME_REMAINING_STR, glue->utilsGetReadableTimeRemaining(total_estimated_time_remaining));
683
                subject_item->setText(FIELD_ESTIMATED_TIME_TO_FINISH_STR, glue->utilsGetReadableFinishedTime(total_estimated_time_remaining));
684
            }
685
        }
686
#endif
687
    }
688

                
689
    ui->treeWidget->resizeColumnToContents(FIELD_ESTIMATED_TIME_REMAINING_STR);
690
    ui->treeWidget->resizeColumnToContents(FIELD_ESTIMATED_TIME_TO_FINISH_STR);
691
}
692

                
693 198
QTreeWidgetItem *WidgetDownloadQueue::getTreeWidgetByCollection(QString collection_name)
694 199
{
695 200
    QTreeWidgetItem *collection_node =  NULL;
... ...
721 226
    }
722 227
    return subject_node;
723 228
}
229
#endif
724 230

                
725 231
void WidgetDownloadQueue::onItemSelectionChanged(void)
726 232
{
727
    if (ui->treeWidget->selectedItems().count() == 0) {
233
#if 0
234
    if (ui->treeView->selectedItems().count() == 0) {
728 235
        ui->btnQueueMoveToTop->setEnabled(false);
729 236
        ui->btnQueueMoveUp->setEnabled(false);
730 237
        ui->btnQueueMoveDown->setEnabled(false);
... ...
737 244
        ui->btnQueueMoveToBottom->setEnabled(true);
738 245
        actionRemoveSelectedFiles->setEnabled(true);
739 246
    }
247
#endif
740 248
}
741 249

                
742 250
void WidgetDownloadQueue::actionMarkItemOptional_activated(void)
743 251
{
252
#if 0
744 253
    bool flag_set = FALSE;
745 254
    bool is_optional = FALSE;
746 255

                
747
    for (int i = 0; i < ui->treeWidget->selectedItems().count(); i++) {
256
    for (int i = 0; i < ui->treeView->selectedItems().count(); i++) {
748 257
        QString collection_name = "";
749 258
        QString subject = "";
750 259
        QString errmsg = "";
751 260

                
752
        QTreeWidgetItem *item = ui->treeWidget->selectedItems().at(i);
261
        QTreeWidgetItem *item = ui->treeView->selectedItems().at(i);
753 262

                
754 263
        if (item->childCount() != 0) {
755 264
            /* This is a collection, ignore */
... ...
774 283
            qWarning("%s", tr("Unable to change the status of the selected file(s)").toUtf8().data());
775 284
        }
776 285
    }
286
#endif
777 287
}
778 288

                
779 289
void WidgetDownloadQueue::actionRemoveSelectedFiles_activated(void)
780 290
{
781
    for (int i = 0; i < ui->treeWidget->selectedItems().count(); i++) {
291
#if 0
292
    for (int i = 0; i < ui->treeView->selectedItems().count(); i++) {
782 293
        QString collection_name = "";
783 294
        QString subject = "";
784 295
        QString errmsg = "";
785 296

                
786
        QTreeWidgetItem *item = ui->treeWidget->selectedItems().at(i);
297
        QTreeWidgetItem *item = ui->treeView->selectedItems().at(i);
787 298

                
788 299
        if (item->childCount() == 0) {
789 300
            /* This is a subject */
... ...
798 309
            qWarning(tr("Error occured while removing file(s):\n%s").toUtf8().data(), errmsg.toUtf8().data());
799 310
        }
800 311
    }
312
#endif
801 313
}
802 314

                
803 315
void WidgetDownloadQueue::actionRestartSelectedItems_activated(void)
804 316
{
805
    for (int i = 0; i < ui->treeWidget->selectedItems().count(); i++) {
317
#if 0
318
    for (int i = 0; i < ui->treeView->selectedItems().count(); i++) {
806 319
        QString collection_name = "";
807 320
        QString subject = "";
808 321
        QString errmsg = "";
809 322

                
810
        QTreeWidgetItem *item = ui->treeWidget->selectedItems().at(i);
323
        QTreeWidgetItem *item = ui->treeView->selectedItems().at(i);
811 324

                
812 325
        if (item->childCount() == 0) {
813 326
            /* This is a subject */
... ...
822 335
            qWarning(tr("Error occured while restarting file(s):\n%s").toUtf8().data(), errmsg.toUtf8().data());
823 336
        }
824 337
    }
338
#endif
825 339
}
826 340

                
827 341
void WidgetDownloadQueue::actionOpenDownloadFolder_activated(void)
828 342
{
343
#if 0
829 344
    QString folder = "";
830 345
    QNNTPGrabGlue::ConfigOpts opts = glue->configGetOpts();
831 346

                
832
    if (ui->treeWidget->selectedItems().count() == 1) {
347
    if (ui->treeView->selectedItems().count() == 1) {
833 348
        QString collection_name;
834
        QTreeWidgetItem *item = ui->treeWidget->selectedItems().at(0);
349
        QTreeWidgetItem *item = ui->treeView->selectedItems().at(0);
835 350

                
836 351
        if (item->childCount() == 0) {
837 352
            /* This is a subject */
... ...
861 376
#else
862 377
    QDesktopServices::openUrl(QUrl(QString("file://") + location.absoluteFilePath()));
863 378
#endif
379
#endif
864 380
}
865 381

                
866 382
void WidgetDownloadQueue::actionExecuteSelectedItem_activated(void)
867 383
{
384
#if 0
868 385
    QNNTPGrabGlue::ConfigOpts opts = glue->configGetOpts();
869 386

                
870
    if (ui->treeWidget->selectedItems().count() != 1) {
387
    if (ui->treeView->selectedItems().count() != 1) {
871 388
        return;
872 389
    }
873 390

                
874
    QTreeWidgetItem *item = ui->treeWidget->selectedItems().at(0);
391
    QTreeWidgetItem *item = ui->treeView->selectedItems().at(0);
875 392

                
876 393
    if (item->childCount() != 0) {
877 394
        return;
... ...
897 414
#else
898 415
    QDesktopServices::openUrl(QUrl(QString("file://") + location.absoluteFilePath()));
899 416
#endif
417
#endif
900 418
}
901 419

                
902 420
void WidgetDownloadQueue::showContextMenu(const QPoint &position)
903 421
{
422
#if 0
904 423
    QList actions;
905 424
    QAction *a = new QAction(this);
906 425

                
907
    if (!ui->treeWidget->indexAt(position).isValid()) {
426
    if (!ui->treeView->indexAt(position).isValid()) {
908 427
        return;
909 428
    }
910 429

                
911
    if (ui->treeWidget->selectedItems().count() == 0) {
430
    if (ui->treeView->selectedItems().count() == 0) {
912 431
        return;
913 432
    }
914 433

                
... ...
935 454

                
936 455
    /* Do we have exactly one selected item and is this item already downloaded? */
937 456
    ui->actionExecuteSelectedItem->setEnabled(false);
938
    if (glue->getIsStandalone() && ui->treeWidget->selectedItems().count() == 1) {
939
        item = ui->treeWidget->selectedItems().at(0);
457
    if (glue->getIsStandalone() && ui->treeView->selectedItems().count() == 1) {
458
        item = ui->treeView->selectedItems().at(0);
940 459

                
941 460
        if (item->text(FIELD_STATE_STR).compare(tr("Done")) == 0 ||
942 461
            item->text(FIELD_STATE_STR).compare(tr("Incomplete")) == 0) {
... ...
946 465
    }
947 466

                
948 467
    /* Enable the forcefully download / skip option */
949
    item = ui->treeWidget->selectedItems().at(0);
468
    item = ui->treeView->selectedItems().at(0);
950 469
    if (item->text(FIELD_STATE_STR).compare(tr("Skipped")) == 0) {
951 470
        ui->actionMarkItemOptional->setText(tr("Forcefully download file(s)"));
952 471
        ui->actionMarkItemOptional->setEnabled(true);
... ...
960 479
        ui->actionMarkItemOptional->setEnabled(false);
961 480
    }
962 481

                
963
    QMenu::exec(actions, ui->treeWidget->mapToGlobal(position));
482
    QMenu::exec(actions, ui->treeView->mapToGlobal(position));
964 483

                
965 484
    delete a;
485
#endif
966 486
}
967 487

                
968
void WidgetDownloadQueue::hideField(field f)
488
void WidgetDownloadQueue::hideField(DownloadQueueItem::field f)
969 489
{
970
    ui->treeWidget->hideColumn(f);
490
    ui->treeView->hideColumn(f);
971 491
}
972 492

                
973
void WidgetDownloadQueue::showField(field f)
493
void WidgetDownloadQueue::showField(DownloadQueueItem::field f)
974 494
{
975
    ui->treeWidget->showColumn(f);
495
    ui->treeView->showColumn(f);
976 496
}
trunk/client/gui_qt/DownloadQueueModel.h (revision 1776)
1
#ifndef DOWNLOADQUEUEMODEL_H
2
#define DOWNLOADQUEUEMODEL_H
3

                
4
#include 
5
#include "QNNTPGrabGlue.h"
6
#include "DownloadQueueItem.h"
7

                
8
class DownloadQueueModel : public QAbstractItemModel
9
{
10
    Q_OBJECT
11
public:
12
    DownloadQueueModel(QNNTPGrabGlue *glue, QObject *parent = 0);
13
    ~DownloadQueueModel();
14

                
15
    QVariant data(const QModelIndex &index, int role) const;
16
    Qt::ItemFlags flags(const QModelIndex &index) const;
17
    QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
18
    QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
19
    QModelIndex parent(const QModelIndex &index) const;
20
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
21
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
22

                
23
private:
24
    void loadDownloadQueue(QNNTPGrabGlue *glue);
25
    int calculateProgress(quint64 size, quint64 sizeRemaining);
26

                
27
    DownloadQueueItem *rootItem;
28

                
29
private slots:
30
    void onFileDownloadStateUpdate(QString collection_name, QString subject, int num_parts_total, int num_parts_done, int num_parts_failed, quint64 file_size, quint64 file_size_remaining, quint64 total_size, quint64 total_size_remaining);
31
    void onFileStateChanged(QString collection_name, QString subject, QString real_filename, QNNTPGrabGlue::TaskState old_state, QNNTPGrabGlue::TaskState new_state);
32

                
33
    void onFileAdded(QString collection_name, QString subject, QString poster, QDateTime stamp, quint64 file_size, quint64 total_size, quint64 total_size_remaining, QNNTPGrabGlue::TaskState state, int num_parts, QList groups);
34
    void onFileRemoved(QString collection_name, QString subject, quint64 total_size, quint64 total_size_remaining);
35
    void onFileMoved(QString orig_collection_name, QString subject, QString new_collection_name, int old_position, int new_position);
36

                
37
    void onCollectionAdded(QString collection_name, QString poster);
38
    void onCollectionRemoved(QString collection_name);
39
    void onCollectionModified(QString collection_name, QString poster);
40
    void onCollectionMoved(QString collection_name, int old_position, int new_position);
41

                
42
    void onTrafficMonitorUpdate(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, QDateTime stamp, double average);
43

                
44
signals:
45

                
46
public slots:
47

                
48
};
49

                
50
#endif // DOWNLOADQUEUEMODEL_H
trunk/client/gui_qt/gui_qt.pro (revision 1776)
37 37
    DialogSetCollectionName.cpp \
38 38
    ConfigGuiOpts.cpp \
39 39
    QNZBCreator.cpp \
40
    AutoImport.cpp
40
    AutoImport.cpp \
41
    DownloadQueueModel.cpp \
42
    DownloadQueueItem.cpp
41 43
HEADERS += mainwindow.h \
42 44
    QNNTPGrabGlue.h \
43 45
    WidgetConfig.h \
... ...
67 69
    QNNTPGrabGlue_p.h \
68 70
    ConfigGuiOpts.h \
69 71
    QNZBCreator.h \
70
    AutoImport.h
72
    AutoImport.h \
73
    DownloadQueueModel.h \
74
    DownloadQueueItem.h
71 75
FORMS += mainwindow.ui \
72 76
    WidgetConfig.ui \
73 77
    WidgetDownloadQueue.ui \
trunk/client/gui_qt/WidgetDownloadQueue.ui (revision 1776)
15 15
  
16 16
  
17 17
   
18
    
18
    
19 19
     
20 20
      false
21 21
     
... ...
31 31
     
32 32
      true
33 33
     
34
     
35
      9
36
     
37
     
38
      false
39
     
40
     
41
      125
42
     
43
     
44
      
45
       Progress
46
      
47
     
48
     
49
      
50
       Subject
51
      
52
     
53
     
54
      
55
       Poster
56
      
57
     
58
     
59
      
60
       Newsgroup
61
      
62
     
63
     
64
      
65
       File size
66
      
67
     
68
     
69
      
70
       File size remaining
71
      
72
     
73
     
74
      
75
       Stamp
76
      
77
     
78
     
79
      
80
       Time remaining
81
      
82
     
83
     
84
      
85
       Time to finish
86
      
87
     
88 34
    
89 35
   
90 36
   
trunk/client/gui_qt/WidgetDownloadQueue.h (revision 1776)
5 5
#include 
6 6
#include 
7 7
#include "ProgressBarDelegateDownloadQueue.h"
8
#include "DownloadQueueModel.h"
8 9

                
9 10
namespace Ui {
10 11
    class WidgetDownloadQueue;
... ...
16 17
    WidgetDownloadQueue(QNNTPGrabGlue *glue, QAction *actionRemoveSelectedFiles, QWidget *parent = 0);
17 18
    ~WidgetDownloadQueue();
18 19

                
19
    enum field {
20
        FIELD_STATE_STR,
21
        FIELD_SUBJECT,
22
        FIELD_POSTER,
23
        FIELD_GROUPS,
24
        FIELD_FILE_SIZE_STR,
25
        FIELD_FILE_SIZE_REMAINING_STR,
26
        FIELD_STAMP,
27
        FIELD_ESTIMATED_TIME_REMAINING_STR,
28
        FIELD_ESTIMATED_TIME_TO_FINISH_STR,
29
        FIELD_PROGRESS,
30
        FIELD_FILE_SIZE,
31
        FIELD_FILE_SIZE_REMAINING,
32
        FIELD_REAL_FILENAME,
33
        NUM_FIELDS
34
    };
20
    void hideField(DownloadQueueItem::field f);
21
    void showField(DownloadQueueItem::field f);
35 22

                
36
    void hideField(field f);
37
    void showField(field f);
38

                
39 23
protected:
40 24
    void changeEvent(QEvent *e);
41 25

                
... ...
44 28
    QNNTPGrabGlue *glue;
45 29
    ProgressBarDelegateDownloadQueue *progressbarDelegate;
46 30
    QAction *actionRemoveSelectedFiles;
31
    DownloadQueueModel *model;
47 32

                
48 33
    enum moveDirection {
49 34
        MOVE_DIRECTION_TOP,
... ...
56 41
    int calculateNewPosition(int current_position, moveDirection direction);
57 42
    void processSelectionAndMoveItems(moveDirection direction);
58 43

                
59
    int calculateProgress(quint64 total_file_size, quint64 file_size_remaining);
44
#if 0
60 45
    QTreeWidgetItem *getTreeWidgetByCollection(QString collection_name);
61 46
    QTreeWidgetItem *getTreeWidgetBySubject(QTreeWidgetItem *collection_node, QString subject);
47
#endif
62 48

                
63 49
private slots:
64 50
    void btnQueueMoveToTop_clicked(void);
... ...
66 52
    void btnQueueMoveDown_clicked(void);
67 53
    void btnQueueMoveToBottom_clicked(void);
68 54

                
69
    void onFileDownloadStateUpdate(QString collection_name, QString subject, int num_parts_total, int num_parts_done, int num_parts_failed, quint64 file_size, quint64 file_size_remaining, quint64 total_size, quint64 total_size_remaining);
70
    void onFileStateChanged(QString collection_name, QString subject, QString real_filename, QNNTPGrabGlue::TaskState old_state, QNNTPGrabGlue::TaskState new_state);
71

                
72
    void onFileAdded(QString collection_name, QString subject, QString poster, QDateTime stamp, quint64 file_size, quint64 total_size, quint64 total_size_remaining, QNNTPGrabGlue::TaskState state, int num_parts, QList groups);
73
    void onFileRemoved(QString collection_name, QString subject, quint64 total_size, quint64 total_size_remaining);
74
    void onFileMoved(QString orig_collection_name, QString subject, QString new_collection_name, int old_position, int new_position);
75

                
76
    void onCollectionAdded(QString collection_name, QString poster);
77
    void onCollectionRemoved(QString collection_name);
78
    void onCollectionModified(QString collection_name, QString poster);
79
    void onCollectionMoved(QString collection_name, int old_position, int new_position);
80

                
81
    void onTrafficMonitorUpdate(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, QDateTime stamp, double average);
82

                
83 55
    void onItemSelectionChanged(void);
84 56

                
85 57
    void actionRemoveSelectedFiles_activated(void);
trunk/client/gui_qt/DownloadQueueModel.cpp (revision 1776)
1
#include "DownloadQueueModel.h"
2

                
3
DownloadQueueModel::DownloadQueueModel(QNNTPGrabGlue *glue, QObject *parent) :
4
    QAbstractItemModel(parent)
5
{
6
    rootItem = new DownloadQueueItem();
7

                
8
    loadDownloadQueue(glue);
9

                
10
    connect(glue, SIGNAL(fileDownloadStateUpdate(QString,QString,int,int,int,quint64,quint64,quint64,quint64)), SLOT(onFileDownloadStateUpdate(QString,QString,int,int,int,quint64,quint64,quint64,quint64)), Qt::QueuedConnection);
11
    connect(glue, SIGNAL(fileStateChanged(QString,QString,QString,QNNTPGrabGlue::TaskState,QNNTPGrabGlue::TaskState)), SLOT(onFileStateChanged(QString,QString,QString,QNNTPGrabGlue::TaskState,QNNTPGrabGlue::TaskState)), Qt::QueuedConnection);
12

                
13
    connect(glue, SIGNAL(fileAdded(QString,QString,QString,QDateTime,quint64,quint64,quint64,QNNTPGrabGlue::TaskState,int,QList)), SLOT(onFileAdded(QString,QString,QString,QDateTime,quint64,quint64,quint64,QNNTPGrabGlue::TaskState,int,QList)), Qt::QueuedConnection);
14
    connect(glue, SIGNAL(fileRemoved(QString,QString,quint64,quint64)), SLOT(onFileRemoved(QString,QString,quint64,quint64)), Qt::QueuedConnection);
15
    connect(glue, SIGNAL(fileMoved(QString,QString,QString,int,int)), SLOT(onFileMoved(QString,QString,QString,int,int)), Qt::QueuedConnection);
16

                
17
    connect(glue, SIGNAL(collectionAdded(QString,QString)), SLOT(onCollectionAdded(QString,QString)), Qt::QueuedConnection);
18
    connect(glue, SIGNAL(collectionRemoved(QString)), SLOT(onCollectionRemoved(QString)), Qt::QueuedConnection);
19
    connect(glue, SIGNAL(collectionModified(QString,QString)), SLOT(onCollectionModified(QString,QString)), Qt::QueuedConnection);
20
    connect(glue, SIGNAL(collectionMoved(QString,int,int)), SLOT(onCollectionMoved(QString,int,int)), Qt::QueuedConnection);
21

                
22
    connect(glue, SIGNAL(trafficMonitorUpdate(int,int,int,int,int,int,int,int,int,int,QDateTime,double)), SLOT(onTrafficMonitorUpdate(int,int,int,int,int,int,int,int,int,int,QDateTime,double)), Qt::QueuedConnection);
23
}
24

                
25
DownloadQueueModel::~DownloadQueueModel()
26
{
27
    delete rootItem;
28
}
29

                
30
int DownloadQueueModel::columnCount(const QModelIndex &parent) const
31
{
32
    if (parent.isValid())
33
        return static_cast(parent.internalPointer())->columnCount();
34
    else
35
        return rootItem->columnCount();
36
}
37

                
38
QVariant DownloadQueueModel::data(const QModelIndex &index, int role) const
39
{
40
    if (!index.isValid())
41
        return QVariant();
42

                
43
    if (role != Qt::DisplayRole)
44
        return QVariant();
45

                
46
    DownloadQueueItem *item = static_cast(index.internalPointer());
47

                
48
    return item->data(index.column());
49
}
50

                
51
Qt::ItemFlags DownloadQueueModel::flags(const QModelIndex &index) const
52
{
53
    if (!index.isValid())
54
        return 0;
55

                
56
    return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
57
}
58

                
59
QVariant DownloadQueueModel::headerData(int section, Qt::Orientation orientation, int role) const
60
{
61
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
62
        switch (section) {
63
            case DownloadQueueItem::FIELD_STATE_STR:
64
                return tr("Progress");
65

                
66
            case DownloadQueueItem::FIELD_SUBJECT:
67
                return tr("Subject");
68

                
69
            case DownloadQueueItem::FIELD_POSTER:
70
                return tr("Poster");
71

                
72
            case DownloadQueueItem::FIELD_GROUPS:
73
                return tr("Groups");
74

                
75
            case DownloadQueueItem::FIELD_FILE_SIZE_STR:
76
                return tr("File size");
77

                
78
            case DownloadQueueItem::FIELD_FILE_SIZE_REMAINING_STR:
79
                return tr("File size remaining");
80

                
81
            case DownloadQueueItem::FIELD_STAMP:
82
                return tr("Stamp");
83

                
84
            case DownloadQueueItem::FIELD_ESTIMATED_TIME_REMAINING_STR:
85
                return tr("Estimated time remaining");
86

                
87
            case DownloadQueueItem::FIELD_ESTIMATED_TIME_TO_FINISH_STR:
88
                return tr("Estimated time to finish");
89

                
90
            case DownloadQueueItem::FIELD_PROGRESS:
91
            case DownloadQueueItem::FIELD_FILE_SIZE:
92
            case DownloadQueueItem::FIELD_FILE_SIZE_REMAINING:
93
            case DownloadQueueItem::FIELD_REAL_FILENAME:
94
            default:
95
                break;
96
        }
97
    }
98

                
99
    return QVariant();
100
}
101

                
102
QModelIndex DownloadQueueModel::index(int row, int column, const QModelIndex &parent) const
103
{
104
    if (!hasIndex(row, column, parent))
105
        return QModelIndex();
106

                
107
    DownloadQueueItem *parentItem;
108

                
109
    if (!parent.isValid())
110
        parentItem = rootItem;
111
    else
112
        parentItem = static_cast(parent.internalPointer());
113

                
114
    DownloadQueueItem *childItem = parentItem->child(row);
115
    if (childItem)
116
        return createIndex(row, column, childItem);
117
    else
118
        return QModelIndex();
119
}
120

                
121
QModelIndex DownloadQueueModel::parent(const QModelIndex &index) const
122
{
123
    if (!index.isValid())
124
        return QModelIndex();
125

                
126
    DownloadQueueItem *childItem = static_cast(index.internalPointer());
127
    DownloadQueueItem *parentItem = childItem->parent();
128

                
129
    if (parentItem == rootItem || !parentItem)
130
        return QModelIndex();
131

                
132
    return createIndex(parentItem->row(), 0, parentItem);
133
}
134

                
135
int DownloadQueueModel::rowCount(const QModelIndex &parent) const
136
{
137
    DownloadQueueItem *parentItem;
138
    if (parent.column() > 0)
139
        return 0;
140

                
141
    if (!parent.isValid())
142
        parentItem = rootItem;
143
    else
144
        parentItem = static_cast(parent.internalPointer());
145

                
146
    return parentItem->childCount();
147
}
148

                
149
void DownloadQueueModel::loadDownloadQueue(QNNTPGrabGlue *glue)
150
{
151
    Q_ASSERT(glue != NULL);
152

                
153
    QList collections = glue->schedularGetAllTasks();
154

                
155
    for (int i = 0; i < collections.count(); i++) {
156
        QNNTPGrabGlue::Collection collection = collections[i];
157

                
158
        DownloadQueueItem *collection_item = new DownloadQueueItem();
159

                
160
        for (int j = 0; j < collection.files.count(); j++) {
161
            QNNTPGrabGlue::File file = collection.files[j];
162

                
163
            DownloadQueueItem *file_item = new DownloadQueueItem(collection_item);
164

                
165
            file_item->subject = file.subject;
166
            file_item->poster = file.poster;
167
            file_item->size = file.file_size;
168
            file_item->sizeRemaining = file.file_size_remaining;
169
            file_item->stamp = file.stamp;
170
            file_item->state = file.status;
171
            file_item->realFilename = file.filename;
172

                
173
            for (int k = 0; k < file.groups.count(); k++) {
174
                if (file_item->groups.isEmpty()) {
175
                    file_item->groups = file.groups[k];
176
                } else {
177
                    file_item->groups += QString(",") + file.groups[k];
178
                }
179
            }
180

                
181
            if (file.status == QNNTPGrabGlue::TASK_STATE_SKIPPED) {
182
                file_item->progress = 0;
183
            } else {
184
                file_item->progress = calculateProgress(file.file_size, file.file_size_remaining);
185
            }
186

                
187
            switch(file.status) {
188
                case QNNTPGrabGlue::TASK_STATE_WAITING_FOR_DOWNLOAD:
189
                case QNNTPGrabGlue::TASK_STATE_DOWNLOADING:
190
                    file_item->stateStr = "";
191
                    break;
192

                
193
                case QNNTPGrabGlue::TASK_STATE_WAITING_FOR_DECODE:
194
                    file_item->stateStr = tr("Waiting");
195
                    break;
196

                
197
                case QNNTPGrabGlue::TASK_STATE_DECODING:
198
                    file_item->stateStr = tr("Decoding");
199
                    break;
200

                
201
                case QNNTPGrabGlue::TASK_STATE_FINISHED_COMPLETE:
202
                    file_item->stateStr = tr("Done");
203
                    break;
204

                
205
                case QNNTPGrabGlue::TASK_STATE_FINISHED_INCOMPLETE:
206
                    file_item->stateStr = tr("Incomplete");
207
                    break;
208

                
209
                case QNNTPGrabGlue::TASK_STATE_FINISHED_NO_PARTS_AVAIL:
210
                    file_item->stateStr = tr("Not available");
211
                    break;
212

                
213
                case QNNTPGrabGlue::TASK_STATE_SKIPPED:
214
                    file_item->stateStr = tr("Skipped");
215
                    break;
216

                
217
                default:
218
                    file_item->stateStr = "";
219
                    break;
220
            };
221

                
222
            collection_item->appendChild(file_item);
223
        }
224

                
225
        collection_item->progress = calculateProgress(collection.total_size, collection.total_size_remaining);
226
        collection_item->subject = collection.collection_name;
227
        collection_item->poster = collection.poster;
228
        collection_item->size = collection.total_size;
229
        collection_item->sizeRemaining = collection.total_size_remaining;
230

                
231

                
232
        rootItem->appendChild(collection_item);
233
    }
234
}
235

                
236
int DownloadQueueModel::calculateProgress(quint64 size, quint64 sizeRemaining)
237
{
238
    if (size == 0) {        /* Prevent a divide by zero */
239
        return 0;
240
    }
241

                
242
    int ret = (((double)size - sizeRemaining) / size) * 100;
243

                
244
    if (ret < 0 || ret > 100) {
245
        qWarning("Invalid progress detected, size = %lli, size_remaining = %lli", size, sizeRemaining);
246
    }
247

                
248
    return qMin(100, ret);
249
}
250

                
251
void DownloadQueueModel::onFileDownloadStateUpdate(QString collection_name, QString subject, int num_parts_total, int num_parts_done,
252
                                                   int num_parts_failed, quint64 file_size, quint64 file_size_remaining,
253
                                                   quint64 total_size, quint64 total_size_remaining)
254
{
255
    Q_UNUSED(num_parts_total);
256
    Q_UNUSED(num_parts_done);
257
    Q_UNUSED(num_parts_failed);
258
#if 0
259
    QTreeWidgetItem *collection_node;
260
    QTreeWidgetItem *subject_node;
261

                
262
    collection_node = getTreeWidgetByCollection(collection_name);
263
    subject_node = getTreeWidgetBySubject(collection_node, subject);
264
    if (subject_node == NULL) {
265
        qWarning("onFileDownloadStateUpdate: Subject '%s' from collection '%s' could not found", collection_name.toUtf8().data(), subject.toUtf8().data());
266
        return;
267
    }
268

                
269
    collection_node->setText(FIELD_PROGRESS, QString("%1").arg(calculateProgress(total_size, total_size_remaining)));
270
    collection_node->setText(FIELD_STATE_STR, collection_node->text(FIELD_STATE_STR));    /* Force redraw */
271
    collection_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(total_size_remaining));
272
    collection_node->setText(FIELD_FILE_SIZE_REMAINING_STR, glue->utilsCalculateFileSize(total_size_remaining));
273

                
274
    subject_node->setText(FIELD_PROGRESS, QString("%1").arg(calculateProgress(file_size, file_size_remaining)));
275
    subject_node->setText(FIELD_STATE_STR, subject_node->text(FIELD_STATE_STR));    /* Force redraw */
276
    subject_node->setData(FIELD_FILE_SIZE_REMAINING, Qt::DisplayRole, QVariant(file_size_remaining));
277
    subject_node->setText(FIELD_FILE_SIZE_REMAINING_STR, glue->utilsCalculateFileSize(file_size_remaining));
278
#endif
279
}
280

                
281
void DownloadQueueModel::onFileStateChanged(QString collection_name, QString subject, QString real_filename, QNNTPGrabGlue::TaskState old_state, QNNTPGrabGlue::TaskState new_state)
282
{
283
    Q_UNUSED(old_state);
284

                
285
#if 0
286
    QTreeWidgetItem *collection_node;
287
    QTreeWidgetItem *subject_node;
288

                
289
    collection_node = getTreeWidgetByCollection(collection_name);
290
    subject_node = getTreeWidgetBySubject(collection_node, subject);
291

                
292
    if (!subject_node) {
293
        qWarning("onFileStateChanged: Subject '%s' from collection '%s' could not be found", subject.toUtf8().data(), collection_name.toUtf8().data());
294
        return;
295
    }
296

                
297
    quint64 file_size;
298
    quint64 file_size_remaining;
299
    QString size_remaining;
300
    quint64 total_size;
301
    quint64 total_size_remaining;
302
    int progress;
303

                
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff