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

Store friend requests

This commit is contained in:
TheSpiritXIII 2015-08-20 17:58:03 -04:00
parent 74b771373d
commit 997418a9de
8 changed files with 268 additions and 11 deletions

View File

@ -368,7 +368,7 @@ QSplitter:handle{
<verstretch>0</verstretch> <verstretch>0</verstretch>
</sizepolicy> </sizepolicy>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_6"> <layout class="QVBoxLayout" name="statusLayout">
<property name="spacing"> <property name="spacing">
<number>0</number> <number>0</number>
</property> </property>
@ -1085,7 +1085,7 @@ QSplitter:handle{
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>284</width> <width>284</width>
<height>398</height> <height>393</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_5"/> <layout class="QVBoxLayout" name="verticalLayout_5"/>

View File

@ -301,6 +301,21 @@ void Settings::loadPersonnal(Profile* profile)
ps.endArray(); ps.endArray();
ps.endGroup(); ps.endGroup();
ps.beginGroup("Requests");
unreadFriendRequests = ps.value("unread", 0).toUInt();
size = ps.beginReadArray("Request");
friendLst.reserve(size);
for (int i = 0; i < size; i ++)
{
ps.setArrayIndex(i);
QPair<QString, QString> request;
request.first = ps.value("addr").toString();
request.second = ps.value("message").toString();
friendRequests.push_back(request);
}
ps.endArray();
ps.endGroup();
ps.beginGroup("General"); ps.beginGroup("General");
compactLayout = ps.value("compactLayout", false).toBool(); compactLayout = ps.value("compactLayout", false).toBool();
ps.endGroup(); ps.endGroup();
@ -467,7 +482,22 @@ void Settings::savePersonal(QString profileName, QString password)
if (getEnableLogging()) if (getEnableLogging())
ps.setValue("activity", frnd.activity); ps.setValue("activity", frnd.activity);
index++; ++index;
}
ps.endArray();
ps.endGroup();
ps.beginGroup("Requests");
ps.setValue("unread", unreadFriendRequests);
ps.beginWriteArray("Request", friendRequests.size());
index = 0;
for (auto& request : friendRequests)
{
ps.setArrayIndex(index);
ps.setValue("addr", request.first);
ps.setValue("message", request.second);
++index;
} }
ps.endArray(); ps.endArray();
ps.endGroup(); ps.endGroup();
@ -1379,6 +1409,48 @@ void Settings::setCircleExpanded(int id, bool expanded)
circleLst[id].expanded = expanded; circleLst[id].expanded = expanded;
} }
void Settings::addFriendRequest(const QString &friendAddress, const QString &message)
{
QMutexLocker locker{&bigLock};
QPair<QString, QString> request(friendAddress, message);
if (friendRequests.indexOf(request) != -1)
return;
friendRequests.push_back(request);
++unreadFriendRequests;
}
unsigned int Settings::getUnreadFriendRequests() const
{
QMutexLocker locker{&bigLock};
return unreadFriendRequests;
}
QPair<QString, QString> Settings::getFriendRequest(int index) const
{
QMutexLocker locker{&bigLock};
return friendRequests.at(index);
}
int Settings::getFriendRequestSize() const
{
QMutexLocker locker{&bigLock};
return friendRequests.size();
}
void Settings::clearUnreadFriendRequests()
{
QMutexLocker locker{&bigLock};
unreadFriendRequests = 0;
}
void Settings::removeFriendRequest(int index)
{
QMutexLocker locker{&bigLock};
friendRequests.removeAt(index);
}
int Settings::removeCircle(int id) int Settings::removeCircle(int id)
{ {
// Replace index with last one and remove last one instead. // Replace index with last one and remove last one instead.

View File

@ -251,6 +251,13 @@ public:
bool getCircleExpanded(int id) const; bool getCircleExpanded(int id) const;
void setCircleExpanded(int id, bool expanded); void setCircleExpanded(int id, bool expanded);
void addFriendRequest(const QString &friendAddress, const QString &message);
unsigned int getUnreadFriendRequests() const;
QPair<QString, QString> getFriendRequest(int index) const;
int getFriendRequestSize() const;
void clearUnreadFriendRequests();
void removeFriendRequest(int index);
// Assume all widgets have unique names // Assume all widgets have unique names
// Don't use it to save every single thing you want to save, use it // Don't use it to save every single thing you want to save, use it
// for some general purpose widgets, such as MainWindows or Splitters, // for some general purpose widgets, such as MainWindows or Splitters,
@ -327,6 +334,9 @@ private:
bool autoSaveEnabled; bool autoSaveEnabled;
QString globalAutoAcceptDir; QString globalAutoAcceptDir;
QList<QPair<QString, QString>> friendRequests;
unsigned int unreadFriendRequests;
// GUI // GUI
QString smileyPack; QString smileyPack;
int emojiFontPointSize; int emojiFontPointSize;

View File

@ -23,6 +23,8 @@
#include <QMessageBox> #include <QMessageBox>
#include <QErrorMessage> #include <QErrorMessage>
#include <QClipboard> #include <QClipboard>
#include <QTabWidget>
#include <QSignalMapper>
#include <tox/tox.h> #include <tox/tox.h>
#include "ui_mainwindow.h" #include "ui_mainwindow.h"
#include "src/nexus.h" #include "src/nexus.h"
@ -35,13 +37,23 @@
AddFriendForm::AddFriendForm() AddFriendForm::AddFriendForm()
{ {
main = new QWidget(), head = new QWidget(); tabWidget = new QTabWidget();
main = new QWidget(tabWidget), head = new QWidget();
QFont bold; QFont bold;
bold.setBold(true); bold.setBold(true);
headLabel.setFont(bold); headLabel.setFont(bold);
retranslateUi(); retranslateUi();
tabWidget->addTab(main, tr("Add a friend"));
QScrollArea* scrollArea = new QScrollArea(tabWidget);
QWidget* requestWidget = new QWidget(tabWidget);
scrollArea->setWidget(requestWidget);
scrollArea->setWidgetResizable(true);
requestsLayout = new QVBoxLayout(requestWidget);
requestsLayout->addStretch(1);
tabWidget->addTab(scrollArea, tr("Friend requests"));
main->setLayout(&layout); main->setLayout(&layout);
layout.addWidget(&toxIdLabel); layout.addWidget(&toxIdLabel);
layout.addWidget(&toxId); layout.addWidget(&toxId);
@ -52,25 +64,38 @@ AddFriendForm::AddFriendForm()
head->setLayout(&headLayout); head->setLayout(&headLayout);
headLayout.addWidget(&headLabel); headLayout.addWidget(&headLabel);
connect(tabWidget, &QTabWidget::currentChanged, this, &AddFriendForm::onCurrentChanged);
connect(&toxId,&QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered); connect(&toxId,&QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered);
connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered())); connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
connect(Nexus::getCore(), &Core::usernameSet, this, &AddFriendForm::onUsernameSet); connect(Nexus::getCore(), &Core::usernameSet, this, &AddFriendForm::onUsernameSet);
Translator::registerHandler(std::bind(&AddFriendForm::retranslateUi, this), this); Translator::registerHandler(std::bind(&AddFriendForm::retranslateUi, this), this);
acceptMapper = new QSignalMapper(requestWidget);
rejectMapper = new QSignalMapper(requestWidget);
connect(acceptMapper, SIGNAL(mapped(QWidget*)), this, SLOT(onFriendRequestAccepted(QWidget*)));
connect(rejectMapper, SIGNAL(mapped(QWidget*)), this, SLOT(onFriendRequestRejected(QWidget*)));
int size = Settings::getInstance().getFriendRequestSize();
for (int i = 0; i < size; ++i)
{
QPair<QString, QString> request = Settings::getInstance().getFriendRequest(i);
addFriendRequestWidget(request.first, request.second);
}
} }
AddFriendForm::~AddFriendForm() AddFriendForm::~AddFriendForm()
{ {
Translator::unregister(this); Translator::unregister(this);
head->deleteLater(); head->deleteLater();
main->deleteLater(); tabWidget->deleteLater();
} }
void AddFriendForm::show(Ui::MainWindow &ui) void AddFriendForm::show(Ui::MainWindow &ui)
{ {
ui.mainContent->layout()->addWidget(main); ui.mainContent->layout()->addWidget(tabWidget);
ui.mainHead->layout()->addWidget(head); ui.mainHead->layout()->addWidget(head);
main->show(); tabWidget->show();
head->show(); head->show();
setIdFromClipboard(); setIdFromClipboard();
toxId.setFocus(); toxId.setFocus();
@ -82,6 +107,18 @@ QString AddFriendForm::getMessage() const
return !msg.isEmpty() ? msg : message.placeholderText(); return !msg.isEmpty() ? msg : message.placeholderText();
} }
void AddFriendForm::setMode(Mode mode)
{
tabWidget->setCurrentIndex(mode);
}
void AddFriendForm::addFriendRequest(const QString &friendAddress, const QString &message)
{
addFriendRequestWidget(friendAddress, message);
Settings::getInstance().addFriendRequest(friendAddress, message);
onCurrentChanged(tabWidget->currentIndex());
}
void AddFriendForm::onUsernameSet(const QString& username) void AddFriendForm::onUsernameSet(const QString& username)
{ {
lastUsername = username; lastUsername = username;
@ -140,6 +177,35 @@ void AddFriendForm::setIdFromClipboard()
toxId.setText(id); toxId.setText(id);
} }
} }
#include <QDebug>
void AddFriendForm::onFriendRequestAccepted(QWidget* friendWidget)
{
int index = requestsLayout->indexOf(friendWidget);
friendWidget->deleteLater();
requestsLayout->removeWidget(friendWidget);
emit friendRequestAccepted(Settings::getInstance().getFriendRequest(requestsLayout->count() - index - 1).first);
qDebug() << "Accepted:" << requestsLayout->count() - index - 1;
}
void AddFriendForm::onFriendRequestRejected(QWidget* friendWidget)
{
int index = requestsLayout->indexOf(friendWidget);
friendWidget->deleteLater();
requestsLayout->removeWidget(friendWidget);
Settings::getInstance().removeFriendRequest(requestsLayout->count() - index - 1);
Settings::getInstance().savePersonal();
qDebug() << "Rejected:" << requestsLayout->count() - index - 1;
}
void AddFriendForm::onCurrentChanged(int index)
{
if (index == FriendRequest && Settings::getInstance().getUnreadFriendRequests() != 0)
{
Settings::getInstance().clearUnreadFriendRequests();
Settings::getInstance().savePersonal();
emit friendRequestsSeen();
}
}
void AddFriendForm::retranslateUi() void AddFriendForm::retranslateUi()
{ {
@ -151,3 +217,27 @@ void AddFriendForm::retranslateUi()
"Default message in friend requests if the field is left blank. Write something appropriate!") "Default message in friend requests if the field is left blank. Write something appropriate!")
.arg(lastUsername)); .arg(lastUsername));
} }
void AddFriendForm::addFriendRequestWidget(const QString &friendAddress, const QString &message)
{
QWidget* friendWidget = new QWidget(tabWidget);
QHBoxLayout* friendLayout = new QHBoxLayout(friendWidget);
QVBoxLayout* horLayout = new QVBoxLayout();
horLayout->setMargin(0);
friendLayout->addLayout(horLayout);
CroppingLabel* friendLabel = new CroppingLabel(friendWidget);
friendLabel->setText("<b>" "12345678901234567890" "</b>");
horLayout->addWidget(friendLabel);
QLabel* messageLabel = new QLabel(message);
messageLabel->setWordWrap(true);
horLayout->addWidget(messageLabel, 1);
QPushButton* acceptButton = new QPushButton(tr("Accept"));
connect(acceptButton, SIGNAL(pressed()), acceptMapper,SLOT(map()));
acceptMapper->setMapping(acceptButton, friendWidget);
friendLayout->addWidget(acceptButton);
QPushButton* rejectButton = new QPushButton(tr("Reject"));
connect(rejectButton, SIGNAL(pressed()), rejectMapper,SLOT(map()));
rejectMapper->setMapping(rejectButton, friendWidget);
friendLayout->addWidget(rejectButton);
requestsLayout->insertWidget(0, friendWidget);
}

