contentstorage/caclient/src/canotifier.cpp
changeset 85 7feec50967db
child 87 9d806967057c
equal deleted inserted replaced
4:1a2a00e78665 85:7feec50967db
       
     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      }
       
   128     else if (QLatin1String(signal)
       
   129         == SIGNAL(entryChanged(CaEntry,ChangeType))) {
       
   130         // signal is entryChanged(const CaEntry &, ChangeType)
       
   131         if (receivers(SIGNAL(entryChanged(const CaEntry &,ChangeType))) == 1) {
       
   132             m_d->registerNotifier(
       
   133                 CaNotifierPrivate::EntryChangedWithEntryNotifierType);
       
   134         }
       
   135     }
       
   136     else if (QLatin1String(signal)
       
   137         == SIGNAL(entryTouched(int))) {
       
   138         // signal is entryTouched(int)
       
   139         if (receivers(SIGNAL(entryTouched(int)))) {
       
   140             m_d->registerNotifier(
       
   141                 CaNotifierPrivate::EntryTouchedNotifierType);
       
   142         }
       
   143     }
       
   144     else if (QLatin1String(signal)
       
   145         == SIGNAL(groupContentChanged(int))) {
       
   146         // signal is groupContentChanged(int)
       
   147         if (receivers(SIGNAL(groupContentChanged(int)))) {
       
   148             m_d->registerNotifier(
       
   149                 CaNotifierPrivate::GroupContentChangedNotifierType);
       
   150         }
       
   151     }
       
   152 }
       
   153 
       
   154 /*!
       
   155     This method is called when something has been
       
   156     disconnected from signal in this object.
       
   157     \param signal which is used to unregister an appropriate notifier.
       
   158 */
       
   159 void CaNotifier::disconnectNotify(const char *signal)
       
   160 {
       
   161     qDebug("CaNotifier::disconnectNotify");
       
   162     qDebug("\tsignal: %s", signal);
       
   163     if (QLatin1String(signal)
       
   164         == SIGNAL(entryChanged(int,ChangeType))) {
       
   165          // signal is entryChanged(int, ChangeType)
       
   166         if(receivers(SIGNAL(entryChanged(int,ChangeType)))==0) {
       
   167             m_d->unregisterNotifier(
       
   168                 CaNotifierPrivate::EntryChangedWithIdNotifierType);
       
   169         }
       
   170      }
       
   171     else if (QLatin1String(signal)
       
   172         == SIGNAL(entryChanged(CaEntry,ChangeType))) {
       
   173         // signal is entryChanged(const CaEntry &, ChangeType)
       
   174         if (receivers(SIGNAL(entryChanged(const CaEntry &,ChangeType)))==0) {
       
   175             m_d->unregisterNotifier(
       
   176                 CaNotifierPrivate::EntryChangedWithEntryNotifierType);
       
   177         }
       
   178     }
       
   179     else if (QLatin1String(signal)
       
   180         == SIGNAL(entryTouched(int))) {
       
   181         // signal is entryTouched(int)
       
   182         if (receivers(SIGNAL(entryTouched(int))) == 0) {
       
   183             m_d->unregisterNotifier(
       
   184                 CaNotifierPrivate::EntryTouchedNotifierType);
       
   185         }
       
   186     }
       
   187     else if (QLatin1String(signal)
       
   188         == SIGNAL(groupContentChanged(int))) {
       
   189         // signal is groupContentChanged(int)
       
   190         if (receivers(SIGNAL(groupContentChanged(int))) == 0) {
       
   191             m_d->unregisterNotifier(
       
   192                 CaNotifierPrivate::GroupContentChangedNotifierType);
       
   193         }
       
   194     }
       
   195 }
       
   196 
       
   197 /*!
       
   198  Constructor.
       
   199  \param notifierFilter descrbies entries to observe.
       
   200  */
       
   201 CaNotifierPrivate::CaNotifierPrivate(
       
   202     const CaNotifierFilter &notifierFilter) :
       
   203     m_q(NULL),
       
   204     mNotifierFilter(NULL),
       
   205     mNotifierProxy(NULL)
       
   206 {
       
   207         mNotifierFilter = new CaNotifierFilter(notifierFilter);
       
   208 }
       
   209 
       
   210 /*!
       
   211  Destructor.
       
   212  */
       
   213 CaNotifierPrivate::~CaNotifierPrivate()
       
   214 {
       
   215     if (mNotifierProxy) {
       
   216         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   217             EntryChangedWithIdNotifierType);
       
   218         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   219             EntryChangedWithEntryNotifierType);
       
   220         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   221             EntryTouchedNotifierType);
       
   222         mNotifierProxy->unregisterNotifier(*mNotifierFilter,
       
   223             GroupContentChangedNotifierType);
       
   224     }
       
   225     delete mNotifierFilter;
       
   226     delete mNotifierProxy;
       
   227 }
       
   228 /*!
       
   229  Registers notifier
       
   230  \param notifierType type of notifier to register (class of events to listen)
       
   231  */
       
   232 int CaNotifierPrivate::registerNotifier(NotifierType notifierType)
       
   233 {
       
   234     if (mNotifierProxy) {
       
   235         return mNotifierProxy->registerNotifier(
       
   236             mNotifierFilter,
       
   237             notifierType,
       
   238             mNotifierProxy);
       
   239     }
       
   240     return 0;
       
   241 }
       
   242 /*!
       
   243  Unregisters notifier
       
   244  \param notifierType type of notifier event type to unregister
       
   245      (class of events to listen)
       
   246  */
       
   247 void CaNotifierPrivate::unregisterNotifier(NotifierType notifierType)
       
   248 {
       
   249     if (mNotifierProxy) {
       
   250         mNotifierProxy->unregisterNotifier(*mNotifierFilter, notifierType);
       
   251     }
       
   252 }
       
   253 
       
   254 /*!
       
   255  Connects this Notifier
       
   256  */
       
   257 void CaNotifierPrivate::makeConnect()
       
   258 {
       
   259     if (m_q) {
       
   260         if (!mNotifierProxy) {
       
   261             mNotifierProxy = new CaClientNotifierProxy();
       
   262         }
       
   263         m_q->connect(mNotifierProxy,
       
   264             SIGNAL(signalEntryChanged(int,ChangeType)),
       
   265             m_q,
       
   266             SIGNAL(entryChanged(int,ChangeType)));
       
   267         m_q->connect(mNotifierProxy,
       
   268             SIGNAL(signalEntryChanged(const CaEntry &,ChangeType)),
       
   269             m_q,
       
   270             SIGNAL(entryChanged(const CaEntry &,ChangeType)));
       
   271         m_q->connect(mNotifierProxy,
       
   272             SIGNAL(signalEntryTouched(int)),
       
   273             m_q,
       
   274             SIGNAL(entryTouched(int)));
       
   275         m_q->connect(mNotifierProxy,
       
   276             SIGNAL(signalGroupContentChanged(int)),
       
   277             m_q,
       
   278             SIGNAL(groupContentChanged(int)));
       
   279     }
       
   280 }
       
   281 /*!
       
   282  Disconnects this Notifier
       
   283  */
       
   284 void CaNotifierPrivate::makeDisconnect()
       
   285 {
       
   286     if (m_q && mNotifierProxy) {
       
   287         m_q->disconnect(mNotifierProxy,
       
   288             SIGNAL(signalEntryChanged(int,ChangeType)),
       
   289             m_q,
       
   290             SIGNAL(entryChanged(int,ChangeType)));
       
   291         m_q->disconnect(mNotifierProxy,
       
   292             SIGNAL( signalEntryChanged(const CaEntry &,ChangeType)),
       
   293             m_q,
       
   294             SIGNAL(entryChanged(const CaEntry &,ChangeType)));
       
   295         m_q->disconnect(mNotifierProxy,
       
   296             SIGNAL(signalEntryTouched(int)),
       
   297             m_q,
       
   298             SIGNAL(entryTouched(int)));
       
   299         m_q->disconnect(mNotifierProxy,
       
   300             SIGNAL(signalGroupContentChanged(int)),
       
   301             m_q,
       
   302             SIGNAL(groupContentChanged(int)));
       
   303     }
       
   304 }