File: Controller.m

package info (click to toggle)
gnustep-examples 1%3A1.2.0-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,952 kB
  • ctags: 270
  • sloc: objc: 14,381; makefile: 65
file content (174 lines) | stat: -rw-r--r-- 4,516 bytes parent folder | download | duplicates (4)
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
#include "Controller.h"

int nameSort(id path1, id path2, void *context);

@implementation Controller


-(void) dealloc
{
  RELEASE(tests);
  RELEASE(loadedTests);
  [super dealloc];
}

-(id) init
{ 
  [super init];
  
  loadedTests = [[NSMutableArray alloc] init];
  tests = [[NSMutableArray alloc] init];
  [self _findBundles];

  return self;
}

- (void) awakeFromNib
{
  id <NSMenuItem> menuItem;
  NSMenu *testMenu = [[[NSApp mainMenu] itemWithTag: 42] submenu];
  NSString *name;
  int i;
  
  for (i = 0; i < [tests count]; i++)
    {
      name = [[[tests objectAtIndex: i] lastPathComponent]
       stringByDeletingPathExtension]; 
      menuItem = [testMenu addItemWithTitle: name
        action: @selector (startListedTest:)
        keyEquivalent: @""];
      [menuItem setTag: i];
   }  
}

-(id) loadAndStartTestWithBundlePath: (NSString *)fullPath
{
  NSBundle *bundle;
  Class principalClass;

  // Load the bundle
  bundle = [NSBundle bundleWithPath: fullPath];

  if (bundle) // Bundle was succesfully loaded
    {
      // Load the principal class of the bundle
      principalClass = [bundle principalClass];
      if (principalClass) // succesfully loaded
        {
          return AUTORELEASE ([principalClass new]);
        }
      else // !principalClass
        {
          NSRunAlertPanel (NULL, @"Could not load principal class",
                           @"OK", NULL, NULL);
          return nil;
        }
    }
  else // !bundle
    {
      NSRunAlertPanel (NULL, @"Could not load Bundle",
                       @"OK", NULL, NULL);
      return nil;
    }
}

-(void) startListedTest: (id) sender
{
  NSString *bundlePath;
  int i;

  i = [sender tag];
  if ( [[loadedTests objectAtIndex: i] conformsToProtocol: @protocol(GSTest)])
    {
      [(<GSTest>)[loadedTests objectAtIndex: i] restart];
      return;
    }
  else // not started yet
    { 
      bundlePath = [tests objectAtIndex: i];
      [loadedTests replaceObjectAtIndex: i 
        withObject: [self loadAndStartTestWithBundlePath: bundlePath]];
    }
}

-(void) startUnlistedTest: (id) sender
{
  NSOpenPanel *openPanel;
  int result;

  openPanel = [NSOpenPanel openPanel];
  [openPanel setTitle: @"Select Bundle"];
  [openPanel setPrompt: @"Bundle:"];
  [openPanel setTreatsFilePackagesAsDirectories: NO];
  result = [openPanel runModalForDirectory: nil
                      file: nil
                      types: [NSArray arrayWithObject: @"bundle"]];
  if (result == NSOKButton)
    {
      NSEnumerator *e = [[openPanel filenames] objectEnumerator];
      NSString *file;

      while ((file = (NSString *)[e nextObject]))
        {
          id test = [self loadAndStartTestWithBundlePath: file];
          [loadedTests addObject: test];
        }
    }
}

-(void) _findBundles
{
  NSMutableArray *bundlePaths;
  NSArray *stdPaths;
  NSEnumerator *enm;
  NSString *path;
  int i = 0;
  int k = 0;

  // The test bundles could be in a few places
  bundlePaths = [NSMutableArray arrayWithCapacity: 1];
  [bundlePaths addObject: [[[NSBundle mainBundle] bundlePath]
    stringByDeletingLastPathComponent]];
  stdPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
    NSUserDomainMask | NSLocalDomainMask | NSSystemDomainMask, YES);
  enm = [stdPaths objectEnumerator];
  while( (path = [enm nextObject]) )
    {
      [bundlePaths addObject: 
        [[path stringByAppendingPathComponent: @"ApplicationSupport"]
        stringByAppendingPathComponent: @"GSTest"]];
    }

  // stow away the bundle paths  
  for( i=0; i < [bundlePaths count]; i++ )
    {
      if( [[NSFileManager defaultManager] fileExistsAtPath: 
        [bundlePaths objectAtIndex: i]] )
        {  
          enm = [[NSFileManager defaultManager] enumeratorAtPath: 
            [bundlePaths objectAtIndex: i]];
          while( (path = [enm nextObject]) )
            {
              path = [[bundlePaths objectAtIndex: i] 
                stringByAppendingPathComponent: path];
              if( [[path pathExtension] isEqualToString: @"bundle"] &&
                ![tests containsObject: path] )
                {
                  [tests addObject: path];
                  [loadedTests insertObject: @"X" atIndex: k];
                  k++;  
                }
            }
        }
    }
    
  // alphabatize them so they're easy to find
  [tests sortUsingFunction: nameSort context: nil];
}

int nameSort(id path1, id path2, void *context)
{
  return [[path1 lastPathComponent] compare: [path2 lastPathComponent]];
}

@end