1/*
2**  PlopFolio.m
3**
4**  Copyright (c) 2002
5**
6**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
7**
8**  This program is free software; you can redistribute it and/or modify
9**  it under the terms of the GNU General Public License as published by
10**  the Free Software Foundation; either version 2 of the License, or
11**  (at your option) any later version.
12**
13**  This program is distributed in the hope that it will be useful,
14**  but WITHOUT ANY WARRANTY; without even the implied warranty of
15**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16**  GNU General Public License for more details.
17**
18**  You should have received a copy of the GNU General Public License
19**  along with this program; if not, write to the Free Software
20**  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21*/
22
23#import "PlopFolio.h"
24
25#import "Channel.h"
26#import "Constants.h"
27#import "Plop.h"
28#import "PlopWindowController.h"
29#import "PreferencesPanelController.h"
30
31// XML header needed under GNUstep
32#ifndef MACOSX
33#import <GNUstepBase/GSXML.h>
34#endif
35
36@implementation PlopFolio
37
38//
39//
40//
41- (id) init
42{
43  self = [super init];
44
45  return self;
46}
47
48
49//
50//
51//
52- (void) dealloc
53{
54  RELEASE(allPlops);
55
56  [super dealloc];
57}
58
59
60//
61// action methods
62//
63- (IBAction) showPreferencesPanel: (id) sender
64{
65  PreferencesPanelController *aController;
66
67  aController = [[PreferencesPanelController alloc] initWithWindowNibName: @"PreferencesPanel"];
68
69  [[aController window] orderFront: nil];
70  //[NSApp runModalForWindow: [aController window]];
71}
72
73
74//
75// delegate methods
76//
77- (void) applicationDidFinishLaunching: (NSNotification *) not
78{
79  NSFileManager *aFileManager;
80  BOOL isDir;
81
82  aFileManager = [NSFileManager defaultManager];
83
84  if ( [aFileManager fileExistsAtPath: (NSString *)PlopFolioUserLibraryPath()
85		     isDirectory: &isDir] )
86    {
87      if ( !isDir )
88        {
89          NSLog(@"%@ exists but it is a file not a directory.",
90                PlopFolioUserLibraryPath());
91          exit(1);
92        }
93    }
94  else
95    {
96      if ( ![aFileManager createDirectoryAtPath: (NSString *)PlopFolioUserLibraryPath()
97			  attributes: nil] )
98	{
99	  // directory creation failed.  quit.
100	  NSLog(@"Could not create directory: %@", PlopFolioUserLibraryPath());
101	  exit(1);
102	}
103      else
104        {
105          NSLog(@"Created directory: %@", PlopFolioUserLibraryPath());
106        }
107    }
108
109
110  // We verify if our archived NSMutableArray exists, if not,
111  // we create. If yes, we decode it.
112  if ( [aFileManager fileExistsAtPath: PathToPlops()] )
113    {
114      allPlops = [NSUnarchiver unarchiveObjectWithFile: PathToPlops()];
115      RETAIN(allPlops);
116    }
117  else
118    {
119      allPlops = [[NSMutableArray alloc] init];
120      [self synchronize];
121    }
122
123  [self showAllPlops];
124
125}
126
127
128//
129//
130//
131#ifdef MACOSX
132- (NSApplicationTerminateReply) applicationShouldTerminate: (NSApplication *) theSender
133#else
134- (BOOL) applicationShouldTerminate: (NSApplication *) theSender
135#endif
136{
137  [self synchronize];
138
139  return YES;
140}
141
142
143//
144//
145//
146- (void) applicationWillFinishLaunching: (NSNotification *) not
147{
148  // Local variable
149#ifndef MACOSX
150  SEL action = NULL;
151#endif
152
153  // We begin by setting our NSApp's logo
154  [NSApp setApplicationIconImage: [NSImage imageNamed: @"PlopFolio.tiff"]];
155
156    // We continue by creating our NSMenu
157#ifndef MACOSX
158  menu = [[NSMenu alloc] init];
159
160  [menu addItemWithTitle:_(@"Info") action: action keyEquivalent: @""];
161  [menu addItemWithTitle:_(@"Edit") action: action  keyEquivalent: @""];
162  [menu addItemWithTitle:_(@"Windows") action: action keyEquivalent: @"p"];
163  [menu addItemWithTitle:_(@"Print") action: action keyEquivalent: @"p"];
164  [menu addItemWithTitle:_(@"Services") action: action keyEquivalent: @""];
165  [menu addItemWithTitle:_(@"Hide") action: @selector (hide:) keyEquivalent: @"h"];
166  [menu addItemWithTitle:_(@"Quit") action:@selector(terminate:) keyEquivalent: @"q"];
167
168  // Our Info menu / submenus
169  info = [[NSMenu alloc] init];
170  [menu setSubmenu:info forItem:[menu itemWithTitle:_(@"Info")]];
171  [info addItemWithTitle:_(@"Info Panel...")
172        action: @selector(orderFrontStandardInfoPanel:)
173        keyEquivalent:@""];
174  [info addItemWithTitle:_(@"Preferences...")
175        action: @selector(showPreferencesPanel:)
176        keyEquivalent:@""];
177  [info addItemWithTitle:_(@"Help...")
178        action: action
179        keyEquivalent:@"?"];
180  RELEASE(info);
181
182  // Our Edit menu / submenus
183  edit = [[NSMenu alloc] init];
184  [menu setSubmenu:edit forItem:[menu itemWithTitle:_(@"Edit")]];
185  [edit addItemWithTitle: _(@"Cut")
186	action: @selector(cut:)
187	keyEquivalent: @"x"];
188  [edit addItemWithTitle: _(@"Copy")
189	action: @selector(copy:)
190	keyEquivalent: @"c"];
191  [edit addItemWithTitle: _(@"Paste")
192	action: @selector(paste:)
193	keyEquivalent: @"v"];
194  [edit addItemWithTitle: _(@"Delete")
195	action: @selector(delete:)
196	keyEquivalent: @""];
197  [edit addItemWithTitle: _(@"Select All")
198	action: @selector(selectAll:)
199	keyEquivalent: @"a"];
200  RELEASE(edit);
201
202  // Our Windows menu
203  windows = [[NSMenu alloc] init];
204  [menu setSubmenu:windows forItem: [menu itemWithTitle:_(@"Windows")]];
205
206  // Our Services menu
207  services = [[NSMenu alloc] init];
208  [menu setSubmenu: services forItem: [menu itemWithTitle: _(@"Services")]];
209
210  [NSApp setMainMenu: menu];
211  [NSApp setServicesMenu: services];
212  [NSApp setWindowsMenu: windows];
213
214  RELEASE(services);
215  RELEASE(windows);
216  RELEASE(menu);
217#endif
218}
219
220
221#ifdef MACOSX
222- (void) awakeFromNib
223{
224  dockMenu = [[NSMenu alloc] init];
225  [dockMenu setAutoenablesItems: NO];
226}
227
228
229- (NSMenu *) applicationDockMenu: (NSApplication *) sender
230{
231    return dockMenu;
232}
233#endif
234
235
236//
237// other methods
238//
239- (void) showAllPlops
240{
241  int i;
242
243  // We show all the Plops
244  for (i = 0; i < [allPlops count]; i++)
245    {
246      [self showPlop: [allPlops objectAtIndex: i]];
247    }
248}
249
250
251//
252//
253//
254- (void) showPlop: (Plop *) thePlop
255{
256  PlopWindowController *aPlopWindowController;
257  Channel *aChannel;
258
259  if ( ![thePlop isActive] )
260    {
261      // We verify if it had an opened channel. If so, we close the channel.
262      if ( [thePlop channel] )
263	{
264	  NSLog(@"Must close the channel!");
265	  [[[thePlop channel] plopWindowController] close];
266	}
267
268      return;
269    }
270  else
271    {
272      // If the plop is active and has a channel, we simply do NOTHING here.
273      if ( [thePlop channel] )
274	{
275	  return;
276	}
277    }
278
279  // The plop is active but has no channel, let's create a channel for this plop.
280  aChannel = [[Channel alloc] init];
281  [aChannel setPlop: thePlop];
282  [aChannel update: self];
283
284  aPlopWindowController = [[PlopWindowController alloc] initWithWindowNibName: @"PlopWindow"];
285  [aPlopWindowController setChannel: aChannel];
286  [[aPlopWindowController window] orderFront: nil];
287
288  RELEASE(aChannel);
289}
290
291
292//
293//
294//
295- (BOOL) synchronize
296{
297  return [NSArchiver archiveRootObject: allPlops
298		     toFile: PathToPlops()];
299}
300
301
302//
303// access / mutation methods
304//
305- (NSMutableArray *) allPlops
306{
307  return allPlops;
308}
309
310//
311// static methods
312//
313
314@end
315
316
317//
318// Starting point for Terminal.app
319//
320int main(int argc, const char *argv[], char *env[])
321{
322  NSAutoreleasePool *pool;
323  PlopFolio *plop;
324
325  pool = [[NSAutoreleasePool alloc] init];
326  plop = [[PlopFolio alloc] init];
327
328  [NSApplication sharedApplication];
329  [NSApp setDelegate: plop];
330
331  NSApplicationMain(argc, argv);
332
333  RELEASE(plop);
334  RELEASE(pool);
335
336  return 0;
337}
338