📄 bookstoreappui.cpp
字号:
break;
case EQuickFindCmd: // Find a book using index
IndexFindL();
break;
case ECloseCmd: // Close database
CloseDatabaseL();
break;
case EAknSoftkeyExit: // From CBA
case EEikCmdExit: // From operating system / framework
Exit();
break;
default:
break;
}
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::OpenDatabaseL()
//
// Create instance of iBookstoreDb and open existing database.
// ---------------------------------------------------------------------------
void CBookstoreAppUi::OpenDatabaseL()
{
iBookstoreDb = CBookstoreDb::NewL();
iBookstoreDb->OpenDbL(iDatabaseFile);
ShowAllBooksL(); // Change to list view
}
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// CBookstoreAppUi::CloseDatabaseL()
//
// Close an open database. Database opened with OpenDatabaseL or
// CreateDatabaseL must be closed, when not used any more.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::CloseDatabaseL()
{
if(iBookstoreDb && iBookstoreDb->IsOpen())
{
iBookstoreDb->Close();
delete iBookstoreDb;
iBookstoreDb = NULL;
}
ChangeView(EMainView);
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::ShowBookEditorView()
//
// Activate book editor view
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::ShowBookEditorView()
{
ChangeView(EBookEditorView);
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::UpdateBookTitleL()
//
// Change the title of a selected book.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::UpdateBookTitleL()
{
_LIT(KQuery,"New title(s) for: ");
TBuf<KBookItemMaxLength> selectedBookTitle;
if(iListboxView->GetSelectedItemL(selectedBookTitle) != KErrNone)
{
_LIT(KErrorMsg,"Failed. Book not selected.");
ShowNoteL(KErrorMsg);
return;
}
TBuf<KBookItemMaxLength+32> prompt;
TBuf<KBookItemMaxLength> newTitle;
prompt.Append(KQuery);
prompt.Append(selectedBookTitle);
if(QueryTextL(prompt, newTitle))
{
newTitle.Trim(); // Remove specific characters from the beginning
// and the end of the string
iBookstoreDb->UpdateBookTitle(selectedBookTitle, newTitle);
ShowAllBooksL();
}
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::ShowAllBooksL()
//
// Get list of all books in the database. Show the titles in the listbox
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::ShowAllBooksL()
{
_LIT(KAllBooksTitle,"Books in bookstore:");
// Get array of full book infos from the database. Construct array of
// titles and show them in the listbox view.
CDesCArrayFlat* books = iBookstoreDb->GetAllBooksL();
CleanupStack::PushL(books);
CDesCArrayFlat* titles = TitlesArrayL(books);
CleanupStack::PushL(titles);
ChangeView(EBookListView); // construct the listbox view
iListboxView->SetCaptionL(KAllBooksTitle);
CleanupStack::Pop(titles);
iListboxView->SetListItems(titles); // Takes ownership
CleanupStack::PopAndDestroy(books);
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::SearchBooksL()
//
// Query book search string from the user and perform book search. Show the
// results in the list.
//
// Implementation finds books according to a KBooksTitleCol column name
// and the queried search pattern.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::SearchBooksL()
{
_LIT(KSearchResult, "Search result:");
TBuf<KTitleMaxLength> titlePattern;
TBuf<32> prompt(_L("Book search"));
QueryTextL(prompt, titlePattern);
titlePattern.Append(_L("*"));
ChangeView(EBookListView);
iListboxView->SetCaptionL(KSearchResult);
// Get array of matching books. Construct array of titles from the
// books array and show it in the listbox view.
CDesCArrayFlat* books =
iBookstoreDb->GetBooksByKeyL(KBooksTitleCol, titlePattern);
CleanupStack::PushL(books);
CDesCArrayFlat* titles = TitlesArrayL(books);
iListboxView->SetListItems(titles); // Takes ownership
CleanupStack::PopAndDestroy(books);
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::IndexFindL()
//
// Find a book using index. Show full info for the book.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::IndexFindL()
{
_LIT(KDetailsCaption,"Book details:");
// Query the title of the selected book in the listbox
TBuf<KBookItemMaxLength> selectedBook;
if(iListboxView->GetSelectedItemL(selectedBook) != KErrNone)
{
_LIT(KErrorMsg,"Failed. Book not selected.");
ShowNoteL(KErrorMsg);
return;
}
// Query book details from the database using book title
TBuf<KBookItemMaxLength> result;
TInt err = iBookstoreDb->GetABookFastL(selectedBook, result);
if(err==KErrNotFound)
{
_LIT(KNotFoundMsg,"Book not found.");
ShowNoteL(KNotFoundMsg);
return;
}
iEikonEnv->InfoWinL(KDetailsCaption, result);
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::ApplicationDriveAndPath()
//
// Get the application path and drive. It must be done differently in the
// development environment and in the device.
// ---------------------------------------------------------------------------
//
TFileName CBookstoreAppUi::ApplicationDriveAndPath() const
{
TFileName appfullname(Application()->AppFullName());
TParse parse;
#ifdef EMULATOR // See macro definition in BookstoreDb.mmp
// On development environment the AppFullName points to z drive.
// Replace it to point to C drive, which is writable by our application.
parse.Set(_L("c:"), &appfullname, NULL);
#else // In device use the application fullname directly.
parse.Set(appfullname, NULL, NULL);
#endif
TFileName fn = parse.DriveAndPath();
// Make sure the path exists (create if not). This is needed in EMULATOR.
BaflUtils::EnsurePathExistsL(CCoeEnv::Static()->FsSession(), fn);
return fn;
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::ShowNoteL()
//
// Show a note. Note that successive frequent calls to this method results in
// showing the latest message only.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::ShowNoteL(const TDesC& aMessage) const
{
CAknInformationNote* note = new(ELeave)CAknInformationNote;
note->ExecuteLD(aMessage); // Deletes itself, when returns
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::TitlesArrayL()
//
// Build an array of book titles from an array having books with full info.
// ---------------------------------------------------------------------------
//
CDesCArrayFlat* CBookstoreAppUi::TitlesArrayL(
const CDesCArrayFlat* aFullBookInfoArray) const
{
// Assume the items within aFullBookInfoArray are in the format:
// <Author>|<Title>|<Description>
CDesCArrayFlat* resultArray =
new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
CleanupStack::PushL(resultArray);
TPtrC16 sourceRow;
TInt startPos = 0;
TInt endPos = 0;
// Iterate through the books.
// From each book row, parse the <Title> and append it to result array.
for(TInt i=0; i<aFullBookInfoArray->MdcaCount(); i++)
{
sourceRow.Set(aFullBookInfoArray->MdcaPoint(i));
startPos = sourceRow.Locate('|') + 1; // exclude '|' from result
endPos = sourceRow.LocateReverse('|');
resultArray->AppendL(sourceRow.Mid(startPos, endPos-startPos));
}
CleanupStack::Pop(resultArray);
return resultArray;
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::QueryTextL()
//
// Show simple text query dialos for the user
// ---------------------------------------------------------------------------
//
TBool CBookstoreAppUi::QueryTextL(TDesC& aPrompt,
TDes& aResultText) const
{
// Note: aPrompt cannot be const TDesC&, because CAknTextQueryDialog
// does not accept const TDesC& as a second parameter.
CAknTextQueryDialog* dlg = new(ELeave)
CAknTextQueryDialog( aResultText, // result is placed here
aPrompt,
CAknTextQueryDialog::ENoTone );
dlg->SetMaxLength(aResultText.MaxLength());
return dlg->ExecuteLD(R_SIMPLE_TEXT_QUERY);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -