summaryrefslogtreecommitdiffstats
path: root/doc/html/qaxcontainer-example-qutlook.html
blob: fb9241f526ffdd139bd09e78f4f559dfbcd491a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/extensions/activeqt/examples/qutlook/qutlook.doc:1 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>In Sync with Outlook</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>In Sync with Outlook</h1>

   

This example is a modified version of the standard 
<a href="addressbook-example.html">Qt addressbook</a> example.
<p> It demonstrates the use of <a href="qaxobject.html">QAxObject</a> and querySubObject to instantiate and
navigate the Outlook Object Model, and the use of the Qt property system to 
read and write values of items in the Outlook contact folder.
<p> 

The modifications in the class declaration of the central widget are
a forward declaration of the QAxObject class and the IDispatch interface, 
and a new <a href="qlistviewitem.html">QListViewItem</a> subclass <tt>ABListViewItem</tt> that implements a 
constructor and a destructor and has a member <tt>contact_item</tt> of type 
<a href="qaxobject.html">QAxObject</a>.
<pre>    class QAxObject;
    struct IDispatch;

    class ABListViewItem : public <a href="qlistviewitem.html">QListViewItem</a>
    {
    public:
        ABListViewItem( <a href="qlistview.html">QListView</a> *listview, QString firstName, QString lastName, QString address, QString eMail, QAxObject *contact );
        ~ABListViewItem();

        <a href="qaxobject.html">QAxObject</a> *contactItem() const;

    private:
        <a href="qaxobject.html">QAxObject</a> *contact_item;
    };
</pre>
<p> The ABCentralWidget gets a destructor, a new protected function <tt>setupOutlook</tt>,
a new protected slot <tt>updateOutlook</tt>, and also three members of type <a href="qaxobject.html">QAxObject</a>.
<pre>        void findEntries();

        void updateOutlook();

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

        <a href="qaxobject.html">QAxObject</a> *outlook, *outlookSession, *contactItems;

        <a href="qgridlayout.html">QGridLayout</a> *mainGrid;
</pre>
<p> 

The implementation of the ABListViewItem class is trivial:
<pre>    ABListViewItem::ABListViewItem( <a href="qlistview.html">QListView</a> *listview,
                                    <a href="qstring.html">QString</a> firstName,
                                    <a href="qstring.html">QString</a> lastName,
                                    <a href="qstring.html">QString</a> address,
                                    <a href="qstring.html">QString</a> eMail,
                                    <a href="qaxobject.html">QAxObject</a> *contact )
    : <a href="qlistviewitem.html">QListViewItem</a>( listview, firstName, lastName, address, eMail ), contact_item( contact )
    {
    }

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

    QAxObject *ABListViewItem::contactItem() const
    {
        return contact_item;
    }
</pre>The ABCentralWidget constructor initializes the <a href="qaxobject.html">QAxObject</a> pointers to zero and 
calls the <tt>setupOutlook</tt> function. The ABCentralWidget destructor calls the
Logoff method of the outlookSession object.
<pre>    ABCentralWidget::ABCentralWidget( <a href="qwidget.html">QWidget</a> *parent, const char *name )
        : <a href="qwidget.html">QWidget</a>( parent, name ), outlook( 0 ), outlookSession( 0 ), contactItems( 0 )
    {
        mainGrid = new <a href="qgridlayout.html">QGridLayout</a>( this, 2, 1, 5, 5 );

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

    <a name="x2722"></a>    mainGrid-&gt;<a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 0, 0 );
        mainGrid-&gt;<a href="qgridlayout.html#setRowStretch">setRowStretch</a>( 1, 1 );
    }

    ABCentralWidget::~ABCentralWidget()
    {
        if ( outlookSession )
            outlookSession-&gt;dynamicCall( "Logoff()" );
    }
</pre>The <tt>setupOutlook</tt> implementation creates a QAxObject to wrap the
Outlook.Application COM object.
<pre>    void ABCentralWidget::setupOutlook()
    {
        outlook = new <a href="qaxobject.html">QAxObject</a>( "Outlook.Application", this );
</pre>The call to <tt>querySubObject</tt> returns a new <a href="qaxobject.html">QAxObject</a> 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.
<pre>        // Get a session object
    <a name="x2721"></a>    outlookSession = outlook-&gt;<a href="qaxbase.html#querySubObject">querySubObject</a>( "Session" );
        if ( !outlookSession )
            return;
        // Login; doesn't hurt if you are already running and logged on...
        outlookSession-&gt;dynamicCall( "Logon()" );
</pre>The following call to <tt>querySubObject</tt> returns a new QAxObject wrapper
around the default folder for "contacts".
<pre>        // Get the default folder for contacts
        <a href="qaxobject.html">QAxObject</a> *defFolder = outlookSession-&gt;querySubObject( "GetDefaultFolder(OlDefaultFolders)", "olFolderContacts" );
</pre><tt>querySubObject</tt> is then used again to get the list of all items in the
folder. The <tt>connect</tt> statement connects the new ABCentralWidget slot
to the signals provided by the "Items" COM object. Finally, it calls the
<tt>updateOutlook</tt> function to populate the listview.
<pre>        // Get all items
        if ( defFolder ) {
            contactItems = defFolder-&gt;<a href="qaxbase.html#querySubObject">querySubObject</a>( "Items" );
            <a href="qobject.html#connect">connect</a>( contactItems, SIGNAL(ItemAdd(IDispatch*)), this, SLOT(updateOutlook()) );
            <a href="qobject.html#connect">connect</a>( contactItems, SIGNAL(ItemChange(IDispatch*)), this, SLOT(updateOutlook()) );
            <a href="qobject.html#connect">connect</a>( contactItems, SIGNAL(ItemRemove()), this, SLOT(updateOutlook()) );
        }

        updateOutlook();
    }
