summaryrefslogtreecommitdiffstats
path: root/iw/release_notes.py
blob: 632687eba911e8e5d80617a75eea4419969ad683 (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/python
#
# release_notes.py - "I can't believe it's not a web browser."
#
# David Cantrell <dcantrell@redhat.com>
#
# Copyright 2006 Red Hat, Inc.
#
# This software may be freely redistributed under the terms of the GNU
# library public license.
#
# You should have received a copy of the GNU Library Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

import sys
import os
import gtk
import gtkhtml2
import urllib
import urlparse
import thread
import threading

import gui

from rhpl.translate import _, N_

class ReleaseNotesViewer(threading.Thread):
	def __init__(self):
		self.currentURI = None
		self.htmlheader = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body bgcolor=\"white\"><pre>"
		self.htmlfooter = "</pre></body></html>"
		self.doc = gtkhtml2.Document()
		self.vue = gtkhtml2.View()
		self.opener = urllib.FancyURLopener()

		self.doc.connect('request_url', self.requestURLCallBack)
		self.doc.connect('link_clicked', self.linkClickedCallBack)
		self.vue.connect('request_object', self.requestObjectCallBack)
		self.topDir = None

		self.width = None
		self.height = None

		#self.id = id
		#self.dispatch = dispatch
		self.id = None
		self.dispatch = None

		if self.id is not None and self.dispatch is not None:
			self.load()
			self.resize()

		threading.Thread.__init__(self, name='RelNotesThr')

	def run(self):
		self.load()
		self.resize()
		self.view()

	def setId(self, id):
		self.id = id

	def setDispatch(self, dispatch):
		self.dispatch = dispatch

	def getReleaseNotes(self):
		langs = self.id.instLanguage.getCurrentLangSearchList() + [ "" ]
		suffixList = []
		for lang in langs:
			if lang:
				suffixList.append("-%s.html" % (lang,))
				suffixList.append(".%s" % (lang,))

		for suffix in suffixList:
			fn = "RELEASE-NOTES%s" % (suffix,)
			try:
				tmpfile = os.path.abspath("/" +
					self.dispatch.method.getFilename(fn,
					destdir="/tmp", retry=0))
				if tmpfile is None:
					continue

				# Just because we got a filename back doesn't
				# mean it's a valid file.  Check that it's not
				# zero length too.
				st = os.stat(tmpfile)
				if st.st_size == 0L:
					os.remove(tmpfile)
					continue

				self.topDir = os.path.dirname(tmpfile)
				return tmpfile
			except:
				continue

		print "here"
		return None

	def resize(self, w=None, h=None):
		sw = gtk.gdk.screen_width()
		(step, args) = self.dispatch.currentStep()

		if w is None:
			if sw >= 800:
				self.width = 800
			else:
				self.width = 640
		else:
			self.width = int(w)

		# if we are at the installation progress bar step, make the
		# release notes window smaller so the progress bar is still
		# visible...otherwise, consume the entire screen
		if h is None:
			if sw >= 800:
				if step == "installpackages":
					self.height = 445
				else:
					self.height = 600
			else:
				if step == "installpackages":
					self.height = 300
				else:
					self.height = 480
		else:
			self.height = int(h)

	# FIXME: replace with logger from anaconda_log (fix exec first)
	def log(self, string):
		print string

	def load(self, uri=None):
		def loadWrapper(baloney):
			self.doc.open_stream('text/html')
			self.doc.write_stream(self.htmlheader)
			self.doc.write_stream(baloney)
			self.doc.write_stream(self.htmlfooter)

		if uri is None:
			uri = self.getReleaseNotes()

		if os.access(uri, os.R_OK):
			try:
				f = self.openURI(uri)
			except OSError:
				log.info("Failed to open %s" % (link,))
				return

			self.doc.clear()
			headers = f.info()

			mime = headers.getheader('Content-type')
			if mime:
				self.doc.open_stream(mime)
				self.doc.write_stream(f.read())
			else:
				loadWrapper(f.read())

			self.doc.close_stream()
			f.close()

			self.currentURI = self.resolveURI(uri)
		else:
			loadWrapper(_("Release notes are missing.\n"))

			self.currentURI = None

	def view(self):
		self.vue.set_document(self.doc)
		textWin = gtk.Dialog(flags=gtk.DIALOG_MODAL)
		table = gtk.Table(3, 3, False)
		textWin.vbox.pack_start(table)
		textWin.add_button('gtk-close', gtk.RESPONSE_NONE)
		textWin.connect("response", self.closedCallBack)

		vbox1 = gtk.VBox()
		vbox1.set_border_width(10)
		frame = gtk.Frame("")
		frame.add(vbox1)
		frame.set_label_align(0.5, 0.5)
		frame.set_shadow_type(gtk.SHADOW_NONE)

		textWin.set_position(gtk.WIN_POS_NONE)
		textWin.set_gravity(gtk.gdk.GRAVITY_NORTH_WEST)

		if self.vue is not None:
			sw = gtk.ScrolledWindow()
			sw.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
			sw.set_shadow_type(gtk.SHADOW_IN)
			sw.add(self.vue)
			vbox1.pack_start(sw)

			a = gtk.Alignment(0, 0, 1.0, 1.0)
			a.add(frame)

			textWin.set_default_size(self.width, self.height)
			textWin.set_size_request(self.width, self.height)

			# we want the release notes dialog to be the same
			# size as the main installer window so it covers it
			# up completely.  this isn't always the same size
			# as the root window, so figure out our northwest
			# origin point and then move the window
			if gtk.gdk.screen_width() == self.width:
				textWin.move(0, 0)
			else:
				# the width will always be fixed, but our
				# height changes depending on the installation
				# stage, so do the origin point calculations
				# using what would be the full height
				if self.width == 800:
					fullh = 600
				elif self.width == 640:
					fullh = 480

				left = (gtk.gdk.screen_width() - self.width) / 2
				top = (gtk.gdk.screen_height() - fullh) / 2
				textWin.move(left, top)

			table.attach(a, 1, 2, 1, 2, gtk.FILL | gtk.EXPAND, gtk.FILL | gtk.EXPAND, 5, 5)

			textWin.set_border_width(0)
			gui.addFrame(textWin, _("Release Notes"))
			textWin.show_all()
		else:
			textWin.set_position(gtk.WIN_POS_CENTER)
			label = gtk.Label(_("Unable to load file!"))

			table.attach(label, 1, 2, 1, 2, gtk.FILL | gtk.EXPAND, gtk.FILL | gtk.EXPAND, 5, 5)

			textWin.set_border_width(0)
			gui.addFrame(textWin)
			textWin.show_all()

		# set cursor to normal (assuming that anaconda set it to busy
		# when it exec'd this viewer app to give progress indicator
		# to user).
		root = gtk.gdk.get_default_root_window()
		cursor = gtk.gdk.Cursor(gtk.gdk.LEFT_PTR)
		root.set_cursor(cursor)

		gtk.threads_enter()
		gtk.main()
		gtk.threads_leave()

	def resolveURI(self, link):
		parts = urlparse.urlparse(link)
		if parts[0] or parts[1]:
			return link
		else:
			return urlparse.urljoin(self.currentURI, link)

	def openURI(self, link):
		return self.opener.open(self.resolveURI(link))

	def closedCallBack(self, widget, data):
		root = gtk.gdk.get_default_root_window()
		cursor = gtk.gdk.Cursor(gtk.gdk.WATCH)
		root.set_cursor(cursor)
		thread.exit()

	def linkClickedCallBack(self, document, link):
		if link[0] == '#':
			self.log("jump to anchor: %s" % (link,))
			self.vue.jump_to_anchor(link)
		else:
			self.load(link)

	def requestURLCallBack(self, document, url, stream):
		try:
			f = self.openURI(url)
			stream.write(f.read())
		except:
			# we'll try local from self.topDir
			url = os.path.abspath(self.topDir + '/' + url)
			try:
				f = self.openURI(url)
				stream.write(f.read())
			except:
				self.log("requested url not found: %s" % (url,))

	def requestObjectCallBack(self, *args):
		self.log("request objects call back: %s" % (args))