qtmobility/plugins/contacts/symbian/src/transform/cnttransformavatarsimple.cpp
changeset 4 90517678cc4f
parent 1 2b40d63a9c3d
child 5 453da2cfceef
equal deleted inserted replaced
1:2b40d63a9c3d 4:90517678cc4f
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include "cnttransformavatarsimple.h"
       
    42 #include "cntthumbnailcreator.h"
       
    43 #include "cntsymbiantransformerror.h"
       
    44 
       
    45 // S60 specific contact field type containing image call object data
       
    46 #define KUidContactFieldCodImageValue 0x101F8841
       
    47 const TUid KUidContactFieldCodImage={KUidContactFieldCodImageValue};
       
    48 // Extra field that is defined in TB10.1 platform for video ring tone
       
    49 #define KUidContactFieldVideoRingToneValue  0x200100E5
       
    50 const TUid KUidContactFieldVideoRingTone={KUidContactFieldVideoRingToneValue};
       
    51 // The max. size of the thumbnail image that is saved into contacts database
       
    52 const TSize KMaxThumbnailSize(80, 96);
       
    53 
       
    54 CntTransformAvatarSimple::CntTransformAvatarSimple() :
       
    55     m_thumbnailCreator(0)
       
    56 {
       
    57 }
       
    58 
       
    59 CntTransformAvatarSimple::~CntTransformAvatarSimple()
       
    60 {
       
    61     delete m_thumbnailCreator;
       
    62 }
       
    63 
       
    64 QList<CContactItemField *> CntTransformAvatarSimple::transformDetailL(const QContactDetail &detail)
       
    65 {
       
    66     if(detail.definitionName() != QContactAvatar::DefinitionName)
       
    67         User::Leave(KErrArgument);
       
    68 
       
    69     QList<CContactItemField *> fieldList;
       
    70 
       
    71     //cast to avatar
       
    72     const QContactAvatar &avatar(static_cast<const QContactAvatar&>(detail));
       
    73 
       
    74     //create new field
       
    75     TPtrC fieldText(reinterpret_cast<const TUint16*>(avatar.avatar().utf16()));
       
    76 
       
    77     //copy filename and replace slash with a backslash
       
    78     TFileName filename;
       
    79     for(TInt i(0); i < fieldText.Length(); i++) {
       
    80         if(i >= filename.MaxLength())
       
    81             User::Leave(KErrTooBig);
       
    82         if(fieldText[i] == '/') {
       
    83             filename.Append('\\');
       
    84         } else {
       
    85             filename.Append(fieldText[i]);
       
    86         }
       
    87     }
       
    88 
       
    89 	if(filename.Length()) {
       
    90         const QString& subTypeImage(QContactAvatar::SubTypeImage);
       
    91         const QString& subTypeAudioRingtone(QContactAvatar::SubTypeAudioRingtone);
       
    92 	    const QString& subTypeVideoRingtone(QContactAvatar::SubTypeVideoRingtone);
       
    93 
       
    94         QString subType = avatar.subType();
       
    95         TUid uid(KNullUid);
       
    96         // Default to SubTypeImage
       
    97         if(subType.isEmpty() || subType.compare(subTypeImage) == 0) {
       
    98             uid = KUidContactFieldCodImage;
       
    99 	    } else if (subType.compare(subTypeAudioRingtone) == 0) {
       
   100 	        uid = KUidContactFieldRingTone;
       
   101 	    } else if (subType.compare(subTypeVideoRingtone) == 0) {
       
   102 	        uid = KUidContactFieldVideoRingTone;
       
   103 	    } else {
       
   104 	        User::LeaveIfError(KErrNotSupported);
       
   105 	    }
       
   106 	    CContactItemField* newField = CContactItemField::NewLC(KStorageTypeText, uid);
       
   107 
       
   108 	    newField->SetMapping(KUidContactFieldVCardMapUnknown);
       
   109 	    newField->TextStorage()->SetTextL(filename);
       
   110 
       
   111 	    fieldList.append(newField);
       
   112 	    CleanupStack::Pop(newField);
       
   113     }
       
   114 
       
   115 	if(!avatar.pixmap().isNull()) {
       
   116 	    // lazy instantiation
       
   117 	    if(!m_thumbnailCreator) {
       
   118 	        m_thumbnailCreator = new (ELeave) CntThumbnailCreator();
       
   119 	    }
       
   120 
       
   121         // Scaling is done before converting to CFbsBitmap because
       
   122         // toSymbianCFbsBitmap may generate a duplicate of the bitmap data
       
   123         // Note: scaling to thumbnail may take some time if the image is big
       
   124         // TODO: aspect ratio?
       
   125         QPixmap scaled = avatar.pixmap().scaled(KMaxThumbnailSize.iWidth, KMaxThumbnailSize.iHeight);
       
   126         CFbsBitmap* bitmap = scaled.toSymbianCFbsBitmap();
       
   127         CleanupStack::PushL(bitmap);
       
   128 
       
   129         m_thumbnailCreator->addThumbnailFieldL(&fieldList, bitmap, KMaxThumbnailSize);
       
   130         CleanupStack::Pop(bitmap); // ownership transferred
       
   131     }
       
   132 
       
   133 	return fieldList;
       
   134 }
       
   135 
       
   136 QContactDetail *CntTransformAvatarSimple::transformItemField(const CContactItemField& field, const QContact &contact)
       
   137 {
       
   138 	Q_UNUSED(contact);
       
   139 	QContactAvatar *avatar = new QContactAvatar();
       
   140 
       
   141 	if (field.ContentType().ContainsFieldType(KUidContactFieldCodImage)) {
       
   142 	    CContactTextField* storage = field.TextStorage();
       
   143 	    QString avatarString = QString::fromUtf16(storage->Text().Ptr(), storage->Text().Length());
       
   144 	    avatar->setAvatar(avatarString);
       
   145         avatar->setSubType(QContactAvatar::SubTypeImage);
       
   146     } else if (field.ContentType().ContainsFieldType(KUidContactFieldRingTone)) {
       
   147         CContactTextField* storage = field.TextStorage();
       
   148         QString avatarString = QString::fromUtf16(storage->Text().Ptr(), storage->Text().Length());
       
   149         avatar->setAvatar(avatarString);
       
   150         avatar->setSubType(QContactAvatar::SubTypeAudioRingtone);
       
   151 	} else if (field.ContentType().ContainsFieldType(KUidContactFieldPicture)
       
   152 	    || field.ContentType().ContainsFieldType(KUidContactFieldVCardMapJPEG)) {
       
   153 	    // use the existing QContactAvatar (if available) in case of a pixmap
       
   154 	    // field.
       
   155 	    delete avatar;
       
   156 	    avatar = 0;
       
   157 	    avatar = new QContactAvatar(contact.detail<QContactAvatar>());
       
   158 	    CContactStoreField* storage = field.StoreStorage();
       
   159         QPixmap pixmap;
       
   160         HBufC8 *theThing = storage->Thing();
       
   161         QByteArray bytes((char*)theThing->Ptr(), theThing->Length());
       
   162         bool loaded = pixmap.loadFromData(bytes, "JPG");
       
   163         if (loaded) {
       
   164             avatar->setPixmap(pixmap);
       
   165         } else {
       
   166             User::Leave(KErrInvalidContactDetail);
       
   167         }
       
   168 	}
       
   169     else if (field.ContentType().ContainsFieldType(KUidContactFieldVideoRingTone)) {
       
   170         CContactTextField* storage = field.TextStorage();
       
   171         QString avatarString = QString::fromUtf16(storage->Text().Ptr(), storage->Text().Length());
       
   172         avatar->setAvatar(avatarString);
       
   173         avatar->setSubType(QContactAvatar::SubTypeVideoRingtone);
       
   174     }
       
   175 
       
   176 	return avatar;
       
   177 }
       
   178 
       
   179 bool CntTransformAvatarSimple::supportsField(TUint32 fieldType) const
       
   180 {
       
   181     bool ret = false;
       
   182     if (fieldType == KUidContactFieldCodImage.iUid
       
   183         || fieldType == KUidContactFieldRingTone.iUid
       
   184         || fieldType == KUidContactFieldVideoRingTone.iUid
       
   185         || fieldType == KUidContactFieldPicture.iUid
       
   186         // Used as "extra mapping/extra field type" by thumbnail data fields
       
   187         || fieldType == KUidContactFieldVCardMapJPEG.iUid) {
       
   188         ret = true;
       
   189     }
       
   190     return ret;
       
   191 }
       
   192 
       
   193 bool CntTransformAvatarSimple::supportsDetail(QString detailName) const
       
   194 {
       
   195     bool ret = false;
       
   196     if (detailName == QContactAvatar::DefinitionName) {
       
   197         ret = true;
       
   198     }
       
   199     return ret;
       
   200 }
       
   201 
       
   202 QList<TUid> CntTransformAvatarSimple::supportedSortingFieldTypes(QString /*detailFieldName*/) const
       
   203 {
       
   204     // Sorting not supported
       
   205     return QList<TUid>();
       
   206 }
       
   207 
       
   208 
       
   209 /*!
       
   210  * Checks whether the subtype is supported
       
   211  *
       
   212  * \a subType The subtype to be checked
       
   213  * \return True if this subtype is supported
       
   214  */
       
   215 bool CntTransformAvatarSimple::supportsSubType(const QString& subType) const
       
   216 {
       
   217     if(QContactAvatar::FieldSubType  == subType)
       
   218       return true;
       
   219     else
       
   220       return false;
       
   221 }
       
   222 
       
   223 /*!
       
   224  * Returns the filed id corresponding to a field
       
   225  *
       
   226  * \a fieldName The name of the supported field
       
   227  * \return fieldId for the fieldName, 0  if not supported
       
   228  */
       
   229 quint32 CntTransformAvatarSimple::getIdForField(const QString& fieldName) const
       
   230 {
       
   231    if (QContactAvatar::FieldAvatar  == fieldName)
       
   232         return 0;
       
   233     else if (QContactAvatar::SubTypeImage == fieldName)
       
   234         return 0;
       
   235     else if (QContactAvatar::SubTypeVideo == fieldName)
       
   236         return 0;
       
   237     else if (QContactAvatar::SubTypeTexturedMesh == fieldName)
       
   238         return 0;
       
   239     else if (QContactAvatar::SubTypeAudioRingtone == fieldName)
       
   240         return 0;
       
   241     else if (QContactAvatar::SubTypeVideoRingtone == fieldName)
       
   242         return 0;
       
   243     else
       
   244         return 0;
       
   245 }
       
   246 
       
   247 /*!
       
   248  * Modifies the detail definitions. The default detail definitions are
       
   249  * queried from QContactManagerEngine::schemaDefinitions and then modified
       
   250  * with this function in the transform leaf classes.
       
   251  *
       
   252  * \a definitions The detail definitions to modify.
       
   253  * \a contactType The contact type the definitions apply for.
       
   254  */
       
   255 void CntTransformAvatarSimple::detailDefinitions(QMap<QString, QContactDetailDefinition> &definitions, const QString& contactType) const
       
   256 {
       
   257     Q_UNUSED(contactType);
       
   258 
       
   259     if(definitions.contains(QContactAvatar::DefinitionName)) {
       
   260         QContactDetailDefinition d = definitions.value(QContactAvatar::DefinitionName);
       
   261         QMap<QString, QContactDetailFieldDefinition> fields = d.fields();
       
   262 
       
   263         // Update sub-types
       
   264         QContactDetailFieldDefinition f;
       
   265         f.setDataType(QVariant::String); // only allowed to be a single subtype
       
   266         f.setAllowableValues(QVariantList()
       
   267                 << QString(QLatin1String(QContactAvatar::SubTypeImage))
       
   268                 << QString(QLatin1String(QContactAvatar::SubTypeAudioRingtone))
       
   269                 << QString(QLatin1String(QContactAvatar::SubTypeVideoRingtone)));
       
   270         fields.insert(QContactAvatar::FieldSubType, f);
       
   271 
       
   272         // Context not supported in symbian back-end, remove
       
   273         fields.remove(QContactAvatar::FieldContext);
       
   274 
       
   275         d.setFields(fields);
       
   276         d.setUnique(true);
       
   277 
       
   278         // Replace original definitions
       
   279         definitions.insert(d.name(), d);
       
   280     }
       
   281 }