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 "cookiesdialog.h"
33 #include "base/global.h"
34 #include "base/net/downloadmanager.h"
35 #include "cookiesmodel.h"
36 #include "ui_cookiesdialog.h"
37 #include "uithememanager.h"
40 #define SETTINGS_KEY(name) u"CookiesDialog/" name
42 CookiesDialog::CookiesDialog(QWidget
*parent
)
44 , m_ui(new Ui::CookiesDialog
)
45 , m_cookiesModel(new CookiesModel(Net::DownloadManager::instance()->allCookies(), this))
46 , m_storeDialogSize(SETTINGS_KEY(u
"Size"_qs
))
47 #if (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0))
48 , m_storeViewState("GUI/Qt6/" SETTINGS_KEY(u
"ViewState"_qs
))
50 , m_storeViewState(SETTINGS_KEY(u
"CookiesViewState"_qs
))
55 setWindowIcon(UIThemeManager::instance()->getIcon(u
"browser-cookies"_qs
));
56 m_ui
->buttonAdd
->setIcon(UIThemeManager::instance()->getIcon(u
"list-add"_qs
));
57 m_ui
->buttonDelete
->setIcon(UIThemeManager::instance()->getIcon(u
"list-remove"_qs
));
58 m_ui
->buttonAdd
->setIconSize(Utils::Gui::mediumIconSize());
59 m_ui
->buttonDelete
->setIconSize(Utils::Gui::mediumIconSize());
61 m_ui
->treeView
->setModel(m_cookiesModel
);
62 if (m_cookiesModel
->rowCount() > 0)
63 m_ui
->treeView
->selectionModel()->setCurrentIndex(
64 m_cookiesModel
->index(0, 0),
65 QItemSelectionModel::ClearAndSelect
| QItemSelectionModel::Rows
);
67 if (const QSize dialogSize
= m_storeDialogSize
; dialogSize
.isValid())
70 m_ui
->treeView
->header()->restoreState(m_storeViewState
);
73 CookiesDialog::~CookiesDialog()
75 m_storeDialogSize
= size();
76 m_storeViewState
= m_ui
->treeView
->header()->saveState();
80 void CookiesDialog::accept()
82 Net::DownloadManager::instance()->setAllCookies(m_cookiesModel
->cookies());
86 void CookiesDialog::onButtonAddClicked()
88 int row
= m_ui
->treeView
->selectionModel()->currentIndex().row() + 1;
90 m_cookiesModel
->insertRow(row
);
91 m_ui
->treeView
->selectionModel()->setCurrentIndex(
92 m_cookiesModel
->index(row
, 0), QItemSelectionModel::ClearAndSelect
| QItemSelectionModel::Rows
);
95 void CookiesDialog::onButtonDeleteClicked()
97 QModelIndexList idxs
= m_ui
->treeView
->selectionModel()->selectedRows();
99 // sort in descending order
100 std::sort(idxs
.begin(), idxs
.end(),
101 [](const QModelIndex
&l
, const QModelIndex
&r
)
103 return (l
.row() > r
.row());
107 for (const QModelIndex
&idx
: asConst(idxs
))
108 m_cookiesModel
->removeRow(idx
.row());