From 9a5ccc89daede1f289b2302430bdd41f9e56022d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8ger=20Hanseg=C3=A5rd?= Date: Tue, 8 Aug 2023 14:49:28 +0200 Subject: [ActiveQt] qutlook sample crashes with distribution lists The qutlook sample may crash if the contact list contains items that are not regular contacts, for example distribution lists. The crash occur because items such as distribution lists do not have the requested properties (FirstName, LastName etc). This patch works around this issue by checking if the contact list item is a contact before attempting to access the requested properties. A side effect is that non-contact entries show up as blank lines in the address list. In addition, the patch fixes an issue where the qutlook app crashes if creating the Outlook COM server fails. This was related to an uninitialized variable (AddressBookModel::folderItems) Task-number: QTBUG-111191 Change-Id: I6abfdaba4a49ef85bc245b26779b8bfdc409887b Reviewed-by: Volker Hilsheimer Reviewed-by: Oliver Wolff (cherry picked from commit 58a3cfe8574550872d47d7e08ae9b62e3b99bfd1) Reviewed-by: Qt Cherry-pick Bot --- examples/activeqt/qutlook/addressview.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) (limited to 'examples/activeqt') diff --git a/examples/activeqt/qutlook/addressview.cpp b/examples/activeqt/qutlook/addressview.cpp index 2f5242a..f3d4fc7 100644 --- a/examples/activeqt/qutlook/addressview.cpp +++ b/examples/activeqt/qutlook/addressview.cpp @@ -23,7 +23,7 @@ public: private: Outlook::Application outlook; - Outlook::Items * contactItems; + Outlook::Items *folderItems = nullptr; mutable QHash cache; }; @@ -36,10 +36,10 @@ AddressBookModel::AddressBookModel(AddressView *parent) Outlook::NameSpace session(outlook.Session()); session.Logon(); Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderContacts); - contactItems = new Outlook::Items(folder->Items()); - connect(contactItems, SIGNAL(ItemAdd(IDispatch*)), parent, SLOT(updateOutlook())); - connect(contactItems, SIGNAL(ItemChange(IDispatch*)), parent, SLOT(updateOutlook())); - connect(contactItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook())); + 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())); delete folder; } @@ -48,7 +48,7 @@ AddressBookModel::AddressBookModel(AddressView *parent) //! [1] //! [2] AddressBookModel::~AddressBookModel() { - delete contactItems; + delete folderItems; if (!outlook.isNull()) Outlook::NameSpace(outlook.Session()).Logoff(); @@ -57,7 +57,7 @@ AddressBookModel::~AddressBookModel() //! [2] //! [3] int AddressBookModel::rowCount(const QModelIndex &) const { - return contactItems ? contactItems->Count() : 0; + return folderItems ? folderItems->Count() : 0; } int AddressBookModel::columnCount(const QModelIndex & /*parent*/) const @@ -97,8 +97,11 @@ QVariant AddressBookModel::data(const QModelIndex &index, int role) const if (cache.contains(index)) { data = cache.value(index); } else { - Outlook::ContactItem contact(contactItems->Item(index.row() + 1)); - data << contact.FirstName() << contact.LastName() << contact.HomeAddress() << contact.Email1Address(); + Outlook::ContactItem contact(folderItems->Item(index.row() + 1)); + + if (contact.Class() == Outlook::OlObjectClass::olContact) + data << contact.FirstName() << contact.LastName() << contact.HomeAddress() << contact.Email1Address(); + cache.insert(index, data); } @@ -111,7 +114,10 @@ QVariant AddressBookModel::data(const QModelIndex &index, int role) const //! [5] //! [6] void AddressBookModel::changeItem(const QModelIndex &index, const QString &firstName, const QString &lastName, const QString &address, const QString &email) { - Outlook::ContactItem item(contactItems->Item(index.row() + 1)); + Outlook::ContactItem item(folderItems->Item(index.row() + 1)); + + if (item.Class() != Outlook::OlObjectClass::olContact) + return; // Not a contact item.SetFirstName(firstName); item.SetLastName(lastName); -- cgit v1.2.3