📄 entryappview.cpp
字号:
/*
============================================================================
Name : EntryAppView.cpp
Author : Sazzad and Mozaharul
Copyright : @Encryption 2008
Description : Application view implementation
============================================================================
*/
// INCLUDE FILES
#include <coemain.h>
#include <AknQueryDialog.h>
#include <e32base.h>
#include <e32std.h>
#include <bautils.H>
#include <Entry_0xEA1678C1.rsg>
#include <cryptopbe.h>
#include <CHARCONV.H>
#include "EntryAppView.h"
_LIT(KtxDatabaseName, "EntryItems.db");
_LIT(KEntryTable, "Entry"); /* Name of the one table */
_LIT(KEntryCol0, "index");
_LIT(KEntryCol1, "Name"); /* First column */
_LIT(SaveNote, "Message Save Successfully!");
// ============================ MEMBER FUNCTIONS ===============================
// -----------------------------------------------------------------------------
// CEntryAppView::NewL()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CEntryAppView* CEntryAppView::NewL(const TRect& aRect)
{
CEntryAppView* self = CEntryAppView::NewLC(aRect);
CleanupStack::Pop(self);
return self;
}
// -----------------------------------------------------------------------------
// CEntryAppView::NewLC()
// Two-phased constructor.
// -----------------------------------------------------------------------------
//
CEntryAppView* CEntryAppView::NewLC(const TRect& aRect)
{
CEntryAppView* self = new ( ELeave ) CEntryAppView;
CleanupStack::PushL(self);
self->ConstructL(aRect);
return self;
}
// -----------------------------------------------------------------------------
// CEntryAppView::CreateDb()
// To create or open the database
// -----------------------------------------------------------------------------
//
void CEntryAppView::CreateDb()
{
User::LeaveIfError(iFsSession.Connect());
TFileName DBFileName;
// private path with no drive on it
iFsSession.PrivatePath(DBFileName);
TFindFile PrivFolder(iFsSession);
// find out the drive
if(KErrNone == PrivFolder.FindByDir(DBFileName, KNullDesC))
{
DBFileName.Copy(PrivFolder.File());
DBFileName.Append(KtxDatabaseName);
if(BaflUtils::FileExists(iFsSession, DBFileName))
{
User::LeaveIfError(iItemsDatabase.Open(iFsSession, DBFileName));
//ReadDbItemsL();
}
else
{ // no database exists so we make one
User::LeaveIfError(iItemsDatabase.Create(iFsSession, DBFileName));
// and will create the onlt table needed for it
CreateTableL(iItemsDatabase);
}
}
}
// -----------------------------------------------------------------------------
// CEntryAppView::ConstructL()
// Symbian 2nd phase constructor can leave.
// -----------------------------------------------------------------------------
//
void CEntryAppView::ConstructL(const TRect& aRect)
{
// Create a window for this application view
CreateWindowL();
// Set the windows size
SetRect(aRect);
// Activate the window, which makes it ready to be drawn
ActivateL();
}
// -----------------------------------------------------------------------------
// CEntryAppView::CEntryAppView()
// C++ default constructor can NOT contain any code, that might leave.
// -----------------------------------------------------------------------------
//
CEntryAppView::CEntryAppView()
{
// No implementation required
}
// -----------------------------------------------------------------------------
// CEntryAppView::~CEntryAppView()
// Destructor.
// -----------------------------------------------------------------------------
//
CEntryAppView::~CEntryAppView()
{
// No implementation required
}
// -----------------------------------------------------------------------------
// CEntryAppView::CreateTableL()
// Create a database table
// -----------------------------------------------------------------------------
//
void CEntryAppView::CreateTableL(RDbDatabase& aDatabase)
{
// Create a table definition
CDbColSet* columns=CDbColSet::NewLC();
// Add Columns
TDbCol id(KEntryCol0,EDbColInt32);
// automatic indexing for items,it is our key field.
id.iAttributes=id.EAutoIncrement;
columns->AddL(id);
columns->AddL(TDbCol(KEntryCol1, EDbColText,160));
// Create a table
User::LeaveIfError(aDatabase.CreateTable(KEntryTable, *columns));
// cleanup the column set
CleanupStack::PopAndDestroy(columns);
}
// -----------------------------------------------------------------------------
// CEntryAppView::Draw()
// Draws the display.
// -----------------------------------------------------------------------------
//
void CEntryAppView::Draw(const TRect& /*aRect*/) const
{
// Get the standard graphics context
CWindowGc& gc = SystemGc();
// Gets the control's extent
TRect drawRect(Rect());
// Clears the screen
gc.Clear(drawRect);
}
// -----------------------------------------------------------------------------
// CEntryAppView::SaveToDatabaseL()
// save the encrypted data to the database
// -----------------------------------------------------------------------------
//
void CEntryAppView::SaveToDatabaseL(TInt& aIndex)
{
TFileName QueryBuffer;
TBuf<160> myText;
CreateDescInstance();
myText.Copy(EncryptMsg());
QueryBuffer.Copy(_L("SELECT * FROM "));
QueryBuffer.Append(KEntryTable);
iItemsDatabase.Begin();
RDbView Myview;
Myview.Prepare(iItemsDatabase, TDbQuery(QueryBuffer));
CleanupClosePushL(Myview);
Myview.InsertL();
Myview.SetColL(2, myText);
Myview.PutL();
aIndex = Myview.ColInt(1);// autoincrement gives us unique index.
CleanupStack::PopAndDestroy(1); // Myview
iItemsDatabase.Commit();
iEikonEnv->InfoMsg(SaveNote);
iItemsDatabase.Close();
iFsSession.Close();
}
// -----------------------------------------------------------------------------
// CEntryAppView::EncryptMsg()
// Perform the encryption operation on text
// -----------------------------------------------------------------------------
//
const TDes& CEntryAppView::EncryptMsg()
{
TBuf<160> mySms;
iBfrd->ProcessFinalL(indata,encdata);
mySms.Copy(encdata);
return mySms;
}
// -----------------------------------------------------------------------------
// CEntryAppView::AddView()
// Call the GetTextL() function to get the SMS text from the text dialog
// -----------------------------------------------------------------------------
//
void CEntryAppView::AddView()
{
if(GetTextL())
if(SMSText.Length()!=0)
{
CreateDb();
SaveToDatabaseL(iIndex);
}
}
// -----------------------------------------------------------------------------
// CEntryAppView::GetTextL()
// Create a Text dialog box to write text
// -----------------------------------------------------------------------------
//
TInt CEntryAppView::GetTextL()
{
CAknTextQueryDialog* dlg = new (ELeave) CAknTextQueryDialog(SMSText,CAknQueryDialog::ENoTone);
return dlg->ExecuteLD(R_SMS_TEXT_QUERY);
}
// -----------------------------------------------------------------------------
// CEntryAppView::DataView()
// To raed the data from database to call ConForReadDbItemsL()
// -----------------------------------------------------------------------------
//
void CEntryAppView::DataView()
{
ConForReadDbItemsL();
}
// -----------------------------------------------------------------------------
// CEntryAppView::DecryptMsg()
// Perform the decryption operation on text
// -----------------------------------------------------------------------------
//
const TDes& CEntryAppView::DecryptMsg(const TDes& KData)
{
TBuf8<160> encodedText,decdata;
TBuf<160> outputData;
encodedText.Copy(KData);
iDecbuf->ProcessFinalL(encodedText,decdata);
outputData.Copy(decdata);
// CEikonEnv::Static()->AlertWin(outputData);
encodedText.SetLength(0);
deskey.SetLength(0);
decdata.SetLength(0);
return outputData;
}
// -----------------------------------------------------------------------------
// CEntryAppView::CreateDescInstance()
// Create the instances for data encryption and decryption
// -----------------------------------------------------------------------------
//
void CEntryAppView::CreateDescInstance()
{
indata.SetLength(0);
encdata.SetLength(0);
_LIT8(KDESKey,"12345678");
deskey.Copy(KDESKey);
indata.Copy(SMSText);
iTrans = CDESEncryptor::NewL(deskey);
KDESBlockSize = iTrans->BlockSize();
iDec = CDESDecryptor::NewL(KDESKey);
pad = CPaddingPKCS7::NewL(KDESBlockSize);
pad2 = CPaddingPKCS7::NewL(KDESBlockSize);
iBfrd = CBufferedEncryptor ::NewLC(iTrans,pad);
iDecbuf = CBufferedDecryptor ::NewLC(iDec,pad2);
}
// -----------------------------------------------------------------------------
// CEntryAppView::DeleteDescInstance()
// Clean the stack that containing the encryption and decryption instances
// -----------------------------------------------------------------------------
//
void CEntryAppView::DeleteDescInstance()
{
CleanupStack::PopAndDestroy(2);
}
// -----------------------------------------------------------------------------
// CEntryAppView::ReadDataL()
// Read the data from database with decryption
// -----------------------------------------------------------------------------
//
void CEntryAppView::ReadDataL()
{
//RArray<TDbItem> aItemArray;
TBuf<160> temp;
CreateDescInstance();
//aItemArray.Reset();// first reset the array
TFileName QueryBuffer;
// just get all columns & rows
QueryBuffer.Copy(_L("SELECT * FROM "));
QueryBuffer.Append(KEntryTable);
RDbView Myview;
Myview.Prepare(iItemsDatabase,TDbQuery(QueryBuffer));
CleanupClosePushL(Myview);
Myview.EvaluateAll();
Myview.FirstL();
while(Myview.AtRow())
{
TDbItem NewItem;
Myview.GetL();
NewItem.iId = Myview.ColInt(1);
temp.Copy(Myview.ColDes(2));
NewItem.iSms.Copy(DecryptMsg(temp));
temp.SetLength(0);
//aItemArray.Append(NewItem);
Myview.NextL();
}
CleanupStack::PopAndDestroy(1); // Myview
iItemsDatabase.Close();
iFsSession.Close();
DeleteDescInstance();
//aItemArray.Reset();
}
// -----------------------------------------------------------------------------
// CEntryAppView::ConForReadDbItemsL()
// connection to the database to read data
// -----------------------------------------------------------------------------
//
void CEntryAppView::ConForReadDbItemsL()
{
User::LeaveIfError(iFsSession.Connect());
TFileName DBFileName;
// private path with no drive on it
iFsSession.PrivatePath(DBFileName);
TFindFile PrivFolder(iFsSession);
// find out the drive
if(KErrNone == PrivFolder.FindByDir(DBFileName, KNullDesC))
{
DBFileName.Copy(PrivFolder.File());
DBFileName.Append(KtxDatabaseName);
if(BaflUtils::FileExists(iFsSession, DBFileName))
{
User::LeaveIfError(iItemsDatabase.Open(iFsSession, DBFileName));
ReadDataL();
}
else
return;
}
}
// -----------------------------------------------------------------------------
// CEntryAppView::SizeChanged()
// Called by framework when the view size is changed.
// -----------------------------------------------------------------------------
//
void CEntryAppView::SizeChanged()
{
DrawNow();
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -