crashanalysercmd/PerfToolsSharedLibraries/Engine/SymbianDebugLib/Entity/Manager/DbgEntityManager.cs
changeset 0 818e61de6cd1
equal deleted inserted replaced
-1:000000000000 0:818e61de6cd1
       
     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:
       
    15 *
       
    16 */
       
    17 using System;
       
    18 using System.Collections.Generic;
       
    19 using System.Text;
       
    20 using System.IO;
       
    21 using SymbianUtils;
       
    22 using SymbianUtils.Settings;
       
    23 using SymbianUtils.FileSystem;
       
    24 using SymbianDebugLib.Engine;
       
    25 using SymbianDebugLib.PluginAPI;
       
    26 using SymbianDebugLib.Entity.Descriptors;
       
    27 
       
    28 namespace SymbianDebugLib.Entity.Manager
       
    29 {
       
    30     public class DbgEntityManager : IEnumerable<DbgEntity>, IXmlSettingsExtended
       
    31     {
       
    32         #region Constructors
       
    33         internal DbgEntityManager( DbgEngine aEngine )
       
    34         {
       
    35             iEngine = aEngine;
       
    36         }
       
    37         #endregion
       
    38 
       
    39         #region API
       
    40         public void Clear()
       
    41         {
       
    42             foreach ( KeyValuePair<string, DbgEntityListCategorised> kvp in iLists )
       
    43             {
       
    44                 DbgEntityListCategorised list = kvp.Value;
       
    45 
       
    46                 // Tell the descriptor it is being cleared
       
    47                 list.Descriptor.OnCleared();
       
    48 
       
    49                 // Get rid of the list
       
    50                 list.Dispose();
       
    51             }
       
    52             //
       
    53             iLists.Clear();
       
    54             iDisplayOrderList.Clear();
       
    55             Engine.OnCleared();
       
    56         }
       
    57 
       
    58         public DbgEntity Add( FSEntity aFSEntity )
       
    59         {
       
    60             DbgEntityDescriptorManager descManager = Engine.DescriptorManager;
       
    61             //
       
    62             DbgEntity entity = descManager.Create( aFSEntity );
       
    63             if ( entity != null )
       
    64             {
       
    65                 SaveEntity( entity );
       
    66             }
       
    67             //
       
    68             return entity;
       
    69         }
       
    70 
       
    71         public DbgEntity AddFile( FileInfo aFile )
       
    72         {
       
    73             return Add( new FSEntityFile( aFile ) );
       
    74         }
       
    75 
       
    76         public DbgEntity AddDirectory( DirectoryInfo aDirectory )
       
    77         {
       
    78             return Add( new FSEntityDirectory( aDirectory ) );
       
    79         }
       
    80 
       
    81         public void Remove( DbgEntity aEntity )
       
    82         {
       
    83             DbgEntityDescriptor descriptor = aEntity.Descriptor;
       
    84             if ( descriptor != null )
       
    85             {
       
    86                 DbgEntityListCategorised list;
       
    87                 if ( iLists.TryGetValue( descriptor.CategoryName, out list ) )
       
    88                 {
       
    89                     // Try to find the entity from the list and, if found,
       
    90                     // destroy it.
       
    91                     using ( DbgEntity removed = list.Remove( aEntity ) )
       
    92                     {
       
    93                         if ( removed != null )
       
    94                         {
       
    95                             // Tell the entity
       
    96                             removed.OnRemoved();
       
    97 
       
    98                             // Tell everybody else
       
    99                             Engine.OnRemoved( removed );
       
   100                         }
       
   101                     }
       
   102                 }
       
   103                 if ( iDisplayOrderList.TryGetValue( descriptor, out list ) )
       
   104                 {
       
   105                     iDisplayOrderList.Remove( descriptor );
       
   106                 }
       
   107             }
       
   108         }
       
   109 
       
   110         public bool IsReadyToPrime( out string aErrorList )
       
   111         {
       
   112             bool ready = true;
       
   113             aErrorList = string.Empty;
       
   114             //
       
   115             foreach ( DbgEntity e in this )
       
   116             {
       
   117                 if ( !e.IsReadyToPrime( out aErrorList ) )
       
   118                 {
       
   119                     ready = false;
       
   120                     break;
       
   121                 }
       
   122             }
       
   123             //
       
   124             return ready;
       
   125         }
       
   126 
       
   127         public bool Contains( FSEntity aFSEntity )
       
   128         {
       
   129             bool ret = false;
       
   130             //
       
   131             foreach ( KeyValuePair<DbgEntityDescriptor, DbgEntityListCategorised> kvp in iDisplayOrderList )
       
   132             {
       
   133                 DbgEntityListCategorised list = kvp.Value;
       
   134                 if ( list.Contains( aFSEntity ) )
       
   135                 {
       
   136                     ret = true;
       
   137                     break;
       
   138                 }
       
   139             }
       
   140             //
       
   141             return ret;
       
   142         }
       
   143         #endregion
       
   144 
       
   145         #region Properties
       
   146         public int Count
       
   147         {
       
   148             get
       
   149             {
       
   150                 int ret = 0;
       
   151                 //
       
   152                 foreach ( KeyValuePair<DbgEntityDescriptor, DbgEntityListCategorised> kvp in iDisplayOrderList )
       
   153                 {
       
   154                     ret += kvp.Value.Count;
       
   155                 }
       
   156                 //
       
   157                 return ret;
       
   158             }
       
   159         }
       
   160 
       
   161         public DbgEntity this[ int aIndex ]
       
   162         {
       
   163             get
       
   164             {
       
   165                 int i = 0;
       
   166                 DbgEntity ret = null;
       
   167                 //
       
   168                 foreach ( KeyValuePair<DbgEntityDescriptor, DbgEntityListCategorised> kvp in iDisplayOrderList )
       
   169                 {
       
   170                     DbgEntityListCategorised list = kvp.Value;
       
   171                     int listCount = list.Count;
       
   172                     if ( aIndex < ( i + listCount ) )
       
   173                     {
       
   174                         ret = list[ aIndex - i ];
       
   175                         break;
       
   176                     }
       
   177                     else
       
   178                     {
       
   179                         i += listCount;
       
   180                     }
       
   181                 }
       
   182                 //
       
   183                 if ( ret == null )
       
   184                 {
       
   185                     throw new ArgumentException( "aIndex is out of range" );
       
   186                 }
       
   187                 //
       
   188                 return ret;
       
   189            }
       
   190         }
       
   191 
       
   192         public DbgEngine Engine
       
   193         {
       
   194             get { return iEngine; }
       
   195         }
       
   196 
       
   197         public FSEntity[] FileSystemEntities
       
   198         {
       
   199             get
       
   200             {
       
   201                 List<FSEntity> entities = new List<FSEntity>();
       
   202                 //
       
   203                 foreach ( DbgEntity e in this )
       
   204                 {
       
   205                     FSEntity clone = FSEntity.New( e );
       
   206                     entities.Add( clone );
       
   207                 }
       
   208                 //
       
   209                 return entities.ToArray();
       
   210             }
       
   211         }
       
   212         #endregion
       
   213 
       
   214         #region Event handlers
       
   215         #endregion
       
   216 
       
   217         #region Settings related
       
   218         public void XmlSettingsSave( XmlSettings aSettings, string aCategory )
       
   219         {
       
   220             aSettings.Clear();
       
   221             aSettings[ aCategory, "__Count" ] = Count;
       
   222             int index = 0;
       
   223             foreach ( DbgEntity entity in this )
       
   224             {
       
   225                 string entityCategory = string.Format( "DbgEntity_{0:d5}", index++ );
       
   226 
       
   227                 // Get the category where we'll save the settings for this entity to...
       
   228                 XmlSettingCategory category = aSettings[ entityCategory ];
       
   229 
       
   230                 // Save entity specific settings
       
   231                 entity.Save( category );
       
   232             }
       
   233         }
       
   234 
       
   235         public void XmlSettingsLoad( XmlSettings aSettings, string aCategory )
       
   236         {
       
   237             Clear();
       
   238             //
       
   239             int count = aSettings.Load( aCategory, "__Count", 0 );
       
   240             for ( int i = 0; i < count; i++ )
       
   241             {
       
   242                 try
       
   243                 {
       
   244                     // Create category name and try to obtain it...
       
   245                     string entityCategory = string.Format( "DbgEntity_{0:d5}", i );
       
   246                     XmlSettingCategory category = aSettings[ entityCategory ];
       
   247                     if ( category != null )
       
   248                     {
       
   249                         // Make a new entity object based upon the type
       
   250                         DbgEntityDescriptorManager descManager = Engine.DescriptorManager;
       
   251                         DbgEntity entity = descManager.Create( category );
       
   252                         if ( entity != null )
       
   253                         {
       
   254                             SaveEntity( entity );
       
   255                         }
       
   256                     }
       
   257                 }
       
   258                 catch ( Exception )
       
   259                 {
       
   260                 }
       
   261             }
       
   262         }
       
   263         #endregion
       
   264 
       
   265         #region Internal properties
       
   266         #endregion
       
   267 
       
   268         #region Internal methods
       
   269         private void SaveEntity( DbgEntity aEntity )
       
   270         {
       
   271             // Check if the dictionary already contains an entity descriptor list
       
   272             DbgEntityDescriptor descriptor = aEntity.Descriptor;
       
   273             DbgEntityListCategorised list;
       
   274             if ( !iLists.TryGetValue( descriptor.CategoryName, out list ) )
       
   275             {
       
   276                 list = new DbgEntityListCategorised( Engine, descriptor );
       
   277 
       
   278                 // We create two lists. One sorts and indexes by display order, the other indexes by category.
       
   279                 iLists.Add( descriptor.CategoryName, list );
       
   280                 if ( !iDisplayOrderList.ContainsKey( descriptor ) )
       
   281                 {
       
   282                     iDisplayOrderList.Add( descriptor, list );
       
   283                 }
       
   284             }
       
   285            
       
   286             // Add to list
       
   287             list.Add( aEntity );
       
   288 
       
   289             // Notify engine
       
   290             Engine.OnAdded( aEntity );
       
   291         }
       
   292         #endregion
       
   293 
       
   294         #region From IEnumerable<DbgEntity>
       
   295         public IEnumerator<DbgEntity> GetEnumerator()
       
   296         {
       
   297             foreach ( KeyValuePair<DbgEntityDescriptor, DbgEntityListCategorised> kvp in iDisplayOrderList )
       
   298             {
       
   299                 foreach ( DbgEntity e in kvp.Value )
       
   300                 {
       
   301                     yield return e;
       
   302                 }
       
   303             }
       
   304         }
       
   305 
       
   306         System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
       
   307         {
       
   308             foreach ( KeyValuePair<DbgEntityDescriptor, DbgEntityListCategorised> kvp in iDisplayOrderList )
       
   309             {
       
   310                 foreach ( DbgEntity e in kvp.Value )
       
   311                 {
       
   312                     yield return e;
       
   313                 }
       
   314             }
       
   315         }
       
   316         #endregion
       
   317 
       
   318         #region Data members
       
   319         private readonly DbgEngine iEngine;
       
   320         private SortedList<DbgEntityDescriptor, DbgEntityListCategorised> iDisplayOrderList = new SortedList<DbgEntityDescriptor, DbgEntityListCategorised>();
       
   321         private Dictionary<string, DbgEntityListCategorised> iLists = new Dictionary<string, DbgEntityListCategorised>();
       
   322         #endregion
       
   323     }
       
   324 }