MessageSplitViewController.swift 4.41 KB
Newer Older
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
//
//  MessageSplitViewController.swift
//  MessageTextEntrySpike
//
//  Created by Aral Balkan on 03/01/2015.
//  Copyright (c) 2015 ind.ie. All rights reserved.
//

import Cocoa

class MessageSplitViewController: NSSplitViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
        NSNotificationCenter.defaultCenter().when(youGet: MessageNotification.named(.SendMessage), call: "sendMessage:", on: self)
    }
    
    
    func sendMessage(notification:NSNotification)
    {
        let timelineViewController = (self.splitViewItems[0] as! NSSplitViewItem).viewController as! TimelineViewController
        if let userInfo = (notification.userInfo as Dictionary!)
        {
            let message = userInfo["message"] as! NSAttributedString
            println("Send message: received text: \(message)")
            
            //
            // Create a file wrapper to save the contents as a directory.
            //
            let messageDirectoryWrapper:NSFileWrapper? = message.fileWrapperFromRange(NSMakeRange(0, message.length), documentAttributes: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSUTF8StringEncoding, NSExcludedElementsDocumentAttribute: ["xml", "html", "head", "body", "font", "span"], NSPrefixSpacesDocumentAttribute: 4], error: nil)
            
            let fileURL:NSURL? = NSURL(fileURLWithPath: NSHomeDirectory().stringByAppendingPathComponent("message"), isDirectory: true)
            
            if let messageDirectoryWrapper = messageDirectoryWrapper, fileURL = fileURL
            {
                let messageFileWrappers:[NSObject:AnyObject] = messageDirectoryWrapper.fileWrappers
                
                for (fileName, fileWrapper) in messageFileWrappers
                {
                    println(" * \(fileName): \(fileWrapper)")
                    if fileName == "index.html"
                    {
                        let indexData:NSData? = fileWrapper.regularFileContents
                        if let indexData = indexData
                        {
                            let indexHTML = NSString(data: indexData, encoding: NSUTF8StringEncoding)
                            if var indexHTML = indexHTML
                            {
                                //
                                // Massage the HTML.
                                //
                                
                                // Fix image URLs
                                indexHTML = indexHTML.stringByReplacingOccurrencesOfString("file:///", withString: "")
                                
                                // Remove redundant empty lines.
                                indexHTML = indexHTML.stringByReplacingOccurrencesOfString("<p><br></p>", withString: "")
                                
                                //
                                // Update the index.html.
                                //
                                
                                // Remove the index.html file wrapper.
                                messageDirectoryWrapper.removeFileWrapper(fileWrapper as! NSFileWrapper)
                                
                                // And replace it with the new index.html we created.
                                let newIndexHTMLData:NSData? = indexHTML.dataUsingEncoding(NSUTF8StringEncoding)
                                
                                if let newIndexHTMLData = newIndexHTMLData
                                {
                                    let newIndexHTMLFileWrapper = NSFileWrapper(regularFileWithContents: newIndexHTMLData)
                                    newIndexHTMLFileWrapper.preferredFilename = "index.html"
                                    messageDirectoryWrapper.addFileWrapper(newIndexHTMLFileWrapper)
                                    
                                    break
                                }
                            }
                        }
                    }
                }
                
                // TODO: Error checking.
                messageDirectoryWrapper.writeToURL(fileURL, options: NSFileWrapperWritingOptions.Atomic, originalContentsURL: nil, error: nil)
                
                println(messageDirectoryWrapper)
                
            }
            
            timelineViewController.showMessageAtURL(fileURL!)
        }
        
    }
    
}