1
0
mirror of https://github.com/qTox/qTox.git synced 2024-03-22 14:00:36 +08:00

refactor(aboutuser): Use const Friend pointer and union methods

This commit is contained in:
Diadlo 2017-07-26 12:10:23 +03:00
parent 9fe503c708
commit 6ba24b65ca
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727
3 changed files with 34 additions and 42 deletions

View File

@ -8,7 +8,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QMessageBox> #include <QMessageBox>
AboutUser::AboutUser(ToxPk& toxId, QWidget* parent) AboutUser::AboutUser(const Friend* f, QWidget* parent)
: QDialog(parent) : QDialog(parent)
, ui(new Ui::AboutUser) , ui(new Ui::AboutUser)
{ {
@ -23,65 +23,58 @@ AboutUser::AboutUser(ToxPk& toxId, QWidget* parent)
connect(ui->selectSaveDir, &QPushButton::clicked, this, &AboutUser::onSelectDirClicked); connect(ui->selectSaveDir, &QPushButton::clicked, this, &AboutUser::onSelectDirClicked);
connect(ui->removeHistory, &QPushButton::clicked, this, &AboutUser::onRemoveHistoryClicked); connect(ui->removeHistory, &QPushButton::clicked, this, &AboutUser::onRemoveHistoryClicked);
this->friendPk = toxId; friendPk = f->getPublicKey();
Settings& s = Settings::getInstance(); Settings& s = Settings::getInstance();
QString dir = s.getAutoAcceptDir(this->friendPk); QString dir = s.getAutoAcceptDir(friendPk);
ui->autoacceptfile->setChecked(!dir.isEmpty()); ui->autoacceptfile->setChecked(!dir.isEmpty());
ui->autoacceptcall->setCurrentIndex(s.getAutoAcceptCall(this->friendPk)); ui->autoacceptcall->setCurrentIndex(s.getAutoAcceptCall(friendPk));
ui->selectSaveDir->setEnabled(ui->autoacceptfile->isChecked()); ui->selectSaveDir->setEnabled(ui->autoacceptfile->isChecked());
ui->autogroupinvite->setChecked(s.getAutoGroupInvite(this->friendPk)); ui->autogroupinvite->setChecked(s.getAutoGroupInvite(friendPk));
if (ui->autoacceptfile->isChecked()) { if (ui->autoacceptfile->isChecked()) {
ui->selectSaveDir->setText(s.getAutoAcceptDir(this->friendPk)); ui->selectSaveDir->setText(s.getAutoAcceptDir(friendPk));
} }
}
void AboutUser::setFriend(Friend* f) setWindowTitle(f->getDisplayedName());
{
this->setWindowTitle(f->getDisplayedName());
ui->userName->setText(f->getDisplayedName()); ui->userName->setText(f->getDisplayedName());
ui->publicKey->setText(QString(f->getPublicKey().toString())); ui->publicKey->setText(friendPk.toString());
ui->publicKey->setCursorPosition(0); // scroll textline to left ui->publicKey->setCursorPosition(0); // scroll textline to left
ui->note->setPlainText(Settings::getInstance().getContactNote(f->getPublicKey())); ui->note->setPlainText(Settings::getInstance().getContactNote(friendPk));
QPixmap avatar = Nexus::getProfile()->loadAvatar(f->getPublicKey().toString()); QPixmap avatar = Nexus::getProfile()->loadAvatar(friendPk.toString());
ui->statusMessage->setText(f->getStatusMessage()); ui->statusMessage->setText(f->getStatusMessage());
if (!avatar.isNull()) { ui->avatar->setPixmap(avatar.isNull() ? QPixmap(":/img/contact_dark.svg") : avatar);
ui->avatar->setPixmap(avatar);
} else {
ui->avatar->setPixmap(QPixmap(":/img/contact_dark.svg"));
}
} }
void AboutUser::onAutoAcceptDirClicked() void AboutUser::onAutoAcceptDirClicked()
{ {
QString dir;
if (!ui->autoacceptfile->isChecked()) { if (!ui->autoacceptfile->isChecked()) {
dir = QDir::homePath();
ui->autoacceptfile->setChecked(false); ui->autoacceptfile->setChecked(false);
Settings::getInstance().setAutoAcceptDir(this->friendPk, ""); Settings::getInstance().setAutoAcceptDir(friendPk, "");
ui->selectSaveDir->setText(tr("Auto accept for this contact is disabled")); ui->selectSaveDir->setText(tr("Auto accept for this contact is disabled"));
} else if (ui->autoacceptfile->isChecked()) { } else if (ui->autoacceptfile->isChecked()) {
dir = QFileDialog::getExistingDirectory(Q_NULLPTR, tr("Choose an auto accept directory", QString dir = QFileDialog::getExistingDirectory(
"popup title"), dir); Q_NULLPTR, tr("Choose an auto accept directory", "popup title"), dir);
if (dir.isEmpty()) { if (dir.isEmpty()) {
ui->autoacceptfile->setChecked(false); ui->autoacceptfile->setChecked(false);
return; // user canellced return; // user canellced
} }
Settings::getInstance().setAutoAcceptDir(this->friendPk, dir);
ui->selectSaveDir->setText(Settings::getInstance().getAutoAcceptDir(this->friendPk)); Settings::getInstance().setAutoAcceptDir(friendPk, dir);
ui->selectSaveDir->setText(Settings::getInstance().getAutoAcceptDir(friendPk));
} }
Settings::getInstance().saveGlobal(); Settings::getInstance().saveGlobal();
ui->selectSaveDir->setEnabled(ui->autoacceptfile->isChecked()); ui->selectSaveDir->setEnabled(ui->autoacceptfile->isChecked());
} }
void AboutUser::onAutoAcceptCallClicked() void AboutUser::onAutoAcceptCallClicked()
{ {
Settings::getInstance().setAutoAcceptCall(this->friendPk, QFlag flag = QFlag(ui->autoacceptcall->currentIndex());
Settings::AutoAcceptCallFlags( Settings::getInstance().setAutoAcceptCall(friendPk, Settings::AutoAcceptCallFlags(flag));
QFlag(ui->autoacceptcall->currentIndex())));
Settings::getInstance().savePersonal(); Settings::getInstance().savePersonal();
} }
@ -90,18 +83,17 @@ void AboutUser::onAutoAcceptCallClicked()
*/ */
void AboutUser::onAutoGroupInvite() void AboutUser::onAutoGroupInvite()
{ {
Settings::getInstance().setAutoGroupInvite(this->friendPk, ui->autogroupinvite->isChecked()); Settings::getInstance().setAutoGroupInvite(friendPk, ui->autogroupinvite->isChecked());
Settings::getInstance().savePersonal(); Settings::getInstance().savePersonal();
} }
void AboutUser::onSelectDirClicked() void AboutUser::onSelectDirClicked()
{ {
QString dir; QString dir = QFileDialog::getExistingDirectory(
dir = QFileDialog::getExistingDirectory(Q_NULLPTR, Q_NULLPTR, tr("Choose an auto accept directory", "popup title"), dir);
tr("Choose an auto accept directory", "popup title"),
dir);
ui->autoacceptfile->setChecked(true); ui->autoacceptfile->setChecked(true);
Settings::getInstance().setAutoAcceptDir(this->friendPk, dir); Settings::getInstance().setAutoAcceptDir(friendPk, dir);
Settings::getInstance().savePersonal(); Settings::getInstance().savePersonal();
} }
@ -117,11 +109,12 @@ void AboutUser::onAcceptedClicked()
void AboutUser::onRemoveHistoryClicked() void AboutUser::onRemoveHistoryClicked()
{ {
History* history = Nexus::getProfile()->getHistory(); History* history = Nexus::getProfile()->getHistory();
if (history) if (history) {
history->removeFriendHistory(friendPk.toString()); history->removeFriendHistory(friendPk.toString());
QMessageBox::information(this, tr("History removed"), }
tr("Chat history with %1 removed!").arg(ui->userName->text().toHtmlEscaped()),
QMessageBox::Ok); QMessageBox::information(this, tr("History removed"), tr("Chat history with %1 removed!")
.arg(ui->userName->text().toHtmlEscaped()), QMessageBox::Ok);
} }
AboutUser::~AboutUser() AboutUser::~AboutUser()

