1 // Copyright (c) 2006 by Mike Sharov <msharov@users.sourceforge.net>
10 //----------------------------------------------------------------------
12 /// Default constructor.
13 CTodoDocument::CTodoDocument (void)
18 m_Todos
.insert (CTodoItem());
21 //--{ Internal data accessors }-----------------------------------------
23 /// Looks up item \p id in m_Todos, returns NULL if not found.
24 CTodoDocument::icitem_t
CTodoDocument::FindItem (itemid_t id
) const
26 icitem_t i
= m_Todos
.find (id
);
27 return (i
== m_Todos
.end() ? NULL
: i
);
30 /// Links \p m to the dependency list for \p id.
31 void CTodoDocument::ItemDeps (itemid_t id
, tddepmap_t
& m
) const
33 pair
<icdep_t
, icdep_t
> dr
= equal_range (m_Deps
, CTodoDep(id
));
34 m
.link (dr
.first
, dr
.second
);
37 /// Returns the next available item id.
38 inline CTodoDocument::itemid_t
CTodoDocument::GetNextItemId (void) const
40 return (m_Todos
.back().Id() + 1);
43 //--{ Serialization }---------------------------------------------------
46 fmt_TodoList
= IFF_FMT('T','O','D','O'),
47 fmt_Options
= IFF_FMT('O','P','T','S'),
48 fmt_Item
= IFF_FMT('T','I','T','M'),
49 fmt_Dep
= IFF_FMT('T','D','E','P')
52 static const uint32_t c_CurrentVersion
= 1;
54 //----------------------------------------------------------------------
58 inline CTodoHeader (void) : m_Version (c_CurrentVersion
), m_Flags (0), m_Reserved (0) { }
59 inline void read (istream
& is
) { is
>> m_Version
>> m_Flags
>> m_Reserved
; }
60 inline void write (ostream
& os
) const { os
<< m_Version
<< m_Flags
<< m_Reserved
; }
61 inline size_t stream_size (void) const { return (stream_size_of (m_Version
) + stream_size_of (m_Flags
) + stream_size_of(m_Reserved
)); }
63 uint32_t m_Version
; ///< File format's version number.
64 bitset
<32> m_Flags
; ///< Various flags. None for now.
65 uint32_t m_Reserved
; ///< Future functionality.
68 STD_STREAMABLE (CTodoHeader
)
70 /// Reads the object from stream \p is.
71 void CTodoDocument::read (istream
& is
)
73 iff::SkipChunk (is
, fmt_Options
);
74 iff::ReadVector (is
, m_Todos
, fmt_Item
);
75 iff::ReadVector (is
, m_Deps
, fmt_Dep
);
79 /// Writes the object to stream \p os.
80 void CTodoDocument::write (ostream
& os
) const
82 iff::WriteChunk (os
, CTodoHeader(), fmt_Options
);
83 iff::WriteVector (os
, m_Todos
, fmt_Item
);
84 iff::WriteVector (os
, m_Deps
, fmt_Dep
);
87 /// Returns the size of the written object.
88 size_t CTodoDocument::stream_size (void) const
90 return (iff::chunk_size_of (CTodoHeader()) +
91 iff::vector_size_of (m_Todos
) +
92 iff::vector_size_of (m_Deps
));
95 /// Opens \p filename and reads todo entries from it.
96 void CTodoDocument::Open (const string
& filename
)
98 CDocument::Open (filename
);
99 if (access (filename
, F_OK
)) // Check if it's a new document.
101 if (access (filename
, W_OK
)) // Check if it is read-only.
102 SetFlag (f_ReadOnly
);
104 buf
.read_file (filename
);
106 iff::ReadFORM (is
, *this, fmt_TodoList
);
107 SetFlag (f_Changed
, false);
111 /// Saves the data to the currently open file.
112 void CTodoDocument::Save (void)
114 if (Flag (f_ReadOnly
) && access (Filename(), W_OK
)) {
115 MessageBox ("Can't save: this file is marked as read-only");
118 memblock
buf (iff::form_size_of (*this));
120 iff::WriteFORM (os
, *this, fmt_TodoList
);
121 buf
.write_file (Filename());
122 SetFlag (f_Changed
, false);
126 /// Verifies and corrects any defects in the data.
127 void CTodoDocument::VerifyData (void)
129 foreach (tddepmap_t::iterator
, i
, m_Deps
) {
130 if (!FindItem (i
->m_ItemId
) || !FindItem (i
->m_DepId
)) {
131 assert (!"Found a dependency pointing to a nonexistent item!");
132 --(i
= m_Deps
.erase (i
));
137 /// Returns the true if \p i1 ought to appear before \p i2 in the visible list.
138 bool CTodoDocument::VisibleOrderLess (const CTodoDep
& d1
, const CTodoDep
& d2
) const
140 const icitem_t
i1 (FindItem (d1
.m_DepId
)), i2 (FindItem (d2
.m_DepId
));
141 // Sort to put completed items at the end, then sort by priority, and then by creation date.
142 return (i1
->Complete() < i2
->Complete() ||
143 (!i1
->Complete() && !i2
->Complete() && (i1
->Priority() < i2
->Priority() ||
144 (i1
->Priority() == i2
->Priority() && *i1
< *i2
))) ||
145 (i1
->Complete() && i2
->Complete() && (i1
->Done() > i2
->Done() ||
146 (i1
->Done() == i2
->Done() && *i1
< *i2
))));
149 /// Reorders the items in the list by canonical sort order.
150 void CTodoDocument::ResortItemDeps (idep_t first
, idep_t last
) const
152 for (idep_t j
, i
= first
; ++i
< last
;) {
153 for (j
= i
; j
-- > first
&& !VisibleOrderLess (*j
, *i
););
154 rotate (++j
, i
, i
+ 1);
158 //--{ Item progress recursive updating }--------------------------------
160 /// Creates a new item and returns its id.
161 CTodoDocument::itemid_t
CTodoDocument::CreateItem (void)
163 CTodoItem
e (GetNextItemId());
164 assert (!FindItem (e
.Id()));
165 // To the complete list
171 /// Makes item \p id a dependency of \p parent item.
172 void CTodoDocument::LinkItem (itemid_t id
, itemid_t parent
)
174 iitem_t iItem
= FindItem (id
), iParent
= FindItem (parent
);
175 if (!iParent
| !iItem
)
177 // m_Deps is sorted by parent, but not by < (because < requires accessing m_Todos)
178 idep_t ip
= upper_bound (m_Deps
, CTodoDep (parent
));
179 m_Deps
.insert (ip
, CTodoDep (parent
, id
));
180 // Update the new item's status.
182 iParent
->SetHasSublist (true);
183 UpdateCompleteStatus (id
);
189 /// Removes one reference the given item.
190 void CTodoDocument::UnlinkItem (icdep_t id
)
192 assert (id
>= m_Deps
.begin() && id
< m_Deps
.end() && "UnlinkItem takes an iterator from the vector set by ItemDeps");
193 iitem_t ii
= FindItem (id
->m_DepId
); // Cache master list iterator while we still have the item to look for.
194 // Remove from dependency list.
195 m_Deps
.erase (const_cast<idep_t
>(id
));
196 pair
<idep_t
, idep_t
> r
= equal_range (m_Deps
, CTodoDep(ii
->Id()));
197 if (r
.first
== r
.second
) // Can't be cleanly done in UpdateItemProgress.
198 FindItem (id
->m_ItemId
)->SetHasSublist (false);
199 // Update the item in the master list.
200 if (!ii
->DelRef()) // If no more refs, then delete.
202 UpdateItemProgress (r
.first
, r
.second
);
203 // Update visible list.
208 /// Updates item \p v in the master list.
209 void CTodoDocument::UpdateItem (rcitem_t v
)
211 iitem_t i
= FindItem (v
.Id());
212 assert (i
&& "Update item must only be called with already existent items. Call AppendItem to create new ones.");
214 UpdateCompleteStatus (v
.Id());
220 /// Updates the progress of the item given by dependency range [first, last)
221 void CTodoDocument::UpdateItemProgress (idep_t first
, idep_t last
)
223 const uint32_t nItems
= distance (first
, last
);
226 ResortItemDeps (first
, last
);
227 const itemid_t
rangeId (first
->m_ItemId
);
228 uint32_t progress
= 0;
229 for (; first
< last
; ++first
)
230 progress
+= FindItem(first
->m_DepId
)->Progress();
231 // +1 treats the parent item as part of the list.
232 progress
= min (progress
/ (nItems
+ 1), 100U);
233 iitem_t ii
= FindItem (rangeId
);
234 if (!ii
|| ii
->Progress() == progress
)
236 ii
->MarkComplete (progress
);
237 UpdateCompleteStatus (rangeId
); // Recurse to propagate the change up.
240 /// Updates progress values of item dependent on \p dep
241 void CTodoDocument::UpdateCompleteStatus (itemid_t dep
)
243 // Each item has a range of dependencies; ranges are sequential in m_Deps.
244 idep_t rangeStart
= m_Deps
.begin(); // The start of the current range.
245 const idep_t iEnd
= m_Deps
.end();
246 bool hasDep
= false; // Does current range have dep?
247 for (idep_t i
= rangeStart
; i
< iEnd
; ++i
) {
248 if (i
->m_ItemId
!= rangeStart
->m_ItemId
) {
250 UpdateItemProgress (rangeStart
, i
);
254 hasDep
= hasDep
|| i
->m_DepId
== dep
;
257 UpdateItemProgress (rangeStart
, iEnd
);