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

View File

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

View File

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

View File

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

View File

@ -425,7 +425,9 @@ void FriendWidget::mouseMoveEvent(QMouseEvent* ev)
const int distance = (dragStartPos - ev->pos()).manhattanLength(); const int distance = (dragStartPos - ev->pos()).manhattanLength();
if (distance > QApplication::startDragDistance()) { if (distance > QApplication::startDragDistance()) {
QMimeData* mdata = new QMimeData; 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); QDrag* drag = new QDrag(this);
drag->setMimeData(mdata); drag->setMimeData(mdata);

View File

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