summaryrefslogtreecommitdiffstats
blob: 57d5aacfb0398baf4aa368684bd4d10b6d3704d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/MUCSearch/MUCSearchServiceItem.h>

#include <memory>

#include <QString>
#include <QVector>
#include <QtAlgorithms>

namespace Swift {

MUCSearchServiceItem::MUCSearchServiceItem(const QString& jidString) : jidString_(jidString) {

}

void MUCSearchServiceItem::addRoom(std::shared_ptr<MUCSearchItem> room) {
    room->setParent(shared_from_this());
    rooms_.push_back(room);
    if (sortOrder_) {
        sort();
    }
}

void MUCSearchServiceItem::addRooms(const std::vector<std::shared_ptr<MUCSearchItem> >& rooms) {
    for (auto&& room: rooms) {
        room->setParent(shared_from_this());
        rooms_.push_back(room);
    }
    if (sortOrder_) {
        sort();
    }
}

int MUCSearchServiceItem::rowCount() {
    return rooms_.count();
}

MUCSearchItem* MUCSearchServiceItem::getItem(int i) {
    return rooms_[i].get();
}

QVariant MUCSearchServiceItem::data(int role) {
    switch (role) {
        case Qt::DisplayRole:
            return QVariant(jidString_);
        default:
            return QVariant();
    }
}

QString MUCSearchServiceItem::getHost() const {
    return jidString_;
}

void MUCSearchServiceItem::setSorting(Qt::SortOrder sortOrder) {
    sortOrder_ = sortOrder;
    sort();
}

void MUCSearchServiceItem::sort() {
    if (*sortOrder_ == Qt::AscendingOrder) {
        qStableSort(rooms_.begin(), rooms_.end(), [](const std::shared_ptr<MUCSearchItem>& item1, const std::shared_ptr<MUCSearchItem>& item2) -> bool {
            return QString::localeAwareCompare(item1->data(Qt::DisplayRole).toString(), item2->data(Qt::DisplayRole).toString()) < 0;
        });
    }
    else {
        qStableSort(rooms_.begin(), rooms_.end(), [](const std::shared_ptr<MUCSearchItem>& item1, const std::shared_ptr<MUCSearchItem>& item2) -> bool {
            return QString::localeAwareCompare(item1->data(Qt::DisplayRole).toString(), item2->data(Qt::DisplayRole).toString()) > 0;
        });
    }
}

}