View File

@ -26,12 +26,21 @@
#include <QTextEdit> #include <QTextEdit>
#include <QPushButton> #include <QPushButton>
class QTabWidget;
class QSignalMapper;
namespace Ui {class MainWindow;} namespace Ui {class MainWindow;}
class AddFriendForm : public QObject class AddFriendForm : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
enum Mode
{
AddFriend = 0,
FriendRequest = 1
};
AddFriendForm(); AddFriendForm();
AddFriendForm(const AddFriendForm&) = delete; AddFriendForm(const AddFriendForm&) = delete;
AddFriendForm& operator=(const AddFriendForm&) = delete; AddFriendForm& operator=(const AddFriendForm&) = delete;
@ -39,18 +48,27 @@ public:
void show(Ui::MainWindow &ui); void show(Ui::MainWindow &ui);
QString getMessage() const; QString getMessage() const;
void setMode(Mode mode);
void addFriendRequest(const QString& friendAddress, const QString& message);
signals: signals:
void friendRequested(const QString& friendAddress, const QString& message); void friendRequested(const QString& friendAddress, const QString& message);
void friendRequestAccepted(const QString& friendAddress);
void friendRequestsSeen();
public slots: public slots:
void onUsernameSet(const QString& userName); void onUsernameSet(const QString& userName);
private slots: private slots:
void onSendTriggered(); void onSendTriggered();
void onFriendRequestAccepted(QWidget *friendWidget);
void onFriendRequestRejected(QWidget *friendWidget);
void onCurrentChanged(int index);
private: private:
void retranslateUi(); void retranslateUi();
void addFriendRequestWidget(const QString& friendAddress, const QString& message);
private: private:
void setIdFromClipboard(); void setIdFromClipboard();
@ -61,6 +79,10 @@ private:
QVBoxLayout layout, headLayout; QVBoxLayout layout, headLayout;
QWidget *head, *main; QWidget *head, *main;
QString lastUsername; // Cached username so we can retranslate the invite message QString lastUsername; // Cached username so we can retranslate the invite message
QTabWidget* tabWidget;
QVBoxLayout* requestsLayout;
QSignalMapper* acceptMapper;
QSignalMapper* rejectMapper;
}; };
#endif // ADDFRIENDFORM_H #endif // ADDFRIENDFORM_H

