examples/Messaging/TextMTM/txts/TXTMBOX.CPP

00001 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
00002 // All rights reserved.
00003 // This component and the accompanying materials are made available
00004 // under the terms of "Eclipse Public License v1.0"
00005 // which accompanies this distribution, and is available
00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00007 //
00008 // Initial Contributors:
00009 // Nokia Corporation - initial contribution.
00010 //
00011 // Contributors:
00012 //
00013 // Description:
00014 //
00015 
00016 #if !defined(__TXTSPAN_H__)
00017 #include "TXTSPAN.H"
00018 #endif
00019 
00020 #if !defined(__MSVUIDS_H__)
00021 #include <msvuids.h>
00022 #endif
00023 
00024 #if !defined(__MSVIDS_H__)
00025 #include <msvids.h>
00026 #endif
00027 
00028 #include "TXTMBOX.H"
00029 
00030 //
00031 //      CTxtRefreshMBox: refresher class to synchronise real file system and service
00032 //
00033 
00034 CTxtRefreshMBox* CTxtRefreshMBox::NewL(RFs& aFs, TFileName& aRelativePath, TMsvId aCurrentRootEntryId, 
00035                                                                            CMsvServerEntry *aEntry, TMsvId aServiceEntryId,
00036                                                                            const TMTMTxtSettings& aTxtSettings)
00037 //
00038 //  1. sort files and entries by description.
00039 //
00040         {
00041         CTxtRefreshMBox* self = new (ELeave)CTxtRefreshMBox(aFs, aRelativePath, 
00042                 aCurrentRootEntryId, aEntry, aServiceEntryId, aTxtSettings);
00043         CleanupStack::PushL(self);
00044         self->ConstructL();
00045         CleanupStack::Pop(); // self
00046         return self;
00047         }       
00048         
00049 void CTxtRefreshMBox::ConstructL()
00050         {
00051         // Find absolute path of folder to refresh
00052         TFileName fullPath = iTxtSettings.RootFolder();         
00053         fullPath.Append(iRelativePath);
00054         
00055         // Get directory list
00056         User::LeaveIfError(iFs.GetDir(fullPath, KEntryAttDir | KEntryAttNormal,ESortByName 
00057                 | EDirsAnyOrder | EAscending, iFilelist));
00058 
00059         // Get sorted list of current entries
00060         iExistingEntries = new (ELeave) CMsvEntrySelection;
00061         TMsvSelectionOrdering order(KMsvNoGrouping, EMsvSortByDescription);    
00062     User::LeaveIfError(iEntry->SetEntry(iCurrentRootEntryId));
00063         iEntry->SetSort( order );
00064         iEntry->GetChildren(*iExistingEntries);
00065 
00066         iCurrentFile  = 0;
00067         iCurrentEntry = 0;
00068         }
00069 
00070 CTxtRefreshMBox::~CTxtRefreshMBox()
00071         {
00072         delete iExistingEntries;
00073         delete iFilelist;
00074         }
00075 
00076 void CTxtRefreshMBox::DeleteEntryL()
00077 // Delete current entry
00078         {
00079         __ASSERT_DEBUG(iCurrentEntry < iExistingEntries->Count(), gPanic(ETxtsInvalidEntryIndex));
00080 
00081         User::LeaveIfError(iEntry->SetEntry(iCurrentRootEntryId));
00082         iEntry->DeleteEntry((*iExistingEntries)[iCurrentEntry]); // deletes recursively
00083         }
00084 
00085 TInt CTxtRefreshMBox::CreateChild(const TDesC& aDescription, const TDesC& aDetails, TUid aMessageType, 
00086                                                                   const TTime& aDate, const TInt aSize)
00087 //
00088 // Create a child. return its Id.
00089 //
00090         {
00091         TMsvEntry newChildEntry;
00092         newChildEntry.iType= aMessageType;
00093         newChildEntry.iMtm = KUidMsgTypeText;
00094         newChildEntry.iDescription.Set(aDescription);
00095         newChildEntry.iDetails.Set(aDetails);              
00096         newChildEntry.iServiceId = iServiceEntryId;
00097         newChildEntry.iSize = aSize;
00098         newChildEntry.iDate=aDate;
00099         iEntry->CreateEntry(newChildEntry);
00100         return newChildEntry.Id();
00101         }
00102 
00103 TMsvId CTxtRefreshMBox::InsertFileL()
00104 // Insert file in entries list
00105 //
00106 // Return the new id if this is a folder entry, or KMsvNullIndexEntryId if it isn't
00107 //
00108         {
00109         __ASSERT_DEBUG(iCurrentFile < iFilelist->Count(), gPanic(ETxtsInvalidEntryIndex));
00110         User::LeaveIfError(iEntry->SetEntry( iCurrentRootEntryId ));
00111         TEntry fileEntry = (*iFilelist)[iCurrentFile];
00112         TTime date;
00113         TInt size;
00114         if (fileEntry.IsDir())
00115                 {
00116                 date.HomeTime();
00117                 size=0;
00118                 return CreateChild(fileEntry.iName, fileEntry.iName, KUidMsvFolderEntry, date, size);
00119                 }
00120         else
00121                 {
00122                 TFileName filename = iTxtSettings.RootFolder();
00123                 filename.Append(iRelativePath);
00124                 filename.Append(fileEntry.iName);
00125                 RFile file;
00126                 User::LeaveIfError(file.Open(iFs,filename,EFileRead));
00127 
00128                 file.Modified(date);
00129                 file.Size(size);
00130                 file.Close();
00131                 CreateChild(fileEntry.iName, iRelativePath, KUidMsvMessageEntry, date, size);
00132 
00133                 return KMsvNullIndexEntryId;
00134                 }
00135         }
00136 
00137 TBool CTxtRefreshMBox::DoStepL()
00138 // Main method
00139 //
00140 //  2. walk over the list: 
00141 //  3.   if file name smaller than that in current pos in list:
00142 //  4.     if file doesn't exist in list, insert it in list, and move to next file.
00143 //  5.   else if file equals description
00144 //  6.     skip both
00145 //  7.   else delete TMsvEntry on the other side, and go to next in list.
00146 //  =
00147 //  a. at the end of either list: if the file list was finished, delete all in the entry list from 
00148 //     current position. if entry list was finished, insert all files starting at current.
00149 //  b. when skipping a entry, and the entry is a folder, do the folder also.
00150 //
00151         {
00152         // If both file list and entry list are done, task is done.
00153     TInt nrFiles = iFilelist->Count();
00154     TInt existingEntries = iExistingEntries->Count();
00155     if (iCurrentFile  == nrFiles && iCurrentEntry == existingEntries)
00156                 return ETrue;
00157 
00158         TMsvId folderId = KMsvNullIndexEntryId;
00159 
00160         // End of the file list: delete all entries following.
00161         if (iCurrentFile == nrFiles)
00162                 {
00163                 // Delete current entry.
00164                 DeleteEntryL();
00165                 // Step to next entry.
00166                 iCurrentEntry++;
00167                 }
00168         // End of entries list. Append current file to end of list.
00169         else if (iCurrentEntry == existingEntries)
00170                 {
00171                 // Insert file in entries list.
00172                 // if file is folder, do the folder recursively
00173                 folderId = InsertFileL();
00174                 // step to next file
00175                 iCurrentFile++;
00176                 }
00177         else
00178                 {
00179                 // Continue walking
00180                 User::LeaveIfError(iEntry->SetEntry( (*iExistingEntries)[iCurrentEntry] ));
00181 
00182                 TInt compare = (*iFilelist)[iCurrentFile].iName.CompareF(iEntry->Entry().iDescription);
00183 
00184                 // If current file name smaller than name of current entry, then the file doesn't yet
00185                 // exist in the list, and needs to be added.
00186                 if ( compare < 0 )
00187                         {
00188                         // Insert file in entries list.
00189                         // if file is folder, do the folder recursively
00190                         folderId = InsertFileL();
00191                         // step to next file
00192                         iCurrentFile++;
00193                         }
00194                 // Files are equal. Both should be the same.
00195                 else if (compare == 0)
00196                         {
00197                         // if file is folder, do the folder recursively
00198                         if (iEntry->Entry().iType == KUidMsvFolderEntry)
00199                                 folderId = (*iExistingEntries)[iCurrentEntry];
00200                         // Skip both file and entry
00201                         iCurrentFile++;
00202                         iCurrentEntry++;
00203                         }
00204                 // File name is greater than name of current entry, so the current entry 
00205                 // shouldn't be there.
00206                 else
00207                         {
00208                         // Delete entry.
00209                         DeleteEntryL();
00210                         // Step to next entry.
00211                         iCurrentEntry++;
00212                         }
00213                 }
00214 
00215         // If just passing folder, do the folder recursively.
00216         if (folderId != KMsvNullIndexEntryId)
00217                 {
00218                 User::LeaveIfError(iEntry->SetEntry(folderId));
00219                 
00220                 // Set absolute name
00221                 TFileName subDir = iRelativePath;
00222                 subDir.Append(iEntry->Entry().iDescription);
00223                 subDir.Append(KPathDelimiter);
00224 
00225                 CTxtRefreshMBox *folderSynchroniser = CTxtRefreshMBox::NewL(iFs, subDir, folderId, iEntry, iServiceEntryId, iTxtSettings);
00226                 CleanupStack::PushL(folderSynchroniser);
00227                 while (!folderSynchroniser->DoStepL()) ;
00228                 CleanupStack::PopAndDestroy(); //folderSynchroniser
00229                 }
00230         
00231         return EFalse;
00232         }
00233 

Generated on Thu Jan 21 10:32:59 2010 for TB10.1 Example Applications by  doxygen 1.5.3