Enable customizing the save statistics time interval
[qBittorrent.git] / src / gui / cookiesmodel.cpp
bloba8d65864654901df65113a5660f8cf4e2a108af7
1 /*
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"
31 #include <QDateTime>
33 CookiesModel::CookiesModel(const QList<QNetworkCookie> &cookies, QObject *parent)
34 : QAbstractItemModel(parent)
35 , m_cookies(cookies)
39 QList<QNetworkCookie> CookiesModel::cookies() const
41 return m_cookies;
44 QVariant CookiesModel::headerData(int section, Qt::Orientation orientation, int role) const
46 if ((role == Qt::DisplayRole) && (orientation == Qt::Horizontal))
48 switch (section)
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");
63 return {};
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))
71 return {};
73 return createIndex(row, column, &m_cookies[row]);
76 QModelIndex CookiesModel::parent([[maybe_unused]] const QModelIndex &index) const
78 return {};
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
90 return NB_COLUMNS;
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)))
97 return {};
99 switch (index.column())
101 case COL_DOMAIN:
102 return m_cookies[index.row()].domain();
103 case COL_PATH:
104 return m_cookies[index.row()].path();
105 case COL_NAME:
106 return QString::fromLatin1(m_cookies[index.row()].name());
107 case COL_VALUE:
108 return QString::fromLatin1(m_cookies[index.row()].value());
109 case COL_EXPDATE:
110 return m_cookies[index.row()].expirationDate();
113 return {};
116 bool CookiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
118 if (role != Qt::EditRole) return false;
120 switch (index.column())
122 case COL_DOMAIN:
123 m_cookies[index.row()].setDomain(value.toString());
124 break;
125 case COL_PATH:
126 m_cookies[index.row()].setPath(value.toString());
127 break;
128 case COL_NAME:
129 m_cookies[index.row()].setName(value.toString().toLatin1());
130 break;
131 case COL_VALUE:
132 m_cookies[index.row()].setValue(value.toString().toLatin1());
133 break;
134 case COL_EXPDATE:
135 m_cookies[index.row()].setExpirationDate(value.toDateTime());
136 break;
137 default:
138 return false;
141 emit dataChanged(index, index);
142 return true;
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);
153 while (count-- > 0)
154 m_cookies.insert(row, newCookie);
155 endInsertRows();
157 return true;
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()))
165 return false;
167 beginRemoveRows(parent, row, row + count - 1);
168 while (count-- > 0)
169 m_cookies.removeAt(row);
170 endRemoveRows();
172 return true;
175 Qt::ItemFlags CookiesModel::flags(const QModelIndex &index) const
177 if (!index.isValid()) return Qt::NoItemFlags;
179 return Qt::ItemIsEditable | QAbstractItemModel::flags(index);