mtpdataproviders/mtpimagedp/mediasyncserver/src/rmediasyncserver.cpp
changeset 0 d0791faffa3f
equal deleted inserted replaced
-1:000000000000 0:d0791faffa3f
       
     1 // Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalTechnology
       
    19 */
       
    20 
       
    21 #include <e32base.h>
       
    22 
       
    23 #include "rmediasyncserver.h"
       
    24 
       
    25 const TInt KDefGlobalSharedHeapSize = 64 * 1024;//64K byte memory
       
    26 
       
    27 //-----------------------------------------------------------------------------
       
    28 //
       
    29 //-----------------------------------------------------------------------------
       
    30 //
       
    31 EXPORT_C RMediaSyncServer::RMediaSyncServer() :
       
    32     iHasSharedHeap(EFalse)
       
    33     {
       
    34     }
       
    35 
       
    36 EXPORT_C TInt RMediaSyncServer::Startup()
       
    37     {
       
    38     const TUidType serverUid(KNullUid, KNullUid, KMediaSyncServerUid3);
       
    39     
       
    40     // Create the server process.
       
    41     RProcess server;
       
    42     TInt err(server.Create(KMediaSyncServerName, KNullDesC, serverUid));
       
    43   
       
    44     // Loading failed.
       
    45     if ( err != KErrNone )
       
    46         {
       
    47         return err;
       
    48         }
       
    49     
       
    50     TRequestStatus status;
       
    51     server.Rendezvous(status);
       
    52 
       
    53     if (status != KRequestPending)
       
    54         {
       
    55         server.Kill(0);     // abort startup
       
    56         server.Close();
       
    57         return KErrGeneral;
       
    58         }
       
    59     else
       
    60         {
       
    61         server.Resume();    // Logon OK - start the server.
       
    62         }
       
    63         
       
    64     User::WaitForRequest(status);
       
    65     server.Close();
       
    66     
       
    67     return status.Int();    
       
    68     }
       
    69 
       
    70 EXPORT_C void RMediaSyncServer::Shutdown()
       
    71     {
       
    72     SendReceive(EMediaSyncClientShutdown);
       
    73     }
       
    74 
       
    75 EXPORT_C TInt RMediaSyncServer::Connect()
       
    76     {      
       
    77     TFindProcess findMdEServer(KFinderMSSName);
       
    78     TFullName name;
       
    79     TInt result = findMdEServer.Next(name);
       
    80     if(result == KErrNotFound)
       
    81         {
       
    82         // Server is not running
       
    83         result = Startup();
       
    84         }
       
    85     else if(KErrNone == result)
       
    86         {
       
    87         RProcess mss;
       
    88         result = mss.Open(findMdEServer, EOwnerProcess);
       
    89         if((result != KErrNone) && (mss.ExitReason() != KErrNone))
       
    90             {
       
    91             result = Startup();           
       
    92             }
       
    93         mss.Close();
       
    94         }    
       
    95     
       
    96     if(KErrNone == result)
       
    97         {
       
    98         TVersion version(KMediaSyncServerVersionMajor, KMediaSyncServerVersionMinor, 0);
       
    99         result = CreateSession( KMediaSyncServerName, version );
       
   100         }
       
   101  
       
   102 	return result;
       
   103     }
       
   104 
       
   105 EXPORT_C void RMediaSyncServer::Close()
       
   106     {
       
   107     RSessionBase::Close();
       
   108     
       
   109     if (iHasSharedHeap)
       
   110         {
       
   111         iGlobalSharedHeap.Close();
       
   112         iHasSharedHeap = EFalse;
       
   113         }    
       
   114     }
       
   115 
       
   116 EXPORT_C TInt RMediaSyncServer::NeedFullSync(TBool& aNeedFullSync)
       
   117     {
       
   118     TPtr8 finishPtr((TUint8*)&aNeedFullSync, sizeof(TBool), sizeof(TBool));
       
   119     
       
   120     TIpcArgs args;    
       
   121     args.Set(0, &finishPtr);
       
   122     
       
   123     return SendReceive(EMediaSyncClientNeedFullSync, args);
       
   124     }
       
   125 
       
   126 EXPORT_C TInt RMediaSyncServer::ClearFullSync()
       
   127     {
       
   128     return SendReceive(EMediaSyncClientClearFullSync);    
       
   129     }
       
   130 
       
   131 EXPORT_C void RMediaSyncServer::GetChangesL(CMediaSyncDataReader*& aDataReader, TBool& aIsFinished, TRequestStatus& aStatus, TInt aMaxFetchCount)
       
   132     {
       
   133     if (!iHasSharedHeap)
       
   134         {
       
   135         User::LeaveIfError(GetGlobalSharedHeapHandle());
       
   136         iHasSharedHeap = ETrue;
       
   137         }
       
   138     
       
   139     TPtr8 finishPtr((TUint8*)&aIsFinished, sizeof(TBool), sizeof(TBool));
       
   140     
       
   141     TIpcArgs args;
       
   142     args.Set(0, aMaxFetchCount);       
       
   143     args.Set(1, &finishPtr);
       
   144     
       
   145     TInt ret = SendReceive(EMediaSyncClientGetChanges, args);    
       
   146     User::LeaveIfError(ret);
       
   147     
       
   148     aDataReader = CMediaSyncDataReader::NewL(iGlobalSharedHeap);
       
   149     
       
   150     TRequestStatus* pClient = &aStatus;
       
   151     User::RequestComplete(pClient, ret);   
       
   152     }
       
   153 
       
   154 EXPORT_C void RMediaSyncServer::RemoveAllRecords()
       
   155     {
       
   156     SendReceive(EMediaSyncClientRemoveAllRecords);
       
   157     RelaseGlobalSharedHeap();
       
   158     return;
       
   159     }
       
   160 
       
   161 EXPORT_C TInt RMediaSyncServer::EnableMonitor()
       
   162     {
       
   163     return SendReceive(EMediaSyncClientEnableMonitor);
       
   164     }
       
   165 
       
   166 EXPORT_C TInt RMediaSyncServer::DisableMonitor()
       
   167     {
       
   168     return SendReceive(EMediaSyncClientDisableMonitor);
       
   169     }
       
   170 
       
   171 TInt RMediaSyncServer::GetGlobalSharedHeapHandle()
       
   172     {
       
   173     TIpcArgs args;
       
   174     args.Set(0, KDefGlobalSharedHeapSize);    
       
   175     
       
   176     TInt ret = SendReceive(EMediaSyncClientGetGSHHandle, args);    
       
   177     ret = iGlobalSharedHeap.SetReturnedHandle(ret);
       
   178     
       
   179     return ret;    
       
   180     }
       
   181 
       
   182 void RMediaSyncServer::RelaseGlobalSharedHeap()
       
   183     {
       
   184     iGlobalSharedHeap.Close();
       
   185     iHasSharedHeap = EFalse;
       
   186     }
       
   187 
       
   188 CMediaSyncDataReader* CMediaSyncDataReader::NewL(const RChunk& aChunk)
       
   189     {
       
   190     CMediaSyncDataReader* self = new(ELeave) CMediaSyncDataReader();
       
   191     CleanupStack::PushL(self);    
       
   192     self->ConstructL(aChunk);  
       
   193     CleanupStack::Pop(self);
       
   194     return self;
       
   195     }
       
   196 
       
   197 EXPORT_C CMediaSyncDataReader::~CMediaSyncDataReader()
       
   198     {
       
   199     
       
   200     }
       
   201 
       
   202 CMediaSyncDataReader::CMediaSyncDataReader() :
       
   203     iOffset(0),
       
   204     iCurrentIdx(0)
       
   205     {
       
   206     
       
   207     }
       
   208 
       
   209 void CMediaSyncDataReader::ConstructL(const RChunk& aChunk)
       
   210     {
       
   211     TUint8* base = aChunk.Base();
       
   212     User::LeaveIfNull(base);
       
   213     
       
   214     iHeaderInfo = reinterpret_cast<TDataHeaderInfo*>(base);
       
   215     iReadBase   = base + sizeof(TDataHeaderInfo);
       
   216     }
       
   217 
       
   218 EXPORT_C TInt CMediaSyncDataReader::Count()
       
   219     {
       
   220     return iHeaderInfo->iCount;
       
   221     }
       
   222 
       
   223 EXPORT_C TBool CMediaSyncDataReader::HasNext()
       
   224     {
       
   225     return (iCurrentIdx < iHeaderInfo->iCount);
       
   226     }
       
   227 
       
   228 EXPORT_C void CMediaSyncDataReader::GetNextL(TUint32& aObjectId, TUint8& aType, TPtr16& aUri)
       
   229     {
       
   230     if (iCurrentIdx < iHeaderInfo->iCount)
       
   231         {
       
   232         //read object id
       
   233         Mem::Copy(&aObjectId, (iReadBase + iOffset), sizeof(TUint32));
       
   234         iOffset += sizeof(TUint32);
       
   235         
       
   236         //read notification type
       
   237         Mem::Copy(&aType, (iReadBase + iOffset), sizeof(TUint8));
       
   238         iOffset += sizeof(TUint8);
       
   239         
       
   240         //read uri length
       
   241         TUint8 uriLen = 0;
       
   242         Mem::Copy(&uriLen, (iReadBase + iOffset), sizeof(TUint8));
       
   243         iOffset += sizeof(TUint8);
       
   244         if (uriLen > 0)
       
   245             {
       
   246             //read uri content
       
   247             TUint16* ptr = (TUint16*)(iReadBase + iOffset);
       
   248             aUri.Set(ptr, uriLen, uriLen);
       
   249             iOffset += (uriLen * sizeof(TUint16));
       
   250             }
       
   251         else
       
   252             {
       
   253             aUri.Set(NULL, 0, 0);
       
   254             }
       
   255         
       
   256         ++iCurrentIdx;        
       
   257         }
       
   258     else
       
   259         {
       
   260         User::Leave(KErrOverflow);
       
   261         }
       
   262     }