1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
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 
AddressBookModel(AddressView * parent)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]
~AddressBookModel()86 AddressBookModel::~AddressBookModel()
87 {
88     delete contactItems;
89 
90     if (!outlook.isNull())
91         Outlook::NameSpace(outlook.Session()).Logoff();
92 }
93 
94 //! [2] //! [3]
rowCount(const QModelIndex &) const95 int AddressBookModel::rowCount(const QModelIndex &) const
96 {
97     return contactItems ? contactItems->Count() : 0;
98 }
99 
columnCount(const QModelIndex & parent) const100 int AddressBookModel::columnCount(const QModelIndex &parent) const
101 {
102     return 4;
103 }
104 
105 //! [3] //! [4]
headerData(int section,Qt::Orientation orientation,int role) const106 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]
data(const QModelIndex & index,int role) const128 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]
changeItem(const QModelIndex & index,const QString & firstName,const QString & lastName,const QString & address,const QString & email)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]
addItem(const QString & firstName,const QString & lastName,const QString & address,const QString & email)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]
update()178 void AddressBookModel::update()
179 {
180     cache.clear();
181 
182     emit reset();
183 }
184 
185 
186 //! [8] //! [9]
AddressView(QWidget * parent)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 
245     connect(treeView->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), this, SLOT(itemSelected(QModelIndex)));
246 
247     mainGrid->addWidget(treeView, 2, 0, 1, 5);
248 }
249 
updateOutlook()250 void AddressView::updateOutlook()
251 {
252     model->update();
253 }
254 
addEntry()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 
changeEntry()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]
itemSelected(const QModelIndex & index)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]
289