View File

@ -14,9 +14,8 @@ class AboutUser : public QDialog
Q_OBJECT Q_OBJECT
public: public:
explicit AboutUser(ToxPk& toxID, QWidget* parent = 0); explicit AboutUser(const Friend* f, QWidget* parent = 0);
~AboutUser(); ~AboutUser();
void setFriend(Friend* f);
private: private:
Ui::AboutUser* ui; Ui::AboutUser* ui;

View File

@ -232,8 +232,8 @@ void FriendWidget::onContextMenuCalled(QContextMenuEvent* event)
Settings::getInstance().setAutoAcceptDir(id, dir); Settings::getInstance().setAutoAcceptDir(id, dir);
} }
} else if (selectedItem == aboutWindow) { } else if (selectedItem == aboutWindow) {
AboutUser* aboutUser = new AboutUser(id, Widget::getInstance()); const Friend* f = FriendList::findFriend(friendId);
aboutUser->setFriend(FriendList::findFriend(friendId)); AboutUser* aboutUser = new AboutUser(f, Widget::getInstance());
aboutUser->show(); aboutUser->show();
} else if (selectedItem == newGroupAction) { } else if (selectedItem == newGroupAction) {
int groupId = Core::getInstance()->createGroup(); int groupId = Core::getInstance()->createGroup();