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: dmSubscriptionManager.cc
     20 
     21     General Description: Implementation of the DMSubscriptionManager class
     22 
     23 ==================================================================================================*/
     24 
     25 #include "dm_tree_class.H"
     26 #include "dmSubscriptionManager.h"
     27 #include "dm_uri_utils.h"
     28 #include "xpl_Logger.h"
     29 
     30 SYNCML_DM_RET_STATUS_T
     31 DMSubscriptionManager::Init( CEnv* env, DMTree* tree )
     32 {
     33     return DMConfigManager::Init(env,tree,SYNCML_DM_FILE_EVENT);
     34 }
     35 
     36 
     37 SYNCML_DM_RET_STATUS_T
     38 DMSubscriptionManager::UpdateEvents(CPCHAR szPath,
     39                                     SYNCML_DM_EVENT_ACTION_T nAction,
     40                                     CPCHAR szNewName)
     41 {
     42     SYNCML_DM_RET_STATUS_T  dm_stat;
     43     SyncML_DM_Archiver & m_oArchiver = m_pTree->GetArchiver();
     44 
     45     if ( nAction == SYNCML_DM_EVENT_RENAME )
     46     {
     47         dm_stat = m_oArchiver.UpdateEvents(szPath,szNewName);
     48         if ( dm_stat != SYNCML_DM_SUCCESS )
     49            return dm_stat;
     50     }
     51     return SYNCML_DM_SUCCESS;
     52 
     53 }
     54 
     55 
     56 SYNCML_DM_RET_STATUS_T
     57 DMSubscriptionManager::CleanEvents(CPCHAR szPath)
     58 {
     59     SyncML_DM_Archiver & m_oArchiver = m_pTree->GetArchiver();
     60 
     61     return m_oArchiver.CleanEvents(szPath);
     62 
     63 }
     64 
     65 CPCHAR
     66 DMSubscriptionManager::ResetName(BOOLEAN bIsEnabledByParent,
     67                                  DMEventSubscription * pItem,
     68                                  CPCHAR szNewName)
     69 {
     70     if ( bIsEnabledByParent && pItem->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
     71         return NULL;
     72     else
     73         return szNewName;
     74 }
     75 
     76 
     77 
     78 
     79 SYNCML_DM_RET_STATUS_T
     80 DMSubscriptionManager::ProcessUpdateForSubscription(SyncML_DM_Archive * pArchive,
     81                                                     CPCHAR szPath,
     82                                                     SYNCML_DM_EVENT_ACTION_T nAction,
     83                                                     CPCHAR szNewName)
     84 {
     85 
     86 
     87     if ( pArchive == NULL )
     88         return SYNCML_DM_FAIL;
     89 
     90     DMString strTrackedPath = szPath;
     91 
     92     PDMConfigItem pItem;
     93     CPCHAR pNewName = NULL;
     94     BOOLEAN bIsEnabledByParent = FALSE;
     95 
     96 
     97     if ( IsEnabled( strTrackedPath,nAction,pItem, bIsEnabledByParent,FALSE) )
     98     {
     99           pNewName =  ResetName(bIsEnabledByParent,(DMSubscriptionItem*)(pItem.GetPtr()),szNewName);
    100           pArchive->GetEventLogger().OnNodeChanged( strTrackedPath,
    101                                                     nAction,
    102                                                     pItem,
    103                                                     bIsEnabledByParent,
    104                                                     pNewName);
    105     }
    106 
    107     return SYNCML_DM_SUCCESS;
    108 
    109 }
    110 
    111 
    112 
    113 SYNCML_DM_RET_STATUS_T
    114 DMSubscriptionManager::ProcessUpdateForPlugin(SyncML_DM_Archive * pArchive,
    115                                               CPCHAR szPath,
    116                                               SYNCML_DM_EVENT_ACTION_T nAction,
    117                                               CPCHAR szNewName)
    118 {
    119 
    120     if ( pArchive == NULL )
    121         return SYNCML_DM_FAIL;
    122 
    123     DMString strTrackedPath = szPath;
    124 
    125     PDMPlugin pPlugin;
    126     CPCHAR pNewName = NULL;
    127     BOOLEAN bIsEnabledByParent = FALSE;
    128 
    129 
    130     if ( !IsEnabled(strTrackedPath,nAction,pPlugin, bIsEnabledByParent,FALSE) )
    131          return SYNCML_DM_SUCCESS;
    132 
    133     pNewName =  ResetName(bIsEnabledByParent,(DMSubscriptionItem*)(pPlugin.GetPtr()),szNewName);
    134     pArchive->GetEventLogger().OnNodeChanged(strTrackedPath,
    135                                              nAction,
    136                                              pPlugin,
    137                                              bIsEnabledByParent,
    138                                              pNewName);
    139     return SYNCML_DM_SUCCESS;
    140 
    141 }
    142 
    143 
    144 SYNCML_DM_RET_STATUS_T
    145 DMSubscriptionManager::OnNodeChanged(SyncML_DM_Archive * pArchive,
    146                                      CPCHAR szPath,
    147                                      SYNCML_DM_EVENT_ACTION_T nAction,
    148                                      CPCHAR szNewName)
    149 {
    150 
    151     Deserialize();
    152 
    153 
    154     XPL_LOG_DM_TMN_Debug(("Received update notification for path= %s\n", szPath));
    155 
    156     UpdateEvents(szPath,nAction,szNewName);
    157     if ( !pArchive->GetEventLogger().IsIgnoreSubscriptionEvent(szPath, nAction,szNewName) )
    158         ProcessUpdateForSubscription(pArchive,szPath,nAction,szNewName);
    159     if ( !pArchive->GetEventLogger().IsIgnorePluginEvent(szPath, nAction,szNewName) )
    160         ProcessUpdateForPlugin(pArchive,szPath,nAction,szNewName);
    161     return SYNCML_DM_SUCCESS;
    162 
    163 }
    164 
    165 
    166 
    167 SYNCML_DM_RET_STATUS_T
    168 DMSubscriptionManager::ProcessDeleteForSubscription(SyncML_DM_Archive * pArchive,
    169                                                     CPCHAR szPath,
    170                                                     BOOLEAN bIsChild )
    171 {
    172     if ( pArchive == NULL )
    173         return SYNCML_DM_FAIL;
    174 
    175     DMString strTrackedPath = szPath;
    176     PDMConfigItem pItem;
    177     BOOLEAN bIsEnabledByParent = FALSE;
    178 
    179 
    180     if ( IsEnabled(strTrackedPath,SYNCML_DM_EVENT_DELETE,pItem, bIsEnabledByParent,bIsChild) )
    181     {
    182          pArchive->GetEventLogger().OnNodeChanged(strTrackedPath,
    183                                                   SYNCML_DM_EVENT_DELETE,
    184                                                   pItem,
    185                                                   bIsEnabledByParent);
    186     }
    187 
    188     return SYNCML_DM_SUCCESS;
    189 }
    190 
    191 
    192 
    193 SYNCML_DM_RET_STATUS_T
    194 DMSubscriptionManager::ProcessDeleteForPlugin(SyncML_DM_Archive * pArchive,
    195                                               CPCHAR szPath,
    196                                               BOOLEAN bIsChild)
    197 {
    198     if ( pArchive == NULL )
    199         return SYNCML_DM_FAIL;
    200 
    201     DMString strTrackedPath = szPath;
    202     PDMPlugin pPlugin;
    203     BOOLEAN bIsEnabledByParent = FALSE;
    204 
    205     if ( IsEnabled(strTrackedPath,SYNCML_DM_EVENT_DELETE,pPlugin,bIsEnabledByParent,bIsChild) )
    206     {
    207         pArchive->GetEventLogger().OnNodeChanged(strTrackedPath,
    208                                                  SYNCML_DM_EVENT_DELETE,
    209                                                  pPlugin,
    210                                                  bIsEnabledByParent);
    211     }
    212 
    213     return SYNCML_DM_SUCCESS;
    214 }
    215 
    216 
    217 SYNCML_DM_RET_STATUS_T
    218 DMSubscriptionManager::OnNodeDeleted(SyncML_DM_Archive * pArchive,
    219                                      CPCHAR szPath,
    220                                      const DMStringVector & aDeletedChildren )
    221 {
    222 
    223     Deserialize();
    224 
    225     XPL_LOG_DM_TMN_Debug(("Received delete notification for path= %s\n", szPath));
    226 
    227 
    228     CleanEvents(szPath);
    229     if ( !pArchive->GetEventLogger().IsIgnoreSubscriptionEvent(szPath,SYNCML_DM_EVENT_DELETE) )
    230         ProcessDeleteForSubscription(pArchive,szPath,FALSE);
    231     if ( !pArchive->GetEventLogger().IsIgnorePluginEvent(szPath,SYNCML_DM_EVENT_DELETE) )
    232         ProcessDeleteForPlugin(pArchive,szPath,FALSE);
    233 
    234     for (INT32 index=0; index<aDeletedChildren.size(); index++)
    235     {
    236          CPCHAR szChildPath = aDeletedChildren[index].c_str();
    237          if ( !pArchive->GetEventLogger().IsIgnoreSubscriptionEvent(szChildPath,SYNCML_DM_EVENT_DELETE) )
    238             ProcessDeleteForSubscription(pArchive,szChildPath,TRUE);
    239          if ( !pArchive->GetEventLogger().IsIgnorePluginEvent(szChildPath,SYNCML_DM_EVENT_DELETE) )
    240             ProcessDeleteForPlugin(pArchive,szChildPath,TRUE);
    241     }
    242 
    243     return SYNCML_DM_SUCCESS;
    244 }
    245 
    246 
    247 
    248 
    249 BOOLEAN
    250 DMSubscriptionManager::IsParentEnabled(DMString & strPath,
    251                                        CPCHAR szParent,
    252                                        SYNCML_DM_EVENT_ACTION_T nAction,
    253                                        DMEventSubscription * pItem) const
    254 {
    255 
    256     if ( pItem->IsEnabled( nAction, m_pTree ) )
    257     {
    258         if ( pItem->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
    259         {
    260             strPath = szParent;
    261             return TRUE;
    262         }
    263         else
    264             if ( pItem->GetType() == SYNCML_DM_EVENT_DETAIL )
    265                 return TRUE;
    266    }
    267    return FALSE;
    268 }
    269 
    270 
    271 
    272 BOOLEAN
    273 DMSubscriptionManager::IsParentEnabledBySubscription(DMString & strPath,
    274                                                      CPCHAR szParent,
    275                                                      SYNCML_DM_EVENT_ACTION_T nAction,
    276                                                      PDMConfigItem & pItem) const
    277 {
    278 
    279     INT32 index = Find(szParent,pItem);
    280     if ( index == -1 )
    281         return FALSE;
    282 
    283     DMSubscriptionItem * pSubscriptionItem = (DMSubscriptionItem*)(pItem.GetPtr());
    284 
    285     return IsParentEnabled(strPath,
    286                            szParent,
    287                            nAction,
    288                            (DMEventSubscription*)pSubscriptionItem);
    289 }
    290 
    291 
    292 
    293 BOOLEAN
    294 DMSubscriptionManager::IsParentEnabledByPlugin(DMString & strPath,
    295                                              CPCHAR szParent,
    296                                              SYNCML_DM_EVENT_ACTION_T nAction,
    297                                              PDMPlugin & pPlugin) const
    298 {
    299     DMPluginManager & oPluginManager = m_pTree->GetPluginManager();
    300 
    301     pPlugin = oPluginManager.FindCommitPlugin(szParent);
    302     if ( pPlugin == NULL )
    303         return FALSE;
    304 
    305    return IsParentEnabled(strPath,
    306                           szParent,
    307                           nAction,
    308                           (DMEventSubscription*)(pPlugin.GetPtr()));
    309 }
    310 
    311 
    312 
    313 BOOLEAN
    314 DMSubscriptionManager::IsEnabledBySubscription(DMString & strPath,
    315                                             SYNCML_DM_EVENT_ACTION_T nAction,
    316                                             PDMConfigItem & pItem,
    317                                             BOOLEAN & bIsEnabledByParent,
    318                                             BOOLEAN bIsChild) const
    319 {
    320 
    321     bIsEnabledByParent = FALSE;
    322     INT32 index = Find(strPath,pItem);
    323     if ( index != -1 )
    324     {
    325           if ( ((DMSubscriptionItem*)(pItem.GetPtr()))->IsEnabled( nAction,m_pTree) )
    326               return TRUE;
    327     }
    328 
    329     if ( bIsChild )
    330         return FALSE;
    331 
    332     DMURI oURI(TRUE,strPath.GetBuffer());
    333     CPCHAR pParent = NULL;
    334     bIsEnabledByParent = TRUE;
    335 
    336     while ( (pParent = oURI.getParentURI()) != NULL )
    337     {
    338          if ( IsParentEnabledBySubscription(strPath,pParent,nAction, pItem) )
    339               return TRUE;
    340     }
    341 
    342     return FALSE;
    343 }
    344 
    345 
    346 
    347 
    348 
    349 BOOLEAN
    350 DMSubscriptionManager::IsEnabledByPlugin(DMString & strPath,
    351                                         SYNCML_DM_EVENT_ACTION_T nAction,
    352                                         PDMPlugin & pPlugin,
    353                                         BOOLEAN & bIsEnabledByParent,
    354                                         BOOLEAN bIsChild) const
    355 {
    356 
    357     DMPluginManager & oPluginManager = m_pTree->GetPluginManager();
    358 
    359     bIsEnabledByParent = FALSE;
    360     pPlugin = oPluginManager.FindCommitPlugin(strPath);
    361     if ( pPlugin != NULL )
    362     {
    363           if ( pPlugin->IsEnabled( nAction,m_pTree) )
    364                return TRUE;
    365     }
    366 
    367     if ( bIsChild )
    368         return FALSE;
    369 
    370     DMURI oURI(TRUE,strPath.GetBuffer());
    371     CPCHAR pParent = NULL;
    372     bIsEnabledByParent = TRUE;
    373 
    374     while ( (pParent = oURI.getParentURI()) != NULL )
    375     {
    376         if ( IsParentEnabledByPlugin(strPath,pParent,nAction, pPlugin) )
    377             return TRUE;
    378     }
    379 
    380     return FALSE;
    381 }
    382 
    383 
    384 SYNCML_DM_RET_STATUS_T
    385 DMSubscriptionManager::GetLogPath(DMString & strPath,
    386                                   CPCHAR szMDF) const
    387 {
    388 
    389     UINT32 nSegmentCount = 0;
    390     {
    391         DMParser oParser(szMDF);
    392         nSegmentCount = oParser.getSegmentsCount();
    393     }
    394     char * pEnd = NULL;
    395 
    396     {
    397          DMParser oParser(strPath);
    398          if (  nSegmentCount == oParser.getSegmentsCount() )
    399             return SYNCML_DM_SUCCESS;
    400 
    401          for (INT32 count=0; count<=(INT32)nSegmentCount; count++)
    402             pEnd = (char*)oParser.nextSegment();
    403     }
    404 
    405     if ( pEnd != NULL )
    406     {
    407         pEnd--;
    408         *pEnd = '\0'; // pEnd can't be NULL
    409     }
    410     return SYNCML_DM_SUCCESS;
    411 
    412 }
    413 
    414 
    415 
    416 
    417 BOOLEAN
    418 DMSubscriptionManager:: IsEnabled(DMString & strPath,
    419                                   SYNCML_DM_EVENT_ACTION_T nAction,
    420                                   PDMConfigItem & pItem,
    421                                   BOOLEAN & bIsEnabledByParent,
    422                                   BOOLEAN bIsChild) const
    423 {
    424 
    425     BOOLEAN bIsEnabled = FALSE;
    426 
    427     if ( !m_aConfig.size() )
    428         return FALSE;
    429 
    430     bIsEnabled = IsEnabledBySubscription(strPath,
    431                                          nAction,
    432                                          pItem,
    433                                          bIsEnabledByParent,
    434                                          bIsChild);
    435     if ( bIsEnabled )
    436         return TRUE;
    437 
    438     DMString strMDF;
    439     SYNCML_DM_RET_STATUS_T ret_status;
    440     DMMetaDataManager & m_oMDFObj =  m_pTree->GetMetaDataManager();
    441     ret_status = m_oMDFObj.GetPath(strPath, strMDF);
    442 
    443     if ( ret_status == SYNCML_DM_SUCCESS &&  strMDF != strPath)
    444     {
    445         bIsEnabled = IsEnabledBySubscription(strMDF,
    446                                              nAction,
    447                                              pItem,
    448                                              bIsEnabledByParent,
    449                                              bIsChild);
    450 
    451         if ( bIsEnabled && bIsChild )
    452         {
    453             if ( ((DMSubscriptionItem*)(pItem.GetPtr()))->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
    454                 GetLogPath(strPath,(CPCHAR)strMDF);
    455 
    456             return TRUE;
    457         }
    458     }
    459 
    460     return bIsEnabled;
    461 
    462 }
    463 
    464 
    465 
    466 BOOLEAN
    467 DMSubscriptionManager:: IsEnabled(DMString & strPath,
    468                                   SYNCML_DM_EVENT_ACTION_T nAction,
    469                                   PDMPlugin & pPlugin,
    470                                   BOOLEAN & bIsEnabledByParent,
    471                                   BOOLEAN bIsChild) const
    472 {
    473 
    474     BOOLEAN bIsEnabled = FALSE;
    475 
    476     bIsEnabled = IsEnabledByPlugin(strPath,
    477                                    nAction,
    478                                    pPlugin,
    479                                    bIsEnabledByParent,
    480                                    bIsChild);
    481 
    482     if ( bIsEnabled )
    483         return TRUE;
    484 
    485     DMString strMDF;
    486     SYNCML_DM_RET_STATUS_T ret_status;
    487     DMMetaDataManager & m_oMDFObj =  m_pTree->GetMetaDataManager();
    488     ret_status = m_oMDFObj.GetPath(strPath, strMDF);
    489 
    490     if ( ret_status == SYNCML_DM_SUCCESS &&  strMDF != strPath)
    491     {
    492         bIsEnabled = IsEnabledByPlugin(strMDF,
    493                                        nAction,
    494                                        pPlugin,
    495                                        bIsEnabledByParent,
    496                                        bIsChild);
    497 
    498         if ( bIsEnabled && bIsChild == FALSE )
    499         {
    500             if ( (pPlugin.GetPtr())->GetType() == SYNCML_DM_EVENT_CUMULATIVE )
    501                 GetLogPath(strPath,(CPCHAR)strMDF);
    502             return TRUE;
    503         }
    504     }
    505 
    506     return bIsEnabled;
    507 
    508 }
    509 
    510 
    511 
    512 
    513 SYNCML_DM_RET_STATUS_T
    514 DMSubscriptionManager::EnableEvent(CPCHAR szPath,
    515                                    const DmtEventSubscription & oEvent)
    516 {
    517 
    518     CheckLocking();
    519 
    520     DMString szMDF;
    521     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    522     DMMetaDataManager & m_oMDFObj =  m_pTree->GetMetaDataManager();
    523     dm_stat = m_oMDFObj.GetPath(szPath, szMDF);
    524     if ( dm_stat != SYNCML_DM_SUCCESS )
    525     {
    526         XPL_LOG_DM_TMN_Error(("MDF path isn't found, path = %s\n", szPath));
    527         return dm_stat;
    528     }
    529 
    530     Delete(szPath);
    531 
    532     DMSubscriptionItem * pEventItem = new DMSubscriptionItem(szPath);
    533     if ( pEventItem == NULL )
    534     {
    535         XPL_LOG_DM_TMN_Error(("Not enough memory\n"));
    536         return SYNCML_DM_DEVICE_FULL;
    537     }
    538 
    539     dm_stat = pEventItem->Set(oEvent);
    540     if ( dm_stat != SYNCML_DM_SUCCESS )
    541     {
    542         XPL_LOG_DM_TMN_Error(("Event data can't be set for path = %s\n", szPath));
    543         delete pEventItem;
    544         return dm_stat;
    545     }
    546 
    547     m_aConfig.push_back(PDMConfigItem((DMConfigItem*)pEventItem));
    548     m_bChanged = true;
    549     return dm_stat;
    550 }
    551 
    552 
    553 void DMSubscriptionManager::GetFileName(DMString & strFileName)
    554 {
    555     m_pEnv->GetWFSFullPath(DM_DEFAULT_EVENT_FILENAME,strFileName);
    556 }
    557 
    558 DMConfigItem * DMSubscriptionManager::AllocateConfigItem()
    559 {
    560     return new DMSubscriptionItem();
    561 }
    562 
    563 
    564 SYNCML_DM_RET_STATUS_T DMSubscriptionManager::GetEvent(CPCHAR szPath,
    565         DmtEventSubscription & oEvent)
    566 {
    567     PDMConfigItem pItem;
    568     SYNCML_DM_RET_STATUS_T dm_stat = SYNCML_DM_SUCCESS;
    569 
    570     Deserialize();
    571 
    572     dm_stat = Get(szPath,pItem);
    573 
    574     if ( dm_stat != SYNCML_DM_SUCCESS )
    575         return dm_stat;
    576 
    577     DMSubscriptionItem * ptr = ((DMSubscriptionItem*)(pItem.GetPtr()));
    578 
    579     return oEvent.Set(ptr->GetAction(),
    580                        ptr->GetType(),
    581                        ptr->GetTopic(),
    582                        ptr->GetPrincipals(TRUE),
    583                        ptr->GetPrincipals(FALSE));
    584 }
    585