File: model.cpp

package info (click to toggle)
psi-plus 1.4.1456-2.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,188 kB
  • sloc: cpp: 211,254; ansic: 19,786; javascript: 13,687; xml: 4,056; sh: 1,610; makefile: 437; objc: 407; python: 277; ruby: 171
file content (294 lines) | stat: -rw-r--r-- 8,307 bytes parent folder | download | duplicates (2)
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
/*
 * model.cpp - plugin
 * Copyright (C) 2010  Evgeny Khryukin
 *
 * This program 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 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */

#include "model.h"

Model::Model(const QStringList &watchedJids_, const QStringList &Sounds_, const QStringList &enabledJids_,
             QObject *parent) :
    QAbstractTableModel(parent),
    watchedJids(watchedJids_), sounds(Sounds_), enabledJids(enabledJids_)
{
    headers << tr("") << tr("Watch for JIDs") << tr("Sounds (if empty default sound will be used)") << tr("") << tr("");

    tmpWatchedJids_ = watchedJids;
    tmpSounds_      = sounds;

    for (auto enabledJid : enabledJids_) {
        tmpEnabledJids_ << (enabledJid == "true" ? true : false);
    }
}

QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role == Qt::DisplayRole) {
        if (orientation == Qt::Horizontal) {
            return headers.at(section);
        } else {
            return section + 1;
        }
    } else
        return QVariant();
}

Qt::ItemFlags Model::flags(const QModelIndex &index) const
{
    Qt::ItemFlags flags = Qt::ItemIsSelectable | Qt::ItemIsEnabled;

    int column = index.column();

    if (column == 0)
        flags |= Qt::ItemIsUserCheckable;
    else if (column == 1 || column == 2)
        flags |= Qt::ItemIsEditable;

    return flags;
}

int Model::columnCount(const QModelIndex & /*parent*/) const { return headers.size(); }

int Model::rowCount(const QModelIndex & /* parent*/) const { return tmpWatchedJids_.size(); }

