|
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 SymbianUtils; |
|
23 |
|
24 namespace SymbianUtils.FileSystem |
|
25 { |
|
26 public class FSDirectoryScanner : DisposableObject |
|
27 { |
|
28 #region Delegates & events |
|
29 public delegate void OperationStarted( FSDirectoryScanner aScanner ); |
|
30 public event OperationStarted Started; |
|
31 public delegate void ProgressHandler( FSDirectoryScanner aScanner, int aProgress, FileInfo aFile ); |
|
32 public event ProgressHandler Progress; |
|
33 public delegate void OperationComplete( FSDirectoryScanner aScanner ); |
|
34 public event OperationComplete Complete; |
|
35 #endregion |
|
36 |
|
37 #region Constructors |
|
38 public FSDirectoryScanner() |
|
39 { |
|
40 iWorker.ProgressChanged += new ProgressChangedEventHandler( Worker_ProgressChanged ); |
|
41 iWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler( Worker_RunWorkerCompleted ); |
|
42 iWorker.DoWork += new DoWorkEventHandler( Worker_DoWork ); |
|
43 } |
|
44 #endregion |
|
45 |
|
46 #region API |
|
47 public virtual void Start( DirectoryInfo aDirectory ) |
|
48 { |
|
49 iWorker.RunWorkerAsync( aDirectory ); |
|
50 } |
|
51 #endregion |
|
52 |
|
53 #region Framework API |
|
54 protected virtual void OnFileLocated( FileInfo aFile ) |
|
55 { |
|
56 } |
|
57 #endregion |
|
58 |
|
59 #region Worker event handlers |
|
60 private void Worker_DoWork( object aSender, DoWorkEventArgs aArgs ) |
|
61 { |
|
62 if ( Started != null ) |
|
63 { |
|
64 Started( this ); |
|
65 } |
|
66 // |
|
67 DirectoryInfo dir = aArgs.Argument as DirectoryInfo; |
|
68 if ( dir != null && dir.Exists ) |
|
69 { |
|
70 // Locate all the map files in the directory |
|
71 FileInfo[] fileInfoList = dir.GetFiles( "*.*" ); |
|
72 int count = fileInfoList.Length; |
|
73 for( int i=0; i<count; i++ ) |
|
74 { |
|
75 FileInfo file = fileInfoList[ i ]; |
|
76 // |
|
77 ReportProgress( file, i, count ); |
|
78 } |
|
79 } |
|
80 } |
|
81 |
|
82 private void Worker_RunWorkerCompleted( object aSender, RunWorkerCompletedEventArgs aArgs ) |
|
83 { |
|
84 if ( Complete != null ) |
|
85 { |
|
86 Complete( this ); |
|
87 } |
|
88 } |
|
89 |
|
90 private void Worker_ProgressChanged( object aSender, ProgressChangedEventArgs aArgs ) |
|
91 { |
|
92 if ( Progress != null ) |
|
93 { |
|
94 Progress( this, aArgs.ProgressPercentage, aArgs.UserState as FileInfo ); |
|
95 } |
|
96 } |
|
97 #endregion |
|
98 |
|
99 #region From DisposableObject |
|
100 protected override void CleanupManagedResources() |
|
101 { |
|
102 if ( iWorker != null ) |
|
103 { |
|
104 iWorker.Dispose(); |
|
105 iWorker = null; |
|
106 } |
|
107 base.CleanupManagedResources(); |
|
108 } |
|
109 #endregion |
|
110 |
|
111 #region Internal methods |
|
112 protected void ReportProgress( FileInfo aFile, int aFileIndex, int aFileCount ) |
|
113 { |
|
114 OnFileLocated( aFile ); |
|
115 // |
|
116 int progress = (int) ( ( (float) aFileIndex / (float) aFileCount ) * 100.0f ); |
|
117 iWorker.ReportProgress( progress, aFile ); |
|
118 } |
|
119 #endregion |
|
120 |
|
121 #region Data members |
|
122 private BackgroundWorker iWorker = new BackgroundWorker(); |
|
123 #endregion |
|
124 } |
|
125 } |