private static void FindRoot(IAnkhServiceProvider context, Uri selectedUri, CheckoutProject dlg)
        {
            AnkhAction ds = delegate
            {
                using (SvnClient client = context.GetService <ISvnClientPool>().GetClient())
                {
                    string value;
                    if (client.TryGetProperty(selectedUri, AnkhSccPropertyNames.ProjectRoot, out value))
                    {
                        if (dlg.IsHandleCreated)
                        {
                            dlg.Invoke((AnkhAction) delegate
                            {
                                try
                                {
                                    dlg.ProjectTop = new Uri(selectedUri, value);
                                }
                                catch { };
                            });
                        }
                    }
                }
            };

            ds.BeginInvoke(null, null);
        }
        private static void OpenSolution(CommandEventArgs e, CheckoutProject dlg)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            IVsSolution2 sol = e.GetService <IVsSolution2>(typeof(SVsSolution));

            if (sol != null)
            {
                sol.CloseSolutionElement(VSItemId.Root, null, 0); // Closes the current solution
            }

            IAnkhSccService scc = e.GetService <IAnkhSccService>();

            if (scc != null)
            {
                scc.RegisterAsPrimarySccProvider(); // Make us the current SCC provider!
            }
            CheckOutAndOpenSolution(e, dlg.ProjectTop, dlg.Revision, dlg.ProjectTop, dlg.SelectedPath, dlg.ProjectUri);

            sol = e.GetService <IVsSolution2>(typeof(SVsSolution));

            if (sol != null)
            {
                string file, user, dir;

                if (VSErr.Succeeded(sol.GetSolutionInfo(out dir, out file, out user)) &&
                    !string.IsNullOrEmpty(file))
                {
                    scc.SetProjectManaged(null, true);
                }
            }
        }
        public override void OnExecute(CommandEventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            Uri selectedUri = null;
            Uri rootUri     = null;

            bool addingProject = (e.Command == AnkhCommand.FileFileAddFromSubversion ||
                                  e.Command == AnkhCommand.FileSccAddFromSubversion);

            if (e.Argument is string && Uri.TryCreate((string)e.Argument, UriKind.Absolute, out selectedUri))
            {
            }
            else if (e.Argument is SvnOrigin)
            {
                SvnOrigin origin = (SvnOrigin)e.Argument;
                selectedUri = origin.Uri;
                rootUri     = origin.RepositoryRoot;
            }
            else if (e.Argument is Uri)
            {
                selectedUri = (Uri)e.Argument;
            }

            IAnkhSolutionSettings settings = e.GetService <IAnkhSolutionSettings>();

            if (e.PromptUser || selectedUri == null)
            {
                using (RepositoryOpenDialog dlg = new RepositoryOpenDialog())
                {
                    if (addingProject)
                    {
                        dlg.Text = CommandStrings.AddProjectFromSubversion;
                    }

                    dlg.Filter = settings.OpenProjectFilterName + "|" + settings.AllProjectExtensionsFilter + "|All Files (*.*)|*";

                    if (selectedUri != null)
                    {
                        dlg.SelectedUri = selectedUri;
                    }

                    if (dlg.ShowDialog(e.Context) != DialogResult.OK)
                    {
                        return;
                    }

                    selectedUri = dlg.SelectedUri;
                    rootUri     = dlg.SelectedRepositoryRoot;
                }
            }
            else if (rootUri == null)
            {
                if (!e.GetService <IProgressRunner>().RunModal(CommandStrings.RetrievingRepositoryRoot,
                                                               delegate(object sender, ProgressWorkerArgs a)
                {
                    rootUri = a.Client.GetRepositoryRoot(selectedUri);
                }).Succeeded)
                {
                    return;
                }
            }

            string defaultPath = settings.NewProjectLocation;

            if (addingProject)
            {
                IAnkhSolutionSettings ss = e.GetService <IAnkhSolutionSettings>();

                if (!string.IsNullOrEmpty(ss.ProjectRoot))
                {
                    defaultPath = ss.ProjectRoot;
                }
            }

            string name = Path.GetFileNameWithoutExtension(SvnTools.GetFileName(selectedUri));

            string newPath;
            int    n = 0;

            do
            {
                newPath = Path.Combine(defaultPath, name);
                if (n > 0)
                {
                    newPath += string.Format("({0})", n);
                }
                n++;
            }while (File.Exists(newPath) || Directory.Exists(newPath));

            using (CheckoutProject dlg = new CheckoutProject())
            {
                dlg.Context = e.Context;

                if (addingProject)
                {
                    dlg.Text = CommandStrings.AddProjectFromSubversion;
                }
                dlg.ProjectUri        = selectedUri;
                dlg.RepositoryRootUri = rootUri;
                dlg.SelectedPath      = newPath;
                dlg.SvnOrigin         = new SvnOrigin(selectedUri, rootUri);
                dlg.HandleCreated    += delegate
                {
                    FindRoot(e.Context, selectedUri, dlg);
                };

                if (dlg.ShowDialog(e.Context) != DialogResult.OK)
                {
                    return;
                }

                if (!addingProject)
                {
                    OpenSolution(e, dlg);
                }
                else
                {
                    CheckOutAndOpenProject(e, dlg.ProjectTop, dlg.Revision, dlg.ProjectTop, dlg.SelectedPath, dlg.ProjectUri);
                }
            }
        }