</pre>
<p> The implementation of the <tt>updateOutlook</tt> slot clears the listview, and uses
<tt>querySubObject</tt> 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 <tt>querySubObject</tt> 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.
<pre>    void ABCentralWidget::updateOutlook()
    {
    <a name="x2725"></a>    listView-&gt;<a href="qlistview.html#clear">clear</a>();
        if ( !contactItems )
            return;

        <a href="qaxobject.html">QAxObject</a> *item = contactItems-&gt;querySubObject( "GetFirst()" );
        while ( item ) {
    <a name="x2729"></a>        <a href="qstring.html">QString</a> firstName = item-&gt;<a href="qobject.html#property">property</a>( "FirstName" ).toString();
            <a href="qstring.html">QString</a> lastName = item-&gt;<a href="qobject.html#property">property</a>( "LastName" ).toString();
            <a href="qstring.html">QString</a> address = item-&gt;<a href="qobject.html#property">property</a>( "HomeAddress" ).toString();
            <a href="qstring.html">QString</a> email = item-&gt;<a href="qobject.html#property">property</a>( "Email1Address" ).toString();

            (void)new ABListViewItem( listView, firstName, lastName, address, email, item );
            // the listviewitem takes ownership
    <a name="x2727"></a>        item-&gt;<a href="qlistviewitem.html#parent">parent</a>()-&gt;removeChild( item );

            item = contactItems-&gt;querySubObject( "GetNext()" );
        }
    }
</pre>
<p> The <tt>addEntry</tt> 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.
<pre>    void ABCentralWidget::addEntry()
    {
    <a name="x2724"></a>    if ( !iFirstName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ||
             !iAddress-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ) {
            <a href="qaxobject.html">QAxObject</a> *contactItem = outlook-&gt;<a href="qaxbase.html#querySubObject">querySubObject</a>( "CreateItem(OlItemType)", "olContactItem" );
            if ( contactItem ) {
    <a name="x2730"></a>            contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-&gt;<a href="qlineedit.html#text">text</a>() );
                contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "LastName", iLastName-&gt;<a href="qlineedit.html#text">text</a>() );
                contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-&gt;<a href="qlineedit.html#text">text</a>() );
                contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-&gt;<a href="qlineedit.html#text">text</a>() );
    <a name="x2720"></a>            contactItem-&gt;<a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" );

                new ABListViewItem( listView, iFirstName-&gt;<a href="qlineedit.html#text">text</a>(),
                    iLastName-&gt;<a href="qlineedit.html#text">text</a>(), iAddress-&gt;<a href="qlineedit.html#text">text</a>(), iEMail-&gt;<a href="qlineedit.html#text">text</a>(), contactItem );
            }
        }

    <a name="x2723"></a>    iFirstName-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
        iLastName-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
        iAddress-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
        iEMail-&gt;<a href="qlineedit.html#setText">setText</a>( "" );
    }
</pre>
<p> The <tt>changeEntry</tt> implementation updates the values in the contact item of the current
listview item as well as the values of the listview item itself.
<pre>    void ABCentralWidget::changeEntry()
    {
    <a name="x2726"></a>    ABListViewItem *item = (ABListViewItem*)listView-&gt;<a href="qlistview.html#currentItem">currentItem</a>();

        if ( item &amp;&amp;
             ( !iFirstName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iLastName-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ||
               !iAddress-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() || !iEMail-&gt;<a href="qlineedit.html#text">text</a>().isEmpty() ) ) {

            <a href="qaxobject.html">QAxObject</a> *contactItem = item-&gt;contactItem();
            contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "FirstName", iFirstName-&gt;<a href="qlineedit.html#text">text</a>() );
            contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "LastName", iLastName-&gt;<a href="qlineedit.html#text">text</a>() );
            contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "HomeAddress", iAddress-&gt;<a href="qlineedit.html#text">text</a>() );
            contactItem-&gt;<a href="qobject.html#setProperty">setProperty</a>( "Email1Address", iEMail-&gt;<a href="qlineedit.html#text">text</a>() );
            contactItem-&gt;<a href="qaxbase.html#dynamicCall">dynamicCall</a>( "Save()" );

    <a name="x2728"></a>        item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 0, iFirstName-&gt;<a href="qlineedit.html#text">text</a>() );
            item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 1, iLastName-&gt;<a href="qlineedit.html#text">text</a>() );
            item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 2, iAddress-&gt;<a href="qlineedit.html#text">text</a>() );
            item-&gt;<a href="qlistviewitem.html#setText">setText</a>( 3, iEMail-&gt;<a href="qlineedit.html#text">text</a>() );
        }
    }
</pre>
<p> To build the example you must first build the <a href="qaxcontainer.html">QAxContainer</a> 
library. Then run your make tool in <tt>examples/qutlook</tt> and run the resulting <tt>qutlok.exe</tt>.
<p>See also <a href="qaxcontainer-examples.html">The QAxContainer Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>