Package muntjac :: Package terminal :: Package gwt :: Package server :: Module web_browser
[hide private]
[frames] | no frames]

Source Code for Module muntjac.terminal.gwt.server.web_browser

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """Defines a class that provides information about the web browser the user 
 17  is using.""" 
 18   
 19  from time import time 
 20  from datetime import date 
 21   
 22  from muntjac.terminal.gwt.client.v_browser_details import VBrowserDetails 
 23  from muntjac.terminal.terminal import ITerminal 
 24   
 25   
26 -class WebBrowser(ITerminal):
27 """Class that provides information about the web browser the user is 28 using. Provides information such as browser name and version, screen 29 resolution and IP address. 30 31 @author: Vaadin Ltd. 32 @author: Richard Lincoln 33 @version: 1.1.2 34 """ 35
36 - def __init__(self):
37 self._screenHeight = 0 38 self._screenWidth = 0 39 self._browserApplication = None 40 self._locale = None 41 self._address = None 42 self._secureConnection = None 43 self._timezoneOffset = 0 44 self._rawTimezoneOffset = 0 45 self._dstSavings = None 46 self._dstInEffect = None 47 self._touchDevice = None 48 49 self._browserDetails = None 50 self._clientServerTimeDelta = None
51 52
53 - def getDefaultTheme(self):
54 """There is no default-theme for this terminal type. 55 56 @return: Always returns null. 57 """ 58 return None
59 60
61 - def getScreenHeight(self):
62 return self._screenHeight
63 64
65 - def getScreenWidth(self):
66 return self._screenWidth
67 68
69 - def getBrowserApplication(self):
70 """Get the browser user-agent string. 71 72 @return: The raw browser userAgent string 73 """ 74 return self._browserApplication
75 76
77 - def getAddress(self):
78 """Gets the IP-address of the web browser. If the application is 79 running inside a portlet, this method will return C{None}. 80 81 @return: IP-address in 1.12.123.123 -format 82 """ 83 return self._address
84 85
86 - def getLocale(self):
87 """Get the default locate of the browser.""" 88 return self._locale
89 90
91 - def isSecureConnection(self):
92 """Is the connection made using HTTPS?""" 93 return self._secureConnection
94 95
96 - def isFirefox(self):
97 """Tests whether the user is using Firefox. 98 99 @return: true if the user is using Firefox, false if the user is not 100 using Firefox or if no information on the browser is present 101 """ 102 if self._browserDetails is None: 103 return False 104 105 return self._browserDetails.isFirefox()
106 107
108 - def isIE(self):
109 """Tests whether the user is using Internet Explorer. 110 111 @return: true if the user is using Internet Explorer, false if the 112 user is not using Internet Explorer or if no information on 113 the browser is present 114 """ 115 if self._browserDetails is None: 116 return False 117 118 return self._browserDetails.isIE()
119 120
121 - def isSafari(self):
122 """Tests whether the user is using Safari. 123 124 @return: true if the user is using Safari, false if the user is not 125 using Safari or if no information on the browser is present 126 """ 127 if self._browserDetails is None: 128 return False 129 130 return self._browserDetails.isSafari()
131 132
133 - def isOpera(self):
134 """Tests whether the user is using Opera. 135 136 @return: true if the user is using Opera, false if the user is not 137 using Opera or if no information on the browser is present 138 """ 139 if self._browserDetails is None: 140 return False 141 142 return self._browserDetails.isOpera()
143 144
145 - def isChrome(self):
146 """Tests whether the user is using Chrome. 147 148 @return: true if the user is using Chrome, false if the user is not 149 using Chrome or if no information on the browser is present 150 """ 151 if self._browserDetails is None: 152 return False 153 154 return self._browserDetails.isChrome()
155 156
157 - def getBrowserMajorVersion(self):
158 """Gets the major version of the browser the user is using. 159 160 Note that Internet Explorer in IE7 compatibility mode might 161 return 8 in some cases even though it should return 7. 162 163 @return: The major version of the browser or -1 if not known. 164 """ 165 if self._browserDetails is None: 166 return -1 167 168 return self._browserDetails.getBrowserMajorVersion()
169 170
171 - def getBrowserMinorVersion(self):
172 """Gets the minor version of the browser the user is using. 173 174 @see: #getBrowserMajorVersion() 175 176 @return: The minor version of the browser or -1 if not known. 177 """ 178 if self._browserDetails is None: 179 return -1 180 181 return self._browserDetails.getBrowserMinorVersion()
182 183
184 - def isLinux(self):
185 """Tests whether the user is using Linux. 186 187 @return: true if the user is using Linux, false if the user is not 188 using Linux or if no information on the browser is present 189 """ 190 return self._browserDetails.isLinux()
191 192
193 - def isMacOSX(self):
194 """Tests whether the user is using Mac OS X. 195 196 @return: true if the user is using Mac OS X, false if the user is not 197 using Mac OS X or if no information on the browser is present 198 """ 199 return self._browserDetails.isMacOSX()
200 201
202 - def isWindows(self):
203 """Tests whether the user is using Windows. 204 205 @return: true if the user is using Windows, false if the user is not 206 using Windows or if no information on the browser is present 207 """ 208 return self._browserDetails.isWindows()
209 210
211 - def getTimezoneOffset(self):
212 """Returns the browser-reported TimeZone offset in milliseconds from 213 GMT. This includes possible daylight saving adjustments, to figure 214 out which TimeZone the user actually might be in, see 215 L{getRawTimezoneOffset}. 216 217 @see: L{getRawTimezoneOffset} 218 @return: timezone offset in milliseconds, 0 if not available 219 """ 220 return self._timezoneOffset
221 222
223 - def getRawTimezoneOffset(self):
224 """Returns the browser-reported TimeZone offset in milliseconds 225 from GMT ignoring possible daylight saving adjustments that may 226 be in effect in the browser. 227 228 You can use this to figure out which TimeZones the user could actually 229 be in by calling L{TimeZone.getAvailableIDs}. 230 231 If L{getRawTimezoneOffset} and L{getTimezoneOffset} returns the same 232 value, the browser is either in a zone that does not currently have 233 daylight saving time, or in a zone that never has daylight saving time. 234 235 @return: timezone offset in milliseconds excluding DST, 0 if not 236 available 237 """ 238 return self._rawTimezoneOffset
239 240
241 - def getDSTSavings(self):
242 """Gets the difference in minutes between the browser's GMT TimeZone 243 and DST. 244 245 @return: the amount of minutes that the TimeZone shifts when DST is in 246 effect 247 """ 248 return self._dstSavings
249 250
251 - def isDSTInEffect(self):
252 """Determines whether daylight savings time (DST) is currently in 253 effect in the region of the browser or not. 254 255 @return: true if the browser resides at a location that currently is in 256 DST 257 """ 258 return self._dstInEffect
259 260
261 - def getCurrentDate(self):
262 """Returns the current date and time of the browser. This will not be 263 entirely accurate due to varying network latencies, but should provide 264 a close-enough value for most cases. Also note that the returned Date 265 object uses servers default time zone, not the clients. 266 267 @return: the current date and time of the browser. 268 @see: L{isDSTInEffect} 269 @see: L{getDSTSavings} 270 @see: L{getTimezoneOffset} 271 """ 272 return date.fromtimestamp(time() + self._clientServerTimeDelta)
273 274
275 - def isTouchDevice(self):
276 """@return: true if the browser is detected to support touch events""" 277 return self._touchDevice
278 279
280 - def updateClientSideDetails(self, sw, sh, tzo, rtzo, dstSavings, 281 dstInEffect, curDate, touchDevice):
282 """For internal use by AbstractApplicationServlet only. Updates all 283 properties in the class according to the given information. 284 285 @param sw: 286 Screen width 287 @param sh: 288 Screen height 289 @param tzo: 290 TimeZone offset in minutes from GMT 291 @param rtzo: 292 raw TimeZone offset in minutes from GMT (w/o DST adjustment) 293 @param dstSavings: 294 the difference between the raw TimeZone and DST in minutes 295 @param dstInEffect: 296 is DST currently active in the region or not? 297 @param curDate: 298 the current date in milliseconds since the epoch 299 @param touchDevice: 300 """ 301 if sw is not None: 302 try: 303 self._screenHeight = int(sh) 304 self._screenWidth = int(sw) 305 except ValueError: 306 self._screenHeight = self._screenWidth = 0 307 if tzo is not None: 308 try: 309 # browser->python conversion: min->ms, reverse sign 310 self._timezoneOffset = -int(tzo) * 60 * 1000 311 except ValueError: 312 self._timezoneOffset = 0 # default gmt+0 313 if rtzo is not None: 314 try: 315 # browser->python conversion: min->ms, reverse sign 316 self._rawTimezoneOffset = -int(rtzo) * 60 * 1000 317 except ValueError: 318 self._rawTimezoneOffset = 0 # default gmt+0 319 if dstSavings is not None: 320 try: 321 # browser->python conversion: min->ms 322 self._dstSavings = int(dstSavings) * 60 * 1000 323 except ValueError: 324 self._dstSavings = 0 # default no savings 325 326 if dstInEffect is not None: 327 self._dstInEffect = bool(dstInEffect) 328 329 if curDate is not None: 330 try: 331 curTime = int(curDate) 332 self._clientServerTimeDelta = curTime - time() 333 except ValueError: 334 self._clientServerTimeDelta = 0 335 336 self._touchDevice = touchDevice
337 338
339 - def updateRequestDetails(self, locale, address, secureConnection, agent):
340 """For internal use by AbstractApplicationServlet only. Updates all 341 properties in the class according to the given information. 342 343 @param locale: 344 The browser primary locale 345 @param address: 346 The browser ip address 347 @param secureConnection: 348 true if using an https connection 349 @param agent: 350 Raw userAgent string from the browser 351 """ 352 self._locale = locale 353 self._address = address 354 self._secureConnection = secureConnection 355 if agent is not None: 356 self._browserApplication = agent 357 self._browserDetails = VBrowserDetails(agent)
358