Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / gui / cookiesmodel.cpp
blob348561a0c7d973e44854b90a9738b8833ef2b025
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(const QModelIndex &index) const
78 Q_UNUSED(index);
79 return {};
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
91 Q_UNUSED(parent);
92 return NB_COLUMNS;
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)))
99 return {};
101 switch (index.column())
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();
115 return {};
118 bool CookiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
120 if (role != Qt::EditRole) return false;
122 switch (index.column())
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;
143 emit dataChanged(index, index);
144 return true;
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);
155 while (count-- > 0)
156 m_cookies.insert(row, newCookie);
157 endInsertRows();
159 return true;
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()))
167 return false;
169 beginRemoveRows(parent, row, row + count - 1);
170 while (count-- > 0)
171 m_cookies.removeAt(row);
172 endRemoveRows();
174 return true;
177 Qt::ItemFlags CookiesModel::flags(const QModelIndex &index) const
179 if (!index.isValid()) return Qt::NoItemFlags;
181 return Qt::ItemIsEditable | QAbstractItemModel::flags(index);