2007-03-04 15:26:35 +00:00
|
|
|
/** <title>NSSpellServer</title>
|
|
|
|
|
|
|
|
<abstract>Class to allow a spell checker to be available to other apps.</abstract>
|
|
|
|
|
|
|
|
Copyright (C) 2001, 1996 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Author by: Gregory John Casamento <borgheron@yahoo.com>
|
|
|
|
Date: 2001
|
|
|
|
Author: Scott Christley <scottc@net-community.com>
|
|
|
|
Date: 1996
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
2007-09-14 11:36:11 +00:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
2007-03-04 15:26:35 +00:00
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
version 2 of the License, or (at your option) any later version.
|
2007-03-04 15:26:35 +00:00
|
|
|
|
|
|
|
This library 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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2007-03-04 15:26:35 +00:00
|
|
|
License along with this library; see the file COPYING.LIB.
|
|
|
|
If not, write to the Free Software Foundation,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
#import "common.h"
|
2010-02-14 10:48:10 +00:00
|
|
|
#define EXPOSE_NSSpellServer_IVARS 1
|
|
|
|
#import "Foundation/NSSpellServer.h"
|
|
|
|
#import "Foundation/NSDictionary.h"
|
|
|
|
#import "Foundation/NSRunLoop.h"
|
|
|
|
#import "Foundation/NSFileManager.h"
|
|
|
|
#import "Foundation/NSUserDefaults.h"
|
|
|
|
#import "Foundation/NSPathUtilities.h"
|
|
|
|
#import "Foundation/NSConnection.h"
|
|
|
|
#import "Foundation/NSProcessInfo.h"
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
#import "Foundation/NSSet.h"
|
2007-03-04 15:26:35 +00:00
|
|
|
|
2008-06-06 13:57:06 +00:00
|
|
|
NSString *const NSGrammarRange = @"NSGrammarRange";
|
|
|
|
NSString *const NSGrammarUserDescription = @"NSGrammarUserDescription";
|
|
|
|
NSString *const NSGrammarCorrections = @"NSGrammarCorrections";
|
|
|
|
|
2011-02-10 12:11:03 +00:00
|
|
|
static NSConnection *spellServerConnection = nil;
|
|
|
|
|
2007-03-04 15:26:35 +00:00
|
|
|
/* User dictionary location */
|
|
|
|
static NSString *GNU_UserDictionariesDir = @"Dictionaries";
|
|
|
|
|
|
|
|
// Function to create name for spell server....
|
|
|
|
NSString*
|
|
|
|
GSSpellServerName(NSString *vendor, NSString *language)
|
|
|
|
{
|
|
|
|
NSString *serverName = nil;
|
|
|
|
|
|
|
|
if (language == nil || vendor == nil)
|
|
|
|
{
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
serverName = [[vendor stringByAppendingString: language]
|
|
|
|
stringByAppendingString: @"SpellChecker"];
|
|
|
|
|
|
|
|
return serverName;
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation NSSpellServer
|
|
|
|
|
|
|
|
// Class methods
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
|
|
|
if (self == [NSSpellServer class])
|
|
|
|
{
|
|
|
|
// Initial version
|
|
|
|
[self setVersion: 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Non-private Instance methods
|
|
|
|
- (id) init
|
|
|
|
{
|
2011-03-05 07:54:05 +00:00
|
|
|
NSArray *userPreference;
|
|
|
|
NSString *currentLanguage;
|
2007-03-04 15:26:35 +00:00
|
|
|
|
2011-03-05 07:54:05 +00:00
|
|
|
userPreference = [[NSUserDefaults standardUserDefaults]
|
|
|
|
stringArrayForKey: @"NSLanguages"];
|
|
|
|
currentLanguage = [userPreference objectAtIndex: 0];
|
2007-03-04 15:26:35 +00:00
|
|
|
if ((self = [super init]) != nil)
|
|
|
|
{
|
|
|
|
_delegate = nil;
|
|
|
|
_ignoredWords = nil;
|
|
|
|
ASSIGN(_userDictionaries, [NSMutableDictionary dictionary]);
|
|
|
|
ASSIGN(_currentLanguage, currentLanguage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup when deallocated
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
|
|
|
RELEASE(_userDictionaries);
|
|
|
|
RELEASE(_currentLanguage);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checking in Your Service
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method vends the spell server to the Distributed Objects system
|
|
|
|
* so that it can be connected to by clients.
|
|
|
|
*/
|
|
|
|
- (BOOL) registerLanguage: (NSString *)language
|
|
|
|
byVendor: (NSString *)vendor
|
|
|
|
{
|
|
|
|
NSString *serverName = GSSpellServerName(vendor, language);
|
|
|
|
BOOL result = NO;
|
|
|
|
|
|
|
|
if (serverName == nil)
|
|
|
|
{
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2011-02-10 12:11:03 +00:00
|
|
|
spellServerConnection = [[NSConnection alloc] init];
|
|
|
|
if (spellServerConnection)
|
2007-03-04 15:26:35 +00:00
|
|
|
{
|
2011-02-10 12:11:03 +00:00
|
|
|
[spellServerConnection setRootObject: self];
|
|
|
|
result = [spellServerConnection registerName: serverName];
|
2007-03-04 15:26:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assigning a Delegate
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the spell server delegate.
|
|
|
|
*/
|
|
|
|
- (id) delegate
|
|
|
|
{
|
|
|
|
return _delegate;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is used to set the delegate of the spellserver.
|
|
|
|
* When a spelling service is run the spell server is vended out
|
|
|
|
* to DO. The spelling service must instantiate an instance of
|
|
|
|
* this class and set itself to be the delegate. This allows
|
|
|
|
* the service to respond to messages sent by the client.
|
|
|
|
*/
|
|
|
|
- (void) setDelegate: (id)anObject
|
|
|
|
{
|
|
|
|
/* FIXME - we should not retain the delegate ! */
|
2009-01-12 12:48:46 +00:00
|
|
|
IF_NO_GC(RETAIN(anObject);)
|
2007-03-04 15:26:35 +00:00
|
|
|
ASSIGN(_delegate, anObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Running the Service
|
|
|
|
/**
|
|
|
|
* Initiate the run loop of this service. Once the spell server
|
|
|
|
* object is vended, this method is called so that the server can
|
|
|
|
* start responding to the messages sent by the client. These
|
|
|
|
* messages are passed on to the NSSpellServer instance's delegate.
|
|
|
|
*/
|
|
|
|
- (void) run
|
|
|
|
{
|
|
|
|
// Start the runloop explicitly.
|
|
|
|
[[NSRunLoop currentRunLoop] run];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private method
|
|
|
|
// Determine the path to the dictionary
|
|
|
|
/**
|
|
|
|
* Path to the dictionary for the specified language.
|
|
|
|
*/
|
|
|
|
- (NSString *) _pathToDictionary: (NSString *)currentLanguage
|
|
|
|
{
|
|
|
|
NSString *path = nil;
|
|
|
|
NSString *user_gsroot = nil;
|
|
|
|
|
2007-03-06 18:38:22 +00:00
|
|
|
user_gsroot = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
|
2007-03-04 15:26:35 +00:00
|
|
|
NSUserDomainMask, YES) lastObject];
|
|
|
|
|
|
|
|
if (currentLanguage != nil)
|
|
|
|
{
|
|
|
|
NSString *dirPath = nil;
|
|
|
|
NSFileManager *mgr = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
// Build the path and try to get the dictionary
|
2007-03-06 18:38:22 +00:00
|
|
|
dirPath = [user_gsroot stringByAppendingPathComponent:
|
|
|
|
GNU_UserDictionariesDir];
|
2007-03-04 15:26:35 +00:00
|
|
|
path = [dirPath stringByAppendingPathComponent: currentLanguage];
|
|
|
|
|
|
|
|
if (![mgr fileExistsAtPath: path ])
|
|
|
|
{
|
|
|
|
if ([mgr fileExistsAtPath: dirPath])
|
|
|
|
{
|
|
|
|
// The directory exists create the file.
|
|
|
|
NSArray *emptyDict = [NSArray array];
|
|
|
|
|
|
|
|
if (![emptyDict writeToFile: path atomically: YES])
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to create %@",path);
|
|
|
|
path = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// The directory does not exist create it.
|
2012-08-10 08:36:09 +00:00
|
|
|
if ([mgr createDirectoryAtPath: dirPath
|
|
|
|
withIntermediateDirectories: YES
|
|
|
|
attributes: nil
|
|
|
|
error: NULL])
|
2007-03-04 15:26:35 +00:00
|
|
|
{
|
|
|
|
// Directory created. Now create the empty file.
|
|
|
|
NSArray *emptyDict = [NSArray array];
|
|
|
|
|
|
|
|
if (![emptyDict writeToFile: path atomically: YES])
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to create %@",path);
|
|
|
|
path = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Failed to create %@",dirPath);
|
|
|
|
path = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Private method
|
|
|
|
/** Open up dictionary stored in the user's directory. */
|
|
|
|
- (NSMutableSet *) _openUserDictionary: (NSString *)language
|
|
|
|
{
|
|
|
|
NSString *path = nil;
|
|
|
|
NSMutableSet *words = nil;
|
|
|
|
|
|
|
|
if ((words = [_userDictionaries objectForKey: language]) == nil)
|
|
|
|
{
|
|
|
|
if ((path = [self _pathToDictionary: language]) != nil)
|
|
|
|
{
|
|
|
|
NSArray *wordarray = [NSArray arrayWithContentsOfFile: path];
|
2007-03-06 18:38:22 +00:00
|
|
|
|
2007-03-04 15:26:35 +00:00
|
|
|
if (wordarray == nil)
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to load user dictionary from path %@",path);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
words = [NSMutableSet setWithArray: wordarray];
|
|
|
|
[_userDictionaries setObject: words forKey: language];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to find user dictionary at: %@", path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// successful in opening the desired dictionary..
|
|
|
|
return words;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checking User Dictionaries
|
|
|
|
/** Check if word is in dict, flag determines if the search is case sensitive. */
|
|
|
|
- (BOOL) _isWord: (NSString *)word
|
|
|
|
inDictionary: (NSSet *)dict
|
|
|
|
caseSensitive: (BOOL)flag
|
|
|
|
{
|
|
|
|
BOOL result = NO;
|
|
|
|
NSString *dictWord = nil;
|
|
|
|
NSEnumerator *setEnumerator = nil;
|
|
|
|
|
|
|
|
// Catch the odd cases before they start trouble later on...
|
|
|
|
if (word == nil || dict == nil)
|
|
|
|
{
|
|
|
|
return NO; // avoid checking, if NIL.
|
|
|
|
}
|
|
|
|
|
|
|
|
if ([word length] == 0 || [dict count] == 0)
|
|
|
|
{
|
|
|
|
return NO; // avoid checking, if has no length.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the dictionary for the word...
|
|
|
|
setEnumerator = [dict objectEnumerator];
|
|
|
|
while ((dictWord = [setEnumerator nextObject]) && result == NO)
|
|
|
|
{
|
|
|
|
// If the case is important then uppercase both strings
|
|
|
|
// and compare, otherwise do the comparison.
|
|
|
|
if (flag == NO)
|
|
|
|
{
|
|
|
|
NSString *upperWord = [word uppercaseString];
|
|
|
|
NSString *upperDictWord = [dictWord uppercaseString];
|
|
|
|
|
|
|
|
result = [upperWord isEqualToString: upperDictWord];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = [word isEqualToString: dictWord];
|
|
|
|
}
|
|
|
|
}
|
2010-02-22 20:09:51 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checking User Dictionaries
|
|
|
|
/**
|
|
|
|
Checks to see if the word is in the user's dictionary. The user dictionary
|
|
|
|
is a set of words learned by the spell service for that particular user
|
|
|
|
combined with the set of ignored words in the current document.
|
|
|
|
*/
|
|
|
|
- (BOOL) isWordInUserDictionaries: (NSString *)word
|
|
|
|
caseSensitive: (BOOL)flag
|
|
|
|
{
|
|
|
|
NSSet *userDict = [self _openUserDictionary: _currentLanguage];
|
|
|
|
BOOL result = NO;
|
|
|
|
|
|
|
|
if (userDict)
|
|
|
|
{
|
|
|
|
result = [self _isWord: word
|
|
|
|
inDictionary: userDict
|
|
|
|
caseSensitive: flag];
|
|
|
|
}
|
2007-03-04 15:26:35 +00:00
|
|
|
|
|
|
|
if (result == NO && _ignoredWords)
|
|
|
|
{
|
|
|
|
NSEnumerator *arrayEnumerator = [_ignoredWords objectEnumerator];
|
|
|
|
NSString *iword = nil;
|
|
|
|
|
|
|
|
while ((iword = [arrayEnumerator nextObject]) && result == NO)
|
|
|
|
{
|
|
|
|
// If the case is important then uppercase both strings
|
|
|
|
// and compare, otherwise do the comparison.
|
|
|
|
if (flag == NO)
|
|
|
|
{
|
|
|
|
NSString *upperWord = [word uppercaseString];
|
|
|
|
NSString *upperIWord = [iword uppercaseString];
|
|
|
|
|
|
|
|
result = [upperWord isEqualToString: upperIWord];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = [word isEqualToString: iword];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Save the dictionary stored in user's directory. */
|
|
|
|
- (BOOL) _saveUserDictionary: (NSString *)language
|
|
|
|
{
|
|
|
|
NSString *path = nil;
|
|
|
|
|
|
|
|
if ((path = [self _pathToDictionary: language]) != nil)
|
|
|
|
{
|
|
|
|
NSMutableSet *set = [_userDictionaries objectForKey: language];
|
2007-03-06 18:38:22 +00:00
|
|
|
|
2007-03-04 15:26:35 +00:00
|
|
|
if (![[set allObjects] writeToFile: path atomically: YES])
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to save dictionary to path %@",path);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NSLog(@"Unable to save dictionary at: %@", path);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
// successful in saving the desired dictionary..
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Learn a new word and put it into the dictionary. */
|
|
|
|
- (BOOL) _learnWord: (NSString *)word
|
|
|
|
inDictionary: (NSString *)language
|
|
|
|
{
|
|
|
|
NSMutableSet *set = [self _openUserDictionary: language];
|
|
|
|
[set addObject: word];
|
|
|
|
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[_delegate spellServer: self
|
|
|
|
didLearnWord: word
|
|
|
|
inLanguage: language];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Call to delegate cause the following exception: %@",
|
|
|
|
[localException reason]);
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
|
|
|
return [self _saveUserDictionary: language];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Forget a word and remove it from the dictionary. */
|
|
|
|
- (BOOL)_forgetWord: (NSString *)word
|
|
|
|
inDictionary: (NSString *)language
|
|
|
|
{
|
|
|
|
NSMutableSet *set = [self _openUserDictionary: language];
|
|
|
|
[set removeObject: word];
|
|
|
|
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
[_delegate spellServer: self
|
|
|
|
didForgetWord: word
|
|
|
|
inLanguage: language];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Call to delegate caused following exception: %@",
|
|
|
|
[localException reason]);
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
|
|
|
return [self _saveUserDictionary: language];
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Find a misspelled word. */
|
|
|
|
- (NSRange) _findMisspelledWordInString: (NSString *)stringToCheck
|
|
|
|
language: (NSString *)language
|
|
|
|
ignoredWords: (NSArray *)ignoredWords
|
2010-02-18 08:45:00 +00:00
|
|
|
wordCount: (int32_t *)wordCount
|
2007-03-04 15:26:35 +00:00
|
|
|
countOnly: (BOOL)countOnly
|
|
|
|
{
|
|
|
|
NSRange r = NSMakeRange(0,0);
|
|
|
|
|
|
|
|
// Forward to delegate
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
ASSIGN(_ignoredWords,ignoredWords);
|
|
|
|
r = [_delegate spellServer: self
|
|
|
|
findMisspelledWordInString: stringToCheck
|
|
|
|
language: language
|
|
|
|
wordCount: wordCount
|
|
|
|
countOnly: countOnly];
|
|
|
|
_ignoredWords = nil;
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Call to delegate caused the following exception: %@",
|
|
|
|
[localException reason]);
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Suggest a correction for the word. */
|
|
|
|
- (NSArray *) _suggestGuessesForWord: (NSString *)word
|
|
|
|
inLanguage: (NSString *)language
|
|
|
|
{
|
|
|
|
NSArray *words = nil;
|
|
|
|
|
|
|
|
// Forward to delegate
|
|
|
|
NS_DURING
|
|
|
|
{
|
|
|
|
words = [_delegate spellServer: self
|
|
|
|
suggestGuessesForWord: word
|
|
|
|
inLanguage: language];
|
|
|
|
}
|
|
|
|
NS_HANDLER
|
|
|
|
{
|
|
|
|
NSLog(@"Call to delegate caused the following exception: %@",
|
|
|
|
[localException reason]);
|
|
|
|
}
|
|
|
|
NS_ENDHANDLER
|
|
|
|
|
|
|
|
return words;
|
|
|
|
}
|
|
|
|
|
2008-06-06 13:57:06 +00:00
|
|
|
- (NSArray *) spellServer: (NSSpellServer *)sender
|
|
|
|
suggestCompletionsForPartialWordRange: (NSRange)range
|
|
|
|
inString: (NSString *)string
|
|
|
|
language: (NSString *)language
|
|
|
|
{
|
|
|
|
return nil; // FIXME
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSRange) spellServer: (NSSpellServer *)sender
|
|
|
|
checkGrammarInString: (NSString *)stringToCheck
|
|
|
|
language: (NSString *)language
|
|
|
|
details: (NSArray **)details
|
|
|
|
{
|
|
|
|
return NSMakeRange(0, 0); // FIXME
|
|
|
|
}
|
2007-03-04 15:26:35 +00:00
|
|
|
@end
|