-
Notifications
You must be signed in to change notification settings - Fork 0
/
accountmodel.cpp
201 lines (167 loc) · 4.68 KB
/
accountmodel.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
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
/************************************************
*
* Copyright © 2009-2010 Florian Staudacher
*
*
* This file is part of FSugar.
*
* FSugar is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FSugar 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 FSugar. If not, see <http://www.gnu.org/licenses/>.
*
***********************************************/
#include <QtGui>
#include "account.h"
#include "accountmodel.h"
#include "sugarcrm.h"
bool compareAccountsLessThan(const Account *a1, const Account *a2)
{
return (a1->name < a2->name);
}
AccountModel *AccountModel::instance = NULL;
AccountModel::AccountModel(QObject *parent) :
QAbstractListModel(parent)
{
QHash<int, QByteArray> roles;
roles.insert(16, "name");
roles.insert(32, "address");
roles.insert(64, "address2");
roles.insert(128, "telephone");
setRoleNames(roles);
crm = SugarCrm::getInstance();
connect(crm, SIGNAL(dataAvailable(QString)),
this, SLOT(processData(QString)));
}
AccountModel *AccountModel::getInstance()
{
if(AccountModel::instance == NULL) {
AccountModel::instance = new AccountModel();
}
return AccountModel::instance;
}
int AccountModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
if(!accounts.isEmpty()) {
return accounts.size();
}
return 0;
}
int AccountModel::columnCount(const QModelIndex &parent)
{
Q_UNUSED(parent)
return 1;
}
QVariant AccountModel::data(const QModelIndex &index, int role) const
{
if(accounts.isEmpty()) {
return QVariant();
}
Account *acc = accounts[index.row()];
switch(role) {
case 16:
case Qt::DisplayRole:
return acc->name.simplified();
case 32:
return acc->address_street.simplified();
case 64:
return acc->address_postalcode.append(", ").append(acc->address_city).simplified();
case 128:
return acc->phone_office.simplified();
case 256:
return acc->category;
}
return QVariant();
}
Qt::ItemFlags AccountModel::flags(const QModelIndex &index)
{
qDebug() << "flags requested:" << index.row();
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
bool AccountModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
qDebug() << "trying to set data";
if(index.row() < accounts.size() && index.row() >= 0) {
QString newVal(value.toString());
switch(role) {
case 16:
accounts[index.row()]->name = newVal;
return true;
/* case 32:
accounts.at(index.row()).address_street = newVal;
return true;
case 64:
accounts.at(index.row()).address_postalcode = newVal;
return true;
case 128:
accounts.at(index.row()).name = newVal;
return true;*/
}
emit dataChanged(index, index);
return true;
}
return false;
}
Account* AccountModel::getAccount(const int num)
{
return accounts[num];
}
void AccountModel::fetchData()
{
crm->getAccountList();
}
void AccountModel::processData(const QString _id)
{
if(!_id.isEmpty()) return;
if (crm->entries->size() < 1) return;
accounts.clear();
QMap<QString, QMap<QString, QString> >::const_iterator i = crm->entries->begin();
while (i != crm->entries->end()) {
Account *tmp = new Account();
tmp->id = i.value().value("id");
tmp->name = i.value().value("name");
tmp->address_street = i.value().value("billing_address_street");
tmp->address_city = i.value().value("billing_address_city");
tmp->address_postalcode = i.value().value("billing_address_postalcode");
tmp->address_country = i.value().value("billing_address_country");
tmp->phone_office = i.value().value("phone_office");
tmp->phone_fax = i.value().value("phone_fax");
tmp->phone_alternate = i.value().value("phone_alternate");
tmp->email = i.value().value("email1");
tmp->website = i.value().value("website");
tmp->description = i.value().value("description");
tmp->category = i.value().value("kategorie_c");
accounts.append(tmp);
i++;
}
sortData();
reset();
emit dataReady();
}
Account* AccountModel::newAccount()
{
Account *_acc = new Account();
accounts.append(_acc);
connect(_acc, SIGNAL(saved()),
this, SLOT(processNewAccount()));
return _acc;
}
void AccountModel::processNewAccount()
{
sortData();
reset();
}
void AccountModel::sortData()
{
qSort(accounts.begin(), accounts.end(), compareAccountsLessThan);
}