Don't overwrite stored layout of main window with incorrect one
[qBittorrent.git] / src / gui / cookiesdialog.cpp
blob52d0dc860e94edde36555b477984c821f2d99b76
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 "cookiesdialog.h"
31 #include <algorithm>
33 #include "base/global.h"
34 #include "base/net/downloadmanager.h"
35 #include "cookiesmodel.h"
36 #include "ui_cookiesdialog.h"
37 #include "uithememanager.h"
38 #include "utils.h"
40 #define SETTINGS_KEY(name) u"CookiesDialog/" name
42 CookiesDialog::CookiesDialog(QWidget *parent)
43 : QDialog(parent)
44 , m_ui(new Ui::CookiesDialog)
45 , m_cookiesModel(new CookiesModel(Net::DownloadManager::instance()->allCookies(), this))
46 , m_storeDialogSize(SETTINGS_KEY(u"Size"_s))
47 , m_storeViewState("GUI/Qt6/" SETTINGS_KEY(u"ViewState"_s))
49 m_ui->setupUi(this);
51 connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
52 connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
54 setWindowIcon(UIThemeManager::instance()->getIcon(u"browser-cookies"_s));
56 m_ui->buttonAdd->setIcon(UIThemeManager::instance()->getIcon(u"list-add"_s));
57 m_ui->buttonAdd->setIconSize(Utils::Gui::mediumIconSize());
58 connect(m_ui->buttonAdd, &QToolButton::clicked, this, &CookiesDialog::onButtonAddClicked);
60 m_ui->buttonDelete->setIcon(UIThemeManager::instance()->getIcon(u"list-remove"_s));
61 m_ui->buttonDelete->setIconSize(Utils::Gui::mediumIconSize());
62 connect(m_ui->buttonDelete, &QToolButton::clicked, this, &CookiesDialog::onButtonDeleteClicked);
64 m_ui->treeView->setModel(m_cookiesModel);
65 if (m_cookiesModel->rowCount() > 0)
66 m_ui->treeView->selectionModel()->setCurrentIndex(
67 m_cookiesModel->index(0, 0),
68 QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
70 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
71 resize(dialogSize);
73 m_ui->treeView->header()->restoreState(m_storeViewState);
76 CookiesDialog::~CookiesDialog()
78 m_storeDialogSize = size();
79 m_storeViewState = m_ui->treeView->header()->saveState();
80 delete m_ui;
83 void CookiesDialog::accept()
85 Net::DownloadManager::instance()->setAllCookies(m_cookiesModel->cookies());
86 QDialog::accept();
89 void CookiesDialog::onButtonAddClicked()
91 int row = m_ui->treeView->selectionModel()->currentIndex().row() + 1;
93 m_cookiesModel->insertRow(row);
94 m_ui->treeView->selectionModel()->setCurrentIndex(
95 m_cookiesModel->index(row, 0), QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
98 void CookiesDialog::onButtonDeleteClicked()
100 QModelIndexList idxs = m_ui->treeView->selectionModel()->selectedRows();
102 // sort in descending order
103 std::sort(idxs.begin(), idxs.end(),
104 [](const QModelIndex &l, const QModelIndex &r)
106 return (l.row() > r.row());
110 for (const QModelIndex &idx : asConst(idxs))
111 m_cookiesModel->removeRow(idx.row());