View File

@ -349,6 +349,12 @@ void Widget::init()
if (!Settings::getInstance().getShowSystemTray()) if (!Settings::getInstance().getShowSystemTray())
show(); show();
friendRequestsButton = nullptr;
friendRequestsUpdate();
connect(addFriendForm, &AddFriendForm::friendRequestsSeen, this, &Widget::friendRequestsUpdate);
connect(addFriendForm, &AddFriendForm::friendRequestAccepted, this, &Widget::friendRequestAccepted);
} }
bool Widget::eventFilter(QObject *obj, QEvent *event) bool Widget::eventFilter(QObject *obj, QEvent *event)
@ -534,6 +540,7 @@ void Widget::forceShow()
void Widget::onAddClicked() void Widget::onAddClicked()
{ {
hideMainForms(); hideMainForms();
addFriendForm->setMode(AddFriendForm::AddFriend);
addFriendForm->show(*ui); addFriendForm->show(*ui);
setWindowTitle(tr("Add friend")); setWindowTitle(tr("Add friend"));
setActiveToolMenuButton(Widget::AddButton); setActiveToolMenuButton(Widget::AddButton);
@ -971,10 +978,9 @@ void Widget::playRingtone()
void Widget::onFriendRequestReceived(const QString& userId, const QString& message) void Widget::onFriendRequestReceived(const QString& userId, const QString& message)
{ {
FriendRequestDialog dialog(this, userId, message); QApplication::alert(this);
eventFlag = true;
if (dialog.exec() == QDialog::Accepted) friendRequestRecieved(userId, message);
emit friendRequestAccepted(userId);
} }
void Widget::updateFriendActivity(Friend *frnd) void Widget::updateFriendActivity(Friend *frnd)
@ -1670,6 +1676,38 @@ void Widget::friendListContextMenu(const QPoint &pos)
contactListWidget->addCircleWidget(); contactListWidget->addCircleWidget();
} }
void Widget::friendRequestRecieved(const QString& friendAddress, const QString& message)
{
addFriendForm->addFriendRequest(friendAddress, message);
friendRequestsUpdate();
}
void Widget::friendRequestsUpdate()
{
unsigned int unreadFriendRequests = Settings::getInstance().getUnreadFriendRequests();
if (unreadFriendRequests == 0)
{
delete friendRequestsButton;
friendRequestsButton = nullptr;
}
else
{
if (!friendRequestsButton)
{
friendRequestsButton = new QPushButton(tr("%n New Friend Request(s)", "", unreadFriendRequests));
friendRequestsButton->setObjectName("green");
}
ui->statusLayout->insertWidget(2, friendRequestsButton);
connect(friendRequestsButton, &QPushButton::released, [this]()
{
onAddClicked();
addFriendForm->setMode(AddFriendForm::FriendRequest);
});
}
}
void Widget::setActiveToolMenuButton(ActiveToolMenuButton newActiveButton) void Widget::setActiveToolMenuButton(ActiveToolMenuButton newActiveButton)
{ {
ui->addButton->setChecked(newActiveButton == Widget::AddButton); ui->addButton->setChecked(newActiveButton == Widget::AddButton);

View File

@ -51,6 +51,7 @@ class SettingsWidget;
class AddFriendForm; class AddFriendForm;
class CircleWidget; class CircleWidget;
class QActionGroup; class QActionGroup;
class QPushButton;
class Widget final : public QMainWindow class Widget final : public QMainWindow
{ {
@ -159,6 +160,8 @@ private slots:
void onSplitterMoved(int pos, int index); void onSplitterMoved(int pos, int index);
void processOfflineMsgs(); void processOfflineMsgs();
void friendListContextMenu(const QPoint &pos); void friendListContextMenu(const QPoint &pos);
void friendRequestRecieved(const QString& friendAddress, const QString& message);
void friendRequestsUpdate();
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
void bringAllToFront(); void bringAllToFront();
@ -241,6 +244,7 @@ private:
bool eventFlag; bool eventFlag;
bool eventIcon; bool eventIcon;
bool wasMaximized = false; bool wasMaximized = false;
QPushButton* friendRequestsButton;
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
QAction* fullscreenAction; QAction* fullscreenAction;

View File

@ -25,6 +25,26 @@ QToolButton::menu-indicator {
image: none image: none
} }
QPushButton#green {
background: none;
background-color: #6bc260;
color: white;
border-style: none;
border-radius: 4px;
padding: 4px;
margin: 4px 8px;
}
QPushButton#green:hover
{
background-color: #79c76f;
}
QPushButton#green:pressed
{
background-color: #51b244;
}
/** /**
Uncomment this after https://github.com/tux3/qTox/pull/1640 Uncomment this after https://github.com/tux3/qTox/pull/1640
is merged! is merged!
@ -96,3 +116,4 @@ QListView {
position: relative; position: relative;
bottom: 2px; bottom: 2px;
} }