mtpdataproviders/mtppictbridgedp/pictbridgeengine/src/dpstransaction.cpp
branchRCL_3
changeset 20 4a793f564d72
parent 19 0aa8cc770c8a
child 21 74aa6861c87d
equal deleted inserted replaced
19:0aa8cc770c8a 20:4a793f564d72
     1 /*
       
     2 * Copyright (c) 2006, 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:  This class creates and parses dps operations. 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <e32base.h>
       
    20 #include <e32debug.h>
       
    21 #include <pathinfo.h>
       
    22 #include <s32file.h>
       
    23 #include "dpstransaction.h"
       
    24 #include "dpsscriptsender.h"
       
    25 #include "dpsdefs.h"
       
    26 #include "dpsxmlparser.h"
       
    27 #include "dpsxmlgenerator.h"
       
    28 #include "dpsconst.h"
       
    29 #include "dpsfile.h"
       
    30 #include "dpsstatemachine.h"
       
    31 #include "dpsscriptreceiver.h"
       
    32 #include "pictbridge.h"
       
    33 #include <xml/parser.h>
       
    34 #include "dpsparam.h"
       
    35 #include "dpsoperation.h"
       
    36 #include "dpsxmlstring.h"
       
    37 
       
    38 #ifdef _DEBUG
       
    39 #	define IF_DEBUG(t) {RDebug::t;}
       
    40 #else
       
    41 #	define IF_DEBUG(t)
       
    42 #endif
       
    43 
       
    44 const TInt KPathLength = 3;
       
    45 const TInt KPercentagePosition = 3;
       
    46 const TInt KPercentage = 100;
       
    47 // ---------------------------------------------------------------------------
       
    48 // 
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 CDpsTransaction* CDpsTransaction::NewL(CDpsStateMachine* aOperator)
       
    52     {
       
    53     IF_DEBUG(Print(_L("CDpsTransaction::NewL")));
       
    54     CDpsTransaction* self = new(ELeave) CDpsTransaction(aOperator);
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57     CleanupStack::Pop();
       
    58     return self;	
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------------------------
       
    62 // 
       
    63 // ---------------------------------------------------------------------------
       
    64 //	
       
    65 CDpsTransaction::CDpsTransaction(CDpsStateMachine* aOperator) :
       
    66     iOperator(aOperator), iReply(EFalse)
       
    67 	{
       
    68     IF_DEBUG(Print(_L("CDpsTransaction::Ctor")));
       
    69 	}
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // 
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 void CDpsTransaction::ConstructL()
       
    76     {
       
    77     IF_DEBUG(Print(_L(">>>CDpsTransaction::ConstructL")));	
       
    78     iFile = CDpsFile::NewL();
       
    79     iXmlGen = CDpsXmlGenerator::NewL(iOperator->DpsEngine());
       
    80     iXmlPar = CDpsXmlParser::NewL(iOperator->DpsEngine());
       
    81     IF_DEBUG(Print(_L("<<<CDpsTransaction::ConstructL")));
       
    82     }
       
    83 
       
    84 // ---------------------------------------------------------------------------
       
    85 // 
       
    86 // ---------------------------------------------------------------------------
       
    87 //	
       
    88 CDpsTransaction::~CDpsTransaction()
       
    89 	{
       
    90     IF_DEBUG(Print(_L(">>>~CDpsTransaction")));
       
    91     delete iXmlPar;
       
    92     iXmlPar = NULL;
       
    93 	
       
    94     delete iXmlGen;
       
    95     iXmlGen = NULL;
       
    96 	
       
    97     delete iFile;
       
    98     iFile = NULL;
       
    99 	
       
   100     IF_DEBUG(Print(_L("<<<~CDpsTransaction")));
       
   101     }
       
   102     
       
   103 // ---------------------------------------------------------------------------
       
   104 // 
       
   105 // ---------------------------------------------------------------------------
       
   106 //	
       
   107 void CDpsTransaction::CreateRequestL(TMDpsOperation* aOperation)
       
   108 	{
       
   109     IF_DEBUG(Print(_L(">>>CDpsTransaction::CreateRequest ")));
       
   110     IF_DEBUG(Print(_L("  the request is %d"), aOperation->iOperation));
       
   111     TDpsArgArray args;
       
   112     TDpsEleArray elements; 
       
   113     TDpsAttribute attrib = 0;
       
   114     CleanupClosePushL(args);
       
   115     CleanupClosePushL(elements);
       
   116     RFileWriteStream writer;
       
   117     writer.PushL();
       
   118     TFileName defaultPath = iOperator->DpsEngine()->DpsFolder();
       
   119     defaultPath.Append(KDpsDeviceRequestFileName);
       
   120     User::LeaveIfError(writer.Replace(iFile->FileSession(), defaultPath, 
       
   121                                       EFileWrite));
       
   122     IF_DEBUG(Print(_L("*** file created  ")));                                  
       
   123     User::LeaveIfError(aOperation->FillReqArgs(args, elements, attrib, this));
       
   124     aOperation->CreateReqScriptL(args, elements, attrib, writer, this);    
       
   125     writer.CommitL();
       
   126     writer.Pop();
       
   127     writer.Release();
       
   128     User::LeaveIfError(iOperator->ScriptSender()->SendScript(EFalse));
       
   129     CleanupStack::PopAndDestroy(&elements);
       
   130     CleanupStack::PopAndDestroy(&args);
       
   131     IF_DEBUG(Print(_L("<<<CDpsTransaction::CreateRequest "))); 
       
   132 	}
       
   133 
       
   134 // ---------------------------------------------------------------------------
       
   135 // 
       
   136 // ---------------------------------------------------------------------------
       
   137 //	
       
   138 void CDpsTransaction::Filter(TDes8& aScript)
       
   139     {    
       
   140     TInt size = aScript.Size();
       
   141     IF_DEBUG(Print(_L(">>>CDpsTransaction::Filter size %d"), size));
       
   142     
       
   143     for (TInt i = 0; i < size; )
       
   144         {
       
   145         // removes any unprintalbe char between two XML attributes, e.g. 
       
   146         // between > and <            
       
   147         if (aScript[i] >= KSOH && aScript[i] <= KSpace && aScript[i -1] == '>')    
       
   148             {
       
   149             aScript.Delete(i, 1);
       
   150             size--;
       
   151             }
       
   152         else
       
   153             {
       
   154             i++;
       
   155             }
       
   156         }
       
   157     IF_DEBUG(Print(_L("<<<CDpsTransaction::Filter size %d"), size));
       
   158     }
       
   159  
       
   160 // ---------------------------------------------------------------------------
       
   161 // 
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void CDpsTransaction::ParseScriptL(TBool aReply)
       
   165     {
       
   166     IF_DEBUG(Print(_L(">>>CDpsTransaction::ParseScript")));
       
   167     iReply = aReply;
       
   168     TInt size;
       
   169     iFile->FileSizeL(iOperator->ScriptReceiver()->FileNameAndPath(), size);
       
   170     HBufC8* script = HBufC8::NewLC(size);
       
   171     TPtr8 ptr_script = script->Des();
       
   172     iFile->GetContentL(iOperator->ScriptReceiver()->FileNameAndPath(), 
       
   173                        ptr_script);
       
   174     Filter(ptr_script);    
       
   175 	iXmlPar->Reset();
       
   176     CParser* parser = CParser::NewLC(KDpsXmlMimeType, *iXmlPar);
       
   177     Xml::ParseL(*parser, ptr_script);
       
   178     TDpsArgArray args; iXmlPar->GetParameters(args);
       
   179     
       
   180 #ifdef _DEBUG
       
   181     //print what we get now
       
   182     TBuf<KMaxArgLen> print;
       
   183     for (TInt i = 0; i < args.Count(); i++)
       
   184         {
       
   185         print.Copy(args[i].iContent);
       
   186         IF_DEBUG(Print(_L("element %d content %S"), 
       
   187             args[i].iElement, &print));
       
   188         }
       
   189 #endif
       
   190    
       
   191     if (aReply)
       
   192         {
       
   193         TMDpsOperation* op = iOperator->MOperation();
       
   194         User::LeaveIfError(op->FillRepArgs(args, this));
       
   195         iXmlPar->SetOperationResult(op->iResult);    
       
   196         }  
       
   197     else 
       
   198         {
       
   199         TDpsResult result; 
       
   200    	    result.iMajorCode = EDpsResultOk; 
       
   201         result.iMinorCode = EDpsResultNone;       			
       
   202         if (iXmlPar->IsEvent())
       
   203             {
       
   204             TDpsEvent event = iXmlPar->Event();
       
   205             iOperator->SetEvent(event);
       
   206             if (event == EDpsEvtNotifyJobStatus)
       
   207                 {
       
   208                 User::LeaveIfError(iOperator->DpsEngine()->Event()->
       
   209                     iJobEvent.FillRepArgs(args, this));
       
   210                 }
       
   211             else
       
   212                 {
       
   213                 User::LeaveIfError(iOperator->DpsEngine()->Event()->
       
   214                     iPrinterEvent.FillRepArgs(args, this));
       
   215                 }
       
   216             CreateEventReplyL(event, result);    
       
   217             }
       
   218         else
       
   219             {
       
   220             // the request from the host is only this one:
       
   221             // GetFileID and used by DPOF printing 
       
   222             TDpsOperation ope = iXmlPar->Operation();
       
   223             iOperator->SetOperation(ope);
       
   224             if (iOperator->Operation() != EDpsOpGetFileID)
       
   225                 {
       
   226                 User::Leave(KErrNotSupported);
       
   227                 }    		    
       
   228             CreateRequestReplyL(args, result);    
       
   229             }	
       
   230         }
       
   231     CleanupStack::PopAndDestroy(parser);
       
   232     CleanupStack::PopAndDestroy(script);
       
   233     IF_DEBUG(Print(_L("<<<CDpsTransaction::ParseScript")));	
       
   234     }
       
   235         
       
   236 // ---------------------------------------------------------------------------
       
   237 // 
       
   238 // ---------------------------------------------------------------------------
       
   239 //	
       
   240 void CDpsTransaction::HandleHostRequestError(TInt aErr)
       
   241     {
       
   242     IF_DEBUG(Print(_L(">>>CDpsTransaction::HandleHostRequestError %d"), aErr));
       
   243     TDpsResult result; 
       
   244     // here we need to map the aErr to Dps standard error
       
   245     switch (aErr)
       
   246         {
       
   247         case KErrNotSupported: 
       
   248             result.iMajorCode = EDpsResultNotRecognized; 
       
   249             result.iMinorCode = EDpsResultNone;         
       
   250         break;
       
   251         
       
   252         case KErrArgument:
       
   253             result.iMajorCode = EDpsResultNotSupported; 
       
   254             result.iMinorCode = EDpsResultillegalParam;         
       
   255         break;
       
   256         
       
   257         default:
       
   258             IF_DEBUG(Print(_L("unknown err")));
       
   259             return;    
       
   260         }
       
   261     TRAP_IGNORE(CreateEventReplyL(iXmlPar->Event(), result));
       
   262     
       
   263     IF_DEBUG(Print(_L("<<<CDpsTransaction::HandleHostRequestError")));    
       
   264     return;
       
   265     }
       
   266 
       
   267 // ---------------------------------------------------------------------------
       
   268 // 
       
   269 // ---------------------------------------------------------------------------
       
   270 //	
       
   271 TInt CDpsTransaction::ConvertVersion(TLex8& aParser, TDpsVersion& aVersion)
       
   272     {
       
   273     aParser.Mark();
       
   274     while (!aParser.Eos())
       
   275         {
       
   276         TChar c = aParser.Peek();
       
   277         if (!c.IsDigit())
       
   278             {
       
   279             break;
       
   280             }
       
   281         aParser.Inc();    
       
   282         }
       
   283     TPtrC8 token = aParser.MarkedToken();
       
   284     TLex8 converter(token);    
       
   285     TInt error = converter.Val(aVersion.iMajor);
       
   286                         
       
   287     if (error != KErrNone)
       
   288         {
       
   289          IF_DEBUG(Print(_L("\t convert error 1")));
       
   290          return error;
       
   291         }
       
   292     IF_DEBUG(Print(_L("verion major %d"), aVersion.iMajor));
       
   293     aParser.Inc();
       
   294     TPtrC8 tokenDe = aParser.Remainder();
       
   295     converter.Assign(tokenDe);
       
   296     error = converter.Val(aVersion.iMinor);
       
   297     if (error != KErrNone)
       
   298         {
       
   299         IF_DEBUG(Print(_L("\t convert error 2")));
       
   300         return error;
       
   301         }
       
   302     IF_DEBUG(Print(_L("verion minor %d"), aVersion.iMinor));        
       
   303     return KErrNone;
       
   304     }
       
   305  
       
   306 // ---------------------------------------------------------------------------
       
   307 // 
       
   308 // ---------------------------------------------------------------------------
       
   309 //	
       
   310 TInt CDpsTransaction::ParsePercentage(const TDes8& aPer)  
       
   311     {
       
   312     TLex8 parser(aPer);
       
   313     parser.Mark();
       
   314     parser.Inc(KPercentagePosition);
       
   315     TPtrC8 digital = parser.MarkedToken();
       
   316     TLex8 converter(digital);
       
   317     TInt num1; 
       
   318     TInt error = converter.Val(num1);
       
   319     if (error != KErrNone)
       
   320         {
       
   321         return error;
       
   322         }
       
   323     
       
   324     parser.Inc(1);
       
   325     parser.Mark();
       
   326     parser.Inc(KPercentagePosition);
       
   327     digital.Set(parser.MarkedToken());
       
   328     converter.Assign(digital);
       
   329     TInt num2; 
       
   330     error = converter.Val(num2);    
       
   331     if (error != KErrNone)
       
   332         {
       
   333         return error;
       
   334         }
       
   335     
       
   336     TReal per = (TReal)num1 / (TReal)num2 * KPercentage;
       
   337     return (TInt)per;
       
   338     }
       
   339  
       
   340 // ---------------------------------------------------------------------------
       
   341 // 
       
   342 // ---------------------------------------------------------------------------
       
   343 //    
       
   344 void CDpsTransaction::CreateEventReplyL(TDpsEvent aEvent, 
       
   345                                         const TDpsResult& aResult)
       
   346                                    
       
   347 	{
       
   348 	IF_DEBUG(Print(_L(">>>CDpsTransaction::CreateReply")));
       
   349 	IF_DEBUG(Print(_L
       
   350 	    ("  the operation reply is %d"), iOperator->Operation()));
       
   351 	IF_DEBUG(Print(_L("\t the event reply is %d"), iOperator->Event()));
       
   352     RFileWriteStream writer;
       
   353     writer.PushL();
       
   354     TFileName defaultPath = iOperator->DpsEngine()->DpsFolder();
       
   355     defaultPath.Append(KDpsDeviceResponseFileName);
       
   356     User::LeaveIfError(writer.Replace(iFile->FileSession(), defaultPath, 
       
   357                                       EFileWrite));
       
   358     IF_DEBUG(Print(_L("*** file created  ")));     
       
   359     iXmlGen->CreateResultScriptL(aEvent, writer, aResult);
       
   360     writer.CommitL();
       
   361     writer.Pop();
       
   362     writer.Release();
       
   363 	User::LeaveIfError(iOperator->ScriptSender()->SendScript(ETrue));
       
   364     IF_DEBUG(Print(_L("<<<CDpsTransaction::CreateReply")));
       
   365 	}
       
   366 
       
   367 // ---------------------------------------------------------------------------
       
   368 // 
       
   369 // ---------------------------------------------------------------------------
       
   370 //    
       
   371 void CDpsTransaction::CreateRequestReplyL(const TDpsArgArray& aArgs, 
       
   372                                           const TDpsResult& aResult)
       
   373     {
       
   374     IF_DEBUG(Print(_L(">>>CDpsTransaction::CreateRequestReply")));
       
   375     TInt count = aArgs.Count();
       
   376     TInt basePathId;
       
   377     TBuf8<KMaxArgLen> filePath;
       
   378     TLex8 converter;
       
   379     
       
   380     for (TInt i = 0; i < count; i++)
       
   381         {
       
   382         switch (aArgs[i].iElement)
       
   383             {
       
   384             case EDpsArgBasePathID:
       
   385                 converter.Assign(aArgs[i].iContent);
       
   386                 User::LeaveIfError(converter.Val(basePathId));
       
   387             break;
       
   388             
       
   389             case EDpsArgFilePath:
       
   390                 filePath.Copy(aArgs[i].iContent);
       
   391             break;
       
   392             
       
   393             default:
       
   394                 __IF_DEBUG(Print(_L("***wrong args")));
       
   395                 User::Leave(KErrArgument);
       
   396             break;
       
   397             }
       
   398         TUint32 fileId;
       
   399         
       
   400         SubstitutePath(filePath);
       
   401         TBuf<KMaxArgLen> file;  
       
   402         file.Copy(filePath);
       
   403         User::LeaveIfError(iOperator->DpsEngine()->
       
   404             Ptp().GetObjectHandleByName(file, fileId));
       
   405         TDpsArg arg;
       
   406         arg.iElement = EDpsArgFileID;
       
   407         arg.iContent.AppendNumFixedWidth(fileId, EHex, KFullWordWidth);
       
   408         RFileWriteStream writer;
       
   409         writer.PushL();
       
   410         TFileName defaultPath = iOperator->DpsEngine()->DpsFolder();
       
   411         defaultPath.Append(KDpsDeviceResponseFileName);
       
   412         User::LeaveIfError(writer.Replace(iFile->FileSession(), defaultPath, 
       
   413                                           EFileWrite));
       
   414         IF_DEBUG(Print(_L("*** file created  ")));        
       
   415         iXmlGen->CreateReplyScriptL(EDpsOpGetFileID, writer, aResult, arg);   
       
   416 	    User::LeaveIfError(iOperator->ScriptSender()->SendScript(ETrue));
       
   417 	    writer.CommitL();
       
   418         writer.Pop();
       
   419         writer.Release();    
       
   420         } 
       
   421     IF_DEBUG(Print(_L("<<<CDpsTransaction::CreateRequestReply")));
       
   422     }
       
   423     
       
   424 // ---------------------------------------------------------------------------
       
   425 // The aPath is not the full file path, at least the
       
   426 // driver letter is not included. The structure of the DPOF filePath is
       
   427 // ../path/childpath/imagePrinted.jpg and GetObjectHandleByName
       
   428 // requires the full path file as e:\images\image1.jpg
       
   429 // the basePathId is not useful because it is always the fileId
       
   430 // of AUTPRINT.MRK. But since DPOF is always used for the removable
       
   431 // media, we assume that images are only stored in removable media.
       
   432 // If the assumption is true (must be true), we need here first to
       
   433 // get the removable drive (hopefully only one). Then substitute
       
   434 // the / by the \ in the filePath. Finally, insert the removable drive
       
   435 // letter and : at the beginning of the filePath. The new full path
       
   436 // file can be used by GetObjectHandleByName
       
   437 // ---------------------------------------------------------------------------
       
   438 //        
       
   439 void CDpsTransaction::SubstitutePath(TDes8& aPath)
       
   440     {
       
   441     IF_DEBUG(Print(_L(">>>CDpsTransaction::SubstitutePath %S"), &aPath));
       
   442     TInt size = aPath.Size();
       
   443     for (TInt i = 0; i < size; i++)
       
   444         {
       
   445         if (aPath[i] == KSlash)
       
   446             {
       
   447             aPath[i] = KBackSlash;
       
   448             }
       
   449         }
       
   450     TBuf<KPathLength> driveEWide = PathInfo::MemoryCardRootPath();
       
   451     TBuf8<KPathLength> driveENarrow;
       
   452     driveENarrow.Copy(driveEWide);    
       
   453     aPath.Replace(0, KPathLength - 1, driveENarrow);
       
   454     IF_DEBUG(Print(_L("<<<CDpsTransaction::SubstitutePath %S"), &aPath));
       
   455     }