1 | #include "vcaddressbook.h" |
---|
2 | |
---|
3 | #include <QMessageBox> |
---|
4 | #include <QDebug> |
---|
5 | |
---|
6 | #include "../completers/bettercompleter.h" |
---|
7 | |
---|
8 | #include "../VCEditContact/vceditcontact.h" |
---|
9 | #include "../VCEditGroup/vceditgroup.h" |
---|
10 | |
---|
11 | #include "../UIHelper.h" |
---|
12 | |
---|
13 | #include <scontact.h> |
---|
14 | #include <sgroup.h> |
---|
15 | #include <snumber.h> |
---|
16 | #include "../../business/BCContactManager.h" |
---|
17 | #include "../../business/BCGroupManager.h" |
---|
18 | |
---|
19 | #include <typeconvert.h> |
---|
20 | |
---|
21 | |
---|
22 | const int CONTACT_COL_INDEX_IMAGE = 0; |
---|
23 | const int CONTACT_COL_INDEX_NAME = 1; |
---|
24 | const int CONTACT_COL_INDEX_NUMBER = 2; |
---|
25 | |
---|
26 | const int GROUP_COL_INDEX_IMAGE = 0; |
---|
27 | const int GROUP_COL_INDEX_NAME = 1; |
---|
28 | const int GROUP_COL_INDEX_MEMBERS = 2; |
---|
29 | |
---|
30 | |
---|
31 | VCAddressBook::VCAddressBook(QWidget *parent) : QDialog(parent) { |
---|
32 | ui.setupUi(this); |
---|
33 | |
---|
34 | /* Contacts */ |
---|
35 | contactModel_ = new ContactTableModel(this); |
---|
36 | contactProxyModel_ = new BetterCompletionModel(this); |
---|
37 | contactProxyModel_->setFilterKeyColumn(-1); // Filter from all |
---|
38 | contactProxyModel_->setSourceModel(contactModel_); |
---|
39 | connect(ui.edtContactFilter, SIGNAL(textChanged(QString)), |
---|
40 | contactProxyModel_, SLOT(setFilterFixedString(QString))); |
---|
41 | |
---|
42 | ui.tblViewContacts->setModel(contactProxyModel_); |
---|
43 | ui.tblViewContacts->resizeRowsToContents(); |
---|
44 | ui.tblViewContacts->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); |
---|
45 | ui.tblViewContacts->horizontalHeader()->setResizeMode(ContactTableModel::ColAliases, QHeaderView::Stretch); |
---|
46 | ui.tblViewContacts->sortByColumn(ContactTableModel::ColName, Qt::AscendingOrder); |
---|
47 | ui.tblViewContacts->resizeColumnsToContents(); |
---|
48 | ui.tblViewContacts->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents); |
---|
49 | |
---|
50 | connect(ui.tblViewContacts, SIGNAL(doubleClicked(const QModelIndex&)), |
---|
51 | this, SLOT(on_btnEditContact_clicked())); |
---|
52 | connect(ui.tblViewContacts->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), |
---|
53 | this, SLOT(on_tblViewContacts_selectionChanged())); |
---|
54 | |
---|
55 | on_tblViewContacts_selectionChanged(); // Set initial state |
---|
56 | |
---|
57 | |
---|
58 | /* Groups */ |
---|
59 | groupModel_ = new GroupTableModel(this); |
---|
60 | groupProxyModel_ = new BetterCompletionModel(this); |
---|
61 | groupProxyModel_->setFilterKeyColumn(-1); // Filter from all |
---|
62 | groupProxyModel_->setSourceModel(groupModel_); |
---|
63 | connect(ui.edtGroupFilter, SIGNAL(textChanged(QString)), |
---|
64 | groupProxyModel_, SLOT(setFilterFixedString(QString))); |
---|
65 | |
---|
66 | ui.tblViewGroups->setModel(groupProxyModel_); |
---|
67 | ui.tblViewGroups->resizeRowsToContents(); |
---|
68 | ui.tblViewGroups->horizontalHeader()->setResizeMode(QHeaderView::ResizeToContents); |
---|
69 | ui.tblViewGroups->horizontalHeader()->setResizeMode(GroupTableModel::ColMembers, QHeaderView::Stretch); |
---|
70 | ui.tblViewGroups->sortByColumn(ContactTableModel::ColName, Qt::AscendingOrder); |
---|
71 | ui.tblViewGroups->resizeColumnsToContents(); |
---|
72 | ui.tblViewGroups->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents); |
---|
73 | |
---|
74 | connect(ui.tblViewGroups, SIGNAL(doubleClicked(const QModelIndex&)), |
---|
75 | this, SLOT(on_btnEditGroup_clicked())); |
---|
76 | connect(ui.tblViewGroups->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), |
---|
77 | this, SLOT(on_tblViewGroups_selectionChanged())); |
---|
78 | |
---|
79 | on_tblViewGroups_selectionChanged(); // Set initial state |
---|
80 | } |
---|
81 | |
---|
82 | /*QSet<IContact*> VCAddressBook::getFilteredContactList(){ |
---|
83 | if (actualContactFilterStr_ == ""){ |
---|
84 | return contactList_; |
---|
85 | } |
---|
86 | |
---|
87 | QSet<IContact*> contactList; |
---|
88 | foreach (IContact* contact, contactList_) { |
---|
89 | bool containsFilter = false; |
---|
90 | |
---|
91 | containsFilter = containsFilter || contact->name().contains(actualContactFilterStr_, Qt::CaseInsensitive); |
---|
92 | |
---|
93 | SNumber number = contact->number(); |
---|
94 | QString numberStr; |
---|
95 | // +41791234567 |
---|
96 | numberStr = number.toString("+CCAAAu"); |
---|
97 | numberStr.remove(" "); |
---|
98 | containsFilter = containsFilter || numberStr.contains(actualContactFilterStr_, Qt::CaseInsensitive); |
---|
99 | // +410791234567 |
---|
100 | numberStr = number.toString("+ccaaau"); |
---|
101 | containsFilter = containsFilter || numberStr.contains(actualContactFilterStr_, Qt::CaseInsensitive); |
---|
102 | |
---|
103 | QStringList aliases = contact->aliases(); |
---|
104 | for (int x = 0; x < aliases.size() && !containsFilter; ++x){ |
---|
105 | QString alias = aliases.at(x); |
---|
106 | alias.remove(" "); |
---|
107 | containsFilter = containsFilter || alias.contains(actualContactFilterStr_, Qt::CaseInsensitive); |
---|
108 | } |
---|
109 | |
---|
110 | if (containsFilter){ |
---|
111 | contactList.insert(contact); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | return contactList; |
---|
116 | }*/ |
---|
117 | |
---|
118 | /* Contacts */ |
---|
119 | IContact* VCAddressBook::getContactFromRow(int rowId) const { |
---|
120 | QModelIndex idx = contactProxyModel_->mapToSource(contactProxyModel_->index(rowId, 0)); |
---|
121 | return contactModel_->dataObject(idx); |
---|
122 | } |
---|
123 | |
---|
124 | IContact* VCAddressBook::getSelectedContact() const { |
---|
125 | QItemSelectionModel* ism = ui.tblViewContacts->selectionModel(); |
---|
126 | if (!ism->hasSelection()) |
---|
127 | return NULL; |
---|
128 | |
---|
129 | int row = ism->selectedRows().first().row(); |
---|
130 | return getContactFromRow(row); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | void VCAddressBook::editContact(IContact* contact){ |
---|
135 | VCEditContact* ec = new VCEditContact(contact, this); |
---|
136 | if (ec->exec() == QDialog::Accepted){ // Wait for return |
---|
137 | BCContactManager::instance()->saveContact(contact); |
---|
138 | } |
---|
139 | } |
---|
140 | |
---|
141 | |
---|
142 | /* Groups */ |
---|
143 | IGroup* VCAddressBook::getGroupFromRow(int rowId) const { |
---|
144 | QModelIndex idx = groupProxyModel_->mapToSource(groupProxyModel_->index(rowId, 0)); |
---|
145 | return groupModel_->dataObject(idx); |
---|
146 | } |
---|
147 | |
---|
148 | IGroup* VCAddressBook::getSelectedGroup() const { |
---|
149 | QItemSelectionModel* ism = ui.tblViewGroups->selectionModel(); |
---|
150 | if (!ism->hasSelection()) |
---|
151 | return NULL; |
---|
152 | |
---|
153 | int row = ism->selectedRows().first().row(); |
---|
154 | return getGroupFromRow(row); |
---|
155 | } |
---|
156 | |
---|
157 | |
---|
158 | void VCAddressBook::editGroup(IGroup* group){ |
---|
159 | VCEditGroup* eg = new VCEditGroup(group, this); |
---|
160 | if (eg->exec() == QDialog::Accepted){ // Wait for return |
---|
161 | BCGroupManager::instance()->saveGroup(group); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | // Slots... |
---|
167 | |
---|
168 | /* Contacts */ |
---|
169 | void VCAddressBook::on_btnAddContact_clicked(){ |
---|
170 | editContact(new SContact()); |
---|
171 | } |
---|
172 | |
---|
173 | void VCAddressBook::on_btnEditContact_clicked(){ |
---|
174 | IContact* contact = getSelectedContact(); |
---|
175 | if (contact == NULL) |
---|
176 | return; |
---|
177 | |
---|
178 | editContact(contact); |
---|
179 | } |
---|
180 | |
---|
181 | void VCAddressBook::on_btnRemoveContact_clicked(){ |
---|
182 | IContact* contact = getSelectedContact(); |
---|
183 | if (contact == NULL) |
---|
184 | return; |
---|
185 | |
---|
186 | if (QMessageBox::Yes == QMessageBox::warning(this, tr("SMSSender"), |
---|
187 | tr("Do you really want to remove this contact?"), |
---|
188 | QMessageBox::Yes, |
---|
189 | QMessageBox::No | QMessageBox::Default | QMessageBox::Escape)){ |
---|
190 | |
---|
191 | //TODO: do this eventually over the model... |
---|
192 | BCContactManager::instance()->removeContact(contact->id()); |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | void VCAddressBook::on_tblViewContacts_selectionChanged() { |
---|
197 | ui.btnEditContact->setEnabled(ui.tblViewContacts->selectionModel()->hasSelection()); |
---|
198 | ui.btnRemoveContact->setEnabled(ui.tblViewContacts->selectionModel()->hasSelection()); |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | /* Groups */ |
---|
203 | |
---|
204 | void VCAddressBook::on_btnAddGroup_clicked(){ |
---|
205 | editGroup(new SGroup()); |
---|
206 | } |
---|
207 | |
---|
208 | void VCAddressBook::on_btnEditGroup_clicked(){ |
---|
209 | IGroup* group = getSelectedGroup(); |
---|
210 | if (group == NULL) |
---|
211 | return; |
---|
212 | |
---|
213 | editGroup(group); |
---|
214 | } |
---|
215 | |
---|
216 | void VCAddressBook::on_btnRemoveGroup_clicked(){ |
---|
217 | IGroup* group = getSelectedGroup(); |
---|
218 | if (group == NULL) |
---|
219 | return; |
---|
220 | |
---|
221 | if (QMessageBox::Yes == QMessageBox::warning(this, tr("SMSSender"), |
---|
222 | tr("Do you really want to remove this group?"), |
---|
223 | QMessageBox::Yes, |
---|
224 | QMessageBox::No | QMessageBox::Default | QMessageBox::Escape)){ |
---|
225 | |
---|
226 | //TODO: do this eventually over the model... |
---|
227 | BCGroupManager::instance()->removeGroup(group->id()); |
---|
228 | } |
---|
229 | } |
---|
230 | |
---|
231 | void VCAddressBook::on_tblViewGroups_selectionChanged() { |
---|
232 | ui.btnEditGroup->setEnabled(ui.tblViewGroups->selectionModel()->hasSelection()); |
---|
233 | ui.btnRemoveGroup->setEnabled(ui.tblViewGroups->selectionModel()->hasSelection()); |
---|
234 | } |
---|