OSDN Git Service

for macOS 10.14 Mojave
[letter-fix/LetterFix.git] / LetterFix.m
index c543f79..a6e7eb4 100644 (file)
@@ -3,7 +3,7 @@
 //  LetterFix2
 //
 //  Created by kuri on 10/02/01.
-//  Copyright 2010-2015 kuri. All rights reserved.
+//  Copyright 2010-2017 kuri. All rights reserved.
 //
 #import <objc/runtime.h>
 #import <objc/objc-runtime.h>
@@ -58,7 +58,9 @@ static void backendSetPreferredEncoding(id backend, unsigned long long encoding)
        Ivar ivar = object_getInstanceVariable(backend, "_flags", NULL);
        _LF_flags *_flags = (_LF_flags *)((char *)backend + ivar_getOffset(ivar));
        _flags->encodingHint = encoding;
-    } else if ([app ver] == LF_Yosemite || [app ver] == LF_ElCapitan) {
+    } else if ([app ver] == LF_Yosemite || [app ver] == LF_ElCapitan ||
+               [app ver] == LF_Sierra || [app ver] == LF_HighSierra ||
+               [app ver] == LF_Mojave) {
        [backend setEncodingHint: encoding];
     }
 }
@@ -69,7 +71,9 @@ static unsigned long long backendGetPreferredEncoding(id backend)
        Ivar ivar = object_getInstanceVariable(backend, "_flags", NULL);
        _LF_flags *_flags = (_LF_flags *)((char *)backend + ivar_getOffset(ivar));
        return _flags->encodingHint; //FIXME: [backend _encodingHint] return wrong value?
-    } else if ([app ver] == LF_Yosemite || [app ver] == LF_ElCapitan) {
+    } else if ([app ver] == LF_Yosemite || [app ver] == LF_ElCapitan ||
+               [app ver] == LF_Sierra || [app ver] == LF_HighSierra ||
+               [app ver] == LF_Mojave) {
        return [backend encodingHint];
     }
     return LF_Encoding_Auto;
@@ -345,7 +349,7 @@ void fixSubjectOnLoad(id self)
        }
     }
     [[(LetterFix *)self backEnd] setSubject:substr];
-    [[self window] setTitle:substr];
+    [[self _windowLF] setTitle:substr];
     
     id headers = [self headersEditor];
     NSTextField *subjectField = nil;
@@ -408,7 +412,7 @@ void fixHeader(id self)
        }
     }
     [[(LetterFix *)self backEnd] setSubject:substr];
-    [[self window] setTitle:substr];
+    [[self _windowLF] setTitle:substr];
     
     id headers = [self headersEditor];
     NSTextField *subjectField = nil;
