examples/NFC/nfcshareexample/nfcaiwengine.cpp

00001 /*
00002  * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003  *  
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  * 
00007  * Redistributions of source code must retain the above copyright notice, this
00008  *  list of conditions and the following disclaimer.
00009  * Redistributions in binary form must reproduce the above copyright notice,
00010  *   this list of conditions and the following disclaimer in the documentation
00011  *   and/or other materials provided with the distribution.
00012  * Neither the name of Nokia Corporation nor the names of its contributors
00013  *  may be used to endorse or promote products derived from this software
00014  * without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  * Description:  Source file for nfc AIW handler engine class.
00028  * 
00029  */
00030 
00031 // User Include Files.
00032 #include "nfcsharewrapper.h"
00033 #include "nfcaiwengine.h"
00034 
00035 /*
00036  * Constructor which will not leave. 
00037  */
00038 CNfcAiwEngine::CNfcAiwEngine(NfcShareWrapper* aNfcShareWrapper) : iNfcShareWrapper(aNfcShareWrapper)
00039     {
00040     }
00041 
00042 /*
00043  * Two phase Constructor.
00044  * @param aNfcShareWrapper The wrapper class pointer.
00045  */
00046 CNfcAiwEngine* CNfcAiwEngine::NewL(NfcShareWrapper* aNfcShareWrapper)
00047     {
00048     CNfcAiwEngine* self = CNfcAiwEngine::NewLC(aNfcShareWrapper);
00049     CleanupStack::Pop();
00050     return self;
00051     }
00052 
00053 /*
00054  * Two phase Constructor.
00055  * @param aNfcShareWrapper The wrapper class pointer.
00056  */
00057 CNfcAiwEngine* CNfcAiwEngine::NewLC(NfcShareWrapper* aNfcShareWrapper)
00058     {
00059     CNfcAiwEngine* self = new (ELeave) CNfcAiwEngine(aNfcShareWrapper);
00060     CleanupStack::PushL(self);
00061     self->ConstructL();
00062     return self;
00063     }
00064 
00065 /*
00066  * Standard EPOC 2nd phase constructor.
00067  */ 
00068 void CNfcAiwEngine::ConstructL()
00069     {
00070     iAiwServiceHandler = CAiwServiceHandler::NewL();
00071     }
00072 
00073 /*
00074  * Destructor.
00075  */
00076 CNfcAiwEngine::~CNfcAiwEngine()
00077     {
00078     iFs.Close();
00079     if (iAiwServiceHandler)
00080         {
00081          delete iAiwServiceHandler;
00082          iAiwServiceHandler = NULL;
00083         }
00084     }
00085 
00086 /*
00087  * Starts the easy setup service.
00088  */
00089 TInt CNfcAiwEngine::StartEasySetupServiceL()
00090     {
00091     // Create AIW interest.
00092     RCriteriaArray interest;
00093     CleanupClosePushL(interest);
00094     TUid base;
00095     _LIT8(KContentType,"*");
00096     base.iUid = KAiwClassBase;
00097     CAiwCriteriaItem* criteria = CAiwCriteriaItem::NewLC( KAiwCmdNFCEasySetup, KAiwCmdNFCEasySetup, KContentType );
00098     criteria->SetServiceClass(base);
00099     User::LeaveIfError(interest.Append(criteria));
00100 
00101     // Attach to AIW interest.
00102     iAiwServiceHandler->Reset();
00103     iAiwServiceHandler->AttachL(interest);
00104     
00105     iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCEasySetup,
00106             iAiwServiceHandler->InParamListL(),
00107             iAiwServiceHandler->OutParamListL(), KAiwOptASyncronous, this);
00108     CleanupStack::PopAndDestroy(2); // criteria, interest. 
00109     return KErrNone;
00110     }
00111 
00112 /*
00113  * StartSharingServiceL(NfcSharingType aSharingType)
00114  * starts the sharing of files based on sharing type parameter.
00115  * @param aSharingType specifies the type of file shared.
00116  */
00117 TInt CNfcAiwEngine::StartSharingServiceL(NfcSharingType aSharingType)
00118     {
00119     // Create AIW interest.
00120     RCriteriaArray interest;
00121     CleanupClosePushL(interest);
00122     iSharingType = aSharingType;
00123     TUid base;
00124     base.iUid = KAiwClassBase;
00125     _LIT8(KContentType,"*");
00126     CAiwCriteriaItem* criteria = CAiwCriteriaItem::NewLC( KAiwCmdNFCGiveUi, KAiwCmdNFCGiveUi, KContentType);
00127     criteria->SetServiceClass(base);
00128     User::LeaveIfError(interest.Append(criteria));
00129 
00130     // Attach to AIW interest.
00131     iAiwServiceHandler->Reset();
00132     iAiwServiceHandler->AttachL(interest);
00133 
00134     iAiwServiceHandler->ExecuteServiceCmdL(KAiwCmdNFCGiveUi,
00135             iAiwServiceHandler->InParamListL(),
00136             iAiwServiceHandler->OutParamListL(), KAiwOptASyncronous, this);
00137     CleanupStack::PopAndDestroy(2); // criteria, interest.
00138     return KErrNone;
00139     }
00140 
00141 /*
00142 * Handles notifications caused by an asynchronous Execute CommandL call
00143 * or an event. 
00144 * The result of command processing is sent as signal to UI. 
00145 * @param aCmdId The service command associated to the event.
00146 * @param aEventId Occured event, see AiwCommon.hrh.
00147 * @param aEventParamList Event parameters, if any, as defined per
00148 *        each event.
00149 * @param aInParamList Input parameters, if any, given in the
00150 *        related HandleCommmandL.
00151 * @return Error code for the callback.
00152 */
00153 
00154 TInt CNfcAiwEngine::HandleNotifyL(TInt aCmdId, TInt aEventId,
00155         CAiwGenericParamList& /* aEventParamList */,
00156         const CAiwGenericParamList& /* aInParamList */)
00157     {
00158     
00159     // Process based on command id and the event id.
00160     if (aCmdId == KAiwCmdNFCEasySetup && aEventId == KAiwEventStarted)
00161         {  
00162         QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated
00163                 ("Easy setup will complete, if you tap it with a NFC device.")));
00164 
00165         }    
00166     else if (aCmdId == KAiwCmdNFCGiveUi && aEventId == KAiwEventStarted)
00167         {
00168         TInt err = KErrNone;
00169         if (ENfcvCard == iSharingType) 
00170             { 
00171             QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated
00172                     ("sharing vCard started.")));
00173             DoCommandL( KVCardFileName, err );
00174             }
00175         else if(ENfcvCal == iSharingType)
00176             { 
00177             QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated
00178                                 ("sharing vCal started.")));
00179             DoCommandL( KCalenderFileName, err );
00180             }
00181         }
00182     else if ( aCmdId == KAiwCmdNFCGiveUi && aEventId == KAiwEventCompleted)
00183         {
00184         
00185         // Send nothification to UI that file sharing is complete.
00186         if (ENfcvCard == iSharingType) 
00187             { 
00188             QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("vCard sharing is successfully completed")));
00189             }
00190         else if(ENfcvCal == iSharingType)
00191             { 
00192             QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("vCal sharing is successfully completed")));
00193             }        
00194         iAiwServiceHandler->Reset();
00195         } 
00196 
00197     else if ( aCmdId == KAiwCmdNFCEasySetup && aEventId == KAiwEventCompleted )
00198         {
00199         
00200         // Send notification to the UI that easy set up is complete.
00201         QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("Easy set up is complete.")));
00202         iAiwServiceHandler->Reset();
00203         }
00204     else if ( aCmdId == KAiwCmdNFCGiveUi && aEventId == KAiwEventError )
00205         {
00206         
00207         // Send notifcation to the UI that error occured during asynchronous tranfer request. 
00208         QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("Error during asynchronous transfer request! Please try again")));
00209         iAiwServiceHandler->Reset();
00210         }
00211     else if ( aCmdId == KAiwCmdNFCEasySetup && aEventId == KAiwEventError )
00212         {
00213         
00214         // Send notifcation to the UI that error occured during asynchronous setup request.
00215         QT_TRYCATCH_LEAVING(emit(iNfcShareWrapper->windowUpdated("Error during asynchronous service setup request!")));
00216         iAiwServiceHandler->Reset();
00217         }
00218     
00219     else if ( aEventId == KAiwEventCanceled )
00220         {
00221         
00222         // Sharing cancelled.
00223         iAiwServiceHandler->Reset();
00224         }
00225     else if ( aEventId == KAiwEventError )
00226         {
00227         
00228         // AIW event failure. so reset the service handler.
00229         iAiwServiceHandler->Reset();
00230         }
00231     else
00232         {
00233         
00234         // Sharing failed, so reset the service handler.
00235         iAiwServiceHandler->Reset();
00236         }
00237     return KErrNone;
00238     }
00239 
00240 /*
00241  * StopserviceL: stop the AIW service handlers from taking new request by deleting handler
00242  * and create a new handler for subsequent uses.
00243  */
00244 void CNfcAiwEngine::StopServiceL()
00245     {
00246     iAiwServiceHandler->Reset();
00247     if(iAiwServiceHandler)
00248         {
00249         delete iAiwServiceHandler;
00250         }
00251     iAiwServiceHandler = CAiwServiceHandler::NewL();
00252     }
00253 
00254 /*
00255  * DoCommandL
00256  * Executes the command to share the file via NFC channel(KAiwCmdNFCGive)
00257  * @param aFileName The file name to be shared.
00258  * @param error Failure value returned to the calling method.
00259  */
00260 TInt CNfcAiwEngine::DoCommandL( const TDesC& aFileName, TInt error )
00261     {
00262     error = iFs.Connect();
00263     if (error == KErrNone)
00264         {
00265         error = iFs.ShareProtected();
00266         }
00267 
00268     if (error == KErrNone)
00269         {// Open file to be shared in read or sharereadonly mode.
00270         error = iFile.Open(iFs, aFileName, EFileRead | EFileShareReadersOnly);
00271         }
00272     if (error != KErrNone)
00273         {
00274         return error;
00275         }
00276      
00277      CAiwGenericParamList* inParamList = CAiwGenericParamList::NewLC();
00278      TAiwGenericParam obj(EGenericParamFile, TAiwVariant(iFile));
00279      
00280      // Attach the file to inParamList.
00281      inParamList->AppendL(obj);
00282      
00283      // Send the file via NFC (asynchronous).
00284      iAiwServiceHandler->ExecuteServiceCmdL( KAiwCmdNFCGiveUi, *inParamList,
00285                                              iAiwServiceHandler->OutParamListL(), KAiwOptASyncronous,
00286                                              this);
00287      CleanupStack::PopAndDestroy(); // inParamList.
00288      iFile.Close(); 
00289      iFs.Close();
00290      return error;
00291     }

Generated by  doxygen 1.6.2