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 }---------------------------------------------------
45 static const iff::fmt_t fmt_TodoList
= IFF_SFMT("TODO");
46 static const iff::fmt_t fmt_Item
= IFF_SFMT("TITM");
47 static const iff::fmt_t fmt_Dep
= IFF_SFMT("TDEP");
49 static const uint32_t c_CurrentVersion
= 1;
51 //----------------------------------------------------------------------
55 inline CTodoHeader (void) : m_Version (c_CurrentVersion
), m_Flags (0) { }
56 inline void read (istream
& is
) { is
>> m_Version
>> m_Flags
; }
57 inline void write (ostream
& os
) const { os
<< m_Version
<< m_Flags
; }
58 inline size_t stream_size (void) const { return (stream_size_of (m_Version
) + stream_size_of (m_Flags
)); }
60 uint32_t m_Version
; ///< File format's version number.
61 bitset
<32> m_Flags
; ///< Various flags. None for now.
64 STD_STREAMABLE (CTodoHeader
)
66 /// Reads the object from stream \p is.
67 void CTodoDocument::read (istream
& is
)
70 iff::ReadChunk (is
, h
, fmt_TodoList
);
71 iff::ReadVector (is
, m_Todos
, fmt_Item
);
72 iff::ReadVector (is
, m_Deps
, fmt_Dep
);
76 /// Writes the object to stream \p os.
77 void CTodoDocument::write (ostream
& os
) const
80 iff::WriteChunk (os
, h
, fmt_TodoList
);
81 iff::WriteVector (os
, m_Todos
, fmt_Item
);
82 iff::WriteVector (os
, m_Deps
, fmt_Dep
);
85 /// Returns the size of the written object.
86 size_t CTodoDocument::stream_size (void) const
88 return (iff::chunk_size_of (CTodoHeader()) +
89 iff::vector_size_of (m_Todos
) +
90 iff::vector_size_of (m_Deps
));
93 /// Opens \p filename and reads todo entries from it.
94 void CTodoDocument::Open (const string
& filename
)
96 CDocument::Open (filename
);
97 if (access (filename
, F_OK
)) // Check if it's a new document.
99 if (access (filename
, W_OK
)) // Check if it is read-only.
100 SetFlag (f_ReadOnly
);
102 buf
.read_file (filename
);
105 SetFlag (f_Changed
, false);
109 /// Saves the data to the currently open file.
110 void CTodoDocument::Save (void)
112 if (Flag (f_ReadOnly
) && access (Filename(), W_OK
)) {
113 MessageBox ("Can't save: this file is marked as read-only");
116 memblock
buf (stream_size_of (*this));
119 buf
.write_file (Filename());
120 SetFlag (f_Changed
, false);
124 /// Verifies and corrects any defects in the data.
125 void CTodoDocument::VerifyData (void)
127 foreach (tddepmap_t::iterator
, i
, m_Deps
) {
128 if (!FindItem (i
->m_ItemId
) || !FindItem (i
->m_DepId
)) {
129 assert (!"Found a dependency pointing to a nonexistent item!");
130 --(i
= m_Deps
.erase (i
));
135 /// Returns the true if \p i1 ought to appear before \p i2 in the visible list.
136 bool CTodoDocument::VisibleOrderLess (const CTodoDep
& d1
, const CTodoDep
& d2
) const
138 const icitem_t
i1 (FindItem (d1
.m_DepId
)), i2 (FindItem (d2
.m_DepId
));
139 // Sort to put completed items at the end, then sort by priority, and then by creation date.
140 return (i1
->Complete() < i2
->Complete() ||
141 (!i1
->Complete() && !i2
->Complete() && (i1
->Priority() < i2
->Priority() ||
142 (i1
->Priority() == i2
->Priority() && *i1
< *i2
))) ||
143 (i1
->Complete() && i2
->Complete() && (i1
->Done() > i2
->Done() ||
144 (i1
->Done() == i2
->Done() && *i1
< *i2
))));
147 /// Reorders the items in the list by canonical sort order.
148 void CTodoDocument::ResortItemDeps (idep_t first
, idep_t last
) const
150 for (idep_t j
, i
= first
; ++i
< last
;) {
151 for (j
= i
; j
-- > first
&& !VisibleOrderLess (*j
, *i
););
152 rotate (++j
, i
, i
+ 1);
156 //--{ Item progress recursive updating }--------------------------------
158 /// Creates a new item and returns its id.
159 CTodoDocument::itemid_t
CTodoDocument::CreateItem (void)
161 CTodoItem
e (GetNextItemId());
162 assert (!FindItem (e
.Id()));
163 // To the complete list
169 /// Makes item \p id a dependency of \p parent item.
170 void CTodoDocument::LinkItem (itemid_t id
, itemid_t parent
)
172 iitem_t iItem
= FindItem (id
), iParent
= FindItem (parent
);
173 if (!iParent
| !iItem
)
175 // m_Deps is sorted by parent, but not by < (because < requires accessing m_Todos)
176 idep_t ip
= upper_bound (m_Deps
, CTodoDep (parent
));
177 m_Deps
.insert (ip
, CTodoDep (parent
, id
));
178 // Update the new item's status.
180 iParent
->SetHasSublist (true);
181 UpdateCompleteStatus (id
);
187 /// Removes one reference the given item.
188 void CTodoDocument::UnlinkItem (icdep_t id
)
190 assert (id
>= m_Deps
.begin() && id
< m_Deps
.end() && "UnlinkItem takes an iterator from the vector set by ItemDeps");
191 iitem_t ii
= FindItem (id
->m_DepId
); // Cache master list iterator while we still have the item to look for.
192 // Remove from dependency list.
193 m_Deps
.erase (const_cast<idep_t
>(id
));
194 pair
<idep_t
, idep_t
> r
= equal_range (m_Deps
, CTodoDep(ii
->Id()));
195 if (r
.first
== r
.second
) // Can't be cleanly done in UpdateItemProgress.
196 FindItem (id
->m_ItemId
)->SetHasSublist (false);
197 // Update the item in the master list.
198 if (!ii
->DelRef()) // If no more refs, then delete.
200 UpdateItemProgress (r
.first
, r
.second
);
201 // Update visible list.
206 /// Updates item \p v in the master list.
207 void CTodoDocument::UpdateItem (rcitem_t v
)
209 iitem_t i
= FindItem (v
.Id());
210 assert (i
&& "Update item must only be called with already existent items. Call AppendItem to create new ones.");
212 UpdateCompleteStatus (v
.Id());
218 /// Updates the progress of the item given by dependency range [first, last)
219 void CTodoDocument::UpdateItemProgress (idep_t first
, idep_t last
)
221 const uint32_t nItems
= distance (first
, last
);
224 ResortItemDeps (first
, last
);
225 const itemid_t
rangeId (first
->m_ItemId
);
226 uint32_t progress
= 0;
227 for (; first
< last
; ++first
)
228 progress
+= FindItem(first
->m_DepId
)->Progress();
229 // +1 treats the parent item as part of the list.
230 progress
= min (progress
/ (nItems
+ 1), 100U);
231 iitem_t ii
= FindItem (rangeId
);
232 if (!ii
|| ii
->Progress() == progress
)
234 ii
->MarkComplete (progress
);
235 UpdateCompleteStatus (rangeId
); // Recurse to propagate the change up.
238 /// Updates progress values of item dependent on \p dep
239 void CTodoDocument::UpdateCompleteStatus (itemid_t dep
)
241 // Each item has a range of dependencies; ranges are sequential in m_Deps.
242 idep_t rangeStart
= m_Deps
.begin(); // The start of the current range.
243 const idep_t iEnd
= m_Deps
.end();
244 bool hasDep
= false; // Does current range have dep?
245 for (idep_t i
= rangeStart
; i
< iEnd
; ++i
) {
246 if (i
->m_ItemId
!= rangeStart
->m_ItemId
) {
248 UpdateItemProgress (rangeStart
, i
);
252 hasDep
= hasDep
|| i
->m_DepId
== dep
;
255 UpdateItemProgress (rangeStart
, iEnd
);