@@ -460,19 +464,20 @@ BOOL _LF_IMP_isLoaded(id self, SEL _cmd)
             if ([app operationAtOpen] == 0) {
                 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
                 [alert addButtonWithTitle:@"変換"];
-                [alert addButtonWithTitle:@"変換しない"];
-                [alert.buttons.lastObject setKeyEquivalent:@"\x1b"];
+                NSButton *button = [alert addButtonWithTitle:@"変換しない"];
+                button.keyEquivalent = @"\x1b";
                 [alert setShowsSuppressionButton:TRUE];
                 [alert setMessageText:@"編集前にメッセージを変換しますか?"];
                 [alert setInformativeText:@"このメッセージには ISO 2022-JP でエンコードできない文字が含まれています。"];
                 [alert setAlertStyle:NSInformationalAlertStyle];
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
-                    [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode){
+                    [alert beginSheetModalForWindow:[self _windowLF] completionHandler:^(NSModalResponse returnCode){
                         if (returnCode == NSAlertFirstButtonReturn) {
                             fixSubjectOnLoad(self);
                             if ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite) {
                                 fixLetter(self, [self webView], FALSE, TRUE);
-                            } else if ([app ver] == LF_ElCapitan) {
+                            } else if ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+                                       [app ver] == LF_HighSierra|| [app ver] == LF_Mojave) {
                                 fixLetter(self, [self composeWebView], FALSE, TRUE);
                             }
                         } else if (returnCode == NSAlertSecondButtonReturn) {
@@ -535,17 +540,18 @@ void _LF_IMP_finishLoadingEditor(id self, SEL _cmd)
             if ([app operationAtOpen] == 0) {
                 NSAlert *alert = [[[NSAlert alloc] init] autorelease];
                 [alert addButtonWithTitle:@"変換"];
-                [alert addButtonWithTitle:@"変換しない"];
-                [alert.buttons.lastObject setKeyEquivalent:@"\x1b"];
+                NSButton *button = [alert addButtonWithTitle:@"変換しない"];
+               button.keyEquivalent = @"\x1b";
                 [alert setShowsSuppressionButton:TRUE];
                 [alert setMessageText:@"編集前にメッセージを変換しますか?"];
                 [alert setInformativeText:@"このメッセージには ISO 2022-JP でエンコードできない文字が含まれています。"];
                 [alert setAlertStyle:NSInformationalAlertStyle];
                 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
-                    [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode){
+                    [alert beginSheetModalForWindow:[self _windowLF] completionHandler:^(NSModalResponse returnCode){
                         if (returnCode == NSAlertFirstButtonReturn) {
                             fixSubjectOnLoad(self);
-                            if ([app ver] == LF_ElCapitan) {
+                            if ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+                                [app ver] == LF_HighSierra|| [app ver] == LF_Mojave) {
                                 fixLetter(self, [self composeWebView], FALSE, TRUE);
                             }
                         } else if (returnCode == NSAlertSecondButtonReturn) {
@@ -583,7 +589,8 @@ void _LF_IMP_send_(id self, SEL _cmd, id arg1)
     id composeView;
     if ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite) {
         composeView = [self webView];
-    } else if ([app ver] == LF_ElCapitan) {
+    } else if ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+               [app ver] == LF_HighSierra|| [app ver] == LF_Mojave) {
         composeView = [self composeWebView];
     } else {
         [self _LF_send: arg1];
@@ -594,18 +601,19 @@ void _LF_IMP_send_(id self, SEL _cmd, id arg1)
     if (0 < fixLetter(self, composeView, TRUE, FALSE)) {
         NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert addButtonWithTitle:@"変換して送信"];
-       [alert addButtonWithTitle:@"キャンセル"];
-        [alert.buttons.lastObject setKeyEquivalent:@"\x1b"];
+       NSButton *button = [alert addButtonWithTitle:@"キャンセル"];
+       button.keyEquivalent = @"\x1b";
        [alert addButtonWithTitle:@"変換せずに送信"];
        [alert addButtonWithTitle:@"変換のみ"];
        [alert setMessageText:@"メッセージを変換して送信しますか?"];
        [alert setInformativeText:@"送信しようとしているメッセージには ISO 2022-JP でエンコードできない文字が含まれています。"];
        [alert setAlertStyle:NSInformationalAlertStyle];
-        [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
+        [alert beginSheetModalForWindow:[self _windowLF] completionHandler:^(NSModalResponse returnCode) {
             if (returnCode == NSAlertFirstButtonReturn) {
                 if ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite) {
                     fixLetter(self, [self webView], FALSE, FALSE);
-                } else if ([app ver] == LF_ElCapitan) {
+                } else if ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+                           [app ver] == LF_HighSierra|| [app ver] == LF_Mojave) {
                     fixLetter(self, [self composeWebView], FALSE, FALSE);
                 }
                 if (backendGetPreferredEncoding([self backEnd]) != LF_Encoding_ISO2022JP) backendSetPreferredEncoding([self backEnd], LF_Encoding_ISO2022JP);
@@ -623,7 +631,8 @@ void _LF_IMP_send_(id self, SEL _cmd, id arg1)
             } else if (returnCode == (NSAlertThirdButtonReturn + 1)) { // 変換のみ
                 if ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite) {
                     fixLetter(self, [self webView], FALSE, FALSE);
-                } else if ([app ver] == LF_ElCapitan) {
+                } else if ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+                           [app ver] == LF_HighSierra|| [app ver] == LF_Mojave) {
                     fixLetter(self, [self composeWebView], FALSE, FALSE);
                 }
                 if (backendGetPreferredEncoding([self backEnd]) != LF_Encoding_ISO2022JP) backendSetPreferredEncoding([self backEnd], LF_Encoding_ISO2022JP);
@@ -640,14 +649,14 @@ void _LF_alertSubject(id self, SEL _cmd, id arg1)
 {
     NSAlert *alert = [[[NSAlert alloc] init] autorelease];
     [alert addButtonWithTitle:@"変換して送信"];
-    [alert addButtonWithTitle:@"キャンセル"];
-    [alert.buttons.lastObject setKeyEquivalent:@"\x1b"];
+    NSButton *button = [alert addButtonWithTitle:@"キャンセル"];
+    button.keyEquivalent = @"\x1b";
     [alert addButtonWithTitle:@"変換せずに送信"];
     [alert addButtonWithTitle:@"変換のみ"];
     [alert setMessageText:@"件名を変換して送信しますか?"];
     [alert setInformativeText:@"送信するメッセージの件名には ISO 2022-JP でエンコードできない文字が含まれています。"];
     [alert setAlertStyle:NSInformationalAlertStyle];
-    [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
+    [alert beginSheetModalForWindow:[self _windowLF] completionHandler:^(NSModalResponse returnCode) {
         if (returnCode == NSAlertFirstButtonReturn) {
             fixHeader(self);
             [[alert window] orderOut:arg1];
@@ -668,7 +677,9 @@ void _LF_IMP_saveDocument_(id self, SEL _cmd, id arg1)
     id composeView;
     if ([app willCheckOnSave] && ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite)) {
         composeView = [self webView];
-    } else if ([app willCheckOnSave] && [app ver] == LF_ElCapitan) {
+    } else if ([app willCheckOnSave] &&
+               ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+                [app ver] == LF_HighSierra|| [app ver] == LF_Mojave)) {
         composeView = [self composeWebView];
     } else {
         [self _LF_saveDocument: arg1];
@@ -678,17 +689,18 @@ void _LF_IMP_saveDocument_(id self, SEL _cmd, id arg1)
     if (([app isActive] != NO) && ([check_at_save containsObject:self]==NO) &&(0 < fixLetter(self, composeView, TRUE, FALSE))) {
        NSAlert *alert = [[[NSAlert alloc] init] autorelease];
        [alert addButtonWithTitle:@"変換"];
-       [alert addButtonWithTitle:@"変換しない"];
-        [alert.buttons.lastObject setKeyEquivalent:@"\x1b"];
+       NSButton *button = [alert addButtonWithTitle:@"変換しない"];
+       button.keyEquivalent = @"\x1b";
        [alert setMessageText:@"保存前にメッセージを変換しますか?"];
        [alert setInformativeText:@"このメッセージには ISO 2022-JP でエンコードできない文字が含まれています。\n"
         @"変換しないを選択した場合、メールの送信またはウインドウを開き直すまでエンコーディングの確認を行いません。"];
        [alert setAlertStyle:NSWarningAlertStyle];
-        [alert beginSheetModalForWindow:[self window] completionHandler:^(NSModalResponse returnCode) {
+        [alert beginSheetModalForWindow:[self _windowLF] completionHandler:^(NSModalResponse returnCode) {
             if (returnCode == NSAlertFirstButtonReturn) {
                 if ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite) {
                     fixLetter(self, [self webView], FALSE, FALSE);
-                } else if ([app ver] == LF_ElCapitan) {
+                } else if ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+                           [app ver] == LF_HighSierra|| [app ver] == LF_Mojave) {
                     fixLetter(self, [self composeWebView], FALSE, FALSE);
                 }
             } else if (returnCode == NSAlertSecondButtonReturn) {
@@ -708,6 +720,17 @@ void _LF_IMP_animationCompleted(id self, SEL _cmd)
     [self _LF_animationCompleted];
 }
 
+NSWindow *_LF_IMP_window(id self, SEL _cmd)
+{
+    if ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite || [app ver] == LF_ElCapitan ) {
+        return [self window];
+    } else if ([app ver] == LF_Sierra || [app ver] == LF_HighSierra||
+               [app ver] == LF_Mojave) {
+        return [[self view] window];
+    }
+    return nil;
+}
+
 #define MVMailBundle        (NSClassFromString(@"MVMailBundle"))
 
 @implementation LetterFix
@@ -725,11 +748,13 @@ void _LF_IMP_animationCompleted(id self, SEL _cmd)
     Class editorClass = nil;
     if ([app ver] == LF_Mavericks || [app ver] == LF_Yosemite) {
         editorClass = NSClassFromString(@"DocumentEditor");
-    } else if ([app ver] == LF_ElCapitan) {
+    } else if ([app ver] == LF_ElCapitan || [app ver] == LF_Sierra ||
+               [app ver] == LF_HighSierra|| [app ver] == LF_Mojave) {
         editorClass = NSClassFromString(@"ComposeViewController");
     } else {
         return;
     }
+    class_addMethod(editorClass, @selector(_windowLF), (IMP)_LF_IMP_window, "@@:");
     
     //
     // swizzling method
@@ -739,6 +764,8 @@ void _LF_IMP_animationCompleted(id self, SEL _cmd)
         swizzlingMethod(editorClass, @selector(_animationCompleted), @selector(_LF_animationCompleted), (IMP)_LF_IMP_animationCompleted);
     } else if ([app ver] == LF_ElCapitan) {
         swizzlingMethod(editorClass, @selector(finishLoadingEditor), @selector(_LF_finishLoadingEditor), (IMP)_LF_IMP_finishLoadingEditor);
+    } else if ([app ver] == LF_Sierra || [app ver] == LF_HighSierra || [app ver] == LF_Mojave) {
+        swizzlingMethod(editorClass, @selector(_finishLoadingEditor), @selector(_LF_finishLoadingEditor), (IMP)_LF_IMP_finishLoadingEditor);
     }
     swizzlingMethod(editorClass, @selector(saveDocument:), @selector(_LF_saveDocument:), (IMP)_LF_IMP_saveDocument_);
     swizzlingMethod(editorClass, @selector(send:), @selector(_LF_send:), (IMP)_LF_IMP_send_);