void FriendListWidget::moveWidget(FriendWidget* w, Status s, bool add) { if (mode == Name) { int circleId = Settings::getInstance().getFriendCircleID(FriendList::findFriend(w->friendId)->getToxId()); CircleWidget* circleWidget = CircleWidget::getFromID(circleId); if (circleWidget == nullptr || add) { if (circleId != -1) Settings::getInstance().setFriendCircleID(FriendList::findFriend(w->friendId)->getToxId(), -1); listLayout->addFriendWidget(w, s); return; } circleWidget->addFriendWidget(w, s); } else { Friend* contact = FriendList::findFriend(w->friendId); QDate activityDate = getDateFriend(contact); Time time = getTime(activityDate); CategoryWidget* categoryWidget = dynamic_cast<CategoryWidget*>(activityLayout->itemAt(time)->widget()); categoryWidget->addFriendWidget(contact->getFriendWidget(), contact->getStatus()); categoryWidget->show(); } }
void FriendListWidget::moveWidget(FriendWidget* widget, Status s, bool add) { if (mode == Name) { Friend* f = FriendList::findFriend(widget->friendId); int circleId = Settings::getInstance().getFriendCircleID(f->getPublicKey()); CircleWidget* circleWidget = CircleWidget::getFromID(circleId); if (circleWidget == nullptr || add) { if (circleId != -1) Settings::getInstance().setFriendCircleID(f->getPublicKey(), -1); listLayout->addFriendWidget(widget, s); return; } circleWidget->addFriendWidget(widget, s); } else { Friend* contact = FriendList::findFriend(widget->friendId); QDate activityDate = getDateFriend(contact); int time = static_cast<int>(getTime(activityDate)); QWidget* w = activityLayout->itemAt(time)->widget(); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w); categoryWidget->addFriendWidget(widget, contact->getStatus()); categoryWidget->show(); } }
void FriendListWidget::updateActivityDate(const QDate& date) { if (mode != Activity) return; CategoryWidget* categoryWidget = static_cast<CategoryWidget*>(activityLayout->itemAt(getTime(date))->widget()); categoryWidget->updateStatus(); categoryWidget->setVisible(categoryWidget->hasChatrooms()); }
void FriendListWidget::removeFriendWidget(FriendWidget* w) { Friend* contact = FriendList::findFriend(w->friendId); if (mode == Activity) { QDate activityDate = getDateFriend(contact); int time = static_cast<int>(getTime(activityDate)); QWidget* widget = activityLayout->itemAt(time)->widget(); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); categoryWidget->removeFriendWidget(w, contact->getStatus()); categoryWidget->setVisible(categoryWidget->hasChatrooms()); } else { int id = Settings::getInstance().getFriendCircleID(contact->getPublicKey()); CircleWidget* circleWidget = CircleWidget::getFromID(id); if (circleWidget != nullptr) { circleWidget->removeFriendWidget(w, contact->getStatus()); Widget::getInstance()->searchCircle(circleWidget); } } }
void FriendListWidget::moveFriends(QLayout* layout) { for (int i = 0; i < layout->count(); i++) { QWidget* widget = layout->itemAt(i)->widget(); FriendWidget* friendWidget = qobject_cast<FriendWidget*>(widget); CircleWidget* circleWidget = qobject_cast<CircleWidget*>(widget); if (circleWidget) { circleWidget->moveFriendWidgets(this); } else if (friendWidget) { const Friend* contact = friendWidget->getFriend(); QDate activityDate = getDateFriend(contact); int time = static_cast<int>(getTime(activityDate)); QWidget* w = activityLayout->itemAt(time)->widget(); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(w); categoryWidget->addFriendWidget(friendWidget, contact->getStatus()); } } }
void FriendListWidget::searchChatrooms(const QString& searchString, bool hideOnline, bool hideOffline, bool hideGroups) { groupLayout.search(searchString, hideGroups); listLayout->searchChatrooms(searchString, hideOnline, hideOffline); if (circleLayout != nullptr) { for (int i = 0; i != circleLayout->getLayout()->count(); ++i) { CircleWidget* circleWidget = static_cast<CircleWidget*>(circleLayout->getLayout()->itemAt(i)->widget()); circleWidget->search(searchString, true, hideOnline, hideOffline); } } else if (activityLayout != nullptr) { for (int i = 0; i != activityLayout->count(); ++i) { CategoryWidget* categoryWidget = static_cast<CategoryWidget*>(activityLayout->itemAt(i)->widget()); categoryWidget->search(searchString, true, hideOnline, hideOffline); categoryWidget->setVisible(categoryWidget->hasChatrooms()); } } }
void FriendListWidget::onFriendWidgetRenamed(FriendWidget* friendWidget) { const Friend* contact = friendWidget->getFriend(); auto status = contact->getStatus(); if (mode == Activity) { QDate activityDate = getDateFriend(contact); int time = static_cast<int>(getTime(activityDate)); QWidget* widget = activityLayout->itemAt(time)->widget(); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); categoryWidget->removeFriendWidget(friendWidget, status); categoryWidget->addFriendWidget(friendWidget, status); } else { int id = Settings::getInstance().getFriendCircleID(contact->getPublicKey()); CircleWidget* circleWidget = CircleWidget::getFromID(id); if (circleWidget != nullptr) { circleWidget->removeFriendWidget(friendWidget, status); circleWidget->addFriendWidget(friendWidget, status); Widget::getInstance()->searchCircle(circleWidget); } else { listLayout->removeFriendWidget(friendWidget, status); listLayout->addFriendWidget(friendWidget, status); } } }
void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget, bool forward) { if (activeChatroomWidget == nullptr) return; int index = -1; FriendWidget* friendWidget = dynamic_cast<FriendWidget*>(activeChatroomWidget); if (mode == Activity) { if (friendWidget == nullptr) return; QDate activityDate = getDateFriend(FriendList::findFriend(friendWidget->friendId)); index = getTime(activityDate); CategoryWidget* categoryWidget = dynamic_cast<CategoryWidget*>(activityLayout->itemAt(index)->widget()); if (categoryWidget == nullptr || categoryWidget->cycleContacts(friendWidget, forward)) return; index += forward ? 1 : -1; for (;;) { // Bounds checking. if (index < Today) { index = Never; continue; } else if (index > Never) { index = Today; continue; } CategoryWidget* categoryWidget = dynamic_cast<CategoryWidget*>(activityLayout->itemAt(index)->widget()); if (categoryWidget != nullptr) { if (!categoryWidget->cycleContacts(forward)) { // Skip empty or finished categories. index += forward ? 1 : -1; continue; } } break; } return; } QLayout* currentLayout = nullptr; CircleWidget* circleWidget = nullptr; if (friendWidget != nullptr) { circleWidget = CircleWidget::getFromID(Settings::getInstance().getFriendCircleID(FriendList::findFriend(friendWidget->friendId)->getToxId())); if (circleWidget != nullptr) { if (circleWidget->cycleContacts(friendWidget, forward)) return; index = circleLayout->indexOfSortedWidget(circleWidget); currentLayout = circleLayout->getLayout(); } else { currentLayout = listLayout->getLayoutOnline(); index = listLayout->indexOfFriendWidget(friendWidget, true); if (index == -1) { currentLayout = listLayout->getLayoutOffline(); index = listLayout->indexOfFriendWidget(friendWidget, false); } } } else { GroupWidget* groupWidget = dynamic_cast<GroupWidget*>(activeChatroomWidget); if (groupWidget != nullptr) { currentLayout = groupLayout.getLayout(); index = groupLayout.indexOfSortedWidget(groupWidget); } else { return; }; } index += forward ? 1 : -1; for (;;) { // Bounds checking. if (index < 0) { currentLayout = nextLayout(currentLayout, forward); index = currentLayout->count() - 1; continue; } else if (index >= currentLayout->count()) { currentLayout = nextLayout(currentLayout, forward); index = 0; continue; } // Go to the actual next index. if (currentLayout == listLayout->getLayoutOnline() || currentLayout == listLayout->getLayoutOffline() || currentLayout == groupLayout.getLayout()) { GenericChatroomWidget* chatWidget = dynamic_cast<GenericChatroomWidget*>(currentLayout->itemAt(index)->widget()); if (chatWidget != nullptr) emit chatWidget->chatroomWidgetClicked(chatWidget); return; } else if (currentLayout == circleLayout->getLayout()) { circleWidget = dynamic_cast<CircleWidget*>(currentLayout->itemAt(index)->widget()); if (circleWidget != nullptr) { if (!circleWidget->cycleContacts(forward)) { // Skip empty or finished circles. index += forward ? 1 : -1; continue; } } return; } else { return; } } }
void FriendListWidget::setMode(Mode mode) { if (this->mode == mode) return; this->mode = mode; if (mode == Name) { circleLayout = new GenericChatItemLayout; circleLayout->getLayout()->setSpacing(0); circleLayout->getLayout()->setMargin(0); for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) { addCircleWidget(i); CircleWidget::getFromID(i)->setVisible(false); } // Only display circles once all created to avoid artifacts. for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) CircleWidget::getFromID(i)->setVisible(true); QList<Friend*> friendList = FriendList::getAllFriends(); for (Friend* contact : friendList) { int circleId = Settings::getInstance().getFriendCircleID(contact->getToxId()); addFriendWidget(contact->getFriendWidget(), contact->getStatus(), circleId); } listLayout->addLayout(listLayout->getLayoutOnline()); listLayout->addLayout(listLayout->getLayoutOffline()); listLayout->addLayout(circleLayout->getLayout()); onGroupchatPositionChanged(groupsOnTop); if (activityLayout != nullptr) { QLayoutItem* item; while ((item = activityLayout->takeAt(0)) != nullptr) { delete item->widget(); delete item; } delete activityLayout; activityLayout = nullptr; } reDraw(); } else if (mode == Activity) { activityLayout = new QVBoxLayout(); CategoryWidget* categoryToday = new CategoryWidget(this); categoryToday->setObjectName("Todddd"); categoryToday->setName(tr("Today", "Category for sorting friends by activity")); activityLayout->addWidget(categoryToday); CategoryWidget* categoryYesterday = new CategoryWidget(this); categoryYesterday->setName(tr("Yesterday", "Category for sorting friends by activity")); activityLayout->addWidget(categoryYesterday); CategoryWidget* categoryLastWeek = new CategoryWidget(this); categoryLastWeek->setName(tr("Last 7 days", "Category for sorting friends by activity")); activityLayout->addWidget(categoryLastWeek); QDate currentDate = QDate::currentDate(); //if (last7DaysWasLastMonth()) { CategoryWidget* categoryThisMonth = new CategoryWidget(this); categoryThisMonth->setName(tr("This month", "Category for sorting friends by activity")); activityLayout->addWidget(categoryThisMonth); categoryThisMonth->setVisible(last7DaysWasLastMonth()); if (categoryThisMonth->isVisible()) currentDate = currentDate.addMonths(-1); } CategoryWidget* categoryLast1Month = new CategoryWidget(this); categoryLast1Month->setName(QDate::longMonthName(currentDate.month())); activityLayout->addWidget(categoryLast1Month); currentDate = currentDate.addMonths(-1); CategoryWidget* categoryLast2Month = new CategoryWidget(this); categoryLast2Month->setName(QDate::longMonthName(currentDate.month())); activityLayout->addWidget(categoryLast2Month); currentDate = currentDate.addMonths(-1); CategoryWidget* categoryLast3Month = new CategoryWidget(this); categoryLast3Month->setName(QDate::longMonthName(currentDate.month())); activityLayout->addWidget(categoryLast3Month); currentDate = currentDate.addMonths(-1); CategoryWidget* categoryLast4Month = new CategoryWidget(this); categoryLast4Month->setName(QDate::longMonthName(currentDate.month())); activityLayout->addWidget(categoryLast4Month); currentDate = currentDate.addMonths(-1); CategoryWidget* categoryLast5Month = new CategoryWidget(this); categoryLast5Month->setName(QDate::longMonthName(currentDate.month())); activityLayout->addWidget(categoryLast5Month); CategoryWidget* categoryOlder = new CategoryWidget(this); categoryOlder->setName(tr("Older than 6 Months", "Category for sorting friends by activity")); activityLayout->addWidget(categoryOlder); CategoryWidget* categoryNever = new CategoryWidget(this); categoryNever->setName(tr("Unknown", "Category for sorting friends by activity")); activityLayout->addWidget(categoryNever); QList<Friend*> friendList = FriendList::getAllFriends(); for (Friend* contact : friendList) { QDate activityDate = getDateFriend(contact); Time time = getTime(activityDate); CategoryWidget* categoryWidget = dynamic_cast<CategoryWidget*>(activityLayout->itemAt(time)->widget()); categoryWidget->addFriendWidget(contact->getFriendWidget(), contact->getStatus()); } for (int i = 0; i < activityLayout->count(); ++i) { CategoryWidget* categoryWidget = dynamic_cast<CategoryWidget*>(activityLayout->itemAt(i)->widget()); categoryWidget->setVisible(categoryWidget->hasChatrooms()); } listLayout->removeItem(listLayout->getLayoutOnline()); listLayout->removeItem(listLayout->getLayoutOffline()); listLayout->removeItem(circleLayout->getLayout()); listLayout->insertLayout(1, activityLayout); if (circleLayout != nullptr) { QLayoutItem* item; while ((item = circleLayout->getLayout()->takeAt(0)) != nullptr) { delete item->widget(); delete item; } delete circleLayout; circleLayout = nullptr; } reDraw(); } }
void FriendListWidget::setMode(Mode mode) { if (this->mode == mode) return; this->mode = mode; if (mode == Name) { circleLayout = new GenericChatItemLayout; circleLayout->getLayout()->setSpacing(0); circleLayout->getLayout()->setMargin(0); for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) { addCircleWidget(i); CircleWidget::getFromID(i)->setVisible(false); } // Only display circles once all created to avoid artifacts. for (int i = 0; i < Settings::getInstance().getCircleCount(); ++i) CircleWidget::getFromID(i)->setVisible(true); int count = activityLayout ? activityLayout->count() : 0; for (int i = 0; i < count; i++) { QWidget* widget = activityLayout->itemAt(i)->widget(); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); if (categoryWidget) { categoryWidget->moveFriendWidgets(this); } else { qWarning() << "Unexpected widget"; } } listLayout->addLayout(listLayout->getLayoutOnline()); listLayout->addLayout(listLayout->getLayoutOffline()); listLayout->addLayout(circleLayout->getLayout()); onGroupchatPositionChanged(groupsOnTop); if (activityLayout != nullptr) { QLayoutItem* item; while ((item = activityLayout->takeAt(0)) != nullptr) { delete item->widget(); delete item; } delete activityLayout; activityLayout = nullptr; } reDraw(); } else if (mode == Activity) { QLocale ql(Settings::getInstance().getTranslation()); QDate today = QDate::currentDate(); #define COMMENT "Category for sorting friends by activity" // clang-format off const QMap<Time, QString> names { { Time::Today, tr("Today", COMMENT) }, { Time::Yesterday, tr("Yesterday", COMMENT) }, { Time::ThisWeek, tr("Last 7 days", COMMENT) }, { Time::ThisMonth, tr("This month", COMMENT) }, { Time::LongAgo, tr("Older than 6 Months", COMMENT) }, { Time::Never, tr("Never", COMMENT) }, { Time::Month1Ago, ql.monthName(today.addMonths(-1).month()) }, { Time::Month2Ago, ql.monthName(today.addMonths(-2).month()) }, { Time::Month3Ago, ql.monthName(today.addMonths(-3).month()) }, { Time::Month4Ago, ql.monthName(today.addMonths(-4).month()) }, { Time::Month5Ago, ql.monthName(today.addMonths(-5).month()) }, }; // clang-format on #undef COMMENT activityLayout = new QVBoxLayout(); bool compact = Settings::getInstance().getCompactLayout(); for (Time t : names.keys()) { CategoryWidget* category = new CategoryWidget(compact, this); category->setName(names[t]); activityLayout->addWidget(category); } moveFriends(listLayout->getLayoutOffline()); moveFriends(listLayout->getLayoutOnline()); moveFriends(circleLayout->getLayout()); for (int i = 0; i < activityLayout->count(); ++i) { QWidget* widget = activityLayout->itemAt(i)->widget(); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); categoryWidget->setVisible(categoryWidget->hasChatrooms()); } listLayout->removeItem(listLayout->getLayoutOnline()); listLayout->removeItem(listLayout->getLayoutOffline()); if (circleLayout != nullptr) { listLayout->removeItem(circleLayout->getLayout()); QLayoutItem* item; while ((item = circleLayout->getLayout()->takeAt(0)) != nullptr) { delete item->widget(); delete item; } delete circleLayout; circleLayout = nullptr; } listLayout->insertLayout(1, activityLayout); reDraw(); } }
void FriendListWidget::cycleContacts(GenericChatroomWidget* activeChatroomWidget, bool forward) { if (!activeChatroomWidget) { return; } int index = -1; FriendWidget* friendWidget = qobject_cast<FriendWidget*>(activeChatroomWidget); if (mode == Activity) { if (!friendWidget) { return; } QDate activityDate = getDateFriend(friendWidget->getFriend()); index = static_cast<int>(getTime(activityDate)); QWidget* widget = activityLayout->itemAt(index)->widget(); CategoryWidget* categoryWidget = qobject_cast<CategoryWidget*>(widget); if (categoryWidget == nullptr || categoryWidget->cycleContacts(friendWidget, forward)) { return; } index += forward ? 1 : -1; for (;;) { // Bounds checking. if (index < 0) { index = LAST_TIME; continue; } else if (index > LAST_TIME) { index = 0; continue; } widget = activityLayout->itemAt(index)->widget(); categoryWidget = qobject_cast<CategoryWidget*>(widget); if (categoryWidget != nullptr) { if (!categoryWidget->cycleContacts(forward)) { // Skip empty or finished categories. index += forward ? 1 : -1; continue; } } break; } return; } QLayout* currentLayout = nullptr; CircleWidget* circleWidget = nullptr; if (friendWidget != nullptr) { const ToxPk& pk = friendWidget->getFriend()->getPublicKey(); uint32_t circleId = Settings::getInstance().getFriendCircleID(pk); circleWidget = CircleWidget::getFromID(circleId); if (circleWidget != nullptr) { if (circleWidget->cycleContacts(friendWidget, forward)) { return; } index = circleLayout->indexOfSortedWidget(circleWidget); currentLayout = circleLayout->getLayout(); } else { currentLayout = listLayout->getLayoutOnline(); index = listLayout->indexOfFriendWidget(friendWidget, true); if (index == -1) { currentLayout = listLayout->getLayoutOffline(); index = listLayout->indexOfFriendWidget(friendWidget, false); } } } else { GroupWidget* groupWidget = qobject_cast<GroupWidget*>(activeChatroomWidget); if (groupWidget != nullptr) { currentLayout = groupLayout.getLayout(); index = groupLayout.indexOfSortedWidget(groupWidget); } else { return; }; } index += forward ? 1 : -1; for (;;) { // Bounds checking. if (index < 0) { currentLayout = nextLayout(currentLayout, forward); index = currentLayout->count() - 1; continue; } else if (index >= currentLayout->count()) { currentLayout = nextLayout(currentLayout, forward); index = 0; continue; } // Go to the actual next index. if (currentLayout == listLayout->getLayoutOnline() || currentLayout == listLayout->getLayoutOffline() || currentLayout == groupLayout.getLayout()) { GenericChatroomWidget* chatWidget = qobject_cast<GenericChatroomWidget*>(currentLayout->itemAt(index)->widget()); if (chatWidget != nullptr) emit chatWidget->chatroomWidgetClicked(chatWidget); return; } else if (currentLayout == circleLayout->getLayout()) { circleWidget = qobject_cast<CircleWidget*>(currentLayout->itemAt(index)->widget()); if (circleWidget != nullptr) { if (!circleWidget->cycleContacts(forward)) { // Skip empty or finished circles. index += forward ? 1 : -1; continue; } } return; } else { return; } } }