Qutlook Example (ActiveQt)▲
Sélectionnez
/**
**************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
***************************************************************************
*/
#include
"addressview.h"
#include
"msoutl.h"
#include <QtWidgets>
class
AddressBookModel : public
QAbstractListModel
{
public
:
explicit
AddressBookModel(AddressView *
parent);
virtual
~
AddressBookModel();
int
rowCount(const
QModelIndex &
amp;parent =
QModelIndex()) const
;
int
columnCount(const
QModelIndex &
amp;parent) const
;
QVariant headerData(int
section, Qt::
Orientation orientation, int
role) const
;
QVariant data(const
QModelIndex &
amp;index, int
role) const
;
void
changeItem(const
QModelIndex &
amp;index, const
QString &
amp;firstName, const
QString &
amp;lastName, const
QString &
amp;address, const
QString &
amp;email);
void
addItem(const
QString &
amp;firstName, const
QString &
amp;lastName, const
QString &
amp;address, const
QString &
amp;email);
void
update();
private
:
Outlook::
Application outlook;
Outlook::
Items *
contactItems;
mutable
QHash&
lt;QModelIndex, QStringList&
gt; cache;
}
;
AddressBookModel::
AddressBookModel(AddressView *
parent)
:
QAbstractListModel(parent)
{
if
(!
outlook.isNull()) {
Outlook::
NameSpace session(outlook.Session());
session.Logon();
Outlook::
MAPIFolder *
folder =
session.GetDefaultFolder(Outlook::
olFolderContacts);
contactItems =
new
Outlook::
Items(folder-&
gt;Items());
connect(contactItems, SIGNAL(ItemAdd(IDispatch*
)), parent, SLOT(updateOutlook()));
connect(contactItems, SIGNAL(ItemChange(IDispatch*
)), parent, SLOT(updateOutlook()));
connect(contactItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook()));
delete
folder;
}
}
AddressBookModel::
~
AddressBookModel()
{
delete
contactItems;
if
(!
outlook.isNull())
Outlook::
NameSpace(outlook.Session()).Logoff();
}
int
AddressBookModel::
rowCount(const
QModelIndex &
amp;) const
{
return
contactItems ? contactItems-&
gt;Count() : 0
;
}
int
AddressBookModel::
columnCount(const
QModelIndex &
amp; /*parent*/
) const
{
return
4
;
}
QVariant AddressBookModel::
headerData(int
section, Qt::
Orientation /*orientation*/
, int
role) const
{
if
(role !=
Qt::
DisplayRole)
return
QVariant();
switch
(section) {
case
0
:
return
tr("First Name"
);
case
1
:
return
tr("Last Name"
);
case
2
:
return
tr("Address"
);
case
3
:
return
tr("Email"
);
default
:
break
;
}
return
QVariant();
}
QVariant AddressBookModel::
data(const
QModelIndex &
amp;index, int
role) const
{
if
(!
index.isValid() ||
role !=
Qt::
DisplayRole)
return
QVariant();
QStringList data;
if
(cache.contains(index)) {
data =
cache.value(index);
}
else
{
Outlook::
ContactItem contact(contactItems-&
gt;Item(index.row() +
1
));
data &
lt;&
lt; contact.FirstName() &
lt;&
lt; contact.LastName() &
lt;&
lt; contact.HomeAddress() &
lt;&
lt; contact.Email1Address();
cache.insert(index, data);
}
if
(index.column() &
lt; data.count())
return
data.at(index.column());
return
QVariant();
}
void
AddressBookModel::
changeItem(const
QModelIndex &
amp;index, const
QString &
amp;firstName, const
QString &
amp;lastName, const
QString &
amp;address, const
QString &
amp;email)
{
Outlook::
ContactItem item(contactItems-&
gt;Item(index.row() +
1
));
item.SetFirstName(firstName);
item.SetLastName(lastName);
item.SetHomeAddress(address);
item.SetEmail1Address(email);
item.Save();
cache.take(index);
}
void
AddressBookModel::
addItem(const
QString &
amp;firstName, const
QString &
amp;lastName, const
QString &
amp;address, const
QString &
amp;email)
{
Outlook::
ContactItem item(outlook.CreateItem(Outlook::
olContactItem));
if
(!
item.isNull()) {
item.SetFirstName(firstName);
item.SetLastName(lastName);
item.SetHomeAddress(address);
item.SetEmail1Address(email);
item.Save();
}
}
void
AddressBookModel::
update()
{
beginResetModel();
cache.clear();
endResetModel();
}
AddressView::
AddressView(QWidget *
parent)
:
QWidget(parent)
{
QGridLayout *
mainGrid =
new
QGridLayout(this
);
QLabel *
firstNameLabel =
new
QLabel(tr("First &Name"
), this
);
firstNameLabel-&
gt;resize(firstNameLabel-&
gt;sizeHint());
mainGrid-&
gt;addWidget(firstNameLabel, 0
, 0
);
QLabel *
lastNameLabel =
new
QLabel(tr("&Last Name"
), this
);
lastNameLabel-&
gt;resize(lastNameLabel-&
gt;sizeHint());
mainGrid-&
gt;addWidget(lastNameLabel, 0
, 1
);
QLabel *
addressLabel =
new
QLabel(tr("Add&ress"
), this
);
addressLabel-&
gt;resize(addressLabel-&
gt;sizeHint());
mainGrid-&
gt;addWidget(addressLabel, 0
, 2
);
QLabel *
emailLabel =
new
QLabel(tr("&E-Mail"
), this
);
emailLabel-&
gt;resize(emailLabel-&
gt;sizeHint());
mainGrid-&
gt;addWidget(emailLabel, 0
, 3
);
m_addButton =
new
QPushButton(tr("A&dd"
), this
);
m_addButton-&
gt;resize(m_addButton-&
gt;sizeHint());
mainGrid-&
gt;addWidget(m_addButton, 0
, 4
);
connect(m_addButton, &
amp;QPushButton::
clicked, this
, &
amp;AddressView::
addEntry);
m_firstName =
new
QLineEdit(this
);
m_firstName-&
gt;resize(m_firstName-&
gt;sizeHint());
mainGrid-&
gt;addWidget(m_firstName, 1
, 0
);
firstNameLabel-&
gt;setBuddy(m_firstName);
m_lastName =
new
QLineEdit(this
);
m_lastName-&
gt;resize(m_lastName-&
gt;sizeHint());
mainGrid-&
gt;addWidget(m_lastName, 1
, 1
);
lastNameLabel-&
gt;setBuddy(m_lastName);
m_address =
new
QLineEdit(this
);
m_address-&
gt;resize(m_address-&
gt;sizeHint());
mainGrid-&
gt;addWidget(m_address, 1
, 2
);
addressLabel-&
gt;setBuddy(m_address);
m_email =
new
QLineEdit(this
);
m_email-&
gt;resize(m_email-&
gt;sizeHint());
mainGrid-&
gt;addWidget(m_email, 1
, 3
);
emailLabel-&
gt;setBuddy(m_email);
m_changeButton =
new
QPushButton(tr("&Change"
), this
);
m_changeButton-&
gt;resize(m_changeButton-&
gt;sizeHint());
mainGrid-&
gt;addWidget(m_changeButton, 1
, 4
);
connect(m_changeButton, &
amp;QPushButton::
clicked, this
, &
amp;AddressView::
changeEntry);
m_treeView =
new
QTreeView(this
);
m_treeView-&
gt;setSelectionMode(QTreeView::
SingleSelection);
m_treeView-&
gt;setRootIsDecorated(false
);
model =
new
AddressBookModel(this
);
m_treeView-&
gt;setModel(model);
connect(m_treeView-&
gt;selectionModel(), &
amp;QItemSelectionModel::
currentChanged, this
, &
amp;AddressView::
itemSelected);
mainGrid-&
gt;addWidget(m_treeView, 2
, 0
, 1
, 5
);
}
void
AddressView::
updateOutlook()
{
model-&
gt;update();
}
void
AddressView::
addEntry()
{
if
(!
m_firstName-&
gt;text().isEmpty() ||
!
m_lastName-&
gt;text().isEmpty() ||
!
m_address-&
gt;text().isEmpty() ||
!
m_email-&
gt;text().isEmpty()) {
model-&
gt;addItem(m_firstName-&
gt;text(), m_lastName-&
gt;text(), m_address-&
gt;text(), m_email-&
gt;text());
}
m_firstName-&
gt;clear();
m_lastName-&
gt;clear();
m_address-&
gt;clear();
m_email-&
gt;clear();
}
void
AddressView::
changeEntry()
{
QModelIndex current =
m_treeView-&
gt;currentIndex();
if
(current.isValid())
model-&
gt;changeItem(current, m_firstName-&
gt;text(), m_lastName-&
gt;text(), m_address-&
gt;text(), m_email-&
gt;text());
}
void
AddressView::
itemSelected(const
QModelIndex &
amp;index)
{
if
(!
index.isValid())
return
;
QAbstractItemModel *
model =
m_treeView-&
gt;model();
m_firstName-&
gt;setText(model-&
gt;data(model-&
gt;index(index.row(), 0
)).toString());
m_lastName-&
gt;setText(model-&
gt;data(model-&
gt;index(index.row(), 1
)).toString());
m_address-&
gt;setText(model-&
gt;data(model-&
gt;index(index.row(), 2
)).toString());
m_email-&
gt;setText(model-&
gt;data(model-&
gt;index(index.row(), 3
)).toString());
}