|
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 |
|
22 namespace SymbianUtils.FileSystem |
|
23 { |
|
24 public class FSExtensionDescriptor |
|
25 { |
|
26 #region Constructors |
|
27 public FSExtensionDescriptor( string aDescription ) |
|
28 : this( aDescription, string.Empty ) |
|
29 { |
|
30 } |
|
31 |
|
32 public FSExtensionDescriptor( string aDescription, string aExtension ) |
|
33 { |
|
34 iDescription = aDescription.Trim(); |
|
35 Add( aExtension ); |
|
36 } |
|
37 |
|
38 public FSExtensionDescriptor( string aDescription, IEnumerable<string> aExtensions ) |
|
39 { |
|
40 iDescription = aDescription.Trim(); |
|
41 AddRange( aExtensions ); |
|
42 } |
|
43 |
|
44 public FSExtensionDescriptor( string aDescription, params string[] aExtensions ) |
|
45 { |
|
46 iDescription = aDescription.Trim(); |
|
47 AddRange( aExtensions ); |
|
48 } |
|
49 #endregion |
|
50 |
|
51 #region API |
|
52 public void Add( string aExtension ) |
|
53 { |
|
54 if ( !string.IsNullOrEmpty( aExtension ) ) |
|
55 { |
|
56 string ext = aExtension.Trim(); |
|
57 if ( !string.IsNullOrEmpty( ext ) ) |
|
58 { |
|
59 iExtensions.Add( ext ); |
|
60 } |
|
61 } |
|
62 } |
|
63 |
|
64 public void AddRange( IEnumerable<string> aExtensions ) |
|
65 { |
|
66 foreach ( string e in aExtensions ) |
|
67 { |
|
68 Add( e ); |
|
69 } |
|
70 } |
|
71 |
|
72 public bool ContainsExtension( string aExtension ) |
|
73 { |
|
74 string searchFor = aExtension.ToUpper(); |
|
75 Predicate<string> searcher = delegate( string aEntry ) |
|
76 { |
|
77 return ( aEntry.ToUpper() == searchFor ); |
|
78 }; |
|
79 // |
|
80 string ret = iExtensions.Find( searcher ); |
|
81 bool found = !string.IsNullOrEmpty( ret ); |
|
82 return found; |
|
83 } |
|
84 #endregion |
|
85 |
|
86 #region Properties |
|
87 public int Count |
|
88 { |
|
89 get { return iExtensions.Count; } |
|
90 } |
|
91 |
|
92 public string FirstExtension |
|
93 { |
|
94 get |
|
95 { |
|
96 string ret = string.Empty; |
|
97 // |
|
98 if ( Count > 0 ) |
|
99 { |
|
100 ret = this[ 0 ]; |
|
101 } |
|
102 // |
|
103 return ret; |
|
104 } |
|
105 } |
|
106 |
|
107 public string[] Extensions |
|
108 { |
|
109 get { return iExtensions.ToArray(); } |
|
110 } |
|
111 |
|
112 public string Description |
|
113 { |
|
114 get { return iDescription; } |
|
115 } |
|
116 |
|
117 public string this[ int aIndex ] |
|
118 { |
|
119 get { return iExtensions[ aIndex ]; } |
|
120 } |
|
121 #endregion |
|
122 |
|
123 #region Internal methods |
|
124 #endregion |
|
125 |
|
126 #region From System.Object |
|
127 public override int GetHashCode() |
|
128 { |
|
129 return iDescription.GetHashCode(); |
|
130 } |
|
131 |
|
132 public override string ToString() |
|
133 { |
|
134 // Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.* |
|
135 StringBuilder ret = new StringBuilder( Description ); |
|
136 // |
|
137 if ( ret.Length > 0 && iExtensions.Count > 0 ) |
|
138 { |
|
139 StringBuilder extList = new StringBuilder(); |
|
140 |
|
141 foreach ( string ext in iExtensions ) |
|
142 { |
|
143 extList.Append( ext ); |
|
144 extList.Append( ";" ); |
|
145 } |
|
146 |
|
147 // Strip final ";". |
|
148 extList.Length = extList.Length - 1; |
|
149 |
|
150 // Save as formatted extension |
|
151 string list = extList.ToString(); |
|
152 ret.AppendFormat( " ({0})|{1}", list, list ); |
|
153 } |
|
154 // |
|
155 return ret.ToString(); |
|
156 } |
|
157 #endregion |
|
158 |
|
159 #region Data members |
|
160 private readonly string iDescription; |
|
161 private List<string> iExtensions = new List<string>(); |
|
162 #endregion |
|
163 } |
|
164 } |