Statistics
| Branch: | Tag: | Revision:

root / kadu-core / gui / widgets / account-buddy-list-widget.cpp @ f38188d2

History | View | Annotate | Download (7.8 kB)

1
/*
2
 * %kadu copyright begin%
3
 * Copyright 2009 Bartlomiej Zimon (uzi18@o2.pl)
4
 * Copyright 2009, 2010 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
5
 * Copyright 2009 Michał Podsiadlik (michal@kadu.net)
6
 * Copyright 2009 Piotr Galiszewski (piotrgaliszewski@gmail.com)
7
 * %kadu copyright end%
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License as
11
 * published by the Free Software Foundation; either version 2 of
12
 * the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include <QtGui/QHBoxLayout>
24
#include <QtGui/QVBoxLayout>
25
26
#include "buddies/buddy-manager.h"
27
#include "buddies/buddy-shared.h"
28
#include "contacts/contact.h"
29
#include "contacts/contact-shared.h"
30
#include "contacts/contact-details.h"
31
#include "contacts/contact-manager.h"
32
#include "buddies/model/buddies-model.h"
33
#include "buddies/model/buddies-model-proxy.h"
34
#include "buddies/filter/account-buddy-filter.h"
35
#include "gui/windows/message-dialog.h"
36
37
#include "debug.h"
38
#include "protocols/protocol.h"
39
#include "protocols/services/contact-list-service.h"
40
41
#include "account-buddy-list-widget.h"
42
43
AccountBuddyListWidget::AccountBuddyListWidget(Account account, QWidget *parent) : QWidget(parent), CurrentAccount(account)
44
{
45
        QVBoxLayout *layout = new QVBoxLayout(this);
46
        layout->setContentsMargins(0, 0, 0, 0);
47
        layout->setSpacing(5);
48
49
        BuddiesWidget = new BuddiesListView(0, this);
50
        BuddiesModelProxy *model = new BuddiesModelProxy(this);
51
        model->setSourceModel(new BuddiesModel(BuddyManager::instance(), this));
52
        BuddiesWidget->setModel(model);
53
        BuddiesWidget->setMinimumSize(QSize(30, 30));
54
55
        QWidget *buttons = new QWidget(this);
56
        QHBoxLayout *buttonsLayout = new QHBoxLayout(buttons);
57
        buttonsLayout->setContentsMargins(0, 0, 0, 0);
58
        buttonsLayout->setSpacing(5);
59
60
        ImportButton = new QPushButton(tr("Import contacts"), buttons);
61
        //importButton->setMinimumWidth(ContactsWidget->minimumWidth());
62
        connect(ImportButton, SIGNAL(clicked()), this, SLOT(startImportTransfer()));
63
        buttonsLayout->addWidget(ImportButton);        
64
65
        ExportButton = new QPushButton(tr("Export contacts"), buttons);
66
        //exportButton->setMinimumWidth(ContactsWidget->minimumWidth());
67
        connect(ExportButton, SIGNAL(clicked()), this, SLOT(startExportTransfer()));
68
        buttonsLayout->addWidget(ExportButton);
69
70
        layout->addWidget(BuddiesWidget);
71
        layout->addWidget(buttons);
72
73
        AccountBuddyFilter *filter = new AccountBuddyFilter(CurrentAccount, this);
74
        filter->setEnabled(true);
75
        BuddiesWidget->addFilter(filter);
76
77
        ContactListService *manager = CurrentAccount.protocolHandler()->contactListService();
78
        if (!manager)
79
        {
80
                ImportButton->setEnabled(false);
81
                ExportButton->setEnabled(false);
82
                return;
83
        }
84
85
        connect(manager, SIGNAL(contactListExported(bool)), this, SLOT(buddiesListExported(bool)));
86
        connect(manager, SIGNAL(contactListImported(bool, BuddyList)),
87
                this, SLOT(buddiesListImported(bool, BuddyList)));
88
}
89
90
void AccountBuddyListWidget::startImportTransfer()
91
{
92
        kdebugf();
93
94
        if (!CurrentAccount.protocolHandler()->isConnected())
95
        {
96
                MessageDialog::msg(tr("Cannot import user list from server in offline mode"), false, "Critical", this);
97
                return;
98
        }
99
100
        ContactListService *service = CurrentAccount.protocolHandler()->contactListService();
101
        if (!service)
102
                return;
103
        ImportButton->setEnabled(false);
104
        service->importContactList();
105
106
        kdebugf2();
107
}
108
109
void AccountBuddyListWidget::startExportTransfer()
110
{
111
        kdebugf();
112
113
        if (!CurrentAccount.protocolHandler()->isConnected())
114
        {
115
                MessageDialog::msg(tr("Cannot export user list to server in offline mode"), false, "Critical", this);
116
                kdebugf2();
117
                return;
118
        }
119
120
        ContactListService *service = CurrentAccount.protocolHandler()->contactListService();
121
        if (!service)
122
                return;
123
124
        ExportButton->setEnabled(false);
125
        Clear = false;
126
        service->exportContactList();
127
128
        kdebugf2();
129
}
130
131
void AccountBuddyListWidget::buddiesListImported(bool ok, BuddyList buddies)
132
{
133
        kdebugf();
134
135
        ImportButton->setEnabled(true);
136
        if (!ok)
137
                return;
138
139
        QList<Contact> unImportedContacts;// = ContactManager::instance()->contacts(CurrentAccount);
140
141
        foreach (const Buddy &onebuddy, buddies)
142
        {
143
                Buddy buddy;
144
                QList<Contact> oneBuddyContacts = onebuddy.contacts(CurrentAccount);
145
146
                if (oneBuddyContacts.count() > 0)
147
                {
148
                        foreach (const Contact &contact, oneBuddyContacts)
149
                        {
150
                                Contact contactOnList = ContactManager::instance()->byId(CurrentAccount, contact.id());
151
                                if (contactOnList.isNull()) // not on list add this one as new
152
                                {
153
                                        buddy = Buddy::create();
154
                                        // move contact to buddy
155
                                        ContactManager::instance()->addItem(contact);
156
                                        kdebugmf(KDEBUG_FUNCTION_START, "\nuuid add: '%s' %s\n",
157
                                                 qPrintable(contactOnList.uuid().toString()), qPrintable(buddy.display()));
158
                                        contact.setOwnerBuddy(buddy);
159
                                }
160
                                else // already on list
161
                                {
162
                                        // found contact so use his buddy
163
                                        //kdebugmf(KDEBUG_FUNCTION_START, "\nuuid before: '%s'\n", qPrintable(contactOnList.ownerBuddy().uuid().toString()));
164
                                        buddy = contactOnList.ownerBuddy();
165
                                        kdebugmf(KDEBUG_FUNCTION_START, "\nuuid owner: '%s' %s\n",
166
                                                 qPrintable(contactOnList.uuid().toString()), qPrintable(buddy.display()));
167
                                        //unImportedContacts.removeOne(contactOnList);
168
                                }
169
                        }
170
                }
171
                else
172
                {
173
                        // THIS WORKS NICE
174
                        // find one by display, but what if display().isEmpty()?
175
                        buddy = BuddyManager::instance()->byDisplay(onebuddy.display(), ActionCreateAndAdd);
176
                        if (buddy.isNull())
177
                        {
178
                                // not found so add new one
179
                                buddy = Buddy::create();
180
                                // TODO: 0.6.6
181
                                buddy.setDisplay(buddy.uuid().toString());
182
                        }
183
                }
184
185
                // TODO 0.6.6: update rest data, consider to add some logic here
186
                // TODO 0.6.6: consider to find contact by some data if no contacts inside buddy
187
                buddy.setFirstName(onebuddy.firstName());
188
                buddy.setLastName(onebuddy.lastName());
189
                buddy.setNickName(onebuddy.nickName());
190
                buddy.setMobile(onebuddy.mobile());
191
                buddy.setGroups(onebuddy.groups());
192
                buddy.setEmail(onebuddy.email());
193
                buddy.setDisplay(onebuddy.display());
194
                buddy.setHomePhone(onebuddy.homePhone());
195
                buddy.setAnonymous(false);
196
197
                BuddyManager::instance()->addItem(buddy);
198
        }
199
200
        if (!unImportedContacts.isEmpty())
201
        {
202
                // create names list
203
                QStringList contactsList;
204
                foreach (const Contact &c, unImportedContacts)
205
                {
206
                        QString display = c.ownerBuddy().display();
207
                        if (!contactsList.contains(display))
208
                                contactsList.append(display);
209
                }
210
211
                if (MessageDialog::ask(tr("Following contacts from your list were not found on server: %0.\nDo you want to remove them from contacts list?").arg(contactsList.join(", "))))
212
                        foreach (const Contact &c, unImportedContacts)
213
                                ContactManager::instance()->removeItem(c);
214
        }
215
216
        MessageDialog::msg(tr("Your contact list has been successfully imported from server"), false, "Infromation", this);
217
218
        // flush configuration to save all changes
219
        ConfigurationManager::instance()->flush();
220
221
        kdebugf2();
222
}
223
224
void AccountBuddyListWidget::buddiesListExported(bool ok)
225
{
226
        kdebugf();
227
228
        if (Clear)
229
                if (ok)
230
                        MessageDialog::msg(tr("Your contact list has been successfully deleted on server"), false, "Infromation", this);
231
                else
232
                        MessageDialog::msg(tr("The application encountered an internal error\nThe delete userlist on server was unsuccessful"), false, "Critical", this);
233
        else
234
                if (ok)
235
                        MessageDialog::msg(tr("Your contact list has been successfully exported to server"), false, "Information", this);
236
                else
237
                        MessageDialog::msg(tr("The application encountered an internal error\nThe export was unsuccessful"), false, "Critical", this);
238
239
        ExportButton->setEnabled(true);
240
241
        kdebugf2();
242
}