File: MainDocument.m

package info (click to toggle)
fortunate.app 3.1-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 508 kB
  • sloc: ansic: 2,697; objc: 470; makefile: 23
file content (186 lines) | stat: -rw-r--r-- 5,219 bytes parent folder | download
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
/* MainDocument.m
 * Chris Saldanha, June 1999 (modified April 2001 for MacOS X 10.0)
 * Class to display fortunes in a simple window.
 * Can copy the fortune into the pasteboard from a menu option.
 * Clicking in the window's content view makes it go away.
 * For MacOS X - now using swanky classes NSTask/NSFileHandle/NSPipe instead of UNIX calls
 */

#import <Foundation/NSException.h>
#import "MainDocument.h"
#import "PreferencesController.h"

@implementation MainDocument

- (id)init
{
   if ((self = [super init]))
   {
      text = nil;
   }

   return self;
}

- (void)activateMainWindow:(id)sender
{
    [self loadNewFortuneAndUpdateWindow:nil];
}

- (void) loadNewFortuneAndUpdateWindow:(id)sender
{
    NSPipe *pipe;
    NSFileHandle *fortuneHandle, *errorHandle;
    NSTask *fortuneTask;
    NSData *fortuneData;
    NSMutableArray *arguments;
    int ret;

    [text autorelease];
    text = [[FortuneTextView alloc] initWithFrame: NSMakeRect(0, 0, 10, 10)];
    [text setFont: [self fortuneFont]];
    [text setEditable: NO];
    [text setRichText: NO];
    [text setDrawsBackground: YES];
    [text setBackgroundColor: [NSColor lightGrayColor]];
    [text setMaxSize:(NSSize){1e7, 1e7}];
    [text setHorizontallyResizable: YES];
    [text setVerticallyResizable: YES];
    [[text textContainer] setContainerSize: (NSSize){1e7, 1e7}];
    [[text textContainer] setWidthTracksTextView:NO];
    [text setTextContainerInset: (NSSize){5, 1}];

    fortuneTask = [[NSTask alloc] init];
    [fortuneTask setLaunchPath: [self fortuneLocation]];

    arguments = [NSMutableArray array];
    if ([self showOffensive])
        [arguments addObject: @"-a"];

    if ([self datfilesLocation])
        [arguments addObject: [self datfilesLocation]];

    [fortuneTask setArguments: arguments];
    
    pipe = [NSPipe pipe];
    fortuneHandle = [pipe fileHandleForReading];
    [fortuneTask setStandardOutput: pipe];
    pipe = [NSPipe pipe];
    errorHandle = [pipe fileHandleForReading];
    [fortuneTask setStandardError: pipe];
    
    ret = 0;
    NS_DURING
        [fortuneTask launch];
    NS_HANDLER
        [text setString: [NSString stringWithFormat: @"Got no fortune from %@.\nUse the preferences panel to chose the location of the fortune program.", [self fortuneLocation]]];
        ret = 1;
    NS_ENDHANDLER
    
    if (ret != 1)
    {
       [fortuneTask waitUntilExit];
       ret = [fortuneTask terminationStatus];
       if (ret != 0)	//failed to get a fortune...
       {
          fortuneData = [errorHandle readDataToEndOfFile];
          [text setString: [NSString stringWithFormat: @"Got an error from fortune:\n%@",
                                     [NSString stringWithCString: [fortuneData bytes] length: [fortuneData length]]]];
       }
       else		//we succeeded in getting a fortune
       {
          fortuneData = [fortuneHandle readDataToEndOfFile];
          [text setString: [NSString stringWithCString: [fortuneData bytes]
                                     length: [fortuneData length]]];
       }
    }
    [fortuneTask release];

    [text sizeToFit];
    [mainWindow setContentSize: [text frame].size];
    [mainWindow setContentView: text];
    [mainWindow center];	//Americans don't know how to spel centre
    [mainWindow makeKeyAndOrderFront: self];
}

- (void)copyFortune:(id)sender
{
    [(NSTextView *)[mainWindow contentView] selectAll: nil];
    [(NSTextView *)[mainWindow contentView] copy: nil];
    [(NSTextView *)[mainWindow contentView] setSelectedRange: NSMakeRange(0, 0)];
}

- (void)showPrefs:(id)sender
{
    if (!prefsPanel)
        if(![NSBundle loadNibNamed: @"Preferences" owner: self])
            return;
    [prefsController showPrefs: nil];
}

- (void)showInfo:(id)sender
{
    if (!infoPanel)
        if(![NSBundle loadNibNamed: @"Info" owner: self])
            return;
    [infoPanel makeKeyAndOrderFront: nil];
}

- (void)printFortune:(id)sender
{
    [text print: nil];
}

- (NSString *)fortuneLocation
{
   BOOL useBundled;

   useBundled = [[NSUserDefaults standardUserDefaults] boolForKey: kUseBundledFortune];
   if (useBundled)
   {
      return [[NSBundle mainBundle] pathForResource: @"fortune" ofType: @""];
   }
   return [[NSUserDefaults standardUserDefaults] stringForKey: kFortuneLocation];
}

- (NSString *)datfilesLocation
{
   BOOL useBundled;

   useBundled = [[NSUserDefaults standardUserDefaults] boolForKey: kUseBundledDatfiles];
   if (useBundled)
   {
      return [[NSBundle mainBundle] pathForResource: @"datfiles" ofType: @""];
   }
   return [[NSUserDefaults standardUserDefaults] stringForKey: kDatfilesLocation];
}

- (NSFont *)fortuneFont
{
   NSString* fontName;
   float     fontSize;

   fontSize = [[NSUserDefaults standardUserDefaults] floatForKey: kFortuneFontSize];
   fontName = [[NSUserDefaults standardUserDefaults] stringForKey: kFortuneFontName];
   
   if ((fontName == nil) || (fontSize == 0.0))
   {
      return [NSFont userFixedPitchFontOfSize: 12.0];
   }
   else
   {
      return [NSFont fontWithName: fontName size: fontSize];
   }
}

- (BOOL)showOffensive
{
   return [[NSUserDefaults standardUserDefaults] boolForKey: kShowOffensive];
}

- (void)dealloc
{
    [super dealloc];
}

@end