00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <avkon.hrh>
00031 #include <badesca.h>
00032 #include <bautils.h>
00033 #include <eikmenup.h>
00034 #include <eikapp.h>
00035 #include <aknnotewrappers.h>
00036 #include <aknquerydialog.h>
00037 #include "DBMSAppUi.h"
00038 #include "DBMSAppView.h"
00039 #include "DBMSListboxView.h"
00040 #include "DBMSEditorView.h"
00041 #include "DBMS.hrh"
00042 #include <DBMS.rsg>
00043
00044 #define KEnableSkinFlag 0x1000
00045
00046
00047 const TInt KArrayGranularity = 5;
00048
00049
00050 _LIT(KCDrive, "C:");
00051
00052
00053
00054
00055
00056
00057
00058 void CDBMSAppUi::ConstructL()
00059 {
00060 BaseConstructL(EAknEnableSkin);
00061
00062 iCurrentView = ENone;
00063
00064 ChangeViewL(EMainView);
00065
00066 iDatabaseFile = DatabaseDriveAndPathL();
00067
00068 iDatabaseFile.Append(KDatabaseFile);
00069 }
00070
00071
00072
00073
00074
00075
00076
00077
00078 CDBMSAppUi::CDBMSAppUi() : iAppView(NULL), iListboxView(NULL),
00079 iBookEditorView(NULL), iBookDb(NULL)
00080 {
00081 }
00082
00083
00084
00085
00086
00087
00088
00089
00090 CDBMSAppUi::~CDBMSAppUi()
00091 {
00092 if (iAppView)
00093 {
00094 if(iCurrentView == EMainView)
00095 iEikonEnv->RemoveFromStack(iAppView);
00096 delete iAppView;
00097 iAppView = NULL;
00098 }
00099
00100 if (iListboxView)
00101 {
00102 if(iCurrentView == EListView || iCurrentView == EColumnsView)
00103 iEikonEnv->RemoveFromStack(iListboxView);
00104 delete iListboxView;
00105 iListboxView = NULL;
00106 }
00107
00108 if (iBookEditorView)
00109 {
00110 if(iCurrentView == EEditorView)
00111 iEikonEnv->RemoveFromStack(iBookEditorView);
00112 delete iBookEditorView;
00113 iBookEditorView = NULL;
00114 }
00115
00116 if(iBookDb)
00117 {
00118 iBookDb->Close();
00119 delete iBookDb;
00120 iBookDb = NULL;
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129
00130
00131 void CDBMSAppUi::ChangeViewL(TViewId aNewView)
00132 {
00133
00134 if(aNewView == iCurrentView)
00135 return;
00136
00137
00138 switch(iCurrentView)
00139 {
00140 case EMainView:
00141 RemoveFromStack(iAppView);
00142 delete iAppView;
00143 iAppView = NULL;
00144 break;
00145
00146 case EListView:
00147 RemoveFromStack(iListboxView);
00148 delete iListboxView;
00149 iListboxView = NULL;
00150 break;
00151
00152 case EColumnsView:
00153 RemoveFromStack(iListboxView);
00154 delete iListboxView;
00155 iListboxView = NULL;
00156 break;
00157
00158 case EEditorView:
00159 RemoveFromStack(iBookEditorView);
00160 delete iBookEditorView;
00161 iBookEditorView = NULL;
00162 break;
00163
00164 case ENone:
00165 default:
00166 break;
00167 }
00168
00169 switch(aNewView)
00170 {
00171 case EMainView:
00172 iAppView = CDBMSAppView::NewL(ClientRect());
00173 AddToStackL(iAppView);
00174 break;
00175
00176 case EListView:
00177 case EColumnsView:
00178 iListboxView = CDBMSListboxView::NewL(ClientRect());
00179 AddToStackL(iListboxView);
00180 break;
00181
00182 case EEditorView:
00183 iBookEditorView = CDBMSEditorView::NewL(ClientRect());
00184 AddToStackL(iBookEditorView);
00185 break;
00186
00187 case ENone:
00188 default:
00189 break;
00190 }
00191 iCurrentView = aNewView;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 void CDBMSAppUi::DynInitMenuPaneL(TInt aResourceId,
00203 CEikMenuPane* aMenuPane)
00204 {
00205 if (aResourceId != R_MAIN_MENU)
00206 {
00207 return;
00208 }
00209
00210
00211 aMenuPane->SetItemDimmed(EOpenCmd, ETrue);
00212 aMenuPane->SetItemDimmed(ECreateCmd, ETrue);
00213 aMenuPane->SetItemDimmed(ERemoveDbCmd, ETrue);
00214 aMenuPane->SetItemDimmed(EAddBookCmd, ETrue);
00215 aMenuPane->SetItemDimmed(EBackCmd, ETrue);
00216 aMenuPane->SetItemDimmed(EAddBookAPICmd, ETrue);
00217 aMenuPane->SetItemDimmed(EAddBookSQLCmd, ETrue);
00218 aMenuPane->SetItemDimmed(ERemoveBookCmd, ETrue);
00219 aMenuPane->SetItemDimmed(ERemoveAllBooksCmd, ETrue);
00220 aMenuPane->SetItemDimmed(EChangeTitleCmd, ETrue);
00221 aMenuPane->SetItemDimmed(EGetAllBooksCmd, ETrue);
00222 aMenuPane->SetItemDimmed(ESearchBooksCmd, ETrue);
00223 aMenuPane->SetItemDimmed(EQuickFindCmd, ETrue);
00224 aMenuPane->SetItemDimmed(EAddDateCmd, ETrue);
00225 aMenuPane->SetItemDimmed(ERemoveDateCmd, ETrue);
00226 aMenuPane->SetItemDimmed(EColumnNamesCmd, ETrue);
00227 aMenuPane->SetItemDimmed(ECloseCmd, ETrue);
00228
00229
00230 switch(iCurrentView)
00231 {
00232 case EMainView:
00233 {
00234
00235 if(BaflUtils::FileExists(CCoeEnv::Static()->FsSession(),
00236 iDatabaseFile))
00237 {
00238 aMenuPane->SetItemDimmed(EOpenCmd, EFalse);
00239 aMenuPane->SetItemDimmed(ERemoveDbCmd, EFalse);
00240 }
00241 aMenuPane->SetItemDimmed(ECreateCmd, EFalse);
00242 break;
00243 }
00244
00245 case EListView:
00246 {
00247
00248 if(iBookDb && iBookDb->IsOpen())
00249 {
00250 aMenuPane->SetItemDimmed(EAddBookCmd, EFalse);
00251 aMenuPane->SetItemDimmed(ERemoveBookCmd, EFalse);
00252 aMenuPane->SetItemDimmed(ERemoveAllBooksCmd, EFalse);
00253 aMenuPane->SetItemDimmed(EChangeTitleCmd, EFalse);
00254 aMenuPane->SetItemDimmed(EGetAllBooksCmd, EFalse);
00255 aMenuPane->SetItemDimmed(ESearchBooksCmd, EFalse);
00256 aMenuPane->SetItemDimmed(EQuickFindCmd, EFalse);
00257 aMenuPane->SetItemDimmed(EColumnNamesCmd, EFalse);
00258 aMenuPane->SetItemDimmed(ECloseCmd, EFalse);
00259 }
00260 break;
00261 }
00262
00263 case EColumnsView:
00264 {
00265 if(iBookDb && iBookDb->IsOpen())
00266 {
00267
00268 TBool hasDateColumn(EFalse);
00269 User::LeaveIfError(iBookDb->HasDateColumn(hasDateColumn));
00270
00271 aMenuPane->SetItemDimmed(EBackCmd, EFalse);
00272 if(!hasDateColumn)
00273 aMenuPane->SetItemDimmed(EAddDateCmd, EFalse);
00274 else
00275 aMenuPane->SetItemDimmed(ERemoveDateCmd, EFalse);
00276 aMenuPane->SetItemDimmed(ECloseCmd, EFalse);
00277 }
00278 break;
00279 }
00280
00281 case EEditorView:
00282 {
00283 if(iBookDb && iBookDb->IsOpen())
00284 {
00285 aMenuPane->SetItemDimmed(EBackCmd, EFalse);
00286 aMenuPane->SetItemDimmed(EAddBookAPICmd, EFalse);
00287 aMenuPane->SetItemDimmed(EAddBookSQLCmd, EFalse);
00288 aMenuPane->SetItemDimmed(ECloseCmd, EFalse);
00289 }
00290 break;
00291 }
00292
00293 case ENone:
00294 default:
00295 break;
00296 }
00297 }
00298
00299
00300
00301
00302
00303
00304
00305 void CDBMSAppUi::HandleCommandL(TInt aCommand)
00306 {
00307
00308 switch(aCommand)
00309 {
00310
00311 case EOpenCmd:
00312 OpenDatabaseL();
00313 break;
00314
00315 case ECreateCmd:
00316 CreateDatabaseL();
00317 break;
00318
00319 case ERemoveDbCmd:
00320 RemoveDatabaseL();
00321 break;
00322
00323 case EBackCmd:
00324 ShowAllBooksL();
00325 break;
00326
00327 case EAddBookCmd:
00328 ShowBookEditorViewL();
00329 break;
00330
00331 case EAddBookAPICmd:
00332 AddBookL(EFalse);
00333 break;
00334
00335 case EAddBookSQLCmd:
00336 AddBookL(ETrue);
00337 break;
00338
00339 case ERemoveBookCmd:
00340 RemoveBookL();
00341 break;
00342
00343 case ERemoveAllBooksCmd:
00344 RemoveAllBooksL();
00345 break;
00346
00347 case EChangeTitleCmd:
00348 UpdateBookTitleL();
00349 break;
00350
00351 case EGetAllBooksCmd:
00352 ShowAllBooksL();
00353 break;
00354
00355 case ESearchBooksCmd:
00356 SearchBooksL();
00357 break;
00358
00359 case EQuickFindCmd:
00360 IndexFindL();
00361 break;
00362
00363 case EAddDateCmd:
00364 AddDateColumnL();
00365 break;
00366
00367 case ERemoveDateCmd:
00368 RemoveDateColumnL();
00369 break;
00370
00371 case EColumnNamesCmd:
00372 ShowColumnsL();
00373 break;
00374
00375 case ECloseCmd:
00376 CloseDatabaseL();
00377 break;
00378
00379 case EAknSoftkeyExit:
00380 case EEikCmdExit:
00381 Exit();
00382 break;
00383
00384 default:
00385 break;
00386 }
00387 }
00388
00389
00390
00391
00392
00393
00394 void CDBMSAppUi::OpenDatabaseL()
00395 {
00396 iBookDb = CBookDb::NewL();
00397 iBookDb->OpenDb(iDatabaseFile);
00398 ShowAllBooksL();
00399 }
00400
00401
00402
00403
00404
00405
00406
00407 void CDBMSAppUi::CreateDatabaseL()
00408 {
00409 _LIT(KMessage,"Database created.");
00410 iBookDb = CBookDb::NewL();
00411 iBookDb->CreateDb(iDatabaseFile);
00412 ShowAllBooksL();
00413 ShowNoteL(KMessage);
00414 }
00415
00416
00417
00418
00419
00420
00421
00422
00423 void CDBMSAppUi::RemoveDatabaseL()
00424 {
00425 _LIT(KMessage,"Database removed.");
00426 iBookDb = CBookDb::NewL();
00427 iBookDb->RemoveDb(iDatabaseFile);
00428 delete iBookDb;
00429 iBookDb = NULL;
00430 ShowNoteL(KMessage);
00431 }
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 void CDBMSAppUi::CloseDatabaseL()
00442 {
00443 if(iBookDb && iBookDb->IsOpen())
00444 {
00445 iBookDb->Close();
00446 delete iBookDb;
00447 iBookDb = NULL;
00448 }
00449 ChangeViewL(EMainView);
00450 }
00451
00452
00453
00454
00455
00456
00457
00458
00459 void CDBMSAppUi::ShowBookEditorViewL()
00460 {
00461 ChangeViewL(EEditorView);
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473 void CDBMSAppUi::AddBookL(TBool aUseSql)
00474 {
00475
00476 _LIT(KErrorMsg,"Failed. Make sure the fields are not empty.");
00477 TInt err(KErrNone);
00478
00479
00480 TBuf<50> author;
00481 TBuf<KTitleMaxLength> title;
00482 TBuf<KDescriptionMaxLength> description;
00483
00484
00485 iBookEditorView->GetAuthorL(author);
00486 iBookEditorView->GetTitleL(title);
00487 iBookEditorView->GetDescriptionL(description);
00488
00489 if(aUseSql)
00490 err = iBookDb->AddBookWithSql(author,
00491 title,
00492 description);
00493 else
00494 err = iBookDb->AddBookWithCppApiL(author,
00495 title,
00496 description);
00497
00498 if(err)
00499 ShowNoteL(KErrorMsg);
00500 else
00501 ShowAllBooksL();
00502
00503 }
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515 void CDBMSAppUi::RemoveBookL()
00516 {
00517
00518 TBuf<KBookItemMaxLength> selectedBookTitle;
00519 if(iListboxView->GetSelectedItem(selectedBookTitle) != KErrNone)
00520 {
00521 _LIT(KErrorMsg,"Failed. Book not selected.");
00522 ShowNoteL(KErrorMsg);
00523 return;
00524 }
00525
00526 TInt resultCount;
00527
00528 iBookDb->RemoveBooks(selectedBookTitle, resultCount);
00529 ShowAllBooksL();
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539 void CDBMSAppUi::RemoveAllBooksL()
00540 {
00541 _LIT(KInfoText,"All Books removed.");
00542 TInt resultCount;
00543 iBookDb->RemoveAllBooks(resultCount);
00544 ShowNoteL(KInfoText);
00545 ShowAllBooksL();
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555 void CDBMSAppUi::UpdateBookTitleL()
00556 {
00557 _LIT(KQuery,"New title(s) for: ");
00558 TBuf<KBookItemMaxLength> selectedBookTitle;
00559 if(iListboxView->GetSelectedItem(selectedBookTitle) != KErrNone)
00560 {
00561 _LIT(KErrorMsg,"Failed. Book not selected.");
00562 ShowNoteL(KErrorMsg);
00563 return;
00564 }
00565 TBuf<KBookItemMaxLength+32> prompt;
00566 TBuf<KBookItemMaxLength> newTitle;
00567
00568 prompt.Append(KQuery);
00569 prompt.Append(selectedBookTitle);
00570
00571 if(QueryTextL(prompt, newTitle))
00572 {
00573 newTitle.Trim();
00574
00575 iBookDb->UpdateBookTitle(selectedBookTitle, newTitle);
00576 ShowAllBooksL();
00577 }
00578 }
00579
00580
00581
00582
00583
00584
00585
00586
00587 void CDBMSAppUi::ShowAllBooksL()
00588 {
00589 _LIT(KAllBooksTitle,"Books in DBMS:");
00590
00591
00592
00593
00594 CDesCArrayFlat* Books = iBookDb->GetAllBooksL();
00595 CleanupStack::PushL(Books);
00596 CDesCArrayFlat* titles = TitlesArrayL(Books);
00597 CleanupStack::PushL(titles);
00598
00599 ChangeViewL(EListView);
00600 iListboxView->SetCaptionL(KAllBooksTitle);
00601 CleanupStack::Pop(titles);
00602 iListboxView->SetListItemsL(titles);
00603 CleanupStack::PopAndDestroy(Books);
00604
00605 }
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618 void CDBMSAppUi::SearchBooksL()
00619 {
00620 _LIT(KSearchResult, "Search result:");
00621 _LIT(KBookSearch, "Book search");
00622 _LIT(KWildCard, "*");
00623
00624 TBuf<KTitleMaxLength> titlePattern;
00625 TBuf<32> prompt(KBookSearch);
00626
00627 QueryTextL(prompt, titlePattern);
00628 titlePattern.Append(KWildCard);
00629
00630 ChangeViewL(EListView);
00631 iListboxView->SetCaptionL(KSearchResult);
00632
00633
00634
00635
00636 CDesCArrayFlat* Books =
00637 iBookDb->GetBooksByKeyL(KBooksTitleCol, titlePattern);
00638 CleanupStack::PushL(Books);
00639
00640 CDesCArrayFlat* titles = TitlesArrayL(Books);
00641 iListboxView->SetListItemsL(titles);
00642
00643 CleanupStack::PopAndDestroy(Books);
00644 }
00645
00646
00647
00648
00649
00650
00651
00652
00653 void CDBMSAppUi::IndexFindL()
00654 {
00655 _LIT(KDetailsCaption,"Book details:");
00656
00657
00658 TBuf<KBookItemMaxLength> selectedBook;
00659 if(iListboxView->GetSelectedItem(selectedBook) != KErrNone)
00660 {
00661 _LIT(KErrorMsg,"Failed. Book not selected.");
00662 ShowNoteL(KErrorMsg);
00663 return;
00664 }
00665
00666
00667 TBuf<KBookItemMaxLength> result;
00668 TInt err = iBookDb->GetABookFast(selectedBook, result);
00669 if(err==KErrNotFound)
00670 {
00671 _LIT(KNotFoundMsg,"Book not found.");
00672 ShowNoteL(KNotFoundMsg);
00673 return;
00674 }
00675 iEikonEnv->InfoWinL(KDetailsCaption, result);
00676 }
00677
00678
00679
00680
00681
00682
00683
00684
00685 void CDBMSAppUi::AddDateColumnL()
00686 {
00687 iBookDb->AddDateColumn();
00688 ShowColumnsL();
00689 }
00690
00691
00692
00693
00694
00695
00696
00697
00698 void CDBMSAppUi::RemoveDateColumnL()
00699 {
00700 iBookDb->RemoveDateColumn();
00701 ShowColumnsL();
00702 }
00703
00704
00705
00706
00707
00708
00709
00710
00711 void CDBMSAppUi::ShowColumnsL()
00712 {
00713 _LIT(KColumnsCaption,"Columns in Book:");
00714 ChangeViewL(EColumnsView);
00715 iListboxView->SetCaptionL(KColumnsCaption);
00716 CDesCArrayFlat* tmp = iBookDb->ColumnNamesAndSizesL();
00717 iListboxView->SetListItemsL(tmp);
00718 }
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728 TFileName CDBMSAppUi::ApplicationDriveAndPathL() const
00729 {
00730 TFileName appfullname(Application()->AppFullName());
00731 TParse parse;
00732
00733 #ifdef __WINS__ // See macro definition in DBMSDb.mmp
00734
00735
00736
00737
00738 parse.Set(KCDrive, &appfullname, NULL);
00739
00740 #else // In device use the application fullname directly.
00741
00742 parse.Set(appfullname, NULL, NULL);
00743
00744 #endif
00745
00746 TFileName fn = parse.DriveAndPath();
00747
00748 BaflUtils::EnsurePathExistsL(CCoeEnv::Static()->FsSession(), fn);
00749 return fn;
00750 }
00751
00752
00753
00754
00755
00756 TFileName CDBMSAppUi::DatabaseDriveAndPathL() const
00757 {
00758 RFs fsSession;
00759 User::LeaveIfError(fsSession.Connect());
00760 CleanupClosePushL(fsSession);
00761
00762 TFileName appfullname, fn;
00763 appfullname = Application()->AppFullName();
00764 fsSession.PrivatePath(fn);
00765
00766 #ifdef __WINS__ // See macro definition in DBMS.mmp
00767 fn.Insert(0,KCDrive);
00768
00769 #else // In device use the application fullname directly.
00770 TParse parse;
00771 parse.Set(appfullname, NULL, NULL);
00772 fn.Insert(0,parse.Drive());
00773 #endif
00774
00775 BaflUtils::EnsurePathExistsL(fsSession, fn);
00776 CleanupStack::PopAndDestroy(&fsSession);
00777
00778 return fn;
00779 }
00780
00781
00782
00783
00784
00785
00786
00787
00788 void CDBMSAppUi::ShowNoteL(const TDesC& aMessage) const
00789 {
00790
00791 CAknInformationNote* note = new(ELeave)CAknInformationNote;
00792 note->ExecuteLD(aMessage);
00793 }
00794
00795
00796
00797
00798
00799
00800
00801
00802 CDesCArrayFlat* CDBMSAppUi::TitlesArrayL(
00803 const CDesCArrayFlat* aFullDBMSInfoArray) const
00804 {
00805
00806
00807
00808 CDesCArrayFlat* resultArray =
00809 new (ELeave)CDesC16ArrayFlat(KArrayGranularity);
00810 CleanupStack::PushL(resultArray);
00811
00812 TPtrC16 sourceRow;
00813 TInt startPos = 0;
00814 TInt endPos = 0;
00815
00816
00817
00818 for(TInt i=0; i<aFullDBMSInfoArray->MdcaCount(); i++)
00819 {
00820 sourceRow.Set(aFullDBMSInfoArray->MdcaPoint(i));
00821 startPos = sourceRow.Locate('|') + 1;
00822 endPos = sourceRow.LocateReverse('|');
00823 resultArray->AppendL(sourceRow.Mid(startPos, endPos-startPos));
00824 }
00825 CleanupStack::Pop(resultArray);
00826 return resultArray;
00827 }
00828
00829
00830
00831
00832
00833
00834
00835 TBool CDBMSAppUi::QueryTextL(TDesC& aPrompt,
00836 TDes& aResultText) const
00837 {
00838
00839
00840
00841 CAknTextQueryDialog* dlg = new(ELeave)
00842 CAknTextQueryDialog( aResultText,
00843 aPrompt,
00844 CAknTextQueryDialog::ENoTone );
00845
00846 dlg->SetMaxLength(aResultText.MaxLength());
00847 return dlg->ExecuteLD(R_SIMPLE_TEXT_QUERY);
00848 }
00849
00850
00851
00852
00853
00854
00855 void CDBMSAppUi::HandleStatusPaneSizeChange()
00856 {
00857 CAknAppUi::HandleStatusPaneSizeChange();
00858
00859 if(iAppView)
00860 iAppView->SetRect( ClientRect() );
00861 if(iListboxView)
00862 iListboxView->SetRect( ClientRect() );
00863 if(iBookEditorView)
00864 iBookEditorView->SetRect( ClientRect() );
00865 }
00866
00867