PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/qtopiacore/qt/doc/html/activeqt-qutlook.html

https://github.com/muromec/qtopia-ezx
HTML | 319 lines | 269 code | 49 blank | 1 comment | 0 complexity | 9d426d383f88f7cd020fd23c098ff0f6 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, AGPL-1.0, LGPL-2.1, BSD-3-Clause
  1. <?xml version="1.0" encoding="iso-8859-1"?>
  2. <!DOCTYPE html
  3. PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  5. <!-- /home/test1/tmp/qtopia/qt//qtopia-core-opensource-src-4.3.6/doc/src/examples/activeqt/qutlook.qdoc -->
  6. <head>
  7. <title>Qt 4.3: Qutlook Example (ActiveQt)</title>
  8. <link href="classic.css" rel="stylesheet" type="text/css" />
  9. </head>
  10. <body>
  11. <table border="0" cellpadding="0" cellspacing="0" width="100%">
  12. <tr>
  13. <td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
  14. <td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
  15. <td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 class="title">Qutlook Example (ActiveQt)<br /><span class="subtitle"></span>
  16. </h1>
  17. <p>Files:</p>
  18. <ul>
  19. <li><a href="activeqt-qutlook-addressview-cpp.html">activeqt/qutlook/addressview.cpp</a></li>
  20. <li><a href="activeqt-qutlook-addressview-h.html">activeqt/qutlook/addressview.h</a></li>
  21. <li><a href="activeqt-qutlook-main-cpp.html">activeqt/qutlook/main.cpp</a></li>
  22. <li><a href="activeqt-qutlook-qutlook-pro.html">activeqt/qutlook/qutlook.pro</a></li>
  23. </ul>
  24. <p>The Qutlook example demonstrates the use of <a href="activeqt.html#activeqt">ActiveQt</a> to automate Outlook. The example makes use of the <a href="activeqt-dumpcpp.html#dumpcpp">dumpcpp</a> tool to generate a C++ namespace for the type library describing the Outlook Object Model.</p>
  25. <p>The project file for the example looks like this:</p>
  26. <pre> TEMPLATE = app
  27. TARGET = qutlook
  28. CONFIG += qaxcontainer
  29. TYPELIBS = $$system(dumpcpp -getfile {00062FFF-0000-0000-C000-000000000046})
  30. isEmpty(TYPELIBS) {
  31. message(&quot;Microsoft Outlook type library not found!&quot;)
  32. REQUIRES += Outlook
  33. } else {
  34. HEADERS = addressview.h
  35. SOURCES = addressview.cpp main.cpp
  36. }</pre>
  37. <p>The project file uses the <tt>dumpcpp</tt> tool to add an MS Outlook type library to the project. If this fails, then the generated makefile will just print an error message, otherwise the build step will now run the <i>dumpcpp</i> tool on the type library, and generate a header and a cpp file (in this case, <tt>msoutl.h</tt> and <tt>msoutl.cpp</tt>) that declares and implement an easy to use API to the Outlook objects.</p>
  38. <pre> class AddressView : public QWidget
  39. {
  40. Q_OBJECT
  41. public:
  42. AddressView(QWidget *parent = 0);
  43. protected slots:
  44. void addEntry();
  45. void changeEntry();
  46. void itemSelected(const QModelIndex &amp;index);
  47. void updateOutlook();
  48. protected:
  49. AddressBookModel *model;
  50. QTreeView *treeView;
  51. QPushButton *add, *change;
  52. QLineEdit *iFirstName, *iLastName, *iAddress, *iEMail;
  53. };</pre>
  54. <p>The AddressView class is a <a href="qwidget.html">QWidget</a> subclass for the user interface. The <a href="qtreeview.html">QTreeView</a> widget will display the contents of Outlook's Contact folder as provided by the <tt>model</tt>.</p>
  55. <pre> #include &quot;addressview.h&quot;
  56. #include &quot;msoutl.h&quot;
  57. #include &lt;QtGui&gt;
  58. class AddressBookModel : public QAbstractListModel
  59. {
  60. public:
  61. AddressBookModel(AddressView *parent);
  62. ~AddressBookModel();
  63. int rowCount(const QModelIndex &amp;parent = QModelIndex()) const;
  64. int columnCount(const QModelIndex &amp;parent) const;
  65. QVariant headerData(int section, Qt::Orientation orientation, int role) const;
  66. QVariant data(const QModelIndex &amp;index, int role) const;
  67. void changeItem(const QModelIndex &amp;index, const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email);
  68. void addItem(const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email);
  69. void update();
  70. private:
  71. Outlook::Application outlook;
  72. Outlook::Items * contactItems;
  73. mutable QHash&lt;QModelIndex, QStringList&gt; cache;
  74. };</pre>
  75. <p>The AddressBookModel class is a <a href="qabstractlistmodel.html">QAbstractListModel</a> subclass that communicates directly with Outlook, using a <a href="qhash.html">QHash</a> for caching.</p>
  76. <pre> AddressBookModel::AddressBookModel(AddressView *parent)
  77. : QAbstractListModel(parent)
  78. {
  79. if (!outlook.isNull()) {
  80. Outlook::NameSpace session(outlook.Session());
  81. session.Logon();
  82. Outlook::MAPIFolder *folder = session.GetDefaultFolder(Outlook::olFolderContacts);
  83. contactItems = new Outlook::Items(folder-&gt;Items());
  84. connect(contactItems, SIGNAL(ItemAdd(IDispatch*)), parent, SLOT(updateOutlook()));
  85. connect(contactItems, SIGNAL(ItemChange(IDispatch*)), parent, SLOT(updateOutlook()));
  86. connect(contactItems, SIGNAL(ItemRemove()), parent, SLOT(updateOutlook()));
  87. delete folder;
  88. }
  89. }</pre>
  90. <p>The constructor initializes Outlook. The various signals Outlook provides to notify about contents changes are connected to the <tt>updateOutlook()</tt> slot.</p>
  91. <pre> AddressBookModel::~AddressBookModel()
  92. {
  93. delete contactItems;
  94. if (!outlook.isNull())
  95. Outlook::NameSpace(outlook.Session()).Logoff();
  96. }</pre>
  97. <p>The destructor logs off from the session.</p>
  98. <pre> int AddressBookModel::rowCount(const QModelIndex &amp;) const
  99. {
  100. return contactItems ? contactItems-&gt;Count() : 0;
  101. }
  102. int AddressBookModel::columnCount(const QModelIndex &amp;parent) const
  103. {
  104. return 4;
  105. }</pre>
  106. <p>The <tt>rowCount()</tt> implementation returns the number of entries as reported by Outlook. <tt>columnCount</tt> and <tt>headerData</tt> are implemented to show four columns in the tree view.</p>
  107. <pre> QVariant AddressBookModel::headerData(int section, Qt::Orientation orientation, int role) const
  108. {
  109. if (role != Qt::DisplayRole)
  110. return QVariant();
  111. switch (section) {
  112. case 0:
  113. return tr(&quot;First Name&quot;);
  114. case 1:
  115. return tr(&quot;Last Name&quot;);
  116. case 2:
  117. return tr(&quot;Address&quot;);
  118. case 3:
  119. return tr(&quot;Email&quot;);
  120. default:
  121. break;
  122. }
  123. return QVariant();
  124. }</pre>
  125. <p>The <tt>headerData()</tt> implementation returns hardcoded strings.</p>
  126. <pre> QVariant AddressBookModel::data(const QModelIndex &amp;index, int role) const
  127. {
  128. if (!index.isValid() || role != Qt::DisplayRole)
  129. return QVariant();
  130. QStringList data;
  131. if (cache.contains(index)) {
  132. data = cache.value(index);
  133. } else {
  134. Outlook::ContactItem contact(contactItems-&gt;Item(index.row() + 1));
  135. data &lt;&lt; contact.FirstName() &lt;&lt; contact.LastName() &lt;&lt; contact.HomeAddress() &lt;&lt; contact.Email1Address();
  136. cache.insert(index, data);
  137. }
  138. if (index.column() &lt; data.count())
  139. return data.at(index.column());
  140. return QVariant();
  141. }</pre>
  142. <p>The <tt>data()</tt> implementation is the core of the model. If the requested data is in the cache the cached value is used, otherwise the data is acquired from Outlook.</p>
  143. <pre> void AddressBookModel::changeItem(const QModelIndex &amp;index, const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email)
  144. {
  145. Outlook::ContactItem item(contactItems-&gt;Item(index.row() + 1));
  146. item.SetFirstName(firstName);
  147. item.SetLastName(lastName);
  148. item.SetHomeAddress(address);
  149. item.SetEmail1Address(email);
  150. item.Save();
  151. cache.take(index);
  152. }</pre>
  153. <p>The <tt>changeItem()</tt> slot is called when the user changes the current entry using the user interface. The Outlook item is accessed using the Outlook API, and is modified using the property setters. Finally, the item is saved to Outlook, and removed from the cache. Note that the model does not signal the view of the data change, as Outlook will emit a signal on it's own.</p>
  154. <pre> void AddressBookModel::addItem(const QString &amp;firstName, const QString &amp;lastName, const QString &amp;address, const QString &amp;email)
  155. {
  156. Outlook::ContactItem item(outlook.CreateItem(Outlook::olContactItem));
  157. if (!item.isNull()) {
  158. item.SetFirstName(firstName);
  159. item.SetLastName(lastName);
  160. item.SetHomeAddress(address);
  161. item.SetEmail1Address(email);
  162. item.Save();
  163. }
  164. }</pre>
  165. <p>The <tt>addItem()</tt> slot calls the CreateItem method of Outlook to create a new contact item, sets the properties of the new item to the values entered by the user and saves the item.</p>
  166. <pre> void AddressBookModel::update()
  167. {
  168. cache.clear();
  169. emit reset();
  170. }</pre>
  171. <p>The <tt>update()</tt> slot clears the cache, and emits the <a href="qtextstream.html#reset">reset</a>() signal to notify the view about the data change requiring a redraw of the contents.</p>
  172. <pre> AddressView::AddressView(QWidget *parent)
  173. : QWidget(parent)
  174. {
  175. QGridLayout *mainGrid = new QGridLayout(this);
  176. QLabel *liFirstName = new QLabel(&quot;First &amp;Name&quot;, this);
  177. liFirstName-&gt;resize(liFirstName-&gt;sizeHint());
  178. mainGrid-&gt;addWidget(liFirstName, 0, 0);
  179. QLabel *liLastName = new QLabel(&quot;&amp;Last Name&quot;, this);
  180. liLastName-&gt;resize(liLastName-&gt;sizeHint());
  181. mainGrid-&gt;addWidget(liLastName, 0, 1);
  182. QLabel *liAddress = new QLabel(&quot;Add&amp;ress&quot;, this);
  183. liAddress-&gt;resize(liAddress-&gt;sizeHint());
  184. mainGrid-&gt;addWidget(liAddress, 0, 2);
  185. QLabel *liEMail = new QLabel(&quot;&amp;E-Mail&quot;, this);
  186. liEMail-&gt;resize(liEMail-&gt;sizeHint());
  187. mainGrid-&gt;addWidget(liEMail, 0, 3);
  188. add = new QPushButton(&quot;A&amp;dd&quot;, this);
  189. add-&gt;resize(add-&gt;sizeHint());
  190. mainGrid-&gt;addWidget(add, 0, 4);
  191. connect(add, SIGNAL(clicked()), this, SLOT(addEntry()));
  192. iFirstName = new QLineEdit(this);
  193. iFirstName-&gt;resize(iFirstName-&gt;sizeHint());
  194. mainGrid-&gt;addWidget(iFirstName, 1, 0);
  195. liFirstName-&gt;setBuddy(iFirstName);
  196. iLastName = new QLineEdit(this);
  197. iLastName-&gt;resize(iLastName-&gt;sizeHint());
  198. mainGrid-&gt;addWidget(iLastName, 1, 1);
  199. liLastName-&gt;setBuddy(iLastName);
  200. iAddress = new QLineEdit(this);
  201. iAddress-&gt;resize(iAddress-&gt;sizeHint());
  202. mainGrid-&gt;addWidget(iAddress, 1, 2);
  203. liAddress-&gt;setBuddy(iAddress);
  204. iEMail = new QLineEdit(this);
  205. iEMail-&gt;resize(iEMail-&gt;sizeHint());
  206. mainGrid-&gt;addWidget(iEMail, 1, 3);
  207. liEMail-&gt;setBuddy(iEMail);
  208. change = new QPushButton(&quot;&amp;Change&quot;, this);
  209. change-&gt;resize(change-&gt;sizeHint());
  210. mainGrid-&gt;addWidget(change, 1, 4);
  211. connect(change, SIGNAL(clicked()), this, SLOT(changeEntry()));
  212. treeView = new QTreeView(this);
  213. treeView-&gt;setSelectionMode(QTreeView::SingleSelection);
  214. treeView-&gt;setRootIsDecorated(false);
  215. model = new AddressBookModel(this);
  216. treeView-&gt;setModel(model);
  217. connect(treeView-&gt;selectionModel(), SIGNAL(currentChanged(QModelIndex, QModelIndex)), this, SLOT(itemSelected(QModelIndex)));
  218. mainGrid-&gt;addWidget(treeView, 2, 0, 1, 5);
  219. }
  220. void AddressView::updateOutlook()
  221. {
  222. model-&gt;update();
  223. }
  224. void AddressView::addEntry()
  225. {
  226. if (!iFirstName-&gt;text().isEmpty() || !iLastName-&gt;text().isEmpty() ||
  227. !iAddress-&gt;text().isEmpty() || !iEMail-&gt;text().isEmpty()) {
  228. model-&gt;addItem(iFirstName-&gt;text(), iFirstName-&gt;text(), iAddress-&gt;text(), iEMail-&gt;text());
  229. }
  230. iFirstName-&gt;setText(&quot;&quot;);
  231. iLastName-&gt;setText(&quot;&quot;);
  232. iAddress-&gt;setText(&quot;&quot;);
  233. iEMail-&gt;setText(&quot;&quot;);
  234. }
  235. void AddressView::changeEntry()
  236. {
  237. QModelIndex current = treeView-&gt;currentIndex();
  238. if (current.isValid())
  239. model-&gt;changeItem(current, iFirstName-&gt;text(), iLastName-&gt;text(), iAddress-&gt;text(), iEMail-&gt;text());
  240. }
  241. void AddressView::itemSelected(const QModelIndex &amp;index)
  242. {
  243. if (!index.isValid())
  244. return;
  245. QAbstractItemModel *model = treeView-&gt;model();
  246. iFirstName-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 0)).toString());
  247. iLastName-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 1)).toString());
  248. iAddress-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 2)).toString());
  249. iEMail-&gt;setText(model-&gt;data(model-&gt;index(index.row(), 3)).toString());
  250. }</pre>
  251. <p>The rest of the file implements the user interface using only Qt APIs, i.e&#x2e; without communicating with Outlook directly.</p>
  252. <pre> #include &quot;addressview.h&quot;
  253. #include &lt;QApplication&gt;
  254. int main(int argc, char ** argv)
  255. {
  256. QApplication a(argc, argv);
  257. AddressView view;
  258. view.setWindowTitle(&quot;Qt Example - Looking at Outlook&quot;);
  259. view.show();
  260. return a.exec();
  261. }</pre>
  262. <p>The <tt>main()</tt> entry point function finally instantiates the user interface and enters the event loop.</p>
  263. <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/activeqt/qutlook</tt> and run the resulting <tt>qutlook.exe</tt>.</p>
  264. <p /><address><hr /><div align="center">
  265. <table width="100%" cellspacing="0" border="0"><tr class="address">
  266. <td width="30%">Copyright &copy; 2008 <a href="trolltech.html">Trolltech</a></td>
  267. <td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
  268. <td width="30%" align="right"><div align="right">Qt 4.3.6</div></td>
  269. </tr></table></div></address></body>
  270. </html>