1 |
/* Watcher.m |
2 |
* |
3 |
* Copyright (C) 2003 Free Software Foundation, Inc. |
4 |
* |
5 |
* Author: Enrico Sersale <enrico@imago.ro> |
6 |
* Date: August 2001 |
7 |
* |
8 |
* This file is part of the GNUstep GWorkspace application |
9 |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
12 |
* the Free Software Foundation; either version 2 of the License, or |
13 |
* (at your option) any later version. |
14 |
* |
15 |
* This program is distributed in the hope that it will be useful, |
16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 |
* GNU General Public License for more details. |
19 |
* |
20 |
* You should have received a copy of the GNU General Public License |
21 |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
23 |
*/ |
24 |
|
25 |
|
26 |
#include <Foundation/Foundation.h> |
27 |
#include <AppKit/AppKit.h> |
28 |
#ifdef GNUSTEP |
29 |
#include "GWLib.h" |
30 |
#include "GWNotifications.h" |
31 |
#else |
32 |
#include <GWorkspace/GWLib.h> |
33 |
#include <GWorkspace/GWNotifications.h> |
34 |
#endif |
35 |
#include "FSWatcher.h" |
36 |
#include "GNUstep.h" |
37 |
|
38 |
@implementation FSWatcher |
39 |
|
40 |
- (void)dealloc |
41 |
{ |
42 |
RELEASE (watchedPath); |
43 |
TEST_RELEASE (pathContents); |
44 |
RELEASE (date); |
45 |
[super dealloc]; |
46 |
} |
47 |
|
48 |
- (id)initForWatchAtPath:(NSString *)path |
49 |
{ |
50 |
self = [super init]; |
51 |
|
52 |
if (self) { |
53 |
NSDictionary *attributes = [fm fileAttributesAtPath: path traverseLink: YES]; |
54 |
|
55 |
fm = [NSFileManager defaultManager]; |
56 |
ASSIGN (watchedPath, path); |
57 |
ASSIGN (pathContents, ([fm directoryContentsAtPath: watchedPath])); |
58 |
ASSIGN (date, [attributes fileModificationDate]); |
59 |
listeners = 1; |
60 |
isOld = NO; |
61 |
} |
62 |
|
63 |
return self; |
64 |
} |
65 |
|
66 |
- (void)watchFile |
67 |
{ |
68 |
NSDictionary *attributes; |
69 |
NSDate *moddate; |
70 |
NSMutableDictionary *notifdict; |
71 |
|
72 |
#define FW_NOTIFY(o) { \ |
73 |
[[NSNotificationCenter defaultCenter] \ |
74 |
postNotificationName: GWFileWatcherFileDidChangeNotification object: o]; \ |
75 |
} |
76 |
|
77 |
if (isOld) { |
78 |
return; |
79 |
} |
80 |
|
81 |
attributes = [fm fileAttributesAtPath: watchedPath traverseLink: YES]; |
82 |
|
83 |
if (attributes == nil) { |
84 |
notifdict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
85 |
[notifdict setObject: GWWatchedDirectoryDeleted forKey: @"event"]; |
86 |
[notifdict setObject: watchedPath forKey: @"path"]; |
87 |
FW_NOTIFY (notifdict); |
88 |
isOld = YES; |
89 |
return; |
90 |
} |
91 |
|
92 |
moddate = [attributes fileModificationDate]; |
93 |
|
94 |
if ([date isEqualToDate: moddate] == NO) { |
95 |
NSArray *newconts = [fm directoryContentsAtPath: watchedPath]; |
96 |
NSMutableArray *diffFiles = [NSMutableArray arrayWithCapacity: 1]; |
97 |
int i; |
98 |
|
99 |
notifdict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
100 |
[notifdict setObject: watchedPath forKey: @"path"]; |
101 |
|
102 |
/* if there is an error in fileAttributesAtPath */ |
103 |
/* or watchedPath doesn't exist anymore */ |
104 |
if (newconts == nil) { |
105 |
[notifdict setObject: GWWatchedDirectoryDeleted forKey: @"event"]; |
106 |
FW_NOTIFY (notifdict); |
107 |
isOld = YES; |
108 |
return; |
109 |
} |
110 |
|
111 |
for (i = 0; i < [pathContents count]; i++) { |
112 |
NSString *fname = [pathContents objectAtIndex: i]; |
113 |
if ((newconts) && ([newconts containsObject: fname] == NO)) { |
114 |
[diffFiles addObject: fname]; |
115 |
} |
116 |
} |
117 |
|
118 |
if ([diffFiles count] > 0) { |
119 |
BOOL locked = NO; |
120 |
|
121 |
for (i = 0; i < [diffFiles count]; i++) { |
122 |
NSString *fname = [diffFiles objectAtIndex: i]; |
123 |
NSString *fpath = [watchedPath stringByAppendingPathComponent: fname]; |
124 |
|
125 |
if ([GWLib isLockedPath: fpath]) { |
126 |
locked = YES; |
127 |
break; |
128 |
} |
129 |
} |
130 |
|
131 |
if (locked == NO) { |
132 |
[notifdict setObject: GWFileDeletedInWatchedDirectory forKey: @"event"]; |
133 |
[notifdict setObject: diffFiles forKey: @"files"]; |
134 |
FW_NOTIFY (notifdict); |
135 |
} |
136 |
} |
137 |
|
138 |
[diffFiles removeAllObjects]; |
139 |
|
140 |
if (newconts) { |
141 |
for (i = 0; i < [newconts count]; i++) { |
142 |
NSString *fname = [newconts objectAtIndex: i]; |
143 |
if ([pathContents containsObject: fname] == NO) { |
144 |
[diffFiles addObject: fname]; |
145 |
} |
146 |
} |
147 |
} |
148 |
|
149 |
if ([diffFiles count] > 0) { |
150 |
BOOL locked = NO; |
151 |
|
152 |
for (i = 0; i < [diffFiles count]; i++) { |
153 |
NSString *fname = [diffFiles objectAtIndex: i]; |
154 |
NSString *fpath = [watchedPath stringByAppendingPathComponent: fname]; |
155 |
|
156 |
if ([GWLib isLockedPath: fpath]) { |
157 |
locked = YES; |
158 |
break; |
159 |
} |
160 |
} |
161 |
|
162 |
if (locked == NO) { |
163 |
[notifdict setObject: watchedPath forKey: @"path"]; |
164 |
[notifdict setObject: GWFileCreatedInWatchedDirectory forKey: @"event"]; |
165 |
[notifdict setObject: diffFiles forKey: @"files"]; |
166 |
FW_NOTIFY (notifdict); |
167 |
} |
168 |
} |
169 |
|
170 |
ASSIGN (pathContents, newconts); |
171 |
ASSIGN (date, moddate); |
172 |
} |
173 |
} |
174 |
|
175 |
- (void)addListener |
176 |
{ |
177 |
listeners++; |
178 |
} |
179 |
|
180 |
- (void)removeListener |
181 |
{ |
182 |
listeners--; |
183 |
if (listeners == 0) { |
184 |
isOld = YES; |
185 |
} |
186 |
} |
187 |
|
188 |
- (BOOL)isWathcingPath:(NSString *)apath |
189 |
{ |
190 |
return ([apath isEqualToString: watchedPath]); |
191 |
} |
192 |
|
193 |
- (NSString *)watchedPath |
194 |
{ |
195 |
return watchedPath; |
196 |
} |
197 |
|
198 |
- (BOOL)isOld |
199 |
{ |
200 |
return isOld; |
201 |
} |
202 |
|
203 |
- (void)setIsOld |
204 |
{ |
205 |
isOld = YES; |
206 |
} |
207 |
|
208 |
@end |
209 |
|