1from Cocoa import *
2from loader import MHTLoader # XXX: integrate?
3
4class MHTDocument (NSDocument):
5    locationbox = objc.IBOutlet()
6    webview = objc.IBOutlet()
7
8    path = None
9    statusText = None
10
11    @objc.IBAction
12    def navigateHistory_(self, sender):
13        if sender.selectedSegment() == 0:
14            self.webview.goBack_(sender)
15        else:
16            self.webview.goForward_(sender)
17
18    def windowNibName(self):
19        return u"MHTDocument"
20
21    def readFromFile_ofType_(self, path, tp):
22        if self.webview is None:
23            self.path = path
24        else:
25            self.readMHT_(path)
26
27        return True
28
29    def writeToFile_ofType_(self, path, tp):
30        # TODO: 'save-as' functionality
31        return False
32
33    def windowControllerDidLoadNib_(self, controller):
34        if self.path:
35            self.readMHT_(self.path)
36
37    def readMHT_(self, path):
38        self.mht = MHTLoader(path)
39        self.locationbox.setStringValue_(self.mht.fixupURL(self.mht.root))
40        archive = self.mht.asWebArchive()
41        print "Archvie", archive.description()
42        open('/tmp/archive.webarchive', 'wb').write(archive.data().bytes())
43        self.webview.mainFrame().stopLoading()
44        self.webview.mainFrame().loadArchive_(archive)
45