author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 06 Jul 2010 15:10:48 +0300 | |
changeset 30 | 5dc02b23752f |
parent 18 | 2f34d5167611 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the examples of the Qt Toolkit. |
|
8 |
** |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
9 |
** $QT_BEGIN_LICENSE:BSD$ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
10 |
** You may use this file under the terms of the BSD license as follows: |
0 | 11 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
12 |
** "Redistribution and use in source and binary forms, with or without |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
13 |
** modification, are permitted provided that the following conditions are |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
14 |
** met: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
15 |
** * Redistributions of source code must retain the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
16 |
** notice, this list of conditions and the following disclaimer. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
17 |
** * Redistributions in binary form must reproduce the above copyright |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
18 |
** notice, this list of conditions and the following disclaimer in |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
19 |
** the documentation and/or other materials provided with the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
20 |
** distribution. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
21 |
** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
22 |
** the names of its contributors may be used to endorse or promote |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
23 |
** products derived from this software without specific prior written |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
24 |
** permission. |
0 | 25 |
** |
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
26 |
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
27 |
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
28 |
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
29 |
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
30 |
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
31 |
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
32 |
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
33 |
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
34 |
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
35 |
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
36 |
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
0 | 37 |
** $QT_END_LICENSE$ |
38 |
** |
|
39 |
****************************************************************************/ |
|
40 |
||
41 |
//! [0] |
|
42 |
#include "addressview.h" |
|
43 |
#include "msoutl.h" |
|
44 |
#include <QtGui> |
|
45 |
||
46 |
class AddressBookModel : public QAbstractListModel |
|
47 |
{ |
|
48 |
public: |
|
49 |
AddressBookModel(AddressView *parent); |
|
50 |
~AddressBookModel(); |
|
51 |
||
52 |
int rowCount(const QModelIndex &parent = QModelIndex()) const; |
|
53 |
int columnCount(const QModelIndex &parent) const; |
|
54 |
QVariant headerData(int section, Qt::Orientation orientation, int role) const; |
|
55 |
QVariant data(const QModelIndex &index, int role) const; |
|
56 |
||
57 |
void changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email); |
|
58 |
void addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &email); |
|
59 |
void update(); |
|
60 |
||
61 |
private: |
|
62 |
Outlook::Application outlook; |
|
63 |
Outlook::Items * contactItems; |
|
64 |
||
65 |
mutable QHash<QModelIndex, QStringList> cache; |
|
66 |
}; |
|
67 |
//! [0] //! [1] |
|
68 |
||
69 |
AddressBookModel::AddressBookModel(AddressView *parent) |
|
70 |
: QAbstractListModel(parent) |
|
71 |
{ |
|
72 |
if (!outlook.isNull()) { |
|
73 |
Outlook::NameSpace session(outlook.Session()); |
|
74 |
session.Logon(); |
|
75 |
Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderContacts); |
|
76 |
contactItems = new Outlook::Items(folder->Items()); |
|
77 |
connect(contactItems, SIGNAL(ItemAdd(IDispatch*)), parent, SLOT(updateOutlook())); |
|
78 |
connect(contactItems, SIGNAL(ItemChange(IDispatch*)), parent, SLOT(updateOutlook())); |
|
79 |
connect(contactItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook())); |
|
80 |
||
81 |
delete folder; |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
//! [1] //! [2] |
|
86 |
AddressBookModel::~AddressBookModel() |
|
87 |
{ |
|
88 |
delete contactItems; |
|
89 |
||
90 |
if (!outlook.isNull()) |
|
91 |
Outlook::NameSpace(outlook.Session()).Logoff(); |
|
92 |
} |
|
93 |
||
94 |
//! [2] //! [3] |
|
95 |
int AddressBookModel::rowCount(const QModelIndex &) const |
|
96 |
{ |
|
97 |
return contactItems ? contactItems->Count() : 0; |
|
98 |
} |
|
99 |
||
100 |
int AddressBookModel::columnCount(const QModelIndex &parent) const |
|
101 |
{ |
|
102 |
return 4; |
|
103 |
} |
|
104 |
||
105 |
//! [3] //! [4] |
|
106 |
QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
107 |
{ |
|
108 |
if (role != Qt::DisplayRole) |
|
109 |
return QVariant(); |
|
110 |
||
111 |
switch (section) { |
|
112 |
case 0: |
|
113 |
return tr("First Name"); |
|
114 |
case 1: |
|
115 |
return tr("Last Name"); |
|
116 |
case 2: |
|
117 |
return tr("Address"); |
|
118 |
case 3: |
|
119 |
return tr("Email"); |
|
120 |
default: |
|
121 |
break; |
|
122 |
} |
|
123 |
||
124 |
return QVariant(); |
|
125 |
} |
|
126 |
||
127 |
//! [4] //! [5] |
|
128 |
QVariant AddressBookModel::data(const QModelIndex &index, int role) const |
|
129 |
{ |
|
130 |
if (!index.isValid() || role != Qt::DisplayRole) |
|
131 |
return QVariant(); |
|
132 |
||
133 |
QStringList data; |
|
134 |
if (cache.contains(index)) { |
|
135 |
data = cache.value(index); |
|
136 |
} else { |
|
137 |
Outlook::ContactItem contact(contactItems->Item(index.row() + 1)); |
|
138 |
data << contact.FirstName() << contact.LastName() << contact.HomeAddress() << contact.Email1Address(); |
|
139 |
cache.insert(index, data); |
|
140 |
} |
|
141 |
||
142 |
if (index.column() < data.count()) |
|
143 |
return data.at(index.column()); |
|
144 |
||
145 |
return QVariant(); |
|
146 |
} |
|
147 |
||
148 |
//! [5] //! [6] |
|
149 |
void AddressBookModel::changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email) |
|
150 |
{ |
|
151 |
Outlook::ContactItem item(contactItems->Item(index.row() + 1)); |
|
152 |
||
153 |
item.SetFirstName(firstName); |
|
154 |
item.SetLastName(lastName); |
|
155 |
item.SetHomeAddress(address); |
|
156 |
item.SetEmail1Address(email); |
|
157 |
||
158 |
item.Save(); |
|
159 |
||
160 |
cache.take(index); |
|
161 |
} |
|
162 |
||
163 |
//! [6] //! [7] |
|
164 |
void AddressBookModel::addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &email) |
|
165 |
{ |
|
166 |
Outlook::ContactItem item(outlook.CreateItem(Outlook::olContactItem)); |
|
167 |
if (!item.isNull()) { |
|
168 |
item.SetFirstName(firstName); |
|
169 |
item.SetLastName(lastName); |
|
170 |
item.SetHomeAddress(address); |
|
171 |
item.SetEmail1Address(email); |
|
172 |
||
173 |
item.Save(); |
|
174 |
} |
|
175 |
} |
|
176 |
||
177 |
//! [7] //! [8] |
|
178 |
void AddressBookModel::update() |
|
179 |
{ |
|
180 |
cache.clear(); |
|
181 |
||
182 |
emit reset(); |
|
183 |
} |
|
184 |
||
185 |
||
186 |
//! [8] //! [9] |
|
187 |
AddressView::AddressView(QWidget *parent) |
|
188 |
: QWidget(parent) |
|
189 |
{ |
|
190 |
QGridLayout *mainGrid = new QGridLayout(this); |
|
191 |
||
192 |
QLabel *liFirstName = new QLabel("First &Name", this); |
|
193 |
liFirstName->resize(liFirstName->sizeHint()); |
|
194 |
mainGrid->addWidget(liFirstName, 0, 0); |
|
195 |
||
196 |
QLabel *liLastName = new QLabel("&Last Name", this); |
|
197 |
liLastName->resize(liLastName->sizeHint()); |
|
198 |
mainGrid->addWidget(liLastName, 0, 1); |
|
199 |
||
200 |
QLabel *liAddress = new QLabel("Add&ress", this); |
|
201 |
liAddress->resize(liAddress->sizeHint()); |
|
202 |
mainGrid->addWidget(liAddress, 0, 2); |
|
203 |
||
204 |
QLabel *liEMail = new QLabel("&E-Mail", this); |
|
205 |
liEMail->resize(liEMail->sizeHint()); |
|
206 |
mainGrid->addWidget(liEMail, 0, 3); |
|
207 |
||
208 |
add = new QPushButton("A&dd", this); |
|
209 |
add->resize(add->sizeHint()); |
|
210 |
mainGrid->addWidget(add, 0, 4); |
|
211 |
connect(add, SIGNAL(clicked()), this, SLOT(addEntry())); |
|
212 |
||
213 |
iFirstName = new QLineEdit(this); |
|
214 |
iFirstName->resize(iFirstName->sizeHint()); |
|
215 |
mainGrid->addWidget(iFirstName, 1, 0); |
|
216 |
liFirstName->setBuddy(iFirstName); |
|
217 |
||
218 |
iLastName = new QLineEdit(this); |
|
219 |
iLastName->resize(iLastName->sizeHint()); |
|
220 |
mainGrid->addWidget(iLastName, 1, 1); |
|
221 |
liLastName->setBuddy(iLastName); |
|
222 |
||
223 |
iAddress = new QLineEdit(this); |
|
224 |
iAddress->resize(iAddress->sizeHint()); |
|
225 |
mainGrid->addWidget(iAddress, 1, 2); |
|
226 |
liAddress->setBuddy(iAddress); |
|
227 |
||
228 |
iEMail = new QLineEdit(this); |
|
229 |
iEMail->resize(iEMail->sizeHint()); |
|
230 |
mainGrid->addWidget(iEMail, 1, 3); |
|
231 |
liEMail->setBuddy(iEMail); |
|
232 |
||
233 |
change = new QPushButton("&Change", this); |
|
234 |
change->resize(change->sizeHint()); |
|
235 |
mainGrid->addWidget(change, 1, 4); |
|
236 |
connect(change, SIGNAL(clicked()), this, SLOT(changeEntry())); |
|
237 |
||
238 |
treeView = new QTreeView(this); |
|
239 |
treeView->setSelectionMode(QTreeView::SingleSelection); |
|
240 |
treeView->setRootIsDecorated(false); |
|
241 |
||
242 |
model = new AddressBookModel(this); |
|
243 |
treeView->setModel(model); |
|
244 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
245 |
connect(treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(itemSelected(QModelIndex))); |
0 | 246 |
|
247 |
mainGrid->addWidget(treeView, 2, 0, 1, 5); |
|
248 |
} |
|
249 |
||
250 |
void AddressView::updateOutlook() |
|
251 |
{ |
|
252 |
model->update(); |
|
253 |
} |
|
254 |
||
255 |
void AddressView::addEntry() |
|
256 |
{ |
|
257 |
if (!iFirstName->text().isEmpty() || !iLastName->text().isEmpty() || |
|
258 |
!iAddress->text().isEmpty() || !iEMail->text().isEmpty()) { |
|
259 |
model->addItem(iFirstName->text(), iFirstName->text(), iAddress->text(), iEMail->text()); |
|
260 |
} |
|
261 |
||
262 |
iFirstName->setText(""); |
|
263 |
iLastName->setText(""); |
|
264 |
iAddress->setText(""); |
|
265 |
iEMail->setText(""); |
|
266 |
} |
|
267 |
||
268 |
void AddressView::changeEntry() |
|
269 |
{ |
|
270 |
QModelIndex current = treeView->currentIndex(); |
|
271 |
||
272 |
if (current.isValid()) |
|
273 |
model->changeItem(current, iFirstName->text(), iLastName->text(), iAddress->text(), iEMail->text()); |
|
274 |
} |
|
275 |
||
276 |
//! [9] //! [10] |
|
277 |
void AddressView::itemSelected(const QModelIndex &index) |
|
278 |
{ |
|
279 |
if (!index.isValid()) |
|
280 |
return; |
|
281 |
||
282 |
QAbstractItemModel *model = treeView->model(); |
|
283 |
iFirstName->setText(model->data(model->index(index.row(), 0)).toString()); |
|
284 |
iLastName->setText(model->data(model->index(index.row(), 1)).toString()); |
|
285 |
iAddress->setText(model->data(model->index(index.row(), 2)).toString()); |
|
286 |
iEMail->setText(model->data(model->index(index.row(), 3)).toString()); |
|
287 |
} |
|
288 |
//! [10] |