browserui/browser/FavouritesInc/BrowserFavouritesModel.h
changeset 0 84ad3b177aa3
child 24 868cceedabd3
equal deleted inserted replaced
-1:000000000000 0:84ad3b177aa3
       
     1 /*
       
     2 * Copyright (c) 2002 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *      Declaration of class CBrowserFavouritesModel.
       
    16 *      
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 #ifndef BROWSER_FAVOURITES_MODEL_H
       
    22 #define BROWSER_FAVOURITES_MODEL_H
       
    23 
       
    24 //  INCLUDES
       
    25 
       
    26 #include <e32base.h>
       
    27 #include <FavouritesDbObserver.h>
       
    28 #include "BrowserBookmarksOrder.h"
       
    29 #include "FavouritesDb.h"
       
    30 #include "FavouritesFile.h"
       
    31 #include "ApiProvider.h"
       
    32 
       
    33 // FORWARD DECLARATION
       
    34 
       
    35 class CFavouritesItemList;
       
    36 class CActiveFavouritesDbNotifier;
       
    37 class MBrowserFavouritesModelObserver;
       
    38 class CFavouritesItem;
       
    39 
       
    40 // CLASS DECLARATION
       
    41 
       
    42 /**
       
    43 * Model for the Favourites Views of the WML Browser.
       
    44 * This is a layer above the actual engines that do the data manipulation;
       
    45 * and provides UI support for that (i.e. dialogs, notes etc.)
       
    46 * Pure virtual.
       
    47 */
       
    48 class CBrowserFavouritesModel: public CBase, public MFavouritesDbObserver
       
    49 	{
       
    50 
       
    51     public:     // types
       
    52 
       
    53         /**
       
    54         * Answer for database queries.
       
    55         */
       
    56         enum TDbQuery
       
    57             {
       
    58             EYes,   ///< Answer to query is yes.
       
    59             ENo,    ///< Answer to query is no.
       
    60             EError  ///< Could not get answer (i.e. database is busy etc.).
       
    61             };
       
    62 
       
    63         /**
       
    64         * Rename mode for adding / updating items (what to do if an item
       
    65         * to be added / updated in the database has a conflicting name).
       
    66         */
       
    67         enum TRenameMode
       
    68             {
       
    69             EAutoRename,    ///< Rename silently.
       
    70             EDontRename,    ///< Do not rename (quit operation).
       
    71             EAskIfRename    ///< Ask user.
       
    72             };
       
    73 
       
    74 	public:     // construction
       
    75 
       
    76         /**
       
    77         * Destructor.
       
    78         */
       
    79         virtual ~CBrowserFavouritesModel();
       
    80 
       
    81 	public:     // string resources
       
    82 
       
    83         /**
       
    84         * Resource selector type for strings.
       
    85         */
       
    86         enum TTextResourceType
       
    87             {
       
    88             EDefaultName,   ///< Default name (e.g. "New Bookmark").
       
    89             ERenamePrompt,  ///< Rename prompt (e.g. "New Folder:").
       
    90             ENewPrompt,  ///< New prompt (e.g. "Folder name:").
       
    91             ESaved,         ///< Saved note (e.g. "Bookmark saved").
       
    92             ENotSaved       ///< Not saved note (e.g. "Bookmark not saved").
       
    93             };
       
    94 
       
    95         /**
       
    96         * Get resource id of some text for an item.
       
    97         * @param aItem The item to get resource id for.
       
    98         * @param aType Type of resource text.
       
    99         * @return Resource id of the text.
       
   100         */
       
   101         virtual TInt StringResourceId
       
   102             (
       
   103             const CFavouritesItem& aItem,
       
   104             TTextResourceType aType
       
   105             ) const = 0;
       
   106 
       
   107         /**
       
   108         * Set the name of this item to default (e.g. "New Bookmark",
       
   109         * "New Folder" etc.). The name that is set is read from resource,
       
   110         * the resource id is StringResourceId( EDefaultName ).
       
   111         * @param aItem The item to set default name for.
       
   112         */
       
   113         void SetNameToDefaultL( CFavouritesItem& aItem ) const;
       
   114 
       
   115     public:    // database opening / closing / access
       
   116 
       
   117         /**
       
   118         * Open the database.
       
   119         * After succesful open, notifications to observer may come.
       
   120         * Safe to call if already open.
       
   121         * @param aDbErrorNote If ETrue, unsuccesful open will bring up an
       
   122         * error note.
       
   123         * @return Error code.
       
   124         */
       
   125         TInt OpenDbL( TBool aDbErrorNote = ETrue );
       
   126 
       
   127         /**
       
   128         * Close the database. No further notification may come to the
       
   129         * observer.
       
   130         */
       
   131         void CloseDb();
       
   132 
       
   133         /**
       
   134         * Start a transaction.
       
   135         * @param aWrite If ETrue, the table is opened with write-lock on.
       
   136         * @param aDbErrorNote If ETrue, unsuccesful open will bring up an
       
   137         * error note.
       
   138         * @param aLffsCheck If ETrue (the default), free disk space is
       
   139         * checked to be above critical level (only if aWrite==ETrue).
       
   140         * Always use the default (except for deleting)!
       
   141         * @return Error code.
       
   142         */
       
   143         TInt BeginL
       
   144             ( TBool aWrite, TBool aDbErrorNote, TBool aLffsCheck = ETrue);
       
   145 
       
   146         /**
       
   147         * Commit the transaction.
       
   148         * Safe to call if not open.
       
   149         */
       
   150         void CommitL();
       
   151 
       
   152         /**
       
   153         * Add an observer.
       
   154         * @param aObserver Observer to be notified about database-related
       
   155         * events.
       
   156         */
       
   157         void AddObserverL( MBrowserFavouritesModelObserver& aObserver );
       
   158 
       
   159         /**
       
   160         * Remove an observer. Panic if not added.
       
   161         * @param aObserver The observer.
       
   162         */
       
   163         void RemoveObserver( MBrowserFavouritesModelObserver& aObserver );
       
   164 
       
   165         /**
       
   166         * Get the database.
       
   167         * @return The actual database.
       
   168         */
       
   169         inline RFavouritesDb Database() const;
       
   170 
       
   171         /**
       
   172         * Opens an item associated with aUid in aFavFile.
       
   173         * @return Error code.
       
   174         */
       
   175                 
       
   176         TInt OpenFavouritesFile ( RFavouritesFile & aFavFile, TInt aUid );
       
   177 
       
   178         /**
       
   179         * Opens an item associated with aUid in aFavFile for replacement.
       
   180         * @return Error code.
       
   181         */
       
   182         
       
   183         TInt ReplaceFavouritesFile ( RFavouritesFile & aFavFile, TInt aUid );        
       
   184 
       
   185         
       
   186     public:     // database query / update
       
   187 
       
   188         /**
       
   189         * Add an item (folder or bookmark) to the database.
       
   190         * @param aItem Item to add.
       
   191         * @param aDbErrorNote If ETrue, unsuccesful database open will bring up an
       
   192         * error note.
       
   193         * @param aRenameMode What to do in case of the name is conflicting.
       
   194         * @return Error code.
       
   195         */
       
   196         TInt AddL
       
   197             (
       
   198             CFavouritesItem& aItem,
       
   199             TBool aDbErrorNote,
       
   200             TRenameMode aRenameMode
       
   201             );
       
   202 
       
   203         /**
       
   204         * Update an item (folder or bookmark) in the database.
       
   205         * @param aItem Item data.
       
   206         * @param aUid Uid of item to be updated.
       
   207         * @param aDbErrorNote If ETrue, unsuccesful database open will bring up
       
   208         * an error note.
       
   209         * @param aRenameMode What to do in case of the name is conflicting.
       
   210         * @return Error code.
       
   211         */
       
   212         TInt UpdateL
       
   213             (
       
   214             CFavouritesItem& aItem,
       
   215             TInt aUid,
       
   216             TBool aDbErrorNote,
       
   217             TRenameMode aRenameMode
       
   218             );
       
   219 
       
   220         /**
       
   221         * Set Homepage.
       
   222         * @param aItem Item containing Homepage data.
       
   223         * @param aDbErrorNote If ETrue, unsuccesful database open will bring up an
       
   224         * error note.
       
   225         * @param aRenameMode What to do in case of the name is conflicting.
       
   226         * @return Error code.
       
   227         */
       
   228         TInt SetHomepageL
       
   229             (
       
   230             CFavouritesItem& aItem,
       
   231             TBool aDbErrorNote,
       
   232             TRenameMode aRenameMode
       
   233             );
       
   234 
       
   235         /**
       
   236         * Set Last Visited Page.
       
   237         * @param aItem Item containing Homepage data.
       
   238         * @param aDbErrorNote If ETrue, unsuccesful database open will bring up
       
   239         * an error note.
       
   240         * @param aRenameMode What to do in case of the name is conflicting.
       
   241         * @return Error code.
       
   242         */
       
   243         TInt SetLastVisitedL
       
   244             (
       
   245             CFavouritesItem& aItem,
       
   246             TBool aDbErrorNote,
       
   247             TRenameMode aRenameMode
       
   248             );
       
   249 
       
   250         /**
       
   251         * Check if there are any folders (except the root).
       
   252         * @return EYes, ENo or EError (if could not access the database).
       
   253         */
       
   254         TDbQuery AnyFoldersL();
       
   255 
       
   256         /**
       
   257         * Check if the given list of uids contains
       
   258         * any folders that are not empty.
       
   259         * @param aUids List of Uids to check.
       
   260         * @return EYes if there are non-empty folder(s), ENo if not,
       
   261         * EError if could not access the database.
       
   262         */
       
   263         TDbQuery AnyNonEmptyFoldersL( CArrayFix<TInt>& aUids );
       
   264 
       
   265         /**
       
   266         * Get the ordinal number of this folder in the alphabetically sorted
       
   267         * list of folders. Expects a transaction.
       
   268         * @param aFolder The folder to find.
       
   269         * @param aFolderCount On successful return, this will be set to the
       
   270         * number of folders.
       
   271         * @param On successful return, this will be set to the ordinal number
       
   272         * of the given folder.
       
   273         * @return Error code.
       
   274         */
       
   275         TInt FolderInfoL
       
   276             ( TInt aFolder, TInt& aFolderCount, TInt& aFolderIndex );
       
   277 
       
   278         /**
       
   279         * In the alphabetically sorted list of folders, get Uid of folder next
       
   280         * to aFolder. Expects a transaction.
       
   281         * @param aFolder Folder to start from.
       
   282         * @param aForward If ETrue, get next; if EFalse, get previous.
       
   283         * @return Uid of the next folder or KFavouritesNullUid in case of any
       
   284         * error.
       
   285         */
       
   286         TInt NextFolderL( TInt aFolder, TBool aForward );
       
   287 
       
   288         /**
       
   289         * Display a query for renaming this item. If the query is accepted,
       
   290         * the item is renamed.
       
   291         * @param aItem The item to rename.
       
   292         * @param aItem New item, we do not rename.
       
   293         * @return ETrue if the query is accepted and the item is renamed.
       
   294         */
       
   295         TBool RenameQueryL( CFavouritesItem& aItem, TBool aNewItem );
       
   296 
       
   297         /**
       
   298         * Sort items in a list.
       
   299         * @param aList List to sort.
       
   300         */
       
   301         void SortL( CFavouritesItemList& aList );
       
   302 
       
   303     public:     // convenience methods
       
   304 
       
   305         /**
       
   306         * Check if aItem has a unique name (in its parent folder), and if
       
   307         * not, make its name unique.
       
   308         * @param aItem Item to set a unique name for.
       
   309         * @return Error code.
       
   310         */
       
   311         TInt MakeUniqueNameL( CFavouritesItem& aItem );
       
   312 
       
   313 	protected:	// new functions
       
   314 
       
   315 		//MApiProvider& ApiProvider() const;
       
   316 
       
   317     public:     // from MFavouritesDbObserver
       
   318 
       
   319         /**
       
   320         * Handle database event.
       
   321         * @param aEvent Database event.
       
   322         */
       
   323         void HandleFavouritesDbEventL( RDbNotifier::TEvent aEvent );
       
   324 
       
   325         void ManualSortL(TInt aFolder, CBrowserBookmarksOrder* aBMOrder, CArrayFixFlat<TInt>* aOrderArray, CFavouritesItemList* aItems);
       
   326 
       
   327         /**
       
   328         * Adds the given uid to the last place in bookmarks list
       
   329         * @param aUid The uid to add
       
   330         * @param aOrderArray the array that stores uids in manual order
       
   331         * @param aCurrentOrder the object that stores manual sorting data
       
   332         */
       
   333 
       
   334         void AddUidToLastPlaceL( TInt aUid , CArrayFixFlat<TInt>* aOrderArray, CBrowserBookmarksOrder* aCurrentOrder);
       
   335 
       
   336 	protected :	// construction
       
   337 
       
   338         /**
       
   339         * Constructor.
       
   340         */
       
   341 		CBrowserFavouritesModel( MApiProvider& aApiProvider );
       
   342 
       
   343         /**
       
   344         * Second phase constructor. Leaves on failure.
       
   345         */
       
   346 		void ConstructL();
       
   347 
       
   348         /**
       
   349         * Returns with the underlying database's name.
       
   350         * Derived classes must provide this method.
       
   351         * @return The Database Engine's name.
       
   352         */
       
   353         virtual const TDesC& GetDBName();
       
   354      
       
   355     private:    // implementation details
       
   356 
       
   357         /**
       
   358         * Open the database. If unsuccesful, retry two more times in
       
   359         * 0.2 secs interval.
       
   360         * @return Error code.
       
   361         */
       
   362         TInt TimedOpenDbL();
       
   363 
       
   364         /**
       
   365         * Start a transaction. If unsuccesful, retry two more times in
       
   366         * 0.2 secs interval.
       
   367         * @param aWrite If ETrue, the table is opened with write-lock on.
       
   368         * @return Error code.
       
   369         */
       
   370         TInt TimedBeginL( TBool aWrite );
       
   371 
       
   372         /**
       
   373         * Add/update an item (folder or item) in the database; factoring out
       
   374         * the common code.
       
   375         * @param aItem Item data.
       
   376         * @param aUid
       
   377         *   - When KFavouritesNullUid, the item is added.
       
   378         *   - Otherwise, Uid of the item to be updated.
       
   379         * @param aDbErrorNote If ETrue, unsuccesful database open will bring up
       
   380         * an error note.
       
   381         * @param aRenameMode What to do in case of the name is conflicting.
       
   382         * @return Error code.
       
   383         */
       
   384         TInt AddUpdateL
       
   385             (
       
   386             CFavouritesItem& aItem,
       
   387             TInt aUid,
       
   388             TBool aDbErrorNote,
       
   389             TRenameMode aRenameMode
       
   390             );
       
   391             
       
   392         /**
       
   393         * Compare two descriptors (case insensitive).
       
   394         * @since 3.2
       
   395         * @param aFirst   first descriptor
       
   396         * @param aSecond  second descriptor
       
   397         * @return ETrue if strings match, EFalse otherwise
       
   398         */
       
   399         TBool CompareIgnoreCase( const TDesC& aFirst, const TDesC& aSecond );
       
   400 
       
   401 
       
   402 	protected:
       
   403         /**
       
   404         * Get all folders in the root, sorted, and push it on the cleanup
       
   405         * stack. No sanity check. Expects a transaction.
       
   406         * @return List of folders. Owner is the caller.
       
   407         */
       
   408 	virtual CFavouritesItemList* GetFoldersSortedLC();
       
   409 
       
   410 	private:	// observer support
       
   411 
       
   412         /**
       
   413         * Call each observer's HandleFavouritesModelChangeL().
       
   414         */
       
   415         void NotifyObserversL();
       
   416 
       
   417 	private:	// types
       
   418 
       
   419         enum TState     ///< Database state.
       
   420             {
       
   421             EClosed,    ///< Database is closed.
       
   422             EOpen,      ///< Database is open.
       
   423             ECorrupt    ///< Database is corrupted (and is closed)
       
   424             };
       
   425 
       
   426 	private:	// data
       
   427 
       
   428         /// Used database object. Owned.
       
   429         
       
   430         RFavouritesDb iDb;
       
   431         RFavouritesSession iFavouritesSess;        
       
   432         /// Notifier object. Owned.
       
   433         CActiveFavouritesDbNotifier* iNotifier;
       
   434         /// Observers. Array owned, contents not.
       
   435         CArrayPtrFlat<MBrowserFavouritesModelObserver>* iObservers;
       
   436         /// Database state.
       
   437         TState iState;
       
   438 
       
   439 		MApiProvider& iApiProvider;
       
   440 	};
       
   441 
       
   442 #include "BrowserFavouritesModel.inl"
       
   443 
       
   444 #endif
       
   445 
       
   446 // End of file