Exemplo n.º 1
0
        void LaunchDocumentationUpdate(Dictionary <Product, Tuple <bool, bool> > toUpdate)
        {
            var outdatedProducts = string.Join(" and ", toUpdate.Where(kvp => kvp.Value.Item1 || kvp.Value.Item2).Select(kvp => ProductUtils.GetFriendlyName(kvp.Key)));
            var informative      = "We have detected your " + outdatedProducts + " documentation can be upgraded with Apple documentation.";

            // Check if we are going to be downloading stuff
            if (toUpdate.Any(kvp => kvp.Value.Item1))
            {
                informative += Environment.NewLine + Environment.NewLine + "Warning: we are going to download documentation from Apple servers which can take a long time depending on your Internet connection.";
            }
            informative += Environment.NewLine + Environment.NewLine + "Would you like to update the documentation now?";

            var infoDialog = new NSAlert {
                AlertStyle      = NSAlertStyle.Informational,
                MessageText     = "Documentation update available",
                InformativeText = informative
            };

            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;
            }

            // Launching AppleDocWizard as root
            var mergerTasks = toUpdate
                              .Where(kvp => kvp.Value.Item1 || kvp.Value.Item2)
                              .Select(kvp => Task.Factory.StartNew(() => {
                var mergeToolPath = ProductUtils.GetMergeToolForProduct(kvp.Key);
                var docOutdated   = kvp.Value.Item1;

                // If the script has its setuid bit on and user as root, then we launch it directly otherwise we first restore it
                if (!RootLauncher.IsRootEnabled(mergeToolPath))
                {
                    RootLauncher.LaunchExternalTool(mergeToolPath, new string[] { "--self-repair" });
                    // No good way to know when the process will finish, so wait a bit. Not ideal but since this is an unlikely codepath, shouldn't matter.
                    System.Threading.Thread.Sleep(1000);
                }
                var psi = new System.Diagnostics.ProcessStartInfo(mergeToolPath, docOutdated ? "--force-download" : 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();
        }
Exemplo n.º 2
0
        public override void FinishedLaunching(NSObject 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();

            if (products.Where(p => File.Exists(ProductUtils.GetMergeToolForProduct(p))).Any())
            {
                Task.Factory.StartNew(() => {
                    return(products.ToDictionary(p => p,
                                                 p => {
                        AppleDocHandler.AppleDocInformation infos;
                        bool mergeOutdated = false;
                        bool docOutdated = AppleDocHandler.CheckAppleDocFreshness(ProductUtils.GetDocFeedForProduct(p),
                                                                                  out infos);
                        if (!docOutdated)
                        {
                            mergeOutdated = AppleDocHandler.CheckMergedDocumentationFreshness(infos, p);
                        }
                        return Tuple.Create(docOutdated, mergeOutdated);
                    }));
                }).ContinueWith(t => {
                    Logger.Log("Merged status {0}", string.Join(", ", t.Result.Select(kvp => kvp.ToString())));
                    if (!t.Result.Any(kvp => kvp.Value.Item1 || kvp.Value.Item2))
                    {
                        return;
                    }
                    BeginInvokeOnMainThread(() => LaunchDocumentationUpdate(t.Result));
                });
            }
        }