phonebookengines/VirtualPhonebook/VPbkCntModel/src/TContactFieldDataImpl.cpp
branchRCL_3
changeset 63 f4a778e096c2
child 64 c1e8ba0c2b16
equal deleted inserted replaced
62:5b6f26637ad3 63:f4a778e096c2
       
     1 /*
       
     2 * Copyright (c) 2004-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDES
       
    20 #include "TContactFieldDataImpl.h"
       
    21 #include <VPbkError.h>
       
    22 #include <cntdef.h>
       
    23 #include <cntfield.h>
       
    24 #include <cntfldst.h>
       
    25 #include "VPbkCntModelUid.h"
       
    26 
       
    27 
       
    28 
       
    29 
       
    30 namespace VPbkCntModel {
       
    31 
       
    32 _LIT(KColon, ":");
       
    33 
       
    34 // TContactFieldTextData
       
    35 TBool TContactFieldTextData::IsEmpty() const
       
    36     {
       
    37     return !iCntModelField->Storage()->IsFull();
       
    38     }
       
    39 
       
    40 void TContactFieldTextData::CopyL(const MVPbkContactFieldData& aFieldData)
       
    41     {
       
    42     __ASSERT_ALWAYS(DataType() == aFieldData.DataType(), 
       
    43         VPbkError::Panic(VPbkError::EFieldDataTypeMismatch));
       
    44     SetTextL(MVPbkContactFieldTextData::Cast(aFieldData).Text());
       
    45     }
       
    46 
       
    47 TPtrC TContactFieldTextData::Text() const
       
    48     {
       
    49     return iCntModelField->TextStorage()->Text();
       
    50     }
       
    51 
       
    52 void TContactFieldTextData::SetTextL(const TDesC& aText)
       
    53     {
       
    54     iCntModelField->TextStorage()->SetTextL(aText);
       
    55     }
       
    56 
       
    57 TInt TContactFieldTextData::MaxLength() const
       
    58     {
       
    59     return KVPbkUnlimitedFieldLength;
       
    60     }
       
    61 
       
    62 // TContactFieldBinaryData
       
    63 TBool TContactFieldBinaryData::IsEmpty() const
       
    64     {
       
    65     return !iCntModelField->Storage()->IsFull();
       
    66     }
       
    67 
       
    68 void TContactFieldBinaryData::CopyL(const MVPbkContactFieldData& aFieldData)
       
    69     {
       
    70     __ASSERT_ALWAYS(DataType() == aFieldData.DataType(), 
       
    71         VPbkError::Panic(VPbkError::EFieldDataTypeMismatch));
       
    72     SetBinaryDataL(MVPbkContactFieldBinaryData::Cast(aFieldData).BinaryData());
       
    73     }
       
    74 
       
    75 TPtrC8 TContactFieldBinaryData::BinaryData() const
       
    76     {
       
    77     TPtrC8 result;
       
    78     HBufC8* buf = iCntModelField->StoreStorage()->Thing();
       
    79     if (buf)
       
    80         {
       
    81         result.Set(*buf);
       
    82         }
       
    83     return result;
       
    84     }
       
    85 
       
    86 void TContactFieldBinaryData::SetBinaryDataL(const TDesC8& aBinaryData)
       
    87     {
       
    88     iCntModelField->StoreStorage()->SetThingL(aBinaryData);
       
    89     }
       
    90 
       
    91 // TContactFieldDateTimeData
       
    92 TBool TContactFieldDateTimeData::IsEmpty() const
       
    93     {
       
    94     return !iCntModelField->Storage()->IsFull();
       
    95     }
       
    96 
       
    97 void TContactFieldDateTimeData::CopyL(const MVPbkContactFieldData& aFieldData)
       
    98     {
       
    99     __ASSERT_ALWAYS(DataType() == aFieldData.DataType(), 
       
   100         VPbkError::Panic(VPbkError::EFieldDataTypeMismatch));
       
   101     SetDateTime(MVPbkContactFieldDateTimeData::Cast(aFieldData).DateTime());
       
   102     }
       
   103 
       
   104 TTime TContactFieldDateTimeData::DateTime() const
       
   105     {
       
   106     return iCntModelField->DateTimeStorage()->Time();
       
   107     }
       
   108 
       
   109 void TContactFieldDateTimeData::SetDateTime(const TTime& aTime)
       
   110     {
       
   111     iCntModelField->DateTimeStorage()->SetTime(aTime);
       
   112     }
       
   113 
       
   114 // TContactFieldUriData
       
   115 TBool TContactFieldUriData::IsEmpty() const
       
   116     {
       
   117     return !iCntModelField->Storage()->IsFull();
       
   118     }
       
   119 
       
   120 void TContactFieldUriData::CopyL(const MVPbkContactFieldData& aFieldData)
       
   121     {
       
   122     __ASSERT_ALWAYS(DataType() == aFieldData.DataType(), 
       
   123         VPbkError::Panic(VPbkError::EFieldDataTypeMismatch));
       
   124     SetUriL(MVPbkContactFieldUriData::Cast(aFieldData).Uri());
       
   125     }
       
   126 
       
   127 TPtrC TContactFieldUriData::Scheme() const
       
   128     {
       
   129     TPtrC ptr = iCntModelField->TextStorage()->Text();
       
   130     if (!ptr.Length())
       
   131         {
       
   132         return ptr;
       
   133         }
       
   134     TInt pos = ptr.Find(KColon);
       
   135     __ASSERT_DEBUG(pos > 0, 
       
   136         VPbkError::Panic(VPbkError::EFieldDataTypeMismatch));
       
   137     return ptr.Left(pos);
       
   138     }
       
   139 
       
   140 TPtrC TContactFieldUriData::Text() const
       
   141     {
       
   142     TPtrC ptr = iCntModelField->TextStorage()->Text();
       
   143     if (!ptr.Length())
       
   144         {
       
   145         return ptr;
       
   146         }
       
   147     TInt pos = ptr.Find(KColon);
       
   148     __ASSERT_DEBUG(pos > 0 && pos + 1 < ptr.Length(), 
       
   149         VPbkError::Panic(VPbkError::EFieldDataTypeMismatch));
       
   150     return ptr.Mid(++pos);
       
   151     }
       
   152 
       
   153 TPtrC TContactFieldUriData::Uri() const
       
   154     {
       
   155     return iCntModelField->TextStorage()->Text();
       
   156     }
       
   157 
       
   158 // aUri: URI as specified in RFC 3986
       
   159 // For example:
       
   160 //   ftp://ftp.is.co.za/rfc/rfc1808.txt
       
   161 //   ldap://[2001:db8::7]/c=GB?objectClass?one
       
   162 //   mailto:John.Doe@example.com
       
   163 //   tel:+1-816-555-1212
       
   164 void TContactFieldUriData::SetUriL(const TDesC& aUri)
       
   165     {
       
   166     if (aUri.Length())
       
   167         {
       
   168         // skip th validate if field type is IMPP, because IMPP is not URI,
       
   169         // scheme of URI is taken from sXP service name, which might contains
       
   170         // special characters
       
   171         TFieldType fType = {KUidContactFieldIMPPValue};
       
   172         if (!iCntModelField->ContentType().ContainsFieldType(fType))
       
   173             {
       
   174             if ( !Validate(aUri, ETrue) )
       
   175                {
       
   176                User::Leave(KErrNotSupported);
       
   177                }
       
   178             }
       
   179         else // If it is an IMPP field
       
   180             {
       
   181             TInt pos = aUri.Find(KColon);
       
   182             // If the service name or the colon doesn't exist, let it leave the function.
       
   183             // e.g.:
       
   184             // (1) If the value is YAHOO:peterpan@yahoo.com, YAHOO is the service name
       
   185             // (2) If the value is :peterpan@yahoo.com, the service name is NULL
       
   186             // (3) If the value is peterpan@yahoo.com, neither the service name and the colon exists
       
   187             if( 0 == pos || KErrNotFound == pos )
       
   188                 {
       
   189                 User::Leave(KErrNotSupported);
       
   190                 }
       
   191             }
       
   192         }
       
   193     iCntModelField->TextStorage()->SetTextL(aUri);
       
   194     }
       
   195 
       
   196 // aScheme: scheme part of URI aText: rest of URI
       
   197 // For example:
       
   198 //   aScheme:                  aText:
       
   199 //   ftp                       //ftp.is.co.za/rfc/rfc1808.txt
       
   200 //   ldap                      //[2001:db8::7]/c=GB?objectClass?one
       
   201 //   mailto                    John.Doe@example.com
       
   202 //   tel                       +1-816-555-1212
       
   203 void TContactFieldUriData::SetUriL(const TDesC& aScheme, const TDesC& aText)
       
   204     {
       
   205     if (!aText.Length())
       
   206         {
       
   207         if (aScheme.Length())
       
   208             {
       
   209             User::Leave(KErrCorrupt);
       
   210             }
       
   211         }
       
   212     if (aScheme.Length())
       
   213         {
       
   214         /*
       
   215         skip th validate if field type is IMPP, because IMPP is not URI,
       
   216         scheme of URI is taken from sXP service name, which might contains
       
   217         special characters
       
   218         */
       
   219         TFieldType fType = {KUidContactFieldIMPPValue};
       
   220         if (!iCntModelField->ContentType().ContainsFieldType(fType))
       
   221             {
       
   222             if ( !Validate(aScheme, EFalse))
       
   223                {
       
   224                User::Leave(KErrCorrupt);
       
   225                }
       
   226             }
       
   227         HBufC* buf = HBufC::NewLC(aScheme.Length() + KColon().Length() +
       
   228                 aText.Length());
       
   229         TPtr ptr(NULL, 0);
       
   230         ptr.Set(buf->Des());
       
   231         ptr = aScheme;
       
   232         ptr.Append(KColon);
       
   233         ptr.Append(aText);
       
   234         iCntModelField->TextStorage()->SetTextL(*buf);
       
   235         CleanupStack::PopAndDestroy(buf);
       
   236         }
       
   237     else
       
   238         {
       
   239         if (aText.Length())
       
   240             {
       
   241             User::Leave(KErrCorrupt);
       
   242             }
       
   243         iCntModelField->TextStorage()->SetTextL(KNullDesC);
       
   244         }
       
   245     }
       
   246 
       
   247 TInt TContactFieldUriData::MaxLength() const
       
   248     {
       
   249     return KVPbkUnlimitedFieldLength;
       
   250     }
       
   251 
       
   252 TBool TContactFieldUriData::Validate(const TDesC& aUri, TBool aFullUri)
       
   253     {
       
   254     if (!((TChar)aUri[0]).IsAlpha())
       
   255         {
       
   256         return EFalse;
       
   257         }
       
   258     for (TInt i = 1; i < aUri.Length(); i++)
       
   259         {
       
   260         TChar chr = aUri[i];
       
   261         if ((chr == ':') && aFullUri)
       
   262             {
       
   263             return ETrue;
       
   264             }
       
   265         if (!chr.IsAlphaDigit() && (chr != '+') && (chr != '-') && (chr != '.'))
       
   266             {
       
   267             return EFalse;
       
   268             }
       
   269         }
       
   270     if (aFullUri)
       
   271         {
       
   272         return EFalse;
       
   273         }
       
   274     return ETrue;
       
   275     }
       
   276 
       
   277 }  // namespace VPbkCntModel
       
   278