~bratsche/ubuntu/maverick/monodevelop/disable-appmenu

« back to all changes in this revision

Viewing changes to src/core/MonoDevelop.Ide/MonoDevelop.Ide.Gui/HelpViewer.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2009-03-17 17:55:55 UTC
  • mfrom: (1.1.13 sid)
  • Revision ID: james.westby@ubuntu.com-20090317175555-2w5qbmu0l5maq6fq
Tags: 1.9.3+dfsg-1ubuntu1
* FFe for Monodevelop 2 granted by motu-release team :)
* Merge from Debian Unstable , remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// HelpViewer.cs
2
 
//
3
 
// Author:
4
 
//   Todd Berman  <tberman@off.net>
5
 
//
6
 
// Copyright (c) 2007 Todd Berman
7
 
//
8
 
// Permission is hereby granted, free of charge, to any person obtaining a copy
9
 
// of this software and associated documentation files (the "Software"), to deal
10
 
// in the Software without restriction, including without limitation the rights
11
 
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 
// copies of the Software, and to permit persons to whom the Software is
13
 
// furnished to do so, subject to the following conditions:
14
 
//
15
 
// The above copyright notice and this permission notice shall be included in
16
 
// all copies or substantial portions of the Software.
17
 
//
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 FROM,
23
 
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 
// THE SOFTWARE.
25
 
//
26
 
//
27
 
 
28
 
using System;
29
 
using System.IO;
30
 
using System.Text;
31
 
 
32
 
using Gtk;
33
 
using Monodoc;
34
 
 
35
 
using MonoDevelop.Core.Gui;
36
 
using MonoDevelop.Core.Gui.WebBrowser;
37
 
using MonoDevelop.Core;
38
 
using MonoDevelop.Projects.Dom.Parser;
39
 
 
40
 
namespace MonoDevelop.Ide.Gui
41
 
