File: main.m

package info (click to toggle)
lusernet.app 0.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 828 kB
  • sloc: objc: 8,495; makefile: 22
file content (485 lines) | stat: -rw-r--r-- 12,125 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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 *
 * Copyright 2002 Alexander Malmberg <alexander@malmberg.org>
 *           2012 The Free Software Foundation
 *
 * Authors: Alexander Malmberg <alexander@malmberg.org>
 *          Riccardo Mottola <rm@gnu.org>
 *
 */

#import <Foundation/NSObject.h>
#import <Foundation/NSAutoreleasePool.h>
#import <Foundation/NSUserDefaults.h>
#import <Foundation/NSPathUtilities.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSDictionary.h>
#import <AppKit/NSApplication.h>
#import <AppKit/NSGraphics.h>
#import <AppKit/NSMenu.h>
#import <AppKit/NSWindow.h>

#import <Pantomime/CWCharset.h>

#import "MsgDB.h"

#import "LogWindowController.h"
#import "FolderWindowController.h"
#import "FolderListController.h"
#import "ComposeWindowController.h"
#import "NNTPSource.h"


#import "main.h"

#include "VERSION"


AppDelegate *app_delegate;


#define LOG_MESSAGES_ONLY


#define LogWindowVisibleKey @"LogWindowVisible"
#define FolderListVisibleKey @"FolderListVisible"
#define FoldersVisibleKey @"FoldersVisible"

#define LocationKey @"MessageDatabaseLocation"

NSString *DefaultEncodingChangedNotification = @"DefaultEncodingChangedNotification";

@interface NSMenu (im_lazy)
-(id <NSMenuItem>) addItemWithTitle: (NSString *)s;
-(id <NSMenuItem>) addItemWithTitle: (NSString *)s  action: (SEL)sel;
@end

@implementation NSMenu (im_lazy)
-(id <NSMenuItem>) addItemWithTitle: (NSString *)s
{
	return [self addItemWithTitle: s  action: NULL  keyEquivalent: nil];
}

-(id <NSMenuItem>) addItemWithTitle: (NSString *)s  action: (SEL)sel
{
	return [self addItemWithTitle: s  action: sel  keyEquivalent: nil];
}
@end


#include <AppKit/NSSavePanel.h>

@implementation AppDelegate


-(void) composeArticle: (NSDictionary *)headers;
{
	[[[ComposeWindowController alloc] initWithHeaders: headers] showWindow: self];
}

-(void) composeFollowupToMessage: (CWMessage *)msg
{
	[[[ComposeWindowController alloc] initWithFollowupToMessage: msg] showWindow: self];
}


-(void) composeNewArticle: (id)sender
{
	[self composeArticle: nil];
}


-(void) postArticleFrom: (id)sender : (unsigned char *)data : (int)length
{ /* TODO */
	id src;

//	fprintf(stderr,"post article %@\n",sender);
	src=[[mdb sources] objectAtIndex: 0];
	if (!src || ![src respondsToSelector: @selector(postArticle:length:sender:)])
	{
		fprintf(stderr,"no source!\n");
		NSBeep();
		return;
	}

	[src postArticle: data  length: length  sender: sender];
}


- initWithApp: (NSApplication *)a
{
	CREATE_AUTORELEASE_POOL(arp);
	NSString *dir;

	if (!(self=[super init])) return nil;
	app=a;
	folder_windows=[[NSMutableDictionary alloc] init];

	dir=[[NSUserDefaults standardUserDefaults] objectForKey: LocationKey];
	if (!dir)
	{
		dir=[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,NSUserDomainMask,YES) objectAtIndex: 0];
		dir=[[dir stringByAppendingPathComponent: @"LuserNET"]
			stringByStandardizingPath];

		[[NSUserDefaults standardUserDefaults] setObject: dir  forKey: LocationKey];
	}

	mdb=[[MsgDB alloc] initWithDirectory: dir];

	DESTROY(arp);
	return self;
}

-(void) applicationWillTerminate: (NSNotification *)n
{
	[mdb syncToDisk];

	[[NSNotificationCenter defaultCenter]
		removeObserver: self];

	{
		NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
		NSMutableArray *a;
		NSEnumerator *e;
		NSString *fn;

		[ud setBool: [[log_window window] isVisible] forKey: LogWindowVisibleKey];
		if (folder_list)
			[ud setBool: [[folder_list window] isVisible] forKey: FolderListVisibleKey];
		else
			[ud setBool: NO forKey: FolderListVisibleKey];

		a=[[NSMutableArray alloc] init];
		for (e=[folder_windows keyEnumerator];(fn=[e nextObject]);)
		{
			if ([[[folder_windows objectForKey: fn] window] isVisible])
				[a addObject: fn];
		}
		[ud setObject: a forKey: FoldersVisibleKey];
		[a release];
	}

	DESTROY(preferences_window);
	DESTROY(log_window);
	DESTROY(folder_list);
	DESTROY(folder_windows);
	DESTROY(mdb);
}


