1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
/*
IMLoading.m
Copyright (C) 1996 Free Software Foundation, Inc.
Author: Ovidiu Predescu <ovidiu@net-community.com>
Date: November 1997
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#ifndef GNUSTEP
#include <Foundation/Foundation.h>
#else
#include <Foundation/NSDictionary.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSFileManager.h>
#include <Foundation/NSPathUtilities.h>
#endif
#include "GNUstepGUI/GMArchiver.h"
#include "GNUstepGUI/IMLoading.h"
#include "GNUstepGUI/IMCustomObject.h"
/* For awakeFromNib */
#include <AppKit/NSNibLoading.h>
void __dummy_IMLoading_functionForLinking()
{
__dummy_IMLoading_functionForLinking();
}
@implementation GMModel
id _nibOwner = nil;
BOOL _fileOwnerDecoded = NO;
+ (void)initialize
{
/* Force linking of AppKit GModel categories */
extern void __dummy_GMAppKit_functionForLinking();
__dummy_GMAppKit_functionForLinking ();
}
+ (BOOL)loadIMFile:(NSString*)path owner:(id)owner
{
return [self loadIMFile:path owner:owner bundle:[NSBundle mainBundle]];
}
+ (BOOL)loadIMFile:(NSString*)path owner:(id)owner bundle:(NSBundle*)mainBundle
{
#ifdef GNU_GUI_LIBRARY
NSString* resourcePath = [mainBundle resourcePath];
GMUnarchiver* unarchiver;
id previousNibOwner = _nibOwner;
GMModel* decoded;
if (![[path pathExtension] isEqualToString:@"gmodel"])
{
path = [path stringByAppendingPathExtension:@"gmodel"];
}
/* First check to see if path is an absolute path; if so try to load the
pointed file. */
if ([path isAbsolutePath]) {
if (![[NSFileManager defaultManager] fileExistsAtPath:path])
{
/* The file is an absolute path name but the model file
doesn't exist. */
return NO;
}
}
else
{
/* The path is a relative path; search it in the current bundle. */
NSString *abspath = [resourcePath stringByAppendingPathComponent:path];
if (![[NSFileManager defaultManager] fileExistsAtPath:abspath])
{
NSArray *paths;
NSString *root;
paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
NSSystemDomainMask, YES);
if ((paths != nil) && ([paths count] > 0))
{
root = [paths objectAtIndex: 0];
root = [root stringByAppendingPathComponent:@"Model"];
abspath = [root stringByAppendingPathComponent:path];
if (![[NSFileManager defaultManager] fileExistsAtPath:abspath])
{
return NO;
}
}
else
{
return NO;
}
}
path = abspath;
}
NSLog (@"loading model file %@...", path);
unarchiver = [GMUnarchiver unarchiverWithContentsOfFile:path];
if (!unarchiver)
{
return NO;
}
/* Set the _nibOwner to `owner' so that the first decoded custom object
replaces itself with `owner'. Also set _fileOwnerDecoded so that the
first custom object knows it's the first. */
_nibOwner = owner;
_fileOwnerDecoded = NO;
decoded = [unarchiver decodeObjectWithName:@"RootObject"];
[decoded _makeConnections];
/* Restore the previous nib owner. We do this because loadIMFile:owner: can
be invoked recursively. */
_nibOwner = previousNibOwner;
#endif
return YES;
}
- (void)_makeConnections
{
int i, count;
#ifdef __APPLE__
[connections makeObjectsPerformSelector:@selector(establishConnection)];
#else
[connections makeObjectsPerform:@selector(establishConnection)];
#endif
/* Send the -awakeFromModel method */
for (i = 0, count = [objects count]; i < count; i++) {
id object = [[objects objectAtIndex:i] nibInstantiate];
// If the object responds to the awakeFromModel
// method, send that message. If it doesn't then
// check for the awakeFromNib selector and send that.
// This ensures compatibility w/ apps ported from OPENSTEP.
if ([object respondsToSelector:@selector(awakeFromModel)])
{
[object awakeFromModel];
}
else
if ([object respondsToSelector:@selector(awakeFromNib)])
{
[object awakeFromNib];
}
}
}
- (void)dealloc
{
[objects release];
[connections release];
[super dealloc];
}
- (void)_setObjects:_objects connections:_connections
{
objects = [_objects retain];
connections = [_connections retain];
}
- (void)encodeWithModelArchiver:(GMArchiver*)archiver
{
[archiver encodeObject:objects withName:@"Objects"];
[archiver encodeObject:connections withName:@"Connections"];
}
- (id)initWithModelUnarchiver:(GMUnarchiver*)unarchiver
{
objects = [[unarchiver decodeObjectWithName:@"Objects"] retain];
connections = [[unarchiver decodeObjectWithName:@"Connections"] retain];
return self;
}
- (NSArray *) objects
{
return objects;
}
- (NSArray *) connections
{
return connections;
}
@end /* GMModel */
#if GNU_RUNTIME
#include "IMConnectors.h"
__attribute__((unused))
static void __dummyFunctionForLinking (void)
{
[IMCustomObject new];
[IMConnector new];
__dummyFunctionForLinking();
}
#endif
|