Logo Search packages:      
Sourcecode: kmess version File versions  Download package

const QVariant ContactListModelItem::data ( int  role  )  const

Return a collection of data from this item

The role selects which different piece of data will be extracted from the item; DisplayRole gets you the full map of item data, SearchRole a string to perform filtering during search, SortRole a numeric value used to sort the items. Other roles can be easily added if needed :)

Parameters:
role Specify the kind of item data to obtain
Returns:
QVariant containing int, QMap or QString for the three columns respectively.

Definition at line 319 of file contactlistmodelitem.cpp.

References childCount(), ContactBase::getClientName(), Contact::getContactPicturePath(), Contact::getCurrentMediaString(), Contact::getCurrentMediaType(), Contact::getExtension(), Contact::getFriendlyName(), Contact::getGroupIds(), ContactBase::getHandle(), Group::getId(), ContactExtension::getLastMessageDate(), ContactExtension::getLastSeen(), Group::getName(), Contact::getPersonalMessage(), Group::getSortPosition(), Contact::getStatus(), Contact::getTrueFriendlyName(), getType(), ContactExtension::getUseAlternativeName(), Contact::isBlocked(), Group::isExpanded(), ContactBase::isOnline(), Contact::isReverse(), Group::isSpecialGroup(), object(), STATUS_AWAY, STATUS_BE_RIGHT_BACK, STATUS_BUSY, STATUS_IDLE, STATUS_INVISIBLE, STATUS_OFFLINE, STATUS_ON_THE_PHONE, and STATUS_OUT_TO_LUNCH.

Referenced by ContactList::data(), and ContactList::dump().

