📄 bookstoreappui.cpp
字号:
// Remove database file from the system.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::RemoveDatabaseL()
{
_LIT(KMessage,"Database removed.");
iBookstoreDb = CBookstoreDb::NewL();
iBookstoreDb->RemoveDbL(iDatabaseFile);
delete iBookstoreDb;
iBookstoreDb = NULL;
ShowNoteL(KMessage);
}
// ---------------------------------------------------------------------------
// 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::AddBookL()
//
// Add a book to database. Query details from book editor view.
//
// There are two variants for insertion. See DBEngine.h fordetails.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::AddBookL(TBool aUseSql)
{
_LIT(KErrorMsg,"Failed. Make sure the fields are not empty.");
TInt err(KErrNone);
// Lengths are from DBEngine.h. Author uses default length (50)
TBuf<50> author;
TBuf<KTitleMaxLength> title;
TBuf<KDescriptionMaxLength> description;
iBookEditorView->GetAuthor(author);
iBookEditorView->GetTitle(title);
iBookEditorView->GetDescription(description);
if(aUseSql)
err = iBookstoreDb->AddBookWithSqlL(author,
title,
description);
else
err = iBookstoreDb->AddBookWithCppApiL(author,
title,
description);
if(err)
ShowNoteL(KErrorMsg);
else
ShowAllBooksL(); // Change back to listbox view
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::RemoveBookL()
//
// Remove selected book from the database.
//
// Implementation removes named book from the database. If there are multiple
// matches for the book title, the are all removed.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::RemoveBookL()
{
TBuf<KBookItemMaxLength> selectedBookTitle;
if(iListboxView->GetSelectedItemL(selectedBookTitle) != KErrNone)
{
_LIT(KErrorMsg,"Failed. Book not selected.");
ShowNoteL(KErrorMsg);
return;
}
TInt resultCount;
// Wildcards are also allowed, like 'Title*', or '?itle"
iBookstoreDb->RemoveBooksL(selectedBookTitle, resultCount);
ShowAllBooksL();
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::RemoveAllBooksL()
//
// Remove all books from database.
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::RemoveAllBooksL()
{
_LIT(KInfoText,"All books removed.");
TInt resultCount;
iBookstoreDb->RemoveAllBooksL(resultCount);
ShowNoteL(KInfoText);
ShowAllBooksL();
}
// ---------------------------------------------------------------------------
// 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::AddDateColumn()
//
// Adds a date column to the Books table - if the column does not exist already
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::AddDateColumn()
{
iBookstoreDb->AddDateColumnL();
ShowColumns();
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::RemoveDateColumn()
//
// Removes the date column from Books table - if the column exists
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::RemoveDateColumn()
{
iBookstoreDb->RemoveDateColumn();
ShowColumns();
}
// ---------------------------------------------------------------------------
// CBookstoreAppUi::ShowColumns()
//
// Show the columns of the bookstore (Books table)
// ---------------------------------------------------------------------------
//
void CBookstoreAppUi::ShowColumns()
{
_LIT(KColumnsCaption,"Columns in bookstore:");
ChangeView(EColumnsView); // construct the listbox view
iListboxView->SetCaptionL(KColumnsCaption);
CDesCArrayFlat* tmp = iBookstoreDb->ColumnNamesAndSizes();
iListboxView->SetListItems(tmp); // takes ownership
}
// ---------------------------------------------------------------------------
// 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 + -