examples/SysLibs/ECom/InterfaceClient/InterfaceClient.cpp

00001 /*
00002 Copyright (c) 2001-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:  
00028 */
00029 
00030 
00031 #include <badesca.h>
00032 
00033 #include <interface.h>
00034 
00035 #include "CommonFramework.h"
00036 #include "InterfaceClient.h"
00037 
00038 // Utility clean up function
00039 void CleanupEComArray(TAny* aArray);
00040 
00041 // do the example
00042 LOCAL_C void doExampleL()
00043     {
00044         _LIT(KText,"ECom client example\n\n");
00045         console->Printf(KText);
00046 
00047         TInterfaceClient client;
00048         client.GetDefaultL();
00049         client.GetBySpecificationL();
00050         client.GetByDiscoveryL();
00051         
00052         REComSession::FinalClose();
00053         }
00054 
00055 void TInterfaceClient::GetDefaultL()
00056         {
00057         _LIT(KText,"Case 1: getting the default implementation\n");
00058         console->Printf(KText);
00059 
00060         // Get the default implementation and call its method
00061         CExampleInterface* ex1 = CExampleInterface::NewL();
00062         CleanupStack::PushL(ex1);
00063         TBuf<100> buf;
00064         ex1 -> DoMethodL(buf);
00065         CleanupStack::PopAndDestroy(); //ex1
00066 
00067         // Print results
00068         console -> Printf(buf);
00069         }
00070 
00071 void TInterfaceClient::GetBySpecificationL()
00072         {
00073         _LIT(KText,"\nCase 2: getting an implementation by specification\n");
00074         console->Printf(KText);
00075 
00076         // Prepare data to pass to implementation
00077         CExampleInterface::TExampleInterfaceInitParams p;
00078         p.integer = 0;
00079         _LIT(KMsg1,"Data in value: %d\n");
00080         console -> Printf(KMsg1,p.integer);
00081 
00082         // Get the implementation that has a data identifier text/xml
00083         _LIT8(KSpec,"text/xml");
00084         CExampleInterface* ex1 = CExampleInterface::NewL(KSpec,p);
00085         CleanupStack::PushL(ex1);
00086         TBuf<100> buf;
00087         ex1 -> DoMethodL(buf);
00088         CleanupStack::PopAndDestroy(); //ex1
00089 
00090         // Print results
00091         console -> Printf(buf);
00092         _LIT(KMsg2,"Data out value: %d\n");
00093         console -> Printf(KMsg2,p.integer);
00094         }
00095 
00096 
00097 void TInterfaceClient::GetByDiscoveryL()
00098         {
00099         _LIT(KText,"\nCase 3: getting all implementations\n");
00100         console->Printf(KText);
00101 
00102         // Read info about all implementations into infoArray
00103         RImplInfoPtrArray infoArray;
00104         // Note that a special cleanup function is required to reset and destroy
00105         // all items in the array, and then close it.
00106         TCleanupItem cleanup(CleanupEComArray, &infoArray);
00107         CleanupStack::PushL(cleanup);
00108         CExampleInterface::ListAllImplementationsL(infoArray);
00109 
00110         // Loop through each info for each implementation
00111         // and create and use each in turn
00112         CExampleInterface* ex;
00113         TBuf<255> buf;
00114         for (TInt i=0; i< infoArray.Count(); i++)
00115                 {
00116                 // Slice off first sub-section in the data section
00117                 TPtrC8 type = infoArray[i]->DataType();
00118                 type.Set(type.Left(type.Locate('|')));
00119                 // Need to convert narrow descriptor to be wide in order to print it
00120                 buf.Copy(type);
00121                 console -> Printf(buf);
00122                 console -> Printf(_L(": "));
00123 
00124                 // Create object of type and call its function
00125                 ex = CExampleInterface::NewL(type);
00126                 CleanupStack::PushL(ex);
00127                 ex -> DoMethodL(buf);
00128                 CleanupStack::PopAndDestroy(); //ex
00129 
00130                 // Print results
00131                 console -> Printf(buf); 
00132                 
00133                 ex = NULL;
00134                 buf.Zero();
00135                 }
00136 
00137         // Clean up
00138         CleanupStack::PopAndDestroy(); //infoArray, results in a call to CleanupEComArray
00139         }
00140 
00141 // CleanupEComArray function is used for cleanup support of locally declared arrays
00142 void CleanupEComArray(TAny* aArray)
00143         {
00144         (static_cast<RImplInfoPtrArray*> (aArray))->ResetAndDestroy();
00145         (static_cast<RImplInfoPtrArray*> (aArray))->Close();
00146         }
00147 

Generated by  doxygen 1.6.2