1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
5 # import needed classes/functions from Cocoa
6 from Foundation import *
9 # import Nib loading functionality from AppKit
10 from PyObjCTools import NibClassBuilder, AppHelper
12 from twisted.internet import _threadedselect
13 _threadedselect.install()
15 from twisted.internet import reactor, protocol
16 from twisted.web import http
17 from twisted.python import log
20 # create ObjC classes as defined in MainMenu.nib
21 NibClassBuilder.extractClasses("MainMenu")
22 class TwistzillaClient(http.HTTPClient):
23 def __init__(self, delegate, urls):
25 self.delegate = delegate
27 def connectionMade(self):
28 self.sendCommand('GET', str(self.urls[2]))
29 self.sendHeader('Host', '%s:%d' % (self.urls[0], self.urls[1]))
30 self.sendHeader('User-Agent', 'CocoaTwistzilla')
33 def handleResponse(self, data):
34 self.delegate.gotResponse_(data)
36 class MyAppDelegate(NibClassBuilder.AutoBaseClass):
37 def gotResponse_(self, html):
38 s = self.resultTextField.textStorage()
39 s.replaceCharactersInRange_withString_((0, s.length()), html)
40 self.progressIndicator.stopAnimation_(self)
42 def doTwistzillaFetch_(self, sender):
43 s = self.resultTextField.textStorage()
44 s.deleteCharactersInRange_((0, s.length()))
45 self.progressIndicator.startAnimation_(self)
46 u = urlparse.urlparse(self.messageTextField.stringValue())
51 host, port = u[1][:pos], int(u[1][pos+1:])
56 host = host.encode('utf8')
57 fname = fname.encode('utf8')
58 protocol.ClientCreator(reactor, TwistzillaClient, self, (host, port, fname)).connectTCP(host, port).addErrback(lambda f:self.gotResponse_(f.getBriefTraceback()))
60 def applicationDidFinishLaunching_(self, aNotification):
62 Invoked by NSApplication once the app is done launching and
63 immediately before the first pass through the main event
66 self.messageTextField.setStringValue_("http://www.twistedmatrix.com/")
67 reactor.interleave(AppHelper.callAfter)
69 def applicationShouldTerminate_(self, sender):
71 reactor.addSystemEventTrigger(
72 'after', 'shutdown', AppHelper.stopEventLoop)
77 if __name__ == '__main__':
78 log.startLogging(sys.stdout)
79 AppHelper.runEventLoop()