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

View File

@ -301,6 +301,21 @@ void Settings::loadPersonnal(Profile* profile)
ps.endArray();
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");
compactLayout = ps.value("compactLayout", false).toBool();
ps.endGroup();
@ -467,7 +482,22 @@ void Settings::savePersonal(QString profileName, QString password)
if (getEnableLogging())
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.endGroup();
@ -1379,6 +1409,48 @@ void Settings::setCircleExpanded(int id, bool 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)
{
// Replace index with last one and remove last one instead.

View File

@ -251,6 +251,13 @@ public:
bool getCircleExpanded(int id) const;
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
// 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,
@ -327,6 +334,9 @@ private:
bool autoSaveEnabled;
QString globalAutoAcceptDir;
QList<QPair<QString, QString>> friendRequests;
unsigned int unreadFriendRequests;
// GUI
QString smileyPack;
int emojiFontPointSize;

View File

@ -23,6 +23,8 @@
#include <QMessageBox>
#include <QErrorMessage>
#include <QClipboard>
#include <QTabWidget>
#include <QSignalMapper>
#include <tox/tox.h>
#include "ui_mainwindow.h"
#include "src/nexus.h"
@ -35,13 +37,23 @@
AddFriendForm::AddFriendForm()
{
main = new QWidget(), head = new QWidget();
tabWidget = new QTabWidget();
main = new QWidget(tabWidget), head = new QWidget();
QFont bold;
bold.setBold(true);
headLabel.setFont(bold);
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);
layout.addWidget(&toxIdLabel);
layout.addWidget(&toxId);
@ -52,25 +64,38 @@ AddFriendForm::AddFriendForm()
head->setLayout(&headLayout);
headLayout.addWidget(&headLabel);
connect(tabWidget, &QTabWidget::currentChanged, this, &AddFriendForm::onCurrentChanged);
connect(&toxId,&QLineEdit::returnPressed, this, &AddFriendForm::onSendTriggered);
connect(&sendButton, SIGNAL(clicked()), this, SLOT(onSendTriggered()));
connect(Nexus::getCore(), &Core::usernameSet, this, &AddFriendForm::onUsernameSet);
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()
{
Translator::unregister(this);
head->deleteLater();
main->deleteLater();
tabWidget->deleteLater();
}
void AddFriendForm::show(Ui::MainWindow &ui)
{
ui.mainContent->layout()->addWidget(main);
ui.mainContent->layout()->addWidget(tabWidget);
ui.mainHead->layout()->addWidget(head);
main->show();
tabWidget->show();
head->show();
setIdFromClipboard();
toxId.setFocus();
@ -82,6 +107,18 @@ QString AddFriendForm::getMessage() const
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)
{
lastUsername = username;
@ -140,6 +177,35 @@ void AddFriendForm::setIdFromClipboard()
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()
{
@ -151,3 +217,27 @@ void AddFriendForm::retranslateUi()
"Default message in friend requests if the field is left blank. Write something appropriate!")
.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 <QPushButton>
class QTabWidget;
class QSignalMapper;
namespace Ui {class MainWindow;}
class AddFriendForm : public QObject
{
Q_OBJECT
public:
enum Mode
{
AddFriend = 0,
FriendRequest = 1
};
AddFriendForm();
AddFriendForm(const AddFriendForm&) = delete;
AddFriendForm& operator=(const AddFriendForm&) = delete;
@ -39,18 +48,27 @@ public:
void show(Ui::MainWindow &ui);
QString getMessage() const;
void setMode(Mode mode);
void addFriendRequest(const QString& friendAddress, const QString& message);
signals:
void friendRequested(const QString& friendAddress, const QString& message);
void friendRequestAccepted(const QString& friendAddress);
void friendRequestsSeen();
public slots:
void onUsernameSet(const QString& userName);
private slots:
void onSendTriggered();
void onFriendRequestAccepted(QWidget *friendWidget);
void onFriendRequestRejected(QWidget *friendWidget);
void onCurrentChanged(int index);
private:
void retranslateUi();
void addFriendRequestWidget(const QString& friendAddress, const QString& message);
private:
void setIdFromClipboard();
@ -61,6 +79,10 @@ private:
QVBoxLayout layout, headLayout;
QWidget *head, *main;
QString lastUsername; // Cached username so we can retranslate the invite message
QTabWidget* tabWidget;
QVBoxLayout* requestsLayout;
QSignalMapper* acceptMapper;
QSignalMapper* rejectMapper;
};
#endif // ADDFRIENDFORM_H

View File

@ -349,6 +349,12 @@ void Widget::init()
if (!Settings::getInstance().getShowSystemTray())
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)
@ -534,6 +540,7 @@ void Widget::forceShow()
void Widget::onAddClicked()
{
hideMainForms();
addFriendForm->setMode(AddFriendForm::AddFriend);
addFriendForm->show(*ui);
setWindowTitle(tr("Add friend"));
setActiveToolMenuButton(Widget::AddButton);
@ -971,10 +978,9 @@ void Widget::playRingtone()
void Widget::onFriendRequestReceived(const QString& userId, const QString& message)
{
FriendRequestDialog dialog(this, userId, message);
if (dialog.exec() == QDialog::Accepted)
emit friendRequestAccepted(userId);
QApplication::alert(this);
eventFlag = true;
friendRequestRecieved(userId, message);
}
void Widget::updateFriendActivity(Friend *frnd)
@ -1670,6 +1676,38 @@ void Widget::friendListContextMenu(const QPoint &pos)
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)
{
ui->addButton->setChecked(newActiveButton == Widget::AddButton);

View File

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

View File

@ -25,6 +25,26 @@ QToolButton::menu-indicator {
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
is merged!
@ -96,3 +116,4 @@ QListView {
position: relative;
bottom: 2px;
}