|
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 System.ComponentModel; |
|
22 using System.Threading; |
|
23 using SymbianUtils; |
|
24 using SymbianUtils.Tracer; |
|
25 using SymbianUtils.PluginManager; |
|
26 using SymbianStructuresLib.Compression.Common; |
|
27 using SymbianImageLib.Common.Content; |
|
28 using SymbianImageLib.Common.Header; |
|
29 using SymbianImageLib.Common.Streams; |
|
30 using SymbianImageLib.Common.Factory; |
|
31 |
|
32 namespace SymbianImageLib.Common.Image |
|
33 { |
|
34 public abstract class SIImage : DisposableObject, ITracer, IEnumerable<SIContent> |
|
35 { |
|
36 #region Factory |
|
37 public static SIImage New( ITracer aTracer, string aFileName ) |
|
38 { |
|
39 return SIImage.New( aTracer, new FileInfo( aFileName ) ); |
|
40 } |
|
41 |
|
42 public static SIImage New( ITracer aTracer, FileInfo aFileInfo ) |
|
43 { |
|
44 SIImage ret = null; |
|
45 // |
|
46 if ( aFileInfo.Exists ) |
|
47 { |
|
48 Stream fileStream = aFileInfo.OpenRead(); |
|
49 try |
|
50 { |
|
51 // If creating the image succeeds then we transfer ownership |
|
52 // of the file stream |
|
53 ret = SIImage.New( aTracer, fileStream, aFileInfo.FullName ); |
|
54 if ( ret == null ) |
|
55 { |
|
56 fileStream.Close(); |
|
57 } |
|
58 } |
|
59 catch( Exception ) |
|
60 { |
|
61 fileStream.Close(); |
|
62 } |
|
63 } |
|
64 // |
|
65 return ret; |
|
66 } |
|
67 |
|
68 public static SIImage New( ITracer aTracer, Stream aStream, string aName ) |
|
69 { |
|
70 SIImage ret = null; |
|
71 // |
|
72 PluginManager<SIFactory> imageFactories = new PluginManager<SIFactory>(); |
|
73 imageFactories.LoadFromCallingAssembly(); |
|
74 foreach ( SIFactory factory in imageFactories ) |
|
75 { |
|
76 ret = factory.CreateImage( aTracer, aStream, aName ); |
|
77 if ( ret != null ) |
|
78 { |
|
79 break; |
|
80 } |
|
81 } |
|
82 // |
|
83 return ret; |
|
84 } |
|
85 #endregion |
|
86 |
|
87 #region Constructors |
|
88 internal SIImage( ITracer aTracer, SIStream aStream, string aName ) |
|
89 { |
|
90 iName = aName; |
|
91 iTracer = aTracer; |
|
92 iStream = aStream; |
|
93 } |
|
94 #endregion |
|
95 |
|
96 #region Framework API |
|
97 public abstract SIHeader Header |
|
98 { |
|
99 get; |
|
100 } |
|
101 #endregion |
|
102 |
|
103 #region API |
|
104 public bool Contains( SIContent aContent ) |
|
105 { |
|
106 lock ( iContentList ) |
|
107 { |
|
108 bool ret = iContentList.Contains( aContent ); |
|
109 return ret; |
|
110 } |
|
111 } |
|
112 |
|
113 internal void Remove( SIContent aContent ) |
|
114 { |
|
115 lock ( iContentList ) |
|
116 { |
|
117 iContentList.Remove( aContent ); |
|
118 } |
|
119 } |
|
120 #endregion |
|
121 |
|
122 #region Properties |
|
123 public int Count |
|
124 { |
|
125 get |
|
126 { |
|
127 lock ( iContentList ) |
|
128 { |
|
129 return iContentList.Count; |
|
130 } |
|
131 } |
|
132 } |
|
133 |
|
134 public TSymbianCompressionType CompressionType |
|
135 { |
|
136 get { return Header.CompressionType; } |
|
137 } |
|
138 |
|
139 public SIContent this[ int aIndex ] |
|
140 { |
|
141 get |
|
142 { |
|
143 lock ( iContentList ) |
|
144 { |
|
145 return iContentList[ aIndex ]; |
|
146 } |
|
147 } |
|
148 } |
|
149 |
|
150 public SIContent this[ string aFileName ] |
|
151 { |
|
152 get |
|
153 { |
|
154 lock ( iContentList ) |
|
155 { |
|
156 return iContentList[ aFileName ]; |
|
157 } |
|
158 } |
|
159 } |
|
160 |
|
161 internal SIStream Stream |
|
162 { |
|
163 get { return iStream; } |
|
164 set |
|
165 { |
|
166 System.Diagnostics.Debug.Assert( iStream != value ); |
|
167 // |
|
168 if ( iStream != null ) |
|
169 { |
|
170 iStream.Close(); |
|
171 } |
|
172 iStream = value; |
|
173 } |
|
174 } |
|
175 |
|
176 public string Name |
|
177 { |
|
178 get { return iName; } |
|
179 } |
|
180 #endregion |
|
181 |
|
182 #region Internal methods |
|
183 protected void RegisterFile( SIContent aFile ) |
|
184 { |
|
185 iContentList.Add( aFile ); |
|
186 } |
|
187 |
|
188 private SIContentList FileList |
|
189 { |
|
190 get { return iContentList; } |
|
191 } |
|
192 #endregion |
|
193 |
|
194 #region From ITracer |
|
195 public void Trace( string aMessage ) |
|
196 { |
|
197 iTracer.Trace( aMessage ); |
|
198 } |
|
199 |
|
200 public void Trace( string aFormat, params object[] aParams ) |
|
201 { |
|
202 iTracer.Trace( aFormat, aParams ); |
|
203 } |
|
204 #endregion |
|
205 |
|
206 #region From DisposableObject |
|
207 protected override void CleanupManagedResources() |
|
208 { |
|
209 try |
|
210 { |
|
211 base.CleanupManagedResources(); |
|
212 } |
|
213 finally |
|
214 { |
|
215 if ( iStream != null ) |
|
216 { |
|
217 iStream.Close(); |
|
218 iStream = null; |
|
219 } |
|
220 } |
|
221 } |
|
222 #endregion |
|
223 |
|
224 #region From IEnumerable<SymbianImageContentFile> |
|
225 public IEnumerator<SIContent> GetEnumerator() |
|
226 { |
|
227 return iContentList.GetEnumerator(); |
|
228 } |
|
229 |
|
230 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() |
|
231 { |
|
232 return iContentList.GetEnumerator(); |
|
233 } |
|
234 #endregion |
|
235 |
|
236 #region Data members |
|
237 private readonly ITracer iTracer; |
|
238 private readonly string iName; |
|
239 private SIStream iStream = null; |
|
240 private SIContentList iContentList = new SIContentList(); |
|
241 #endregion |
|
242 } |
|
243 } |