Add an (untested) connection widget for connections
This commit is contained in:
parent
8b9e31d8e4
commit
46dd3d9688
2 changed files with 192 additions and 7 deletions
|
@ -21,15 +21,185 @@
|
|||
*/
|
||||
|
||||
#include "danbooruconnectwidget.h"
|
||||
#include "libdanbooru/danbooruservice.h"
|
||||
|
||||
#include <QCryptographicHash>
|
||||
|
||||
#include <KWallet/Wallet>
|
||||
|
||||
|
||||
Danbooru::DanbooruConnectWidget::DanbooruConnectWidget(QVector< KUrl > urlList, QWidget* parent): QWidget(parent)
|
||||
using KWallet::Wallet;
|
||||
|
||||
namespace Danbooru {
|
||||
|
||||
const QMap< KUrl, QString > initBoardSalts()
|
||||
{
|
||||
|
||||
QMap< KUrl, QString > boardSalts;
|
||||
|
||||
boardSalts.insert(KUrl("http://konachan.com"),
|
||||
QString("So-I-Heard-You-Like-Mupkids-?--%1--"));
|
||||
boardSalts.insert(KUrl("http://konachan.net"),
|
||||
QString("So-I-Heard-You-Like-Mupkids-?--%1--"));
|
||||
boardSalts.insert(KUrl("http://yande.re"),
|
||||
QString("choujin-steiner--%1--"));
|
||||
boardSalts.insert(KUrl("http://danbooru.donmai.us"),
|
||||
QString("choujin-steiner--%1--"));
|
||||
|
||||
return boardSalts;
|
||||
}
|
||||
|
||||
const QMap<KUrl, QString> DanbooruConnectWidget::boardSalts = initBoardSalts();
|
||||
|
||||
DanbooruConnectWidget::DanbooruConnectWidget(QVector< KUrl > urlList,
|
||||
QWidget* parent):
|
||||
QWidget(parent),
|
||||
m_wallet(0)
|
||||
|
||||
{
|
||||
setupUi(this);
|
||||
|
||||
danbooruUrlComboBox->setFocus();
|
||||
closeButton->setIcon(KIcon(QLatin1String("dialog-close")));
|
||||
closeButton->setToolTip(i18n("Close dialog and discard changes"));
|
||||
userLineEdit->setClearButtonShown(true);
|
||||
passwdLineEdit->setClearButtonShown(true);
|
||||
|
||||
if (anonCheckBox->isChecked()) {
|
||||
userLineEdit->setEnabled(false);
|
||||
passwdLineEdit->setEnabled(false);
|
||||
}
|
||||
|
||||
WId winid = this->parentWidget()->winId();
|
||||
|
||||
danbooruUrlComboBox->clear();
|
||||
|
||||
for (auto item: urlList) {
|
||||
danbooruUrlComboBox->insertUrl(urlList.indexOf(item), item);
|
||||
}
|
||||
|
||||
m_wallet = Wallet::openWallet(Wallet::NetworkWallet(), winid,
|
||||
Wallet::Asynchronous
|
||||
);
|
||||
|
||||
connect(m_wallet, SIGNAL(walletOpened(bool)), this,
|
||||
SLOT(checkWallet(bool)));
|
||||
|
||||
connect(danbooruUrlComboBox, SIGNAL(currentIndexChanged(int)), this,
|
||||
SLOT(getWalletData()));
|
||||
|
||||
connect(anonCheckBox, SIGNAL(stateChanged(int)), this,
|
||||
SLOT(toggleLineEdits()));
|
||||
|
||||
connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
|
||||
connect(closeButton, SIGNAL(clicked(bool)), this,
|
||||
SLOT(emitRejected()));
|
||||
|
||||
}
|
||||
|
||||
Danbooru::DanbooruConnectWidget::~DanbooruConnectWidget()
|
||||
DanbooruConnectWidget::~DanbooruConnectWidget()
|
||||
{
|
||||
|
||||
delete m_wallet;
|
||||
}
|
||||
|
||||
void DanbooruConnectWidget::checkWallet(bool result)
|
||||
{
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!m_wallet->hasFolder(Wallet::PasswordFolder())) {
|
||||
m_wallet->createFolder(Wallet::PasswordFolder());
|
||||
}
|
||||
|
||||
getWalletData();
|
||||
}
|
||||
|
||||
void DanbooruConnectWidget::getWalletData()
|
||||
{
|
||||
if (!m_wallet) {
|
||||
return;
|
||||
}
|
||||
|
||||
QMap<QString, QString> valueMap;
|
||||
QString key = danbooruUrlComboBox->currentText();
|
||||
|
||||
if (m_wallet->hasEntry(key)) {
|
||||
|
||||
|
||||
if (m_wallet->readMap(key, valueMap) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_username = valueMap[QLatin1String("username")];
|
||||
m_password = valueMap[QLatin1String("password")];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DanbooruConnectWidget::toggleLineEdits(int state)
|
||||
{
|
||||
if (state == Qt::Unchecked) {
|
||||
userLineEdit->setEnabled(true);
|
||||
passwdLineEdit->setEnabled(true);
|
||||
} else if (state == Qt::Checked) {
|
||||
userLineEdit->setEnabled(false);
|
||||
passwdLineEdit->setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void DanbooruConnectWidget::emitRejected()
|
||||
{
|
||||
Q_EMIT rejected();
|
||||
}
|
||||
|
||||
KUrl DanbooruConnectWidget::boardUrl() const
|
||||
{
|
||||
return m_boardUrl;
|
||||
}
|
||||
|
||||
QString DanbooruConnectWidget::username() const
|
||||
{
|
||||
return m_username;
|
||||
}
|
||||
|
||||
|
||||
QString DanbooruConnectWidget::password() const
|
||||
{
|
||||
return m_password;
|
||||
}
|
||||
|
||||
void DanbooruConnectWidget::accept()
|
||||
{
|
||||
if (m_boardUrl.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
QString hashedPassword;
|
||||
|
||||
if (!m_username.isEmpty() && !m_password.isEmpty()) {
|
||||
|
||||
if (m_wallet && !m_wallet->hasEntry(m_boardUrl.url())) {
|
||||
QMap<QString,QString> dataMap;
|
||||
dataMap.insert(QLatin1String("username"), m_username);
|
||||
dataMap.insert(QLatin1String("password"), m_password);
|
||||
m_wallet->writeMap(m_boardUrl.url(), dataMap);
|
||||
}
|
||||
|
||||
hashedPassword = boardSalts.value(m_boardUrl).arg(m_password);
|
||||
hashedPassword = QCryptographicHash::hash(hashedPassword.toUtf8(),
|
||||
QCryptographicHash::Sha1
|
||||
).toHex();
|
||||
}
|
||||
|
||||
DanbooruService* service = new DanbooruService(m_boardUrl, m_username,
|
||||
hashedPassword);
|
||||
Q_EMIT(connectionEstablished(service));
|
||||
|
||||
hide();
|
||||
}
|
||||
|
||||
|
||||
}; // namespace Danbooru
|
||||
|
|
|
@ -28,8 +28,16 @@
|
|||
|
||||
#include <KUrl>
|
||||
|
||||
namespace KWallet {
|
||||
class Wallet;
|
||||
}
|
||||
|
||||
namespace Danbooru {
|
||||
|
||||
class DanbooruService;
|
||||
|
||||
const QMap< KUrl, QString > initBoardSalts();
|
||||
|
||||
class DanbooruConnectWidget: public QWidget, public Ui::DanbooruConnectWidget {
|
||||
Q_OBJECT
|
||||
|
||||
|
@ -40,19 +48,26 @@ namespace Danbooru {
|
|||
|
||||
QString username() const;
|
||||
QString password() const;
|
||||
KUrl boardUrl() const;
|
||||
|
||||
private:
|
||||
KUrl m_Boardurl;
|
||||
KUrl m_boardUrl;
|
||||
QString m_username;
|
||||
QString m_password;
|
||||
KWallet::Wallet* m_wallet;
|
||||
static const QMap<KUrl, QString> boardSalts;
|
||||
|
||||
Q_SIGNALS:
|
||||
void connectionEstablished();
|
||||
void connectionEstablished(DanbooruService* service);
|
||||
void rejected();
|
||||
|
||||
|
||||
|
||||
private Q_SLOTS:
|
||||
void checkWallet(bool);
|
||||
void getWalletData();
|
||||
void toggleLineEdits(int state);
|
||||
void emitRejected();
|
||||
void accept();
|
||||
};
|
||||
|
||||
|
||||
} // namespace Danbooru
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue