Home | History | Annotate | Download | only in src
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 /*==================================================================================================
     18 
     19     Source Name: dmEventLogger.cc
     20 
     21     General Description: Implementation of the DMEventLogger class
     22 
     23 ==================================================================================================*/
     24 
     25 #include "dmEventLogger.h"
     26 #include "dm_uri_utils.h"
     27 #include "dmtoken.h"
     28 #include "xpl_dm_Notifications.h"
     29 #include "dmPluginManager.h"
     30 #include "dm_tree_class.H"
     31 #include "dmEvent.h"
     32 #include "xpl_Logger.h"
     33 
     34 DMEventLogger::DMEventLogger()
     35 {
     36      m_pTree = NULL;
     37 }
     38 
     39 
     40 DMEventLogger::~DMEventLogger()
     41 {
     42 }
     43 
     44 SYNCML_DM_RET_STATUS_T DMEventLogger::Init( DMTree* tree )
     45 {
     46     if( !tree ) return SYNCML_DM_FAIL;
     47 
     48     m_pTree = tree;
     49     return SYNCML_DM_SUCCESS;
     50 }
     51 
     52 
     53 PDmtEventData
     54 DMEventLogger::Find(const DmtEventDataVector & aVector,
     55                              SYNCML_DM_EVENT_ACTION_T nAction,
     56                              BOOLEAN bIsCumulative,
     57                              BOOLEAN bIsEnabledByParent,
     58                              CPCHAR szName,
     59                              CPCHAR szNewName)
     60 {
     61 
     62     if ( bIsCumulative )
     63     {
     64         // no need to search if new node added in it is tracked by node itself
     65         if ( nAction == SYNCML_DM_EVENT_ADD && !bIsEnabledByParent )
     66             return NULL;
     67     }
     68     else
     69     {
     70         if ( nAction != SYNCML_DM_EVENT_INDIRECT && nAction != SYNCML_DM_EVENT_REPLACE )
     71            return NULL;
     72     }
     73 
     74     for (INT32 index=aVector.size()-1; index>=0; index--)
     75     {
     76         PDmtEventData & pEventData = (PDmtEventData&)(aVector[index]);
     77         if ( pEventData->GetName() == szName || pEventData->GetNewName() == szName )
     78         {
     79             return pEventData;
     80         }
     81     }
     82     return NULL;
     83 }
     84 
     85 
     86 
     87 SYNCML_DM_RET_STATUS_T
     88 DMEventLogger::AddEvent(PDmtEventData & pEventData,
     89                         SYNCML_DM_EVENT_ACTION_T nAction,
     90                         CPCHAR szNewName)
     91 {
     92 
     93     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
     94 
     95     ((DMEventData*)pEventData.GetPtr())->AddAction(nAction);
     96     if ( nAction == SYNCML_DM_EVENT_RENAME && szNewName )
     97     {
     98         dm_stat = ((DMEventData*)pEventData.GetPtr())->SetNewName(szNewName);
     99     }
    100 
    101     XPL_LOG_DM_TMN_Debug(("New action is added to logger, name = %s\n", (pEventData->GetName().c_str())));
    102     return SYNCML_DM_SUCCESS;
    103 }
    104 
    105 
    106 
    107 SYNCML_DM_RET_STATUS_T
    108 DMEventLogger::AddEvent(DmtEventDataVector & aVector,
    109                         SYNCML_DM_EVENT_ACTION_T nAction,
    110                         BOOLEAN bIsLeaf,
    111                         BOOLEAN bIsEnabledByParent,
    112                         CPCHAR szName,
    113                         CPCHAR szNewName)
    114 {
    115 
    116     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    117 
    118     DMEventData* pDataPtr = NULL;
    119 
    120     pDataPtr = new DMEventData();
    121     if ( pDataPtr == NULL )
    122         return SYNCML_DM_DEVICE_FULL;
    123 
    124     pDataPtr->SetAction(nAction);
    125     pDataPtr->SetLeaf(bIsLeaf);
    126     pDataPtr->SetEnabledByParent(bIsEnabledByParent);
    127 
    128     dm_stat =   pDataPtr->SetName(szName);
    129     if ( dm_stat != SYNCML_DM_SUCCESS )
    130     {
    131         delete pDataPtr;
    132         return dm_stat;
    133     }
    134 
    135     if ( nAction == SYNCML_DM_EVENT_RENAME && szNewName )
    136     {
    137         dm_stat = pDataPtr->SetNewName(szNewName);
    138         if ( dm_stat != SYNCML_DM_SUCCESS )
    139         {
    140             delete pDataPtr;
    141             return dm_stat;
    142         }
    143     }
    144 
    145     aVector.push_back(PDmtEventData(pDataPtr));
    146     XPL_LOG_DM_TMN_Debug(("New event is added to logger, name = %s\n", szName));
    147 
    148     return SYNCML_DM_SUCCESS;
    149 }
    150 
    151 
    152 
    153 BOOLEAN
    154 DMEventLogger::Find(CPCHAR szPath,
    155                               const DMEventMap & aEventMap,
    156                               PDMEventPath & pEventPath,
    157                               DmtEventDataVector & aVector)
    158 {
    159 
    160     for ( DmtEventMap::POS nPos = 0; nPos < aEventMap.end(); nPos++ )
    161     {
    162         pEventPath =  aEventMap.get_key(nPos);
    163         if ( pEventPath ->GetPath() == szPath )
    164         {
    165             aVector =  aEventMap.get_value(nPos);
    166             return TRUE;
    167             break;
    168         }
    169     }
    170     return FALSE;
    171 
    172 }
    173 
    174 
    175 
    176 SYNCML_DM_RET_STATUS_T
    177 DMEventLogger::OnNodeChanged(CPCHAR szPath ,
    178                              SYNCML_DM_EVENT_ACTION_T nAction,
    179                              DMEventMap & aEventMap,
    180                              BOOLEAN bIsCumulative,
    181                              BOOLEAN bIsEnabledByParent,
    182                              CPCHAR szNewName )
    183 {
    184 
    185     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    186 
    187     DMURI oURI(FALSE, szPath);
    188 
    189     CPCHAR pParent = oURI.getParentURI();
    190     CPCHAR pNode = oURI.getTailSegments();
    191 
    192     DmtEventDataVector aVector;
    193     PDMEventPath pEventPath;
    194     BOOLEAN bFound = Find(pParent,aEventMap, pEventPath,aVector);
    195 
    196     if ( bFound == FALSE )
    197     {
    198         pEventPath = PDMEventPath(new DMEventPath(pParent));
    199 
    200         if ( pEventPath  == NULL )
    201         {
    202             XPL_LOG_DM_TMN_Error(("Memory allocation error \n"));
    203             return SYNCML_DM_DEVICE_FULL;
    204         }
    205     }
    206 
    207     PDmtEventData pEventData = NULL;
    208     pEventData = Find(aVector,nAction, bIsCumulative,bIsEnabledByParent,pNode,szNewName);
    209 
    210     if ( pEventData != NULL )
    211     {
    212           dm_stat = AddEvent(pEventData, nAction, szNewName);
    213     }
    214     else
    215     {
    216          BOOLEAN bIsLeaf = FALSE;
    217 
    218          if ( nAction == SYNCML_DM_EVENT_REPLACE )
    219             bIsLeaf = TRUE;
    220         else
    221         {
    222             DMMetaDataManager & m_oMDFObj =  m_pTree->GetMetaDataManager();
    223             bIsLeaf = m_oMDFObj.IsLeaf(szPath);
    224         }
    225         dm_stat = AddEvent(aVector, nAction, bIsLeaf, bIsEnabledByParent, pNode, szNewName);
    226     }
    227 
    228     if ( dm_stat != SYNCML_DM_SUCCESS )
    229     {
    230         XPL_LOG_DM_TMN_Error(("Cannot add new event, dm_stat = %d\n", dm_stat));
    231         return dm_stat;
    232     }
    233 
    234     aEventMap.put( pEventPath , aVector );
    235 
    236     return SYNCML_DM_SUCCESS;
    237 
    238 }
    239 
    240 
    241 
    242 SYNCML_DM_RET_STATUS_T
    243 DMEventLogger::OnNodeChanged(CPCHAR szPath,
    244                              SYNCML_DM_EVENT_ACTION_T nAction,
    245                              const PDMPlugin & pPlugin,
    246                              BOOLEAN bIsEnabledByParent,
    247                              CPCHAR szNewName )
    248 {
    249 
    250     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    251     BOOLEAN bIsCumulative = FALSE;
    252 
    253     DMEventMap aEventMap;
    254 
    255     XPL_LOG_DM_TMN_Debug(("OnNodeChanged called for commit plug-in %s\n", szPath));
    256     m_aPluginEvents.lookup(pPlugin,aEventMap);
    257 
    258     if ( pPlugin->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
    259          bIsCumulative = TRUE;
    260 
    261     dm_stat =  OnNodeChanged(szPath,nAction,aEventMap,bIsCumulative,bIsEnabledByParent,szNewName);
    262     if ( dm_stat != SYNCML_DM_SUCCESS )
    263         return dm_stat;
    264 
    265     m_aPluginEvents.put(pPlugin,aEventMap);
    266     return SYNCML_DM_SUCCESS;
    267 
    268 }
    269 
    270 
    271 SYNCML_DM_RET_STATUS_T
    272 DMEventLogger::OnNodeChanged(CPCHAR szPath,
    273                              SYNCML_DM_EVENT_ACTION_T nAction,
    274                              const PDMConfigItem & pItem,
    275                              BOOLEAN bIsEnabledByParent,
    276                              CPCHAR szNewName )
    277 {
    278 
    279     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    280     BOOLEAN bIsCumulative = FALSE;
    281 
    282     DMEventMap aEventMap;
    283     XPL_LOG_DM_TMN_Debug(("OnNodeChanged called for ES %s\n", szPath));
    284     m_aPostEvents.lookup(pItem,aEventMap);
    285 
    286     if ( ((DMSubscriptionItem*)(pItem.GetPtr()))->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
    287          bIsCumulative = TRUE;
    288 
    289     dm_stat =  OnNodeChanged(szPath,nAction,aEventMap,bIsCumulative,bIsEnabledByParent,szNewName);
    290     if ( dm_stat != SYNCML_DM_SUCCESS )
    291         return dm_stat;
    292 
    293     m_aPostEvents.put(pItem,aEventMap);
    294     return SYNCML_DM_SUCCESS;
    295 
    296 }
    297 
    298 
    299 UINT32 DMEventLogger::GetSize(const DMEventMap & aEventMap)
    300 {
    301     UINT32 size = sizeof(UINT32) * 2;
    302 
    303     for ( DMEventMap::POS nPos = 0; nPos < aEventMap.end(); nPos++ )
    304     {
    305          const PDMEventPath & pPath = aEventMap.get_key(nPos);
    306          const DmtEventDataVector & aVector = aEventMap.get_value(nPos);
    307 
    308          size += pPath->GetSize();
    309          size += sizeof(UINT32);
    310          for (INT32 index=0; index<aVector.size(); index++)
    311          {
    312             const PDmtEventData & pData = aVector[index];
    313             DMEventData * pDataPtr  = (DMEventData*)pData.GetPtr();
    314             size += pDataPtr->GetSize();
    315          }
    316     }
    317 
    318     return size;
    319 }
    320 
    321 
    322 
    323 
    324 SYNCML_DM_RET_STATUS_T
    325 DMEventLogger::Serialize(const DmtEventDataVector & aVector,
    326                          DMBufferWriter & oBuffer)
    327 {
    328     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    329 
    330     oBuffer.WriteUINT32(aVector.size());
    331 
    332     for (INT32 index=0; index<aVector.size(); index++)
    333     {
    334          const PDmtEventData & pData = aVector[index];
    335          dm_stat = ((DMEventData*)(pData.GetPtr()))->Serialize(oBuffer);
    336          if ( dm_stat != SYNCML_DM_SUCCESS )
    337              return dm_stat;
    338     }
    339 
    340     return SYNCML_DM_SUCCESS;
    341 }
    342 
    343 
    344 SYNCML_DM_RET_STATUS_T
    345 DMEventLogger::Serialize(const DMEventMap & aEventMap,
    346                          DMBufferWriter & oBuffer)
    347 {
    348     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    349 
    350     INT32 size = GetSize(aEventMap);
    351     dm_stat = oBuffer.Allocate(size);
    352 
    353     if ( dm_stat != SYNCML_DM_SUCCESS )
    354         return dm_stat;
    355 
    356     oBuffer.WriteUINT32(size);
    357 
    358     oBuffer.WriteUINT32(aEventMap.end());
    359 
    360     for ( DMEventMap::POS nPos = 0; nPos < aEventMap.end(); nPos++ )
    361     {
    362          const PDMEventPath & pPath = aEventMap.get_key(nPos);
    363          const DmtEventDataVector & aVector = aEventMap.get_value(nPos);
    364 
    365          dm_stat = pPath->Serialize(oBuffer);
    366          if ( dm_stat != SYNCML_DM_SUCCESS )
    367             return dm_stat;
    368 
    369          dm_stat = Serialize(aVector,oBuffer);
    370          if ( dm_stat != SYNCML_DM_SUCCESS )
    371             return dm_stat;
    372     }
    373 
    374     return SYNCML_DM_SUCCESS;
    375 }
    376 
    377 SYNCML_DM_RET_STATUS_T
    378 DMEventLogger::Deserialize(DMBufferReader & oBuffer,
    379                            DMString & strParent,
    380                            PDmtEventData & aData)
    381 {
    382     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    383 
    384     UINT32 size = oBuffer.ReadUINT32();
    385     if ( size != oBuffer.GetSize() )
    386         return SYNCML_DM_FAIL;
    387 
    388     INT32 nPathsCount = (INT32)oBuffer.ReadUINT32();
    389     if ( nPathsCount != 1 )
    390         return SYNCML_DM_FAIL;
    391 
    392     DMEventPath oPath;
    393     dm_stat = oPath.Deserialize(oBuffer);
    394 
    395     strParent = oPath.GetPath();
    396 
    397     INT32 nNodesCount =  (INT32)oBuffer.ReadUINT32();
    398     if ( nNodesCount != 1 )
    399        return SYNCML_DM_FAIL;
    400 
    401     DMEventData * pDataPtr = new DMEventData();
    402     if ( pDataPtr == NULL )
    403          return SYNCML_DM_DEVICE_FULL;
    404 
    405     dm_stat = pDataPtr->Deserialize(oBuffer);
    406     if ( dm_stat != SYNCML_DM_SUCCESS )
    407     {
    408           delete pDataPtr;
    409           return dm_stat;
    410     }
    411 
    412     aData = PDmtEventData(pDataPtr);
    413 
    414     return SYNCML_DM_SUCCESS;
    415 
    416 }
    417 
    418 
    419 
    420 SYNCML_DM_RET_STATUS_T
    421 DMEventLogger::Deserialize(DMBufferReader & oBuffer,
    422                            DmtEventDataVector & aVector)
    423 {
    424     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    425 
    426     INT32 nNodesCount =  (INT32)oBuffer.ReadUINT32();
    427     if ( nNodesCount == 0 )
    428         return SYNCML_DM_FAIL;
    429 
    430     for (INT32 index =0; index<nNodesCount; index++)
    431     {
    432         DMEventData * pDataPtr = new DMEventData();
    433         if ( pDataPtr == NULL )
    434             return SYNCML_DM_DEVICE_FULL;
    435 
    436         dm_stat = pDataPtr->Deserialize(oBuffer);
    437         if ( dm_stat != SYNCML_DM_SUCCESS )
    438         {
    439             delete pDataPtr;
    440             return dm_stat;
    441         }
    442         aVector.push_back(PDmtEventData(pDataPtr));
    443     }
    444 
    445     return SYNCML_DM_SUCCESS;
    446 }
    447 
    448 
    449 
    450 
    451 SYNCML_DM_RET_STATUS_T
    452 DMEventLogger::Deserialize(DMBufferReader & oBuffer,
    453                            DmtEventMap & aEventMap)
    454 {
    455     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    456 
    457     UINT32 size = oBuffer.ReadUINT32();
    458     if ( size != oBuffer.GetSize() )
    459         return SYNCML_DM_FAIL;
    460 
    461     INT32 nPathsCount = (INT32)oBuffer.ReadUINT32();
    462     if ( nPathsCount == 0 )
    463         return SYNCML_DM_FAIL;
    464 
    465     for (INT32 index = 0; index<nPathsCount; index++)
    466     {
    467         DMEventPath oPath;
    468         dm_stat = oPath.Deserialize(oBuffer);
    469         if ( dm_stat != SYNCML_DM_SUCCESS )
    470             return dm_stat;
    471 
    472         DmtEventDataVector aVector;
    473 
    474         dm_stat = Deserialize(oBuffer,aVector);
    475         if ( dm_stat != SYNCML_DM_SUCCESS )
    476             return dm_stat;
    477 
    478         aEventMap.put(oPath.GetPath(), aVector);
    479 
    480     }
    481 
    482     return SYNCML_DM_SUCCESS;
    483 }
    484 
    485 void DMEventLogger::OnTreeSaved()
    486 {
    487 
    488     for ( DMPostEventMap::POS nPos = 0; nPos < m_aPostEvents.end(); nPos++ )
    489     {
    490         const PDMConfigItem & pItem = m_aPostEvents.get_key(nPos);
    491         const DMEventMap & aEventMap = m_aPostEvents.get_value(nPos);
    492         SYNCML_DM_EVENT_TYPE_T nType =  ((DMSubscriptionItem*)(pItem.GetPtr()))->GetType();
    493         DMString strTopic = ((DMSubscriptionItem*)(pItem.GetPtr()))->GetTopic();
    494 
    495         DMBufferWriter oBuffer;
    496         if ( Serialize(aEventMap,oBuffer) == SYNCML_DM_SUCCESS )
    497         {
    498              if ( strTopic == NULL )
    499              {
    500                 strTopic = pItem->GetPath();
    501                 XPL_LOG_DM_TMN_Debug(("Sending event for %s\n", strTopic.c_str()));
    502                 XPL_DM_NotifyTreeUpdate(strTopic, NULL, nType, oBuffer.GetBuffer(), oBuffer.GetSize());
    503              }
    504              else
    505              {
    506                 XPL_LOG_DM_TMN_Debug(("Sending event for %s\n", strTopic.c_str()));
    507                 XPL_DM_NotifyTreeUpdate(strTopic, pItem->GetPath(), nType, oBuffer.GetBuffer(), oBuffer.GetSize());
    508              }
    509         }
    510         else
    511         {
    512             XPL_LOG_DM_TMN_Error(("Event Serialization failed\n"));
    513         }
    514     }
    515 
    516     Reset();
    517 }
    518 
    519 void DMEventLogger::Reset()
    520 {
    521     m_aPostEvents.clear();
    522     m_aPluginEvents.clear();
    523 }
    524 
    525 void DMEventLogger::GetCommitPluginEvents(const PDMPlugin & pPlugin,
    526                                           DmtEventMap  & aUpdatedNodes ) const
    527 {
    528     DMEventMap aStoredMap;
    529 
    530     m_aPluginEvents.lookup(pPlugin,aStoredMap);
    531     for ( DmtEventMap::POS nPos = 0; nPos < aStoredMap.end(); nPos++ )
    532     {
    533          const PDMEventPath & pPath = aStoredMap.get_key(nPos);
    534          const DmtEventDataVector & aVector = aStoredMap.get_value(nPos);
    535          aUpdatedNodes.put(pPath->GetPath(),aVector);
    536     }
    537 
    538 }
    539 
    540 
    541 
    542 SYNCML_DM_RET_STATUS_T
    543 DMEventLogger::CleanEvents(DmtEventDataVector & aVector)
    544 {
    545 
    546     for ( INT32 index = aVector.size()-1; index >= 0; index-- )
    547     {
    548         const PDmtEventData & pData = aVector[index];
    549         if (  ((DMEventData*)(pData.GetPtr()))->IsEnabledByParent() )
    550             aVector.remove(index);
    551     }
    552     return SYNCML_DM_SUCCESS;
    553 
    554 }
    555 
    556 
    557 SYNCML_DM_RET_STATUS_T
    558 DMEventLogger::CleanEvents(DMEventMap & aEventMap, CPCHAR szPath)
    559 {
    560 
    561     for ( DMEventMap::POS nPos = aEventMap.end()-1; nPos >= 0; nPos-- )
    562     {
    563         const PDMEventPath & pPath = aEventMap.get_key(nPos);
    564 
    565         BOOLEAN bIsParent = DmIsParentURI(szPath,pPath->GetPath());
    566         if ( !bIsParent &&  !DmIsParentURI(pPath->GetPath(),szPath) )
    567         {
    568             continue;
    569         }
    570 
    571         if ( bIsParent )
    572         {
    573             DmtEventDataVector & aVector = aEventMap.get_value(nPos);
    574             CleanEvents(aVector);
    575             if ( !aVector.size() )
    576                 aEventMap.remove(pPath);
    577         }
    578     }
    579     return SYNCML_DM_SUCCESS;
    580 
    581 }
    582 
    583 
    584 
    585 SYNCML_DM_RET_STATUS_T
    586 DMEventLogger::CleanConfigEvents(CPCHAR szPath)
    587 {
    588 
    589     for ( DMPostEventMap::POS nPos = m_aPostEvents.end()-1; nPos >= 0; nPos-- )
    590     {
    591         const PDMConfigItem & pConfigItem = m_aPostEvents.get_key(nPos);
    592 
    593         if ( !DmIsParentURI(szPath,pConfigItem->GetPath()) &&
    594              !DmIsParentURI(pConfigItem->GetPath(),szPath) )
    595         {
    596             continue;
    597         }
    598 
    599         DMParser oStored(pConfigItem->GetPath());
    600         DMParser oRemoved(szPath);
    601 
    602         if ( oRemoved.getSegmentsCount() < oStored.getSegmentsCount() )
    603         {
    604             m_aPostEvents.remove(pConfigItem);
    605             continue;
    606         }
    607 
    608         DMEventMap & aEventMap = m_aPostEvents.get_value(nPos);
    609         CleanEvents(aEventMap,szPath);
    610         if ( !aEventMap.size() )
    611             m_aPostEvents.remove(pConfigItem);
    612     }
    613 
    614     return SYNCML_DM_SUCCESS;
    615 
    616 }
    617 
    618 SYNCML_DM_RET_STATUS_T
    619 DMEventLogger::CleanPluginEvents(CPCHAR szPath)
    620 {
    621 
    622     for ( DMPostEventMap::POS nPos = m_aPluginEvents.end()-1; nPos >= 0; nPos-- )
    623     {
    624         const PDMPlugin & pPlugin = m_aPluginEvents.get_key(nPos);
    625 
    626          if ( !DmIsParentURI(szPath,pPlugin->GetPath()) &&
    627              !DmIsParentURI(pPlugin->GetPath(),szPath) )
    628         {
    629             continue;
    630         }
    631 
    632         DMParser oStored(pPlugin->GetPath());
    633         DMParser oRemoved(szPath);
    634 
    635         if ( oRemoved.getSegmentsCount() < oStored.getSegmentsCount() )
    636         {
    637             m_aPluginEvents.remove(pPlugin);
    638             continue;
    639         }
    640 
    641         DMEventMap & aEventMap = m_aPluginEvents.get_value(nPos);
    642         CleanEvents(aEventMap,szPath);
    643         if ( !aEventMap.size() )
    644             m_aPluginEvents.remove(pPlugin);
    645     }
    646     return SYNCML_DM_SUCCESS;
    647 
    648 }
    649 
    650 
    651 SYNCML_DM_RET_STATUS_T
    652 DMEventLogger::CleanEvents(CPCHAR szPath)
    653 {
    654     XPL_LOG_DM_TMN_Debug(("Clean events for %s\n", szPath));
    655     CleanConfigEvents(szPath);
    656     CleanPluginEvents(szPath);
    657     return SYNCML_DM_SUCCESS;
    658 
    659 }
    660 
    661 
    662 
    663 SYNCML_DM_RET_STATUS_T
    664 DMEventLogger::UpdateEvents(DMEventMap & aEventMap,
    665                             CPCHAR szPath,
    666                             CPCHAR szNewName)
    667 {
    668 
    669     for ( DMEventMap::POS nPos = aEventMap.end()-1; nPos >= 0; nPos-- )
    670     {
    671         PDMEventPath & pPath = (PDMEventPath &)aEventMap.get_key(nPos);
    672 
    673         if ( !DmIsParentURI(szPath,pPath->GetPath()) )
    674             continue;
    675 
    676         DMBuffer oNewPath;
    677         if ( !oNewPath.allocate((pPath->GetPath()).length() + DmStrlen(szNewName)) )
    678             return SYNCML_DM_DEVICE_FULL;
    679 
    680         DMURI oPath(FALSE,szPath);
    681         CPCHAR pParent = oPath.getParentURI();
    682 
    683         CPCHAR pStoredPath = (CPCHAR)(pPath->GetPath());
    684         DMURI oStoredTail(FALSE, pStoredPath + DmStrlen(szPath) + 1);
    685 
    686         oStoredTail.nextSegment();
    687         CPCHAR pTailSegments = oStoredTail.getTailSegments();
    688 
    689 
    690         oNewPath.assign(pParent);
    691         oNewPath.append((UINT8*)"/",1);
    692         oNewPath.append((UINT8*)szNewName,DmStrlen(szNewName));
    693 
    694         if ( pTailSegments )
    695         {
    696             oNewPath.append((UINT8*)"/",1);
    697             oNewPath.append((UINT8*)pTailSegments,DmStrlen(pTailSegments));
    698         }
    699 
    700         pPath = PDMEventPath(new DMEventPath((CPCHAR)oNewPath.getBuffer()));
    701 
    702         if ( pPath  == NULL )
    703             return SYNCML_DM_DEVICE_FULL;
    704     }
    705     return SYNCML_DM_SUCCESS;
    706 
    707 }
    708 
    709 
    710 
    711 
    712 SYNCML_DM_RET_STATUS_T
    713 DMEventLogger::UpdateSubscriptionEvents(CPCHAR szPath,
    714                                         CPCHAR szNewName)
    715 {
    716 
    717     for ( DMPostEventMap::POS nPos = m_aPostEvents.end()-1; nPos >= 0; nPos-- )
    718     {
    719         const PDMConfigItem & pConfigItem = m_aPostEvents.get_key(nPos);
    720 
    721         if ( !DmIsParentURI(szPath,pConfigItem->GetPath()) &&
    722              !DmIsParentURI(pConfigItem->GetPath(),szPath) )
    723         {
    724             continue;
    725         }
    726 
    727         DMEventMap & aEventMap = m_aPostEvents.get_value(nPos);
    728         UpdateEvents(aEventMap,szPath,szNewName);
    729     }
    730 
    731     return SYNCML_DM_SUCCESS;
    732 
    733 }
    734 
    735 
    736 SYNCML_DM_RET_STATUS_T
    737 DMEventLogger::UpdatePluginEvents(CPCHAR szPath,
    738                                   CPCHAR szNewName)
    739 {
    740 
    741     for ( DMPluginEventMap::POS nPos = m_aPluginEvents.end()-1; nPos >= 0; nPos-- )
    742     {
    743         const PDMPlugin & pPlugin = m_aPluginEvents.get_key(nPos);
    744 
    745         if ( !DmIsParentURI(szPath,pPlugin->GetPath()) &&
    746              !DmIsParentURI(pPlugin->GetPath(),szPath) )
    747         {
    748             continue;
    749         }
    750 
    751         DMEventMap & aEventMap = m_aPluginEvents.get_value(nPos);
    752         UpdateEvents(aEventMap,szPath,szNewName);
    753     }
    754 
    755     return SYNCML_DM_SUCCESS;
    756 
    757 }
    758 
    759 
    760 SYNCML_DM_RET_STATUS_T
    761 DMEventLogger::UpdateEvents(CPCHAR szPath,
    762                             CPCHAR szNewName)
    763 {
    764     XPL_LOG_DM_TMN_Debug(("Update events for %s\n", szPath));
    765     UpdateSubscriptionEvents(szPath,szNewName);
    766     UpdatePluginEvents(szPath,szNewName);
    767     return SYNCML_DM_SUCCESS;
    768 
    769 }
    770 
    771 
    772 
    773 BOOLEAN
    774 DMEventLogger::FindRecordForAdd(const DmtEventDataVector & aVector,
    775                                     CPCHAR szName)
    776 {
    777 
    778     for (INT32 index=aVector.size()-1; index>=0; index--)
    779     {
    780         const PDmtEventData & pEventData = aVector[index];
    781 
    782         if ( !pEventData->IsLeaf() )
    783             continue;  // we need only leaf nodes
    784 
    785         if ( pEventData->GetName() == szName ) // No Rename on leaf nodes, so compare only name
    786         {
    787             if (pEventData->GetAction() == SYNCML_DM_EVENT_DELETE) // node was deleted
    788             {
    789                 ((DMEventData*)pEventData.GetPtr())->SetAction(SYNCML_DM_EVENT_REPLACE); // delete+add on leaf node = replace
    790                 return TRUE;
    791             }
    792             break;
    793         }
    794     }
    795     return FALSE;
    796 }
    797 
    798 
    799 BOOLEAN
    800 DMEventLogger::FindRecordForDelete(DmtEventDataVector & aVector,
    801                                    CPCHAR szName)
    802 {
    803     BOOLEAN bIsIgnore = FALSE;
    804     for (INT32 index=aVector.size()-1; index>=0; index--)
    805     {
    806          const PDmtEventData & pEventData = aVector[index];
    807         if ( pEventData->GetName() == szName || pEventData->GetNewName() == szName)
    808         {
    809              if ( (pEventData->GetAction() & SYNCML_DM_EVENT_ADD) == SYNCML_DM_EVENT_ADD )
    810             {
    811                 bIsIgnore = TRUE;
    812                 aVector.remove(index);
    813             }
    814 
    815             return bIsIgnore;
    816         }
    817     }
    818 
    819     return bIsIgnore;
    820 
    821 }
    822 
    823 
    824 BOOLEAN
    825 DMEventLogger::FindRecordForReplace(const DmtEventDataVector & aVector,
    826                                     CPCHAR szName)
    827 {
    828 
    829     for (INT32 index=aVector.size()-1; index>=0; index--)
    830     {
    831         const PDmtEventData & pEventData = aVector[index];
    832         if ( pEventData->GetName() == szName ) // Replace is only on leaf nodes , no need to check new name
    833         {
    834             if ((pEventData->GetAction() & SYNCML_DM_EVENT_REPLACE) == SYNCML_DM_EVENT_REPLACE)
    835                 return TRUE;
    836 
    837             if ((pEventData->GetAction() & SYNCML_DM_EVENT_ADD) == SYNCML_DM_EVENT_ADD)
    838                 return TRUE;
    839 
    840             break;
    841         }
    842     }
    843     return FALSE;
    844 }
    845 
    846 
    847 BOOLEAN
    848 DMEventLogger::FindRecordForRename(DmtEventDataVector & aVector,
    849                                    CPCHAR szName,
    850                                    CPCHAR szNewName)
    851 {
    852     BOOLEAN bIsIgnore = FALSE;
    853 
    854     for (INT32 index=aVector.size()-1; index>=0; index--)
    855     {
    856         PDmtEventData & pEventData = aVector[index];
    857         if (  ((pEventData->GetAction() & SYNCML_DM_EVENT_ADD) == SYNCML_DM_EVENT_ADD) &&
    858                pEventData->GetName() == szName )
    859         {
    860             ((DMEventData*)pEventData.GetPtr())->SetName(szNewName);
    861             bIsIgnore = TRUE;
    862             break;
    863         }
    864 
    865         if (  ((pEventData->GetAction() & SYNCML_DM_EVENT_RENAME) == SYNCML_DM_EVENT_RENAME) &&
    866                pEventData->GetNewName() == szName )
    867         {
    868             ((DMEventData*)pEventData.GetPtr())->SetNewName(szNewName);
    869             bIsIgnore = TRUE;
    870             break;
    871         }
    872     }
    873     return bIsIgnore;
    874 }
    875 
    876 
    877 
    878 BOOLEAN
    879 DMEventLogger::CheckEventOnSameNode(DmtEventDataVector & aVector,
    880                                     SYNCML_DM_EVENT_ACTION_T nAction,
    881                                     CPCHAR szName,
    882                                     BOOLEAN bIsCumulative,
    883                                     CPCHAR szNewName)
    884 {
    885 
    886 
    887     if ( bIsCumulative )
    888     {
    889         if ( nAction == SYNCML_DM_EVENT_RENAME )
    890             return FindRecordForRename(aVector,szName,szNewName);
    891     }
    892     else
    893     {
    894         switch ( nAction )
    895         {
    896             case SYNCML_DM_EVENT_ADD:
    897                 return FindRecordForAdd(aVector,szName);
    898 
    899             case SYNCML_DM_EVENT_REPLACE:
    900                 return FindRecordForReplace(aVector,szName);
    901 
    902             case SYNCML_DM_EVENT_DELETE:
    903                 return FindRecordForDelete(aVector, szName);
    904 
    905            case  SYNCML_DM_EVENT_RENAME:
    906                 return FindRecordForRename(aVector,szName,szNewName);
    907          }
    908      }
    909 
    910      return  FALSE;
    911 
    912 }
    913 
    914 
    915 
    916 
    917 BOOLEAN
    918 DMEventLogger::IsIgnoreEvent(CPCHAR szPath ,
    919                              SYNCML_DM_EVENT_ACTION_T nAction,
    920                              DMEventMap & aEventMap,
    921                              BOOLEAN bIsCumulative,
    922                              CPCHAR szNewName )
    923 {
    924 
    925 
    926     DMURI oURI(FALSE, szPath);
    927 
    928     CPCHAR pParent = oURI.getParentURI();
    929     CPCHAR pNode = oURI.getTailSegments();
    930 
    931     DmtEventDataVector aVector;
    932     PDMEventPath pEventPath;
    933     BOOLEAN bIsFound = Find(pParent,aEventMap, pEventPath,aVector);
    934 
    935     if ( !bIsFound )
    936         return FALSE;
    937 
    938     BOOLEAN bIsIgnore = CheckEventOnSameNode(aVector,
    939                                              nAction,
    940                                              pNode,
    941                                              bIsCumulative,
    942                                              szNewName);
    943     if ( !aVector.size() )
    944         aEventMap.remove(pEventPath);
    945     else
    946         aEventMap.put(pEventPath,aVector);
    947 
    948     return bIsIgnore;
    949 
    950 }
    951 
    952 
    953 
    954 BOOLEAN
    955 DMEventLogger::IsIgnoreSubscriptionEvent(CPCHAR szPath,
    956                                          SYNCML_DM_EVENT_ACTION_T nAction,
    957                                          CPCHAR szNewName)
    958 {
    959 
    960     BOOLEAN bIsIgnore = TRUE;
    961     BOOLEAN bIsCumulative = FALSE;
    962 
    963     PDMConfigItem pFoundItem;
    964     DMEventMap aEventMap;
    965     UINT32 nSegmentCount = 0;
    966 
    967     for ( DMPostEventMap::POS nPos = m_aPostEvents.end()-1; nPos >= 0; nPos--  )
    968     {
    969         const PDMConfigItem & pConfigItem = m_aPostEvents.get_key(nPos);
    970 
    971         if (  !DmIsParentURI(pConfigItem->GetPath(),szPath) )
    972             continue;
    973 
    974         DMParser oParser(szPath);
    975         if ( nSegmentCount < oParser.getSegmentsCount() )
    976         {
    977             nSegmentCount = oParser.getSegmentsCount();
    978             pFoundItem = pConfigItem;
    979             aEventMap = m_aPostEvents.get_value(nPos);
    980         }
    981     }
    982 
    983     if ( pFoundItem == NULL )
    984        return FALSE;
    985 
    986     if ( ((DMSubscriptionItem*)( pFoundItem.GetPtr()))->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
    987         bIsCumulative = TRUE;
    988 
    989     bIsIgnore = IsIgnoreEvent(szPath,
    990                               nAction,
    991                               aEventMap,
    992                               bIsCumulative,
    993                               szNewName);
    994 
    995     if ( !aEventMap.size() )
    996         m_aPostEvents.remove( pFoundItem);
    997     else
    998         m_aPostEvents.put(pFoundItem,aEventMap);
    999 
   1000     return bIsIgnore;
   1001 }
   1002 
   1003 
   1004 
   1005 
   1006 
   1007 BOOLEAN
   1008 DMEventLogger::IsIgnorePluginEvent(CPCHAR szPath,
   1009                                    SYNCML_DM_EVENT_ACTION_T nAction,
   1010                                    CPCHAR szNewName)
   1011 {
   1012 
   1013     BOOLEAN bIsIgnore = TRUE;
   1014     BOOLEAN bIsCumulative = FALSE;
   1015 
   1016     PDMPlugin pFoundPlugin;
   1017     DMEventMap aEventMap;
   1018     UINT32 nSegmentCount = 0;
   1019 
   1020     for ( DMPluginEventMap::POS nPos = m_aPluginEvents.end()-1; nPos >= 0; nPos-- )
   1021     {
   1022         const PDMPlugin & pPlugin = m_aPluginEvents.get_key(nPos);
   1023 
   1024         if (  !DmIsParentURI(pPlugin->GetPath(),szPath) )
   1025             continue;
   1026 
   1027         DMParser oParser(szPath);
   1028         if ( nSegmentCount < oParser.getSegmentsCount() )
   1029         {
   1030             nSegmentCount = oParser.getSegmentsCount();
   1031             pFoundPlugin = pPlugin;
   1032             aEventMap = m_aPluginEvents.get_value(nPos);
   1033         }
   1034     }
   1035 
   1036 
   1037     if ( pFoundPlugin == NULL )
   1038         return FALSE;
   1039 
   1040     if ( pFoundPlugin->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
   1041          bIsCumulative = TRUE;
   1042 
   1043     bIsIgnore = IsIgnoreEvent(szPath,
   1044                               nAction,
   1045                               aEventMap,
   1046                               bIsCumulative,
   1047                               szNewName);
   1048 
   1049     if ( !aEventMap.size() )
   1050         m_aPluginEvents.remove(pFoundPlugin);
   1051     else
   1052         m_aPluginEvents.put(pFoundPlugin,aEventMap);
   1053 
   1054     return bIsIgnore;
   1055 }
   1056