|
1 /* |
|
2 * Copyright (c) 2008-2008 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: Monitors the file system for changes in a file. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include <e32base.h> |
|
20 #include <bautils.h> |
|
21 #include <mpxlog.h> |
|
22 |
|
23 #include "filemonitor.h" |
|
24 #include "filemonitorobserver.h" |
|
25 |
|
26 |
|
27 |
|
28 // --------------------------------------------------------------------------- |
|
29 // Constructor |
|
30 // --------------------------------------------------------------------------- |
|
31 // |
|
32 CFileMonitor::CFileMonitor( MFileMonitorObserver& aObserver) |
|
33 : CActive( EPriorityNormal ), |
|
34 iObserver( aObserver ), |
|
35 iFilePath( NULL ) |
|
36 { |
|
37 } |
|
38 |
|
39 // --------------------------------------------------------------------------- |
|
40 // Second Phase Constructor |
|
41 // --------------------------------------------------------------------------- |
|
42 // |
|
43 void CFileMonitor::ConstructL() |
|
44 { |
|
45 CActiveScheduler::Add( this ); |
|
46 User::LeaveIfError( iFs.Connect() ); |
|
47 } |
|
48 |
|
49 // --------------------------------------------------------------------------- |
|
50 // Two-phased constructor |
|
51 // --------------------------------------------------------------------------- |
|
52 // |
|
53 CFileMonitor* CFileMonitor::NewL( MFileMonitorObserver& aObserver) |
|
54 { |
|
55 CFileMonitor* self = new( ELeave ) CFileMonitor( aObserver); |
|
56 CleanupStack::PushL( self ); |
|
57 self->ConstructL(); |
|
58 CleanupStack::Pop( self ); |
|
59 return self; |
|
60 } |
|
61 |
|
62 |
|
63 // --------------------------------------------------------------------------- |
|
64 // Destructor |
|
65 // --------------------------------------------------------------------------- |
|
66 // |
|
67 CFileMonitor::~CFileMonitor() |
|
68 { |
|
69 Cancel(); |
|
70 iFs.Close(); |
|
71 delete iFilePath; |
|
72 } |
|
73 |
|
74 // --------------------------------------------------------------------------- |
|
75 // Starts monitoring a particular file |
|
76 // --------------------------------------------------------------------------- |
|
77 // |
|
78 void CFileMonitor::StartL( const TDesC& aFile) |
|
79 { |
|
80 MPX_DEBUG1("CFileMonitor::Start <---"); |
|
81 |
|
82 delete iFilePath; |
|
83 iFilePath = NULL; |
|
84 iFilePath = aFile.AllocL(); |
|
85 |
|
86 TNotifyType notType = ENotifyAll; |
|
87 iFs.NotifyChange( notType, iStatus, *iFilePath ); |
|
88 SetActive(); |
|
89 MPX_DEBUG1("CFileMonitor::Start --->"); |
|
90 } |
|
91 |
|
92 // --------------------------------------------------------------------------- |
|
93 // RunL callback |
|
94 // --------------------------------------------------------------------------- |
|
95 // |
|
96 void CFileMonitor::RunL() |
|
97 { |
|
98 MPX_DEBUG1("CFileMonitor::RunL <---"); |
|
99 if (!BaflUtils::FileExists(iFs, *iFilePath )) |
|
100 { |
|
101 iObserver.HandleFileRemovedL(); |
|
102 MPX_DEBUG1("CFileMonitor::RunL - File was removed"); |
|
103 } |
|
104 else |
|
105 { |
|
106 // Listen again |
|
107 TNotifyType notType(ENotifyAll); |
|
108 iFs.NotifyChange(notType, iStatus, *iFilePath); |
|
109 SetActive(); |
|
110 MPX_DEBUG1("CFileMonitor::RunL - File exists"); |
|
111 } |
|
112 MPX_DEBUG1("CFileMonitor::RunL --->"); |
|
113 } |
|
114 |
|
115 // --------------------------------------------------------------------------- |
|
116 // Handle Cancelling |
|
117 // --------------------------------------------------------------------------- |
|
118 // |
|
119 void CFileMonitor::DoCancel() |
|
120 { |
|
121 MPX_DEBUG1("CFileMonitor::DoCancel <---"); |
|
122 |
|
123 // Stop monitoring |
|
124 iFs.NotifyChangeCancel(); |
|
125 MPX_DEBUG1("CFileMonitor::DoCancel --->"); |
|
126 |
|
127 } |
|
128 |
|
129 // ---------------------------------------------------------------------------- |
|
130 // Handles a leave occurring in the request completion event handler RunL() |
|
131 // ---------------------------------------------------------------------------- |
|
132 // |
|
133 TInt CFileMonitor::RunError(TInt aError) |
|
134 { |
|
135 MPX_DEBUG2("CFileMonitor::RunError(%d)", aError ); |
|
136 |
|
137 // Listen again |
|
138 TNotifyType notType(ENotifyAll); |
|
139 iFs.NotifyChange( notType, iStatus, *iFilePath ); |
|
140 SetActive(); |
|
141 return KErrNone; |
|
142 } |