diff --git a/src/core/core.cpp b/src/core/core.cpp index 019f3c5a5..7ec5b1f11 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -341,7 +341,8 @@ void Core::start() } else { - qDebug() << "Self avatar not found"; + qDebug() << "Self avatar not found, will broadcast empty avatar to friends"; + setAvatar({}); } ready = true; @@ -792,10 +793,17 @@ void Core::setUsername(const QString& username) void Core::setAvatar(const QByteArray& data) { - QPixmap pic; - pic.loadFromData(data); - Settings::getInstance().saveAvatar(pic, getSelfId().toString()); - emit selfAvatarChanged(pic); + if (!data.isEmpty()) + { + QPixmap pic; + pic.loadFromData(data); + Settings::getInstance().saveAvatar(pic, getSelfId().toString()); + emit selfAvatarChanged(pic); + } + else + { + emit selfAvatarChanged(QPixmap(":/img/contact_dark.svg")); + } AvatarBroadcaster::setAvatar(data); AvatarBroadcaster::enableAutoBroadcast(); diff --git a/src/core/corefile.cpp b/src/core/corefile.cpp index c4f462630..89a0e464e 100644 --- a/src/core/corefile.cpp +++ b/src/core/corefile.cpp @@ -56,12 +56,20 @@ void CoreFile::sendAvatarFile(Core* core, uint32_t friendId, const QByteArray& d { QMutexLocker mlocker(&fileSendMutex); + if (data.isEmpty()) + { + tox_file_send(core->tox, friendId, TOX_FILE_KIND_AVATAR, 0, + nullptr, nullptr, 0, nullptr); + return; + } + static_assert(TOX_HASH_LENGTH <= TOX_FILE_ID_LENGTH, "TOX_HASH_LENGTH > TOX_FILE_ID_LENGTH!"); uint8_t avatarHash[TOX_HASH_LENGTH]; tox_hash(avatarHash, (uint8_t*)data.data(), data.size()); uint64_t filesize = data.size(); uint32_t fileNum = tox_file_send(core->tox, friendId, TOX_FILE_KIND_AVATAR, filesize, - avatarHash, avatarHash, TOX_HASH_LENGTH, nullptr); + avatarHash, avatarHash, TOX_HASH_LENGTH, nullptr); + if (fileNum == std::numeric_limits::max()) { qWarning() << "sendAvatarFile: Can't create the Tox file sender"; diff --git a/src/widget/form/profileform.cpp b/src/widget/form/profileform.cpp index a8c76e390..afc0f0cc7 100644 --- a/src/widget/form/profileform.cpp +++ b/src/widget/form/profileform.cpp @@ -45,6 +45,8 @@ #include #include #include +#include +#include ProfileForm::ProfileForm(QWidget *parent) : QWidget{parent}, qr{nullptr} @@ -84,8 +86,11 @@ ProfileForm::ProfileForm(QWidget *parent) : profilePicture = new MaskablePixmapWidget(this, QSize(64, 64), ":/img/avatar_mask.svg"); profilePicture->setPixmap(QPixmap(":/img/contact_dark.svg")); + profilePicture->setContextMenuPolicy(Qt::CustomContextMenu); profilePicture->setClickable(true); + profilePicture->installEventFilter(this); connect(profilePicture, SIGNAL(clicked()), this, SLOT(onAvatarClicked())); + connect(profilePicture, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showProfilePictureContextMenu(const QPoint&))); QHBoxLayout *publicGrouplayout = qobject_cast(bodyUI->publicGroup->layout()); publicGrouplayout->insertWidget(0, profilePicture); publicGrouplayout->insertSpacing(1, 7); @@ -163,6 +168,38 @@ void ProfileForm::show(ContentLayout* contentLayout) bodyUI->userName->selectAll(); } +bool ProfileForm::eventFilter(QObject *object, QEvent *event) +{ + if (object == static_cast(profilePicture) && event->type() == QEvent::MouseButtonPress) + { + QMouseEvent *mouseEvent = static_cast(event); + if (mouseEvent->button() == Qt::RightButton) + return true; + } + return false; +} + +void ProfileForm::showProfilePictureContextMenu(const QPoint &point) +{ + QPoint pos = profilePicture->mapToGlobal(point); + + QMenu contextMenu; + QAction *removeAction = contextMenu.addAction(tr("Remove")); + QAction *selectedItem = contextMenu.exec(pos); + + if (selectedItem == removeAction) + { + QString selfPubKey = Core::getInstance()->getSelfId().publicKey; + if (!QFile::remove(Settings::getInstance().getSettingsDirPath()+"avatars/"+selfPubKey.left(64)+".png")) + { + GUI::showError(tr("Error"), tr("Could not remove avatar.")); + return; + } + + Core::getInstance()->setAvatar({}); + } +} + void ProfileForm::copyIdClicked() { toxId->selectAll(); diff --git a/src/widget/form/profileform.h b/src/widget/form/profileform.h index cc995e154..eaa37d7e7 100644 --- a/src/widget/form/profileform.h +++ b/src/widget/form/profileform.h @@ -67,7 +67,6 @@ public slots: private slots: void setToxId(const QString& id); void copyIdClicked(); - void onAvatarClicked(); void onUserNameEdited(); void onStatusMessageEdited(); void onRenameClicked(); @@ -78,12 +77,15 @@ private slots: void onSaveQrClicked(); void onDeletePassClicked(); void onChangePassClicked(); + void onAvatarClicked(); + void showProfilePictureContextMenu(const QPoint &point); private: void retranslateUi(); void prFileLabelUpdate(); private: + bool eventFilter(QObject *object, QEvent *event); void refreshProfiles(); Ui::IdentitySettings* bodyUI; MaskablePixmapWidget* profilePicture;