commsconfig/commsdatabaseshim/commdbshim/ProtectedDB/ProtectDB.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2006-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 // Table/Record Protection
       
    15 // 
       
    16 //
       
    17 
       
    18 /**
       
    19  @file
       
    20  @deprecated since v9.1. Functionality is replaced with commsdat.
       
    21 */
       
    22 
       
    23 #include "protectdb.h"
       
    24 #include "CDBSTD.H"
       
    25 #include "commdb_impl.H"
       
    26 #include "Commsdat_Log.h"
       
    27 /**
       
    28 @internalComponent
       
    29 */
       
    30 
       
    31 /** Creates a `CCommsDatabaseProtect`. If the database filestore does not exist
       
    32     the default filestore is duplicated. If no default filestore exists an
       
    33     empty database is created. */
       
    34 EXPORT_C CCommsDatabaseProtect* CCommsDatabaseProtect::NewL()
       
    35     {
       
    36     CCommsDatabaseProtect* r = new(ELeave) CCommsDatabaseProtect();
       
    37     CleanupStack::PushL(r);
       
    38     r->ConstructL();
       
    39     CleanupStack::Pop(r);
       
    40     return r;
       
    41     }
       
    42 
       
    43 /** Creates a `CCommsDatabaseProtect`. If the database filestore exists and
       
    44     `aUseDefaultDb` is True the default filestore is duplicated. Otherwise
       
    45     an empty database is created. */
       
    46 EXPORT_C CCommsDatabaseProtect* CCommsDatabaseProtect::NewL(TBool /*aUseDefaultDb*/)
       
    47     {
       
    48     CCommsDatabaseProtect* r = new(ELeave) CCommsDatabaseProtect();
       
    49     CleanupStack::PushL(r);
       
    50     r->ConstructL();
       
    51     CleanupStack::Pop(r);
       
    52     return r;
       
    53     }
       
    54 
       
    55 /** Creates a `CCommsDatabaseProtect` as with `NewL()`. The method of opening:
       
    56     (Created, CopiedDefault or Opened) is returned in `aOpeningMethod`. */
       
    57 EXPORT_C CCommsDatabaseProtect* CCommsDatabaseProtect::NewL(TCommDbOpeningMethod& /*aOpeningMethod*/)
       
    58     {
       
    59     CCommsDatabaseProtect* r = new(ELeave) CCommsDatabaseProtect();
       
    60     CleanupStack::PushL(r);
       
    61     r->ConstructL();
       
    62     CleanupStack::Pop(r);
       
    63     return r;
       
    64     }
       
    65 
       
    66 EXPORT_C CCommsDatabaseProtect::~CCommsDatabaseProtect()
       
    67     {
       
    68     delete iTableExt;
       
    69     }
       
    70 
       
    71 CCommsDatabaseProtect::CCommsDatabaseProtect()
       
    72     {
       
    73     }
       
    74 
       
    75 void CCommsDatabaseProtect::ConstructL()
       
    76     {
       
    77     iImpl->iDbs = CMDBSession::NewL(KCDVersion1_1);
       
    78     __FLOG_STATIC0(KLogComponent, KCommDbShim, _L("CommsDat Session is created!"));
       
    79     
       
    80     iTableExt = CCommDbTableExtension::NewL();
       
    81     }
       
    82 
       
    83 void CCommsDatabaseProtect::DoTableProtectionL(CMDBRecordSetBase* aRecordSet, TBool aProtect)
       
    84 	{
       
    85 	CMDBSession* dbSession = iImpl->iDbs;
       
    86     TBool showHidden = dbSession->IsSetAttributeMask(ECDHidden);
       
    87 
       
    88     // set all attribute masks as must be able to see every record (already set for ECDPrivate)
       
    89     dbSession->SetAttributeMask(ECDHidden | ECDNoWriteButDelete | ECDProtectedWrite );
       
    90 
       
    91     aProtect ? aRecordSet->SetAttributes(ECDProtectedWrite) : aRecordSet->ClearAttributes(ECDProtectedWrite);
       
    92     aRecordSet->ModifyL(*dbSession);
       
    93 
       
    94     if (showHidden)
       
    95         {
       
    96         dbSession->ClearAttributeMask(ECDNoWriteButDelete | ECDProtectedWrite);
       
    97         }
       
    98     else
       
    99         {
       
   100         dbSession->ClearAttributeMask(ECDHidden | ECDNoWriteButDelete | ECDProtectedWrite);
       
   101         }
       
   102 
       
   103 	}
       
   104 
       
   105 EXPORT_C TInt CCommsDatabaseProtect::ProtectTable(const TDesC& aTableName)
       
   106 /**Sets the access right field in the ACCESS_TYPE_TABLE for the table specified in aTableName to RDbRowSet::EReadOnly
       
   107 @param aTableName Contains the descriptor of a CommDb table name
       
   108 @return KErrNone or a standard error code
       
   109 @capability Dependent on table, see the guide page referenced below.
       
   110 */
       
   111     {
       
   112 	CMDBRecordSetBase* rs(NULL);
       
   113 	TRAPD(err, rs = iTableExt->RecordSetFactoryL(aTableName, EFalse, *iImpl->iDbs));
       
   114 
       
   115     if (err == KErrNone)
       
   116         {
       
   117         TRAP(err, DoTableProtectionL(rs, KProtectTable));
       
   118         }
       
   119 
       
   120 	delete rs;
       
   121 	
       
   122     return err;
       
   123     }
       
   124 
       
   125 EXPORT_C TInt CCommsDatabaseProtect::UnProtectTable(const TDesC& aTableName)
       
   126 /**Sets the access right field in the ACCESS_TYPE_TABLE for the table specified in aTableName to RDbRowSet::EUpdatable
       
   127 @param aTableName Contains the descriptor of a CommDb table name
       
   128 @return KErrNone or a standard error code
       
   129 @capability Dependent on table, see the guide page referenced below.
       
   130 */
       
   131     {
       
   132     CMDBRecordSetBase* rs(NULL);
       
   133 	TRAPD(err, rs = iTableExt->RecordSetFactoryL(aTableName, EFalse, *iImpl->iDbs));
       
   134 
       
   135     if (err == KErrNone)
       
   136         {
       
   137         TRAP(err, DoTableProtectionL(rs, KUnprotectTable));
       
   138         }
       
   139     
       
   140     delete rs;
       
   141 
       
   142     return err;
       
   143     }
       
   144 
       
   145 EXPORT_C RDbRowSet::TAccess CCommsDatabaseProtect::GetTableAccessL(const TDesC& aTableName)
       
   146 /** returns the access rights for aTableName from the ACCESS_TYPE_TABLE
       
   147 @param aTableName Contains the descriptor of a CommDb table name
       
   148 @return EUpdatable or EReadOnly
       
   149 @capability Dependent on table, see the guide page referenced below.
       
   150 */
       
   151     {
       
   152     iTableExt->SetRecordSetL(aTableName, *iImpl->iDbs, EFalse, ETrue);
       
   153     RDbRowSet::TAccess access;
       
   154 
       
   155     User::LeaveIfError(iTableExt->GetTableAccess(access));
       
   156 
       
   157     return access;
       
   158     }
       
   159 
       
   160 void CCommsDatabaseProtect::DoTablesProtectionL(CPtrCArray& aTableList, TBool aProtect)
       
   161 	{
       
   162 	CMDBRecordSetBase* rs(NULL);
       
   163 	
       
   164     TUint arraySize = aTableList.Count();
       
   165 
       
   166     for(TUint loopCount(0); loopCount<arraySize; ++loopCount)
       
   167         {
       
   168         __FLOG_STMT(TPtrC tableName = aTableList.MdcaPoint(loopCount);)
       
   169 
       
   170 	    rs = iTableExt->RecordSetFactoryL(aTableList.MdcaPoint(loopCount), EFalse, *iImpl->iDbs);
       
   171 		CleanupStack::PushL(rs);
       
   172 	    
       
   173 		DoTableProtectionL(rs, aProtect);
       
   174 		
       
   175 		CleanupStack::Pop(rs);
       
   176 
       
   177         __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("Protect Table: Name = %S"), &tableName);
       
   178         }
       
   179    	}
       
   180 	
       
   181 	
       
   182 EXPORT_C TInt CCommsDatabaseProtect::ProtectTables(CPtrCArray& aTableList)
       
   183 /**Sets the access right fields in the ACCESS_TYPE_TABLE for the tables specified
       
   184 in aTableList to RDbRowSet::EReadOnly
       
   185 @param aTableList array of CommDb table names
       
   186 @return KErrNone or a standard error code
       
   187 @capability Dependent on table, see the guide page referenced below.
       
   188 */
       
   189     {
       
   190     if(InTransaction())
       
   191         {
       
   192         return KErrLocked;
       
   193         }
       
   194 
       
   195     TInt err = BeginTransaction();
       
   196     __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("ProtectTables: BeginTransaction returned %d"), err);
       
   197     
       
   198     // On success execution recordsets and fileds representing tables from the list
       
   199     // will be on the cleanupstack   // VCT eh?
       
   200 	TRAP(err, DoTablesProtectionL(aTableList, ETrue));
       
   201 	
       
   202     if(err != KErrNone)
       
   203         {
       
   204         RollbackTransaction();
       
   205         __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("ProtectTables unsuccessful, err %d, RollbackTransaction()"), err);
       
   206         }
       
   207     else
       
   208     	{
       
   209     	err = CommitTransaction();
       
   210         __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("ProtectTables successful, CommitTransaction() returned %d"), err);
       
   211     	}
       
   212 	
       
   213 	return err;    	
       
   214     }
       
   215 
       
   216 EXPORT_C TInt CCommsDatabaseProtect::UnprotectTables(CPtrCArray& aTableList)
       
   217 /**Sets the access right fields in the ACCESS_TYPE_TABLE for the tables specified
       
   218 in aTableList to RDbRowSet::EUpdatable
       
   219 @param aTableList array of CommDb table names
       
   220 @return KErrNone or a standard error code
       
   221 @capability Dependent on table, see the guide page referenced below.
       
   222 */
       
   223     {
       
   224     if(InTransaction())
       
   225         {
       
   226         return KErrLocked;
       
   227         }
       
   228 
       
   229     TInt err = BeginTransaction();
       
   230     __FLOG_STATIC0(KLogComponent, KCommDbShim, _L("UnProtectTables: BeginTransaction"));
       
   231     
       
   232     // On success execution recordsets and fileds representing tables from the list
       
   233     // will be on the cleanupstack   // VCT ???
       
   234     
       
   235     TRAP(err, DoTablesProtectionL(aTableList, KUnprotectTable));
       
   236     
       
   237     if(err != KErrNone)
       
   238         {
       
   239         RollbackTransaction();
       
   240         __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("UnProtectTables unsuccessful, err %d, RollbackTransaction()"), err);
       
   241         }
       
   242     else
       
   243     	{
       
   244     	err = CommitTransaction();
       
   245         __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("UnProtectTables successful, CommitTransaction() returned %d"), err);
       
   246     	}
       
   247 
       
   248     return err;
       
   249     }
       
   250 
       
   251 CCommsDbProtectTableView* CCommsDbProtectTableView::NewL(CCommsDatabaseBase& /*aDb*/,const TDesC& /*aTableName*/,const TDbQuery& /*aQuery*/,TBool /*aUseTemplate*/)
       
   252 /**
       
   253 @deprecated This method is deprecated from 9.1 and always returns/leaves with KErrNotSupported instead of describing past operation/parameters.
       
   254 
       
   255 @param aDb reference to the database to use.
       
   256 @param aTableName A reference to a descriptor containing the name of a table in the communications database protect table view.
       
   257 @param aQuery Query to perform
       
   258 @param aUseTemplate A boolean value whether to use template while creating the object or not
       
   259 @return A pointer to a communications database protect table view object on the heap.
       
   260 */
       
   261     {
       
   262     User::Leave(KErrNotSupported);
       
   263     return NULL;
       
   264     }
       
   265 
       
   266 
       
   267 CCommsDbProtectTableView::CCommsDbProtectTableView(CCommsDatabaseBase& aDb,const TDesC& aTableName)
       
   268     : CCommsDbTableView(aDb,aTableName)
       
   269 /**
       
   270 Constructor
       
   271 
       
   272 @param aDb reference to the database to use.
       
   273 @param aTableName A reference to a descriptor containing the name of a table in the communications database protect table view.
       
   274 */
       
   275     {
       
   276     }
       
   277 
       
   278 void CCommsDbProtectTableView::ConstructL(const TDbQuery& /*aQuery*/,TBool /*aUseTemplate*/)
       
   279 /**
       
   280 @deprecated This method is deprecated from 9.1 and always returns/leaves with KErrNotSupported instead of describing past operation/parameters.
       
   281 
       
   282 Prepare and evaluate the table view using the SQL query aQuery
       
   283 */
       
   284     {
       
   285     User::Leave(KErrNotSupported);
       
   286     }
       
   287 
       
   288 EXPORT_C CCommsDbProtectTableView::~CCommsDbProtectTableView()
       
   289 /**
       
   290 Destructor.
       
   291 */
       
   292     {
       
   293     }
       
   294 
       
   295 void CCommsDbProtectTableView::DoProtectionL(CMDBSession* aSession, CMDBElement* aElement, TBool aProtect)
       
   296 	{
       
   297     TBool showHidden = aSession->IsSetAttributeMask(ECDHidden);
       
   298 
       
   299     // set all attribute masks as must be able to see every record (already set for ECDPrivate)
       
   300     aSession->SetAttributeMask(ECDHidden | ECDNoWriteButDelete | ECDProtectedWrite);
       
   301 
       
   302     aProtect ? aElement->SetAttributes(ECDProtectedWrite) : aElement->ClearAttributes(ECDProtectedWrite);
       
   303     aElement->ModifyL(*aSession);
       
   304 
       
   305     if (showHidden)
       
   306         {
       
   307         aSession->ClearAttributeMask(ECDNoWriteButDelete | ECDProtectedWrite);
       
   308         }
       
   309     else
       
   310         {
       
   311         aSession->ClearAttributeMask(ECDHidden | ECDNoWriteButDelete | ECDProtectedWrite);
       
   312         }
       
   313 	}
       
   314 
       
   315 EXPORT_C TInt CCommsDbProtectTableView::ProtectTable()
       
   316 /**
       
   317 Protects the  table
       
   318 Protection of settings
       
   319 @return KErrNone if successful, otherwise another of the system-wide error codes.
       
   320 @capability Dependent on table, see the guide page referenced below.
       
   321 */
       
   322     {
       
   323 	CMDBRecordSetBase* rs(NULL);
       
   324 	TInt err = iTableExt->GetRecordSet(rs);
       
   325 	
       
   326 	if(err == KErrNone)
       
   327 		{
       
   328 	    TRAP(err, DoProtectionL(iDb.iImpl->iDbs, rs, KProtectTable));
       
   329 		}
       
   330 
       
   331     __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("CCommsDbProtectTableView::ProtectTable, Err:%d"), err);
       
   332     return err;
       
   333     }
       
   334 
       
   335 EXPORT_C TInt CCommsDbProtectTableView::UnprotectTable()
       
   336 /**
       
   337 Unprotects the  table
       
   338 
       
   339 @return KErrNone if successful, otherwise another of the system-wide error codes.
       
   340 @capability Dependent on table, see the guide page referenced below.
       
   341 */
       
   342     {
       
   343 	CMDBRecordSetBase* rs(NULL);
       
   344 	TInt err = iTableExt->GetRecordSet(rs);
       
   345 	
       
   346 	if(err == KErrNone)
       
   347 		{
       
   348 	    TRAP(err, DoProtectionL(iDb.iImpl->iDbs, rs, KUnprotectTable));
       
   349 		}
       
   350 
       
   351     __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("CCommsDbProtectTableView::UnprotectTable, Err:%d"), err);
       
   352     return err;
       
   353     }
       
   354 
       
   355 EXPORT_C TInt CCommsDbProtectTableView::ProtectRecord()
       
   356 /**
       
   357 Marks record as read-only so that it cannot be modified or deleted.
       
   358 
       
   359 @return KErrNone if successful, otherwise another of the system-wide error codes.
       
   360 @capability Dependent on table, see the guide page referenced below.
       
   361 */
       
   362     {
       
   363 	CMDBRecordBase* record(NULL);
       
   364 	TInt err = iTableExt->GetCurrentRecord(record);
       
   365 	
       
   366 	if(err == KErrNone)
       
   367 		{
       
   368 	    TRAP(err, DoProtectionL(iDb.iImpl->iDbs, record, KProtectRecord));
       
   369 		}
       
   370 
       
   371     __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("CCommsDbProtectTableView::ProtectRecord, Err:%d"), err);
       
   372     return err;
       
   373     }
       
   374 
       
   375 
       
   376 EXPORT_C TInt CCommsDbProtectTableView::UnprotectRecord()
       
   377 /**
       
   378 Marks record as not read-only so that it can be modified or deleted
       
   379 
       
   380 @return KErrNone if successful, otherwise another of the system-wide error codes.
       
   381 @capability Dependent on table, see the guide page referenced below.
       
   382 */
       
   383     {
       
   384 	CMDBRecordBase* record(NULL);
       
   385 	TInt err = iTableExt->GetCurrentRecord(record);
       
   386 	
       
   387 	if(err == KErrNone)
       
   388 		{
       
   389 	    TRAP(err, DoProtectionL(iDb.iImpl->iDbs, record, KUnprotectRecord));
       
   390 		}
       
   391 
       
   392     __FLOG_STATIC1(KLogComponent, KCommDbShim, _L("CCommsDbProtectTableView::UnprotectRecord, Err:%d"), err);
       
   393     return err;
       
   394     }
       
   395 
       
   396 EXPORT_C TInt CCommsDbProtectTableView::GetTableAccess(RDbRowSet::TAccess& aAccessType)
       
   397 /**
       
   398 Checks table's ReadOnly Setting
       
   399 
       
   400 @return KErrNone if successful, otherwise another of the system-wide error codes.
       
   401 @capability Dependent on table, see the guide page referenced below.
       
   402 */
       
   403     {
       
   404     return iTableExt->GetTableAccess(aAccessType);
       
   405     }
       
   406 
       
   407 
       
   408 EXPORT_C TInt CCommsDbProtectTableView::GetRecordAccess(TInt& aAccess)
       
   409 /**
       
   410 Check record's protection setting
       
   411 Currently a record can be Writable (0) or Read Only (1).
       
   412 Read Only records cannot be amended or deleted until they are unprotected via this API.
       
   413 
       
   414 @param aAccess On return contains the value of the column ordinal
       
   415 @return KErrNone if successful, otherwise another of the system-wide error codes.
       
   416 @capability Dependent on table, see the guide page referenced below.
       
   417 */
       
   418     {
       
   419     CMDBRecordBase* record(NULL);
       
   420     TInt err = iTableExt->GetCurrentRecord(record);
       
   421     if(err == KErrNone)
       
   422         {
       
   423         if (record->IsSetAttribute(ECDProtectedWrite))
       
   424             {
       
   425             aAccess = RDbRowSet::EReadOnly;
       
   426             }
       
   427         else
       
   428             {
       
   429             aAccess = RDbRowSet::EUpdatable;
       
   430             }
       
   431         }
       
   432     return err;
       
   433     }
       
   434 
       
   435 // EOF