QVariant Model::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();

    int i = index.column();
    switch (i) {
    case 0:
        if (role == Qt::CheckStateRole) {
            return tmpEnabledJids_.at(index.row()) ? 2 : 0;
        } else if (role == Qt::TextAlignmentRole) {
            return (int)(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant();
        break;
    case 1:
        if (role == Qt::TextAlignmentRole) {
            return (int)(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant(tmpWatchedJids_.at(index.row()));
        break;
    case 2:
        if (role == Qt::TextAlignmentRole) {
            return (int)(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant(tmpSounds_.at(index.row()));
        break;
    case 3:
        if (role == Qt::TextAlignmentRole) {
            return (int)(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant();
        break;
    case 4:
        if (role == Qt::TextAlignmentRole) {
            return (int)(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant();
        break;
    }
    return QVariant();
}

QString Model::jid(const QModelIndex &index) const
{
    if (!index.isValid())
        return QString();

    return watchedJids.at(index.row());
}

QString Model::soundFile(const QModelIndex &index) const
{
    if (!index.isValid())
        return QString();

    return sounds.at(index.row());
}

QString Model::tmpSoundFile(const QModelIndex &index) const
{
    if (!index.isValid())
        return QString();

    return tmpSounds_.at(index.row());
}

bool Model::setData(const QModelIndex &index, const QVariant &value, const int role)
{
    if (!index.isValid() || role != Qt::EditRole)
        return false;

    int column = index.column();
    if (column == 0) {
        bool b = tmpEnabledJids_.at(index.row());
        switch (value.toInt()) {
        case 0:
            tmpEnabledJids_.replace(index.row(), false);
            break;
        case 2:
            tmpEnabledJids_.replace(index.row(), true);
            break;
        case 3:
            tmpEnabledJids_.replace(index.row(), !b);
            break;
        }
    } else if (column == 1)
        tmpWatchedJids_.replace(index.row(), value.toString());

    else if (column == 2)
        tmpSounds_.replace(index.row(), value.toString());

    emit dataChanged(index, index);

    return true;
}

void Model::setStatusForJid(const QString &jid, const QString &status) { statuses.insert(jid, status); }

void Model::deleteRows(const QModelIndexList &indexList)
{
    QList<bool> selected;
    for (int i = 0; i < tmpWatchedJids_.size(); i++) {
        selected << false;
    }

    for (auto index : indexList) {
        selected[index.row()] = true;
    }

    QStringList tmpJids, tmpSounds;
    QList<bool> tmpEnabledJids;
    for (int i = tmpWatchedJids_.size() - 1; i >= 0; i--) {
        if (selected.at(i)) {
            removeRow(i);
        }
    }
}

bool Model::removeRows(int row, int count, const QModelIndex &parent)
{
    beginRemoveRows(parent, row, row + count - 1);
    for (int i = 0; i < count; i++) {
        tmpWatchedJids_.removeAt(row);
        tmpSounds_.removeAt(row);
        tmpEnabledJids_.removeAt(row);
    }
    endRemoveRows();
    return true;
}

void Model::reset()
{
    tmpWatchedJids_ = watchedJids;
    tmpSounds_      = sounds;

    tmpEnabledJids_.clear();
    for (auto enabledJid : enabledJids) {
        tmpEnabledJids_ << (enabledJid == "true" ? true : false);
    }
}

void Model::addRow(const QString &jid)
{
    beginInsertRows(QModelIndex(), tmpWatchedJids_.size(), tmpWatchedJids_.size());
    tmpWatchedJids_ << jid;
    tmpSounds_ << "";

    if (!jid.isEmpty()) { //вызов происходит из меню контакта
        watchedJids.append(jid);
        sounds.append("");
        enabledJids.append("true");
    }

    tmpEnabledJids_.append(true);
    endInsertRows();
}

void Model::deleteRow(const QString &jid)
{
    int index = watchedJids.indexOf(QRegExp(jid, Qt::CaseInsensitive));
    if (index == -1)
        return;
    watchedJids.removeAt(index);
    sounds.removeAt(index);
    tmpWatchedJids_.removeAt(index);
    tmpSounds_.removeAt(index);
    tmpEnabledJids_.removeAt(index);

    emit layoutChanged();
}

void Model::apply()
{
    watchedJids = tmpWatchedJids_;
    sounds      = tmpSounds_;
    enabledJids.clear();
    for (auto enabledJid : tmpEnabledJids_) {
        enabledJids << (enabledJid ? "true" : "false");
    }
}

QString Model::statusByJid(const QString &jid) const { return statuses.value(jid, "offline"); }

QString Model::soundByJid(const QString &jid) const
{
    QString sound;
    int     index = watchedJids.indexOf(QRegExp(jid, Qt::CaseInsensitive));
    if (index < sounds.size() && index != -1)
        sound = sounds.at(index);

    return sound;
}

int Model::indexByJid(const QString &jid) const { return watchedJids.indexOf(QRegExp(jid, Qt::CaseInsensitive)); }

QStringList Model::getWatchedJids() const { return watchedJids; }

QStringList Model::getSounds() const { return sounds; }

QStringList Model::getEnabledJids() const { return enabledJids; }

void Model::setJidEnabled(const QString &jid, bool enabled)
{
    // Do nothing if need to disable jid which is not in watcher list
    if (!getWatchedJids().contains(jid, Qt::CaseInsensitive) && !enabled) {
        return;
    }

    // Before add Jid to watcher list
    if (!getWatchedJids().contains(jid, Qt::CaseInsensitive)) {
        addRow(jid);
    }

    QModelIndex ind = index(indexByJid(jid), 0);
    setData(ind, enabled ? 2 : 0);
}

bool Model::jidEnabled(const QString &jid)
{
    // watcher doesn't applied for this jid
    if (!getWatchedJids().contains(jid, Qt::CaseInsensitive)) {
        return false;
    }

    QModelIndex ind = index(indexByJid(jid), 0);
    return (data(ind, Qt::CheckStateRole) == 2) ? true : false;
}