{
42
 
 
43
 
        public class HelpViewer : AbstractViewContent
44
 
        {
45
 
                IWebBrowser html_viewer;
46
 
                Widget widget;
47
 
 
48
 
                public override bool IsViewOnly {
49
 
                        get { return true; }
50
 
                }
51
 
 
52
 
                public override Gtk.Widget Control {
53
 
                        get { return widget; }
54
 
                }
55
 
 
56
 
                public override string ContentName {
57
 
                        get { return GettextCatalog.GetString ("Documentation"); }
58
 
                }
59
 
                
60
 
                public override void Dispose ()
61
 
                {
62
 
                        widget.Destroy ();
63
 
                        base.Dispose ();
64
 
                }
65
 
                
66
 
                public static bool CanLoadHelpViewer {
67
 
                        get { return WebBrowserService.CanGetWebBrowser; }
68
 
                }
69
 
                
70
 
                public HelpViewer ()
71
 
                {
72
 
                        if (WebBrowserService.CanGetWebBrowser) {
73
 
                                html_viewer = WebBrowserService.GetWebBrowser ();
74
 
                                html_viewer.LinkClicked += LinkClicked;
75
 
                                html_viewer.LinkStatusChanged += onLinkMessage;
76
 
                                widget = (Widget) html_viewer;
77
 
                        } else {
78
 
                                Label lab = new Label (GettextCatalog.GetString ("The help viewer could not be loaded, because an embedded web browser is not available."));
79
 
                                widget = lab;
80
 
                        }
81
 
                        Control.ShowAll ();
82
 
                }
83
 
 
84
 
                void onLinkMessage (object sender, StatusMessageChangedEventArgs args)
85
 
                {
86
 
                        IdeApp.Workbench.StatusBar.ShowMessage (args.Message ?? string.Empty);
87
 
                }
88
 
                
89
 
                void LinkClicked (object o, LocationChangingEventArgs args)
90
 
                {
91
 
                        LoadUrl (args.NextLocation);
92
 
                        args.SuppressChange =  true;
93
 
                }
94
 
                
95
 
                public void LoadNode (string nodeText, Node node, string url)
96
 
                {
97
 
                        if (html_viewer == null)
98
 
                                return;
99
 
                        try {
100
 
                                string tempFile = buildTempDocDirectory (nodeText);
101
 
                                html_viewer.LoadUrl (string.Format ("file://{0}#{1}", tempFile, string.Empty));
102
 
                        } catch (Exception e) {
103
 
                                LoadErrorHtml (e, url);
104
 
                        }
105
 
                }
106
 
                
107
 
                public void LoadErrorHtml (Exception e, string missingUrl)
108
 
                {
109
 
                        LoggingService.LogError ("Could not load help url {0}\n{1}", missingUrl, e.ToString ());
110
 
                        html_viewer.LoadHtml (GettextCatalog.GetString("{0}Error: the help topic '{2}' could not be loaded.{1}",
111
 
                                                                       "<html><body><h1>",
112
 
                                                                       "</h1></body></html>",
113
 
                                                                       missingUrl));
114
 
                }
115
 
 
116
 
                public void LoadUrl (string url)
117
 
                {
118
 
                        if (html_viewer == null || url.StartsWith("#"))
119
 
                                return;
120
 
 
121
 
                        if (ProjectDomService.HelpTree != null) {
122
 
                                Node node;
123
 
                                string res = ProjectDomService.HelpTree.RenderUrl (url, out node);
124
 
                                LoadNode (res, node, url);
125
 
                        }
126
 
                }
127
 
                
128
 
                string buildTempDocDirectory (string fileText)
129
 
                {
130
 
                        if (fileText == null)
131
 
                                throw new ArgumentNullException ("fileText");
132
 
                        
133
 
                        string tempDir = Path.GetTempFileName ();
134
 
                        File.Delete (tempDir);
135
 
                        Directory.CreateDirectory (tempDir);
136
 
                        
137
 
                        StringBuilder builder = new StringBuilder ();
138
 
                        builder.Append ("<html><body>");
139
 
                        ProcessImages (fileText, builder, tempDir);
140
 
                        builder.Append ("</body></html>");
141
 
                        
142
 
                        using (StreamWriter writer = File.CreateText (Path.Combine (tempDir, "index.html"))) {
143
 
                                writer.Write (builder.ToString ());
144
 
                        }
145
 
                        return Path.Combine (tempDir, "index.html"); //FIXME: and the anchor too
146
 
                }
147
 
                
148
 
                void ProcessImages (string input, StringBuilder output, string tempDir)
149
 
                {
150
 
                        int imageIndex = 0;
151
 
                        int pos = 0;
152
 
                        bool inCDATA = false;
153
 
                        bool inComment = false;
154
 
                        int previousCaptureEndedAt = 0;
155
 
                        
156
 
                        while (pos < (input.Length - 9)) {
157
 
                                //escape from comments
158
 
                                if (inComment) {
159
 
                                        while (pos < input.Length - 3) {
160
 
                                                if (input[pos] == '-' && input[pos+1] == '-' && input [pos+2] == '>') {
161
 
                                                        pos += 2;
162
 
                                                        break;
163
 
                                                }
164
 
                                                pos++;
165
 
                                        }
166
 
                                        continue;
167
 
                                }
168
 
                                //escape from CDATA
169
 
                                else if (inCDATA) {
170
 
                                        while (pos < input.Length - 3) {
171
 
                                                if (input[pos] == ']' && input[pos+1] == ']' && input [pos+2] == '>') {
172
 
                                                        pos += 2;
173
 
                                                        break;
174
 
                                                }
175
 
                                                pos++;
176
 
                                        }
177
 
                                        continue;
178
 
                                }
179
 
                                //HTML/XML/SGML tags
180
 
                                else if (input[pos] == '<') {
181
 
                                        //enter CDATA or comment
182
 
                                        if (input[pos+1] == '!') {
183
 
                                                if (input[pos+2] == '-' && input [pos+3] == '-') {
184
 
                                                        inComment = true;
185
 
                                                        pos += 4;
186
 
                                                        continue;
187
 
                                                } else if (input.Substring (pos+2, 7) ==  "[CDATA[") {                                          
188
 
                                                        pos += 9;
189
 
                                                        continue;
190
 
                                                }
191
 
                                        }
192
 
                                        //IMG tags; what we're looking for!
193
 
                                        else if (input[pos+1] == 'i' && input [pos+2] == 'm' && input [pos+3] == 'm' ){
194
 
                                                //WriteImage
195
 
                                                //string url = "";
196
 
                                                //string path = Path.Combine (tempDir, "image" + imageIndex);
197
 
                                                //WriteImage (path, url);
198
 
                                                imageIndex++;
199
 
                                                System.Console.WriteLine(input.Substring (pos, 10));
200
 
                                        }
201
 
                                }
202
 
                                pos++;
203
 
                        }
204
 
                        output.Append (input.Substring (previousCaptureEndedAt));
205
 
                }
206
 
                
207
 
                void WriteImage (string path, string url)
208
 
                {
209
 
                        using (Stream s = ProjectDomService.HelpTree.GetImage (url)) {
210
 
                                using (FileStream fs = new FileStream (path, FileMode.Create)) {
211
 
                                        byte[] buffer = new byte [8192];
212
 
                                        int n = 0;
213
 
                                        while ((n = s.Read (buffer, 0, 8192)) != 0)
214
 
                                                fs.Write (buffer, 0, n);
215
 
                                }
216
 
                        }
217
 
                }
218
 
 
219
 
                public override void Load (string s)
220
 
                {
221
 
                }
222
 
 
223
 
        }
224
 
 
225
 
}