Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
6.0 KiB

  1. //=============================================================================
  2. // The CUndoLog is used to implement a log of undo entries to allow MSConfig
  3. // to reverse any changes it may have made. Each tab object is responsible for
  4. // writing a string when it makes changes - this string can be used to undo
  5. // the changes the tab made.
  6. //
  7. // The undo log file will look like this:
  8. //
  9. // [timestamp tabname "description" <SHOW|FINAL>]
  10. // tab specific string - any line starting with a "[" will have a
  11. // backslash preceding it
  12. //
  13. // The entries will be arranged in chronological order (most recent first).
  14. // The "description" field will be the only one shown to the user - so it
  15. // will be the only one which needs to be localized. The tab is responsible
  16. // for supplying this text.
  17. //=============================================================================
  18. #pragma once
  19. //-----------------------------------------------------------------------------
  20. // This class encapsulates an undo entry (instance of this class will be
  21. // saved in a list).
  22. //-----------------------------------------------------------------------------
  23. class CUndoLogEntry
  24. {
  25. public:
  26. CUndoLogEntry(const CString & strTab, const CString & strDescription, const CString & strEntry, BOOL fFinal, const COleDateTime & timestamp)
  27. : m_strTab(strTab),
  28. m_strDescription(strDescription),
  29. m_strEntry(strEntry),
  30. m_timestamp(timestamp),
  31. m_fFinal(fFinal)
  32. {};
  33. CUndoLogEntry(const CString & strTab, const CString & strDescription, const CString & strEntry, BOOL fFinal)
  34. : m_strTab(strTab),
  35. m_strDescription(strDescription),
  36. m_strEntry(strEntry),
  37. m_timestamp(COleDateTime::GetCurrentTime()),
  38. m_fFinal(fFinal)
  39. {};
  40. ~CUndoLogEntry() {};
  41. CString m_strTab;
  42. CString m_strDescription;
  43. CString m_strEntry;
  44. COleDateTime m_timestamp;
  45. BOOL m_fFinal;
  46. };
  47. //-----------------------------------------------------------------------------
  48. // This class implements the undo log.
  49. //-----------------------------------------------------------------------------
  50. class CUndoLog
  51. {
  52. public:
  53. CUndoLog();
  54. ~CUndoLog()
  55. {
  56. while (!m_entrylist.IsEmpty())
  57. {
  58. CUndoLogEntry * pEntry = (CUndoLogEntry *)m_entrylist.RemoveHead();
  59. if (pEntry)
  60. delete pEntry;
  61. }
  62. };
  63. //-------------------------------------------------------------------------
  64. // These functions will load the undo log from a file, or save to a file.
  65. // Note - saving to a file will overwrite the contents of the file with
  66. // the contents of the undo log.
  67. //-------------------------------------------------------------------------
  68. BOOL LoadFromFile(LPCTSTR szFilename);
  69. BOOL SaveToFile(LPCTSTR szFilename);
  70. //-------------------------------------------------------------------------
  71. // How many undo entries are in this log?
  72. //-------------------------------------------------------------------------
  73. int GetUndoEntryCount()
  74. {
  75. int iCount = 0;
  76. for (POSITION pos = m_entrylist.GetHeadPosition(); pos != NULL;)
  77. {
  78. CUndoLogEntry * pEntry = (CUndoLogEntry *)m_entrylist.GetNext(pos);
  79. if (pEntry != NULL && !pEntry->m_fFinal)
  80. iCount += 1;
  81. }
  82. return iCount;
  83. }
  84. //-------------------------------------------------------------------------
  85. // Get information about a specific entry (returns FALSE for bad index).
  86. //-------------------------------------------------------------------------
  87. BOOL GetUndoEntryInfo(int iIndex, CString & strDescription, COleDateTime & timestamp)
  88. {
  89. CUndoLogEntry * pEntry = GetEntryByIndex(iIndex);
  90. if (pEntry != NULL)
  91. {
  92. strDescription = pEntry->m_strDescription;
  93. timestamp = pEntry->m_timestamp;
  94. return TRUE;
  95. }
  96. return FALSE;
  97. }
  98. //-------------------------------------------------------------------------
  99. // Get the entry data (to pass to a tab to undo). FALSE for bad index.
  100. //-------------------------------------------------------------------------
  101. BOOL GetUndoEntry(int iIndex, CString & strTab, CString & strEntry)
  102. {
  103. CUndoLogEntry * pEntry = GetEntryByIndex(iIndex);
  104. if (pEntry != NULL)
  105. {
  106. strTab = pEntry->m_strTab;
  107. strEntry = pEntry->m_strEntry;
  108. return TRUE;
  109. }
  110. return FALSE;
  111. }
  112. //-------------------------------------------------------------------------
  113. // Mark an entry as final (stays in the file, but marked so it won't
  114. // appear in the undo log). FALSE for bad index.
  115. //-------------------------------------------------------------------------
  116. BOOL MarkUndoEntryFinal(int iIndex)
  117. {
  118. CUndoLogEntry * pEntry = GetEntryByIndex(iIndex);
  119. if (pEntry != NULL)
  120. {
  121. pEntry->m_fFinal = TRUE;
  122. return TRUE;
  123. }
  124. return FALSE;
  125. }
  126. //-------------------------------------------------------------------------
  127. // Delete all entries in the log that are older than
  128. // timestampOlderThanThis. The entries will be gone, purged from the file.
  129. //-------------------------------------------------------------------------
  130. BOOL DeleteOldUndoEntries(const COleDateTime & timestampOlderThanThis)
  131. {
  132. }
  133. //-------------------------------------------------------------------------
  134. // Create a new undo entry, using the current time, and add it to the
  135. // end of the undo log. Shouldn't return FALSE unless there is no memory.
  136. //-------------------------------------------------------------------------
  137. BOOL AddUndoEntry(const CString & strTab, const CString & strDescription, const CString & strEntry)
  138. {
  139. CUndoLogEntry * pEntry = new CUndoLogEntry(strTab, strDescription, strEntry, FALSE);
  140. if (pEntry == NULL)
  141. return FALSE;
  142. m_entrylist.AddTail((void *)pEntry);
  143. return TRUE;
  144. }
  145. private:
  146. CUndoLogEntry * GetEntryByIndex(int iIndex)
  147. {
  148. CUndoLogEntry * pEntry = NULL;
  149. POSITION pos = m_entrylist.GetHeadPosition();
  150. do
  151. {
  152. if (pos == NULL)
  153. return NULL;
  154. pEntry = (CUndoLogEntry *)m_entrylist.GetNext(pos);
  155. if (pEntry != NULL && !pEntry->m_fFinal)
  156. iIndex -= 1;
  157. } while (iIndex >= 0);
  158. return pEntry;
  159. }
  160. private:
  161. //-------------------------------------------------------------------------
  162. // Member variables.
  163. //-------------------------------------------------------------------------
  164. CPtrList m_entrylist; // list of CUndoLogEntry pointers
  165. };