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([[maybe_unused
]] const QModelIndex
&index
) const
81 int CookiesModel::rowCount(const QModelIndex
&parent
) const
83 if (parent
.isValid()) return 0;
85 return m_cookies
.size();
88 int CookiesModel::columnCount([[maybe_unused
]] const QModelIndex
&parent
) const
93 QVariant
CookiesModel::data(const QModelIndex
&index
, int role
) const
95 if (!index
.isValid() || (index
.row() >= m_cookies
.size())
96 || ((role
!= Qt::DisplayRole
) && (role
!= Qt::EditRole
)))
99 switch (index
.column())
102 return m_cookies
[index
.row()].domain();
104 return m_cookies
[index
.row()].path();
106 return QString::fromLatin1(m_cookies
[index
.row()].name());
108 return QString::fromLatin1(m_cookies
[index
.row()].value());
110 return m_cookies
[index
.row()].expirationDate();
116 bool CookiesModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
118 if (role
!= Qt::EditRole
) return false;
120 switch (index
.column())
123 m_cookies
[index
.row()].setDomain(value
.toString());
126 m_cookies
[index
.row()].setPath(value
.toString());
129 m_cookies
[index
.row()].setName(value
.toString().toLatin1());
132 m_cookies
[index
.row()].setValue(value
.toString().toLatin1());
135 m_cookies
[index
.row()].setExpirationDate(value
.toDateTime());
141 emit
dataChanged(index
, index
);
145 bool CookiesModel::insertRows(int row
, int count
, const QModelIndex
&parent
)
147 if ((row
< 0) || (row
> m_cookies
.size())) return false;
149 QNetworkCookie newCookie
;
150 newCookie
.setExpirationDate(QDateTime::currentDateTime().addYears(2));
152 beginInsertRows(parent
, row
, row
+ count
- 1);
154 m_cookies
.insert(row
, newCookie
);
160 bool CookiesModel::removeRows(int row
, int count
, const QModelIndex
&parent
)
162 if ((m_cookies
.isEmpty())
163 || (row
>= m_cookies
.size())
164 || ((row
+ count
) > m_cookies
.size()))
167 beginRemoveRows(parent
, row
, row
+ count
- 1);
169 m_cookies
.removeAt(row
);
175 Qt::ItemFlags
CookiesModel::flags(const QModelIndex
&index
) const
177 if (!index
.isValid()) return Qt::NoItemFlags
;
179 return Qt::ItemIsEditable
| QAbstractItemModel::flags(index
);