summaryrefslogtreecommitdiffstats
blob: e242003fe61bd4aa03fcb0de916639186796e545 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2010-2016 Isode Limited.
 * All rights reserved.
 * See the COPYING file for more information.
 */

#include <Swift/QtUI/EventViewer/EventModel.h>

#include <Swiften/Base/Log.h>

namespace Swift {

namespace {
    const int inactiveEventsLimit = 50;
}

EventModel::EventModel() {

}

EventModel::~EventModel() {
    foreach (QtEvent* event, activeEvents_) {
        delete event;
    }
    foreach (QtEvent* event, inactiveEvents_) {
        delete event;
    }
}

QtEvent* EventModel::getItem(int row) const {
    QtEvent* event = nullptr;
    if (row < activeEvents_.size()) {
        event = activeEvents_[row];
    }
    else {
        int inactiveRow = row - activeEvents_.size();
        if (inactiveRow < inactiveEvents_.size()) {
            event = inactiveEvents_[inactiveRow];
        }
        else {
            SWIFT_LOG(error) << "Misbehaving EventModel requests row index outside of range";
        }
    }
    return event;
}

int EventModel::getNewEventCount() {
    return activeEvents_.size();
}

QVariant EventModel::data(const QModelIndex& index, int role) const {
    if (!index.isValid()) {
        return QVariant();
    }
    QtEvent* item = getItem(index.row());
    QVariant result = item ? item->data(role) : QVariant();
    return result;
}

/*
 * We only reimplement this to get the pointers inside the indices.
 */
QModelIndex EventModel::index(int row, int column, const QModelIndex & parent) const {
    if (!hasIndex(row, column, parent) || parent.isValid()) {
        return QModelIndex();
    }

    return row < rowCount() ? createIndex(row, column, getItem(row)) : QModelIndex();
}

int EventModel::rowCount(const QModelIndex& parent) const {
    /* Invalid parent = root, valid parent = child, and we're a list not a tree.*/
    int count = parent.isValid() ? 0 : activeEvents_.size() + inactiveEvents_.size();
    return count;
}

void EventModel::addEvent(std::shared_ptr<StanzaEvent> event, bool active) {
    beginResetModel();
    if (active) {
        activeEvents_.push_front(new QtEvent(event, active));
    } else {
        inactiveEvents_.push_front(new QtEvent(event, active));
        if (inactiveEvents_.size() > inactiveEventsLimit) {
            removeEvent(inactiveEvents_[inactiveEventsLimit]->getEvent());
        }
    }
    endResetModel();
}

void EventModel::removeEvent(std::shared_ptr<StanzaEvent> event) {
    beginResetModel();
    for (int i = inactiveEvents_.size() - 1; i >= 0; i--) {
        if (event == inactiveEvents_[i]->getEvent()) {
            inactiveEvents_.removeAt(i);
            endResetModel();
            return;
        }
    }

    for (int i = 0; i < activeEvents_.size(); i++) {
        if (event == activeEvents_[i]->getEvent()) {
            activeEvents_.removeAt(i);
            endResetModel();
            return;
        }
    }
    endResetModel();
}

}