]> err.no Git - sope/blob - sope-core/NGExtensions/FdExt.subproj/NSRunLoop+FileObjects.m
fix in URL processing
[sope] / sope-core / NGExtensions / FdExt.subproj / NSRunLoop+FileObjects.m
1 /*
2   Copyright (C) 2000-2003 SKYRIX Software AG
3
4   This file is part of OGo
5
6   OGo 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   OGo 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 OGo; 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 // $Id$
22 // Created by Helge Hess on Mon Mar 11 2002.
23
24 #if !LIB_FOUNDATION_LIBRARY
25
26 #include <stdlib.h>
27 #include "NSRunLoop+FileObjects.h"
28 #include "FileObjectHolder.h"
29 #import <Foundation/NSMapTable.h>
30 #import <Foundation/NSNotification.h>
31 #include "common.h"
32
33 NSString *NSFileObjectBecameActiveNotificationName =
34   @"NSFileObjectBecameActiveNotification";
35
36 #if GNUSTEP_BASE_LIBRARY
37
38 @interface NSObject(FileObjectWatcher) < RunLoopEvents >
39 @end
40
41 @implementation NSObject(FileObjectWatcher)
42
43 - (NSDate *)timedOutEvent:(void *)_fdData
44   type: (RunLoopEventType)_type
45   forMode: (NSString *)_mode
46 {
47   NSLog(@"%s: timed out ...", __PRETTY_FUNCTION__);
48   return nil;
49 }
50
51 - (void)receivedEvent:(void *)_fdData
52   type:(RunLoopEventType)_type
53   extra:(void *)_extra
54   forMode:(NSString *)_mode
55 {
56   NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
57   
58   [nc postNotificationName:NSFileObjectBecameActiveNotificationName
59       object:self];
60 }
61
62 @end /* NSObject(FileObjectWatcher) */
63
64 #endif
65
66 @implementation NSRunLoop(FileObjects)
67
68 #if GNUSTEP_BASE_LIBRARY
69
70 /* implement using -addEvent:type:watcher:forMode: */
71
72 - (void)addFileObject:(id)_fileObject
73   activities:(unsigned int)_activities
74   forMode:(NSString *)_mode
75 {
76   int evType = 0;
77   
78   _fileObject = RETAIN(_fileObject);
79   
80   [self addEvent:(void *)[_fileObject fileDescriptor]
81         type:evType
82         watcher:_fileObject
83         forMode:_mode];
84 }
85 - (void)removeFileObject:(id)_fileObject
86   forMode:(NSString *)_mode
87 {
88   int evType = 0;
89   
90   _fileObject = AUTORELEASE(_fileObject);
91   [self removeEvent:(void *)[_fileObject fileDescriptor]
92         type:evType
93         forMode:_mode
94         all:NO];
95 }
96
97 #else /* eg MacOSX Foundation ... */
98
99 static NSMutableArray *activeHandles = nil;
100
101 - (void)addFileObject:(id)_fileObject
102   activities:(unsigned int)_activities
103   forMode:(NSString *)_mode
104 {
105   FileObjectHolder *fo;
106   
107   if (activeHandles == nil)
108     activeHandles = [[NSMutableArray alloc] init];
109   
110   fo = [[FileObjectHolder alloc] initWithFileObject:_fileObject
111                                  activities:_activities
112                                  mode:_mode];
113   [activeHandles addObject:fo];
114   [fo wait];
115   [fo release];
116 }
117
118 - (FileObjectHolder *)_findHolderForObject:(id)_fileObject {
119   NSEnumerator *e;
120   FileObjectHolder *fo;
121   
122   if (activeHandles == nil) return NULL;
123   e = [activeHandles objectEnumerator];
124   while ((fo = [e nextObject])) {
125     if ([fo fileObject] == _fileObject)
126       break;
127   }
128   return fo;
129 }
130   
131 - (void)removeFileObject:(id)_fileObject
132   forMode:(NSString *)_mode
133 {
134   FileObjectHolder *fo;
135
136   if ((fo = [self _findHolderForObject:_fileObject]) == nil) {
137     NSLog(@"found no holder for fileobject %@ ...", _fileObject);
138     return;
139   }
140   
141   [fo retain];
142   [activeHandles removeObject:fo];
143   [fo stopWaiting];
144   [fo release];
145 }
146
147 #endif /* !GNUSTEP_BASE_LIBRARY */
148
149 @end /* NSRunLoop(FileObjects) */
150
151 #endif /* !LIB_FOUNDATION_LIBRARY */