emailservices/nmutilities/src/nmuieventsnotifier.cpp
changeset 65 478bc57ad291
equal deleted inserted replaced
59:16ed8d08d0b1 65:478bc57ad291
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 #include "nmuieventsnotifier.h"
       
    19 #include <QDataStream>
       
    20 #include <XQPublishAndSubscribeUtils>
       
    21 
       
    22 const long int NmSettingsKeyCategoryId(0x2002C326); // NMailAgent UID3
       
    23 const unsigned long int NmSettingsKeyUiEvents(0x00000100);
       
    24 const XQPublishAndSubscribeSettingsKey NmUiEventsKey(NmSettingsKeyCategoryId,
       
    25                                                      NmSettingsKeyUiEvents);
       
    26 
       
    27 #define NmUiEventTypeArrayType qint16
       
    28 #define NmUiViewIdArrayType qint16
       
    29 #define NmMailboxIdArrayType quint64
       
    30 
       
    31 const size_t NmUiEventByteArraySize(sizeof(NmUiEventTypeArrayType) +
       
    32                                     sizeof(NmUiViewIdArrayType) +
       
    33                                     sizeof(NmMailboxIdArrayType));
       
    34 
       
    35 
       
    36 /*!
       
    37     \class NmUiEventsNotifier
       
    38     \brief API class for notifying other processes about email UI events.
       
    39 */
       
    40 
       
    41 
       
    42 /*!
       
    43     Class constructor.
       
    44 */
       
    45 NmUiEventsNotifier::NmUiEventsNotifier(QObject *parent /* = NULL */)
       
    46 : QObject(parent)
       
    47 {
       
    48     XQPublishAndSubscribeUtils psUtils(mSettingsManager);
       
    49 
       
    50     // The following will only work if this instance is constructed inside the
       
    51     // nmailagent process since the category ID of the key is the UID3 of the
       
    52     // process. Other processes do not have the permission to define the
       
    53     // property.
       
    54     psUtils.defineProperty(NmUiEventsKey, XQSettingsManager::TypeByteArray);
       
    55 
       
    56     mSettingsManager.startMonitoring(NmUiEventsKey);
       
    57 
       
    58     connect(&mSettingsManager, SIGNAL(valueChanged(const XQSettingsKey &, const QVariant &)),
       
    59             this, SLOT(valueChanged(const XQSettingsKey &, const QVariant &)),
       
    60             Qt::UniqueConnection);
       
    61 }
       
    62 
       
    63 
       
    64 /*!
       
    65     Class destructor.
       
    66 */
       
    67 NmUiEventsNotifier::~NmUiEventsNotifier()
       
    68 {
       
    69     XQPublishAndSubscribeUtils psUtils(mSettingsManager);
       
    70     psUtils.deleteProperty(NmUiEventsKey);
       
    71 }
       
    72 
       
    73 
       
    74 /*!
       
    75     Notifies observers about view state changed event.
       
    76 
       
    77     \param eventType The type of state change.
       
    78     \param viewId The ID of the view which was shown.
       
    79     \param mailboxId The ID of the mailbox related to the view.
       
    80 
       
    81     \return True if success, false otherwise.
       
    82 */
       
    83 bool NmUiEventsNotifier::notifyViewStateChanged(const NmUiEventType eventType,
       
    84                                                 const NmUiViewId viewId,
       
    85                                                 const NmId &mailboxId)
       
    86 {
       
    87     QByteArray array;
       
    88     array.resize((int)NmUiEventByteArraySize);
       
    89     
       
    90     // Use a data stream to write the values into the array.
       
    91     QDataStream stream(&array, QIODevice::WriteOnly);
       
    92     stream << (NmUiEventTypeArrayType)eventType <<
       
    93               (NmUiViewIdArrayType)viewId <<
       
    94               (NmMailboxIdArrayType)mailboxId.id();
       
    95 
       
    96     XQSettingsManager settingsManager;
       
    97     return settingsManager.writeItemValue(NmUiEventsKey, array);
       
    98 }
       
    99 
       
   100 
       
   101 /*!
       
   102     Compares the two given keys.
       
   103 
       
   104     \param settingsKey XQSettingsKey
       
   105     \param psSettingsKey XQPublishAndSubscribeSettingsKey
       
   106 
       
   107     \return <code>true</code> if the target, uid and key values match, otherwise
       
   108             returns <code>false</code>
       
   109 */
       
   110 bool NmUiEventsNotifier::keysEqual(
       
   111     const XQSettingsKey& settingsKey,
       
   112     const XQPublishAndSubscribeSettingsKey& psSettingsKey) const
       
   113 {
       
   114     return ((settingsKey.target() == psSettingsKey.target()) &&
       
   115             (settingsKey.uid() == psSettingsKey.uid()) &&
       
   116             (settingsKey.key() == psSettingsKey.key()));
       
   117 }
       
   118 
       
   119 
       
   120 /*!
       
   121     Parses the value from the given byte array.
       
   122 
       
   123     \param array The array to parse.
       
   124     \param eventType Where the event type is stored to.
       
   125     \param viewId Where the view ID is stored to.
       
   126     \param mailboxId Where the mailbox ID is stored to.
       
   127 
       
   128     \return True if success, false otherwise.
       
   129 */
       
   130 bool NmUiEventsNotifier::parseKeyValue(const QByteArray &array,
       
   131                                        NmUiEventType &eventType,
       
   132                                        NmUiViewId &viewId,
       
   133                                        NmId &mailboxId) const
       
   134 {
       
   135     bool success(false);
       
   136 
       
   137     if (array.size() >= (int)NmUiEventByteArraySize) {
       
   138         NmUiEventTypeArrayType type(0);
       
   139         NmUiViewIdArrayType vId(0);
       
   140         NmMailboxIdArrayType mbId(0);
       
   141         
       
   142         // Use a data stream to read the values from the array.
       
   143         QDataStream stream(array);
       
   144         stream >> type >> vId >> mbId;
       
   145         
       
   146         // Convert the values into proper types.
       
   147         eventType = (NmUiEventType)type;
       
   148         viewId = (NmUiViewId)vId;
       
   149         mailboxId.setId(mbId);
       
   150 
       
   151         success = true;
       
   152     }
       
   153 
       
   154     return success;
       
   155 }
       
   156 
       
   157 
       
   158 /*!
       
   159     Handles the occured UI events. Processes the value changed events of
       
   160     settings keys.
       
   161 
       
   162     \param key The settings key of which value was changed.
       
   163     \param value The new value of the key. 
       
   164 */
       
   165 void NmUiEventsNotifier::valueChanged(const XQSettingsKey &key,
       
   166                                       const QVariant &value)
       
   167 {
       
   168     if (keysEqual(key, NmUiEventsKey)) {
       
   169         QByteArray array = value.toByteArray();
       
   170         
       
   171         NmUiEventType eventType(NmInvalidUiEvent);
       
   172         NmUiViewId viewId(NmUiViewNone);
       
   173         NmId mailboxId(0);
       
   174         
       
   175         if (parseKeyValue(array, eventType, viewId, mailboxId)) {
       
   176             emit viewStateChanged(eventType, viewId, mailboxId);
       
   177         }
       
   178     }
       
   179 }
       
   180 
       
   181 
       
   182 // End of file.