2022-06-07 11:55:27 +00:00
|
|
|
// Copyright (C) 2015 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
//! [0]
|
|
|
|
#include "addressview.h"
|
|
|
|
#include "msoutl.h"
|
2011-10-04 08:58:57 +00:00
|
|
|
#include <QtWidgets>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
class AddressBookModel : public QAbstractListModel
|
|
|
|
{
|
|
|
|
public:
|
2017-08-21 11:23:48 +00:00
|
|
|
explicit AddressBookModel(AddressView *parent);
|
|
|
|
virtual ~AddressBookModel();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
int rowCount(const QModelIndex &parent = QModelIndex()) const;
|
|
|
|
int columnCount(const QModelIndex &parent) const;
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
|
|
|
QVariant data(const QModelIndex &index, int role) const;
|
|
|
|
|
|
|
|
void changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email);
|
|
|
|
void addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &email);
|
|
|
|
void update();
|
|
|
|
|
|
|
|
private:
|
|
|
|
Outlook::Application outlook;
|
2023-08-08 12:49:28 +00:00
|
|
|
Outlook::Items *folderItems = nullptr;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
mutable QHash<QModelIndex, QStringList> cache;
|
|
|
|
};
|
|
|
|
//! [0] //! [1]
|
|
|
|
|
|
|
|
AddressBookModel::AddressBookModel(AddressView *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
{
|
|
|
|
if (!outlook.isNull()) {
|
|
|
|
Outlook::NameSpace session(outlook.Session());
|
|
|
|
session.Logon();
|
|
|
|
Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderContacts);
|
2023-08-08 12:49:28 +00:00
|
|
|
folderItems = new Outlook::Items(folder->Items());
|
|
|
|
connect(folderItems, SIGNAL(ItemAdd(IDispatch*)), parent, SLOT(updateOutlook()));
|
|
|
|
connect(folderItems, SIGNAL(ItemChange(IDispatch*)), parent, SLOT(updateOutlook()));
|
|
|
|
connect(folderItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook()));
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
delete folder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [1] //! [2]
|
|
|
|
AddressBookModel::~AddressBookModel()
|
|
|
|
{
|
2023-08-08 12:49:28 +00:00
|
|
|
delete folderItems;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (!outlook.isNull())
|
|
|
|
Outlook::NameSpace(outlook.Session()).Logoff();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [2] //! [3]
|
|
|
|
int AddressBookModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
2023-08-08 12:49:28 +00:00
|
|
|
return folderItems ? folderItems->Count() : 0;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
int AddressBookModel::columnCount(const QModelIndex & /*parent*/) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [3] //! [4]
|
2017-08-21 11:23:48 +00:00
|
|
|
QVariant AddressBookModel::headerData(int section, Qt::Orientation /*orientation*/, int role) const
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [4] //! [5]
|
|
|
|
QVariant AddressBookModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (!index.isValid() || role != Qt::DisplayRole)
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
QStringList data;
|
|
|
|
if (cache.contains(index)) {
|
|
|
|
data = cache.value(index);
|
|
|
|
} else {
|
2023-08-08 12:49:28 +00:00
|
|
|
Outlook::ContactItem contact(folderItems->Item(index.row() + 1));
|
|
|
|
|
|
|
|
if (contact.Class() == Outlook::OlObjectClass::olContact)
|
|
|
|
data << contact.FirstName() << contact.LastName() << contact.HomeAddress() << contact.Email1Address();
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
cache.insert(index, data);
|
|
|
|
}
|
|
|
|
|
2022-03-16 09:12:22 +00:00
|
|
|
if (index.column() < data.size())
|
2011-04-27 10:05:43 +00:00
|
|
|
return data.at(index.column());
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [5] //! [6]
|
|
|
|
void AddressBookModel::changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email)
|
|
|
|
{
|
2023-08-08 12:49:28 +00:00
|
|
|
Outlook::ContactItem item(folderItems->Item(index.row() + 1));
|
|
|
|
|
|
|
|
if (item.Class() != Outlook::OlObjectClass::olContact)
|
|
|
|
return; // Not a contact
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
item.SetFirstName(firstName);
|
|
|
|
item.SetLastName(lastName);
|
|
|
|
item.SetHomeAddress(address);
|
|
|
|
item.SetEmail1Address(email);
|
|
|
|
|
|
|
|
item.Save();
|
|
|
|
|
|
|
|
cache.take(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [6] //! [7]
|
|
|
|
void AddressBookModel::addItem(const QString &firstName, const QString &lastName, const QString &address, const QString &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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! [7] //! [8]
|
|
|
|
void AddressBookModel::update()
|
|
|
|
{
|
2012-09-12 11:31:33 +00:00
|
|
|
beginResetModel();
|
2011-04-27 10:05:43 +00:00
|
|
|
cache.clear();
|
2012-09-12 11:31:33 +00:00
|
|
|
endResetModel();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! [8] //! [9]
|
|
|
|
AddressView::AddressView(QWidget *parent)
|
|
|
|
: QWidget(parent)
|
|
|
|
{
|
|
|
|
QGridLayout *mainGrid = new QGridLayout(this);
|
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
QLabel *firstNameLabel = new QLabel(tr("First &Name"), this);
|
|
|
|
firstNameLabel->resize(firstNameLabel->sizeHint());
|
|
|
|
mainGrid->addWidget(firstNameLabel, 0, 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
QLabel *lastNameLabel = new QLabel(tr("&Last Name"), this);
|
|
|
|
lastNameLabel->resize(lastNameLabel->sizeHint());
|
|
|
|
mainGrid->addWidget(lastNameLabel, 0, 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
QLabel *addressLabel = new QLabel(tr("Add&ress"), this);
|
|
|
|
addressLabel->resize(addressLabel->sizeHint());
|
|
|
|
mainGrid->addWidget(addressLabel, 0, 2);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
QLabel *emailLabel = new QLabel(tr("&E-Mail"), this);
|
|
|
|
emailLabel->resize(emailLabel->sizeHint());
|
|
|
|
mainGrid->addWidget(emailLabel, 0, 3);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_addButton = new QPushButton(tr("A&dd"), this);
|
|
|
|
m_addButton->resize(m_addButton->sizeHint());
|
|
|
|
mainGrid->addWidget(m_addButton, 0, 4);
|
|
|
|
connect(m_addButton, &QPushButton::clicked, this, &AddressView::addEntry);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_firstName = new QLineEdit(this);
|
|
|
|
m_firstName->resize(m_firstName->sizeHint());
|
|
|
|
mainGrid->addWidget(m_firstName, 1, 0);
|
|
|
|
firstNameLabel->setBuddy(m_firstName);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_lastName = new QLineEdit(this);
|
|
|
|
m_lastName->resize(m_lastName->sizeHint());
|
|
|
|
mainGrid->addWidget(m_lastName, 1, 1);
|
|
|
|
lastNameLabel->setBuddy(m_lastName);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_address = new QLineEdit(this);
|
|
|
|
m_address->resize(m_address->sizeHint());
|
|
|
|
mainGrid->addWidget(m_address, 1, 2);
|
|
|
|
addressLabel->setBuddy(m_address);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_email = new QLineEdit(this);
|
|
|
|
m_email->resize(m_email->sizeHint());
|
|
|
|
mainGrid->addWidget(m_email, 1, 3);
|
|
|
|
emailLabel->setBuddy(m_email);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_changeButton = new QPushButton(tr("&Change"), this);
|
|
|
|
m_changeButton->resize(m_changeButton->sizeHint());
|
|
|
|
mainGrid->addWidget(m_changeButton, 1, 4);
|
|
|
|
connect(m_changeButton, &QPushButton::clicked, this, &AddressView::changeEntry);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_treeView = new QTreeView(this);
|
|
|
|
m_treeView->setSelectionMode(QTreeView::SingleSelection);
|
|
|
|
m_treeView->setRootIsDecorated(false);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
model = new AddressBookModel(this);
|
2017-08-21 11:23:48 +00:00
|
|
|
m_treeView->setModel(model);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
connect(m_treeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &AddressView::itemSelected);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
mainGrid->addWidget(m_treeView, 2, 0, 1, 5);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressView::updateOutlook()
|
|
|
|
{
|
|
|
|
model->update();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddressView::addEntry()
|
|
|
|
{
|
2017-08-21 11:23:48 +00:00
|
|
|
if (!m_firstName->text().isEmpty() || !m_lastName->text().isEmpty() ||
|
|
|
|
!m_address->text().isEmpty() || !m_email->text().isEmpty()) {
|
|
|
|
model->addItem(m_firstName->text(), m_lastName->text(), m_address->text(), m_email->text());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
m_firstName->clear();
|
|
|
|
m_lastName->clear();
|
|
|
|
m_address->clear();
|
|
|
|
m_email->clear();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AddressView::changeEntry()
|
|
|
|
{
|
2017-08-21 11:23:48 +00:00
|
|
|
QModelIndex current = m_treeView->currentIndex();
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if (current.isValid())
|
2017-08-21 11:23:48 +00:00
|
|
|
model->changeItem(current, m_firstName->text(), m_lastName->text(), m_address->text(), m_email->text());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//! [9] //! [10]
|
|
|
|
void AddressView::itemSelected(const QModelIndex &index)
|
|
|
|
{
|
|
|
|
if (!index.isValid())
|
2014-01-15 21:17:52 +00:00
|
|
|
return;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2017-08-21 11:23:48 +00:00
|
|
|
QAbstractItemModel *model = m_treeView->model();
|
|
|
|
m_firstName->setText(model->data(model->index(index.row(), 0)).toString());
|
|
|
|
m_lastName->setText(model->data(model->index(index.row(), 1)).toString());
|
|
|
|
m_address->setText(model->data(model->index(index.row(), 2)).toString());
|
|
|
|
m_email->setText(model->data(model->index(index.row(), 3)).toString());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
//! [10]
|