Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: 

 

# Copyright 2014-2018 Florian Bruhin (The Compiler) <mail@qutebrowser.org> 

# 

# This file is part of qutebrowser. 

# 

# qutebrowser is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# qutebrowser is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. 

 

"""Handling of HTTP cookies.""" 

 

from PyQt5.QtNetwork import QNetworkCookie, QNetworkCookieJar 

from PyQt5.QtCore import pyqtSignal, QDateTime 

 

from qutebrowser.config import config 

from qutebrowser.utils import utils, standarddir, objreg 

from qutebrowser.misc import lineparser 

 

 

class RAMCookieJar(QNetworkCookieJar): 

 

"""An in-RAM cookie jar. 

 

Signals: 

changed: Emitted when the cookie store was changed. 

""" 

 

changed = pyqtSignal() 

 

def __repr__(self): 

return utils.get_repr(self, count=len(self.allCookies())) 

 

def setCookiesFromUrl(self, cookies, url): 

"""Add the cookies in the cookies list to this cookie jar. 

 

Args: 

cookies: A list of QNetworkCookies. 

url: The URL to set the cookies for. 

 

Return: 

True if one or more cookies are set for 'url', otherwise False. 

""" 

if config.val.content.cookies.accept == 'never': 

return False 

else: 

self.changed.emit() 

return super().setCookiesFromUrl(cookies, url) 

 

 

class CookieJar(RAMCookieJar): 

 

"""A cookie jar saving cookies to disk. 

 

Attributes: 

_lineparser: The LineParser managing the cookies file. 

""" 

 

def __init__(self, parent=None, *, line_parser=None): 

super().__init__(parent) 

 

if line_parser: 

self._lineparser = line_parser 

else: 

self._lineparser = lineparser.LineParser( 

standarddir.data(), 'cookies', binary=True, parent=self) 

self.parse_cookies() 

config.instance.changed.connect(self._on_cookies_store_changed) 

objreg.get('save-manager').add_saveable( 

'cookies', self.save, self.changed, 

config_opt='content.cookies.store') 

 

def parse_cookies(self): 

"""Parse cookies from lineparser and store them.""" 

cookies = [] 

for line in self._lineparser: 

cookies += QNetworkCookie.parseCookies(line) 

self.setAllCookies(cookies) 

 

def purge_old_cookies(self): 

"""Purge expired cookies from the cookie jar.""" 

# Based on: 

# http://doc.qt.io/qt-5/qtwebkitexamples-webkitwidgets-browser-cookiejar-cpp.html 

now = QDateTime.currentDateTime() 

cookies = [c for c in self.allCookies() 

if c.isSessionCookie() or c.expirationDate() >= now] 

self.setAllCookies(cookies) 

 

def save(self): 

"""Save cookies to disk.""" 

self.purge_old_cookies() 

lines = [] 

for cookie in self.allCookies(): 

if not cookie.isSessionCookie(): 

lines.append(cookie.toRawForm()) 

self._lineparser.data = lines 

self._lineparser.save() 

 

@config.change_filter('content.cookies.store') 

def _on_cookies_store_changed(self): 

"""Delete stored cookies if cookies.store changed.""" 

if not config.val.content.cookies.store: 

self._lineparser.data = [] 

self._lineparser.save() 

self.changed.emit()