-(void) openFolderWindow: (NSString *)folder_name
{
	FolderWindowController *fw;

	fw=[folder_windows objectForKey: folder_name];
	if (!fw)
	{
		fw=[[FolderWindowController alloc] initWithMsgDB: mdb  folder: folder_name];
		if (!fw) return; /* TODO: bring up error panel? */
		[folder_windows setObject: fw forKey: folder_name];
		[fw release];
	}
	[fw showWindow: self];
}

-(void) openMessageWindow: (msg_id_t)mid
{
	NSLog(@"TODO: openMessageWindow: %i\n",mid);
}


-(void) quit: (id)sender
{
	[app terminate: sender];
}

-(void) update: (id)sender
{
	[mdb updateAll];
}

-(void) listFolders: (id)sender
{
	if (!folder_list)
		folder_list=[[FolderListController alloc] initWithMsgDB: mdb];
	[folder_list showWindow: self];
}

-(void) logWindow: (id)sender
{
	[log_window showWindow: self];
}


-(void) logMessage: (NSNotification *)n
{
#ifdef LOG_MESSAGES_ONLY
	[log_window addLogString: [[n userInfo] objectForKey: @"Message"]];
#else
	[log_window addLogString: [NSString stringWithFormat: @"%@: %@ %@ %@",n,[n name],[n object],[n userInfo]]];
#endif
}

-(void) changeEncoding:(id)sender
{
  NSDictionary *charsets = [CWCharset allCharsets];

  //  [sender selectItemWithTitle:[sender title]];
  //  [sender synchronizeTitleAndSelectedItem];
  
  [[NSUserDefaults standardUserDefaults] setObject:
	[[charsets allKeysForObject:[sender title]] objectAtIndex:0] 
      forKey:@"DefaultEncoding"];

  [[NSNotificationCenter defaultCenter]
    postNotificationName: DefaultEncodingChangedNotification
    object: nil];
}

