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