change explorer quick launch icon to use default 'my computer' icon instead of a...
[reactos.git] / irc / TechBot / CHMLibrary / TOCItem.cs
1 using System;
2 using System.IO;
3 using System.Collections;
4 using System.Windows.Forms;
5
6 using HtmlHelp.ChmDecoding;
7
8 namespace HtmlHelp
9 {
10 /// <summary>
11 /// The class <c>TOCItem</c> implements a toc-entry item
12 /// </summary>
13 public sealed class TOCItem
14 {
15 /// <summary>
16 /// Constant for standard folder (closed) image index (HH2 image list)
17 /// </summary>
18 public const int STD_FOLDER_HH2 = 4;
19 /// <summary>
20 /// Constant for standard folder (opened) image index (HH2 image list)
21 /// </summary>
22 public const int STD_FOLDER_OPEN_HH2 = 6;
23 /// <summary>
24 /// Constant for standard file image index (HH2 image list)
25 /// </summary>
26 public const int STD_FILE_HH2 = 16;
27
28 /// <summary>
29 /// Constant for standard folder (closed) image index (HH1 image list)
30 /// </summary>
31 public const int STD_FOLDER_HH1 = 0;
32 /// <summary>
33 /// Constant for standard folder (opened) image index (HH1 image list)
34 /// </summary>
35 public const int STD_FOLDER_OPEN_HH1 = 1;
36 /// <summary>
37 /// Constant for standard file image index (HH1 image list)
38 /// </summary>
39 public const int STD_FILE_HH1 = 10;
40
41 /// <summary>
42 /// Internal flag specifying the data extraction mode used for this item
43 /// </summary>
44 private DataMode _tocMode = DataMode.TextBased;
45 /// <summary>
46 /// Internal member storing the offset (only used in binary tocs)
47 /// </summary>
48 private int _offset = 0;
49 /// <summary>
50 /// Internal member storing the offset of the next item(only used in binary tocs)
51 /// </summary>
52 private int _offsetNext = 0;
53 /// <summary>
54 /// Internal member storing a merge link.
55 /// If the target file is in the merged files list of the CHM,
56 /// this item will be replaced with the target TOC or Topic, if not it will
57 /// be removed from TOC.
58 /// </summary>
59 private string _mergeLink = "";
60 /// <summary>
61 /// Internal member storing the toc name
62 /// </summary>
63 private string _name = "";
64 /// <summary>
65 /// Internal member storing the toc loca (content file)
66 /// </summary>
67 private string _local = "";
68 /// <summary>
69 /// Internal member storing all associated information type strings
70 /// </summary>
71 private ArrayList _infoTypeStrings = new ArrayList();
72 /// <summary>
73 /// Internal member storing the associated chm file
74 /// </summary>
75 private string _chmFile = "";
76 /// <summary>
77 /// Internal member storing the image index
78 /// </summary>
79 private int _imageIndex = -1;
80 /// <summary>
81 /// Internal member storing the offset of the associated topic entry (for binary tocs)
82 /// </summary>
83 private int _topicOffset = -1;
84 /// <summary>
85 /// Internal member storing the toc children
86 /// </summary>
87 private ArrayList _children = new ArrayList();
88 /// <summary>
89 /// Internal member storing the parameter collection
90 /// </summary>
91 private Hashtable _otherParams = new Hashtable();
92 /// <summary>
93 /// Internal member storing the associated chmfile object
94 /// </summary>
95 private CHMFile _associatedFile = null;
96 /// <summary>
97 /// Parent item
98 /// </summary>
99 private TOCItem _parent=null;
100
101 /// <summary>
102 /// Holds a pointer to the next item in the TOC
103 /// </summary>
104 public TOCItem Next=null;
105
106 /// <summary>
107 /// Holds a pointer to the previous item in the TOC
108 /// </summary>
109 public TOCItem Prev=null;
110
111 /// <summary>
112 /// Holds a pointer to the TreeNode where this TOC Item is used
113 /// </summary>
114 public System.Windows.Forms.TreeNode treeNode=null;
115
116 /// <summary>
117 /// Constructor of the class used during text-based data extraction
118 /// </summary>
119 /// <param name="name">name of the item</param>
120 /// <param name="local">local content file</param>
121 /// <param name="ImageIndex">image index</param>
122 /// <param name="chmFile">associated chm file</param>
123 public TOCItem(string name, string local, int ImageIndex, string chmFile)
124 {
125 _tocMode = DataMode.TextBased;
126 _name = name;
127 _local = local;
128 _imageIndex = ImageIndex;
129 _chmFile = chmFile;
130 }
131
132 /// <summary>
133 /// Constructor of the class used during binary data extraction
134 /// </summary>
135 /// <param name="topicOffset">offset of the associated topic entry</param>
136 /// <param name="ImageIndex">image index to use</param>
137 /// <param name="associatedFile">associated chm file</param>
138 public TOCItem(int topicOffset, int ImageIndex, CHMFile associatedFile)
139 {
140 _tocMode = DataMode.Binary;
141 _associatedFile = associatedFile;
142 _chmFile = associatedFile.ChmFilePath;
143 _topicOffset = topicOffset;
144 _imageIndex = ImageIndex;
145 }
146
147 /// <summary>
148 /// Standard constructor
149 /// </summary>
150 public TOCItem()
151 {
152 }
153
154 #region Data dumping
155 /// <summary>
156 /// Dump the class data to a binary writer
157 /// </summary>
158 /// <param name="writer">writer to write the data</param>
159 /// <param name="writeFilename">true if the chmfile name should be written</param>
160 internal void Dump(ref BinaryWriter writer, bool writeFilename)
161 {
162 writer.Write((int)_tocMode);
163 writer.Write(_topicOffset);
164 writer.Write(_name);
165
166 if((_tocMode == DataMode.TextBased)||(_topicOffset<0))
167 {
168 writer.Write(_local);
169 }
170
171 writer.Write(_imageIndex);
172
173 writer.Write(_mergeLink);
174
175 if(writeFilename)
176 writer.Write(_chmFile);
177
178 writer.Write(_infoTypeStrings.Count);
179
180 for(int i=0; i<_infoTypeStrings.Count; i++)
181 writer.Write( (_infoTypeStrings[i]).ToString() );
182
183 writer.Write(_children.Count);
184
185 for(int i=0; i<_children.Count; i++)
186 {
187 TOCItem child = ((TOCItem)(_children[i]));
188 child.Dump(ref writer, writeFilename);
189 }
190 }
191
192 /// <summary>
193 /// Dump the class data to a binary writer
194 /// </summary>
195 /// <param name="writer">writer to write the data</param>
196 internal void Dump(ref BinaryWriter writer)
197 {
198 Dump(ref writer, false);
199 }
200
201 /// <summary>
202 /// Reads the object data from a dump store
203 /// </summary>
204 /// <param name="reader">reader to read the data</param>
205 /// <param name="readFilename">true if the chmfile name should be read</param>
206 internal void ReadDump(ref BinaryReader reader, bool readFilename)
207 {
208 int i=0;
209 _tocMode = (DataMode)reader.ReadInt32();
210 _topicOffset = reader.ReadInt32();
211 _name = reader.ReadString();
212
213 if((_tocMode == DataMode.TextBased)||(_topicOffset<0))
214 {
215 _local = reader.ReadString();
216 }
217
218 _imageIndex = reader.ReadInt32();
219
220 _mergeLink = reader.ReadString();
221
222 if(readFilename)
223 _chmFile = reader.ReadString();
224
225 int nCnt = reader.ReadInt32();
226
227 for(i=0; i<nCnt; i++)
228 {
229 string sIT = reader.ReadString();
230 _infoTypeStrings.Add(sIT);
231 }
232
233 nCnt = reader.ReadInt32();
234
235 if(_associatedFile != null)
236 _chmFile = _associatedFile.ChmFilePath;
237
238 for(i=0; i<nCnt; i++)
239 {
240 TOCItem child = new TOCItem();
241 child.AssociatedFile = _associatedFile;
242
243 child.ReadDump(ref reader, readFilename);
244
245 if(_associatedFile != null)
246 child.ChmFile = _associatedFile.ChmFilePath;
247 else if(!readFilename)
248 child.ChmFile = _chmFile;
249 child.Parent = this;
250
251 _children.Add(child);
252
253 if(child.MergeLink.Length > 0)
254 _associatedFile.MergLinks.Add(child);
255 }
256 }
257
258 /// <summary>
259 /// Reads the object data from a dump store
260 /// </summary>
261 /// <param name="reader">reader to read the data</param>
262 internal void ReadDump(ref BinaryReader reader)
263 {
264 ReadDump(ref reader, false);
265 }
266 #endregion
267
268 /// <summary>
269 /// Gets/Sets the data extraction mode with which this item was created.
270 /// </summary>
271 internal DataMode TocMode
272 {
273 get { return _tocMode; }
274 set { _tocMode = value; }
275 }
276
277 /// <summary>
278 /// Gets/Sets the offset of the associated topic entry
279 /// </summary>
280 internal int TopicOffset
281 {
282 get { return _topicOffset; }
283 set { _topicOffset = value; }
284 }
285
286 /// <summary>
287 /// Gets/Sets the associated CHMFile instance
288 /// </summary>
289 internal CHMFile AssociatedFile
290 {
291 get { return _associatedFile; }
292 set
293 {
294 _associatedFile = value;
295 }
296 }
297
298 /// <summary>
299 /// Gets/Sets the offset of the item.
300 /// </summary>
301 /// <remarks>Only used in binary tocs</remarks>
302 internal int Offset
303 {
304 get { return _offset; }
305 set { _offset = value; }
306 }
307
308 /// <summary>
309 /// Gets/Sets the offset of the next item.
310 /// </summary>
311 /// <remarks>Only used in binary tocs</remarks>
312 internal int OffsetNext
313 {
314 get { return _offsetNext; }
315 set { _offsetNext = value; }
316 }
317
318 /// <summary>
319 /// Gets the ArrayList which holds all information types/categories this item is associated
320 /// </summary>
321 internal ArrayList InfoTypeStrings
322 {
323 get { return _infoTypeStrings; }
324 }
325
326 /// <summary>
327 /// Gets/Sets the parent of this item
328 /// </summary>
329 public TOCItem Parent
330 {
331 get { return _parent; }
332 set { _parent = value; }
333 }
334
335 /// <summary>
336 /// Gets/Sets the mergelink for this item.
337 /// <b>You should not set the mergedlink by your own !</b>
338 /// This is only for loading merged CHMs.
339 /// </summary>
340 public string MergeLink
341 {
342 get { return _mergeLink; }
343 set { _mergeLink = value; }
344 }
345
346 /// <summary>
347 /// Gets/Sets the name of the item
348 /// </summary>
349 public string Name
350 {
351 get
352 {
353 if(_mergeLink.Length > 0)
354 return "";
355
356 if(_name.Length <= 0)
357 {
358 if((_tocMode == DataMode.Binary )&&(_associatedFile!=null))
359 {
360 if( _topicOffset >= 0)
361 {
362 TopicEntry te = (TopicEntry) (_associatedFile.TopicsFile[_topicOffset]);
363 if(te != null)
364 {
365 return te.Title;
366 }
367 }
368 }
369 }
370
371 return _name;
372 }
373 set
374 {
375 _name = value;
376 }
377 }
378
379 /// <summary>
380 /// Gets/Sets the local of the item
381 /// </summary>
382 public string Local
383 {
384 get
385 {
386 if(_mergeLink.Length > 0)
387 return "";
388
389 if(_local.Length <= 0)
390 {
391 if((_tocMode == DataMode.Binary )&&(_associatedFile!=null))
392 {
393 if( _topicOffset >= 0)
394 {
395 TopicEntry te = (TopicEntry) (_associatedFile.TopicsFile[_topicOffset]);
396 if(te != null)
397 {
398 return te.Locale;
399 }
400 }
401 }
402 }
403
404 return _local;
405 }
406 set { _local = value; }
407 }
408
409 /// <summary>
410 /// Gets/Sets the chm file
411 /// </summary>
412 public string ChmFile
413 {
414 get
415 {
416 if(_associatedFile!=null)
417 return _associatedFile.ChmFilePath;
418
419 return _chmFile;
420 }
421 set { _chmFile = value; }
422 }
423
424 /// <summary>
425 /// Gets the url for the webbrowser for this file
426 /// </summary>
427 public string Url
428 {
429 get
430 {
431 string sL = Local;
432
433 if( (sL.ToLower().IndexOf("http://") >= 0) ||
434 (sL.ToLower().IndexOf("https://") >= 0) ||
435 (sL.ToLower().IndexOf("mailto:") >= 0) ||
436 (sL.ToLower().IndexOf("ftp://") >= 0) ||
437 (sL.ToLower().IndexOf("ms-its:") >= 0))
438 return sL;
439
440 return HtmlHelpSystem.UrlPrefix + ChmFile + "::/" + sL;
441 }
442 }
443
444 /// <summary>
445 /// Gets/Sets the image index of the item
446 /// </summary>
447 /// <remarks>Set this to -1 for a default icon</remarks>
448 public int ImageIndex
449 {
450 get
451 {
452 if( _imageIndex == -1)
453 {
454 int nFolderAdd = 0;
455
456 if((_associatedFile != null) && (_associatedFile.ImageTypeFolder))
457 {
458 // get the value which should be added, to display folders instead of books
459 if(HtmlHelpSystem.UseHH2TreePics)
460 nFolderAdd = 8;
461 else
462 nFolderAdd = 4;
463 }
464
465
466 if( _children.Count > 0)
467 return (HtmlHelpSystem.UseHH2TreePics ? (STD_FOLDER_HH2+nFolderAdd) : (STD_FOLDER_HH1+nFolderAdd));
468
469 return (HtmlHelpSystem.UseHH2TreePics ? STD_FILE_HH2 : STD_FILE_HH1);
470 }
471 return _imageIndex;
472 }
473 set { _imageIndex = value; }
474 }
475
476 /// <summary>
477 /// Gets/Sets the children of this item.
478 /// </summary>
479 /// <remarks>Each entry in the ArrayList is of type TOCItem</remarks>
480 public ArrayList Children
481 {
482 get { return _children; }
483 set { _children = value; }
484 }
485
486 /// <summary>
487 /// Gets the internal hashtable storing all params
488 /// </summary>
489 public Hashtable Params
490 {
491 get { return _otherParams; }
492 }
493 }
494 }