2016-02-21 05:32:57 +08:00
|
|
|
/*
|
|
|
|
Copyright © 2015 by The qTox Project
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
qTox is libre software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
qTox is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with qTox. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "groupinviteform.h"
|
|
|
|
|
|
|
|
#include <tox/tox.h>
|
2016-04-10 18:25:27 +08:00
|
|
|
#include <QDebug>
|
2016-02-21 05:32:57 +08:00
|
|
|
#include <QSignalMapper>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QBoxLayout>
|
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QLabel>
|
2016-02-22 07:29:12 +08:00
|
|
|
#include <QWindow>
|
2016-02-21 05:32:57 +08:00
|
|
|
#include "ui_mainwindow.h"
|
|
|
|
#include "src/widget/tool/croppinglabel.h"
|
|
|
|
#include "src/widget/translator.h"
|
|
|
|
#include "src/nexus.h"
|
|
|
|
#include "src/core/core.h"
|
2016-02-21 07:45:18 +08:00
|
|
|
#include "src/widget/gui.h"
|
|
|
|
#include "src/widget/translator.h"
|
|
|
|
#include "src/widget/contentlayout.h"
|
2016-02-21 05:32:57 +08:00
|
|
|
|
|
|
|
GroupInviteForm::GroupInviteForm()
|
|
|
|
{
|
|
|
|
QVBoxLayout* layout = new QVBoxLayout(this);
|
|
|
|
createButton = new QPushButton(this);
|
|
|
|
connect(createButton, &QPushButton::released, [this]()
|
|
|
|
{
|
|
|
|
emit groupCreate(TOX_GROUPCHAT_TYPE_AV);
|
|
|
|
});
|
|
|
|
|
|
|
|
inviteBox = new QGroupBox(this);
|
|
|
|
inviteLayout = new QVBoxLayout(inviteBox);
|
|
|
|
inviteLayout->addStretch(1);
|
|
|
|
|
|
|
|
layout->addWidget(createButton);
|
|
|
|
layout->addWidget(inviteBox);
|
|
|
|
|
|
|
|
QFont bold;
|
|
|
|
bold.setBold(true);
|
|
|
|
|
|
|
|
headLabel = new QLabel(this);
|
|
|
|
headLabel->setFont(bold);
|
|
|
|
headWidget = new QWidget(this);
|
|
|
|
QHBoxLayout* headLayout = new QHBoxLayout(headWidget);
|
|
|
|
headLayout->addWidget(headLabel);
|
|
|
|
|
|
|
|
retranslateUi();
|
|
|
|
Translator::registerHandler(std::bind(&GroupInviteForm::retranslateUi, this), this);
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:59:25 +08:00
|
|
|
GroupInviteForm::~GroupInviteForm()
|
|
|
|
{
|
|
|
|
Translator::unregister(this);
|
|
|
|
}
|
|
|
|
|
2016-02-22 07:29:12 +08:00
|
|
|
bool GroupInviteForm::isShown() const
|
|
|
|
{
|
|
|
|
if (this->isVisible())
|
|
|
|
{
|
|
|
|
headWidget->window()->windowHandle()->alert(0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-21 07:45:18 +08:00
|
|
|
void GroupInviteForm::show(ContentLayout* contentLayout)
|
2016-02-21 05:32:57 +08:00
|
|
|
{
|
2016-02-21 07:45:18 +08:00
|
|
|
contentLayout->mainContent->layout()->addWidget(this);
|
2016-02-21 20:46:45 +08:00
|
|
|
contentLayout->mainHead->layout()->addWidget(headWidget);
|
2016-02-21 05:32:57 +08:00
|
|
|
QWidget::show();
|
|
|
|
headWidget->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupInviteForm::addGroupInvite(int32_t friendId, uint8_t type, QByteArray invite)
|
|
|
|
{
|
|
|
|
QWidget* groupWidget = new QWidget(this);
|
|
|
|
QHBoxLayout* groupLayout = new QHBoxLayout(groupWidget);
|
|
|
|
|
|
|
|
CroppingLabel* groupLabel = new CroppingLabel(this);
|
2016-04-10 19:01:53 +08:00
|
|
|
groupLabels.insert(groupLabel);
|
2016-04-10 18:57:52 +08:00
|
|
|
QString name = Nexus::getCore()->getFriendUsername(friendId);
|
|
|
|
QString time = QDateTime::currentDateTime().toString();
|
|
|
|
groupLabel->setText(tr("Invited by <b>%1</b> on %2.").arg(name, time));
|
2016-02-21 05:32:57 +08:00
|
|
|
groupLayout->addWidget(groupLabel);
|
|
|
|
|
|
|
|
QPushButton* acceptButton = new QPushButton(this);
|
|
|
|
acceptButtons.insert(acceptButton);
|
2016-02-22 07:29:12 +08:00
|
|
|
connect(acceptButton, &QPushButton::released, this, &GroupInviteForm::onGroupInviteAccepted);
|
2016-02-21 05:32:57 +08:00
|
|
|
groupLayout->addWidget(acceptButton);
|
|
|
|
retranslateAcceptButton(acceptButton);
|
|
|
|
|
|
|
|
QPushButton* rejectButton = new QPushButton(this);
|
|
|
|
rejectButtons.insert(rejectButton);
|
2016-02-22 07:29:12 +08:00
|
|
|
connect(rejectButton, &QPushButton::released, this, &GroupInviteForm::onGroupInviteRejected);
|
2016-02-21 05:32:57 +08:00
|
|
|
groupLayout->addWidget(rejectButton);
|
|
|
|
retranslateRejectButton(rejectButton);
|
|
|
|
|
|
|
|
inviteLayout->insertWidget(0, groupWidget);
|
|
|
|
|
|
|
|
GroupInvite group;
|
|
|
|
group.friendId = friendId;
|
|
|
|
group.type = type;
|
|
|
|
group.invite = invite;
|
2016-04-10 19:01:53 +08:00
|
|
|
group.time = QDateTime::currentDateTime();
|
2016-02-21 05:32:57 +08:00
|
|
|
groupInvites.push_front(group);
|
|
|
|
|
|
|
|
if (isVisible())
|
|
|
|
emit groupInvitesSeen();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupInviteForm::showEvent(QShowEvent* event)
|
|
|
|
{
|
|
|
|
QWidget::showEvent(event);
|
|
|
|
emit groupInvitesSeen();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupInviteForm::onGroupInviteAccepted()
|
|
|
|
{
|
2016-02-21 20:46:45 +08:00
|
|
|
QPushButton* acceptButton = static_cast<QPushButton*>(sender());
|
|
|
|
QWidget* groupWidget = acceptButton->parentWidget();
|
2016-02-21 05:32:57 +08:00
|
|
|
int index = inviteLayout->indexOf(groupWidget);
|
|
|
|
GroupInvite invite = groupInvites.at(index);
|
|
|
|
groupInvites.removeAt(index);
|
|
|
|
|
2016-04-10 18:25:27 +08:00
|
|
|
deleteInviteButtons(groupWidget);
|
2016-02-21 05:32:57 +08:00
|
|
|
|
|
|
|
emit groupInviteAccepted(invite.friendId, invite.type, invite.invite);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupInviteForm::onGroupInviteRejected()
|
|
|
|
{
|
2016-02-21 20:46:45 +08:00
|
|
|
QPushButton* rejectButton = static_cast<QPushButton*>(sender());
|
|
|
|
QWidget* groupWidget = rejectButton->parentWidget();
|
2016-02-21 05:32:57 +08:00
|
|
|
int index = inviteLayout->indexOf(groupWidget);
|
|
|
|
groupInvites.removeAt(index);
|
|
|
|
|
2016-04-10 18:25:27 +08:00
|
|
|
deleteInviteButtons(groupWidget);
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:57:52 +08:00
|
|
|
void GroupInviteForm::deleteInviteButtons(QWidget* widget)
|
2016-04-10 18:25:27 +08:00
|
|
|
{
|
|
|
|
QList<QPushButton*> buttons = widget->findChildren<QPushButton*>();
|
|
|
|
|
|
|
|
if (acceptButtons.contains(buttons.at(0)))
|
|
|
|
{
|
|
|
|
acceptButtons.remove(buttons.at(0));
|
|
|
|
rejectButtons.remove(buttons.at(1));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
acceptButtons.remove(buttons.at(1));
|
|
|
|
rejectButtons.remove(buttons.at(0));
|
|
|
|
}
|
|
|
|
|
2016-04-10 19:01:53 +08:00
|
|
|
QList<CroppingLabel*> labels = widget->findChildren<CroppingLabel*>();
|
|
|
|
groupLabels.remove(labels.at(0));
|
|
|
|
|
2016-04-10 18:25:27 +08:00
|
|
|
widget->deleteLater();
|
|
|
|
inviteLayout->removeWidget(widget);
|
2016-02-21 05:32:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupInviteForm::retranslateUi()
|
|
|
|
{
|
|
|
|
headLabel->setText(tr("Groups"));
|
2016-02-25 21:59:25 +08:00
|
|
|
if(createButton)
|
|
|
|
{
|
|
|
|
createButton->setText(tr("Create new group"));
|
|
|
|
}
|
2016-02-21 05:32:57 +08:00
|
|
|
inviteBox->setTitle(tr("Group invites"));
|
|
|
|
|
|
|
|
for (QPushButton* acceptButton : acceptButtons)
|
|
|
|
retranslateAcceptButton(acceptButton);
|
|
|
|
|
|
|
|
for (QPushButton* rejectButton : rejectButtons)
|
|
|
|
retranslateRejectButton(rejectButton);
|
2016-04-10 19:01:53 +08:00
|
|
|
|
|
|
|
for (CroppingLabel* label : groupLabels)
|
|
|
|
retranslateGroupLabel(label);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupInviteForm::retranslateGroupLabel(CroppingLabel* label)
|
|
|
|
{
|
|
|
|
QWidget* groupWidget = label->parentWidget();
|
|
|
|
int index = inviteLayout->indexOf(groupWidget);
|
|
|
|
GroupInvite invite = groupInvites.at(index);
|
|
|
|
|
|
|
|
QString name = Nexus::getCore()->getFriendUsername(invite.friendId);
|
|
|
|
QString date = invite.time.toString();
|
|
|
|
label->setText(tr("Invited by <b>%1</b> on %2.").arg(name, date));
|
2016-02-21 05:32:57 +08:00
|
|
|
}
|
|
|
|
|
2016-04-10 18:57:52 +08:00
|
|
|
void GroupInviteForm::retranslateAcceptButton(QPushButton* acceptButton)
|
2016-02-21 05:32:57 +08:00
|
|
|
{
|
|
|
|
acceptButton->setText(tr("Join"));
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:57:52 +08:00
|
|
|
void GroupInviteForm::retranslateRejectButton(QPushButton* rejectButton)
|
2016-02-21 05:32:57 +08:00
|
|
|
{
|
|
|
|
rejectButton->setText(tr("Decline"));
|
|
|
|
}
|