2014-07-07 00:19:45 +08:00
|
|
|
/*
|
|
|
|
Copyright (C) 2014 by Project Tox <https://tox.im>
|
|
|
|
|
|
|
|
This file is part of qTox, a Qt-based graphical interface for Tox.
|
|
|
|
|
|
|
|
This program 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.
|
|
|
|
This program 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 COPYING file for more details.
|
|
|
|
*/
|
|
|
|
|
2014-06-25 04:11:11 +08:00
|
|
|
#include "friendwidget.h"
|
2014-10-08 12:26:25 +08:00
|
|
|
#include "src/group.h"
|
|
|
|
#include "src/grouplist.h"
|
2014-06-28 16:53:38 +08:00
|
|
|
#include "groupwidget.h"
|
2014-10-08 12:26:25 +08:00
|
|
|
#include "src/friendlist.h"
|
|
|
|
#include "src/friend.h"
|
|
|
|
#include "src/core.h"
|
|
|
|
#include "form/chatform.h"
|
|
|
|
#include "maskablepixmapwidget.h"
|
|
|
|
#include "croppinglabel.h"
|
|
|
|
#include "src/misc/style.h"
|
2014-10-18 11:59:59 +08:00
|
|
|
#include "src/misc/settings.h"
|
2014-06-25 04:11:11 +08:00
|
|
|
#include <QContextMenuEvent>
|
|
|
|
#include <QMenu>
|
2014-09-26 00:03:25 +08:00
|
|
|
#include <QDrag>
|
|
|
|
#include <QMimeData>
|
|
|
|
#include <QApplication>
|
2014-09-26 22:39:29 +08:00
|
|
|
#include <QBitmap>
|
2014-10-18 11:59:59 +08:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QDebug>
|
2014-11-08 00:02:10 +08:00
|
|
|
#include <QInputDialog>
|
2014-06-25 04:11:11 +08:00
|
|
|
|
|
|
|
FriendWidget::FriendWidget(int FriendId, QString id)
|
2014-10-01 02:10:42 +08:00
|
|
|
: friendId(FriendId)
|
|
|
|
, isDefaultAvatar{true}
|
2014-06-25 04:11:11 +08:00
|
|
|
{
|
2014-10-01 02:10:42 +08:00
|
|
|
avatar->setPixmap(QPixmap(":img/contact.png"), Qt::transparent);
|
2014-07-01 10:12:17 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_away.png"));
|
2014-10-01 17:13:49 +08:00
|
|
|
nameLabel->setText(id);
|
2014-06-25 04:11:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FriendWidget::contextMenuEvent(QContextMenuEvent * event)
|
|
|
|
{
|
|
|
|
QPoint pos = event->globalPos();
|
2014-10-18 11:59:59 +08:00
|
|
|
QString id = Core::getInstance()->getFriendAddress(friendId);
|
|
|
|
QString dir = Settings::getInstance().getAutoAcceptDir(id);
|
2014-06-25 04:11:11 +08:00
|
|
|
QMenu menu;
|
2014-10-20 12:25:52 +08:00
|
|
|
QMenu* inviteMenu = menu.addMenu(tr("Invite to group","Menu to invite a friend to a groupchat"));
|
2014-07-04 22:41:29 +08:00
|
|
|
QAction* copyId = menu.addAction(tr("Copy friend ID","Menu to copy the Tox ID of that friend"));
|
2014-06-28 16:53:38 +08:00
|
|
|
QMap<QAction*, Group*> groupActions;
|
2014-11-06 05:59:29 +08:00
|
|
|
|
2014-06-28 16:53:38 +08:00
|
|
|
for (Group* group : GroupList::groupList)
|
|
|
|
{
|
2014-10-01 17:13:49 +08:00
|
|
|
QAction* groupAction = inviteMenu->addAction(group->widget->getName());
|
2014-06-28 16:53:38 +08:00
|
|
|
groupActions[groupAction] = group;
|
|
|
|
}
|
2014-11-06 05:59:29 +08:00
|
|
|
|
2014-06-28 16:53:38 +08:00
|
|
|
if (groupActions.isEmpty())
|
|
|
|
inviteMenu->setEnabled(false);
|
2014-11-06 05:59:29 +08:00
|
|
|
|
2014-11-08 00:02:10 +08:00
|
|
|
QAction* setAlias = menu.addAction(tr("Set alias..."));
|
|
|
|
|
2014-10-20 12:25:52 +08:00
|
|
|
menu.addSeparator();
|
2014-10-18 11:59:59 +08:00
|
|
|
QAction* autoAccept = menu.addAction(tr("Auto accept files from this friend", "context menu entry"));
|
2014-11-06 06:34:28 +08:00
|
|
|
autoAccept->setCheckable(true);
|
|
|
|
autoAccept->setChecked(!dir.isEmpty());
|
2014-07-02 05:58:26 +08:00
|
|
|
menu.addSeparator();
|
2014-11-06 05:59:29 +08:00
|
|
|
|
2014-07-04 22:41:29 +08:00
|
|
|
QAction* removeFriendAction = menu.addAction(tr("Remove friend", "Menu to remove the friend from our friendlist"));
|
2014-06-25 04:11:11 +08:00
|
|
|
|
|
|
|
QAction* selectedItem = menu.exec(pos);
|
|
|
|
if (selectedItem)
|
|
|
|
{
|
2014-07-04 22:41:29 +08:00
|
|
|
if (selectedItem == copyId)
|
2014-07-02 05:50:07 +08:00
|
|
|
{
|
|
|
|
emit copyFriendIdToClipboard(friendId);
|
|
|
|
return;
|
2014-11-08 00:02:10 +08:00
|
|
|
} else if (selectedItem == setAlias)
|
|
|
|
{
|
|
|
|
setFriendAlias();
|
2014-07-02 05:50:07 +08:00
|
|
|
}
|
2014-07-04 22:41:29 +08:00
|
|
|
else if (selectedItem == removeFriendAction)
|
2014-06-25 04:11:11 +08:00
|
|
|
{
|
2014-07-05 14:50:18 +08:00
|
|
|
hide();
|
|
|
|
show(); //Toggle visibility to work around bug of repaintEvent() not being fired on parent widget when this is hidden
|
2014-06-25 04:11:11 +08:00
|
|
|
hide();
|
|
|
|
emit removeFriend(friendId);
|
|
|
|
return;
|
|
|
|
}
|
2014-10-18 11:59:59 +08:00
|
|
|
else if (selectedItem == autoAccept)
|
|
|
|
{
|
2014-11-06 06:34:28 +08:00
|
|
|
if (!autoAccept->isChecked())
|
|
|
|
{
|
|
|
|
qDebug() << "not checked";
|
2014-10-18 11:59:59 +08:00
|
|
|
dir = QDir::homePath();
|
2014-11-06 06:34:28 +08:00
|
|
|
autoAccept->setChecked(false);
|
|
|
|
Settings::getInstance().setAutoAcceptDir(id, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (autoAccept->isChecked())
|
2014-10-18 11:59:59 +08:00
|
|
|
{
|
2014-11-06 06:34:28 +08:00
|
|
|
dir = QFileDialog::getExistingDirectory(0, tr("Choose an auto accept directory","popup title"), dir);
|
|
|
|
autoAccept->setChecked(true);
|
2014-10-18 11:59:59 +08:00
|
|
|
qDebug() << "FriendWidget: setting auto accept dir for" << friendId << "to" << dir;
|
|
|
|
Settings::getInstance().setAutoAcceptDir(id, dir);
|
|
|
|
}
|
|
|
|
}
|
2014-06-28 16:53:38 +08:00
|
|
|
else if (groupActions.contains(selectedItem))
|
|
|
|
{
|
|
|
|
Group* group = groupActions[selectedItem];
|
2014-09-11 21:44:34 +08:00
|
|
|
Core::getInstance()->groupInviteFriend(friendId, group->groupId);
|
2014-06-28 16:53:38 +08:00
|
|
|
}
|
2014-06-25 04:11:11 +08:00
|
|
|
}
|
|
|
|
}
|
2014-06-27 07:35:30 +08:00
|
|
|
|
2014-06-27 09:47:16 +08:00
|
|
|
void FriendWidget::setAsActiveChatroom()
|
2014-06-27 07:35:30 +08:00
|
|
|
{
|
2014-10-01 02:10:42 +08:00
|
|
|
setActive(true);
|
2014-06-28 02:11:32 +08:00
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
if (isDefaultAvatar)
|
|
|
|
avatar->setPixmap(QPixmap(":img/contact_dark.png"), Qt::transparent);
|
2014-06-27 07:35:30 +08:00
|
|
|
}
|
|
|
|
|
2014-06-27 09:47:16 +08:00
|
|
|
void FriendWidget::setAsInactiveChatroom()
|
2014-06-27 07:35:30 +08:00
|
|
|
{
|
2014-10-01 02:10:42 +08:00
|
|
|
setActive(false);
|
2014-06-28 02:11:32 +08:00
|
|
|
|
2014-10-01 02:10:42 +08:00
|
|
|
if (isDefaultAvatar)
|
|
|
|
avatar->setPixmap(QPixmap(":img/contact.png"), Qt::transparent);
|
2014-06-27 07:35:30 +08:00
|
|
|
}
|
2014-08-22 23:19:16 +08:00
|
|
|
|
2014-08-31 22:19:16 +08:00
|
|
|
void FriendWidget::updateStatusLight()
|
|
|
|
{
|
|
|
|
Friend* f = FriendList::findFriend(friendId);
|
2014-11-06 23:26:22 +08:00
|
|
|
Status status = f->getStatus();
|
2014-08-31 22:19:16 +08:00
|
|
|
|
2014-11-06 23:26:22 +08:00
|
|
|
if (status == Status::Online && f->getEventFlag() == 0)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_online.png"));
|
2014-11-06 23:26:22 +08:00
|
|
|
else if (status == Status::Online && f->getEventFlag() == 1)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_online_notification.png"));
|
2014-11-06 23:26:22 +08:00
|
|
|
else if (status == Status::Away && f->getEventFlag() == 0)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_idle.png"));
|
2014-11-06 23:26:22 +08:00
|
|
|
else if (status == Status::Away && f->getEventFlag() == 1)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_idle_notification.png"));
|
2014-11-06 23:26:22 +08:00
|
|
|
else if (status == Status::Busy && f->getEventFlag() == 0)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_busy.png"));
|
2014-11-06 23:26:22 +08:00
|
|
|
else if (status == Status::Busy && f->getEventFlag() == 1)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_busy_notification.png"));
|
2014-11-06 23:26:22 +08:00
|
|
|
else if (status == Status::Offline && f->getEventFlag() == 0)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_away.png"));
|
2014-11-06 23:26:22 +08:00
|
|
|
else if (status == Status::Offline && f->getEventFlag() == 1)
|
2014-08-31 22:19:16 +08:00
|
|
|
statusPic.setPixmap(QPixmap(":img/status/dot_away_notification.png"));
|
|
|
|
}
|
2014-09-02 00:20:28 +08:00
|
|
|
|
|
|
|
void FriendWidget::setChatForm(Ui::MainWindow &ui)
|
|
|
|
{
|
|
|
|
Friend* f = FriendList::findFriend(friendId);
|
2014-11-06 23:26:22 +08:00
|
|
|
f->getChatForm()->show(ui);
|
2014-09-02 00:20:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FriendWidget::resetEventFlags()
|
|
|
|
{
|
|
|
|
Friend* f = FriendList::findFriend(friendId);
|
2014-11-06 23:26:22 +08:00
|
|
|
f->setEventFlag(false);
|
2014-09-02 00:20:28 +08:00
|
|
|
}
|
2014-09-24 23:28:38 +08:00
|
|
|
|
|
|
|
void FriendWidget::onAvatarChange(int FriendId, const QPixmap& pic)
|
|
|
|
{
|
|
|
|
if (FriendId != friendId)
|
|
|
|
return;
|
|
|
|
|
2014-09-24 23:35:24 +08:00
|
|
|
isDefaultAvatar = false;
|
2014-09-26 22:39:29 +08:00
|
|
|
avatar->setPixmap(pic);
|
2014-09-30 02:18:03 +08:00
|
|
|
avatar->autopickBackground();
|
2014-09-24 23:28:38 +08:00
|
|
|
}
|
2014-09-26 00:03:25 +08:00
|
|
|
|
2014-09-27 03:23:20 +08:00
|
|
|
void FriendWidget::onAvatarRemoved(int FriendId)
|
|
|
|
{
|
|
|
|
if (FriendId != friendId)
|
|
|
|
return;
|
|
|
|
|
|
|
|
isDefaultAvatar = true;
|
2014-10-05 20:22:28 +08:00
|
|
|
|
|
|
|
if (isActive())
|
|
|
|
avatar->setPixmap(QPixmap(":img/contact_dark.png"), Qt::transparent);
|
|
|
|
else
|
|
|
|
avatar->setPixmap(QPixmap(":img/contact.png"), Qt::transparent);
|
2014-09-27 03:23:20 +08:00
|
|
|
}
|
|
|
|
|
2014-09-26 00:03:25 +08:00
|
|
|
void FriendWidget::mousePressEvent(QMouseEvent *ev)
|
|
|
|
{
|
|
|
|
if (ev->button() == Qt::LeftButton)
|
|
|
|
dragStartPos = ev->pos();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FriendWidget::mouseMoveEvent(QMouseEvent *ev)
|
|
|
|
{
|
|
|
|
if (!(ev->buttons() & Qt::LeftButton))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if ((dragStartPos - ev->pos()).manhattanLength() > QApplication::startDragDistance())
|
|
|
|
{
|
|
|
|
QDrag* drag = new QDrag(this);
|
|
|
|
QMimeData* mdata = new QMimeData;
|
|
|
|
mdata->setData("friend", QString::number(friendId).toLatin1());
|
|
|
|
|
|
|
|
drag->setMimeData(mdata);
|
2014-09-26 22:39:29 +08:00
|
|
|
drag->setPixmap(avatar->getPixmap());
|
2014-09-26 00:03:25 +08:00
|
|
|
|
|
|
|
drag->exec(Qt::CopyAction | Qt::MoveAction);
|
|
|
|
}
|
|
|
|
}
|
2014-11-07 22:55:32 +08:00
|
|
|
|
2014-11-08 00:02:10 +08:00
|
|
|
void FriendWidget::setFriendAlias()
|
2014-11-07 22:55:32 +08:00
|
|
|
{
|
2014-11-08 00:02:10 +08:00
|
|
|
bool ok;
|
2014-11-07 22:55:32 +08:00
|
|
|
Friend* f = FriendList::findFriend(friendId);
|
2014-11-08 00:02:10 +08:00
|
|
|
|
|
|
|
QString alias = QInputDialog::getText(nullptr, tr("User alias"), tr("Alias:"), QLineEdit::Normal,
|
|
|
|
f->getDisplayedName(), &ok);
|
|
|
|
|
|
|
|
if (ok)
|
|
|
|
{
|
|
|
|
f->setAlias(alias);
|
|
|
|
Settings::getInstance().setFriendAlias(f->getToxID(), alias);
|
|
|
|
hide();
|
|
|
|
show();
|
|
|
|
}
|
2014-11-07 22:55:32 +08:00
|
|
|
}
|