remotestoragefw/mountmanager/src/rsfwmountutils.cpp
changeset 13 6b4fc789785b
parent 2 c32dc0be5eb4
equal deleted inserted replaced
2:c32dc0be5eb4 13:6b4fc789785b
     1 /*
       
     2 * Copyright (c) 2009 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:  Utility functions for managing mount configurations
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDES
       
    19 
       
    20 #include <s32file.h>
       
    21 #include <uri8.h>
       
    22 #include <utf.h>
       
    23 #include <ecom/ecom.h>
       
    24 #include <ecom/implementationinformation.h>
       
    25 #include <rsfwmountentry.h>
       
    26 
       
    27 #include "rsfwmountutils.h"
       
    28 #include "rsfwremoteaccess.h"
       
    29 
       
    30 
       
    31 // CONSTANTS
       
    32 // These keywords must match with the entry items
       
    33 _LIT(KKId, "Fname");
       
    34 _LIT(KKDrive, "Fdrv");
       
    35 _LIT(KKUri, "Furi");
       
    36 _LIT(KKUserName, "Fuid");
       
    37 _LIT(KKPassword, "Fpsw");
       
    38 
       
    39 _LIT(KMountMessagePrefix, "//vnd.nokia.s60.mount.config\n");
       
    40 
       
    41 // ============================ MEMBER FUNCTIONS ==============================
       
    42 
       
    43 // ----------------------------------------------------------------------------
       
    44 // RsfwMountUtils::ImportMountEntryL
       
    45 // ----------------------------------------------------------------------------
       
    46 //
       
    47 EXPORT_C void RsfwMountUtils::ImportMountEntryL(const TDesC& aMsg,
       
    48                                             CRsfwMountEntry** aEntry)
       
    49     {
       
    50     // Expected to start with
       
    51     // //vnd.nokia.s60.mount.config\n
       
    52     
       
    53     TPtrC prefix = aMsg.Left(KMountMessagePrefixLength);
       
    54     if (prefix.Compare(KMountMessagePrefix) != 0)
       
    55         {
       
    56         User::Leave(KErrNotFound);
       
    57         }
       
    58     
       
    59     // It is a mount configuration message
       
    60     CRsfwMountEntry* entry = CRsfwMountEntry::NewLC();
       
    61 
       
    62     TInt lineNumber = 0;
       
    63 
       
    64     // SMS rich text messages may be have trailing characters -
       
    65     // that looks like ')' characters
       
    66     TInt i = aMsg.Length() - 1;
       
    67     while (i > 0)
       
    68         {
       
    69         TUint8 c = static_cast<TUint8>(aMsg[i]);
       
    70         if (c != ')')
       
    71             {
       
    72             break;
       
    73             }
       
    74         i--;
       
    75         }
       
    76     TPtrC msg  = aMsg.Left(i + 1);
       
    77 
       
    78     TLex lex(msg);
       
    79     while (!lex.Eos())
       
    80         {
       
    81         lineNumber++;
       
    82         lex.SkipSpaceAndMark();
       
    83         while (lex.Peek() != '\n'  && !lex.Eos())
       
    84             {
       
    85             lex.Inc();
       
    86             }
       
    87         // The marked token is now the whole line
       
    88         TPtrC line = lex.MarkedToken();
       
    89         ParseLineL(line, *entry);
       
    90         if (!lex.Eos())
       
    91             {
       
    92             // Skip the '\n'
       
    93             lex.Inc();
       
    94             }
       
    95         }
       
    96 
       
    97     CleanupStack::Pop(entry);
       
    98     *aEntry = entry;
       
    99     }
       
   100 
       
   101 // ----------------------------------------------------------------------------
       
   102 // RsfwMountUtils::ImportMountEntryFromStreamL
       
   103 // ----------------------------------------------------------------------------
       
   104 //
       
   105 EXPORT_C void RsfwMountUtils::ImportMountEntryFromStreamL(RReadStream& aReadStream,
       
   106 													      TInt aSize,
       
   107                                                           CRsfwMountEntry** aEntry)
       
   108     {
       
   109     // the string is assumed to be 8-bit in stream, 
       
   110     // and should be converted to Unicode
       
   111     *aEntry = NULL;
       
   112     HBufC8* tempbuf = HBufC8::NewLC(aSize);
       
   113     TPtr8 tbufPtr = tempbuf->Des();
       
   114     tbufPtr.Zero();
       
   115     TRAPD(err, aReadStream.ReadL(tbufPtr));
       
   116     
       
   117     HBufC* buf = HBufC::NewLC(aSize);
       
   118     TPtr bufPtr = buf->Des();
       
   119     CnvUtfConverter::ConvertToUnicodeFromUtf8(bufPtr, tbufPtr);
       
   120     if ((err == KErrNone) || (err == KErrEof))
       
   121         {
       
   122         ImportMountEntryL(bufPtr, aEntry);
       
   123         }
       
   124     CleanupStack::PopAndDestroy(2, tempbuf); // buf, tempbuf 
       
   125     }
       
   126 
       
   127 // ----------------------------------------------------------------------------
       
   128 // RsfwMountUtils::ExportMountEntryL
       
   129 // ----------------------------------------------------------------------------
       
   130 //
       
   131 EXPORT_C void RsfwMountUtils::ExportMountEntryL(const CRsfwMountEntry& aEntry,
       
   132                                                 TBool aSendCredentials,
       
   133                                                 TDes& aBuf)
       
   134     {
       
   135     aBuf.Copy(KMountMessagePrefix);
       
   136     TInt i;
       
   137     // smart messaging sends:
       
   138     // EMountEntryItemName
       
   139     // EMountEntryItemUri
       
   140     // if aSendCredentials sends also:
       
   141     // EMountEntryItemUserName
       
   142     // EMountEntryItemPassword 
       
   143     TInt lastToExport;
       
   144     if (aSendCredentials) 
       
   145         {
       
   146         lastToExport = EMountEntryItemPassword; 
       
   147         }
       
   148     else 
       
   149         {
       
   150         lastToExport = EMountEntryItemUri;
       
   151         }
       
   152     
       
   153     for (i = EMountEntryItemName; i < lastToExport + 1; i++)
       
   154         {
       
   155         if (i != EMountEntryItemDrive) 
       
   156         	{
       
   157         	const HBufC* item = aEntry.Item(i);
       
   158         	if (item && item->Length())
       
   159             	{
       
   160             	aBuf.Append(GetKeyWord(i));
       
   161             	aBuf.Append(':');
       
   162                 const TPtrC ip(*item);
       
   163                 aBuf.Append(ip);
       
   164             	aBuf.Append('\n');
       
   165             	}        	
       
   166         	}
       
   167        }
       
   168     }
       
   169  
       
   170 // ----------------------------------------------------------------------------
       
   171 // RsfwMountUtils::IsFriendlyNameValid
       
   172 // ----------------------------------------------------------------------------
       
   173 //
       
   174 EXPORT_C TBool RsfwMountUtils::IsFriendlyNameValid(const TDesC& aFriendlyName)
       
   175     {
       
   176     TInt err;
       
   177     TBool retValue = ETrue;
       
   178     RFs fsSession;
       
   179     err = fsSession.Connect();
       
   180     if (err)
       
   181         {
       
   182         return retValue;
       
   183         }    
       
   184     retValue = fsSession.IsValidName(aFriendlyName);
       
   185     fsSession.Close();  
       
   186         
       
   187     // Some names are acceptable by RFs.IsValidName(), 
       
   188     // but not acceptable by RFs.SetDriveName()
       
   189     // Those are checked below:
       
   190         
       
   191     // solely period characters ( e.g. "...")
       
   192     if (retValue)
       
   193         {
       
   194         retValue = EFalse;
       
   195         TChar period(46); // period (".") character
       
   196         TInt i;
       
   197         for (i = 0; i < aFriendlyName.Length(); i++)
       
   198             {   
       
   199             if (aFriendlyName[i] != period)
       
   200                 {
       
   201                 retValue = ETrue;
       
   202                 break;
       
   203                 }
       
   204             }
       
   205         }
       
   206         
       
   207     // period as the last character ( e.g. "myDrive.")
       
   208     if (retValue)
       
   209         {
       
   210         TChar period(46); // period (".") character
       
   211         TInt nameLength = aFriendlyName.Length();
       
   212         if (nameLength > 0 && aFriendlyName[nameLength-1] == period)
       
   213             {
       
   214             retValue = EFalse;
       
   215             }
       
   216         }    
       
   217 
       
   218     return retValue;    
       
   219     }
       
   220 
       
   221 // ----------------------------------------------------------------------------
       
   222 // RsfwMountUtils::IsDriveAddressValid
       
   223 // ----------------------------------------------------------------------------
       
   224 //
       
   225 EXPORT_C TBool RsfwMountUtils::IsDriveAddressValid(const TDesC8& aDriveAddress)
       
   226     {
       
   227     // extract the protocol
       
   228     TInt err;
       
   229     TUriParser8 parser;
       
   230     err = parser.Parse(aDriveAddress);
       
   231     if (err) 
       
   232     		{
       
   233     		return EFalse;	
       
   234     		}
       
   235     TPtrC8 protocol = parser.Extract(EUriScheme);
       
   236     
       
   237     
       
   238     TBuf8<KMaxMatchStringSize> matchString;
       
   239     _LIT8(KRemoteMatchPrefix, "remoteaccess/");
       
   240     matchString.Copy(KRemoteMatchPrefix);
       
   241 
       
   242     // Both "http" and "https" are handled by davaccess module
       
   243     _LIT8(KHttps, "https"); 
       
   244     if (protocol.Compare(KHttps) == 0)
       
   245         {
       
   246         _LIT8(KHttp, "http");   
       
   247         matchString.Append(KHttp);
       
   248         }
       
   249     else
       
   250         {
       
   251         matchString.Append(protocol);
       
   252         }
       
   253     TEComResolverParams resolverParams;
       
   254     resolverParams.SetDataType(matchString);
       
   255     // Disable wildcard matching
       
   256     resolverParams.SetWildcardMatch(EFalse);    
       
   257     
       
   258     
       
   259     // check whether there is a remote access plugin implementation for that protocol 
       
   260     RImplInfoPtrArray implinfo;
       
   261     TRAP(err, REComSession::ListImplementationsL(
       
   262                              KCRemoteAccessUid,
       
   263                              resolverParams,
       
   264                              implinfo));
       
   265     
       
   266     if (err != KErrNone) 
       
   267         {
       
   268         // if fetching the list of implemenations fail,'
       
   269         // (for example a temporary out of memory situation)
       
   270         // just accept the string
       
   271         return ETrue;
       
   272         }
       
   273      
       
   274     // we assume that protocol used for remote access
       
   275     //  represents hierarchical relationships within the namespace.  This
       
   276     // "generic URI" syntax consists of a sequence of four main components:
       
   277     //      <scheme>://<authority><path>?<query>
       
   278     // check that scheme is followed by "://"
       
   279     _LIT8(KDelimiter, "://");
       
   280     if (aDriveAddress.Length() < protocol.Length()+3) 
       
   281         {
       
   282         return EFalse;
       
   283         }
       
   284     
       
   285     TPtrC8 test = aDriveAddress.Mid(protocol.Length(), 3);
       
   286     if (!(test.Compare(KDelimiter) == 0)) 
       
   287         {
       
   288         return EFalse;    
       
   289         }
       
   290     
       
   291     TInt count = implinfo.Count();
       
   292     implinfo.ResetAndDestroy();
       
   293     if (count > 0) 
       
   294         {
       
   295         return ETrue;
       
   296         }
       
   297     else 
       
   298         {
       
   299         return EFalse;
       
   300         }
       
   301     }
       
   302 
       
   303 // ----------------------------------------------------------------------------
       
   304 // RsfwMountUtils::ParseLineL
       
   305 // ----------------------------------------------------------------------------
       
   306 //
       
   307 void RsfwMountUtils::ParseLineL(const TDesC& aLine,
       
   308                                 CRsfwMountEntry& aEntry)
       
   309     {
       
   310     TLex lex(aLine);
       
   311     // Extract Line i.e "leftToken:RightToken<LF>", then asign to TLex object
       
   312     while (lex.Peek() != ':' && lex.Peek() != '\n' && !lex.Eos())
       
   313         {
       
   314         lex.Inc();
       
   315         }
       
   316 
       
   317     // We are only interested in lines containing a colon delimeter ':'
       
   318     // other text lines are ignored i.e "Welcome !<LF>"
       
   319     if(lex.Peek() != ':')
       
   320         {
       
   321         // It was not a name value pair
       
   322         return;
       
   323         }
       
   324     if (lex.TokenLength() == 0)
       
   325         {
       
   326         User::Leave(KErrNotFound);
       
   327         }
       
   328     // Get the keyword
       
   329     HBufC* keyWord = lex.MarkedToken().AllocLC();
       
   330     // Go past the ':'
       
   331     lex.Inc();
       
   332     lex.SkipSpaceAndMark();
       
   333     while (lex.Peek() != '\n' && lex.Peek() != '\r' && !lex.Eos())
       
   334         {
       
   335         lex.Inc();
       
   336         }
       
   337 
       
   338     TInt i;
       
   339     // Keyword matching
       
   340     // password is the last possible entry that is transferred
       
   341     for (i = EMountEntryItemName; i < EMountEntryItemPassword + 1; i++)
       
   342         {
       
   343         if (GetKeyWord(i).Compare(*keyWord) == 0)
       
   344             {
       
   345             aEntry.SetItemL(i, lex.MarkedToken());
       
   346             }
       
   347         }
       
   348     CleanupStack::PopAndDestroy(keyWord);
       
   349     }
       
   350 
       
   351 // ----------------------------------------------------------------------------
       
   352 // RsfwMountUtils::GetKeyWord
       
   353 // ----------------------------------------------------------------------------
       
   354 //
       
   355 const TDesC& RsfwMountUtils::GetKeyWord(TInt iItem)
       
   356     {
       
   357     switch (iItem)
       
   358         {
       
   359     case EMountEntryItemName:
       
   360         return KKId;
       
   361         
       
   362     case EMountEntryItemDrive:
       
   363         return KKDrive;
       
   364 
       
   365     case EMountEntryItemUri:
       
   366         return KKUri;
       
   367 
       
   368     case EMountEntryItemUserName:
       
   369         return KKUserName;
       
   370 
       
   371     case EMountEntryItemPassword:
       
   372         return KKPassword;
       
   373 
       
   374     default:
       
   375         return KKId;
       
   376         }
       
   377     }
       
   378 // End of File