1 /*
2     Copyright © 2014-2019 by The qTox Project Contributors
3 
4     This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6     qTox is libre software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     qTox is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with qTox.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "privacyform.h"
21 #include "ui_privacysettings.h"
22 
23 #include <QDebug>
24 #include <QFile>
25 #include <QMessageBox>
26 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
27 #include <QRandomGenerator>
28 #endif
29 
30 #include "src/core/core.h"
31 #include "src/nexus.h"
32 #include "src/persistence/history.h"
33 #include "src/persistence/profile.h"
34 #include "src/persistence/settings.h"
35 #include "src/widget/form/setpassworddialog.h"
36 #include "src/widget/form/settingswidget.h"
37 #include "src/widget/gui.h"
38 #include "src/widget/tool/recursivesignalblocker.h"
39 #include "src/widget/translator.h"
40 #include "src/widget/widget.h"
41 
PrivacyForm()42 PrivacyForm::PrivacyForm()
43     : GenericForm(QPixmap(":/img/settings/privacy.png"))
44     , bodyUI(new Ui::PrivacySettings)
45 {
46     bodyUI->setupUi(this);
47 
48     // block all child signals during initialization
49     const RecursiveSignalBlocker signalBlocker(this);
50 
51     eventsInit();
52     Translator::registerHandler(std::bind(&PrivacyForm::retranslateUi, this), this);
53 }
54 
~PrivacyForm()55 PrivacyForm::~PrivacyForm()
56 {
57     Translator::unregister(this);
58     delete bodyUI;
59 }
60 
on_cbKeepHistory_stateChanged()61 void PrivacyForm::on_cbKeepHistory_stateChanged()
62 {
63     Settings::getInstance().setEnableLogging(bodyUI->cbKeepHistory->isChecked());
64     if (!bodyUI->cbKeepHistory->isChecked()) {
65         emit clearAllReceipts();
66         QMessageBox::StandardButton dialogDelHistory;
67         dialogDelHistory =
68             QMessageBox::question(nullptr, tr("Confirmation"),
69                                   tr("Do you want to permanently delete all chat history?"),
70                                   QMessageBox::Yes | QMessageBox::No);
71         if (dialogDelHistory == QMessageBox::Yes) {
72             Nexus::getProfile()->getHistory()->eraseHistory();
73         }
74     }
75 }
76 
on_cbTypingNotification_stateChanged()77 void PrivacyForm::on_cbTypingNotification_stateChanged()
78 {
79     Settings::getInstance().setTypingNotification(bodyUI->cbTypingNotification->isChecked());
80 }
81 
on_nospamLineEdit_editingFinished()82 void PrivacyForm::on_nospamLineEdit_editingFinished()
83 {
84     QString newNospam = bodyUI->nospamLineEdit->text();
85 
86     bool ok;
87     uint32_t nospam = newNospam.toLongLong(&ok, 16);
88     if (ok)
89         Core::getInstance()->setNospam(nospam);
90 }
91 
showEvent(QShowEvent *)92 void PrivacyForm::showEvent(QShowEvent*)
93 {
94     const Settings& s = Settings::getInstance();
95     bodyUI->nospamLineEdit->setText(Core::getInstance()->getSelfId().getNoSpamString());
96     bodyUI->cbTypingNotification->setChecked(s.getTypingNotification());
97     bodyUI->cbKeepHistory->setChecked(Settings::getInstance().getEnableLogging());
98     bodyUI->blackListTextEdit->setText(s.getBlackList().join('\n'));
99 }
100 
on_randomNosapamButton_clicked()101 void PrivacyForm::on_randomNosapamButton_clicked()
102 {
103     QTime time = QTime::currentTime();
104 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
105     QRandomGenerator((uint)time.msec());
106 #else
107     qsrand((uint)time.msec());
108 #endif
109 
110     uint32_t newNospam{0};
111     for (int i = 0; i < 4; ++i)
112 #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
113         newNospam = (newNospam << 8) + (QRandomGenerator::global()->generate() % 256); // Generate byte by byte. For some reason.
114 #else
115         newNospam = (newNospam << 8) + (qrand() % 256); // Generate byte by byte. For some reason.
116 #endif
117 
118     Core::getInstance()->setNospam(newNospam);
119     bodyUI->nospamLineEdit->setText(Core::getInstance()->getSelfId().getNoSpamString());
120 }
121 
on_nospamLineEdit_textChanged()122 void PrivacyForm::on_nospamLineEdit_textChanged()
123 {
124     QString str = bodyUI->nospamLineEdit->text();
125     int curs = bodyUI->nospamLineEdit->cursorPosition();
126     if (str.length() != 8) {
127         str = QString("00000000").replace(0, str.length(), str);
128         bodyUI->nospamLineEdit->setText(str);
129         bodyUI->nospamLineEdit->setCursorPosition(curs);
130     };
131 }
132 
on_blackListTextEdit_textChanged()133 void PrivacyForm::on_blackListTextEdit_textChanged()
134 {
135     const QStringList strlist = bodyUI->blackListTextEdit->toPlainText().split('\n');
136     Settings::getInstance().setBlackList(strlist);
137 }
138 
retranslateUi()139 void PrivacyForm::retranslateUi()
140 {
141     bodyUI->retranslateUi(this);
142 }
143