Added FilterDeb from Kevin Kubasik.
[beagle.git] / Filters / FilterKonqHistory.cs
blob36c6cbb688571d11a954281d79e1107e0c38ecbc
1 //
2 // FilterKonqHistory.cs
3 //
4 // Copyright (C) 2005 Debajyoti Bera <dbera.web@gmail.com>
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
28 using System;
29 using System.Collections;
30 using System.IO;
31 using System.Text;
33 using Beagle.Daemon;
34 using Beagle.Util;
36 using HtmlAgilityPack;
38 namespace Beagle.Filters {
40 public class FilterKonqHistory : Beagle.Filters.FilterHtml {
42 // use a static buffer to prevent constant alloc and de-alloc
43 private static byte[] buf = null;
45 public FilterKonqHistory ()
47 RegisterSupportedTypes ();
50 override protected void DoOpen (FileInfo info)
52 if (buf == null)
53 buf = new byte [1024];
55 StreamReader reader = new StreamReader (Stream, Encoding.GetEncoding (28591));
57 // read the charset hint from indexable
58 string charset = null;
59 foreach (Property property in IndexableProperties) {
60 if (property.Key != (StringFu.UnindexedNamespace + "charset"))
61 continue;
62 charset = (string) property.Value;
63 //Console.WriteLine ("charset hint accepted: " + charset);
64 break;
68 // now create a memorystream where htmlfilter will begin his show
69 Stream.Seek (0, SeekOrigin.Begin);
70 // count past 8 lines ... Streams suck!
71 int c = 0; // stores the number of newlines read
72 int b = 0;
73 while (c < 8 && (b = Stream.ReadByte ()) != -1) {
74 if (b == '\n')
75 c ++;
77 // copy the rest of the file to a memory stream
78 MemoryStream mem_stream = new MemoryStream ();
79 while ((b = Stream.Read (buf, 0, 1024)) != 0)
80 mem_stream.Write (buf, 0, b);
81 mem_stream.Seek (0, SeekOrigin.Begin);
82 reader.Close ();
84 HtmlDocument doc = new HtmlDocument ();
85 doc.ReportNode += HandleNodeEvent;
86 doc.StreamMode = true;
87 // we already determined encoding
88 doc.OptionReadEncoding = false;
89 Encoding enc = Encoding.UTF8;
90 if (charset != null && charset != String.Empty)
91 enc = Encoding.GetEncoding (charset);
93 try {
94 if (enc == null)
95 doc.Load (mem_stream);
96 else
97 doc.Load (mem_stream, enc);
98 } catch (NotSupportedException e) {
99 doc.Load (mem_stream, Encoding.ASCII);
100 } catch (Exception e) {
101 Console.WriteLine (e.Message);
102 Console.WriteLine (e.StackTrace);
105 Finished ();
108 override protected void RegisterSupportedTypes ()
110 AddSupportedFlavor (FilterFlavor.NewFromMimeType (KonqHistoryUtil.KonqCacheMimeType));