1 /* ============================================================
2 * QuiteRSS is a open-source cross-platform RSS/Atom news feeds reader
3 * Copyright (C) 2011-2020 QuiteRSS Team <quiterssteam@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #include "cookiejar.h"
19 
20 #include "mainapplication.h"
21 #include "settings.h"
22 
23 #include <QNetworkCookie>
24 #include <QDateTime>
25 #include <QDir>
26 
CookieJar(QObject * parent)27 CookieJar::CookieJar(QObject *parent)
28   : QNetworkCookieJar(parent)
29 {
30   loadCookies();
31 }
32 
33 /** @brief Load cookies from file
34  *----------------------------------------------------------------------------*/
loadCookies()35 void CookieJar::loadCookies()
36 {
37   Settings settings;
38   useCookies_ = (UseCookies)settings.value("Settings/saveCookies", SaveCookies).toInt();
39 
40   if (useCookies_ != SaveCookies) return;
41 
42   if (!QFile::exists(mainApp->dataDir() + "/cookies.dat")) {
43     return;
44   }
45 
46   QDateTime now = QDateTime::currentDateTime();
47 
48   QList<QNetworkCookie> loadedCookies;
49   QFile file(mainApp->dataDir() + "/cookies.dat");
50   file.open(QIODevice::ReadOnly);
51   QDataStream stream(&file);
52   int count;
53 
54   stream >> count;
55   for (int i = 0; i < count; i++) {
56     QByteArray rawForm;
57     stream >> rawForm;
58     const QList<QNetworkCookie> &cookieList = QNetworkCookie::parseCookies(rawForm);
59     if (cookieList.isEmpty()) {
60       continue;
61     }
62 
63     const QNetworkCookie &cookie = cookieList.at(0);
64     if (cookie.expirationDate() < now) {
65       continue;
66     }
67     loadedCookies.append(cookie);
68   }
69 
70   file.close();
71   setAllCookies(loadedCookies);
72 }
73 
74 /** @brief Save cookies to file
75  *----------------------------------------------------------------------------*/
saveCookies()76 void CookieJar::saveCookies()
77 {
78   Settings settings;
79   settings.setValue("Settings/saveCookies", useCookies_);
80 
81   if (useCookies_ != SaveCookies) return;
82 
83   QList<QNetworkCookie> allCookies = getAllCookies();
84 
85   QFile file(mainApp->dataDir() + "/cookies.dat");
86   file.open(QIODevice::WriteOnly);
87   QDataStream stream(&file);
88   int count = allCookies.count();
89 
90   stream << count;
91   for (int i = 0; i < count; i++) {
92     const QNetworkCookie &cookie = allCookies.at(i);
93 
94     if (cookie.isSessionCookie()) {
95       continue;
96     }
97     stream << cookie.toRawForm();
98   }
99 
100   file.close();
101 }
102 
setCookiesFromUrl(const QList<QNetworkCookie> & cookieList,const QUrl & url)103 bool CookieJar::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
104 {
105   if (useCookies_ == BlockCookies)
106     return false;
107   return QNetworkCookieJar::setCookiesFromUrl(cookieList, url);
108 }
109 
110 /** @brief Clear all cookies
111  *----------------------------------------------------------------------------*/
clearCookies()112 void CookieJar::clearCookies()
113 {
114   setAllCookies(QList<QNetworkCookie>());
115 }
116 
117 /** @brief Retrive all cookies
118  *----------------------------------------------------------------------------*/
getAllCookies()119 QList<QNetworkCookie> CookieJar::getAllCookies()
120 {
121   return QNetworkCookieJar::allCookies();
122 }
123 
124 /** @brief Setup cookies from list
125  *----------------------------------------------------------------------------*/
setAllCookies(const QList<QNetworkCookie> & cookieList)126 void CookieJar::setAllCookies(const QList<QNetworkCookie> &cookieList)
127 {
128   QNetworkCookieJar::setAllCookies(cookieList);
129 }
130 
useCookies() const131 UseCookies CookieJar::useCookies() const
132 {
133   return useCookies_;
134 }
135 
setUseCookies(UseCookies value)136 void CookieJar::setUseCookies(UseCookies value)
137 {
138   useCookies_ = value;
139 }
140