|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
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 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 //! [0] |
|
43 #include "addressview.h" |
|
44 #include "msoutl.h" |
|
45 #include <QtGui> |
|
46 |
|
47 class AddressBookModel : public QAbstractListModel |
|
48 { |
|
49 public: |
|
50 AddressBookModel(AddressView *parent); |
|
51 ~AddressBookModel(); |
|
52 |
|
53 int rowCount(const QModelIndex &parent = QModelIndex()) const; |
|
54 int columnCount(const QModelIndex &parent) const; |
|
55 QVariant headerData(int section, Qt::Orientation orientation, int role) const; |
|
56 QVariant data(const QModelIndex &index, int role) const; |
|
57 |
|
58 void changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email); |
|
59 void addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &email); |
|
60 void update(); |
|
61 |
|
62 private: |
|
63 Outlook::Application outlook; |
|
64 Outlook::Items * contactItems; |
|
65 |
|
66 mutable QHash<QModelIndex, QStringList> cache; |
|
67 }; |
|
68 //! [0] //! [1] |
|
69 |
|
70 AddressBookModel::AddressBookModel(AddressView *parent) |
|
71 : QAbstractListModel(parent) |
|
72 { |
|
73 if (!outlook.isNull()) { |
|
74 Outlook::NameSpace session(outlook.Session()); |
|
75 session.Logon(); |
|
76 Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderContacts); |
|
77 contactItems = new Outlook::Items(folder->Items()); |
|
78 connect(contactItems, SIGNAL(ItemAdd(IDispatch*)), parent, SLOT(updateOutlook())); |
|
79 connect(contactItems, SIGNAL(ItemChange(IDispatch*)), parent, SLOT(updateOutlook())); |
|
80 connect(contactItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook())); |
|
81 |
|
82 delete folder; |
|
83 } |
|
84 } |
|
85 |
|
86 //! [1] //! [2] |
|
87 AddressBookModel::~AddressBookModel() |
|
88 { |
|
89 delete contactItems; |
|
90 |
|
91 if (!outlook.isNull()) |
|
92 Outlook::NameSpace(outlook.Session()).Logoff(); |
|
93 } |
|
94 |
|
95 //! [2] //! [3] |
|
96 int AddressBookModel::rowCount(const QModelIndex &) const |
|
97 { |
|
98 return contactItems ? contactItems->Count() : 0; |
|
99 } |
|
100 |
|
101 int AddressBookModel::columnCount(const QModelIndex &parent) const |
|
102 { |
|
103 return 4; |
|
104 } |
|
105 |
|
106 //! [3] //! [4] |
|
107 QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
108 { |
|
109 if (role != Qt::DisplayRole) |
|
110 return QVariant(); |
|
111 |
|
112 switch (section) { |
|
113 case 0: |
|
114 return tr("First Name"); |
|
115 case 1: |
|
116 return tr("Last Name"); |
|
117 case 2: |
|
118 return tr("Address"); |
|
119 case 3: |
|
120 return tr("Email"); |
|
121 default: |
|
122 break; |
|
123 } |
|
124 |
|
125 return QVariant(); |
|
126 } |
|
127 |
|
128 //! [4] //! [5] |
|
129 QVariant AddressBookModel::data(const QModelIndex &index, int role) const |
|
130 { |
|
131 if (!index.isValid() || role != Qt::DisplayRole) |
|
132 return QVariant(); |
|
133 |
|
134 QStringList data; |
|
135 if (cache.contains(index)) { |
|
136 data = cache.value(index); |
|
137 } else { |
|
138 Outlook::ContactItem contact(contactItems->Item(index.row() + 1)); |
|
139 data << contact.FirstName() << contact.LastName() << contact.HomeAddress() << contact.Email1Address(); |
|
140 cache.insert(index, data); |
|
141 } |
|
142 |
|
143 if (index.column() < data.count()) |
|
144 return data.at(index.column()); |
|
145 |
|
146 return QVariant(); |
|
147 } |
|
148 |
|
149 //! [5] //! [6] |
|
150 void AddressBookModel::changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email) |
|
151 { |
|
152 Outlook::ContactItem item(contactItems->Item(index.row() + 1)); |
|
153 |
|
154 item.SetFirstName(firstName); |
|
155 item.SetLastName(lastName); |
|
156 item.SetHomeAddress(address); |
|
157 item.SetEmail1Address(email); |
|
158 |
|
159 item.Save(); |
|
160 |
|
161 cache.take(index); |
|
162 } |
|
163 |
|
164 //! [6] //! [7] |
|
165 void AddressBookModel::addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &email) |
|
166 { |
|
167 Outlook::ContactItem item(outlook.CreateItem(Outlook::olContactItem)); |
|
168 if (!item.isNull()) { |
|
169 item.SetFirstName(firstName); |
|
170 item.SetLastName(lastName); |
|
171 item.SetHomeAddress(address); |
|
172 item.SetEmail1Address(email); |
|
173 |
|
174 item.Save(); |
|
175 } |
|
176 } |
|
177 |
|
178 //! [7] //! [8] |
|
179 void AddressBookModel::update() |
|
180 { |
|
181 cache.clear(); |
|
182 |
|
183 emit reset(); |
|
184 } |
|
185 |
|
186 |
|
187 //! [8] //! [9] |
|
188 AddressView::AddressView(QWidget *parent) |
|
189 : QWidget(parent) |
|
190 { |
|
191 QGridLayout *mainGrid = new QGridLayout(this); |
|
192 |
|
193 QLabel *liFirstName = new QLabel("First &Name", this); |
|
194 liFirstName->resize(liFirstName->sizeHint()); |
|
195 mainGrid->addWidget(liFirstName, 0, 0); |
|
196 |
|
197 QLabel *liLastName = new QLabel("&Last Name", this); |
|
198 liLastName->resize(liLastName->sizeHint()); |
|
199 mainGrid->addWidget(liLastName, 0, 1); |
|
200 |
|
201 QLabel *liAddress = new QLabel("Add&ress", this); |
|
202 liAddress->resize(liAddress->sizeHint()); |
|
203 mainGrid->addWidget(liAddress, 0, 2); |
|
204 |
|
205 QLabel *liEMail = new QLabel("&E-Mail", this); |
|
206 liEMail->resize(liEMail->sizeHint()); |
|
207 mainGrid->addWidget(liEMail, 0, 3); |
|
208 |
|
209 add = new QPushButton("A&dd", this); |
|
210 add->resize(add->sizeHint()); |
|
211 mainGrid->addWidget(add, 0, 4); |
|
212 connect(add, SIGNAL(clicked()), this, SLOT(addEntry())); |
|
213 |
|
214 iFirstName = new QLineEdit(this); |
|
215 iFirstName->resize(iFirstName->sizeHint()); |
|
216 mainGrid->addWidget(iFirstName, 1, 0); |
|
217 liFirstName->setBuddy(iFirstName); |
|
218 |
|
219 iLastName = new QLineEdit(this); |
|
220 iLastName->resize(iLastName->sizeHint()); |
|
221 mainGrid->addWidget(iLastName, 1, 1); |
|
222 liLastName->setBuddy(iLastName); |
|
223 |
|
224 iAddress = new QLineEdit(this); |
|
225 iAddress->resize(iAddress->sizeHint()); |
|
226 mainGrid->addWidget(iAddress, 1, 2); |
|
227 liAddress->setBuddy(iAddress); |
|
228 |
|
229 iEMail = new QLineEdit(this); |
|
230 iEMail->resize(iEMail->sizeHint()); |
|
231 mainGrid->addWidget(iEMail, 1, 3); |
|
232 liEMail->setBuddy(iEMail); |
|
233 |
|
234 change = new QPushButton("&Change", this); |
|
235 change->resize(change->sizeHint()); |
|
236 mainGrid->addWidget(change, 1, 4); |
|
237 connect(change, SIGNAL(clicked()), this, SLOT(changeEntry())); |
|
238 |
|
239 treeView = new QTreeView(this); |
|
240 treeView->setSelectionMode(QTreeView::SingleSelection); |
|
241 treeView->setRootIsDecorated(false); |
|
242 |
|
243 model = new AddressBookModel(this); |
|
244 treeView->setModel(model); |
|
245 |
|
246 connect(treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(itemSelected(QModelIndex))); |
|
247 |
|
248 mainGrid->addWidget(treeView, 2, 0, 1, 5); |
|
249 } |
|
250 |
|
251 void AddressView::updateOutlook() |
|
252 { |
|
253 model->update(); |
|
254 } |
|
255 |
|
256 void AddressView::addEntry() |
|
257 { |
|
258 if (!iFirstName->text().isEmpty() || !iLastName->text().isEmpty() || |
|
259 !iAddress->text().isEmpty() || !iEMail->text().isEmpty()) { |
|
260 model->addItem(iFirstName->text(), iFirstName->text(), iAddress->text(), iEMail->text()); |
|
261 } |
|
262 |
|
263 iFirstName->setText(""); |
|
264 iLastName->setText(""); |
|
265 iAddress->setText(""); |
|
266 iEMail->setText(""); |
|
267 } |
|
268 |
|
269 void AddressView::changeEntry() |
|
270 { |
|
271 QModelIndex current = treeView->currentIndex(); |
|
272 |
|
273 if (current.isValid()) |
|
274 model->changeItem(current, iFirstName->text(), iLastName->text(), iAddress->text(), iEMail->text()); |
|
275 } |
|
276 |
|
277 //! [9] //! [10] |
|
278 void AddressView::itemSelected(const QModelIndex &index) |
|
279 { |
|
280 if (!index.isValid()) |
|
281 return; |
|
282 |
|
283 QAbstractItemModel *model = treeView->model(); |
|
284 iFirstName->setText(model->data(model->index(index.row(), 0)).toString()); |
|
285 iLastName->setText(model->data(model->index(index.row(), 1)).toString()); |
|
286 iAddress->setText(model->data(model->index(index.row(), 2)).toString()); |
|
287 iEMail->setText(model->data(model->index(index.row(), 3)).toString()); |
|
288 } |
|
289 //! [10] |