-(void) applicationWillFinishLaunching: (NSNotification *)n
{
	NSMenu *menu,*m,*m2;

	menu=[[NSMenu alloc] init];

	/* 'Info' menu */
	m=[[NSMenu alloc] init];
	[m addItemWithTitle: _(@"Preferences...")
		action: @selector(openPreferences:)];
	[m addItemWithTitle: _(@"Info...")
		action: @selector(orderFrontStandardInfoPanel:)];
	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Info")]];
	[m release];

	/* 'Folder list' menu */
	m=[[NSMenu alloc] init];
	[m addItemWithTitle: _(@"Open list")
		action: @selector(listFolders:)
		keyEquivalent: @"f"];
	[m addItemWithTitle: _(@"Open folder")
		action: @selector(openFolder:)];
	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Folder list")]];
	[m release];


	/* 'Folder' menu */
	m=[[NSMenu alloc] init];

	/* Folder->Move to */
	m2=[[NSMenu alloc] init];
	[m2 addItemWithTitle: _(@"Parent")
		action: @selector(moveToParent:)
		keyEquivalent: @"P"];
	[m2 addItemWithTitle: _(@"Next unread")
		action: @selector(moveToNextUnread:)
		keyEquivalent: @"n"];
	[m2 addItemWithTitle: _(@"Scroll/next")
		action: @selector(scrollNextUnread:)
		keyEquivalent: @" "];
	[m2 addItemWithTitle: _(@"Next branch")
		action: @selector(skipBranch:)
		keyEquivalent: @"b"];
	[m2 addItemWithTitle: _(@"Next thread")
		action: @selector(skipBranch:)
		keyEquivalent: @"t"];
	[m setSubmenu: m2 forItem: [m addItemWithTitle: _(@"Move to")]];
	[m2 release];

	/* Folder->Mark read */
	m2=[[NSMenu alloc] init];
	[m2 addItemWithTitle: _(@"Branch")
		action: @selector(skipBranchMark:)
		keyEquivalent: @"B"];
	[m2 addItemWithTitle: _(@"Thread")
		action: @selector(skipBranchMark:)
		keyEquivalent: @"T"];
	[m2 addItemWithTitle: _(@"All")
		action: @selector(markAll:)
		keyEquivalent: @"A"];
	[m setSubmenu: m2 forItem: [m addItemWithTitle: _(@"Mark read")]];
	[m2 release];

	/* Folder->Sort by */
	m2=[[NSMenu alloc] init];
	[m2 addItemWithTitle: _(@"Thread")
		action: @selector(folderSortThread)];
	[m2 addItemWithTitle: _(@"Reverse thread")
		action: @selector(folderSortReverseThread)];
/* TODO: these are currently unusably slow
	[m2 addItemWithTitle: _(@"Subject")
		action: @selector(folderSortSubject)];
	[m2 addItemWithTitle: _(@"Reverse subject")
		action: @selector(folderSortReverseSubject)];
	[m2 addItemWithTitle: _(@"From")
		action: @selector(folderSortFrom)];
	[m2 addItemWithTitle: _(@"Reverse from")
		action: @selector(folderSortReverseFrom)];
	[m2 addItemWithTitle: _(@"Date")
		action: @selector(folderSortDate)];
	[m2 addItemWithTitle: _(@"Reverse date")
		action: @selector(folderSortReverseDate)];*/
	[m2 addItemWithTitle: _(@"Arrival")
		action: @selector(folderSortOrder)];
	[m2 addItemWithTitle: _(@"Reverse arrival")
		action: @selector(folderSortReverseOrder)];
	[m setSubmenu: m2 forItem: [m addItemWithTitle: _(@"Sort by")]];
	[m2 release];

	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Folder")]];
	[m release];


	/* 'Message' menu */
	m=[[NSMenu alloc] init];
/*	[m addItemWithTitle: _(@"Open")
		action: @selector(openMessage:)];*/
	m2=[[NSMenu alloc] init];
	
	NSDictionary *charsets = [CWCharset allCharsets];
	NSEnumerator *en = [[charsets allValues] objectEnumerator];
	NSString *encoding;
	while((encoding = [en nextObject]))
	  {
	    [m2 addItemWithTitle: encoding
		action: @selector(changeEncoding:)];
	  }

	[m setSubmenu: m2 forItem: [m addItemWithTitle: _(@"Change encoding...")]];
	[m addItemWithTitle: _(@"Toggle read/unread")
		action: @selector(messageToggleRead:)
		keyEquivalent: @"m"];
	[m addItemWithTitle: _(@"Switch font")
		action: @selector(switchFont)
		keyEquivalent: @"F"];
	[m addItemWithTitle: _(@"Show source")
		action: @selector(showSource)];
	[m addItemWithTitle: _(@"Download")
		action: @selector(messageDownload)];
	[m addItemWithTitle: _(@"Save...")
		action: @selector(messageSave)
		keyEquivalent: @"s"];
	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Message")]];
	[m release];


	/* 'Compose' menu */
	m=[[NSMenu alloc] init];
	[m addItemWithTitle: _(@"New article")
		action: @selector(composeNewArticle:)];
	[m addItemWithTitle: _(@"Followup to group")
		action: @selector(composeFollowup:)
		keyEquivalent: @"r"];
	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Compose")]];
	[m release];


	/* 'Edit' menu */
	m=[[NSMenu alloc] init];
	[m addItemWithTitle: _(@"Copy")
		action: @selector(copy:)
		keyEquivalent: @"c"];
	[m addItemWithTitle: _(@"Cut")
		action: @selector(cut:)
		keyEquivalent: @"x"];
	[m addItemWithTitle: _(@"Paste")
		action: @selector(paste:)
		keyEquivalent: @"v"];
	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Edit")]];
	[m release];


	/* Main menu entries */
	[menu addItemWithTitle: _(@"Update")
		action: @selector(update:)
		keyEquivalent: @"u"];

	[menu addItemWithTitle: _(@"Log window")
		action: @selector(logWindow:)
		keyEquivalent: @"l"];

	m=[[NSMenu alloc] init];
	[m addItemWithTitle: _(@"Close")
		action: @selector(performClose:)
		keyEquivalent: @"w"];
	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Windows")]];
	[app setWindowsMenu: m];
	[m release];

	m=[[NSMenu alloc] init];
	[menu setSubmenu: m forItem: [menu addItemWithTitle: _(@"Services")]];
	[app setServicesMenu: m];
	[m release];

	[menu addItemWithTitle: _(@"Hide")
		action: @selector(hide:)
		keyEquivalent: @"h"];

	[menu addItemWithTitle: _(@"Quit")
		action: @selector(quit:)
		keyEquivalent: @"q"];

	[app setMainMenu: menu];
	[menu setTitle: @"LuserNET.app"];
	[menu release];


	log_window=[[LogWindowController alloc] init];
	[log_window addLogString: [NSString stringWithFormat: _(@"Welcome to LuserNET.app v%s"),VERSION]];
	[log_window addLogString: _(@"alt-f brings up the folder lists; alt-u checks for new messages")];


	[[NSNotificationCenter defaultCenter]
		addObserver: self
		selector: @selector(logMessage:)
#ifdef LOG_MESSAGES_ONLY
		name: MsgDB_LogMessageNotification
#else
		name: nil
#endif
		object: mdb];
}

-(void) applicationDidFinishLaunching: (NSNotification *)n
{
	NSUserDefaults *ud=[NSUserDefaults standardUserDefaults];
	NSArray *a;

	if ([ud boolForKey: LogWindowVisibleKey])
		[log_window showWindow: self];
	if ([ud boolForKey: FolderListVisibleKey])
		[self listFolders: self];

	a=[ud arrayForKey: FoldersVisibleKey];
	if (a)
	{
		int i,c=[a count];
		for (i=0;i<c;i++)
			[self openFolderWindow: [a objectAtIndex: i]];
	}
}

@end


int main(int argc, char **argv)
{
	AppDelegate *ad;
	NSApplication *a;

	CREATE_AUTORELEASE_POOL(arp);

//	[NSObject enableDoubleReleaseCheck: YES];

	a=[NSApplication sharedApplication];
	app_delegate=ad=[[AppDelegate alloc] initWithApp: a];
	[a setDelegate: ad];
	[a run];

	DESTROY(ad);
	DESTROY(a);

	DESTROY(arp);
	return 0;
}