File: models.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 (464 lines) | stat: -rw-r--r-- 13,602 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
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
/*
 * models.cpp - plugin
 * Copyright (C) 2009-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 "models.h"
#include "optionsparser.h"

#include <QDateTime>
#include <QDir>
#include <QPixmap>
#include <QSet>

//----------------------------------------
//--BaseModel-----------------------------
//----------------------------------------
void BaseModel::reset()
{
    selected_.clear();
    beginResetModel();
    endResetModel();
}

int BaseModel::selectedCount(const QModelIndex &) const { return selected_.size(); }

void BaseModel::selectAll(const QModelIndexList &list)
{
    emit layoutAboutToBeChanged();
    selected_.clear();
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
    selected_ = QSet<QModelIndex>(list.begin(), list.end());
#else
    selected_ = list.toSet();
#endif
    emit updateLabel(0);
    emit layoutChanged();
}

void BaseModel::unselectAll()
{
    emit layoutAboutToBeChanged();
    selected_.clear();
    emit updateLabel(0);
    emit layoutChanged();
}

bool BaseModel::isSelected(const QModelIndex &index) const { return selected_.contains(index); }

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

    if (index.column() == 0)
        flags |= Qt::ItemIsUserCheckable;

    return flags;
}

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

    switch (value.toInt()) {
    case (0):
        if (selected_.contains(index))
            selected_.remove(index);
        break;
    case (2):
        if (!selected_.contains(index))
            selected_ << index;
        break;
    case (3):
        if (selected_.contains(index))
            selected_.remove(index);
        else
            selected_ << index;
        break;
    }

    emit dataChanged(index, index);
    emit updateLabel(0);

    return true;
}

QVariant BaseModel::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();
}

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

//---------------------------------------
//------BaseFileModel--------------------
//---------------------------------------
void BaseFileModel::reset()
{
    files_.clear();
    BaseModel::reset();
}

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

QString BaseFileModel::fileName(const QModelIndex &index) const
{
    if (!index.isValid())
        return QString();

    int i = index.row();
    if (i < 0 || i >= files_.size())
        return QString();

    QString name = files_.at(i);
    return name.split("/", QString::SkipEmptyParts).last();
}

QString BaseFileModel::filePass(const QModelIndex &index) const
{
    if (!index.isValid())
        return QString();

    int i = index.row();
    if (i < 0 || i >= files_.size())
        return QString();

    return files_.at(i);
}

int BaseFileModel::fileSize(const QModelIndex &index) const
{
    if (!index.isValid())
        return 0;

    QFile file(filePass(index));

    return int(file.size());
}

QString BaseFileModel::fileDate(const QModelIndex &index) const
{
    QString result;
    if (!index.isValid())
        return result;
#if QT_VERSION < QT_VERSION_CHECK(5, 10, 0)
    result = QFileInfo(filePass(index)).created().toString("yyyy-MM-dd");
#else
    result    = QFileInfo(filePass(index)).birthTime().toString("yyyy-MM-dd");
#endif
    return result;
}

void BaseFileModel::deleteSelected()
{
    emit layoutAboutToBeChanged();

    for (const QModelIndex &index : selected_) {
        const QString fileName = filePass(index);
        if (fileName.isEmpty())
            continue;

        QFile file(fileName);
        if (file.open(QIODevice::ReadWrite)) {
            file.remove();
        }
    }

    setDirs(dirs_);

    emit updateLabel(0);
}

void BaseFileModel::setDirs(const QStringList &dirs)
{
    reset();
    dirs_ = dirs;
    for (const QString &dirName : dirs_) {
        QDir Dir(dirName);
        for (const QString &fileName : Dir.entryList(QDir::Files)) {
            files_.append(Dir.absoluteFilePath(fileName));
        }
    }

    emit layoutChanged();
}

//----------------------------------------
//--ClearingModel-------------------------
//----------------------------------------
ClearingModel::ClearingModel(const QString &dir, QObject *parent) : BaseFileModel(parent)
{
    headers << tr("") << tr("Nick") << tr("Domain") << tr("Size") << tr("Creation Date");

    setDirs({ dir });
}

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

    int     i        = index.column();
    QString filename = fileName(index);
    filename         = filename.replace("%5f", "_");
    filename         = filename.replace("%2d", "-");
    filename         = filename.replace("%25", "@");
    switch (i) {
    case (0):
        if (role == Qt::CheckStateRole) {
            return isSelected(index) ? 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) {
            if (filename.contains("_at_"))
                return QVariant(filename.split("_at_").first());
            else
                return QVariant();
        }
        break;
    case (2):
        if (role == Qt::TextAlignmentRole) {
            return int(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant(filename.split("_at_").last());
        break;
    case (3):
        if (role == Qt::TextAlignmentRole) {
            return int(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant(fileSize(index));
        break;
    case (4):
        if (role == Qt::TextAlignmentRole) {
            return int(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant(fileDate(index));
        break;
    }
    return QVariant();
}

//----------------------------------------
//--ClearingVcardModel--------------------
//----------------------------------------
QVariant ClearingVcardModel::data(const QModelIndex &index, int role) const
{
    if (index.column() == 2) {
        if (role == Qt::DisplayRole) {
            QString domain = fileName(index).split("_at_").last();
            domain.chop(4);
            domain = domain.replace("%5f", "_");
            domain = domain.replace("%2d", "-");
            domain = domain.replace("%25", "@");
            return QVariant(domain);
        }
    }
    return ClearingModel::data(index, role);
}

//----------------------------------------
//--ClearingHistoryModel------------------
//----------------------------------------
QVariant ClearingHistoryModel::data(const QModelIndex &index, int role) const
{
    QString filename = fileName(index);
    filename         = filename.replace("%5f", "_");
    filename         = filename.replace("%2d", "-");
    filename         = filename.replace("%25", "@");
    if (role == Qt::DisplayRole) {
        if (index.column() == 2) {
            QString domain;
            if (filename.contains("_in_")) {
                domain = filename.split("_in_").last();
                domain = domain.replace("_at_", "@");
            } else {
                domain = filename.split("_at_").last();
                domain.remove(".history");
            }
            return QVariant(domain);
        } else if (index.column() == 1) {
            QString jid;
            if (filename.contains("_in_")) {
                jid = filename.split("_in_").first();
                jid = jid.replace("_at_", "@");
            } else {
                if (filename.contains("_at_"))
                    return QVariant(filename.split("_at_").first());
                else
                    return QVariant();
            }
        }
    }

    return ClearingModel::data(index, role);
}

//----------------------------------------
//--ClearingAvatarModel-------------------
//----------------------------------------
ClearingAvatarModel::ClearingAvatarModel(const QStringList &dir, QObject *parent) : BaseFileModel(parent)
{
    headers << tr("") << tr("Avatar") << tr("Size") << tr("Creation Date");

    setDirs(dir);
}

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

    int     i        = index.column();
    QString filename = filePass(index);
    switch (i) {
    case (0):
        if (role == Qt::CheckStateRole) {
            return isSelected(index) ? 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) {
            QPixmap pix = QPixmap(filename);
            if (pix.isNull())
                return QVariant();
            return QVariant(pix);
        }
        break;
    case (2):
        if (role == Qt::TextAlignmentRole) {
            return int(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant(fileSize(index));
        break;
    case (3):
        if (role == Qt::TextAlignmentRole) {
            return int(Qt::AlignRight | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole)
            return QVariant(fileDate(index));
        break;
    }
    return QVariant();
}

//----------------------------------------
//--ClearingOptionsModel------------------
//----------------------------------------
ClearingOptionsModel::ClearingOptionsModel(const QString &fileName, QObject *parent) :
    BaseModel(parent), fileName_(fileName)
{
    headers << tr("") << tr("Options") << tr("Values");

    parser_ = new OptionsParser(fileName_, this);
    options = parser_->getMissingNodesString();
}

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

QVariant ClearingOptionsModel::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 isSelected(index) ? 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::AlignLeft | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole) {
            return QVariant(options.at(index.row()));
        }
        break;
    case (2):
        if (role == Qt::TextAlignmentRole) {
            return int(Qt::AlignLeft | Qt::AlignVCenter);
        } else if (role == Qt::DisplayRole) {
            QDomNode node = parser_->nodeByString(options.at(index.row()));
            return QVariant(node.toElement().text());
        }
        break;
    }
    return QVariant();
}

void ClearingOptionsModel::deleteSelected()
{
    emit layoutAboutToBeChanged();

    setFile(fileName_);

    emit updateLabel(0);
}

void ClearingOptionsModel::reset()
{
    options.clear();
    BaseModel::reset();
}

void ClearingOptionsModel::setFile(const QString &fileName)
{
    emit layoutAboutToBeChanged();

    reset();
    fileName_ = fileName;
    delete parser_;
    parser_ = new OptionsParser(fileName_, this);
    options = parser_->getMissingNodesString();

    emit layoutChanged();
}

//----------------------------------------
//--ClearingProxyModel--------------------
//----------------------------------------
ClearingProxyModel::ClearingProxyModel(QObject *parent) : QSortFilterProxyModel(parent) { setDynamicSortFilter(true); }

bool ClearingProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &parent) const
{
    QModelIndex index1 = sourceModel()->index(sourceRow, 1, parent);
    QModelIndex index2 = sourceModel()->index(sourceRow, 2, parent);
    bool        b      = index1.data(Qt::DisplayRole).toString().contains(filterRegExp());
    bool        c      = index2.data(Qt::DisplayRole).toString().contains(filterRegExp());
    return (b | c);
}