2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Vladimir Golovnev <glassez@yandex.ru>
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.
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.
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.
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.
29 #include "cookiesmodel.h"
33 CookiesModel::CookiesModel(const QList
<QNetworkCookie
> &cookies
, QObject
*parent
)
34 : QAbstractItemModel(parent
)
39 QList
<QNetworkCookie
> CookiesModel::cookies() const
44 QVariant
CookiesModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
46 if ((role
== Qt::DisplayRole
) && (orientation
== Qt::Horizontal
))
59 return tr("Expiration Date");
66 QModelIndex
CookiesModel::index(int row
, int column
, const QModelIndex
&parent
) const
68 if (parent
.isValid() // no items with valid parent
69 || (row
< 0) || (row
>= m_cookies
.size())
70 || (column
< 0) || (column
>= NB_COLUMNS
))
73 return createIndex(row
, column
, &m_cookies
[row
]);
76 QModelIndex
CookiesModel::parent(const QModelIndex
&index
) const
82 int CookiesModel::rowCount(const QModelIndex
&parent
) const
84 if (parent
.isValid()) return 0;
86 return m_cookies
.size();
89 int CookiesModel::columnCount(const QModelIndex
&parent
) const
95 QVariant
CookiesModel::data(const QModelIndex
&index
, int role
) const
97 if (!index
.isValid() || (index
.row() >= m_cookies
.size())
98 || ((role
!= Qt::DisplayRole
) && (role
!= Qt::EditRole
)))
101 switch (index
.column())
104 return m_cookies
[index
.row()].domain();
106 return m_cookies
[index
.row()].path();
108 return QString::fromLatin1(m_cookies
[index
.row()].name());
110 return QString::fromLatin1(m_cookies
[index
.row()].value());
112 return m_cookies
[index
.row()].expirationDate();
118 bool CookiesModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
120 if (role
!= Qt::EditRole
) return false;
122 switch (index
.column())
125 m_cookies
[index
.row()].setDomain(value
.toString());
128 m_cookies
[index
.row()].setPath(value
.toString());
131 m_cookies
[index
.row()].setName(value
.toString().toLatin1());
134 m_cookies
[index
.row()].setValue(value
.toString().toLatin1());
137 m_cookies
[index
.row()].setExpirationDate(value
.toDateTime());
143 emit
dataChanged(index
, index
);
147 bool CookiesModel::insertRows(int row
, int count
, const QModelIndex
&parent
)
149 if ((row
< 0) || (row
> m_cookies
.size())) return false;
151 QNetworkCookie newCookie
;
152 newCookie
.setExpirationDate(QDateTime::currentDateTime().addYears(2));
154 beginInsertRows(parent
, row
, row
+ count
- 1);
156 m_cookies
.insert(row
, newCookie
);
162 bool CookiesModel::removeRows(int row
, int count
, const QModelIndex
&parent
)
164 if ((m_cookies
.isEmpty())
165 || (row
>= m_cookies
.size())
166 || ((row
+ count
) > m_cookies
.size()))
169 beginRemoveRows(parent
, row
, row
+ count
- 1);
171 m_cookies
.removeAt(row
);
177 Qt::ItemFlags
CookiesModel::flags(const QModelIndex
&index
) const
179 if (!index
.isValid()) return Qt::NoItemFlags
;
181 return Qt::ItemIsEditable
| QAbstractItemModel::flags(index
);