]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSRunLoop+FileObjects.m
Add libxml2-dev to libsope-xml4.7-dev deps
[sope] / sope-core / NGExtensions / FdExt.subproj / NSRunLoop+FileObjects.m
1 /*
2   Copyright (C) 2000-2005 SKYRIX Software AG
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21 // Created by Helge Hess on Mon Mar 11 2002.
22
23 #if !LIB_FOUNDATION_LIBRARY
24
25 #include <stdlib.h>
26 #include "NSRunLoop+FileObjects.h"
27 #include "FileObjectHolder.h"
28 #import <Foundation/NSMapTable.h>
29 #import <Foundation/NSNotification.h>
30 #include "common.h"
31
32 NSString *NSFileObjectBecameActiveNotificationName =
33   @"NSFileObjectBecameActiveNotification";
34
35 #if GNUSTEP_BASE_LIBRARY
36
37 @interface NSObject(FileObjectWatcher) < RunLoopEvents >
38 @end
39
40 @implementation NSObject(FileObjectWatcher)
41
42 - (NSDate *)timedOutEvent:(void *)_fdData
43   type: (RunLoopEventType)_type
44   forMode: (NSString *)_mode
45 {
46   NSLog(@"%s: timed out ...", __PRETTY_FUNCTION__);
47   return nil;
48 }
49
50 - (void)receivedEvent:(void *)_fdData
51   type:(RunLoopEventType)_type
52   extra:(void *)_extra
53   forMode:(NSString *)_mode
54 {
55   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
56   
57   [nc postNotificationName:NSFileObjectBecameActiveNotificationName
58       object:self];
59 }
60
61 @end /* NSObject(FileObjectWatcher) */
62
63 #endif
64
65 @implementation NSRunLoop(FileObjects)
66
67 #if GNUSTEP_BASE_LIBRARY
68
69 /* implement using -addEvent:type:watcher:forMode: */
70
71 - (void)addFileObject:(id)_fileObject
72   activities:(unsigned int)_activities
73   forMode:(NSString *)_mode
74 {
75   int evType = 0;
76   
77   _fileObject = RETAIN(_fileObject);
78   
79   [self addEvent:(void *)[_fileObject fileDescriptor]
80         type:evType
81         watcher:_fileObject
82         forMode:_mode];
83 }
84 - (void)removeFileObject:(id)_fileObject
85   forMode:(NSString *)_mode
86 {
87   int evType = 0;
88   
89   _fileObject = AUTORELEASE(_fileObject);
90   [self removeEvent:(void *)[_fileObject fileDescriptor]
91         type:evType
92         forMode:_mode
93         all:NO];
94 }
95
96 #else /* eg MacOSX Foundation ... */
97
98 static NSMutableArray *activeHandles = nil;
99
100 - (void)addFileObject:(id)_fileObject
101   activities:(unsigned int)_activities
102   forMode:(NSString *)_mode
103 {
104   FileObjectHolder *fo;
105   
106   if (activeHandles == nil)
107     activeHandles = [[NSMutableArray alloc] init];
108   
109   fo = [[FileObjectHolder alloc] initWithFileObject:_fileObject
110                                  activities:_activities
111                                  mode:_mode];
112   [activeHandles addObject:fo];
113   [fo wait];
114   [fo release];
115 }
116
117 - (FileObjectHolder *)_findHolderForObject:(id)_fileObject {
118   NSEnumerator *e;
119   FileObjectHolder *fo;
120   
121   if (activeHandles == nil) return NULL;
122   e = [activeHandles objectEnumerator];
123   while ((fo = [e nextObject])) {
124     if ([fo fileObject] == _fileObject)
125       break;
126   }
127   return fo;
128 }
129   
130 - (void)removeFileObject:(id)_fileObject
131   forMode:(NSString *)_mode
132 {
133   FileObjectHolder *fo;
134
135   if ((fo = [self _findHolderForObject:_fileObject]) == nil) {
136     NSLog(@"found no holder for fileobject %@ ...", _fileObject);
137     return;
138   }
139   
140   [fo retain];
141   [activeHandles removeObject:fo];
142   [fo stopWaiting];
143   [fo release];
144 }
145
146 #endif /* !GNUSTEP_BASE_LIBRARY */
147
148 @end /* NSRunLoop(FileObjects) */
149
150 #endif /* !LIB_FOUNDATION_LIBRARY */