uifw/AvKon/src/AknOpenFileService.cpp
changeset 0 2f259fa3e83a
equal deleted inserted replaced
-1:000000000000 0:2f259fa3e83a
       
     1 /*
       
     2 * Copyright (c) 2004 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:  Server applications framework: Open file service
       
    15  *
       
    16  *
       
    17 */
       
    18 
       
    19 
       
    20 #include <apgcli.h>
       
    21 #include <eikenv.h>
       
    22 #include <eikappui.h>
       
    23 #include <eikdoc.h>
       
    24 #include <s32mem.h> // for CBufStore
       
    25 #include <s32std.h> // for CStreamDictionary
       
    26 #include <AiwGenericParam.h>
       
    27 #include "AknOpenFileServiceImpl.h"
       
    28 
       
    29 const TUid KUidEmbedInputStream  = { 0x1000 };
       
    30 // const TUid KUidEmbedOutputStream = { 0x2000 };
       
    31 
       
    32 // ---------------------------------------------------------
       
    33 //
       
    34 // ---------------------------------------------------------
       
    35 //
       
    36 void RAknOpenFileService::OpenFileNameL(const TDesC& aFileName)
       
    37     {
       
    38     TInt err = SendReceive(EAknOpenFileServiceCmdOpenFilename, TIpcArgs(&aFileName));
       
    39     User::LeaveIfError(err);
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------
       
    43 //
       
    44 // ---------------------------------------------------------
       
    45 //
       
    46 void RAknOpenFileService::OpenFileNameL(const TDesC& aFileName, CAiwGenericParamList* aParam)
       
    47     {
       
    48     TIpcArgs args;
       
    49     HBufC8* buf = aParam->PackForServerL(args); // packs params 0 and 1.
       
    50 
       
    51     // pack more params.
       
    52     args.Set(2, &aFileName);
       
    53 
       
    54     TInt err = SendReceive(EAknOpenFileServiceCmdOpenFilenameWithParams, args);
       
    55 
       
    56     delete buf;
       
    57 
       
    58     User::LeaveIfError(err);
       
    59     }
       
    60 
       
    61 // ---------------------------------------------------------
       
    62 //
       
    63 // ---------------------------------------------------------
       
    64 //
       
    65 void RAknOpenFileService::OpenHandleL(const RFile& aFile, CAiwGenericParamList* aParam)
       
    66     {
       
    67     TAiwVariant filehandle(aFile);
       
    68     TAiwGenericParam param(EGenericParamInternalFile, filehandle);
       
    69     aParam->AppendL(param);
       
    70     
       
    71     TIpcArgs args;
       
    72     HBufC8* buf = aParam->PackForServerL(args); // packs params 0 and 1.
       
    73        
       
    74     TInt err = SendReceive(EAknOpenFileServiceCmdOpenHandleWithParams, args);
       
    75     
       
    76     delete buf;
       
    77     
       
    78     User::LeaveIfError(err);
       
    79     }
       
    80 
       
    81 // ---------------------------------------------------------
       
    82 //
       
    83 // ---------------------------------------------------------
       
    84 //
       
    85 TUid RAknOpenFileService::ServiceUid() const
       
    86     {
       
    87     return KAknOpenFileServiceUid;
       
    88     }
       
    89 
       
    90 
       
    91 
       
    92 // ---------------------------------------------------------
       
    93 //
       
    94 // ---------------------------------------------------------
       
    95 //
       
    96 CAknOpenFileServiceSession::CAknOpenFileServiceSession()
       
    97 : iDoc(CEikonEnv::Static()->EikAppUi()->Document())
       
    98     {
       
    99     }
       
   100 
       
   101 // ---------------------------------------------------------
       
   102 //
       
   103 // ---------------------------------------------------------
       
   104 //
       
   105 CAknOpenFileServiceSession::~CAknOpenFileServiceSession()
       
   106     {
       
   107     delete iStore;
       
   108     delete iDictionary;
       
   109     }
       
   110 
       
   111 // ---------------------------------------------------------
       
   112 //
       
   113 // ---------------------------------------------------------
       
   114 //
       
   115 void CAknOpenFileServiceSession::CreateL()
       
   116     {
       
   117     CAknAppServiceBase::CreateL();
       
   118     }
       
   119 
       
   120 // ---------------------------------------------------------
       
   121 //
       
   122 // ---------------------------------------------------------
       
   123 //
       
   124 void CAknOpenFileServiceSession::ServiceL(const RMessage2& aMessage)
       
   125     {
       
   126     switch (aMessage.Function())
       
   127         {
       
   128         case EAknOpenFileServiceCmdOpenFilename:
       
   129             OpenFileNameL(aMessage);
       
   130             break;
       
   131 
       
   132         case EAknOpenFileServiceCmdOpenFilenameWithParams:
       
   133             OpenFileNameWithParamsL(aMessage);
       
   134             break;
       
   135 
       
   136         case EAknOpenFileServiceCmdOpenHandleWithParams:
       
   137             OpenHandleWithParamsL(aMessage);
       
   138             break;
       
   139 
       
   140         default:
       
   141             CAknAppServiceBase::ServiceL(aMessage);
       
   142             break;
       
   143         }
       
   144     }
       
   145 
       
   146 // ---------------------------------------------------------
       
   147 //
       
   148 // ---------------------------------------------------------
       
   149 //
       
   150 void CAknOpenFileServiceSession::ServiceError(const RMessage2& aMessage,TInt aError)
       
   151     {
       
   152     CAknAppServiceBase::ServiceError(aMessage, aError);
       
   153     }
       
   154 
       
   155 // ---------------------------------------------------------
       
   156 //
       
   157 // ---------------------------------------------------------
       
   158 //
       
   159 void CAknOpenFileServiceSession::OpenFileNameL(const RMessage2& aMessage)
       
   160     {
       
   161     CAiwGenericParamList* paramList = CAiwGenericParamList::NewLC();
       
   162 
       
   163     TSecureId clientSid = aMessage.SecureId();
       
   164     TAiwVariant clientAppUid((TUid)clientSid);
       
   165     TAiwGenericParam uid(EGenericParamApplication, clientAppUid);
       
   166     paramList->AppendL(uid);
       
   167 
       
   168     ExternalizeParamsL(paramList);
       
   169 
       
   170     CleanupStack::PopAndDestroy(); // paramList
       
   171 
       
   172     TFileName fileName;
       
   173     aMessage.Read(0, fileName);
       
   174 
       
   175     iDoc->OpenFileL(ETrue, fileName, CEikonEnv::Static()->FsSession());
       
   176 
       
   177     aMessage.Complete(KErrNone);
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------
       
   181 //
       
   182 // ---------------------------------------------------------
       
   183 //
       
   184 void CAknOpenFileServiceSession::OpenFileNameWithParamsL(const RMessage2& aMessage)
       
   185     {
       
   186     CAiwGenericParamList* paramList = CAiwGenericParamList::NewLC();
       
   187     paramList->UnpackFromClientL(aMessage);
       
   188 
       
   189     TSecureId clientSid = aMessage.SecureId();
       
   190     TAiwVariant clientAppUid((TUid)clientSid);
       
   191     TAiwGenericParam uid(EGenericParamApplication, clientAppUid);
       
   192     paramList->AppendL(uid);
       
   193 
       
   194     ExternalizeParamsL(paramList);
       
   195 
       
   196     CleanupStack::PopAndDestroy(); // paramList
       
   197 
       
   198     TFileName fileName;
       
   199     aMessage.Read(2, fileName);
       
   200 
       
   201     iDoc->OpenFileL(ETrue, fileName, CEikonEnv::Static()->FsSession());
       
   202 
       
   203     aMessage.Complete(KErrNone);
       
   204     }
       
   205 
       
   206 
       
   207 // ---------------------------------------------------------
       
   208 //
       
   209 // ---------------------------------------------------------
       
   210 //
       
   211 void CAknOpenFileServiceSession::OpenHandleWithParamsL(const RMessage2& aMessage)
       
   212     {
       
   213     CAiwGenericParamList* paramList = CAiwGenericParamList::NewLC();
       
   214     paramList->UnpackFromClientL(aMessage);
       
   215 
       
   216     TSecureId clientSid = aMessage.SecureId();
       
   217     TAiwVariant clientAppUid((TUid)clientSid);
       
   218     TAiwGenericParam uid(EGenericParamApplication, clientAppUid);
       
   219     paramList->AppendL(uid);
       
   220 
       
   221     RFile fileHandle;
       
   222     TInt index = 0;
       
   223     const TAiwGenericParam* param = NULL;
       
   224 
       
   225     param = paramList->FindFirst(index, EGenericParamInternalFile, EVariantTypeFileHandle);
       
   226 
       
   227     if (index >= 0 && param)
       
   228         {
       
   229         param->Value().Get(fileHandle);
       
   230         paramList->Remove(EGenericParamInternalFile);
       
   231         }
       
   232 
       
   233     ExternalizeParamsL(paramList);
       
   234 
       
   235     CleanupStack::PopAndDestroy(); // paramList
       
   236 
       
   237     CFileStore* filestore=NULL;
       
   238     iDoc->OpenFileL(filestore,fileHandle);
       
   239 
       
   240     aMessage.Complete(KErrNone);
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------
       
   244 //
       
   245 // ---------------------------------------------------------
       
   246 //
       
   247 void CAknOpenFileServiceSession::ExternalizeParamsL(CAiwGenericParamList* aParamList)
       
   248     {
       
   249     // Create an in-memory store of parameters.
       
   250     delete iStore;
       
   251     iStore = NULL;
       
   252     iStore = CBufStore::NewL(127);
       
   253 
       
   254     RStoreWriteStream outStream;
       
   255     iStreamId = outStream.CreateLC(*iStore);
       
   256     aParamList->ExternalizeL(outStream);
       
   257     iStore->CommitL();
       
   258     CleanupStack::PopAndDestroy(); // outStream
       
   259 
       
   260     delete iDictionary;
       
   261     iDictionary = NULL;
       
   262     iDictionary = CStreamDictionary::NewL();
       
   263 
       
   264     iDictionary->AssignL(KUidEmbedInputStream, iStreamId);
       
   265     outStream.CreateLC(*iStore);
       
   266     iDictionary->ExternalizeL(outStream);
       
   267     iStore->CommitL();
       
   268     CleanupStack::PopAndDestroy(); // outstream
       
   269 
       
   270     RStoreReadStream readStream;
       
   271     readStream.OpenLC(*iStore, iStreamId);
       
   272     iDoc->RestoreL(*iStore, *iDictionary);
       
   273     CleanupStack::PopAndDestroy(); // readStream
       
   274     }
       
   275 
       
   276 
       
   277 // ---------------------------------------------------------
       
   278 //
       
   279 // ---------------------------------------------------------
       
   280 //
       
   281 EXPORT_C CAknOpenFileService* CAknOpenFileService::NewL(
       
   282     const TDesC& aFileName, MAknServerAppExitObserver* aObserver, CAiwGenericParamList* aParam)
       
   283     {
       
   284     CAknOpenFileService* self = NewLC(aFileName, aObserver, aParam);
       
   285     CleanupStack::Pop(self);
       
   286     return self;
       
   287     }
       
   288 
       
   289 // ---------------------------------------------------------
       
   290 //
       
   291 // ---------------------------------------------------------
       
   292 //
       
   293 CAknOpenFileService* CAknOpenFileService::NewLC(
       
   294     const TDesC& aFileName, MAknServerAppExitObserver* aObserver, CAiwGenericParamList* aParam)
       
   295     {
       
   296     CAknOpenFileServiceImpl* self = new (ELeave) CAknOpenFileServiceImpl();
       
   297     CleanupStack::PushL(self);
       
   298     self->ConstructL(aFileName, aObserver, aParam);
       
   299     return self;
       
   300     }
       
   301 
       
   302 // ---------------------------------------------------------
       
   303 //
       
   304 // ---------------------------------------------------------
       
   305 //
       
   306 EXPORT_C CAknOpenFileService* CAknOpenFileService::NewL(
       
   307     const TUid& aAppUid, const RFile& aFileHandle, MAknServerAppExitObserver* aObserver, CAiwGenericParamList* aParam)
       
   308     {
       
   309     CAknOpenFileService* self = NewLC(aAppUid, aFileHandle, aObserver, aParam);
       
   310     CleanupStack::Pop(self);
       
   311     return self;
       
   312     }
       
   313 
       
   314 
       
   315 // ---------------------------------------------------------
       
   316 //
       
   317 // ---------------------------------------------------------
       
   318 //
       
   319 CAknOpenFileService* CAknOpenFileService::NewLC(
       
   320     const TUid& aAppUid, const RFile& aFileHandle, MAknServerAppExitObserver* aObserver, CAiwGenericParamList* aParam)
       
   321     {
       
   322     CAknOpenFileServiceImpl* self = new (ELeave) CAknOpenFileServiceImpl();
       
   323     CleanupStack::PushL(self);
       
   324     self->ConstructL(aAppUid, aFileHandle, aObserver, aParam);
       
   325     return self;
       
   326     }
       
   327 
       
   328 // ---------------------------------------------------------
       
   329 //
       
   330 // ---------------------------------------------------------
       
   331 //
       
   332 CAknOpenFileServiceImpl::CAknOpenFileServiceImpl()
       
   333     {
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------
       
   337 //
       
   338 // ---------------------------------------------------------
       
   339 //
       
   340 CAknOpenFileServiceImpl::~CAknOpenFileServiceImpl()
       
   341     {
       
   342     delete iMonitor;
       
   343     iService.Close();
       
   344     }
       
   345 
       
   346 // ---------------------------------------------------------
       
   347 //
       
   348 // ---------------------------------------------------------
       
   349 //
       
   350 void CAknOpenFileServiceImpl::ConstructL(
       
   351     const TDesC& aFileName, MAknServerAppExitObserver* aObserver, CAiwGenericParamList* aParam)
       
   352     {
       
   353     TUid app = FindAppForFileL(aFileName);
       
   354     iService.ConnectChainedAppL(app);
       
   355 
       
   356     if (aObserver)
       
   357         {
       
   358         iMonitor = CApaServerAppExitMonitor::NewL(iService, *aObserver, EActivePriorityRedrawEvents+1);	// priority used to get exit messages before the client app redraws
       
   359         }
       
   360 
       
   361     if (aParam && aParam->Count())
       
   362         {
       
   363         iService.OpenFileNameL(aFileName, aParam);
       
   364         }
       
   365     else
       
   366         {
       
   367         iService.OpenFileNameL(aFileName);
       
   368         }
       
   369     }
       
   370 
       
   371 // ---------------------------------------------------------
       
   372 //
       
   373 // ---------------------------------------------------------
       
   374 //
       
   375 void CAknOpenFileServiceImpl::ConstructL(
       
   376     const TUid& aAppUid, const RFile& aFileHandle, MAknServerAppExitObserver* aObserver, CAiwGenericParamList* aParam)
       
   377     {
       
   378     iService.ConnectChainedAppL(aAppUid);
       
   379 
       
   380     if (aObserver)
       
   381         {
       
   382         iMonitor = CApaServerAppExitMonitor::NewL(iService, *aObserver, EActivePriorityRedrawEvents+1);	// priority used to get exit messages before the client app redraws
       
   383         }
       
   384 
       
   385     iService.OpenHandleL(aFileHandle, aParam);
       
   386     }
       
   387 
       
   388 // ---------------------------------------------------------
       
   389 //
       
   390 // ---------------------------------------------------------
       
   391 //
       
   392 TUid CAknOpenFileServiceImpl::FindAppForFileL(const TDesC& aFileName)
       
   393     {
       
   394     TUid uid;
       
   395     TDataType type;
       
   396 
       
   397     RApaLsSession ls;
       
   398     User::LeaveIfError(ls.Connect());
       
   399     CleanupClosePushL(ls);
       
   400     User::LeaveIfError(ls.AppForDocument(aFileName, uid, type));
       
   401     CleanupStack::PopAndDestroy(&ls);
       
   402 
       
   403     return uid;
       
   404     }
       
   405 
       
   406 // End of file.