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

refactor(mime): store ToxPk and GroupId as mimedata

This commit is contained in:
Anthony Bilinski 2019-04-16 00:47:56 -07:00
parent f7603c294b
commit c0fdc42e33
No known key found for this signature in database
GPG Key ID: 2AA8E0DA1B31FB3C
6 changed files with 51 additions and 35 deletions

View File

@ -142,8 +142,11 @@ void CircleWidget::contextMenuEvent(QContextMenuEvent* event)
void CircleWidget::dragEnterEvent(QDragEnterEvent* event)
{
ToxId toxId(event->mimeData()->text());
Friend* f = FriendList::findFriend(toxId.getPublicKey());
if(!event->mimeData()->hasFormat("toxPk")) {
return;
}
ToxPk toxPk(event->mimeData()->data("toxPk"));
Friend* f = FriendList::findFriend(toxPk);
if (f != nullptr)
event->acceptProposedAction();
@ -165,14 +168,17 @@ void CircleWidget::dropEvent(QDropEvent* event)
if (!widget)
return;
if (!event->mimeData()->hasFormat("toxPk")) {
return;
}
// Check, that the user has a friend with the same ToxId
ToxId toxId(event->mimeData()->text());
Friend* f = FriendList::findFriend(toxId.getPublicKey());
ToxPk toxPk{event->mimeData()->data("toxPk")};
Friend* f = FriendList::findFriend(toxPk);
if (!f)
return;
// Save CircleWidget before changing the Id
int circleId = Settings::getInstance().getFriendCircleID(toxId.getPublicKey());
int circleId = Settings::getInstance().getFriendCircleID(toxPk);
CircleWidget* circleWidget = getFromID(circleId);
addFriendWidget(widget, f->getStatus());

View File

@ -458,8 +458,9 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
FriendWidget* frnd = qobject_cast<FriendWidget*>(o);
GroupWidget* group = qobject_cast<GroupWidget*>(o);
if (frnd) {
ToxId toxId(event->mimeData()->text());
Friend* contact = FriendList::findFriend(toxId.getPublicKey());
assert(event->mimeData()->hasFormat("toxPk"));
ToxPk toxPk{event->mimeData()->data("toxPk")};
Friend* contact = FriendList::findFriend(toxPk);
if (!contact) {
return;
}
@ -471,11 +472,7 @@ void ContentDialog::dragEnterEvent(QDragEnterEvent* event)
event->acceptProposedAction();
}
} else if (group) {
qDebug() << event->mimeData()->formats();
if (!event->mimeData()->hasFormat("groupId")) {
return;
}
assert(event->mimeData()->hasFormat("groupId"));
GroupId groupId = GroupId{event->mimeData()->data("groupId")};
Group* contact = GroupList::findGroup(groupId);
if (!contact) {
@ -494,8 +491,9 @@ void ContentDialog::dropEvent(QDropEvent* event)
FriendWidget* frnd = qobject_cast<FriendWidget*>(o);
GroupWidget* group = qobject_cast<GroupWidget*>(o);
if (frnd) {
ToxId toxId(event->mimeData()->text());
Friend* contact = FriendList::findFriend(toxId.getPublicKey());
assert(event->mimeData()->hasFormat("toxPk"));
const ToxPk toxId(event->mimeData()->data("toxPk"));
Friend* contact = FriendList::findFriend(toxId);
if (!contact) {
return;
}
@ -503,11 +501,8 @@ void ContentDialog::dropEvent(QDropEvent* event)
Widget::getInstance()->addFriendDialog(contact, this);
ensureSplitterVisible();
} else if (group) {
if (!event->mimeData()->hasFormat("groupId")) {
return;
}
int groupId = event->mimeData()->data("groupId").toInt();
assert(event->mimeData()->hasFormat("groupId"));
const GroupId groupId(event->mimeData()->data("groupId"));
Group* contact = GroupList::findGroup(groupId);
if (!contact) {
return;

View File

@ -398,16 +398,22 @@ void GroupChatForm::peerAudioPlaying(ToxPk peerPk)
void GroupChatForm::dragEnterEvent(QDragEnterEvent* ev)
{
ToxId toxId = ToxId(ev->mimeData()->text());
Friend* frnd = FriendList::findFriend(toxId.getPublicKey());
if (!ev->mimeData()->hasFormat("toxPk")) {
return;
}
ToxPk toxPk{ev->mimeData()->data("toxPk")};
Friend* frnd = FriendList::findFriend(toxPk);
if (frnd)
ev->acceptProposedAction();
}
void GroupChatForm::dropEvent(QDropEvent* ev)
{
ToxId toxId = ToxId(ev->mimeData()->text());
Friend* frnd = FriendList::findFriend(toxId.getPublicKey());
if (!ev->mimeData()->hasFormat("toxPk")) {
return;
}
ToxPk toxPk{ev->mimeData()->data("toxPk")};
Friend* frnd = FriendList::findFriend(toxPk);
if (!frnd)
return;

View File

@ -565,8 +565,11 @@ void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget
void FriendListWidget::dragEnterEvent(QDragEnterEvent* event)
{
ToxId toxId(event->mimeData()->text());
Friend* frnd = FriendList::findFriend(toxId.getPublicKey());
if (!event->mimeData()->hasFormat("toxPk")) {
return;
}
ToxPk toxPk(event->mimeData()->data("toxPk"));;
Friend* frnd = FriendList::findFriend(toxPk);
if (frnd)
event->acceptProposedAction();
}
@ -580,8 +583,8 @@ void FriendListWidget::dropEvent(QDropEvent* event)
return;
// Check, that the user has a friend with the same ToxPk
const QByteArray data = QByteArray::fromHex(event->mimeData()->text().toLatin1());
const ToxPk toxPk{data};
assert(event->mimeData()->hasFormat("toxPk"));
const ToxPk toxPk{event->mimeData()->data("toxPk")};
Friend* f = FriendList::findFriend(toxPk);
if (!f)
return;

View File

@ -425,7 +425,9 @@ void FriendWidget::mouseMoveEvent(QMouseEvent* ev)
const int distance = (dragStartPos - ev->pos()).manhattanLength();
if (distance > QApplication::startDragDistance()) {
QMimeData* mdata = new QMimeData;
mdata->setText(getFriend()->getPublicKey().toString());
const Friend* frnd = getFriend();
mdata->setText(frnd->getDisplayedName());
mdata->setData("toxPk", frnd->getPublicKey().getByteArray());
QDrag* drag = new QDrag(this);
drag->setMimeData(mdata);

View File

@ -147,8 +147,9 @@ void GroupWidget::mouseMoveEvent(QMouseEvent* ev)
if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance()) {
QMimeData* mdata = new QMimeData;
mdata->setText(getGroup()->getName());
mdata->setData("groupId", QByteArray::number(getGroup()->getId()));
const Group* group = getGroup();
mdata->setText(group->getName());
mdata->setData("groupId", group->getPersistentId().getByteArray());
QDrag* drag = new QDrag(this);
drag->setMimeData(mdata);
@ -216,9 +217,10 @@ void GroupWidget::resetEventFlags()
void GroupWidget::dragEnterEvent(QDragEnterEvent* ev)
{
// TODO: Send ToxPk in mimeData
const ToxId toxId = ToxId(ev->mimeData()->text());
const ToxPk pk = toxId.getPublicKey();
if (!ev->mimeData()->hasFormat("toxPk")) {
return;
}
const ToxPk pk{ev->mimeData()->data("toxPk")};
if (chatroom->friendExists(pk)) {
ev->acceptProposedAction();
}
@ -237,8 +239,10 @@ void GroupWidget::dragLeaveEvent(QDragLeaveEvent*)
void GroupWidget::dropEvent(QDropEvent* ev)
{
const ToxId toxId = ToxId(ev->mimeData()->text());
const ToxPk pk = toxId.getPublicKey();
if (!ev->mimeData()->hasFormat("toxPk")) {
return;
}
const ToxPk pk{ev->mimeData()->data("toxPk")};
if (!chatroom->friendExists(pk)) {
return;
}