{
  switch( role )
  {
    // Output the sorting information
    case SortRole:
    {
      int sortIndex;

      if( itemType_ == ItemContact )
      {
        const Contact *contact = static_cast<Contact*>( object_ );
        sortIndex = 8000; // default sortIndex

        if( contact == 0 )
        {
           return QVariant( sortIndex );
        }

        // Contacts are sorted by their status
        switch( contact->getStatus() )
        {
          case STATUS_ONLINE:        sortIndex = 1000; break;
          case STATUS_BE_RIGHT_BACK: sortIndex = 2000; break;
          case STATUS_IDLE:          sortIndex = 3000; break;
          case STATUS_AWAY:          sortIndex = 4000; break;
          case STATUS_OUT_TO_LUNCH:  sortIndex = 5000; break;
          case STATUS_ON_THE_PHONE:  sortIndex = 6000; break;
          case STATUS_BUSY:          sortIndex = 7000; break;
          case STATUS_INVISIBLE:
          case STATUS_OFFLINE:
          default:                   sortIndex = 8000; break;
        }

        // Add to the index the first character of the friendly name, so that
        // when two contacts have the same status, there's a (rough but fast)
        // sorting method
        const QString& sortChar( contact->getFriendlyName().mid( 0, 1 ) );
        if( ! sortChar.isEmpty() )
        {
          sortIndex += sortChar.at( 0 ).toUpper().toAscii();
        }
      }
      else if( itemType_ == ItemGroup )
      {
        const Group *group = static_cast<Group*>( object_ );
        sortIndex = 32766; // default sortIndex

        if( group == 0 )
        {
          return QVariant( sortIndex );
        }

        const QString& groupId( group->getId() );

        // These numbers are set so that the groups always come after the contacts in the root group

        // Non-special groups must be the first
        if( ! group->isSpecialGroup() )
        {
          sortIndex = 10000 + group->getSortPosition();
        }
        // The Individuals group must come right after them
        else if( groupId == SpecialGroups::INDIVIDUALS )
        {
          sortIndex =  32762;
        }
        // The Online group must come right after it
        else if( groupId == SpecialGroups::ONLINE )
        {
          sortIndex =  32763;
        }
        // The Offline group must follow Online
        else if( groupId == SpecialGroups::OFFLINE )
        {
          sortIndex =  32764;
        }
        // Then the remaining special groups, Allowed
        else if( groupId == SpecialGroups::ALLOWED )
        {
          sortIndex = 32765;
        }
        // And Removed
        else
        {
          sortIndex = 32766;
        }
      }
      else
      {
        // Avoid crashing without hints
        kWarning() << "Called data( SortRole ) on an invalid item: (see below)";
        kWarning() << this;
        return QVariant( 0 );
      }

      return QVariant( sortIndex );
    }
    break;

    // Return a string for full contact list search
    case SearchRole:
    {
      if( itemType_ == ItemContact )
      {
        const Contact *contact = static_cast<Contact*>( object_ );
        if( contact == 0 )
        {
          return QVariant( QString() );
        }

        // Enable searching by email, friendly name, personal message and music
        QString searchBy( contact->getHandle()             + " " +
                          contact->getFriendlyName()       + " " +
                          contact->getPersonalMessage()    + " " +
                          contact->getCurrentMediaString() );

        // Enable searching by email, true friendly name, and alias
        if( contact->getExtension() && contact->getExtension()->getUseAlternativeName() )
        {
          searchBy += " " + contact->getTrueFriendlyName();
        }

        return QVariant( searchBy );
      }
      else if( itemType_ == ItemGroup )
      {
        Group *group = static_cast<Group*>( object_ );
        if( group == 0 )
        {
          return QVariant( QString() );
        }

        return QVariant( group->getName() );
      }
      else
      {
        // Avoid crashing without hints
        kWarning() << "Called data( SearchRole ) on an invalid item: (see below)";
        kWarning() << this;
        return QVariant( QString() );
      }
    }
    break;

    case DataRole:
    {
      ModelDataList itemData;

      // Fill the returned data map with a selection of useful contact information
      if( itemType_ == ItemContact )
      {
        Contact *contact = static_cast<Contact*>( object_ );
        if( contact == 0 )
        {
          return QVariant( ModelDataList() );
        }

        itemData[ "type"                     ] = ItemContact;
        itemData[ "handle"                   ] = contact->getHandle();
        itemData[ "friendly"                 ] = contact->getFriendlyName( STRING_CLEANED );
        itemData[ "friendlyFormatted"        ] = contact->getFriendlyName( STRING_FORMATTED );
        itemData[ "trueFriendly"             ] = contact->getTrueFriendlyName( STRING_CLEANED );
        itemData[ "trueFriendlyFormatted"    ] = contact->getTrueFriendlyName( STRING_FORMATTED );
        itemData[ "personalMessage"          ] = contact->getPersonalMessage( STRING_CLEANED );
        itemData[ "personalMessageFormatted" ] = contact->getPersonalMessage( STRING_FORMATTED );
        itemData[ "displayPicture"           ] = contact->getContactPicturePath();
        itemData[ "groups"                   ] = contact->getGroupIds();
        itemData[ "mediaType"                ] = contact->getCurrentMediaType();
        itemData[ "mediaString"              ] = contact->getCurrentMediaString();
        itemData[ "status"                   ] = (int) contact->getStatus();
        itemData[ "isBlocked"                ] = contact->isBlocked();
        itemData[ "isReverse"                ] = contact->isReverse();
        itemData[ "clientName"               ] = contact->getClientName();
        itemData[ "lastSeen"                 ] = contact->getExtension()->getLastSeen();
        itemData[ "lastMessage"              ] = contact->getExtension()->getLastMessageDate();

        if( parentNode_->object() )
        {
          Group *group = static_cast<Group*>( parentNode_->object() );
          if( group != 0 )
          {
            itemData[ "group"            ] = group->getId();
            itemData[ "isInSpecialGroup" ] = group->isSpecialGroup();
          }
        }
        else
        {
          itemData[ "group"            ] = QString();
          itemData[ "isInSpecialGroup" ] = false;
        }
      }
      else if( itemType_ == ItemGroup )
      {
        // Get the item's group to retrieve its data
        Group *group = static_cast<Group*>( object_ );
        if( group == 0 )
        {
          return QVariant( ModelDataList() );
        }

        // Find out how many of the group's contacts are online
        uint numOnlineContacts = 0;
        foreach( ContactListModelItem *item, childItems_ )
        {
          if( item->getType() != ItemContact )
          {
            continue;
          }

          Contact *contact = static_cast<Contact*>( item->object() );

          if( contact && contact->isOnline() )
          {
            numOnlineContacts++;
          }
        }

        itemData[ "type"           ] = ItemGroup;
        itemData[ "id"             ] = group->getId();
        itemData[ "name"           ] = group->getName();
        itemData[ "onlineContacts" ] = QString::number( numOnlineContacts );
        itemData[ "totalContacts"  ] = QString::number( childCount() );
        itemData[ "isExpanded"     ] = group->isExpanded();
        itemData[ "isSpecialGroup" ] = group->isSpecialGroup();
      }
      else if( itemType_ == ItemRoot )
      {
        itemData[ "type"           ] = ItemRoot;
      }
      else
      {
        // Avoid crashing without hints
        kWarning() << "Called data( DisplayRole ) on an invalid item: (see below)";
        kWarning() << this;
        return QVariant( ModelDataList() );
      }

      return QVariant( itemData );
    }
    break;

  default:
    // Called with an invalid role, do nothing
    break;
  }

  return QVariant();
}


Generated by  Doxygen 1.6.0   Back to index