Initial import to Tizen
[profile/ivi/python-twisted.git] / doc / core / examples / threadedselect / Cocoa / SimpleWebClient / Twistzilla.py
1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
3
4
5 # import needed classes/functions from Cocoa
6 from Foundation import *
7 from AppKit import *
8
9 # import Nib loading functionality from AppKit
10 from PyObjCTools import NibClassBuilder, AppHelper
11
12 from twisted.internet import _threadedselect
13 _threadedselect.install()
14
15 from twisted.internet import reactor, protocol
16 from twisted.web import http
17 from twisted.python import log
18 import sys, urlparse
19
20 # create ObjC classes as defined in MainMenu.nib
21 NibClassBuilder.extractClasses("MainMenu")
22 class TwistzillaClient(http.HTTPClient):
23     def __init__(self, delegate, urls):
24         self.urls = urls
25         self.delegate = delegate
26
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')
31         self.endHeaders()
32
33     def handleResponse(self, data):
34         self.delegate.gotResponse_(data)
35
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)
41     
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())
47         pos = u[1].find(':')
48         if pos == -1:
49             host, port = u[1], 80
50         else:
51             host, port = u[1][:pos], int(u[1][pos+1:])
52         if u[2] == '':
53             fname = '/'
54         else:
55             fname = u[2]
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()))
59
60     def applicationDidFinishLaunching_(self, aNotification):
61         """
62         Invoked by NSApplication once the app is done launching and
63         immediately before the first pass through the main event
64         loop.
65         """
66         self.messageTextField.setStringValue_("http://www.twistedmatrix.com/")
67         reactor.interleave(AppHelper.callAfter)
68
69     def applicationShouldTerminate_(self, sender):
70         if reactor.running:
71             reactor.addSystemEventTrigger(
72                 'after', 'shutdown', AppHelper.stopEventLoop)
73             reactor.stop()
74             return False
75         return True
76     
77 if __name__ == '__main__':
78     log.startLogging(sys.stdout)
79     AppHelper.runEventLoop()