Exemple #1
0
def load_cookies():
    '''unpickle the cookie jar'''
    jar = QNetworkCookieJar()
    try:
        with open(join(homedir(), COOKIE_FILE)) as file:           
            cookies = []
            for name, value in pickle.load(file):
                cookies.append(QNetworkCookie(name, value))
            jar.setCookiesFromUrl(cookies, QUrl(COOKIE_URL))
    except IOError:
        pass # no cookie file is okay
    return jar
    
Exemple #2
0
 def setCookiesFromUrl(self, cookieList, url):
     if self.__read_only:
         return True
     resp = QNetworkCookieJar.setCookiesFromUrl(self, cookieList, url)
     if resp:
         self.emit(SIGNAL('cookieJarUpdated()'))
     return resp
Exemple #3
0
    def set_cookies_from_url(self, cookies, url):
        """ Reimplementation from base class. Prevents cookies from being set
        if not from whitelisted domains.

        """
        if ".".join(unicode(url.host()).split('.')[-2:]) not in self.allowed:
            ret = []
        else:
            ret = cookies
        if ret:
            log(format_cookie(url, ret))
        return QNetworkCookieJar.setCookiesFromUrl(self, ret, url)
Exemple #4
0
    def set_cookies_from_url(self, cookies, url):
        """ Reimplementation from base class. Prevents cookies from being set
        if not from whitelisted domains.

        """
        if ".".join(unicode(url.host()).split('.')[-2:]) not in self.allowed:
            ret = []
        else:
            ret = cookies
        if ret:
            log(format_cookie(url, ret))
        return QNetworkCookieJar.setCookiesFromUrl(self, ret, url)
 def setCookiesFromUrl(self, cookieList, url):
     """
     Public method to set cookies for a URL.
     
     @param cookieList list of cookies to set (list of QNetworkCookie)
     @param url url to set cookies for (QUrl)
     @return flag indicating cookies were set (boolean)
     """
     if not self.__loaded:
         self.load()
     
     globalSettings = QWebSettings.globalSettings()
     if globalSettings.testAttribute(QWebSettings.PrivateBrowsingEnabled):
         return False
     
     host = url.host()
     eBlock = self.__isOnDomainList(self.__exceptionsBlock, host)
     eAllow = not eBlock and \
              self.__isOnDomainList(self.__exceptionsAllow, host)
     eAllowSession = not eBlock and \
                     not eAllow and \
                     self.__isOnDomainList(self.__exceptionsAllowForSession, host)
     
     addedCookies = False
     acceptInitially = self.__acceptCookies != self.AcceptNever
     if (acceptInitially and not eBlock) or \
        (not acceptInitially and (eAllow or eAllowSession)):
         # url domain == cookie domain
         soon = QDateTime.currentDateTime()
         soon = soon.addDays(90)
         for cookie in cookieList:
             lst = []
             if not (self.__filterTrackingCookies and \
                     cookie.name().startsWith("__utm")):
                 if eAllowSession:
                     cookie.setExpirationDate(QDateTime())
                 if self.__keepCookies == self.KeepUntilTimeLimit and \
                    not cookie.isSessionCookie and \
                    cookie.expirationDate() > soon:
                     cookie.setExpirationDate(soon)
                 lst.append(cookie)
                 if QNetworkCookieJar.setCookiesFromUrl(self, lst, url):
                     addedCookies = True
                 elif self.__acceptCookies == self.AcceptAlways:
                     # force it in if wanted
                     cookies = self.allCookies()
                     for ocookie in cookies[:]:
                         # does the cookie exist already?
                         if cookie.name() == ocookie.name() and \
                            cookie.domain() == ocookie.domain() and \
                            cookie.path() == ocookie.path():
                             # found a match
                             cookies.remove(ocookie)
                     
                     cookies.append(cookie)
                     self.setAllCookies(cookies)
                     addedCookies = True
     
     if addedCookies:
         self.__saveTimer.changeOccurred()
         self.emit(SIGNAL("cookiesChanged()"))
     
     return addedCookies