1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2016  Vladimir Golovnev <glassez@yandex.ru>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL".  If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #include "cookiesmodel.h"
30 
31 #include <QDateTime>
32 
CookiesModel(const QList<QNetworkCookie> & cookies,QObject * parent)33 CookiesModel::CookiesModel(const QList<QNetworkCookie> &cookies, QObject *parent)
34     : QAbstractItemModel(parent)
35     , m_cookies(cookies)
36 {
37 }
38 
cookies() const39 QList<QNetworkCookie> CookiesModel::cookies() const
40 {
41     return m_cookies;
42 }
43 
headerData(int section,Qt::Orientation orientation,int role) const44 QVariant CookiesModel::headerData(int section, Qt::Orientation orientation, int role) const
45 {
46     if ((role == Qt::DisplayRole) && (orientation == Qt::Horizontal))
47     {
48         switch (section)
49         {
50         case COL_DOMAIN:
51             return tr("Domain");
52         case COL_PATH:
53             return tr("Path");
54         case COL_NAME:
55             return tr("Name");
56         case COL_VALUE:
57             return tr("Value");
58         case COL_EXPDATE:
59             return tr("Expiration Date");
60         }
61     }
62 
63     return {};
64 }
65 
index(int row,int column,const QModelIndex & parent) const66 QModelIndex CookiesModel::index(int row, int column, const QModelIndex &parent) const
67 {
68     if (parent.isValid() // no items with valid parent
69         || (row < 0) || (row >= m_cookies.size())
70         || (column < 0) || (column >= NB_COLUMNS))
71         return {};
72 
73     return createIndex(row, column, &m_cookies[row]);
74 }
75 
parent(const QModelIndex & index) const76 QModelIndex CookiesModel::parent(const QModelIndex &index) const
77 {
78     Q_UNUSED(index);
79     return {};
80 }
81 
rowCount(const QModelIndex & parent) const82 int CookiesModel::rowCount(const QModelIndex &parent) const
83 {
84     if (parent.isValid()) return 0;
85 
86     return m_cookies.size();
87 }
88 
columnCount(const QModelIndex & parent) const89 int CookiesModel::columnCount(const QModelIndex &parent) const
90 {
91     Q_UNUSED(parent);
92     return NB_COLUMNS;
93 }
94 
data(const QModelIndex & index,int role) const95 QVariant CookiesModel::data(const QModelIndex &index, int role) const
96 {
97     if (!index.isValid() || (index.row() >= m_cookies.size())
98         || ((role != Qt::DisplayRole) && (role != Qt::EditRole)))
99         return {};
100 
101     switch (index.column())
102     {
103     case COL_DOMAIN:
104         return m_cookies[index.row()].domain();
105     case COL_PATH:
106         return m_cookies[index.row()].path();
107     case COL_NAME:
108         return QString::fromLatin1(m_cookies[index.row()].name());
109     case COL_VALUE:
110         return QString::fromLatin1(m_cookies[index.row()].value());
111     case COL_EXPDATE:
112         return m_cookies[index.row()].expirationDate();
113     }
114 
115     return {};
116 }
117 
setData(const QModelIndex & index,const QVariant & value,int role)118 bool CookiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
119 {
120     if (role != Qt::EditRole) return false;
121 
122     switch (index.column())
123     {
124     case COL_DOMAIN:
125         m_cookies[index.row()].setDomain(value.toString());
126         break;
127     case COL_PATH:
128         m_cookies[index.row()].setPath(value.toString());
129         break;
130     case COL_NAME:
131         m_cookies[index.row()].setName(value.toString().toLatin1());
132         break;
133     case COL_VALUE:
134         m_cookies[index.row()].setValue(value.toString().toLatin1());
135         break;
136     case COL_EXPDATE:
137         m_cookies[index.row()].setExpirationDate(value.toDateTime());
138         break;
139     default:
140         return false;
141     }
142 
143     emit dataChanged(index, index);
144     return true;
145 }
146 
insertRows(int row,int count,const QModelIndex & parent)147 bool CookiesModel::insertRows(int row, int count, const QModelIndex &parent)
148 {
149     if ((row < 0) || (row > m_cookies.size())) return false;
150 
151     QNetworkCookie newCookie;
152     newCookie.setExpirationDate(QDateTime::currentDateTime().addYears(2));
153 
154     beginInsertRows(parent, row, row + count - 1);
155     while (count-- > 0)
156         m_cookies.insert(row, newCookie);
157     endInsertRows();
158 
159     return true;
160 }
161 
removeRows(int row,int count,const QModelIndex & parent)162 bool CookiesModel::removeRows(int row, int count, const QModelIndex &parent)
163 {
164     if ((m_cookies.isEmpty())
165         || (row >= m_cookies.size())
166         || ((row + count) > m_cookies.size()))
167         return false;
168 
169     beginRemoveRows(parent, row, row + count - 1);
170     while (count-- > 0)
171         m_cookies.removeAt(row);
172     endRemoveRows();
173 
174     return true;
175 }
176 
flags(const QModelIndex & index) const177 Qt::ItemFlags CookiesModel::flags(const QModelIndex &index) const
178 {
179     if (!index.isValid()) return Qt::NoItemFlags;
180 
181     return Qt::ItemIsEditable | QAbstractItemModel::flags(index);
182 }
183