Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

In Sync with Outlook

This example is a modified version of the standard TQt addressbook example.

It demonstrates the use of TQAxObject and querySubObject to instantiate and navigate the Outlook Object Model, and the use of the TQt property system to read and write values of items in the Outlook contact folder.

The modifications in the class declaration of the central widget are a forward declaration of the TQAxObject class and the IDispatch interface, and a new TQListViewItem subclass ABListViewItem that implements a constructor and a destructor and has a member contact_item of type TQAxObject.

    class TQAxObject;
    struct IDispatch;

    class ABListViewItem : public TQListViewItem
    {
    public:
        ABListViewItem( TQListView *listview, TQString firstName, TQString lastName, TQString address, TQString eMail, TQAxObject *contact );
        ~ABListViewItem();

        TQAxObject *contactItem() const;

    private:
        TQAxObject *contact_item;
    };

The ABCentralWidget gets a destructor, a new protected function setupOutlook, a new protected slot updateOutlook, and also three members of type TQAxObject.

        void findEntries();

        void updateOutlook();

    protected:
        void setupTabWidget();
        void setupListView();
        void setupOutlook();

        TQAxObject *outlook, *outlookSession, *contactItems;

        TQGridLayout *mainGrid;

The implementation of the ABListViewItem class is trivial:

    ABListViewItem::ABListViewItem( TQListView *listview,
                                    TQString firstName,
                                    TQString lastName,
                                    TQString address,
                                    TQString eMail,
                                    TQAxObject *contact )
    : TQListViewItem( listview, firstName, lastName, address, eMail ), contact_item( contact )
    {
    }

    ABListViewItem::~ABListViewItem()
    {
        delete contact_item;
    }

    TQAxObject *ABListViewItem::contactItem() const
    {
        return contact_item;
    }
The ABCentralWidget constructor initializes the TQAxObject pointers to zero and calls the setupOutlook function. The ABCentralWidget destructor calls the Logoff method of the outlookSession object.
    ABCentralWidget::ABCentralWidget( TQWidget *parent, const char *name )
        : TQWidget( parent, name ), outlook( 0 ), outlookSession( 0 ), contactItems( 0 )
    {
        mainGrid = new TQGridLayout( this, 2, 1, 5, 5 );

        setupTabWidget();
        setupListView();
        setupOutlook();

        mainGrid->setRowStretch( 0, 0 );
        mainGrid->setRowStretch( 1, 1 );
    }

    ABCentralWidget::~ABCentralWidget()
    {
        if ( outlookSession )
            outlookSession->dynamicCall( "Logoff()" );
    }
The setupOutlook implementation creates a TQAxObject to wrap the Outlook.Application COM object.
    void ABCentralWidget::setupOutlook()
    {
        outlook = new TQAxObject( "Outlook.Application", this );
The call to querySubObject returns a new TQAxObject wrapper around the "Session" object of the Outlook Object hierarchy. If the call fails for some reason setupOutlook returns, otherwise it calls the "Logon" method of the Session object.
        // Get a session object
        outlookSession = outlook->querySubObject( "Session" );
        if ( !outlookSession )
            return;
        // Login; doesn't hurt if you are already running and logged on...
        outlookSession->dynamicCall( "Logon()" );
The following call to querySubObject returns a new TQAxObject wrapper around the default folder for "contacts".
        // Get the default folder for contacts
        TQAxObject *defFolder = outlookSession->querySubObject( "GetDefaultFolder(OlDefaultFolders)", "olFolderContacts" );
querySubObject is then used again to get the list of all items in the folder. The connect statement connects the new ABCentralWidget slot to the signals provided by the "Items" COM object. Finally, it calls the updateOutlook function to populate the listview.
        // Get all items
        if ( defFolder ) {
            contactItems = defFolder->querySubObject( "Items" );
            connect( contactItems, SIGNAL(ItemAdd(IDispatch*)), this, SLOT(updateOutlook()) );
            connect( contactItems, SIGNAL(ItemChange(IDispatch*)), this, SLOT(updateOutlook()) );
            connect( contactItems, SIGNAL(ItemRemove()), this, SLOT(updateOutlook()) );
        }

        updateOutlook();
    }

The implementation of the updateOutlook slot clears the listview, and uses querySubObject to iterate through the list of items. For every item provided a new ABListViewItem object is created and filled with the properties of the item object. The object returned by querySubObject is a child of the callee (ie. "contactItems"), but the list view item should take ownership to provide a cleaner relation between entries, so the item has to be removed from its parent object.

    void ABCentralWidget::updateOutlook()
    {
        listView->clear();
        if ( !contactItems )
            return;

        TQAxObject *item = contactItems->querySubObject( "GetFirst()" );
        while ( item ) {
            TQString firstName = item->property( "FirstName" ).toString();
            TQString lastName = item->property( "LastName" ).toString();
            TQString address = item->property( "HomeAddress" ).toString();
            TQString email = item->property( "Email1Address" ).toString();

            (void)new ABListViewItem( listView, firstName, lastName, address, email, item );
            // the listviewitem takes ownership
            item->parent()->removeChild( item );

            item = contactItems->querySubObject( "GetNext()" );
        }
    }

The addEntry implementation calls the CreateItem method of the Outlook.Application object to create a new contact item, and creates a new ABListViewItem if the call succeeds.

    void ABCentralWidget::addEntry()
    {
        if ( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
             !iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) {
            TQAxObject *contactItem = outlook->querySubObject( "CreateItem(OlItemType)", "olContactItem" );
            if ( contactItem ) {
                contactItem->setProperty( "FirstName", iFirstName->text() );
                contactItem->setProperty( "LastName", iLastName->text() );
                contactItem->setProperty( "HomeAddress", iAddress->text() );
                contactItem->setProperty( "Email1Address", iEMail->text() );
                contactItem->dynamicCall( "Save()" );

                new ABListViewItem( listView, iFirstName->text(),
                    iLastName->text(), iAddress->text(), iEMail->text(), contactItem );
            }
        }

        iFirstName->setText( "" );
        iLastName->setText( "" );
        iAddress->setText( "" );
        iEMail->setText( "" );
    }

The changeEntry implementation updates the values in the contact item of the current listview item as well as the values of the listview item itself.

    void ABCentralWidget::changeEntry()
    {
        ABListViewItem *item = (ABListViewItem*)listView->currentItem();

        if ( item &&
             ( !iFirstName->text().isEmpty() || !iLastName->text().isEmpty() ||
               !iAddress->text().isEmpty() || !iEMail->text().isEmpty() ) ) {

            TQAxObject *contactItem = item->contactItem();
            contactItem->setProperty( "FirstName", iFirstName->text() );
            contactItem->setProperty( "LastName", iLastName->text() );
            contactItem->setProperty( "HomeAddress", iAddress->text() );
            contactItem->setProperty( "Email1Address", iEMail->text() );
            contactItem->dynamicCall( "Save()" );

            item->setText( 0, iFirstName->text() );
            item->setText( 1, iLastName->text() );
            item->setText( 2, iAddress->text() );
            item->setText( 3, iEMail->text() );
        }
    }

To build the example you must first build the TQAxContainer library. Then run your make tool in examples/qutlook and run the resulting qutlok.exe.

See also The TQAxContainer Examples.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8