/
draftsmodel.cpp
115 lines (94 loc) · 2.87 KB
/
draftsmodel.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
/******************************************************************************
**
** This file is part of libcommhistory.
**
** Copyright (C) 2014 Jolla Ltd.
** Contact: John Brooks <john.brooks@jolla.com>
**
** This library is free software; you can redistribute it and/or modify it
** under the terms of the GNU Lesser General Public License version 2.1 as
** published by the Free Software Foundation.
**
** This library 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 Lesser General Public
** License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this library; if not, write to the Free Software Foundation, Inc.,
** 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**
******************************************************************************/
#include "draftsmodel_p.h"
#include "constants.h"
#include "databaseio_p.h"
#include "commhistorydatabase.h"
#include "debug.h"
#include <QSqlQuery>
namespace CommHistory {
DraftsModelPrivate::DraftsModelPrivate(DraftsModel *model)
: EventModelPrivate(model)
{
}
bool DraftsModelPrivate::acceptsEvent(const Event &event) const
{
if (!event.isDraft())
return false;
if (!filterGroups.isEmpty() && !filterGroups.contains(event.groupId()))
return false;
return true;
}
DraftsModel::DraftsModel(QObject *parent)
: EventModel(*new DraftsModelPrivate(this), parent)
{
}
DraftsModel::~DraftsModel()
{
}
QList<int> DraftsModel::filterGroups() const
{
Q_D(const DraftsModel);
return d->filterGroups.values();
}
void DraftsModel::setFilterGroups(const QList<int> &list)
{
Q_D(DraftsModel);
QSet<int> groupIds = list.toSet();
if (groupIds == d->filterGroups)
return;
d->filterGroups = groupIds;
emit filterGroupsChanged();
}
void DraftsModel::setFilterGroup(int groupId)
{
setFilterGroups(QList<int>() << groupId);
}
void DraftsModel::clearFilterGroups()
{
setFilterGroups(QList<int>());
}
bool DraftsModel::getEvents()
{
Q_D(DraftsModel);
beginResetModel();
d->clearEvents();
endResetModel();
// As in ConversationModel, a UNION ALL is used to get better
// optimization out of sqlite
QList<int> groups = d->filterGroups.toList();
int unionCount = 0;
QString q;
do {
if (unionCount)
q += "UNION ALL ";
q += DatabaseIOPrivate::eventQueryBase();
q += "WHERE Events.isDraft = 1 ";
if (unionCount < groups.size())
q += "AND Events.groupId = " + QString::number(groups[unionCount]) + " ";
unionCount++;
} while (unionCount < groups.size());
q += "ORDER BY Events.endTime DESC, Events.id DESC";
QSqlQuery query = d->prepareQuery(q);
return d->executeQuery(query);
}
}