File: admin.cs

package info (click to toggle)
mono-tools 4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 13,868 kB
  • sloc: cs: 100,909; sh: 3,845; makefile: 2,040; xml: 126
file content (494 lines) | stat: -rw-r--r-- 12,693 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//
// admin.cs: Mono collaborative documentation adminsitration tool.
//
// Author:
//   Miguel de Icaza
//
// (C) 2003 Novell, Inc.
//
using Gtk;
using GtkSharp;
using Glade;
using System;
using System.IO;
using System.Xml;
using System.Collections;
using System.Xml.Serialization;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace Monodoc {
class AdminDriver {
	static int Main (string [] args)
	{
		bool real_server = true;
		
		if (Environment.GetEnvironmentVariable ("MONODOCTESTING") != null)
			real_server = false;
		
		Settings.RunningGUI = true;
		Application.Init ();
		Admin admin = new Admin (real_server);
		Application.Run ();
		return 0;
	}
}



class Admin {
	Glade.XML ui;
	[Widget] Window main_window;
	[Widget] ScrolledWindow container, review_container;
	[Widget] Statusbar statusbar;
	[Widget] Notebook notebook;
	[Widget] TextView text_ondisk;
	[Widget] TextView text_diff;
	[Widget] TextView text_current;
	uint contextid;
	
	HTML html, html_review;
	
	Contributions d;
	string login = SettingsHandler.Settings.Email;
	string pass = SettingsHandler.Settings.Key;
	PendingChange [] changes;
	
	public Admin (bool real_server)
	{
		LoadProviders ();
		ui = new Glade.XML (null, "admin.glade", "main_window", null);
		ui.Autoconnect (this);
		contextid = statusbar.GetContextId ("");
		
		main_window.DeleteEvent += new DeleteEventHandler (OnDeleteEvent);
		d = new Contributions ();
		if (real_server)
			d.Url = "http://www.go-mono.com/docs/server.asmx";
		
		html = new HTML ();
                html.LinkClicked += new LinkClickedHandler (LinkClicked);
		html.Show ();
		html.SetSizeRequest (700, 500);
		container.Add (html);

		html_review = new HTML ();
		html_review.LinkClicked += new LinkClickedHandler (ReviewLinkClicked);
		html_review.Show ();
		review_container.Add (html_review);
	}

	bool Decouple (string prefix, string url, out int id, out int serial)
	{
		if (!url.StartsWith (prefix)){
			id = 0;
			serial = 0;
			return false;
		}

		string rest = url.Substring (prefix.Length);
		int p = rest.IndexOf ('$');
		string sid = rest.Substring (0, p);
		string sserial = rest.Substring (p+1);

		id = int.Parse (sid);
		serial = int.Parse (sserial);
		
		return true;
	}
	
        void LinkClicked (object o, LinkClickedArgs args)
	{
		string url = args.Url;
		int id, serial;

		Console.WriteLine ("Got: " + url);
		if (Decouple ("review:", url, out id, out serial)){
			RenderReview (id, serial);
			return;
		}

		Console.WriteLine ("Unhandled url: " + url);
	}

	class FileAction {
		public GlobalChangeset globalset;
		public DocSetChangeset docset;
		public FileChangeset fileset;
		
		public FileAction (GlobalChangeset gs, DocSetChangeset ds, FileChangeset fs)
		{
			globalset = gs;
			docset = ds;
			fileset = fs;
		}
	}

	class ItemAction {
		public GlobalChangeset globalset;
		public DocSetChangeset docset;
		public FileChangeset fileset;
		public Change change;
		
		public ItemAction (GlobalChangeset gs, DocSetChangeset ds, FileChangeset fs, Change c)
		{
			globalset = gs;
			docset = ds;
			fileset = fs;
			change = c;
		}
	}
	
	void ApplyFile (FileAction fa)
	{
		XmlDocument d = LoadDocument (fa.docset, fa.fileset.RealFile);

		foreach (Change c in fa.fileset.Changes){
			XmlNode old = d.SelectSingleNode (c.XPath);
			if (old != null)
				old.ParentNode.ReplaceChild (d.ImportNode (c.NewNode, true), old);
			
		}
		SaveDocument (d, fa.docset, fa.fileset);
	}

	void ApplyItem (ItemAction fa)
	{
		XmlDocument d = LoadDocument (fa.docset, fa.fileset.RealFile);

		XmlNode old = d.SelectSingleNode (fa.change.XPath);
		if (old != null)
			old.ParentNode.ReplaceChild (d.ImportNode (fa.change.NewNode, true), old);

		SaveDocument (d, fa.docset, fa.fileset);
	}

	static void WriteNode (string file, string str)
	{
		using (FileStream s = File.Create (file)){
			using (StreamWriter sw = new StreamWriter (s)){
				sw.Write (str);
			}
		}
	}
	
	void DiffChangeItem (ItemAction fa)
	{
		XmlDocument d = LoadDocument (fa.docset, fa.fileset.RealFile);

		XmlNode orig = d.SelectSingleNode (fa.change.XPath);
		XmlNode newn = fa.change.NewNode;

		text_ondisk.Buffer.Text = orig.InnerXml;
		text_current.Buffer.Text = newn.InnerXml;

		WriteNode ("/tmp/file-1", orig.InnerXml);
		WriteNode ("/tmp/file-2", newn.InnerXml);
		
		Process diffp = new Process ();
		diffp.StartInfo.FileName = "diff";
		diffp.StartInfo.Arguments = "-uw /tmp/file-1 /tmp/file-2";
		diffp.StartInfo.UseShellExecute = false;
		diffp.StartInfo.RedirectStandardOutput = true;
		diffp.Start ();
		
		text_diff.Buffer.Text = "=" + diffp.StandardOutput.ReadToEnd ();
		diffp.WaitForExit ();
	}
	
	void SaveDocument (XmlDocument d, DocSetChangeset docset, FileChangeset fileset)
	{
		string basedir = (string) providers [docset.DocSet];
		string file = basedir + "/" + fileset.RealFile;
		
		d.Save (file);
		RenderReview (current_id, current_serial);
	}
	
	void ReviewLinkClicked (object o, LinkClickedArgs args)
	{
		string url = args.Url;
		int id, serial;

		if (Decouple ("flag-done:", url, out id, out serial)){
			d.UpdateStatus (login, pass, id, serial, 1);
			notebook.Page = 0;
			return;
		}

		if (url.StartsWith ("apply-file:")){
			string rest = url.Substring (11);
			
			ApplyFile ((FileAction) action_map [Int32.Parse (rest)]);
			return;
		}

		if (url.StartsWith ("apply-change:")){
			string rest = url.Substring (13);
			
			ApplyItem ((ItemAction) action_map [Int32.Parse (rest)]);
			return;
		}

		if (url.StartsWith ("diff-change:")){
			string rest = url.Substring (12);
			DiffChangeItem ((ItemAction) action_map [Int32.Parse (rest)]);
			
			notebook.Page = 2;
		}
		
		Console.WriteLine ("Unhandled url: " + url);
	}
	
	Hashtable cache = new Hashtable ();
	
	GlobalChangeset LoadReview (int id, int serial)
	{
		string key = String.Format ("{0}:{1}", id, serial);
		if (cache [key] != null)
			return (GlobalChangeset) cache [key];

		//
		// Download contribution
		//
		XmlNode n = d.FetchContribution (login, pass, id, serial);

		//
		// Parse into GlobalChangeset
		//
		XmlDocument doc = new XmlDocument ();
		doc.AppendChild (doc.ImportNode (n, true));
		XmlNodeReader r = new XmlNodeReader (doc);
		GlobalChangeset s;
		try {
			s = (GlobalChangeset) GlobalChangeset.serializer.Deserialize (r);
		} catch (Exception e) {
			Console.WriteLine ("Error: " + e);
			Status = "Invalid contribution obtained from server: " + key;
			return null;
		}
		
		cache [key] = s;
		return s;
	}

	Hashtable action_map;
	int current_id, current_serial;

	//
	// Renders the id/serial representation for review by the administrator.
	//
	void RenderReview (int id, int serial)
	{
		current_id = id;
		current_serial = serial;
		
		notebook.Page = 1;

		GlobalChangeset globalset;
		globalset = LoadReview (id, serial);

		HTMLStream s = html_review.Begin ("text/html");
		s.Write ("<html><body>");
		if (globalset == null){
			s.Write ("No data found");
			html_review.End (s, HTMLStreamStatus.Ok);
			return;
		}

		int key = 0;
		action_map = new Hashtable ();
		
		//
		// First make sure we dont have sources that we dont know about,
		// so a contribution can not be flagged as done by accident
		//
		bool allow_flag_as_done = true;
		foreach (DocSetChangeset docset in globalset.DocSetChangesets){
			if (!providers.Contains (docset.DocSet)){
				s.Write (String.Format ("<font color='red'>Warning: Skipping {0}</font>", docset.DocSet));
				allow_flag_as_done = false;
				continue;
			}
		}

		if (allow_flag_as_done)
			s.Write (String.Format ("<h1>Changes: <a href=\"flag-done:{0}${1}\">[Flag as Done]</a></h1>", id, serial));

		foreach (DocSetChangeset docset in globalset.DocSetChangesets){
			if (!providers.Contains (docset.DocSet))
				continue;
			
			if (docset == null){
				s.Write ("Null?");
				continue;
			}
			string ds;

			ds = String.Format ("<table width='100%' bgcolor='#aabbaa'><tr><td>Docset: {0}</td></tr></table>", docset.DocSet);
			foreach (FileChangeset fileset in docset.FileChangesets){
				string fs, es = null;

				fs = String.Format ("<h3><a href=\"apply-file:{0}\">[Apply]</a> File: {1} <br><blockquote>",
						    key, fileset.RealFile);
				
				action_map [key++] = new FileAction (globalset, docset, fileset);

				if (fileset.RealFile == null){
					s.Write (String.Format ("Warning: invalid contribution, its missing filename"));
					continue;
				}
				XmlDocument d = LoadDocument (docset, fileset.RealFile);
				
				foreach (Change c in fileset.Changes){
					XmlNode orig = d.SelectSingleNode (c.XPath);
					XmlNode newn = c.NewNode;

					if (orig == null){
						s.Write (String.Format ("Warning, node {0} does not exist", c.XPath));
						continue;
					}

					if (ds != null) { s.Write (ds); ds = null; }
					if (fs != null) { s.Write (fs); fs = null; es = "</blockquote>"; }

					string original_text = orig.InnerXml;
					string new_text = c.NewNode.InnerXml;

					if (original_text == new_text){
						//s.Write ("<b>Applied</b><br>");
						continue;
					}
					
					int p = c.XPath.LastIndexOf ("/");
					s.Write (String.Format ("<a href=\"diff-change:{0}\">[Diff]</a>", key));
					s.Write (String.Format ("<a href=\"apply-change:{0}\">[Apply]</a>: {1} ", key, c.XPath));
					if (c.FromVersion != RootTree.MonodocVersion)
						s.Write ("<b>FROM OLD VERSION</b>");
					action_map [key++] = new ItemAction (globalset, docset, fileset, c);
					s.Write ("<table border=1 width=100%><tr bgcolor=grey><td width=50%>Current</td><td width=50%>New</td>");

					s.Write ("<tr>");
					s.Write (String.Format ("<td>{0}</td>", Htmlize (original_text)));
					
					s.Write ("<td>");
					s.Write (Htmlize (new_text));
					s.Write ("</td></tr>");
					s.Write ("</table>");
				}
				if (es != null) s.Write (es);
			}
		}
		s.Write ("</body></html>");
		html_review.End (s, HTMLStreamStatus.Ok);
	}

	//
	// Colorizes the ECMA XML documentation into some pretty HTML
	//
	string Htmlize (string s)
	{
		string r = s.Replace ("<", "&lt;").Replace (">", "&gt;</font>").Replace ("&lt;", "<font color='blue'>&lt;").Replace
			("/para&gt;</font>", "para&gt;</font><p>");
		return r;
	}

	//
	// Loads the `file' from a DocSetChangeset
	//
	XmlDocument LoadDocument (DocSetChangeset ds, string file)
	{
		XmlDocument d = new XmlDocument ();
		string basedir = (string) providers [ds.DocSet];
		d.Load (basedir + "/" + file);
		return d;
	}

	void OnDeleteEvent (object o, DeleteEventArgs args)
	{
		Application.Quit ();
	}

	void RenderChanges ()
	{
		notebook.Page = 0;
		HTMLStream s = html.Begin ("text/html");
		if (changes != null){
			s.Write ("<h1>Pending Changes</h1>");
			int i = 0;
			foreach (PendingChange ch in changes){
				s.Write (String.Format ("{3}: <a href=\"review:{0}${1}\">{2}</a><br>", ch.ID, ch.Serial, ch.Login, i++));
			}
		} else {
			s.Write ("<h1>No pending changes on the server</h1>");
		}
		html.End (s, HTMLStreamStatus.Ok);
	}
	
	void OnCheckUpdatesClicked (object o, EventArgs args)
	{
		Status = "Loading";
		try {
			changes = d.GetPendingChanges (login, pass);
			if (changes == null)
				Status = "No changes available";
			else
				Status = "Changes loaded: " + changes.Length + " contributions";
			RenderChanges ();
		} catch (Exception e){
			Status = "There was a failure trying to fetch the status from the server";
			Console.WriteLine (e);
		}
	}

	string Status {
		set {
			statusbar.Pop (contextid);
			statusbar.Push (contextid, value);
		}
	}

	Hashtable providers = new Hashtable ();
	public void LoadProviders ()
	{
		string config_dir = Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData);
		string monodoc_dir = System.IO.Path.Combine (config_dir, "monodoc");
		string settings_file = System.IO.Path.Combine (monodoc_dir, "providers.xml");

		XmlSerializer ser = new XmlSerializer (typeof (Providers));
		Providers p;
		if (File.Exists (settings_file))
			p = (Providers) ser.Deserialize (new XmlTextReader (settings_file));
		else {
			Console.WriteLine ("File {0} does not exist", settings_file);
			Console.WriteLine ("Format is:");
			Console.WriteLine (@"<Providers xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  <Location Name=""netdocs"" Path=""/cvs/monodoc/class""/>
</Providers>
");

			
			Environment.Exit (1);
			return;
		}

		for (int i = 0; i < p.Locations.Length; i++){
			providers [p.Locations [i].Name] = p.Locations [i].Path;
		}
	}
}

///
/// Configuration Loading
///
public class ProviderLocation {
	[XmlAttribute] public string Name;
	[XmlAttribute] public string Path;
}

public class Providers {
	[XmlElement ("Location", typeof (ProviderLocation))]
	public ProviderLocation [] Locations;
}

}