Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppDelegate.cs - github.com/xamarin/macdoc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1f30dc350066a62d1c373c209560665090245126 (plain)
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
using System;
using System.IO;
using System.Linq;
using CoreGraphics;
using System.Threading;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using Foundation;
using AppKit;
using ObjCRuntime;

using Monodoc;
using System.Text;
using System.Reflection;
using System.Configuration;

namespace macdoc
{
	public partial class AppDelegate : NSApplicationDelegate
	{
		static public RootTree Root;
		static public string MonodocDir;
		static public NSUrl MonodocBaseUrl;
		static MonodocDocumentController controller;
		static bool isOnLion = false;
		
		bool shouldOpenInitialFile = true;
		
		static void PrepareCache ()
		{
			MonodocDir = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.Personal), "Library/Caches/MacDoc/");
			var mdocimages = Path.Combine (MonodocDir, "mdocimages");
			MonodocBaseUrl = new NSUrl (MonodocDir);
			if (!Directory.Exists (mdocimages)){
				try {
					Directory.CreateDirectory (mdocimages);
				} catch {}
			}
		}
		
		static void ExtractImages ()
		{
			var mdocAssembly = typeof (Node).Assembly;
			
			foreach (var res in mdocAssembly.GetManifestResourceNames ()){
				if (!res.EndsWith (".png") || res.EndsWith (".jpg"))
					continue;
				
				var image = Path.Combine (MonodocDir, "mdocimages", res);
				if (File.Exists (image))
					continue;

				try {
					using (var output = File.Create (image))
						mdocAssembly.GetManifestResourceStream (res).CopyTo (output);
				} catch (UnauthorizedAccessException) {}
			}
		}
		
		public AppDelegate ()
		{
			PrepareCache ();
			ExtractImages ();
			controller = new MonodocDocumentController ();
			
			// Some UI feature we use rely on Lion or better, so special case it
			try {
				var version = new NSDictionary ("/System/Library/CoreServices/SystemVersion.plist");
				var osxVersion = Version.Parse (version.ObjectForKey (new NSString ("ProductVersion")).ToString ());
				isOnLion = osxVersion.Major == 10 && osxVersion.Minor >= 7;
			} catch {}
			
			// Load documentation
			var args = Environment.GetCommandLineArgs ();
			IEnumerable<string> extraDocs = null, extraUncompiledDocs = null;
			if (args != null && args.Length > 1) {
				var extraDirs = args.Skip (1);
				extraDocs = extraDirs
					.Where (d => d.StartsWith ("+"))
					.Select (d => d.Substring (1))
					.Where (d => Directory.Exists (d));
				extraUncompiledDocs = extraDirs
					.Where (d => d.StartsWith ("@"))
					.Select (d => d.Substring (1))
					.Where (d => Directory.Exists (d));
			}

			if (extraUncompiledDocs != null)
				foreach (var dir in extraUncompiledDocs)
					RootTree.AddUncompiledSource (dir);
			
			if (ConfigurationManager.AppSettings == null)
			{
				Logger.Log("Setting default settings because ConfigurationManager.AppSettings is null");
				var keyValueConfigurationCollection = new KeyValueConfigurationCollection();
				keyValueConfigurationCollection.Add("docPath", "/Library/Frameworks/Mono.framework/Versions/Current/lib/monodoc/");
				keyValueConfigurationCollection.Add("docExternalPath", "");
				typeof(Config).GetField("exeConfig", BindingFlags.NonPublic | BindingFlags.Static).SetValue(null, keyValueConfigurationCollection);
				Lucene.Net.Support.AppSettings.Set("java.version", "");
				Lucene.Net.Support.AppSettings.Set("java.vendor", "");
			}
		
			Root = RootTree.LoadTree ();

			if (extraDocs != null)
				foreach (var dir in extraDocs)
					Root.AddSource (dir);
			
			var macDocPath = Path.Combine (Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData), "macdoc");
			if (!Directory.Exists (macDocPath))
				Directory.CreateDirectory (macDocPath);
			var helpSources = Root.HelpSources
				.Cast<HelpSource> ()
				.Where (hs => !string.IsNullOrEmpty (hs.BaseFilePath) && !string.IsNullOrEmpty (hs.Name))
				.Select (hs => Path.Combine (hs.BaseFilePath, hs.Name + ".zip"))
				.Where (File.Exists);
			IndexUpdateManager = new IndexUpdateManager (helpSources,
			                                             macDocPath);
			BookmarkManager = new BookmarkManager (macDocPath);
		}

		int Run (string command)
		{
			var psi = new System.Diagnostics.ProcessStartInfo (command, " --need-update");
			var t = ProcessUtils.StartProcess (psi, null, null, CancellationToken.None);
			t.Wait ();

			try {
				return t.Result;
			} catch {
				return 1;
			}
		}

		public override void DidFinishLaunching (NSNotification notification)
		{
			// Check if we are loaded with a search term and load a document for it
			var args = Environment.GetCommandLineArgs ();
			NSError error;
			var searchArgIdx = Array.IndexOf<string> (args, "--search");
			if (searchArgIdx != -1 && args.Length > searchArgIdx + 1 && !string.IsNullOrEmpty (args [searchArgIdx + 1])) {
				var document = controller.OpenUntitledDocument (true, out error);
				if (document != null)
					((MyDocument)document).LoadWithSearch (args[searchArgIdx + 1]);
			}

			var indexManager = IndexUpdateManager;
			indexManager.CheckIndexIsFresh ().ContinueWith (t => {
				if (t.IsFaulted)
					Logger.LogError ("Error while checking indexes", t.Exception);
				else if (!t.Result)
					indexManager.PerformSearchIndexCreation ();
				else
					indexManager.AdvertiseFreshIndex ();
			}).ContinueWith (t => Logger.LogError ("Error while creating indexes", t.Exception), TaskContinuationOptions.OnlyOnFaulted);

			// Check if there is a MonoTouch/MonoMac documentation installed and launch accordingly
			var products = Root.HelpSources.Where (hs => hs != null && hs.Name != null).ToProducts ().Distinct ().ToArray ();
			var message = new StringBuilder ("We have detected that your documentation for the following products can be improved by merging the Apple documentation:\n");
			var toUpdate = new List<Product> ();
			foreach (var p in products) {
				bool needUpdate = false;
				var tool = ProductUtils.GetMergeToolForProduct (p);

				if (!File.Exists (tool))
					continue;

				if (Run (tool) == 0) {
					toUpdate.Add (p);
					message.AppendFormat ("{0}\n", ProductUtils.GetFriendlyName (p));
				}
			}

			if (toUpdate.Count > 0)
				LaunchDocumentationUpdate (toUpdate.ToArray (), message.ToString ());
		}
		
		public static IndexUpdateManager IndexUpdateManager {
			get;
			private set;
		}
		
		public static BookmarkManager BookmarkManager {
			get;
			private set;
		}

		public static bool IsOnLionOrBetter {
			get {
				return isOnLion;
			}
		}

		public static bool RestartRequested {
			get;
			set;
		}
		
		public override void WillFinishLaunching (NSNotification notification)
		{
			var selector = new ObjCRuntime.Selector ("handleGetURLEvent:withReplyEvent:");
			NSAppleEventManager.SharedAppleEventManager.SetEventHandler (this,
			                                                             selector,
			                                                             AEEventClass.Internet,
			                                                             AEEventID.GetUrl);
		}


		[Export ("handleGetURLEvent:withReplyEvent:")]
		public void HandleGetURLEvent (NSAppleEventDescriptor evt, NSAppleEventDescriptor replyEvt)
		{
			NSError error;
			shouldOpenInitialFile = evt.NumberOfItems == 0;
			
			// Received event is a list (1-based) of URL strings
			for (int i = 1; i <= evt.NumberOfItems; i++) {
				var innerDesc = evt.DescriptorAtIndex (i);
				// The next call works fine but is Lion-specific 
				// controller.OpenDocument (new NSUrl (innerDesc.StringValue), i == evt.NumberOfItems, delegate {});
				if (!string.IsNullOrEmpty (innerDesc.StringValue)) {
					NSUrl url = new NSUrl (innerDesc.StringValue);
					Call_OpenDocument (url, true, out error);
				}
			}
		}
		
		// If the application was launched with an url, we don't open a default window
		public override bool ApplicationShouldOpenUntitledFile (NSApplication sender)
		{
			return shouldOpenInitialFile;
		}
		
		// Prevent new document from being created when already launched
		public override bool ApplicationShouldHandleReopen (NSApplication sender, bool hasVisibleWindows)
		{
			return !hasVisibleWindows;
		}
				
		partial void HandlePrint (NSObject sender)
		{
			controller.CurrentDocument.PrintDocument (sender);
		}
		
		partial void HandleFind (NSMenuItem sender)
		{
			controller.CurrentMyDocument.MainWebView.PerformFindPanelAction (sender);
		}
		
		partial void HandleSearch (NSObject sender)
		{
			var searchField = controller.CurrentMyDocument.WindowForSheet.Toolbar.VisibleItems.Last ().View;
			controller.CurrentDocument.WindowForSheet.MakeFirstResponder (searchField);
		}
		
		public override void WillTerminate (NSNotification notification)
		{
			BookmarkManager.SaveBookmarks ();
			// Relaunch ourselves if it was requested
			if (RestartRequested)
				NSWorkspace.SharedWorkspace.LaunchApp (NSBundle.MainBundle.BundleIdentifier,
				                                       NSWorkspaceLaunchOptions.NewInstance | NSWorkspaceLaunchOptions.Async,
				                                       NSAppleEventDescriptor.NullDescriptor,
				                                       IntPtr.Zero);
		}
		
		void LaunchDocumentationUpdate (Product [] products, string informative)
		{
			var infoDialog = new NSAlert {
				AlertStyle = NSAlertStyle.Informational,
				MessageText = "Documentation update available",
				InformativeText = informative + "\n\nWarning: If you have not downloaded the documentation with Xcode, this program will download the documentation from Apple servers which can take a long time.\n\nWould you like to update the documentation now?"
			};
			
			infoDialog.AddButton ("Update now");
			infoDialog.AddButton ("Remind me later");
			var dialogResult = infoDialog.RunModal ();
			// If Cancel was clicked, just return
			if (dialogResult == (int)NSAlertButtonReturn.Second)
				return;

			var mergerTasks = products.Select (p => Task.Factory.StartNew (() => {
				var mergeToolPath = ProductUtils.GetMergeToolForProduct (p);

				var psi = new System.Diagnostics.ProcessStartInfo (mergeToolPath, null);
				return ProcessUtils.StartProcess (psi, null, null, CancellationToken.None);
			}).Unwrap ());

			// No Task.WhenAll yet
			var tcs = new TaskCompletionSource<int> ();
			Task.Factory.ContinueWhenAll (mergerTasks.ToArray (), ts => {
				var faulteds = ts.Where (t => t.IsFaulted);
				if (faulteds.Any ())
					tcs.SetException (faulteds.Select (t => t.Exception));
				else
					tcs.SetResult (ts.Select (t => t.Result).FirstOrDefault (r => r != 0));
			});

			var mergeController = new AppleDocMergeWindowController ();
			mergeController.TrackProcessTask (tcs.Task);
			mergeController.ShowWindow (this);
			mergeController.Window.Center ();
		}

		[DllImport (Constants.ObjectiveCLibrary, EntryPoint = "objc_msgSend")]
		public extern static System.IntPtr IntPtr_objc_msgSend_IntPtr_bool_IntPtr (IntPtr receiver, IntPtr selector, IntPtr arg1, bool arg2, IntPtr arg3);
		
		// We use a working OpenDocument method that doesn't return anything because of MonoMac bug#3380
		public void Call_OpenDocument (NSUrl absoluteUrl, bool displayDocument, out NSError outError)
		{
			outError = null;
			if (absoluteUrl == null)
				throw new ArgumentNullException ("absoluteUrl");
			IntPtr outErrorPtr = Marshal.AllocHGlobal(4);
			Marshal.WriteInt32(outErrorPtr, 0);

			IntPtr_objc_msgSend_IntPtr_bool_IntPtr (controller.Handle, selOpenDocumentWithContentsOfURLDisplayError_, absoluteUrl.Handle, displayDocument, outErrorPtr);
		}
		
		IntPtr selOpenDocumentWithContentsOfURLDisplayError_  = new Selector ("openDocumentWithContentsOfURL:display:error:").Handle;
	}
}