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 "GWProtocol.h" |
30 |
#include "GWNotifications.h" |
31 |
#else |
32 |
#include <GWorkspace/GWProtocol.h> |
33 |
#include <GWorkspace/GWNotifications.h> |
34 |
#endif |
35 |
#include "Watcher.h" |
36 |
#include "GNUstep.h" |
37 |
|
38 |
@implementation Watcher |
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 |
#ifdef GNUSTEP |
54 |
Class gwclass = [[NSBundle mainBundle] principalClass]; |
55 |
#else |
56 |
Class gwclass = [[NSBundle mainBundle] classNamed: @"GWorkspace"]; |
57 |
#endif |
58 |
NSDictionary *attributes; |
59 |
|
60 |
gworkspace = (id<GWProtocol>)[gwclass gworkspace]; |
61 |
|
62 |
fm = [NSFileManager defaultManager]; |
63 |
ASSIGN (watchedPath, path); |
64 |
ASSIGN (pathContents, ([fm directoryContentsAtPath: watchedPath])); |
65 |
attributes = [fm fileAttributesAtPath: path traverseLink: YES]; |
66 |
ASSIGN (date, [attributes fileModificationDate]); |
67 |
listeners = 1; |
68 |
isOld = NO; |
69 |
} |
70 |
|
71 |
return self; |
72 |
} |
73 |
|
74 |
- (void)watchFile |
75 |
{ |
76 |
NSDictionary *attributes; |
77 |
NSDate *moddate; |
78 |
NSMutableDictionary *notifdict; |
79 |
|
80 |
#define FW_NOTIFY(o) { \ |
81 |
[[NSNotificationCenter defaultCenter] \ |
82 |
postNotificationName: GWFileWatcherFileDidChangeNotification object: o]; \ |
83 |
} |
84 |
|
85 |
if (isOld) { |
86 |
return; |
87 |
} |
88 |
|
89 |
attributes = [fm fileAttributesAtPath: watchedPath traverseLink: YES]; |
90 |
|
91 |
if (attributes == nil) { |
92 |
notifdict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
93 |
[notifdict setObject: GWWatchedDirectoryDeleted forKey: @"event"]; |
94 |
[notifdict setObject: watchedPath forKey: @"path"]; |
95 |
FW_NOTIFY (notifdict); |
96 |
isOld = YES; |
97 |
return; |
98 |
} |
99 |
|
100 |
moddate = [attributes fileModificationDate]; |
101 |
|
102 |
if ([date isEqualToDate: moddate] == NO) { |
103 |
NSArray *newconts = [fm directoryContentsAtPath: watchedPath]; |
104 |
NSMutableArray *diffFiles = [NSMutableArray arrayWithCapacity: 1]; |
105 |
int i; |
106 |
|
107 |
notifdict = [NSMutableDictionary dictionaryWithCapacity: 1]; |
108 |
[notifdict setObject: watchedPath forKey: @"path"]; |
109 |
|
110 |
/* if there is an error in fileAttributesAtPath */ |
111 |
/* or watchedPath doesn't exist anymore */ |
112 |
if (newconts == nil) { |
113 |
[notifdict setObject: GWWatchedDirectoryDeleted forKey: @"event"]; |
114 |
FW_NOTIFY (notifdict); |
115 |
isOld = YES; |
116 |
return; |
117 |
} |
118 |
|
119 |
for (i = 0; i < [pathContents count]; i++) { |
120 |
NSString *fname = [pathContents objectAtIndex: i]; |
121 |
if ((newconts) && ([newconts containsObject: fname] == NO)) { |
122 |
[diffFiles addObject: fname]; |
123 |
} |
124 |
} |
125 |
|
126 |
if ([diffFiles count] > 0) { |
127 |
BOOL locked = NO; |
128 |
|
129 |
for (i = 0; i < [diffFiles count]; i++) { |
130 |
NSString *fname = [diffFiles objectAtIndex: i]; |
131 |
NSString *fpath = [watchedPath stringByAppendingPathComponent: fname]; |
132 |
|
133 |
if ([gworkspace isLockedPath: fpath]) { |
134 |
locked = YES; |
135 |
break; |
136 |
} |
137 |
} |
138 |
|
139 |
if (locked == NO) { |
140 |
[notifdict setObject: GWFileDeletedInWatchedDirectory forKey: @"event"]; |
141 |
[notifdict setObject: diffFiles forKey: @"files"]; |
142 |
FW_NOTIFY (notifdict); |
143 |
} |
144 |
} |
145 |
|
146 |
[diffFiles removeAllObjects]; |
147 |
|
148 |
if (newconts) { |
149 |
for (i = 0; i < [newconts count]; i++) { |
150 |
NSString *fname = [newconts objectAtIndex: i]; |
151 |
if ([pathContents containsObject: fname] == NO) { |
152 |
[diffFiles addObject: fname]; |
153 |
} |
154 |
} |
155 |
} |
156 |
|
157 |
if ([diffFiles count] > 0) { |
158 |
BOOL locked = NO; |
159 |
|
160 |
for (i = 0; i < [diffFiles count]; i++) { |
161 |
NSString *fname = [diffFiles objectAtIndex: i]; |
162 |
NSString *fpath = [watchedPath stringByAppendingPathComponent: fname]; |
163 |
|
164 |
if ([gworkspace isLockedPath: fpath]) { |
165 |
locked = YES; |
166 |
break; |
167 |
} |
168 |
} |
169 |
|
170 |
if (locked == NO) { |
171 |
[notifdict setObject: watchedPath forKey: @"path"]; |
172 |
[notifdict setObject: GWFileCreatedInWatchedDirectory forKey: @"event"]; |
173 |
[notifdict setObject: diffFiles forKey: @"files"]; |
174 |
FW_NOTIFY (notifdict); |
175 |
} |
176 |
} |
177 |
|
178 |
ASSIGN (pathContents, newconts); |
179 |
ASSIGN (date, moddate); |
180 |
} |
181 |
} |
182 |
|
183 |
- (void)addListener |
184 |
{ |
185 |
listeners++; |
186 |
} |
187 |
|
188 |
- (void)removeListener |
189 |
{ |
190 |
listeners--; |
191 |
if (listeners == 0) { |
192 |
isOld = YES; |
193 |
} |
194 |
} |
195 |
|
196 |
- (BOOL)isWathcingPath:(NSString *)apath |
197 |
{ |
198 |
return ([apath isEqualToString: watchedPath]); |
199 |
} |
200 |
|
201 |
- (NSString *)watchedPath |
202 |
{ |
203 |
return watchedPath; |
204 |
} |
205 |
|
206 |
- (BOOL)isOld |
207 |
{ |
208 |
return isOld; |
209 |
} |
210 |
|
211 |
- (void)setIsOld |
212 |
{ |
213 |
isOld = YES; |
214 |
} |
215 |
|
216 |
@end |
217 |
|