qtmobility/src/messaging/qmessageid_win.cpp
changeset 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
0:cfcbf08528c4 1:2b40d63a9c3d
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "qmessageid_p.h"
       
    42 #include <QByteArray>
       
    43 #include <QDataStream>
       
    44 #include <MAPIUtil.h>
       
    45 #include <QDebug>
       
    46 
       
    47 
       
    48 QTM_BEGIN_NAMESPACE
       
    49 
       
    50 #ifdef _WIN32_WCE
       
    51 
       
    52 QMessageId QMessageIdPrivate::from(const MapiEntryId &storeKey, const MapiEntryId &entryId, const MapiRecordKey &messageKey, const MapiEntryId &folderKey)
       
    53 {
       
    54     QMessageId result;
       
    55     if (!result.d_ptr)
       
    56         result.d_ptr = new QMessageIdPrivate(&result);
       
    57 
       
    58     result.d_ptr->_storeRecordKey = storeKey;
       
    59     result.d_ptr->_entryId = entryId;
       
    60     result.d_ptr->_folderRecordKey = folderKey;
       
    61     result.d_ptr->_messageRecordKey = messageKey;
       
    62 
       
    63     return result;
       
    64 }
       
    65 
       
    66 MapiEntryId QMessageIdPrivate::storeRecordKey(const QMessageId &id)
       
    67 {
       
    68     if (id.isValid()) {
       
    69         return id.d_ptr->_storeRecordKey;
       
    70     }
       
    71 
       
    72     return MapiEntryId();
       
    73 }
       
    74 
       
    75 MapiEntryId QMessageIdPrivate::folderRecordKey(const QMessageId &id)
       
    76 {
       
    77     if (id.isValid()) {
       
    78         if (!id.d_ptr->_folderRecordKey.isEmpty()) {
       
    79             return id.d_ptr->_folderRecordKey;
       
    80         } else {
       
    81             // Look up the folder record key for this ID
       
    82             QMessageManager::Error ignoredError(QMessageManager::NoError);
       
    83             MapiSessionPtr session(MapiSession::createSession(&ignoredError));
       
    84             if (ignoredError == QMessageManager::NoError) {
       
    85                 return session->folderEntryId(&ignoredError, id);
       
    86             }
       
    87         }
       
    88     }
       
    89 
       
    90     return MapiEntryId();
       
    91 }
       
    92 
       
    93 #else
       
    94 
       
    95 QMessageId QMessageIdPrivate::from(const MapiRecordKey &storeKey, const MapiEntryId &entryId, const MapiRecordKey &messageKey, const MapiRecordKey &folderKey)
       
    96 {
       
    97     QMessageId result;
       
    98     if (!result.d_ptr)
       
    99         result.d_ptr = new QMessageIdPrivate(&result);
       
   100 
       
   101     result.d_ptr->_storeRecordKey = storeKey;
       
   102     result.d_ptr->_entryId = entryId;
       
   103     result.d_ptr->_folderRecordKey = folderKey;
       
   104     result.d_ptr->_messageRecordKey = messageKey;
       
   105 
       
   106     return result;
       
   107 }
       
   108 
       
   109 MapiRecordKey QMessageIdPrivate::folderRecordKey(const QMessageId &id)
       
   110 {
       
   111     MapiRecordKey result;
       
   112 
       
   113     if (id.isValid()) {
       
   114         if (id.d_ptr->_folderRecordKey.isEmpty()) {
       
   115             // Look up the folder record key for this ID
       
   116             QMessageManager::Error ignoredError(QMessageManager::NoError);
       
   117             MapiSessionPtr session(MapiSession::createSession(&ignoredError));
       
   118             if (ignoredError == QMessageManager::NoError) {
       
   119                 id.d_ptr->_folderRecordKey = session->folderRecordKey(&ignoredError, id);
       
   120             }
       
   121         }
       
   122 
       
   123         result = id.d_ptr->_folderRecordKey;
       
   124     }
       
   125 
       
   126     return result;
       
   127 }
       
   128 
       
   129 MapiRecordKey QMessageIdPrivate::storeRecordKey(const QMessageId &id)
       
   130 {
       
   131     if (id.isValid()) {
       
   132         return id.d_ptr->_storeRecordKey;
       
   133     }
       
   134 
       
   135     return MapiRecordKey();
       
   136 }
       
   137 
       
   138 #endif
       
   139 
       
   140 MapiEntryId QMessageIdPrivate::entryId(const QMessageId &id)
       
   141 {
       
   142     if (id.isValid()) {
       
   143         return id.d_ptr->_entryId;
       
   144     }
       
   145 
       
   146     return MapiEntryId();
       
   147 }
       
   148 
       
   149 MapiRecordKey QMessageIdPrivate::messageRecordKey(const QMessageId &id)
       
   150 {
       
   151     MapiRecordKey result;
       
   152 
       
   153     if (id.isValid()) {
       
   154         if (id.d_ptr->_messageRecordKey.isEmpty()) {
       
   155             // Look up the message record key for this ID
       
   156             QMessageManager::Error ignoredError(QMessageManager::NoError);
       
   157             MapiSessionPtr session(MapiSession::createSession(&ignoredError));
       
   158             if (ignoredError == QMessageManager::NoError) {
       
   159                 id.d_ptr->_messageRecordKey = session->messageRecordKey(&ignoredError, id);
       
   160             }
       
   161         }
       
   162 
       
   163         result = id.d_ptr->_messageRecordKey;
       
   164     }
       
   165 
       
   166     return result;
       
   167 }
       
   168 
       
   169 QMessageId::QMessageId()
       
   170     : d_ptr(0)
       
   171 {
       
   172 }
       
   173 
       
   174 QMessageId::QMessageId(const QMessageId& other)
       
   175     : d_ptr(0)
       
   176 {
       
   177     this->operator=(other);
       
   178 }
       
   179 
       
   180 QMessageId::QMessageId(const QString& id)
       
   181     : d_ptr(new QMessageIdPrivate(this))
       
   182 {
       
   183     QDataStream idStream(QByteArray::fromBase64(id.toLatin1()));
       
   184 
       
   185 #ifdef _WIN32_WCE
       
   186     idStream >> d_ptr->_entryId;
       
   187 #else
       
   188     idStream >> d_ptr->_messageRecordKey;
       
   189 #endif
       
   190 
       
   191     idStream >> d_ptr->_folderRecordKey;
       
   192     idStream >> d_ptr->_storeRecordKey;
       
   193 
       
   194 #ifndef _WIN32_WCE
       
   195     QMessageManager::Error ignoredError(QMessageManager::NoError);
       
   196     MapiSessionPtr session(MapiSession::createSession(&ignoredError));
       
   197     if (ignoredError == QMessageManager::NoError) {
       
   198         d_ptr->_entryId = session->messageEntryId(&ignoredError, d_ptr->_storeRecordKey, d_ptr->_folderRecordKey, d_ptr->_messageRecordKey);
       
   199     }
       
   200 #endif
       
   201 }
       
   202 
       
   203 QMessageId::~QMessageId()
       
   204 {
       
   205     delete d_ptr;
       
   206 }
       
   207 
       
   208 QMessageId& QMessageId::operator=(const QMessageId& other)
       
   209 {
       
   210     if (&other != this) {
       
   211         if (other.isValid()) {
       
   212             if (!d_ptr) {
       
   213                 d_ptr = new QMessageIdPrivate(this);
       
   214             }
       
   215             d_ptr->_messageRecordKey = other.d_ptr->_messageRecordKey;
       
   216             d_ptr->_folderRecordKey = other.d_ptr->_folderRecordKey;
       
   217             d_ptr->_storeRecordKey = other.d_ptr->_storeRecordKey;
       
   218             d_ptr->_entryId = other.d_ptr->_entryId;
       
   219         } else {
       
   220             delete d_ptr;
       
   221             d_ptr = 0;
       
   222         }
       
   223     }
       
   224 
       
   225     return *this;
       
   226 }
       
   227 
       
   228 bool QMessageId::operator==(const QMessageId& other) const
       
   229 {
       
   230     if (isValid()) {
       
   231         if (other.isValid()) {
       
   232             bool result(d_ptr->_storeRecordKey == other.d_ptr->_storeRecordKey);
       
   233                 if (result) {
       
   234                     QMessageManager::Error ignoredError(QMessageManager::NoError);
       
   235                     MapiSessionPtr session(MapiSession::createSession(&ignoredError));
       
   236                     if (ignoredError == QMessageManager::NoError) {
       
   237                         result &= session->equal(d_ptr->_entryId, other.d_ptr->_entryId);
       
   238                     } else {
       
   239                         result = false;
       
   240                         qWarning() << "Unable to compare entry IDs.";
       
   241                     }
       
   242                 }
       
   243             return result;
       
   244         }
       
   245         return false;
       
   246     } else {
       
   247         return !other.isValid();
       
   248     }
       
   249 }
       
   250 
       
   251 bool QMessageId::operator<(const QMessageId& other) const
       
   252 {
       
   253     if (d_ptr && other.d_ptr) {
       
   254         if (operator==(other)) {
       
   255             return false;
       
   256         }
       
   257 
       
   258         return (d_ptr->_entryId < other.d_ptr->_entryId);
       
   259     }
       
   260 
       
   261     if (d_ptr) {
       
   262         return false;
       
   263     } else if (other.d_ptr) {
       
   264         return true;
       
   265     }
       
   266 
       
   267     return false;
       
   268 }
       
   269 
       
   270 QString QMessageId::toString() const
       
   271 {
       
   272     if (!isValid())
       
   273         return QString();
       
   274 
       
   275 #ifndef _WIN32_WCE
       
   276     if (d_ptr->_messageRecordKey.isEmpty())
       
   277         d_ptr->_messageRecordKey = QMessageIdPrivate::messageRecordKey(*this);
       
   278 #endif
       
   279 
       
   280     if (d_ptr->_folderRecordKey.isEmpty())
       
   281         d_ptr->_folderRecordKey = QMessageIdPrivate::folderRecordKey(*this);
       
   282 
       
   283     QByteArray encodedId;
       
   284     QDataStream encodedIdStream(&encodedId, QIODevice::WriteOnly);
       
   285 #ifdef _WIN32_WCE
       
   286     encodedIdStream << d_ptr->_entryId;
       
   287 #else
       
   288     encodedIdStream << d_ptr->_messageRecordKey;
       
   289 #endif
       
   290     encodedIdStream << d_ptr->_folderRecordKey;
       
   291     encodedIdStream << d_ptr->_storeRecordKey;
       
   292 
       
   293     return encodedId.toBase64();
       
   294 }
       
   295 
       
   296 bool QMessageId::isValid() const
       
   297 {
       
   298     return (d_ptr && !d_ptr->_storeRecordKey.isEmpty() && !d_ptr->_entryId.isEmpty());
       
   299 }
       
   300 
       
   301 uint qHash(const QMessageId &id)
       
   302 {
       
   303     return qHash(id.d_ptr->_storeRecordKey) ^ qHash(id.d_ptr->_entryId);
       
   304 }
       
   305 
       
   306 
       
   307 QTM_END_NAMESPACE