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:
roleSpecify 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 getType(), 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:
    {
      QString sortValue;

      if( itemType_ == ItemContact )
      {
        const Contact *contact = static_cast<Contact*>( object_ );
        sortValue = "8"; // default sortValue

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

        // Contacts are sorted by their status
        switch( contact->getStatus() )
        {
          case STATUS_ONLINE:        sortValue = "1"; break;
          case STATUS_BE_RIGHT_BACK: sortValue = "2"; break;
          case STATUS_IDLE:          sortValue = "3"; break;
          case STATUS_AWAY:          sortValue = "4"; break;
          case STATUS_OUT_TO_LUNCH:  sortValue = "5"; break;
          case STATUS_ON_THE_PHONE:  sortValue = "6"; break;
          case STATUS_BUSY:          sortValue = "7"; break;
          case STATUS_INVISIBLE:
          case STATUS_OFFLINE:
          default:                   sortValue = "8"; break;
        }

        // Add first characters of the friendly name to sortValue
        const QString sortStr( contact->getFriendlyName().mid( 0, 15 ) );
        if( ! sortStr.isEmpty() )
        {
          sortValue += sortStr;
        }
      }
      else if( itemType_ == ItemGroup )
      {
        const Group *group = static_cast<Group*>( object_ );
        sortValue = "A"; // default sortValue

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

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

        // These literals 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() )
        {
          sortValue = QString("9%1").arg(group->getSortPosition());
        }
        // The Individuals group must come right after them
        else if( groupId == SpecialGroups::INDIVIDUALS )
        {
          sortValue =  "B";
        }
        // The Online group must come right after it
        else if( groupId == SpecialGroups::ONLINE )
        {
          sortValue =  "C";
        }
        // The Offline group must follow Online
        else if( groupId == SpecialGroups::OFFLINE )
        {
          sortValue =  "D";
        }
        // Then the remaining special groups, Allowed
        else if( groupId == SpecialGroups::ALLOWED )
        {
          sortValue = "E";
        }
        // And Removed
        else
        {
          sortValue = "F";
        }
      }
      else
      {
        // Avoid crashing without hints
        kmWarning() << "Called data( SortRole ) on an invalid item: (see below)";
        kmWarning() << this;
        return QVariant( 0 );
      }

      return QVariant( sortValue );
    }
    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
        kmWarning() << "Called data( SearchRole ) on an invalid item: (see below)";
        kmWarning() << 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();
        itemData[ "isMessengerUser"          ] = contact->isMessengerUser();

        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;
        uint numMsgrContacts = 0;
        foreach( ContactListModelItem *item, childItems_ )
        {
          if( item->getType() != ItemContact )
          {
            continue;
          }

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

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

          if( contact && contact->isMessengerUser() )
          {
            numMsgrContacts++;
          }
        }

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

      return QVariant( itemData );
    }
    break;

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

  return QVariant();
}

Here is the call graph for this function:

Here is the caller graph for this function:


Generated by  Doxygen 1.6.0   Back to index