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

refactor(widget): Small refactoring

This commit is contained in:
Diadlo 2016-12-18 23:31:06 +03:00
parent 1d98a97e3b
commit 9314dd3382
No known key found for this signature in database
GPG Key ID: 5AF9F2E29107C727

View File

@ -103,7 +103,9 @@ void Widget::init()
QIcon themeIcon = QIcon::fromTheme("qtox"); QIcon themeIcon = QIcon::fromTheme("qtox");
if (!themeIcon.isNull()) if (!themeIcon.isNull())
{
setWindowIcon(themeIcon); setWindowIcon(themeIcon);
}
timer = new QTimer(); timer = new QTimer();
timer->start(1000); timer->start(1000);
@ -353,7 +355,9 @@ void Widget::init()
ui->settingsButton->setCheckable(true); ui->settingsButton->setCheckable(true);
if (contentLayout != nullptr) if (contentLayout != nullptr)
{
onAddClicked(); onAddClicked();
}
//restore window state //restore window state
restoreGeometry(Settings::getInstance().getWindowGeometry()); restoreGeometry(Settings::getInstance().getWindowGeometry());
@ -391,7 +395,9 @@ void Widget::init()
Translator::registerHandler(std::bind(&Widget::retranslateUi, this), this); Translator::registerHandler(std::bind(&Widget::retranslateUi, this), this);
if (!Settings::getInstance().getShowSystemTray()) if (!Settings::getInstance().getShowSystemTray())
{
show(); show();
}
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
Nexus::getInstance().updateWindows(); Nexus::getInstance().updateWindows();
@ -413,7 +419,9 @@ bool Widget::eventFilter(QObject *obj, QEvent *event)
case QEvent::WindowStateChange: case QEvent::WindowStateChange:
ce = static_cast<QWindowStateChangeEvent*>(event); ce = static_cast<QWindowStateChangeEvent*>(event);
if (state & Qt::WindowMinimized && obj) if (state & Qt::WindowMinimized && obj)
{
wasMaximized = ce->oldState() & Qt::WindowMaximized; wasMaximized = ce->oldState() & Qt::WindowMaximized;
}
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
emit windowStateChanged(windowState()); emit windowStateChanged(windowState());
@ -429,7 +437,9 @@ bool Widget::eventFilter(QObject *obj, QEvent *event)
void Widget::updateIcons() void Widget::updateIcons()
{ {
if (!icon) if (!icon)
{
return; return;
}
QString status; QString status;
if (eventIcon) if (eventIcon)
@ -440,8 +450,10 @@ void Widget::updateIcons()
{ {
status = ui->statusButton->property("status").toString(); status = ui->statusButton->property("status").toString();
if (!status.length()) if (!status.length())
{
status = QStringLiteral("offline"); status = QStringLiteral("offline");
} }
}
// Some builds of Qt appear to have a bug in icon loading: // Some builds of Qt appear to have a bug in icon loading:
// QIcon::hasThemeIcon is sometimes unaware that the icon returned // QIcon::hasThemeIcon is sometimes unaware that the icon returned
@ -491,8 +503,10 @@ void Widget::updateIcons()
setWindowIcon(ico); setWindowIcon(ico);
if (icon) if (icon)
{
icon->setIcon(ico); icon->setIcon(ico);
} }
}
Widget::~Widget() Widget::~Widget()
{ {
@ -501,13 +515,17 @@ Widget::~Widget()
for (QWidget* window : windowList) for (QWidget* window : windowList)
{ {
if (window != this) if (window != this)
{
window->close(); window->close();
} }
}
Translator::unregister(this); Translator::unregister(this);
AutoUpdater::abortUpdates(); AutoUpdater::abortUpdates();
if (icon) if (icon)
{
icon->hide(); icon->hide();
}
delete icon; delete icon;
delete profileForm; delete profileForm;
@ -532,7 +550,9 @@ Widget::~Widget()
Widget* Widget::getInstance() Widget* Widget::getInstance()
{ {
if (!instance) if (!instance)
{
instance = new Widget(); instance = new Widget();
}
return instance; return instance;
} }
@ -553,6 +573,7 @@ void Widget::moveEvent(QMoveEvent *event)
saveWindowGeometry(); saveWindowGeometry();
saveSplitterGeometry(); saveSplitterGeometry();
} }
QWidget::moveEvent(event); QWidget::moveEvent(event);
} }
@ -583,9 +604,11 @@ void Widget::changeEvent(QEvent *event)
if (isMinimized() && if (isMinimized() &&
Settings::getInstance().getShowSystemTray() && Settings::getInstance().getShowSystemTray() &&
Settings::getInstance().getMinimizeToTray()) Settings::getInstance().getMinimizeToTray())
{
this->hide(); this->hide();
} }
} }
}
void Widget::resizeEvent(QResizeEvent *event) void Widget::resizeEvent(QResizeEvent *event)
{ {
@ -630,7 +653,8 @@ void Widget::onBadProxyCore()
{ {
Settings::getInstance().setProxyType(Settings::ProxyType::ptNone); Settings::getInstance().setProxyType(Settings::ProxyType::ptNone);
QMessageBox critical(this); QMessageBox critical(this);
critical.setText(tr("toxcore failed to start with your proxy settings. qTox cannot run; please modify your " critical.setText(tr("toxcore failed to start with your proxy settings. "
"qTox cannot run; please modify your "
"settings and restart.", "popup text")); "settings and restart.", "popup text"));
critical.setIcon(QMessageBox::Critical); critical.setIcon(QMessageBox::Critical);
critical.exec(); critical.exec();
@ -658,8 +682,10 @@ void Widget::onSeparateWindowChanged(bool separate, bool clicked)
for (QWindow* window : windowList) for (QWindow* window : windowList)
{ {
if (window->objectName() == "detachedWindow") if (window->objectName() == "detachedWindow")
{
window->close(); window->close();
} }
}
QWidget* contentWidget = new QWidget(this); QWidget* contentWidget = new QWidget(this);
contentLayout = new ContentLayout(contentWidget); contentLayout = new ContentLayout(contentWidget);
@ -739,7 +765,9 @@ void Widget::onAddClicked()
if (Settings::getInstance().getSeparateWindow()) if (Settings::getInstance().getSeparateWindow())
{ {
if (!addFriendForm->isShown()) if (!addFriendForm->isShown())
{
addFriendForm->show(createContentDialog(AddDialog)); addFriendForm->show(createContentDialog(AddDialog));
}
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(Widget::None);
} }
@ -757,7 +785,9 @@ void Widget::onGroupClicked()
if (Settings::getInstance().getSeparateWindow()) if (Settings::getInstance().getSeparateWindow())
{ {
if (!groupInviteForm->isShown()) if (!groupInviteForm->isShown())
{
groupInviteForm->show(createContentDialog(GroupDialog)); groupInviteForm->show(createContentDialog(GroupDialog));
}
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(Widget::None);
} }
@ -775,7 +805,9 @@ void Widget::onTransferClicked()
if (Settings::getInstance().getSeparateWindow()) if (Settings::getInstance().getSeparateWindow())
{ {
if (!filesForm->isShown()) if (!filesForm->isShown())
{
filesForm->show(createContentDialog(TransferDialog)); filesForm->show(createContentDialog(TransferDialog));
}
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(Widget::None);
} }
@ -794,11 +826,21 @@ void Widget::confirmExecutableOpen(const QFileInfo &file)
if (dangerousExtensions.contains(file.suffix())) if (dangerousExtensions.contains(file.suffix()))
{ {
if (!GUI::askQuestion(tr("Executable file", "popup title"), tr("You have asked qTox to open an executable file. Executable files can potentially damage your computer. Are you sure want to open this file?", "popup text"), false, true)) bool answer = GUI::askQuestion(
tr("Executable file", "popup title"),
tr("You have asked qTox to open an executable file. "
"Executable files can potentially damage your computer. "
"Are you sure want to open this file?", "popup text"),
false, true);
if (!answer)
{
return; return;
}
// The user wants to run this file, so make it executable and run it // The user wants to run this file, so make it executable and run it
QFile(file.filePath()).setPermissions(file.permissions() | QFile::ExeOwner | QFile::ExeUser | QFile::ExeGroup | QFile::ExeOther); QFile(file.filePath()).setPermissions(file.permissions()
| QFile::ExeOwner | QFile::ExeUser
| QFile::ExeGroup | QFile::ExeOther);
} }
QDesktopServices::openUrl(QUrl::fromLocalFile(file.filePath())); QDesktopServices::openUrl(QUrl::fromLocalFile(file.filePath()));
@ -811,9 +853,13 @@ void Widget::onIconClick(QSystemTrayIcon::ActivationReason reason)
if (isHidden() || isMinimized()) if (isHidden() || isMinimized())
{ {
if (wasMaximized) if (wasMaximized)
{
showMaximized(); showMaximized();
}
else else
{
showNormal(); showNormal();
}
activateWindow(); activateWindow();
} }
@ -830,16 +876,20 @@ void Widget::onIconClick(QSystemTrayIcon::ActivationReason reason)
else if (reason == QSystemTrayIcon::Unknown) else if (reason == QSystemTrayIcon::Unknown)
{ {
if (isHidden()) if (isHidden())
{
forceShow(); forceShow();
} }
} }
}
void Widget::onSettingsClicked() void Widget::onSettingsClicked()
{ {
if (Settings::getInstance().getSeparateWindow()) if (Settings::getInstance().getSeparateWindow())
{ {
if (!settingsWidget->isShown()) if (!settingsWidget->isShown())
{
settingsWidget->show(createContentDialog(SettingDialog)); settingsWidget->show(createContentDialog(SettingDialog));
}
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(Widget::None);
settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg")); settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg"));
@ -858,7 +908,9 @@ void Widget::showProfile() // onAvatarClicked, onUsernameClicked
if (Settings::getInstance().getSeparateWindow()) if (Settings::getInstance().getSeparateWindow())
{ {
if (!profileForm->isShown()) if (!profileForm->isShown())
{
profileForm->show(createContentDialog(ProfileDialog)); profileForm->show(createContentDialog(ProfileDialog));
}
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(Widget::None);
settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg")); settingsWidget->setWindowIcon(QIcon(":/img/icons/qtox.svg"));
@ -877,10 +929,14 @@ void Widget::hideMainForms(GenericChatroomWidget* chatroomWidget)
setActiveToolMenuButton(Widget::None); setActiveToolMenuButton(Widget::None);
if (contentLayout != nullptr) if (contentLayout != nullptr)
{
contentLayout->clear(); contentLayout->clear();
}
if (activeChatroomWidget != nullptr) if (activeChatroomWidget != nullptr)
{
activeChatroomWidget->setAsInactiveChatroom(); activeChatroomWidget->setAsInactiveChatroom();
}
activeChatroomWidget = chatroomWidget; activeChatroomWidget = chatroomWidget;
} }
@ -901,7 +957,7 @@ void Widget::setUsername(const QString& username)
QString sanename = username; QString sanename = username;
sanename.remove(QRegExp("[\\t\\n\\v\\f\\r\\x0000]")); sanename.remove(QRegExp("[\\t\\n\\v\\f\\r\\x0000]"));
nameMention = QRegExp("\\b" + QRegExp::escape(username) + "\\b", Qt::CaseInsensitive); nameMention = QRegExp("\\b" + QRegExp::escape(username) + "\\b", Qt::CaseInsensitive);
sanitizedNameMention = QRegExp("\\b" + QRegExp::escape(sanename) + "\\b", Qt::CaseInsensitive); sanitizedNameMention = nameMention;
} }
void Widget::onStatusMessageChanged(const QString& newStatusMessage) void Widget::onStatusMessageChanged(const QString& newStatusMessage)
@ -926,8 +982,11 @@ void Widget::setStatusMessage(const QString &statusMessage)
void Widget::reloadHistory() void Widget::reloadHistory()
{ {
QDateTime weekAgo = QDateTime::currentDateTime().addDays(-7);
for (auto f : FriendList::getAllFriends()) for (auto f : FriendList::getAllFriends())
f->getChatForm()->loadHistory(QDateTime::currentDateTime().addDays(-7), true); {
f->getChatForm()->loadHistory(weekAgo, true);
}
} }
void Widget::addFriend(int friendId, const QString &userId) void Widget::addFriend(int friendId, const QString &userId)
@ -976,8 +1035,9 @@ void Widget::addFriend(int friendId, const QString &userId)
void Widget::addFriendFailed(const QString&, const QString& errorInfo) void Widget::addFriendFailed(const QString&, const QString& errorInfo)
{ {
QString info = QString(tr("Couldn't request friendship")); QString info = QString(tr("Couldn't request friendship"));
if (!errorInfo.isEmpty()) { if (!errorInfo.isEmpty())
info = info + (QString(": ") + errorInfo); {
info = info + QStringLiteral(": ") + errorInfo;
} }
QMessageBox::critical(0,"Error",info); QMessageBox::critical(0,"Error",info);
@ -993,7 +1053,9 @@ void Widget::onFriendStatusChanged(int friendId, Status status)
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
if (!f) if (!f)
{
return; return;
}
bool isActualChange = f->getStatus() != status; bool isActualChange = f->getStatus() != status;
@ -1025,15 +1087,19 @@ void Widget::onFriendStatusChanged(int friendId, Status status)
switch (f->getStatus()) switch (f->getStatus())
{ {
case Status::Away: case Status::Away:
fStatus = tr("away", "contact status"); break; fStatus = tr("away", "contact status");
break;
case Status::Busy: case Status::Busy:
fStatus = tr("busy", "contact status"); break; fStatus = tr("busy", "contact status");
break;
case Status::Offline: case Status::Offline:
fStatus = tr("offline", "contact status"); fStatus = tr("offline", "contact status");
f->getChatForm()->setFriendTyping(false); // Hide the "is typing" message when a friend goes offline // Hide the "is typing" message when a friend goes offline
f->getChatForm()->setFriendTyping(false);
break;
case Status::Online:
fStatus = tr("online", "contact status");
break; break;
default:
fStatus = tr("online", "contact status"); break;
} }
if (isActualChange) if (isActualChange)
@ -1047,8 +1113,10 @@ void Widget::onFriendStatusChanged(int friendId, Status status)
} }
if (isActualChange && status != Status::Offline) if (isActualChange && status != Status::Offline)
{ // wait a little {
QTimer::singleShot(250, f->getChatForm()->getOfflineMsgEngine(), SLOT(deliverOfflineMsgs())); // wait a little
OfflineMsgEngine* ome = f->getChatForm()->getOfflineMsgEngine();
QTimer::singleShot(250, ome, SLOT(deliverOfflineMsgs()));
} }
} }
@ -1056,10 +1124,14 @@ void Widget::onFriendStatusMessageChanged(int friendId, const QString& message)
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
if (!f) if (!f)
{
return; return;
}
QString str = message; str.replace('\n', ' '); QString str = message;
str.remove('\r'); str.remove(QChar((char)0)); // null terminator... str.replace('\n', ' ');
str.remove('\r');
str.remove(QChar((char)0)); // null terminator...
f->setStatusMessage(str); f->setStatusMessage(str);
ContentDialog::updateFriendStatusMessage(friendId, message); ContentDialog::updateFriendStatusMessage(friendId, message);
@ -1069,25 +1141,24 @@ void Widget::onFriendUsernameChanged(int friendId, const QString& username)
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
if (!f) if (!f)
{
return; return;
}
QString str = username; str.replace('\n', ' '); QString str = username;
str.remove('\r'); str.remove(QChar((char)0)); // null terminator... str.replace('\n', ' ');
str.remove('\r');
str.remove(QChar((char)0)); // null terminator...
f->setName(str); f->setName(str);
} }
void Widget::onFriendDisplayChanged(FriendWidget *friendWidget, Status s) void Widget::onFriendDisplayChanged(FriendWidget *friendWidget, Status s)
{ {
contactListWidget->moveWidget(friendWidget, s); contactListWidget->moveWidget(friendWidget, s);
int filter = getFilterCriteria(); int criteria = getFilterCriteria();
switch (s) bool filter = s == Status::Offline ?
{ filterOffline(criteria) : filterOnline(criteria);
case Status::Offline: friendWidget->searchName(ui->searchContactText->text(), filter);
friendWidget->searchName(ui->searchContactText->text(), filterOffline(filter));
default:
friendWidget->searchName(ui->searchContactText->text(), filterOnline(filter));
}
} }
void Widget::onChatroomWidgetClicked(GenericChatroomWidget *widget, bool group) void Widget::onChatroomWidgetClicked(GenericChatroomWidget *widget, bool group)
@ -1096,17 +1167,23 @@ void Widget::onChatroomWidgetClicked(GenericChatroomWidget *widget, bool group)
widget->updateStatusLight(); widget->updateStatusLight();
if (widget->chatFormIsSet(true) && !group) if (widget->chatFormIsSet(true) && !group)
{
return; return;
}
if (Settings::getInstance().getSeparateWindow() || group) if (Settings::getInstance().getSeparateWindow() || group)
{ {
ContentDialog* dialog = nullptr; ContentDialog* dialog = nullptr;
if (!Settings::getInstance().getDontGroupWindows() && !group) if (!Settings::getInstance().getDontGroupWindows() && !group)
{
dialog = ContentDialog::current(); dialog = ContentDialog::current();
}
if (dialog == nullptr) if (dialog == nullptr)
{
dialog = createContentDialog(); dialog = createContentDialog();
}
dialog->show(); dialog->show();
Friend* frnd = widget->getFriend(); Friend* frnd = widget->getFriend();
@ -1137,13 +1214,25 @@ void Widget::onFriendMessageReceived(int friendId, const QString& message, bool
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
if (!f) if (!f)
{
return; return;
}
QDateTime timestamp = QDateTime::currentDateTime(); QDateTime timestamp = QDateTime::currentDateTime();
Profile* profile = Nexus::getProfile(); Profile* profile = Nexus::getProfile();
if (profile->isHistoryEnabled()) if (profile->isHistoryEnabled())
profile->getHistory()->addNewMessage(f->getToxId().publicKey, isAction ? ChatForm::ACTION_PREFIX + f->getDisplayedName() + " " + message : message, {
f->getToxId().publicKey, timestamp, true, f->getDisplayedName()); QString publicKey = f->getToxId().publicKey;
QString name = f->getDisplayedName();
QString text = message;
if (isAction)
{
text = ChatForm::ACTION_PREFIX + f->getDisplayedName() + " " + text;
}
profile->getHistory()->addNewMessage(publicKey, text,
publicKey, timestamp,
true, name);
}
newFriendMessageAlert(friendId); newFriendMessageAlert(friendId);
} }
@ -1152,7 +1241,9 @@ void Widget::onReceiptRecieved(int friendId, int receipt)
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
if (!f) if (!f)
{
return; return;
}
f->getChatForm()->getOfflineMsgEngine()->dischargeReceipt(receipt); f->getChatForm()->getOfflineMsgEngine()->dischargeReceipt(receipt);
} }
@ -1172,25 +1263,40 @@ void Widget::addFriendDialog(Friend *frnd, ContentDialog *dialog)
friendWidget->setStatusMsg(widget->getStatusMsg()); friendWidget->setStatusMsg(widget->getStatusMsg());
connect(friendWidget, SIGNAL(removeFriend(int)), this, SLOT(removeFriend(int))); connect(friendWidget, SIGNAL(removeFriend(int)),
connect(friendWidget, SIGNAL(copyFriendIdToClipboard(int)), this, SLOT(copyFriendIdToClipboard(int))); this, SLOT(removeFriend(int)));
connect(friendWidget, &FriendWidget::copyFriendIdToClipboard,
this, &Widget::copyFriendIdToClipboard);
connect(Core::getInstance(), &Core::friendAvatarChanged, friendWidget, &FriendWidget::onAvatarChange); Core* core = Core::getInstance();
connect(Core::getInstance(), &Core::friendAvatarRemoved, friendWidget, &FriendWidget::onAvatarRemoved); connect(core, &Core::friendAvatarChanged,
friendWidget, &FriendWidget::onAvatarChange);
connect(core, &Core::friendAvatarRemoved,
friendWidget, &FriendWidget::onAvatarRemoved);
QPixmap avatar = Nexus::getProfile()->loadAvatar(frnd->getToxId().toString()); QPixmap avatar = Nexus::getProfile()->loadAvatar(frnd->getToxId().toString());
if (!avatar.isNull()) if (!avatar.isNull())
{
friendWidget->onAvatarChange(frnd->getFriendID(), avatar); friendWidget->onAvatarChange(frnd->getFriendID(), avatar);
} }
}
void Widget::addGroupDialog(Group *group, ContentDialog *dialog) void Widget::addGroupDialog(Group *group, ContentDialog *dialog)
{ {
if (!ContentDialog::getGroupDialog(group->getGroupId()) && !Settings::getInstance().getSeparateWindow() && activeChatroomWidget == group->getGroupWidget()) int groupId = group->getGroupId();
ContentDialog* groupDialog = ContentDialog::getGroupDialog(groupId);
bool separated = Settings::getInstance().getSeparateWindow();
bool isCurrentWindow = activeChatroomWidget == group->getGroupWidget();
if (!groupDialog && !separated && isCurrentWindow)
{
onAddClicked(); onAddClicked();
}
GroupWidget* groupWidget = dialog->addGroup(group->getGroupId(), group->getName()); GroupWidget* groupWidget = dialog->addGroup(groupId, group->getName());
connect(groupWidget, SIGNAL(removeGroup(int)), this, SLOT(removeGroup(int))); connect(groupWidget, SIGNAL(removeGroup(int)),
connect(groupWidget, SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), group->getChatForm(), SLOT(focusInput())); this, SLOT(removeGroup(int)));
connect(groupWidget, &GroupWidget::chatroomWidgetClicked,
group->getChatForm(), &ChatForm::focusInput);
} }
bool Widget::newFriendMessageAlert(int friendId, bool sound) bool Widget::newFriendMessageAlert(int friendId, bool sound)
@ -1217,8 +1323,10 @@ bool Widget::newFriendMessageAlert(int friendId, bool sound)
{ {
contentDialog = ContentDialog::current(); contentDialog = ContentDialog::current();
if (!contentDialog) if (!contentDialog)
{
contentDialog = createContentDialog(); contentDialog = createContentDialog();
} }
}
addFriendDialog(f, contentDialog); addFriendDialog(f, contentDialog);
currentWindow = contentDialog->window(); currentWindow = contentDialog->window();
@ -1275,8 +1383,11 @@ bool Widget::newGroupMessageAlert(int groupId, bool notify)
hasActive = g->getGroupWidget() == activeChatroomWidget; hasActive = g->getGroupWidget() == activeChatroomWidget;
} }
if (newMessageAlert(currentWindow, hasActive, true, notify)) if (!newMessageAlert(currentWindow, hasActive, true, notify))
{ {
return false;
}
g->setEventFlag(true); g->setEventFlag(true);
g->getGroupWidget()->updateStatusLight(); g->getGroupWidget()->updateStatusLight();
@ -1295,9 +1406,6 @@ bool Widget::newGroupMessageAlert(int groupId, bool notify)
return true; return true;
} }
return false;
}
QString Widget::fromDialogType(DialogType type) QString Widget::fromDialogType(DialogType type)
{ {
switch (type) switch (type)
@ -1322,7 +1430,9 @@ bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool sound,
bool inactiveWindow = isMinimized() || !currentWindow->isActiveWindow(); bool inactiveWindow = isMinimized() || !currentWindow->isActiveWindow();
if (!inactiveWindow && isActive) if (!inactiveWindow && isActive)
{
return false; return false;
}
if (notify) if (notify)
{ {
@ -1336,15 +1446,20 @@ bool Widget::newMessageAlert(QWidget* currentWindow, bool isActive, bool sound,
{ {
currentWindow->show(); currentWindow->show();
if (inactiveWindow && Settings::getInstance().getShowInFront()) if (inactiveWindow && Settings::getInstance().getShowInFront())
{
currentWindow->activateWindow(); currentWindow->activateWindow();
} }
}
bool isBusy = Nexus::getCore()->getStatus() == Status::Busy; bool isBusy = Nexus::getCore()->getStatus() == Status::Busy;
bool busySound = Settings::getInstance().getBusySound(); bool busySound = Settings::getInstance().getBusySound();
bool notifySound = Settings::getInstance().getNotifySound(); bool notifySound = Settings::getInstance().getNotifySound();
if (notifySound && sound && (!isBusy || busySound)) if (notifySound && sound && (!isBusy || busySound))
Audio::getInstance().playMono16Sound(Audio::getSound(Audio::Sound::NewMessage)); {
QString soundPath = Audio::getSound(Audio::Sound::NewMessage);
Audio::getInstance().playMono16Sound(soundPath);
}
} }
return true; return true;
@ -1403,14 +1518,18 @@ void Widget::removeFriend(Friend* f, bool fake)
ContentDialog* lastDialog = ContentDialog::getFriendDialog(f->getFriendID()); ContentDialog* lastDialog = ContentDialog::getFriendDialog(f->getFriendID());
if (lastDialog != nullptr) if (lastDialog != nullptr)
{
lastDialog->removeFriend(f->getFriendID()); lastDialog->removeFriend(f->getFriendID());
}
FriendList::removeFriend(f->getFriendID(), fake); FriendList::removeFriend(f->getFriendID(), fake);
Nexus::getCore()->removeFriend(f->getFriendID(), fake); Nexus::getCore()->removeFriend(f->getFriendID(), fake);
delete f; delete f;
if (contentLayout != nullptr && contentLayout->mainHead->layout()->isEmpty()) if (contentLayout != nullptr && contentLayout->mainHead->layout()->isEmpty())
{
onAddClicked(); onAddClicked();
}
contactListWidget->reDraw(); contactListWidget->reDraw();
} }
@ -1426,12 +1545,16 @@ void Widget::clearContactsList()
QList<Friend*> friends = FriendList::getAllFriends(); QList<Friend*> friends = FriendList::getAllFriends();
for (Friend* f : friends) for (Friend* f : friends)
{
removeFriend(f, true); removeFriend(f, true);
}
QList<Group*> groups = GroupList::getAllGroups(); QList<Group*> groups = GroupList::getAllGroups();
for (Group* g : groups) for (Group* g : groups)
{
removeGroup(g, true); removeGroup(g, true);
} }
}
void Widget::onDialogShown(GenericChatroomWidget* widget) void Widget::onDialogShown(GenericChatroomWidget* widget)
{ {
@ -1607,8 +1730,10 @@ void Widget::onGroupNamelistChanged(int groupnumber, int peernumber, uint8_t Cha
qDebug() << "onGroupNamelistChanged: Group "<<groupnumber<<" not found, creating it"; qDebug() << "onGroupNamelistChanged: Group "<<groupnumber<<" not found, creating it";
g = createGroup(groupnumber); g = createGroup(groupnumber);
if (!g) if (!g)
{
return; return;
} }
}
TOX_CONFERENCE_STATE_CHANGE change = static_cast<TOX_CONFERENCE_STATE_CHANGE>(Change); TOX_CONFERENCE_STATE_CHANGE change = static_cast<TOX_CONFERENCE_STATE_CHANGE>(Change);
if (change == TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN) if (change == TOX_CONFERENCE_STATE_CHANGE_PEER_JOIN)
@ -1639,10 +1764,16 @@ void Widget::onGroupTitleChanged(int groupnumber, const QString& author, const Q
{ {
Group* g = GroupList::findGroup(groupnumber); Group* g = GroupList::findGroup(groupnumber);
if (!g) if (!g)
{
return; return;
}
if (!author.isEmpty()) if (!author.isEmpty())
g->getChatForm()->addSystemInfoMessage(tr("%1 has set the title to %2").arg(author, title), ChatMessage::INFO, QDateTime::currentDateTime()); {
QString message = tr("%1 has set the title to %2").arg(author, title);
QDateTime curTime = QDateTime::currentDateTime();
g->getChatForm()->addSystemInfoMessage(message, ChatMessage::INFO, curTime);
}
contactListWidget->renameGroupWidget(g->getGroupWidget(), title); contactListWidget->renameGroupWidget(g->getGroupWidget(), title);
g->setName(title); g->setName(title);
@ -1654,7 +1785,9 @@ void Widget::onGroupPeerAudioPlaying(int groupnumber, int peernumber)
{ {
Group* g = GroupList::findGroup(groupnumber); Group* g = GroupList::findGroup(groupnumber);
if (!g) if (!g)
{
return; return;
}
g->getChatForm()->peerAudioPlaying(peernumber); g->getChatForm()->peerAudioPlaying(peernumber);
} }
@ -1667,17 +1800,23 @@ void Widget::removeGroup(Group* g, bool fake)
activeChatroomWidget = nullptr; activeChatroomWidget = nullptr;
onAddClicked(); onAddClicked();
} }
GroupList::removeGroup(g->getGroupId(), fake);
ContentDialog* contentDialog = ContentDialog::getGroupDialog(g->getGroupId()); int groupId = g->getGroupId();
GroupList::removeGroup(groupId, fake);
ContentDialog* contentDialog = ContentDialog::getGroupDialog(groupId);
if (contentDialog != nullptr) if (contentDialog != nullptr)
contentDialog->removeGroup(g->getGroupId()); {
contentDialog->removeGroup(groupId);
}
Nexus::getCore()->removeGroup(g->getGroupId(), fake); Nexus::getCore()->removeGroup(groupId, fake);
delete g; delete g;
if (contentLayout != nullptr && contentLayout->mainHead->layout()->isEmpty()) if (contentLayout != nullptr && contentLayout->mainHead->layout()->isEmpty())
{
onAddClicked(); onAddClicked();
}
contactListWidget->reDraw(); contactListWidget->reDraw();
} }
@ -1737,12 +1876,16 @@ void Widget::onEmptyGroupCreated(int groupId)
{ {
Group* group = createGroup(groupId); Group* group = createGroup(groupId);
if (!group) if (!group)
{
return; return;
}
// Only rename group if groups are visible. // Only rename group if groups are visible.
if (Widget::getInstance()->groupsVisible()) if (Widget::getInstance()->groupsVisible())
{
group->getGroupWidget()->editName(); group->getGroupWidget()->editName();
} }
}
/** /**
* @brief Used to reset the blinking icon. * @brief Used to reset the blinking icon.
@ -1889,7 +2032,9 @@ void Widget::onTryCreateTrayIcon()
void Widget::setStatusOnline() void Widget::setStatusOnline()
{ {
if (!ui->statusButton->isEnabled()) if (!ui->statusButton->isEnabled())
{
return; return;
}
Nexus::getCore()->setStatus(Status::Online); Nexus::getCore()->setStatus(Status::Online);
} }
@ -1897,7 +2042,9 @@ void Widget::setStatusOnline()
void Widget::setStatusAway() void Widget::setStatusAway()
{ {
if (!ui->statusButton->isEnabled()) if (!ui->statusButton->isEnabled())
{
return; return;
}
Nexus::getCore()->setStatus(Status::Away); Nexus::getCore()->setStatus(Status::Away);
} }
@ -1905,7 +2052,9 @@ void Widget::setStatusAway()
void Widget::setStatusBusy() void Widget::setStatusBusy()
{ {
if (!ui->statusButton->isEnabled()) if (!ui->statusButton->isEnabled())
{
return; return;
}
Nexus::getCore()->setStatus(Status::Busy); Nexus::getCore()->setStatus(Status::Busy);
} }
@ -1916,25 +2065,35 @@ void Widget::onMessageSendResult(uint32_t friendId, const QString& message, int
Q_UNUSED(messageId) Q_UNUSED(messageId)
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
if (!f) if (!f)
{
return; return;
} }
}
void Widget::onGroupSendResult(int groupId, const QString& message, int result) void Widget::onGroupSendResult(int groupId, const QString& message, int result)
{ {
Q_UNUSED(message) Q_UNUSED(message)
Group* g = GroupList::findGroup(groupId); Group* g = GroupList::findGroup(groupId);
if (!g) if (!g)
{
return; return;
}
if (result == -1) if (result == -1)
g->getChatForm()->addSystemInfoMessage(tr("Message failed to send"), ChatMessage::INFO, QDateTime::currentDateTime()); {
QString message = tr("Message failed to send");
QDateTime curTime = QDateTime::currentDateTime();
g->getChatForm()->addSystemInfoMessage(message, ChatMessage::INFO, curTime);
}
} }
void Widget::onFriendTypingChanged(int friendId, bool isTyping) void Widget::onFriendTypingChanged(int friendId, bool isTyping)
{ {
Friend* f = FriendList::findFriend(friendId); Friend* f = FriendList::findFriend(friendId);
if (!f) if (!f)
{
return; return;
}
f->getChatForm()->setFriendTyping(isTyping); f->getChatForm()->setFriendTyping(isTyping);
} }
@ -1942,8 +2101,10 @@ void Widget::onFriendTypingChanged(int friendId, bool isTyping)
void Widget::onSetShowSystemTray(bool newValue) void Widget::onSetShowSystemTray(bool newValue)
{ {
if (icon) if (icon)
{
icon->setVisible(newValue); icon->setVisible(newValue);
} }
}
void Widget::saveWindowGeometry() void Widget::saveWindowGeometry()
{ {
@ -2010,7 +2171,9 @@ void Widget::processOfflineMsgs()
{ {
QList<Friend*> frnds = FriendList::getAllFriends(); QList<Friend*> frnds = FriendList::getAllFriends();
for (Friend *f : frnds) for (Friend *f : frnds)
{
f->getChatForm()->getOfflineMsgEngine()->deliverOfflineMsgs(); f->getChatForm()->getOfflineMsgEngine()->deliverOfflineMsgs();
}
OfflineMsgEngine::globalMutex.unlock(); OfflineMsgEngine::globalMutex.unlock();
} }
@ -2020,8 +2183,10 @@ void Widget::clearAllReceipts()
{ {
QList<Friend*> frnds = FriendList::getAllFriends(); QList<Friend*> frnds = FriendList::getAllFriends();
for (Friend *f : frnds) for (Friend *f : frnds)
{
f->getChatForm()->getOfflineMsgEngine()->removeAllReceipts(); f->getChatForm()->getOfflineMsgEngine()->removeAllReceipts();
} }
}
void Widget::reloadTheme() void Widget::reloadTheme()
{ {
@ -2080,8 +2245,8 @@ inline QIcon Widget::prepareIcon(QString path, uint32_t w, uint32_t h)
{ {
desktop = getenv("DESKTOP_SESSION"); desktop = getenv("DESKTOP_SESSION");
} }
desktop = desktop.toLower();
desktop = desktop.toLower();
if (desktop == "xfce" || desktop.contains("gnome") || desktop == "mate" || desktop == "x-cinnamon") if (desktop == "xfce" || desktop.contains("gnome") || desktop == "mate" || desktop == "x-cinnamon")
{ {
if (w > 0 && h > 0) if (w > 0 && h > 0)
@ -2152,9 +2317,13 @@ void Widget::changeDisplayMode()
filterDisplayGroup->setEnabled(false); filterDisplayGroup->setEnabled(false);
if (filterDisplayGroup->checkedAction() == filterDisplayActivity) if (filterDisplayGroup->checkedAction() == filterDisplayActivity)
{
contactListWidget->setMode(FriendListWidget::Activity); contactListWidget->setMode(FriendListWidget::Activity);
}
else if (filterDisplayGroup->checkedAction() == filterDisplayName) else if (filterDisplayGroup->checkedAction() == filterDisplayName)
{
contactListWidget->setMode(FriendListWidget::Name); contactListWidget->setMode(FriendListWidget::Name);
}
searchContacts(); searchContacts();
filterDisplayGroup->setEnabled(true); filterDisplayGroup->setEnabled(true);
@ -2164,7 +2333,10 @@ void Widget::changeDisplayMode()
void Widget::updateFilterText() void Widget::updateFilterText()
{ {
ui->searchContactFilterBox->setText(filterDisplayGroup->checkedAction()->text() + QStringLiteral(" | ") + filterGroup->checkedAction()->text()); QString action = filterDisplayGroup->checkedAction()->text();
QString text = filterGroup->checkedAction()->text();
text = action + QStringLiteral(" | ") + text;
ui->searchContactFilterBox->setText(text);
} }
int Widget::getFilterCriteria() const int Widget::getFilterCriteria() const
@ -2186,7 +2358,8 @@ int Widget::getFilterCriteria() const
void Widget::searchCircle(CircleWidget *circleWidget) void Widget::searchCircle(CircleWidget *circleWidget)
{ {
int filter = getFilterCriteria(); int filter = getFilterCriteria();
circleWidget->search(ui->searchContactText->text(), true, filterOnline(filter), filterOffline(filter)); QString text = ui->searchContactText->text();
circleWidget->search(text, true, filterOnline(filter), filterOffline(filter));
} }
void Widget::searchItem(GenericChatItemWidget *chatItem, GenericChatItemWidget::ItemType type) void Widget::searchItem(GenericChatItemWidget *chatItem, GenericChatItemWidget::ItemType type)
@ -2219,10 +2392,14 @@ void Widget::friendListContextMenu(const QPoint &pos)
QAction *chosenAction = menu.exec(ui->friendList->mapToGlobal(pos)); QAction *chosenAction = menu.exec(ui->friendList->mapToGlobal(pos));
if (chosenAction == addCircleAction) if (chosenAction == addCircleAction)
{
contactListWidget->addCircleWidget(); contactListWidget->addCircleWidget();
}
else if (chosenAction == createGroupAction) else if (chosenAction == createGroupAction)
{
Nexus::getCore()->createGroup(); Nexus::getCore()->createGroup();
} }
}
void Widget::friendRequestsUpdate() void Widget::friendRequestsUpdate()
{ {
@ -2247,8 +2424,10 @@ void Widget::friendRequestsUpdate()
} }
if (friendRequestsButton) if (friendRequestsButton)
{
friendRequestsButton->setText(tr("%n New Friend Request(s)", "", unreadFriendRequests)); friendRequestsButton->setText(tr("%n New Friend Request(s)", "", unreadFriendRequests));
} }
}
void Widget::groupInvitesUpdate() void Widget::groupInvitesUpdate()
{ {
@ -2263,12 +2442,15 @@ void Widget::groupInvitesUpdate()
groupInvitesButton->setObjectName("green"); groupInvitesButton->setObjectName("green");
ui->statusLayout->insertWidget(2, groupInvitesButton); ui->statusLayout->insertWidget(2, groupInvitesButton);
connect(groupInvitesButton, &QPushButton::released, this, &Widget::onGroupClicked); connect(groupInvitesButton, &QPushButton::released,
this, &Widget::onGroupClicked);
} }
if (groupInvitesButton) if (groupInvitesButton)
{
groupInvitesButton->setText(tr("%n New Group Invite(s)", "", unreadGroupInvites)); groupInvitesButton->setText(tr("%n New Group Invite(s)", "", unreadGroupInvites));
} }
}
void Widget::groupInvitesClear() void Widget::groupInvitesClear()
{ {
@ -2342,8 +2524,12 @@ void Widget::focusChatInput()
if (activeChatroomWidget) if (activeChatroomWidget)
{ {
if (Friend* f = activeChatroomWidget->getFriend()) if (Friend* f = activeChatroomWidget->getFriend())
{
f->getChatForm()->focusInput(); f->getChatForm()->focusInput();
}
else if (Group* g = activeChatroomWidget->getGroup()) else if (Group* g = activeChatroomWidget->getGroup())
{
g->getChatForm()->focusInput(); g->getChatForm()->focusInput();
} }
} }
}