syncmlfw/ds/adapterlog/src/NSmlAdapterLog.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2005 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:  AdapterLog source
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <s32file.h>
       
    22 #include <s32stor.h>
       
    23 #include <bautils.h>
       
    24 
       
    25 #include <nsmlconstants.h>
       
    26 #include<nsmlconstantdefs.h>
       
    27 #include <nsmldsconstants.h>
       
    28 #include "NSmlAdapterLog.h"
       
    29 
       
    30 
       
    31 // STATIC CONSTANTS
       
    32 const TInt CNSmlAdapterLog::KMaxPathLength;
       
    33 
       
    34 
       
    35 // ============================ MEMBER FUNCTIONS ===============================
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CNSmlAdapterLog::CNSmlAdapterLog
       
    39 // C++ default constructor.
       
    40 // -----------------------------------------------------------------------------
       
    41 //
       
    42 CNSmlAdapterLog::CNSmlAdapterLog()
       
    43     {
       
    44     }
       
    45 
       
    46 // -----------------------------------------------------------------------------
       
    47 // CNSmlAdapterLog::ConstructL
       
    48 // Symbian 2nd phase constructor.
       
    49 // -----------------------------------------------------------------------------
       
    50 //
       
    51 void CNSmlAdapterLog::ConstructL()
       
    52     {
       
    53     User::LeaveIfError(iFsSession.Connect());
       
    54     User::LeaveIfError(iRdbSession.Connect());
       
    55 
       
    56     iPath = HBufC::NewL(CNSmlAdapterLog::KMaxPathLength);
       
    57     TPtr path = iPath->Des();
       
    58 
       
    59     User::LeaveIfError(iFsSession.SetSessionToPrivate(KNSmlAdapterLogDrive));
       
    60     User::LeaveIfError(iFsSession.SessionPath(path));
       
    61 
       
    62     BaflUtils::EnsurePathExistsL(iFsSession, *iPath);
       
    63     delete iPath;
       
    64     iPath = NULL;
       
    65     
       
    66     TParse name;
       
    67     
       
    68 #ifdef SYMBIAN_SECURE_DBMS
       
    69 	name.Set(KNSmlAdapterLogDbName(), NULL, NULL);
       
    70 #else
       
    71     name.Set(KNSmlAdapterLogDbName(), KNSmlDatabasesNonSecurePath, NULL);
       
    72 #endif
       
    73     TInt err(KErrNone);
       
    74     RProcess currentProcess;
       
    75     const TSecureId secureId = currentProcess.SecureId();
       
    76     currentProcess.Close();
       
    77     if ( secureId.iId == KNSmlDSHostRWServerSecureID.iId )
       
    78         {
       
    79         err = iDatabase.Open(iRdbSession,
       
    80                              name.FullName(), KNSmlDBMSSecureDSHostRWID);
       
    81         if ( err == KErrNotFound )
       
    82             {
       
    83             CreateDatabaseL(name.FullName());
       
    84             err = iDatabase.Open(iRdbSession,
       
    85                                name.FullName(), KNSmlDBMSSecureDSHostRWID);
       
    86             }
       
    87         }
       
    88     else if (secureId.iId == KNSmlDSHostNULLServerSecureID.iId)
       
    89         {
       
    90         err = iDatabase.Open(iRdbSession,
       
    91                              name.FullName(), KNSmlDBMSSecureDSHostNULLID);
       
    92         if ( err == KErrNotFound )
       
    93             {
       
    94             CreateDatabaseL(name.FullName());
       
    95             err = iDatabase.Open(iRdbSession, name.FullName(), 
       
    96                                  KNSmlDBMSSecureDSHostNULLID);
       
    97             
       
    98             }
       
    99         }
       
   100     else
       
   101         {
       
   102         User::Leave( KErrAccessDenied );
       
   103         }
       
   104 
       
   105     User::LeaveIfError(err);            
       
   106     User::LeaveIfError(iTable.Open(iDatabase, KNSmlTableAdapterLog));
       
   107     iColSet = iTable.ColSetL();
       
   108     }
       
   109 
       
   110 // -----------------------------------------------------------------------------
       
   111 // CNSmlAdapterLog::NewL
       
   112 // Two-phased constructor.
       
   113 // -----------------------------------------------------------------------------
       
   114 //
       
   115 EXPORT_C CNSmlAdapterLog* CNSmlAdapterLog::NewL()
       
   116     {
       
   117     CNSmlAdapterLog* self = CNSmlAdapterLog::NewLC();
       
   118     CleanupStack::Pop(self);
       
   119 
       
   120     return self;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CNSmlAdapterLog::NewLC
       
   125 // Two-phased constructor.
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 EXPORT_C CNSmlAdapterLog* CNSmlAdapterLog::NewLC()
       
   129     {
       
   130     CNSmlAdapterLog* self = new (ELeave) CNSmlAdapterLog;
       
   131     CleanupStack::PushL(self);
       
   132     self->ConstructL();
       
   133  
       
   134     return self;
       
   135     }
       
   136     
       
   137 // -----------------------------------------------------------------------------
       
   138 // CNSmlAdapterLog::~CNSmlAdapterLog()
       
   139 // Destructor
       
   140 // -----------------------------------------------------------------------------
       
   141 CNSmlAdapterLog::~CNSmlAdapterLog()
       
   142     {
       
   143     delete iPath;
       
   144     
       
   145     if (iDictionaryStore)
       
   146         {
       
   147         iDictionaryStore->Commit();
       
   148         delete iDictionaryStore;
       
   149         }
       
   150     
       
   151    	iView.Close();
       
   152     delete iColSet;
       
   153     
       
   154     iTable.Close();    
       
   155     iDatabase.Close();
       
   156     iFsSession.Close();
       
   157     iRdbSession.Close();
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CNSmlAdapterLog::SetAdapterLogKeyL
       
   162 // Sets the sync relationship key.
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 EXPORT_C void CNSmlAdapterLog::SetAdapterLogKeyL(
       
   166              const TInt aImplUid, const TDesC& aLocalDB, 
       
   167              const TDesC& aServerId, const TDesC& aRemoteDB)
       
   168     {
       
   169     HBufC* sql = HBufC::NewLC(KSqlGetAdapterLogRow().Length() + 5 + 
       
   170         aLocalDB.Length() + aServerId.Length() + aRemoteDB.Length());
       
   171         
       
   172     TPtr tmp = sql->Des();
       
   173     tmp.Format(KSqlGetAdapterLogRow, aImplUid, &aLocalDB, &aServerId,
       
   174         &aRemoteDB);
       
   175     
       
   176     iView.Close();
       
   177     User::LeaveIfError(iView.Prepare(iDatabase, TDbQuery(*sql),
       
   178                                      RDbView::/*TAccess::*/EUpdatable));
       
   179     
       
   180     if (iView.FirstL())
       
   181         {
       
   182         // existing row
       
   183         iView.GetL();
       
   184         iID = iView.ColInt(iColSet->ColNo(KNSmlAdapterLogId));
       
   185         }
       
   186     else
       
   187         {
       
   188         // new row
       
   189         iID = GenerateIdL();
       
   190         iDatabase.Begin();
       
   191         iView.InsertL();
       
   192         iView.SetColL(iColSet->ColNo(KNSmlAdapterLogId), iID);
       
   193         iView.SetColL(iColSet->ColNo(KNSmlAdapterLogImplementationUID), aImplUid);
       
   194         iView.SetColL(iColSet->ColNo(KNSmlAdapterLogLocalDatabase), aLocalDB);
       
   195         iView.SetColL(iColSet->ColNo(KNSmlAdapterLogServerId), aServerId);
       
   196         iView.SetColL(iColSet->ColNo(KNSmlAdapterLogRemoteDatabase), aRemoteDB);   
       
   197         iView.PutL();
       
   198         iDatabase.Commit();
       
   199         iDatabase.Compact();
       
   200         }
       
   201                 
       
   202     CleanupStack::PopAndDestroy(sql);
       
   203 
       
   204     OpenDictionaryStoreL();
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CNSmlAdapterLog::SetServerIdL
       
   209 // Sets server id.
       
   210 // -----------------------------------------------------------------------------
       
   211 //    
       
   212 EXPORT_C void CNSmlAdapterLog::SetServerIdL( TDesC& aOldServerId, TDesC& aNewValue )
       
   213 	{
       
   214 	HBufC* sql = HBufC::NewLC( KSqlGetAdapterLogRowFromServerId().Length() + aOldServerId.Length() );
       
   215 	
       
   216 	TPtr tmp = sql->Des();
       
   217     tmp.Format( KSqlGetAdapterLogRowFromServerId,  &aOldServerId );
       
   218 	
       
   219 	iView.Close();
       
   220 	User::LeaveIfError( iView.Prepare( iDatabase, TDbQuery(*sql),
       
   221                                      RDbView::EUpdatable ) );
       
   222 	iDatabase.Begin();
       
   223 	if ( iView.FirstL() )
       
   224 		{
       
   225 		//existing row
       
   226 		iView.GetL();
       
   227 		iView.UpdateL();
       
   228 		iView.SetColL( iColSet->ColNo( KNSmlAdapterLogServerId ), aNewValue );
       
   229 		iView.PutL();
       
   230 		
       
   231 		iDatabase.Commit();
       
   232 		iDatabase.Compact();
       
   233 		}
       
   234 
       
   235 	CleanupStack::PopAndDestroy( sql );
       
   236 	}
       
   237 
       
   238 // -----------------------------------------------------------------------------
       
   239 // CNSmlAdapterLog::SyncTaskKey
       
   240 // Returns the unique identifier for the synchronization relationship. The
       
   241 // default value for the identifier (zero) is returned, if the adapter log key
       
   242 // has not been set.
       
   243 // -----------------------------------------------------------------------------
       
   244 //
       
   245 EXPORT_C TSmlSyncTaskKey CNSmlAdapterLog::SyncTaskKey() const
       
   246     {
       
   247     return static_cast<TSmlSyncTaskKey>(iID);    
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CNSmlAdapterLog::OpenReadStreamLC
       
   252 // Opens the dictionary stream associated with the given UID, leaving the 
       
   253 // cleanup item on cleanup stack. The adapter log key has to be set before this
       
   254 // method can be used. Method leaves, if association with a stream and the
       
   255 // given uid does not exist in the store.
       
   256 // -----------------------------------------------------------------------------
       
   257 //
       
   258 EXPORT_C void CNSmlAdapterLog::OpenReadStreamLC(RReadStream& aReadStream,
       
   259                                                 TUid aStreamUid)
       
   260     {
       
   261     if (!IsStreamPresentL(aStreamUid))
       
   262         {
       
   263         User::Leave(KErrNotFound);
       
   264         }
       
   265 
       
   266     static_cast<RDictionaryReadStream&>(aReadStream).OpenLC(
       
   267         *iDictionaryStore, aStreamUid);
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CNSmlAdapterLog::OpenWriteStreamLC
       
   272 // Opens the dictionary stream associated with the given UID, leaving the
       
   273 // cleanup item on cleanup stack. If no stream is associated with the UID, new
       
   274 // stream is created with association to given UID. The adapter log key has to
       
   275 // be set before this method can be used.
       
   276 // -----------------------------------------------------------------------------
       
   277 //
       
   278 EXPORT_C void CNSmlAdapterLog::OpenWriteStreamLC(RWriteStream& aWriteStream,
       
   279                                                  TUid aStreamUid)
       
   280     {
       
   281     if (iID == NULL)
       
   282         {
       
   283         User::Leave(KErrNotFound);
       
   284         }
       
   285         
       
   286     static_cast<RDictionaryWriteStream&>(aWriteStream).AssignLC(
       
   287         *iDictionaryStore, aStreamUid);
       
   288     }
       
   289 
       
   290 // -----------------------------------------------------------------------------
       
   291 // CNSmlAdapterLog::IsStreamPresentL
       
   292 // Checks if a stream associated with given UID is present in the opened
       
   293 // dictionary store. The adapter log key has to be set before this
       
   294 // method can be used.
       
   295 // -----------------------------------------------------------------------------
       
   296 //
       
   297 EXPORT_C TBool CNSmlAdapterLog::IsStreamPresentL(TUid aStreamUid) const
       
   298     {
       
   299     if (iID == NULL)
       
   300         {
       
   301         User::Leave(KErrNotFound);
       
   302         }
       
   303         
       
   304     return iDictionaryStore->IsPresentL(aStreamUid);
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CNSmlAdapterLog::CreateDatabaseL
       
   309 // Creates new database with AdapterLog table in the specified file.
       
   310 // -----------------------------------------------------------------------------
       
   311 //
       
   312 void CNSmlAdapterLog::CreateDatabaseL(const TDesC& aFullName)
       
   313     {
       
   314 	_LIT(KCreateAdapterLogTable, "CREATE TABLE AdapterLog (Id \
       
   315 	INTEGER NOT NULL, ImplementationUID INTEGER NOT NULL, LocalDatabase CHAR(%d) \
       
   316 	NOT NULL, ServerId CHAR(%d), RemoteDatabase CHAR(%d) NOT NULL)");
       
   317     
       
   318     HBufC* createAdapterLogTable = 
       
   319         HBufC::NewLC(KCreateAdapterLogTable().Length() + 20);
       
   320         
       
   321     TPtr AdapterLogTablePtr = createAdapterLogTable->Des();
       
   322     
       
   323     AdapterLogTablePtr.Format(KCreateAdapterLogTable, KNSmlMaxDataLength,
       
   324                               KNSmlMaxServerIdLength, KNSmlMaxRemoteNameLength);
       
   325     
       
   326     RProcess currentProcess;
       
   327     const TSecureId secureId = currentProcess.SecureId();
       
   328     currentProcess.Close();
       
   329     if ( secureId.iId == KNSmlDSHostRWServerSecureID.iId )
       
   330         {
       
   331         User::LeaveIfError(iDatabase.Create(iRdbSession,
       
   332                                             aFullName,
       
   333                                             KNSmlDBMSSecureDSHostRWID));
       
   334         }
       
   335     else if (secureId.iId == KNSmlDSHostNULLServerSecureID.iId)
       
   336         {
       
   337         User::LeaveIfError(iDatabase.Create(iRdbSession,
       
   338                                             aFullName,
       
   339                                             KNSmlDBMSSecureDSHostNULLID));
       
   340         }
       
   341     else
       
   342         {
       
   343         User::Leave( KErrAccessDenied );
       
   344         }
       
   345     iDatabase.Begin();
       
   346     iDatabase.Execute(*createAdapterLogTable);                                          
       
   347     iDatabase.Commit();
       
   348 	iDatabase.Compact();
       
   349 	iDatabase.Close();
       
   350 	
       
   351 	CleanupStack::PopAndDestroy(createAdapterLogTable);
       
   352     }
       
   353     
       
   354 // -----------------------------------------------------------------------------
       
   355 // CNSmlAdapterLog::GenerateIdL
       
   356 // Generates a unique identifier for the table by searching the adapter log ID
       
   357 // with highest value and increments it.
       
   358 // -----------------------------------------------------------------------------
       
   359 //
       
   360 TInt CNSmlAdapterLog::GenerateIdL()
       
   361     {
       
   362     TInt highestKey(0);
       
   363     if (iTable.LastL())
       
   364         {
       
   365         TInt currentKey(0);
       
   366         do
       
   367             {
       
   368             iTable.GetL();
       
   369             currentKey = iTable.ColInt(iColSet->ColNo(KNSmlAdapterLogId));
       
   370             if (highestKey < currentKey)
       
   371                 {
       
   372                 highestKey = currentKey;
       
   373                 }
       
   374             }
       
   375         while (iTable.PreviousL());
       
   376         }
       
   377     return highestKey + 1;    
       
   378     }
       
   379         
       
   380 // -----------------------------------------------------------------------------
       
   381 // CNSmlAdapterLog::OpenDictionaryStoreL
       
   382 // Opens the dictionary file store associated with the adapter log ID.
       
   383 // If dictionary store has already been opened, method establishes new commit
       
   384 // point for opened store and closes the store before opening another.
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 void CNSmlAdapterLog::OpenDictionaryStoreL()
       
   388     {
       
   389     if (iDictionaryStore)
       
   390         {
       
   391         iDictionaryStore->CommitL();
       
   392         delete iDictionaryStore;
       
   393         iDictionaryStore = NULL;
       
   394         }
       
   395           
       
   396     HBufC* storeName = HBufC::NewLC(KNSmlAdapterLogStore().Length() + 10);
       
   397     TPtr tmp = storeName->Des();
       
   398     tmp.Format(KNSmlAdapterLogStore(), iID);
       
   399 
       
   400     iPath = HBufC::NewL(CNSmlAdapterLog::KMaxPathLength);
       
   401     TPtr path = iPath->Des();
       
   402  
       
   403     TParse file;
       
   404     User::LeaveIfError(file.Set(*storeName, iPath, NULL));
       
   405     
       
   406     iDictionaryStore = CDictionaryFileStore::OpenL(iFsSession, file.FullName(),
       
   407                                                    TUid::Uid(iID));
       
   408     
       
   409     CleanupStack::PopAndDestroy(storeName);
       
   410     }
       
   411     
       
   412 
       
   413 //  End of File