/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ /* AbiSource Application Framework * Copyright (C) 2005 Francis James Franklin * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA * 02111-1307, USA. */ #import #import "AbiMailMan.h" #import "AbiMailMan_Controller.h" #import "AbiMailMan_DataSetController.h" @implementation AbiMailMan_Controller - (id)initWithAbiWord:(XAP_CocoaPlugin *)AbiWord { if (self = [super initWithWindowNibName:@"Main"]) { m_AbiWord = AbiWord; m_UntitledNumber = 0; } return self; } - (void)dealloc { [m_AbiWord removeMenuItem:oMailMergeManagerItem]; [super dealloc]; } - (void)windowDidLoad { NSMenu * menu = 0; if (menu = [oMailMergeManagerItem menu]) { [oMailMergeManagerItem retain]; [menu removeItem:oMailMergeManagerItem]; } [m_AbiWord appendMenuItem:oMailMergeManagerItem]; NSPanel * panel = (NSPanel *) [self window]; [panel setBecomesKeyOnlyIfNeeded:YES]; [self appendToLog:@"AbiMailMan: Mail Merge Manager plug-in for AbiWord\n"]; } - (void)newDataSetForDocument:(id )document { [self logEntry:@"-" forMethod:@"newDataSetForDocument:"]; AbiMailMan_DataSetController * DSC = [[AbiMailMan_DataSetController alloc] initWithAbiWord:m_AbiWord withController:self withDataSet:nil]; if (DSC) { m_UntitledNumber++; [DSC window]; [DSC setDocument:document]; [DSC setUntitledWithNumber:m_UntitledNumber]; // TODO: add DSC to internal array & release [self logError:@"Incompletely implemented!" forMethod:@"newDataSetForDocument:"]; } } - (void)openDataSet:(NSString *)path forDocument:(id )document { [self logEntry:@"-" forMethod:@"openDataSet:forDocument:"]; NSMutableArray * dataset = [m_AbiWord importMailMergeSource:path]; if (!dataset) { [self logError:@"No data set! Error opening mail merge file?" forMethod:@"openDataSet:forDocument:"]; return; } AbiMailMan_DataSetController * DSC = [[AbiMailMan_DataSetController alloc] initWithAbiWord:m_AbiWord withController:self withDataSet:dataset]; if (DSC) { [DSC window]; [DSC setDocument:document]; [DSC setMailMergeSource:path]; // TODO: add DSC to internal array & release [self logError:@"Incompletely implemented!" forMethod:@"openDataSet:forDocument:"]; } } - (IBAction)aNewDataSet:(id)sender { [self logEntry:@"-" forMethod:@"aNewDataSet:"]; [self newDataSetForDocument:nil]; } - (IBAction)aOpenDataSet:(id)sender { [self logEntry:@"-" forMethod:@"aOpenDataSet:"]; NSString * path = [m_AbiWord selectMailMergeSource]; if (path) { [self openDataSet:path forDocument:nil]; } else { [self logError:@"No file name! (Possibly harmless error.)" forMethod:@"aOpenDataSet:"]; } } - (IBAction)aCurrentDataSet:(id)sender { [self logEntry:@"-" forMethod:@"aCurrentDataSet:"]; id document = [m_AbiWord currentDocument]; if (!document) { [self logError:@"No current document!" forMethod:@"aCurrentDataSet:"]; return; } NSString * path = [document documentMailMergeSource]; if (path) { [self openDataSet:path forDocument:document]; } else { [self newDataSetForDocument:document]; } } - (IBAction)aLog:(id)sender { NSPanel * panel = (NSPanel *) [self window]; if ([panel isVisible]) [panel orderOut:self]; else [panel orderFront:self]; } - (IBAction)aAbout:(id)sender { [self logError:@"Not implemented!" forMethod:@"aAbout:"]; // TODO } - (IBAction)aHelp:(id)sender { NSBundle * AbiMailMan_Bundle = [NSBundle bundleForClass:[self class]]; NSString * HelpIndex_Path = [[AbiMailMan_Bundle resourcePath] stringByAppendingString:@"/help/index.html"]; [[NSWorkspace sharedWorkspace] openURL:[NSURL fileURLWithPath:HelpIndex_Path]]; } - (BOOL)validateMenuItem:(id )menuItem { BOOL bEnabled = YES; if ([menuItem tag] == 4) { if ([m_AbiWord currentDocument] == nil) { bEnabled = NO; } } else if ([menuItem tag] == 5) { NSPanel * panel = (NSPanel *) [self window]; [menuItem setState:([panel isVisible] ? NSOnState : NSOffState)]; } return bEnabled; } - (void)appendToLog:(id)aString { [oLog setEditable:YES]; [oLog setSelectedRange:NSMakeRange(0,0)]; [oLog moveToEndOfDocument:self]; [oLog insertText:aString]; [oLog setEditable:NO]; } - (void)logEntry:(NSString *)aString forMethod:(NSString *)method { NSString * method_name = [NSString stringWithFormat:@"(%@) ", method]; NSDictionary * attr = [NSDictionary dictionaryWithObject:[NSColor darkGrayColor] forKey:NSForegroundColorAttributeName]; NSAttributedString * str = [[NSAttributedString alloc] initWithString:method_name attributes:attr]; [self appendToLog:str]; [str release]; attr = [NSDictionary dictionaryWithObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName]; str = [[NSAttributedString alloc] initWithString:aString attributes:attr]; [self appendToLog:str]; [str release]; [self appendToLog:@"\n"]; } - (void)logError:(NSString *)aString forMethod:(NSString *)method { NSDictionary * attr = [NSDictionary dictionaryWithObject:[NSColor redColor] forKey:NSForegroundColorAttributeName]; NSAttributedString * str = [[NSAttributedString alloc] initWithString:@"Error: " attributes:attr]; [self appendToLog:str]; [str release]; [self logEntry:aString forMethod:method]; } /* Plugin should attempt to save all files, and return NO *only* if the user has answered * "Cancel" to an alert about an unsaved document. */ - (BOOL)pluginCanDeactivate { [self logError:@"Not implemented!" forMethod:@"pluginCanDeactivate"]; return YES; // TODO } @end