FriendWidget* ContentDialog::addFriend(int friendId, QString id) { FriendWidget* friendWidget = new FriendWidget(friendId, id); friendLayout->addFriendWidget(friendWidget, FriendList::findFriend(friendId)->getStatus()); Friend* frnd = friendWidget->getFriend(); const Settings& s = Settings::getInstance(); connect(frnd, &Friend::displayedNameChanged, this, &ContentDialog::updateFriendWidget); connect(&s, &Settings::compactLayoutChanged, friendWidget, &FriendWidget::compactChange); connect(friendWidget, &FriendWidget::chatroomWidgetClicked, this, &ContentDialog::onChatroomWidgetClicked); connect(friendWidget, SIGNAL(chatroomWidgetClicked(GenericChatroomWidget*)), frnd->getChatForm(), SLOT(focusInput())); connect(Core::getInstance(), &Core::friendAvatarChanged, friendWidget, &FriendWidget::onAvatarChange); connect(Core::getInstance(), &Core::friendAvatarRemoved, friendWidget, &FriendWidget::onAvatarRemoved); ContentDialog* lastDialog = getFriendDialog(friendId); if (lastDialog != nullptr) lastDialog->removeFriend(friendId); friendList.insert(friendId, std::make_tuple(this, friendWidget)); onChatroomWidgetClicked(friendWidget, false); return friendWidget; }
void ContentDialog::removeFriend(int friendId) { auto iter = friendList.find(friendId); if (iter == friendList.end()) return; FriendWidget* chatroomWidget = static_cast<FriendWidget*>(std::get<1>(iter.value())); disconnect(chatroomWidget->getFriend(), &Friend::displayedNameChanged, this, &ContentDialog::updateFriendWidget); // Need to find replacement to show here instead. if (activeChatroomWidget == chatroomWidget) cycleContacts(true, false); friendLayout->removeFriendWidget(chatroomWidget, Status::Offline); friendLayout->removeFriendWidget(chatroomWidget, Status::Online); chatroomWidget->deleteLater(); friendList.remove(friendId); if (chatroomWidgetCount() == 0) { contentLayout->clear(); activeChatroomWidget = nullptr; deleteLater(); } else { update(); } }
void FriendListWidget::onTextChanged( const QString& text ) { QString trimmedText = text.trimmed(); setUpdatesEnabled( false ); if ( text.isEmpty() ) { // special case an empty string so it's a bit zippier for ( int i = 1 ; i < ui->friends->count() ; ++i ) ui->friends->item( i )->setHidden( false ); } else { QRegExp re( QString( "^%1" ).arg( trimmedText ), Qt::CaseInsensitive ); for ( int i = 1 ; i < ui->friends->count() ; ++i ) { FriendWidget* user = static_cast<FriendWidget*>( ui->friends->itemWidget( ui->friends->item( i ) ) ); ui->friends->item( i )->setHidden( !( user->name().startsWith( trimmedText, Qt::CaseInsensitive ) || user->realname().startsWith( trimmedText, Qt::CaseInsensitive ) || user->realname().split( ' ' ).filter( re ).count() > 0 ) ); } } setUpdatesEnabled( true ); }
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()); } } }
bool FriendWidget::operator<( const FriendWidget& that ) const { // sort by most recently listened and then by name if ( this->m_listeningNow && !that.m_listeningNow ) return true; if ( !this->m_listeningNow && that.m_listeningNow ) return false; if ( this->m_listeningNow && that.m_listeningNow ) return this->name().toLower() < that.name().toLower(); if ( !this->m_track.timestamp().isNull() && that.m_track.timestamp().isNull() ) return true; if ( this->m_track.timestamp().isNull() && !that.m_track.timestamp().isNull() ) return false; if ( this->m_track.timestamp().isNull() && that.m_track.timestamp().isNull() ) return this->name().toLower() < that.name().toLower(); // both timestamps are valid! if ( this->m_track.timestamp() == that.m_track.timestamp() ) { if ( this->m_order == that.m_order ) return this->name().toLower() < that.name().toLower(); return this->m_order < that.m_order; } // this is the other way around because a higher time means it's lower in the list return this->m_track.timestamp() > that.m_track.timestamp(); }
void FriendListWidget::onGotFriends() { // add this set of users to the list lastfm::XmlQuery lfm; if ( lfm.parse( qobject_cast<QNetworkReply*>(sender()) ) ) { foreach( const lastfm::XmlQuery& user, lfm["friends"].children( "user" ) ) { FriendWidgetItem* item = new FriendWidgetItem( ui->friends ); FriendWidget* friendWidget = new FriendWidget( user, this ); ui->friends->setItemWidget( item, friendWidget ); item->setSizeHint( friendWidget->sizeHint() ); } int page = lfm["friends"].attribute( "page" ).toInt(); int perPage = lfm["friends"].attribute( "perPage" ).toInt(); int totalPages = lfm["friends"].attribute( "totalPages" ).toInt(); //int total = lfm["friends"].attribute( "total" ).toInt(); // Check if we need to fetch another page of users if ( page != totalPages ) { m_reply = lastfm::User().getFriends( true, perPage, page + 1 ); connect( m_reply, SIGNAL(finished()), SLOT(onGotFriends()) ); } else { // we have fetched all the pages! onTextChanged( ui->filter->text() ); m_reply = User().getFriendsListeningNow( 50, 1 ); connect( m_reply, SIGNAL(finished()), SLOT(onGotFriendsListeningNow())); } }
void ContentDialog::updateFriendWidget(FriendWidget *w, Status s) { FriendWidget* friendWidget = static_cast<FriendWidget*>(std::get<1>(friendList.find(w->friendId).value())); friendWidget->setName(w->getName()); friendLayout->addFriendWidget(friendWidget, s); }
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; } } }