contentstorage/caclient/src/canotifier.cpp
changeset 60 f62f87b200ec
child 73 4bc7b118b3df
equal deleted inserted replaced
4:1a2a00e78665 60:f62f87b200ec
       
     1 /*
       
     2  * Copyright (c) 2009 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: canotifier.cpp
       
    15  *
       
    16  */
       
    17 
       
    18 #include "canotifier.h"
       
    19 #include "canotifier_p.h"
       
    20 #include "canotifierfilter.h"
       
    21 #include "caclientnotifierproxy.h"
       
    22 
       
    23 /*!
       
    24  \class CaNotifier.
       
    25  \brief This class describes notifier.
       
    26 
       
    27  \example
       
    28  It's example how client can be notified about changes on data specified by client's filter
       
    29 
       
    30  \code
       
    31  // example Client class which wants to be notified about changes
       
    32 
       
    33  class Client: public QObject
       
    34  {
       
    35  Q_OBJECT
       
    36  ...
       
    37  public slots:
       
    38 
       
    39  updateModelForEntryChanged(int,ChangeType);
       
    40  updateModelForEntryChanged(const CaEntry &,ChangeType);
       
    41  updateModelForEntryTouched(int);
       
    42  updateModelForgroupContentChanged(int);
       
    43  ...
       
    44  };
       
    45  \endcode
       
    46 
       
    47  \code
       
    48  // example code showed how to register for notifications
       
    49  Client * client;
       
    50  ...
       
    51  QSharedPointer<CaService> service = CaService::instance();
       
    52  CaNotifierFilter notifierFilter();
       
    53  CaNotifier * notifier = service->createNotifier(notifierfilter);
       
    54 
       
    55  // Connections cause that notifier is registered to server distributed
       
    56  // notifications while data changes.
       
    57  if( notifier )
       
    58  {
       
    59  connect( notifier, SIGNAL(entryChanged(int,ChangeType)),
       
    60  client, SLOT(updateModelForEntryChanged(int,ChangeType)) );
       
    61  connect( notifier, SIGNAL(entryChanged(CaEntry,ChangeType)),
       
    62  client, SLOT(updateModelForEntryChanged(CaEntry,ChangeType)) );
       
    63  connect( notifier, SIGNAL(entryTouched(int)),
       
    64  client, SLOT(updateModelForEntryTouched(int)) );
       
    65  connect( notifier, SIGNAL(groupContentChanged(int)),
       
    66  client, SLOT(updateModelForgroupContentChanged(int)) );
       
    67  }
       
    68  ...
       
    69  // creation new entry causes sent notification to the client
       
    70  CaEntry entry;
       
    71  entry.setText( "Text" );
       
    72  entry.setTypeName( "TypeName" );
       
    73  CaEntry * newEntry = service->createEntry( entry );
       
    74  ...
       
    75  \endcode
       
    76 
       
    77  \code
       
    78  // here client is being get notifications new entry was created
       
    79  ...
       
    80  Client::updateModelForEntryChanged(int entryId ,ChangeType changeType)
       
    81  {
       
    82  ...
       
    83  changeType == AddChangeType;
       
    84  ...
       
    85  }
       
    86 
       
    87  \endcode
       
    88 
       
    89  */
       
    90 
       
    91 /*!
       
    92  Constructor.
       
    93  \param entryPrivate pointer to private implementation.
       
    94  */
       
    95 CaNotifier::CaNotifier(CaNotifierPrivate *const notifierPrivate) :
       
    96     QObject(0), m_d(notifierPrivate)
       
    97 {
       
    98     m_d->m_q = this;
       
    99     m_d->makeConnect();
       
   100 }
       
   101 
       
   102 /*!
       
   103  Destructor.
       
   104  */
       
   105 CaNotifier::~CaNotifier()
       
   106 {
       
   107     m_d->makeDisconnect();
       
   108     delete m_d;
       
   109 }
       
   110 
       
   111 /*!
       
   112     This method is called when something has been
       
   113     connected to signal in this object.
       
   114     \param signal which is used to register an appropriate notifier.
       
   115 */
       
   116 void CaNotifier::connectNotify(const char *signal)
       
   117 {
       
   118     qDebug("CaNotifier::connectNotify");
       
   119     qDebug("\tsignal: %s", signal);
       
   120     if (QLatin1String(signal)
       
   121             == SIGNAL(entryChanged(int,ChangeType))) {
       
   122         // signal is entryChanged(int, ChangeType)
       
   123         if (receivers(SIGNAL(entryChanged(int,ChangeType))) == 1) {
       
   124             m_d->registerNotifier(
       
   125                 CaNotifierPrivate::EntryChangedWithIdNotifierType);
       
   126         }
       
   127     } else if (QLatin1String(signal)
       
   128                == SIGNAL(entryChanged(CaEntry,ChangeType))) {
       
   129         // signal is entryChanged(const CaEntry &, ChangeType)
       
   130         if (receivers(SIGNAL(entryChanged(const CaEntry &,ChangeType))) == 1) {
       
   131             m_d->registerNotifier(
       
   132                 CaNotifierPrivate::EntryChangedWithEntryNotifierType);
       
   133         }
       
   134     } else if (QLatin1String(signal)
       
   135                == SIGNAL(entryTouched(int))) {
       
   136         // signal is entryTouched(int)
       
   137         if (receivers(SIGNAL(entryTouched(int)))) {
       
   138             m_d->registerNotifier(
       
   139                 CaNotifierPrivate::EntryTouchedNotifierType);
       
   140         }
       
   141     } else if (QLatin1String(signal)
       
   142                == SIGNAL(groupContentChanged(int))) {
       
   143         // signal is groupContentChanged(int)
       
   144         if (receivers(SIGNAL(groupContentChanged(int)))) {
       
   145             m_d->registerNotifier(
       
   146                 CaNotifierPrivate::GroupContentChangedNotifierType);
       
   147         }
       
   148     }
       
   149 }
       
   150 
       
   151 /*!
       
   152     This method is called when something has been
       
   153     disconnected from signal in this object.
       
   154     \param signal which is used to unregister an appropriate notifier.
       
   155 */
       
   156 void CaNotifier::disconnectNotify(const char *signal)
       
   157 {
       
   158     qDebug("CaNotifier::disconnectNotify");
       
   159     qDebug("\tsignal: %s", signal);
       
   160     if (QLatin1String(signal)
       
   161             == SIGNAL(entryChanged(int,ChangeType))) {
       
   162         // signal is entryChanged(int, ChangeType)
       
   163         if (receivers(SIGNAL(entryChanged(int,ChangeType)))==0) {
       
   164             m_d->unregisterNotifier(
       
   165                 CaNotifierPrivate::EntryChangedWithIdNotifierType);
       
   166         }
       
   167     } else if (QLatin1String(signal)
       
   168                == SIGNAL(entryChanged(CaEntry,ChangeType))) {
       
   169         // signal is entryChanged(const CaEntry &, ChangeType)
       
   170         if (receivers(SIGNAL(entryChanged(const CaEntry &,ChangeType)))==0) {
       
   171             m_d->unregisterNotifier(
       
   172                 CaNotifierPrivate::EntryChangedWithEntryNotifierType);
       
   173         }
       
   174     } else if (QLatin1String(signal)
       
   175                == SIGNAL(entryTouched(int))) {
       
   176         // signal is entryTouched(int)
       
   177         if (receivers(SIGNAL(entryTouched(int))) == 0) {
       
   178             m_d->unregisterNotifier(
       
   179                 CaNotifierPrivate::EntryTouchedNotifierType);
       
   180         }
       
   181     } else if (QLatin1String(signal)
       
   182                == SIGNAL(groupContentChanged(int))) {
       
   183         // signal is groupContentChanged(int)
       
   184         if (receivers(SIGNAL(groupContentChanged(int))) == 0) {
       
   185             m_d->unregisterNotifier(
       
   186                 CaNotifierPrivate::GroupContentChangedNotifierType);
       
   187         }
       
   188     }
       
   189 }
       
   190 
       
   191 /*!
       
   192  Constructor.
       
   193  \param notifierFilter descrbies entries to observe.
       
   194  */
       
   195 CaNotifierPrivate::CaNotifierPrivate(
       
   196     const CaNotifierFilter &notifierFilter) :
       
   197     m_q(NULL),
       
   198     mNotifierFilter(NULL),
       
   199     mNotifierProxy(NULL)
       
   200 {
       
   201     mNotifierFilter = new CaNotifierFilter(notifierFilter);
       
   202 }
       
   203 
       
   204 /*!
       
   205  Destructor.
       
   206  */
       
   207 CaNotifierPrivate::~CaNotifierPrivate()
       
   208 {
       
   209     if (mNotifierProxy) {
       
   210         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   211                                            EntryChangedWithIdNotifierType);
       
   212         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   213                                            EntryChangedWithEntryNotifierType);
       
   214         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   215                                            EntryTouchedNotifierType);
       
   216         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   217                                            GroupContentChangedNotifierType);
       
   218     }
       
   219     delete mNotifierFilter;
       
   220     delete mNotifierProxy;
       
   221 }
       
   222 /*!
       
   223  Registers notifier
       
   224  \param notifierType type of notifier to register (class of events to listen)
       
   225  */
       
   226 int CaNotifierPrivate::registerNotifier(NotifierType notifierType)
       
   227 {
       
   228     if (mNotifierProxy) {
       
   229         return mNotifierProxy->registerNotifier(
       
   230                    mNotifierFilter,
       
   231                    notifierType,
       
   232                    mNotifierProxy);
       
   233     }
       
   234     return 0;
       
   235 }
       
   236 /*!
       
   237  Unregisters notifier
       
   238  \param notifierType type of notifier event type to unregister
       
   239      (class of events to listen)
       
   240  */
       
   241 void CaNotifierPrivate::unregisterNotifier(NotifierType notifierType)
       
   242 {
       
   243     if (mNotifierProxy) {
       
   244         mNotifierProxy->unregisterNotifier(*mNotifierFilter, notifierType);
       
   245     }
       
   246 }
       
   247 
       
   248 /*!
       
   249  Connects this Notifier
       
   250  */
       
   251 void CaNotifierPrivate::makeConnect()
       
   252 {
       
   253     if (m_q) {
       
   254         if (!mNotifierProxy) {
       
   255             mNotifierProxy = new CaClientNotifierProxy();
       
   256         }
       
   257         m_q->connect(mNotifierProxy,
       
   258                      SIGNAL(signalEntryChanged(int,ChangeType)),
       
   259                      m_q,
       
   260                      SIGNAL(entryChanged(int,ChangeType)));
       
   261         m_q->connect(mNotifierProxy,
       
   262                      SIGNAL(signalEntryChanged(const CaEntry &,ChangeType)),
       
   263                      m_q,
       
   264                      SIGNAL(entryChanged(const CaEntry &,ChangeType)));
       
   265         m_q->connect(mNotifierProxy,
       
   266                      SIGNAL(signalEntryTouched(int)),
       
   267                      m_q,
       
   268                      SIGNAL(entryTouched(int)));
       
   269         m_q->connect(mNotifierProxy,
       
   270                      SIGNAL(signalGroupContentChanged(int)),
       
   271                      m_q,
       
   272                      SIGNAL(groupContentChanged(int)));
       
   273     }
       
   274 }
       
   275 /*!
       
   276  Disconnects this Notifier
       
   277  */
       
   278 void CaNotifierPrivate::makeDisconnect()
       
   279 {
       
   280     if (m_q && mNotifierProxy) {
       
   281         m_q->disconnect(mNotifierProxy,
       
   282                         SIGNAL(signalEntryChanged(int,ChangeType)),
       
   283                         m_q,
       
   284                         SIGNAL(entryChanged(int,ChangeType)));
       
   285         m_q->disconnect(mNotifierProxy,
       
   286                         SIGNAL(signalEntryChanged(const CaEntry &,ChangeType)),
       
   287                         m_q,
       
   288                         SIGNAL(entryChanged(const CaEntry &,ChangeType)));
       
   289         m_q->disconnect(mNotifierProxy,
       
   290                         SIGNAL(signalEntryTouched(int)),
       
   291                         m_q,
       
   292                         SIGNAL(entryTouched(int)));
       
   293         m_q->disconnect(mNotifierProxy,
       
   294                         SIGNAL(signalGroupContentChanged(int)),
       
   295                         m_q,
       
   296                         SIGNAL(groupContentChanged(int)));
       
   297     }
       
   298 }