My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
Changes to /trunk/python-webqq/src/webqqview.py
r12 vs. r14 Compare: vs.  Format:
Revision r14
Go to: 
Project members, sign in to write a code review
/trunk/python-webqq/src/webqqview.py   r12 /trunk/python-webqq/src/webqqview.py   r14
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # -*- coding=utf-8 -*- 2 # -*- coding=utf-8 -*-
3 3
4 import os, webkit, ctypes, webbrowser 4 import os, webkit, ctypes, webbrowser
5 import const, utils 5 import const, utils
6 6
7 try: 7 try:
8 libwebkit = ctypes.CDLL('libwebkitgtk-1.0.so.0') 8 libwebkit = ctypes.CDLL('libwebkitgtk-1.0.so.0')
9 except: 9 except:
10 try: 10 try:
11 libwebkit = ctypes.CDLL('libwebkit-1.0.so.2') 11 libwebkit = ctypes.CDLL('libwebkit-1.0.so.2')
12 except: 12 except:
13 libwebkit = ctypes.CDLL('libwebkitgtk-1.0.so') 13 libwebkit = ctypes.CDLL('libwebkitgtk-1.0.so')
14 14
15 libgobject = ctypes.CDLL('libgobject-2.0.so.0') 15 libgobject = ctypes.CDLL('libgobject-2.0.so.0')
16 libsoup = ctypes.CDLL('libsoup-2.4.so.1') 16 libsoup = ctypes.CDLL('libsoup-2.4.so.1')
17 17
18 class WebQQView(webkit.WebView): 18 class WebQQView(webkit.WebView):
19 def __init__(self, config): 19 def __init__(self, config):
20 webkit.WebView.__init__(self) 20 webkit.WebView.__init__(self)
21 self.hovered_uri = None 21 self.hovered_uri = None
22 self.config = config 22 self.config = config
23 self.init_settings() 23 self.init_settings()
24 self.init_cookie() 24 self.init_cookie()
25 self.init_proxy() 25 self.init_proxy()
26 self.init_signals() 26 self.init_signals()
27 27
28 def init_settings(self): 28 def init_settings(self):
29 settings = self.get_settings() 29 settings = self.get_settings()
30 settings.set_property("auto-resize-window", False) 30 settings.set_property("auto-resize-window", False)
31 settings.set_property('enable-universal-access-from-file-uris', True) 31 settings.set_property('enable-universal-access-from-file-uris', True)
32 settings.set_property('enable-file-access-from-file-uris', True) 32 settings.set_property('enable-file-access-from-file-uris', True)
33 settings.set_property('enable-page-cache', True) 33 settings.set_property('enable-page-cache', True)
34 settings.set_property('enable-spatial-navigation', True) 34 settings.set_property('enable-spatial-navigation', True)
35 settings.set_property('enable-site-specific-quirks', True) 35 settings.set_property('enable-site-specific-quirks', True)
36 #settings.set_property('user-agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.4+ (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.4+') 36 #settings.set_property('user-agent', 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.4+ (KHTML, like Gecko) Chrome/14.0.835.202 Safari/535.4+')
37 37
38 def init_cookie(self): 38 def init_cookie(self):
39 if not os.path.exists(const.COOKIE_PATH):
40 os.makedirs(const.COOKIE_PATH)
41 if not os.path.exists(const.COOKIE_FILE): 39 if not os.path.exists(const.COOKIE_FILE):
42 os.mknod(const.COOKIE_FILE) 40 os.mknod(const.COOKIE_FILE)
43 session = libwebkit.webkit_get_default_session() 41 session = libwebkit.webkit_get_default_session()
44 libgobject.g_object_set(session, 'add-feature', libsoup.soup_cookie_jar_text_new(const.COOKIE_FILE, False), None) 42 soup_cookie = libsoup.soup_cookie_jar_text_new(const.COOKIE_FILE, False)
43 if soup_cookie < 0:
44 raise Exception("Incorrect cookie value: %s" % (soup_cookie))
45 libgobject.g_object_set(session, 'add-feature', soup_cookie, None)
45 46
46 def init_proxy(self): 47 def init_proxy(self):
47 if self.config.proxy_enable == 'yes': 48 if self.config.proxy_enable == 'yes':
48 session = libwebkit.webkit_get_default_session() 49 session = libwebkit.webkit_get_default_session()
49 libgobject.g_object_set(session, 'proxy-uri', self.config.proxy_uri, None) 50 libgobject.g_object_set(session, 'proxy-uri', self.config.proxy_uri, None)
50 51
51 def init_signals(self): 52 def init_signals(self):
52 self.connect('mime-type-policy-decision-requested', self.policy_decision_requested) 53 self.connect('mime-type-policy-decision-requested', self.policy_decision_requested)
53 self.connect('download-requested', self.download_requested) 54 self.connect('download-requested', self.download_requested)
54 self.connect("create-web-view", self.create_webView) 55 self.connect("create-web-view", self.create_webView)
55 self.connect("hovering-over-link", self.hovering_over_ink) 56 self.connect("hovering-over-link", self.hovering_over_ink)
56 self.connect("navigation-policy-decision-requested", self.navigation_policy_decision_requested) 57 self.connect("navigation-policy-decision-requested", self.navigation_policy_decision_requested)
57 self.connect("load-finished", self.load_finished) 58 self.connect("load-finished", self.load_finished)
58 59
59 def load_finished(self, view, frame): 60 def load_finished(self, view, frame):
60 #print view.get_property('uri') + ':ok:' + frame.get_property('uri') 61 #print view.get_property('uri') + ':ok:' + frame.get_property('uri')
61 frame_uri = frame.get_property('uri') 62 frame_uri = frame.get_property('uri')
62 if self.config.login_auto_run == 'yes' and const.URL == frame_uri: 63 if self.config.login_auto_run == 'yes' and const.URL == frame_uri:
63 self.execute_script('alloy.portal.runApp(50);') 64 self.execute_script('alloy.portal.runApp(50);')
64 return 65 return
65 66
66 if utils.is_qq_login(frame_uri): 67 if utils.is_qq_login(frame_uri):
67 self.execute_script("document.getElementById('ifram_login').contentWindow.document.getElementById('p').value='" + self.config.login_password + "';") 68 self.execute_script("document.getElementById('ifram_login').contentWindow.document.getElementById('p').value='" + self.config.login_password + "';")
68 self.execute_script("document.getElementById('ifram_login').contentWindow.onStateItemClick(" + self.config.login_status + ");") 69 self.execute_script("document.getElementById('ifram_login').contentWindow.onStateItemClick(" + self.config.login_status + ");")
69 #if self.login_password != '': 70 #if self.login_password != '':
70 # self.execute_script("document.getElementById('ifram_login').contentWindow.document.getElementById('loginform').submit();") 71 # self.execute_script("document.getElementById('ifram_login').contentWindow.document.getElementById('loginform').submit();")
71 72
72 def navigation_policy_decision_requested(self, view, frame, request, aciton, decision): 73 def navigation_policy_decision_requested(self, view, frame, request, aciton, decision):
73 if utils.is_qq_download(request.get_uri()): 74 if utils.is_qq_download(request.get_uri()):
74 decision.download() 75 decision.download()
75 return True 76 return True
76 return False 77 return False
77 78
78 def policy_decision_requested(self, view, frame, request, mimetype, decision): 79 def policy_decision_requested(self, view, frame, request, mimetype, decision):
79 if self.can_show_mime_type(mimetype): 80 if self.can_show_mime_type(mimetype):
80 return False 81 return False
81 decision.download() 82 decision.download()
82 return True 83 return True
83 84
84 def download_requested(self, view, download): 85 def download_requested(self, view, download):
85 download.connect('notify::status', self.download_status) 86 download.connect('notify::status', self.download_status)
86 download.set_destination_uri('file://' + self.config.save_path + '/' + download.get_suggested_filename()) 87 download.set_destination_uri('file://' + self.config.save_path + '/' + download.get_suggested_filename())
87 return True 88 return True
88 89
89 def download_status(self, download, pspec): 90 def download_status(self, download, pspec):
90 if download.get_status() == -1: 91 if download.get_status() == -1:
91 utils.notification("文件下载失败", self.config.save_path + '/' + download.get_suggested_filename()) 92 utils.notification("文件下载失败", self.config.save_path + '/' + download.get_suggested_filename())
92 if download.get_status() == 1: 93 if download.get_status() == 1:
93 utils.notification("文件开始下载", self.config.save_path + '/' + download.get_suggested_filename()) 94 utils.notification("文件开始下载", self.config.save_path + '/' + download.get_suggested_filename())
94 if download.get_status() == 3: 95 if download.get_status() == 3:
95 utils.notification("文件下载完成", self.config.save_path + '/' + download.get_suggested_filename()) 96 utils.notification("文件下载完成", self.config.save_path + '/' + download.get_suggested_filename())
96 97
97 def create_webView(self, view, frame): 98 def create_webView(self, view, frame):
98 if self.hovered_uri: 99 if self.hovered_uri:
99 webbrowser.open_new_tab(self.hovered_uri) 100 webbrowser.open_new_tab(self.hovered_uri)
100 101
101 def hovering_over_ink(self, view, title, uri): 102 def hovering_over_ink(self, view, title, uri):
102 self.hovered_uri = uri 103 self.hovered_uri = uri
Powered by Google Project Hosting