/
availablegroups.cpp
671 lines (542 loc) · 15.5 KB
/
availablegroups.cpp
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/***************************************************************************
Copyright (C) 2011-2015 by Martin Demet
quban100@gmail.com
This file is part of Quban.
Quban 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 3 of the License, or
(at your option) any later version.
Quban is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Quban. If not, see <http://www.gnu.org/licenses/>.
***************************************************************************/
#include <common.h>
#include <QDebug>
#include <QtDebug>
#include <QIcon>
#include <QMessageBox>
#include "quban.h"
#include "availablegroups.h"
extern Quban* quban;
static const char *cPatterns[] = {"Newsgroup", "Articles"};
static QIcon* subscribedIcon = 0;
static QIcon* notSubscribedIcon = 0;
static QIcon* crossIcon = 0;
bool availItemLessThan(const AvailGroupItem* s1, const AvailGroupItem* s2);
static int g_sortColumn;
static Qt::SortOrder g_sortOrder;
AvailableGroup::AvailableGroup(QString _name, quint16 hostId, quint64 _articles)
{
name=_name;
subscribed = false;
serverPresence.insert(hostId, true);
articles.insert(hostId, _articles);
}
AvailableGroup::AvailableGroup(char *p)
{
subscribed = false;
char*i = p;
i=retrieve(i, name);
quint16 count;
quint16 id;
bool b;
quint64 artNum;
memcpy(&count, i, sizeof(quint16));
i+=sizeof(quint16);
for (quint16 j = 0; j < count; j++)
{
memcpy(&id, i, sizeof(quint16));
i+=sizeof(quint16);
memcpy(&b, i, sizeof(bool));
i+=sizeof(bool);
serverPresence.insert(id, b);
}
memcpy(&count, i, sizeof(quint16));
i+=sizeof(quint16);
for (quint16 j = 0; j < count; j++)
{
memcpy(&id, i, sizeof(quint16));
i+=sizeof(quint16);
memcpy(&artNum, i, sizeof(quint64));
i+=sizeof(quint64);
articles.insert(id, artNum);
}
}
AvailableGroup::~AvailableGroup()
{
articles.clear();
serverPresence.clear();
}
quint32 AvailableGroup::size()
{
return (name.length() + sizeof(quint16)*3 + serverPresence.count()*(sizeof(quint16) + sizeof(bool))
+ articles.count()*(sizeof(quint16) + sizeof(quint64)));
}
char * AvailableGroup::data()
{
char *p=new char[size()];
char *i=p;
i=insert(name, i);
//Map count
quint16 count=serverPresence.count();
memcpy(i, &count, sizeof(quint16));
i+=sizeof(quint16);
quint16 id;
bool b;
QMap<quint16, bool>::iterator it;
for (it = serverPresence.begin(); it != serverPresence.end(); ++it)
{
id=it.key();
b=it.value();
memcpy(i, &id, sizeof(quint16));
i+=sizeof(quint16);
memcpy(i, &b, sizeof(bool));
i+=sizeof(bool);
}
count = articles.count();
memcpy(i, &count, sizeof(quint16));
i+=sizeof(quint16);
quint64 artNumber;
QMap<quint16, quint64>::iterator aIt;
for (aIt = articles.begin(); aIt != articles.end(); ++aIt)
{
id = aIt.key();
artNumber = aIt.value();
// qDebug() << "id: " << id << " articles: " << artNumber;
memcpy(i, &id, sizeof(quint16));
i+=sizeof(quint16);
memcpy(i, &artNumber, sizeof(quint64));
i+=sizeof(quint64);
}
return p;
}
void AvailableGroup::addHost(quint16 hostId)
{
serverPresence[hostId]=true;
}
AvailableGroups::AvailableGroups(DbEnv *dbenv, Db* agroupDb, QHash<QString, NewsGroup*>* _subscribedGroups, Servers *_servers, QWidget * p)
: parent(p), groupDb(agroupDb), dbEnv(dbenv), servers(_servers), subscribedGroups(_subscribedGroups)
{
int ret;
if ((ret=agroupDb->open(NULL, "availableGroups", NULL, DB_BTREE, DB_CREATE | DB_THREAD, 0644)) != 0)
QMessageBox::warning(0, tr("Error"), tr("Unable to open available groups database ..."));
updating=false;
model = 0;
Num_Col = 1;
}
AvailableGroups::~ AvailableGroups( )
{
qDeleteAll(groupsList);
groupsList.clear();
if (subscribedIcon)
{
Q_DELETE(subscribedIcon);
Q_DELETE(notSubscribedIcon);
Q_DELETE(crossIcon);
}
}
void AvailableGroups::getGroups(QHash<QString, AvailableGroup*>* aGroupsUpdate)
{
QHashIterator<QString, AvailableGroup*> it(*aGroupsUpdate);
while(it.hasNext())
{
it.next();
if (groupsList.contains(it.key()))
aGroupsUpdate->insert(it.key(), groupsList.value(it.key()));
}
}
AvailGroupModel* AvailableGroups::loadData()
{
if (!model)
{
model = new AvailGroupModel(servers, groupDb,this);
Dbt *key = 0, *data = 0;
key=new Dbt;
data=new Dbt;
Dbc *cursor = 0;
AvailableGroup *g = 0;
if (groupDb->cursor(NULL, &cursor, 0))
qDebug() << "Error opening cursor";
int ret;
quint32 count=0;
while ((ret=cursor->get(key, data, DB_NEXT)) != DB_NOTFOUND)
{
g=new AvailableGroup((char*)data->get_data());
if (subscribedGroups->contains(g->getName()))
g->setSubscribed(true);
model->setupTopLevelItem(g); // add to model
groupsList.insert(g->getName(), g);
++count;
}
qDebug() << "Available Groups: " << count;
cursor->close();
Q_DELETE(key);
Q_DELETE(data);
}
return model;
}
AvailGroupModel* AvailableGroups::reloadData()
{
qDeleteAll(groupsList);
groupsList.clear();
Q_DELETE(model);
return loadData();
}
void AvailableGroups::prepareToClose( )
{
// visible = false;
}
void AvailableGroups::slotUnsubscribe(QString name) // this is called from subscribed groups and views
{
if (groupsList.contains(name))
{
AvailableGroup* g = groupsList.value(name);
if (g->isSubscribed())
{
g->setSubscribed(false);
AvailGroupItem* rootItem = model->getRootItem();
AvailGroupItem *child = 0;
for (int i=0; i<rootItem->childCount(); ++i)
{
child = rootItem->child(i);
if (child->data(0).toString() == name)
{
qDebug() << "Matched!!";
child->setSubscribed(false);
break;
}
}
emit sigRefreshView();
}
else
quban->statusBar()->showMessage(tr("News group is not subscribed to."), 3000);
}
}
void AvailableGroups::slotDeleteServer(quint16 serverId)
{
Dbt key, data;
memset(&key, 0, sizeof(key));
memset(&data, 0, sizeof(data));
Dbc *cursor = 0;
int ret;
groupDb->cursor(0, &cursor, DB_WRITECURSOR);
while ((ret=cursor->get(&key, &data, DB_NEXT)) != DB_NOTFOUND)
{
g=new AvailableGroup((char*)data.get_data());
g->removeServerPresence(serverId);
//resave the group...
const char *p=g->data();
memset(&data, 0, sizeof(data));
data.set_data((void*)p);
data.set_size(g->size());
if (g->noServerPresence())
{
ret=cursor->del(0);
if (ret != 0)
quban->getLogAlertList()->logMessage(LogAlertList::Error, tr("Cannot delete server ") + servers->value(serverId)->getHostName() + tr(" group record"));
}
else if ((ret=cursor->put(&key, &data, DB_CURRENT)) != 0)
quban->getLogAlertList()->logMessage(LogAlertList::Error, tr("Cannot update group record. Error: ") + dbEnv->strerror(ret));
Q_DELETE(p);
}
cursor->close();
reloadData();
}
void AvailableGroups::subscribe(QString& name)
{
if (groupsList.contains(name))
{
AvailableGroup* g = groupsList.value(name);
if (!g->isSubscribed())
{
g->setSubscribed(true);
emit subscribe(g);
}
else
quban->statusBar()->showMessage(tr("News group is already subscribed to."), 3000);
}
}
void AvailableGroups::slotRefreshView()
{
emit sigRefreshView(); // received from one view to propagate to all
}
///////////////////////////////////////////////////////////////////////////////////////
AvailGroupItem::AvailGroupItem(const QList<QVariant> &data, const QList<QVariant> &icons, bool _subscribed, AvailGroupItem *parent)
{
parentItem = parent;
itemData = data;
iconData = icons;
subscribed = _subscribed;
}
AvailGroupItem::~AvailGroupItem()
{
qDeleteAll(childItems);
}
void AvailGroupItem::appendChild(AvailGroupItem *item)
{
childItems.append(item);
}
void AvailGroupItem::setSubscribed(bool subsVal)
{
subscribed = subsVal;
if (subscribed)
iconData[0] = *subscribedIcon;
else
iconData[0] = *notSubscribedIcon;
}
AvailGroupItem *AvailGroupItem::child(int row)
{
return childItems.value(row);
}
int AvailGroupItem::childCount() const
{
return childItems.count();
}
bool AvailGroupItem::removeChildren(int position, int count)
{
if (position < 0 || position + count > childItems.size())
return false;
AvailGroupItem* item = 0;
for (int row = 0; row < count; ++row)
{
item = childItems.takeAt(position);
Q_DELETE(item);
}
return true;
}
int AvailGroupItem::columnCount() const
{
return itemData.count();
}
QVariant AvailGroupItem::data(int column) const
{
return itemData.value(column);
}
QVariant AvailGroupItem::icon(int column) const
{
return iconData.value(column);
}
AvailGroupItem *AvailGroupItem::parent()
{
return parentItem;
}
int AvailGroupItem::row() const
{
if (parentItem)
return parentItem->childItems.indexOf(const_cast<AvailGroupItem*>(this));
return 0;
}
AvailGroupModel::AvailGroupModel(Servers *_servers, Db* _db, QObject *parent)
: QAbstractItemModel(parent), servers(_servers), db(_db)
{
QList<QVariant> rootData;
QList<QVariant> iconData;
displayedArticles = 0;
if (!subscribedIcon)
{
subscribedIcon = new QIcon(":/quban/images/fatCow/Newspaper-Add.png");
notSubscribedIcon = new QIcon(":/quban/images/fatCow/Newspaper.png");
crossIcon = new QIcon(":/quban/images/fatCow/Cross.png");
}
rootData << tr(cPatterns[ag_AvailableGroup_Col]);
QByteArray ba;
const char *c_str = 0;
Servers::iterator it;
for (it=servers->begin(); it != servers->end(); ++it)
{
if (it.value() && it.value()->getServerType() != NntpHost::dormantServer)
{
ba = it.value()->getName().toLocal8Bit();
c_str = ba.data();
rootData.append(tr(c_str));
}
}
rootItem = new AvailGroupItem(rootData, iconData, false);
}
AvailGroupModel::~AvailGroupModel()
{
Q_DELETE(rootItem);
}
int AvailGroupModel::columnCount(const QModelIndex &) const
{
return rootItem->columnCount();
}
bool AvailGroupModel::hasChildren ( const QModelIndex &) const
{
return rootItem->childCount();
}
bool AvailGroupModel::removeRows(int position, int rows, const QModelIndex &parent)
{
AvailGroupItem *parentItem = getItem(parent);
bool success = true;
beginRemoveRows(parent, position, position + rows - 1);
success = parentItem->removeChildren(position, rows);
endRemoveRows();
if (success)
displayedArticles -= rows;
return success;
}
AvailGroupItem *AvailGroupModel::getItem(const QModelIndex &index) const
{
if (index.isValid())
{
AvailGroupItem *item = static_cast<AvailGroupItem*>(index.internalPointer());
if (item)
return item;
}
return rootItem;
}
QVariant AvailGroupModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= rootItem->childCount() || index.row() < 0)
return QVariant();
if (role != Qt::DisplayRole && role != Qt::DecorationRole)
return QVariant();
AvailGroupItem *item = static_cast<AvailGroupItem*>(index.internalPointer());
if (role == Qt::DisplayRole)
{
if (index.column() == 0)
return item->data(index.column());
else
{
if (!item->icon(index.column()).isValid()) // doesn't have an icon
return QString("%L1").arg(item->data(index.column()).toULongLong());
else
return QVariant();
}
}
else
return item->icon(index.column());
}
QVariant AvailGroupModel::rawData(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= rootItem->childCount() || index.row() < 0)
return QVariant();
if (role != Qt::DisplayRole && role != Qt::DecorationRole)
return QVariant();
AvailGroupItem *item = static_cast<AvailGroupItem*>(index.internalPointer());
if (role == Qt::DisplayRole)
{
if (index.column() == 0)
return item->data(index.column());
else
return item->data(index.column()).toUInt();
}
else
return item->icon(index.column());
}
Qt::ItemFlags AvailGroupModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return 0;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant AvailGroupModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
return rootItem->data(section);
return QVariant();
}
QModelIndex AvailGroupModel::index(int row, int column, const QModelIndex &parent) const
{
if (!hasIndex(row, column, parent))
return QModelIndex();
AvailGroupItem *parentItem = 0;
parentItem = rootItem;
AvailGroupItem *childItem = parentItem->child(row);
if (childItem)
return createIndex(row, column, childItem);
else
return QModelIndex();
}
QModelIndex AvailGroupModel::parent(const QModelIndex &index) const
{
if (!index.isValid())
return QModelIndex();
AvailGroupItem *childItem = static_cast<AvailGroupItem*>(index.internalPointer());
AvailGroupItem *parentItem = childItem->parent();
if (parentItem == rootItem)
return QModelIndex();
return createIndex(parentItem->row(), 0, parentItem);
}
int AvailGroupModel::fullRowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
return rootItem->childCount();
}
int AvailGroupModel::rowCount(const QModelIndex &parent) const
{
if (parent.column() > 0)
return 0;
return rootItem->childCount();
}
void AvailGroupModel::setupTopLevelItem(AvailableGroup* g)
{
QList<QVariant> columnData;
QList<QVariant> iconData;
columnData << g->getName();
if (g->isSubscribed())
iconData << *subscribedIcon;
else
iconData << *notSubscribedIcon;
if (servers != NULL)
{
QMap<quint16, NntpHost*>::iterator it;
for (it=servers->begin(); it != servers->end(); ++it)
{
if (!it.value())
qDebug() << "Got invalid server !!!!!!!!!!!";
if (it.value() && it.value()->getServerType() != NntpHost::dormantServer)
{
if (g->getServerPresence(it.key()) == true)
{
//columnData << QString("%L1").arg(g->getArticles(it.key()));
columnData << g->getArticles(it.key());
iconData << QVariant();
}
else
{
columnData << QVariant();
iconData << *crossIcon;
}
}
}
}
// Append a new item to the root item's list of children.
rootItem->appendChild(new AvailGroupItem(columnData, iconData, g->isSubscribed(), rootItem));
}
void AvailGroupModel::sort ( int column, Qt::SortOrder order )
{
emit layoutAboutToBeChanged();
g_sortColumn = column;
g_sortOrder = order;
qSort(rootItem->getChildItems().begin(), rootItem->getChildItems().end(), availItemLessThan);
emit layoutChanged();
emit sigFilter();
}
bool availItemLessThan(const AvailGroupItem* s1, const AvailGroupItem* s2)
{
if (g_sortColumn == 0)
{
if (g_sortOrder == Qt::AscendingOrder)
return s2->data(g_sortColumn).toString() < s1->data(g_sortColumn).toString();
else
return s1->data(g_sortColumn).toString() < s2->data(g_sortColumn).toString();
}
else
{
if (g_sortOrder == Qt::AscendingOrder)
return s2->data(g_sortColumn).toLongLong() < s1->data(g_sortColumn).toLongLong();
else
return s1->data(g_sortColumn).toLongLong() < s2->data(g_sortColumn).toLongLong();
}
}