source: SMSSender/src/ui/VCMain/vcmain.cpp @ 43:10dd65d71f7f

3.0
Last change on this file since 43:10dd65d71f7f was 43:10dd65d71f7f, checked in by Sämy Zehnder <saemy.zehnder@…>, 12 years ago
  • Did a lot of work...
File size: 12.4 KB
Line 
1#include "vcmain.h"
2
3#include <QSortFilterProxyModel>
4#include <QMessageBox>
5
6#include <QCompleter>
7#include <QDebug>
8
9#include <algorithm>
10
11#include "../../business/BCAccountManager.h"
12#include "../../business/BCContactManager.h"
13#include "../../business/BCGroupManager.h"
14
15#include <typeconvert.h>
16
17#include "../UIHelper.h"
18
19#include "../VCAccountList/vcaccountlist.h"
20#include "../VCAddressBook/vcaddressbook.h"
21#include "../VCSettings/vcsettings.h"
22
23#include "../completers/bettercompleter.h"
24#include "../models/aliascompletionmodel.h"
25
26#include "../delegates/aligndelegate.h"
27
28
29VCMain::VCMain(QWidget *parent): QMainWindow(parent){
30        ui.setupUi(this);
31
32        //contactListModel = new QStringListModel;
33        //contactListModel = new AliasModel;
34
35        contactGroupListModel_ = new ContactGroupListModel(this);
36        QSortFilterProxyModel* sortModel = new QSortFilterProxyModel(this);
37    sortModel->setSourceModel(contactGroupListModel_);
38    ui.lstContacts->setModel(sortModel);
39
40        QSortFilterProxyModel* completionModel = new AliasCompletionModel(this);
41    completionModel->setSourceModel(contactGroupListModel_);
42    BetterCompleter* aliasCompleter = new BetterCompleter(completionModel, this);
43    aliasCompleter->setCompletionMode(QCompleter::PopupCompletion);
44    aliasCompleter->setCaseSensitivity(Qt::CaseInsensitive);
45
46    ui.lstContacts->setCompleter(aliasCompleter);
47    ui.lstContacts->completer()->popup()->setIconSize(QSize(24, 24));
48    //ui.lstContacts->setEditable(true);
49
50    /*connect(ui.lstContacts->lineEdit(), SIGNAL(textChanged()),
51            aliasCompleter, SLOT());*/
52    connect(ui.lstContacts->lineEdit(), SIGNAL(returnPressed()),
53            this, SLOT(on_btnAddRecipient_clicked()));
54
55    connect(aliasCompleter, SIGNAL(highlighted(const QString&)),
56            this, SLOT(on_lstContacts_completer_changed(const QString&)));
57
58    connect(BCAccountLoadManager::instance(), SIGNAL(accountLoadStateChanged(IAccount*, AccountLoadState)),
59            this, SLOT(on_accountLoadStateChanged(IAccount*, AccountLoadState)));
60    connect(BCAccountLoadManager::instance(), SIGNAL(accountLoaded(IAccount*)),
61            this, SLOT(on_accountLoaded(IAccount*)));
62    connect(BCAccountLoadManager::instance(), SIGNAL(accountLoadingFailed(IAccount*, EException*)),
63            this, SLOT(on_accountLoadingFailed(IAccount*, EException*)));
64
65
66        init();
67
68        ui.lstContacts->setCurrentIndex(-1);
69}
70
71void VCMain::init(){
72    //refillAccountList();
73    refillContactList();
74
75    // First "signal" to initialize some slots
76    on_treeRecipients_itemSelectionChanged();
77}
78
79
80/* Contacts & Recipients... */
81
82void VCMain::refillContactList(){
83    // TODO: Make usage of aliases...
84/*
85    contactListModel->removeRows(0, contactListModel->rowCount());
86    //ui.lstContacts->clear();
87    contactStrToContact.clear();
88    groupStrToGroup.clear();
89
90    QPixmap blankIcon = QPixmap(":/ico/blank.png");
91
92    foreach (IContact* contact, BCContactManager::instance()->getContactList()) {
93        QPixmap icon = QPixmap::fromImage(contact->image());
94        if (icon.isNull()){
95            icon = blankIcon;
96        }
97        QString contactStr = contact->name();
98        QString aliasStr;
99        QStringListIterator i(contact->aliases());
100        while (i.hasNext()) {
101            aliasStr += i.next() + ", ";
102        }
103        aliasStr.remove(QRegExp(", $"));
104        if (aliasStr != "") {
105            contactStr += " (" + aliasStr + ")";
106        }
107
108        contactListModel->insertRow(contactListModel->rowCount());
109        contactListModel->setData(contactListModel->index(contactListModel->rowCount() - 1, 0), QIcon(icon), Qt::DecorationRole);
110        contactListModel->setData(contactListModel->index(contactListModel->rowCount() - 1, 0), contactStr, Qt::DisplayRole);
111
112        //ui.lstContacts->addItem(QIcon(icon), contactStr);
113        contactStrToContact.insert(contactStr, contact);
114    }
115
116    foreach (IGroup* group, BCGroupManager::instance()->getGroupList()) {
117        QPixmap icon = QPixmap::fromImage(group->image());
118        if (icon.isNull()){
119            icon = blankIcon;
120        }
121
122        QString groupStr = "<" + group->name() + ">";
123
124        contactListModel->insertRow(contactListModel->rowCount());
125        contactListModel->setData(contactListModel->index(contactListModel->rowCount() - 1, 0), QIcon(icon), Qt::DecorationRole);
126        contactListModel->setData(contactListModel->index(contactListModel->rowCount() - 1, 0), groupStr, Qt::DisplayRole);
127//        ui.lstContacts->addItem(QIcon(icon), "<" + group->name() + ">");
128        groupStrToGroup.insert(groupStr, group);
129    }
130    ui.lstContacts->model()->sort(0);
131
132    //ui.lstContacts->insertItem(0, blankIcon, ""); // Empty item at first position to prevent default recipient selection
133    ui.lstContacts->setCurrentIndex(-1); */
134}
135
136
137bool VCMain::isContactNode(const QTreeWidgetItem* node){
138    return nodeToContact.contains(const_cast<QTreeWidgetItem*>(node));
139}
140bool VCMain::isGroupNode(const QTreeWidgetItem* node){
141    return nodeToGroup.contains(const_cast<QTreeWidgetItem*>(node));
142}
143
144IContact* VCMain::getContactOfNode(const QTreeWidgetItem* node){
145    if (!isContactNode(node)){
146        return (IContact*)0;
147    } else {
148        return nodeToContact[const_cast<QTreeWidgetItem*>(node)];
149    }
150}
151IGroup* VCMain::getGroupOfNode(const QTreeWidgetItem* node){
152    if (!isGroupNode(node)){
153        return (IGroup*)0;
154    } else {
155        return nodeToGroup[const_cast<QTreeWidgetItem*>(node)];
156    }
157}
158
159bool VCMain::isNodeContactOrHasContact(const QTreeWidgetItem* node, IContact* contact){
160    if (isContactNode(node)){
161        IContact* contact_ = getContactOfNode(node);
162        if (contact->id() == contact_->id()){
163            return true;
164        }
165    } else {
166        // group...
167        for (int x = 0; x < node->childCount(); ++x){
168            if (isNodeContactOrHasContact(node->child(x), contact)){
169                return true;
170            }
171        }
172    }
173    return false;
174
175}
176
177bool VCMain::isContactAlreadyRecipient(IContact* contact){
178    return getRecipientContacts(true).contains(contact);
179}
180bool VCMain::isGroupAlreadyRecipient(IGroup* group){
181    return getRecipientGroups().contains(group);
182}
183
184QSet<IContact*> VCMain::getRecipientContacts(bool withGroupContacts /* = true */, QTreeWidgetItem* node /* = NULL */) {
185    QSet<IContact*> result;
186
187    if (node == NULL) {
188        for (int i = 0; i < ui.treeRecipients->topLevelItemCount(); ++i) {
189            result.unite(getRecipientContacts(withGroupContacts, ui.treeRecipients->topLevelItem(i)));
190        }
191    }
192
193    if (isContactNode(node)) {
194        result.insert(getContactOfNode(node));
195    } else if (withGroupContacts && isGroupNode(node)) {
196        for (int i = 0; i < node->childCount(); ++i) {
197            result.unite(getRecipientContacts(withGroupContacts, node->child(i)));
198        }
199    }
200
201    return result;
202}
203
204QSet<IGroup*> VCMain::getRecipientGroups() {
205    QSet<IGroup*> result;
206    for (int x = 0; x < ui.treeRecipients->topLevelItemCount(); ++x){
207        if (isGroupNode(ui.treeRecipients->topLevelItem(x))){
208            result.insert(getGroupOfNode(ui.treeRecipients->topLevelItem(x)));
209        }
210    }
211    return result;
212}
213
214
215void VCMain::addRecipient(IContact* contact, QTreeWidgetItem* parent /* = NULL */){
216    if ((contact == NULL) || isContactAlreadyRecipient(contact)){
217        return;
218    }
219
220    contactGroupListModel_->addFilteredContact(contact);
221
222    QTreeWidgetItem* node;
223    if (parent) {
224        node = new QTreeWidgetItem(parent); // parent->addChild is done in here
225    } else {
226        node = new QTreeWidgetItem(ui.treeRecipients);
227        ui.treeRecipients->addTopLevelItem(node);
228    }
229
230    node->setText(0, contact->name());
231    node->setIcon(0, QPixmap::fromImage(contact->image()));
232    nodeToContact[node] = contact;
233}
234
235void VCMain::addRecipient(IGroup* group){
236    if ((group == NULL) || isGroupAlreadyRecipient(group)){
237        return;
238    }
239
240    QTreeWidgetItem* node = new QTreeWidgetItem(ui.treeRecipients);
241    ui.treeRecipients->addTopLevelItem(node);
242
243    node->setText(0, group->name());
244    node->setIcon(0, QPixmap::fromImage(group->image()));
245    nodeToGroup[node] = group;
246
247    foreach (IContact* contact, group->contacts()) {
248        addRecipient(contact, node);
249    }
250}
251
252void VCMain::removeNode(QTreeWidgetItem* node){
253    QTreeWidgetItem* parent = node->parent();
254    if (parent){
255        parent->removeChild(node);
256
257        if (parent->childCount() == 0){
258            removeRecipient(parent);
259        }
260    } else {
261        ui.treeRecipients->takeTopLevelItem(ui.treeRecipients->indexOfTopLevelItem(node));
262    }
263}
264void VCMain::removeRecipient(QTreeWidgetItem* node){
265    if (isContactNode(node)){
266        IContact* contact = nodeToContact[node];
267        contactGroupListModel_->removeFilteredContact(contact);
268
269        removeNode(node);
270        nodeToContact.remove(node);
271    } else { // Group
272        for (int x = 0; x < node->childCount(); ++x){
273            removeRecipient(node->child(x)); // Remove all contacts of this group
274        }
275
276        removeNode(node);
277        nodeToGroup.remove(node);
278    }
279}
280
281
282void VCMain::reloadAccountList() {
283    accountStrToAccount.clear();
284    QComboBox* accLst = ui.lstAccounts;
285
286    QString selectedAccount = accLst->currentText();
287
288    accLst->clear();
289    foreach(IAccount* account, BCAccountManager::instance()->getAccountListByLoadState(Loaded)) {
290        accLst->addItem(account->name());
291        accountStrToAccount.insert(account->name(), account);
292    }
293
294    int index = accLst->findText(selectedAccount);
295    index = std::max(index, 0);
296    accLst->setCurrentIndex(index);
297}
298
299void VCMain::enableSendBtnIfAllFilled() {
300    ui.btnSend->setEnabled((ui.txtMessage->toPlainText().trimmed() != "")
301                           && (ui.treeRecipients->topLevelItemCount() > 0)
302                           && (ui.lstAccounts->currentIndex() > -1));
303}
304
305
306void VCMain::clearMessage() {
307    ui.txtMessage->clear();
308    ui.treeRecipients->clear();
309    ui.txtMessage->setFocus();
310}
311
312void VCMain::sendMessage() {
313    IAccount* account          = accountStrToAccount[ui.lstAccounts->currentText()];
314    QString message            = ui.txtMessage->toPlainText().trimmed();
315    QSet<IContact*> recipients = getRecipientContacts(true);
316
317    account->sendSMS(message, recipients);
318    QMessageBox::information(this, tr("SMS sent"), tr("Your sms has successfully been sent."), QMessageBox::Ok, QMessageBox::Ok);
319}
320
321
322/* SLOTS... */
323
324void VCMain::on_btnSend_clicked() {
325    sendMessage();
326}
327
328void VCMain::on_treeRecipients_itemSelectionChanged(){
329    ui.btnRemoveRecipient->setEnabled(ui.treeRecipients->selectedItems().count() > 0);
330}
331
332void VCMain::on_lstContacts_completer_changed(const QString& text) {
333    qDebug() << text << ui.lstContacts->findText(text);
334    ui.lstContacts->setCurrentIndex(ui.lstContacts->findText(text));
335}
336
337
338void VCMain:: on_btnAddRecipient_clicked(){
339    QModelIndex idx = contactGroupListModel_->index(ui.lstContacts->currentIndex());
340    if (idx.isValid()) {
341        IInterface* recipient = contactGroupListModel_->dataObject(idx);
342        addRecipient(dynamic_cast<IContact*>(recipient));
343        addRecipient(dynamic_cast<IGroup*>(recipient));
344    } else {
345        // TODO: Add new numbers to the address book
346    }
347    ui.lstContacts->setCurrentIndex(-1);
348    ui.lstContacts->clearEditText();
349    enableSendBtnIfAllFilled();
350}
351
352void VCMain::on_btnRemoveRecipient_clicked(){
353    QListIterator<QTreeWidgetItem*> i(ui.treeRecipients->selectedItems());
354    while (i.hasNext()){
355        removeRecipient(i.next());
356    }
357    enableSendBtnIfAllFilled();
358}
359
360
361/* File menu */
362
363void VCMain::on_actionNewMessage_activated() {
364    clearMessage();
365}
366
367/* Edit menu */
368
369void VCMain::on_actionAccounts_activated() {
370    VCAccountList* cl = new VCAccountList(this);
371    cl->show();
372}
373
374void VCMain::on_actionAddressBook_activated(){
375    VCAddressBook* cl = new VCAddressBook(this);
376    cl->show();
377}
378
379void VCMain::on_actionPreferences_activated(){
380        VCSettings* s = new VCSettings(this);
381        s->show();
382}
383
384
385/* Help menu */
386
387void VCMain::on_actionAbout_activated(){
388        //
389}
390
391
392void VCMain::on_txtMessage_textChanged() {
393    enableSendBtnIfAllFilled();
394}
395void VCMain::on_selAccounts_currentIndexChanged(int index) {
396    enableSendBtnIfAllFilled();
397}
398
399
400/* Account loading */
401void VCMain::on_accountLoadStateChanged(IAccount* account, AccountLoadState state) {
402
403}
404
405void VCMain::on_accountLoaded(IAccount* account) {
406    reloadAccountList();
407}
408
409void VCMain::on_accountLoadingFailed(IAccount* account, EException* exception) {
410    QMessageBox::critical(this, tr("Error occured"), tr("An unknown error has occured!") + "\n\n" + QString(exception->what()), QMessageBox::Ok, QMessageBox::Ok);
411    reloadAccountList();
412}
413
Note: See TracBrowser for help on using the repository browser.