applicationmanagement/server/src/AMDownloaddb.cpp
changeset 0 3ce708148e4d
child 62 03849bd79877
equal deleted inserted replaced
-1:000000000000 0:3ce708148e4d
       
     1 /*
       
     2 * Copyright (c) 2009 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:  Implementation of applicationmanagement components
       
    15  *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <sysutil.h>
       
    20 #include <bautils.h>
       
    21 
       
    22 #include "AMDownloaddb.h"
       
    23 #include "debug.h"
       
    24 
       
    25 // ============================ MEMBER FUNCTIONS =============================
       
    26 
       
    27 // ---------------------------------------------------------------------------
       
    28 //  CAMDownloaddb::NewL()
       
    29 //  Creates a new instance of CAMDownloaddb object.
       
    30 // ---------------------------------------------------------------------------
       
    31 // 
       
    32 CAMDownloaddb* CAMDownloaddb::NewL()
       
    33     {
       
    34     RDEBUG("CAMDownloaddb::NewL(): begin");
       
    35 
       
    36     CAMDownloaddb* self = CAMDownloaddb::NewLC();
       
    37     CleanupStack::Pop(self);
       
    38 
       
    39     RDEBUG("CAMDownloaddb::NewL(): end");
       
    40 
       
    41     return self;
       
    42     }
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 //  CAMDownloaddb::NewLC()
       
    46 //  Creates a new instance of CAMDownloaddb object. 
       
    47 //  Pushes and leaves new instance onto CleanupStack.
       
    48 // ---------------------------------------------------------------------------
       
    49 // 
       
    50 CAMDownloaddb* CAMDownloaddb::NewLC()
       
    51     {
       
    52     RDEBUG("CAMDownloaddb::NewLC(): begin");
       
    53 
       
    54     CAMDownloaddb* self = new( ELeave ) CAMDownloaddb();
       
    55     CleanupStack::PushL(self);
       
    56     self->ConstructL();
       
    57 
       
    58     RDEBUG("CAMDownloaddb::NewLC(): end");
       
    59 
       
    60     return self;
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 //  CAMDownloaddb::ConstructL()
       
    65 //  Second phase constructor.
       
    66 // ---------------------------------------------------------------------------
       
    67 // 
       
    68 void CAMDownloaddb::ConstructL()
       
    69     {
       
    70     RDEBUG("CAMDownloaddb::ConstructL(): begin");
       
    71 
       
    72     TParse name;
       
    73     TInt err;
       
    74 
       
    75     User::LeaveIfError(iFsSession.Connect());
       
    76     User::LeaveIfError(iRdbSession.Connect() );
       
    77 
       
    78     iFsSession.SetSessionToPrivate(EDriveC);
       
    79 
       
    80 #ifdef SYMBIAN_SECURE_DBMS
       
    81     name.Set(KAMDownloaddbName, NULL, NULL);
       
    82     err = iDatabase.Open(iRdbSession, name.FullName(), KDBMSSecureID);
       
    83 #else
       
    84     name.Set( KAMDownloaddbName, KNonSecureDbFullName, NULL );
       
    85     err = iDatabase.Open(iRdbSession, DBFileName);
       
    86 #endif
       
    87 
       
    88     if (err == KErrNotFound)
       
    89         {
       
    90         CreateandOpenL(name);
       
    91         }
       
    92     else
       
    93         {
       
    94 
       
    95         if ( (err == KErrEof ) || (err == KErrCorrupt ) || (err
       
    96                 == KErrArgument ))
       
    97             {
       
    98             // something seriously wrong with the db, delete it and try 
       
    99             // to create new
       
   100             iRdbSession.DeleteDatabase(name.FullName(), KAMPolicyUID);
       
   101             CreateandOpenL(name);
       
   102             }
       
   103 
       
   104         }
       
   105 
       
   106     RDEBUG("CAMDownloaddb::ConstructL(): end");
       
   107     }
       
   108 
       
   109 // -----------------------------------------------------------------------------
       
   110 // CWPInternetAPDB::CreateandOpenL
       
   111 // -----------------------------------------------------------------------------
       
   112 //    
       
   113 void CAMDownloaddb::CreateandOpenL(TParse& name)
       
   114     {
       
   115 
       
   116     TInt err;
       
   117 
       
   118 #ifdef SYMBIAN_SECURE_DBMS
       
   119     iDatabase.Create(iRdbSession, name.FullName(), KDBMSSecureID);
       
   120 #else
       
   121     if( SysUtil::FFSSpaceBelowCriticalLevelL( &iFsSession, KEmptyDbSizeEstimate ) )
       
   122         {
       
   123         User::Leave( KErrDiskFull );
       
   124         }
       
   125     iDatabase.Create(iFsSession, name.FullName());
       
   126 #endif	
       
   127 
       
   128     CreateTableL(iDatabase);
       
   129     iDatabase.Close();
       
   130 #ifdef SYMBIAN_SECURE_DBMS
       
   131 
       
   132     err = iDatabase.Open(iRdbSession, name.FullName(), KDBMSSecureID);
       
   133 
       
   134 #else
       
   135     err = iDatabase.Open(iFsSession, DBFileName);
       
   136 #endif
       
   137 
       
   138     //Debug
       
   139     if (err != KErrNone)
       
   140         {
       
   141 
       
   142         User::LeaveIfError(err);
       
   143         }
       
   144 
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CWPInternetAPDB::CreateTableL
       
   149 // -----------------------------------------------------------------------------
       
   150 //  
       
   151 void CAMDownloaddb::CreateTableL(RDbDatabase& aDatabase)
       
   152     {
       
   153 
       
   154     // Create a table definition
       
   155     CDbColSet* columns=CDbColSet::NewLC();
       
   156 
       
   157     // Add Columns
       
   158     TDbCol id(NCol1, EDbColInt32);
       
   159 
       
   160     // automatic indexing for items,it is our key field.
       
   161     id.iAttributes=id.EAutoIncrement;
       
   162     columns->AddL(id);
       
   163     columns->AddL(TDbCol(NCol2, EDbColInt32));
       
   164     columns->AddL(TDbCol(NCol3, EDbColInt32));
       
   165     columns->AddL(TDbCol(NCol4, EDbColText8, 255));
       
   166     columns->AddL(TDbCol(NCol5, EDbColInt32));
       
   167 
       
   168     // Create a table
       
   169     TRAPD(err, aDatabase.CreateTable(KTableAMMgmtObject, *columns) )
       
   170     ;
       
   171 
       
   172     if (err!=KErrNone)
       
   173         User::Leave(err);
       
   174 
       
   175     // cleanup the column set
       
   176     CleanupStack::PopAndDestroy(columns);
       
   177 
       
   178     }
       
   179 
       
   180 // ---------------------------------------------------------------------------
       
   181 //  CAMDownloaddb::CAMDownloaddb()
       
   182 //  Constructor.
       
   183 // ---------------------------------------------------------------------------
       
   184 // 
       
   185 CAMDownloaddb::CAMDownloaddb()
       
   186     {
       
   187     RDEBUG("CAMDownloaddb::CAMDownloaddb(): begin");
       
   188     RDEBUG("CAMDownloaddb::CAMDownloaddb(): end");
       
   189     }
       
   190 
       
   191 // ---------------------------------------------------------------------------
       
   192 //  CAMDownloaddb::~CAMDownloaddb()
       
   193 //  Destructor.
       
   194 // ---------------------------------------------------------------------------
       
   195 // 
       
   196 CAMDownloaddb::~CAMDownloaddb()
       
   197     {
       
   198     RDEBUG("CAMDownloaddb::~CAMDownloaddb(): begin");
       
   199 
       
   200     iView.Close();
       
   201 
       
   202     delete iColSet;
       
   203     iAMObjectTable.Close();
       
   204 
       
   205     iDatabase.Close();
       
   206     iFsSession.Close();
       
   207     iRdbSession.Close();
       
   208 
       
   209     RDEBUG("CAMDownloaddb::~CAMDownloaddb(): ends");
       
   210     }
       
   211 
       
   212 CAMDbItem::~CAMDbItem()
       
   213     {
       
   214     delete iTargetURI;
       
   215 
       
   216     }
       
   217 
       
   218 void CAMDownloaddb::GetEntryForLUIDL(RPointerArray<CAMDbItem>& aItemArray,
       
   219         TDesC& aValue)
       
   220     {
       
   221     RDEBUG( "CAMDownloaddb::GetEntryForLUIDL: Step1" );
       
   222     aItemArray.Reset();// first reset the array
       
   223 
       
   224     _LIT(KEqualToString,"=%d");
       
   225     _LIT(KQuery, "SELECT * FROM %S WHERE %S");
       
   226 
       
   227     RDEBUG( "CAMDownloaddb::GetEntryForLUIDL: Step2" );
       
   228 
       
   229     TBuf<100> bufsql;
       
   230     bufsql.Append(KQuery);
       
   231     bufsql.Append(KEqualToString);
       
   232     TLex lex(aValue);
       
   233 
       
   234     RDEBUG( "CAMDownloaddb::GetEntryForLUIDL: Step3" );
       
   235 
       
   236     TInt value;
       
   237     lex.Val(value);
       
   238 
       
   239     TBuf<100> bufsql1;
       
   240     bufsql1.Format(bufsql, &KTableAMMgmtObject, &NCol2, value);
       
   241 
       
   242     RDEBUG( "CAMDownloaddb::GetEntryForLUIDL: Step4" );
       
   243 
       
   244     ReadItemsL(aItemArray, bufsql1);
       
   245 
       
   246     RDEBUG( "CAMDownloaddb::GetEntryForLUIDL: Step5" );
       
   247 
       
   248     }
       
   249 
       
   250 // -----------------------------------------------------------------------------
       
   251 // CWPInternetAPDB::ReadDbItemsL
       
   252 // -----------------------------------------------------------------------------
       
   253 //   
       
   254 void CAMDownloaddb::ReadDbItemsL(RPointerArray<CAMDbItem>& aItemArray)
       
   255     {
       
   256 
       
   257     aItemArray.Reset();// first reset the array
       
   258 
       
   259     TFileName sqlQuery;
       
   260 
       
   261     // just get all columns & rows of same originator
       
   262     sqlQuery.Copy(_L("SELECT * FROM "));
       
   263     sqlQuery.Append(KTableAMMgmtObject);
       
   264 
       
   265     ReadItemsL(aItemArray, sqlQuery);
       
   266 
       
   267     }
       
   268 
       
   269 // -----------------------------------------------------------------------------
       
   270 // CWPInternetAPDB::ReadItemsL
       
   271 // -----------------------------------------------------------------------------
       
   272 // 
       
   273 void CAMDownloaddb::ReadItemsL(RPointerArray<CAMDbItem>& aItemArray,
       
   274         TDesC& asqlQuery)
       
   275     {
       
   276 
       
   277     RDEBUG( "CAMDownloaddb::ReadItemsL: Step1" );
       
   278 
       
   279     RDbView view;
       
   280     view.Prepare(iDatabase, TDbQuery(asqlQuery));
       
   281     CleanupClosePushL(view);
       
   282     view.EvaluateAll();
       
   283     view.FirstL();
       
   284 
       
   285     while (view.AtRow())
       
   286         {
       
   287         view.GetL();
       
   288 
       
   289         CAMDbItem *dbitem = new(ELeave) CAMDbItem;
       
   290         CleanupStack::PushL(dbitem);
       
   291         dbitem->id = view.ColInt(1);
       
   292         dbitem->iLUID = view.ColInt(2);
       
   293         dbitem->iResult = view.ColInt(3);
       
   294 
       
   295         dbitem->iTargetURI= (view.ColDes8(4)).AllocL();
       
   296 
       
   297         dbitem->iapid = view.ColInt(5);
       
   298         aItemArray.Append(dbitem);
       
   299         CleanupStack::Pop(dbitem);
       
   300         view.NextL();
       
   301         }
       
   302 
       
   303     CleanupStack::PopAndDestroy(); // view
       
   304 
       
   305     }
       
   306 
       
   307 // -----------------------------------------------------------------------------
       
   308 // CWPInternetAPDB::SaveToDatabaseL
       
   309 // -----------------------------------------------------------------------------
       
   310 //   
       
   311 void CAMDownloaddb::SaveToDatabaseL(TInt& aIndex, TUint32 aLuid,
       
   312         TInt aResult, const TDesC8& aTargetURI, TUint32 aIapid)
       
   313     {
       
   314 
       
   315     iDatabase.Begin();
       
   316 
       
   317     TFileName sqlQuery;
       
   318     sqlQuery.Copy(_L("SELECT * FROM "));
       
   319     sqlQuery.Append(KTableAMMgmtObject);
       
   320 
       
   321     RDbView view;
       
   322     view.Prepare(iDatabase, TDbQuery(sqlQuery));
       
   323     CleanupClosePushL(view);
       
   324 
       
   325     view.InsertL();
       
   326 
       
   327     view.SetColL(2, aLuid);
       
   328     view.SetColL(3, aResult);
       
   329     view.SetColL(4, aTargetURI);
       
   330     view.SetColL(5, aIapid);
       
   331 
       
   332     view.PutL();
       
   333 
       
   334     aIndex = view.ColInt(1);// autoincrement gives us unique index.
       
   335 
       
   336     CleanupStack::PopAndDestroy(1); // view
       
   337     iDatabase.Commit();
       
   338 
       
   339     }
       
   340 
       
   341 // -----------------------------------------------------------------------------
       
   342 // CWPInternetAPDB::DeleteFromDatabaseL
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 TBool CAMDownloaddb::DeleteFromDatabaseL(TUint32 aLUID)
       
   346     {
       
   347 
       
   348     TBool rowsdeleted = EFalse;
       
   349     TFileName sqlQuery;
       
   350     sqlQuery.Copy(_L("SELECT * FROM "));
       
   351     sqlQuery.Append(KTableAMMgmtObject);
       
   352     sqlQuery.Append(_L(" WHERE "));
       
   353     sqlQuery.Append(NCol2);
       
   354     sqlQuery.Append(_L(" = "));
       
   355     sqlQuery.AppendNum(aLUID);
       
   356 
       
   357     iDatabase.Begin();
       
   358 
       
   359     RDbView view;
       
   360     // query buffer with index finds only the selected item row.
       
   361     view.Prepare(iDatabase, TDbQuery(sqlQuery));
       
   362     CleanupClosePushL(view);
       
   363 
       
   364     view.EvaluateAll();
       
   365     view.FirstL();
       
   366 
       
   367     if (!view.IsEmptyL())
       
   368         {
       
   369         // we have autoincrement in index so it should be unique
       
   370         // but just to make sure, we use 'while', instead of 'if'
       
   371         while (view.AtRow())
       
   372             {
       
   373             view.GetL();
       
   374             view.DeleteL();
       
   375             view.NextL();
       
   376             }
       
   377 
       
   378         iDatabase.Commit();
       
   379         // compacts the databse, by physically removig deleted data.
       
   380         iDatabase.Compact();
       
   381         rowsdeleted = ETrue;
       
   382         }
       
   383     CleanupStack::PopAndDestroy(1); // view
       
   384 
       
   385 
       
   386     return rowsdeleted;
       
   387     }
       
   388