photosgallery/viewframework/uiutilities/src/glxactivemedialistregistry.cpp
changeset 0 4e91876724a2
equal deleted inserted replaced
-1:000000000000 0:4e91876724a2
       
     1 /*
       
     2 * Copyright (c) 2008-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:    Class that stores the active media list pointer
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 #include "glxactivemedialistregistry.h"
       
    22 
       
    23 #include <glxtracer.h>
       
    24 #include <glxpanic.h>
       
    25 #include <glxsingletonstore.h>
       
    26 #include <glxlog.h>
       
    27 #include "mglxactivemedialistchangeobserver.h"
       
    28 
       
    29 // -----------------------------------------------------------------------------
       
    30 // Return an instance
       
    31 // -----------------------------------------------------------------------------
       
    32 //
       
    33 EXPORT_C CGlxActiveMediaListRegistry* CGlxActiveMediaListRegistry::InstanceL(
       
    34         MGlxActiveMediaListChangeObserver* aObserver)
       
    35     {
       
    36     GLX_LOG_INFO1("CGlxActiveMediaListRegistry::InstanceL: %x", aObserver);
       
    37     
       
    38     CGlxActiveMediaListRegistry* obj = CGlxSingletonStore::InstanceL(&NewL);
       
    39     
       
    40     // Add the observer if has not already been added
       
    41     if (aObserver && KErrNotFound == obj->iObservers.Find(aObserver))
       
    42         {
       
    43         TRAPD(err, obj->iObservers.AppendL(aObserver));
       
    44         // Close tls reference and leave if AppendL leaves.
       
    45         // Otherwise there will be one reference that will be never closed
       
    46         if (KErrNone != err)
       
    47             {
       
    48             CGlxSingletonStore::Close(obj);
       
    49             User::Leave(err);
       
    50             }
       
    51         }
       
    52     return obj;
       
    53     }
       
    54     
       
    55 // -----------------------------------------------------------------------------
       
    56 // Return new object
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 CGlxActiveMediaListRegistry* CGlxActiveMediaListRegistry::NewL() 
       
    60     {
       
    61     CGlxActiveMediaListRegistry* obj = new (ELeave) CGlxActiveMediaListRegistry();
       
    62     // (No ConstructL necessary)
       
    63     return obj;    
       
    64     }
       
    65     
       
    66 // -----------------------------------------------------------------------------
       
    67 // Constructor
       
    68 // -----------------------------------------------------------------------------
       
    69 //
       
    70 CGlxActiveMediaListRegistry::CGlxActiveMediaListRegistry()
       
    71     {
       
    72     }
       
    73 
       
    74 // -----------------------------------------------------------------------------
       
    75 // Destructor
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CGlxActiveMediaListRegistry::~CGlxActiveMediaListRegistry()
       
    79     {
       
    80     iObservers.Close();
       
    81     }
       
    82 
       
    83 // -----------------------------------------------------------------------------
       
    84 // Close a reference
       
    85 // -----------------------------------------------------------------------------
       
    86 //
       
    87 void CGlxActiveMediaListRegistry::Close(MGlxActiveMediaListChangeObserver* aObserver)
       
    88     {
       
    89     TRACER("CGlxActiveMediaListRegistry::Close");
       
    90     // Remove the observer if it has been added earlier
       
    91     TInt index = iObservers.Find(aObserver);
       
    92     if (KErrNotFound != index)
       
    93         {
       
    94         iObservers.Remove(index);
       
    95         }
       
    96         
       
    97     // Decrease reference count, and delete if last one
       
    98     // May delete this object
       
    99     CGlxSingletonStore::Close(this);
       
   100     // Member variable access not safe after CGlxTls::Close()
       
   101     }
       
   102     
       
   103 // -----------------------------------------------------------------------------
       
   104 // Register an active media list
       
   105 // -----------------------------------------------------------------------------
       
   106 //
       
   107 EXPORT_C void CGlxActiveMediaListRegistry::RegisterActiveMediaList(MGlxMediaList* aMediaList)
       
   108     {
       
   109     GLX_LOG_INFO1("CGlxActiveMediaListRegistry::RegisterActiveMediaList: %x", aMediaList);
       
   110     // Add to registration count if the new list is the same as the existing one
       
   111     if (aMediaList == iActiveMediaList) 
       
   112         {
       
   113         __ASSERT_DEBUG(!iRegisteredTwice, Panic(EGlxPanicIllegalState)); // Cannot register three times
       
   114         GLX_LOG_INFO("CGlxActiveMediaListRegistry::RegisterActiveMediaList: Media list registered twice");
       
   115         // The same media list has been registered twice
       
   116         iRegisteredTwice = ETrue;
       
   117         }
       
   118 
       
   119     // If active list has changed, pick up the new list and notify observers
       
   120     if (iActiveMediaList != aMediaList)
       
   121         {
       
   122         GLX_LOG_INFO("CGlxActiveMediaListRegistry::RegisterActiveMediaList: Media list changed");
       
   123         // Pick up the new list
       
   124         iActiveMediaList = aMediaList;
       
   125         
       
   126         // This is the first registration
       
   127         iRegisteredTwice = EFalse;
       
   128         
       
   129         // Notify observers
       
   130         NotifyObservers();
       
   131         }
       
   132     }
       
   133  
       
   134 // -----------------------------------------------------------------------------
       
   135 // Deregister an active media list
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 EXPORT_C void CGlxActiveMediaListRegistry::DeregisterActiveMediaList(MGlxMediaList* aMediaList)
       
   139     {
       
   140     GLX_LOG_INFO1("CGlxActiveMediaListRegistry::DeregisterActiveMediaList: %x", aMediaList);
       
   141     // Ignore deregistration if another list has already been registered
       
   142     // (Another view was activated, and it registered a list)
       
   143     if (aMediaList == iActiveMediaList) 
       
   144         {
       
   145         // The list may be registered twice if the new and old view
       
   146         // registered the same list. 
       
   147         if (iRegisteredTwice)   
       
   148             {
       
   149             GLX_LOG_INFO("CGlxActiveMediaListRegistry::DeregisterActiveMediaList: Remove second registration");
       
   150             // Remove second registration
       
   151             iRegisteredTwice = EFalse;
       
   152             }
       
   153         else 
       
   154             {
       
   155             GLX_LOG_INFO("CGlxActiveMediaListRegistry::DeregisterActiveMediaList: Setting active list to NULL");
       
   156             // No new media list has been registered and current one is being
       
   157             // deregistered, so set active list to NULL
       
   158             iActiveMediaList = NULL;
       
   159             
       
   160             // Notify observers
       
   161             NotifyObservers();
       
   162             }
       
   163         }
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // Notify observers
       
   168 // -----------------------------------------------------------------------------
       
   169 //
       
   170 void CGlxActiveMediaListRegistry::NotifyObservers() 
       
   171     {
       
   172     // Notify observers
       
   173     for (TInt i = iObservers.Count() - 1; i >= 0; i--)
       
   174         {
       
   175         iObservers[i]->HandleActiveMediaListChanged();
       
   176         }
       
   177     }
       
   178     
       
   179 // -----------------------------------------------------------------------------
       
   180 // Return active media list
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 MGlxMediaList* CGlxActiveMediaListRegistry::ActiveMediaList() const 
       
   184     {
       
   185     GLX_LOG_INFO("CGlxActiveMediaListRegistry::ActiveMediaList()");
       
   186     return iActiveMediaList;
       
   187     }