Ejemplo n.º 1
0
        /// <summary>
        /// Load or merge a set of repositories from a file.
        /// Returns true is loading succeeded and this class is assigned a new set of repos.
        /// Returns false if loading failed, or was cancelled. The repos in this class did not change.
        /// If the requested operation is a merge, isImport=true, the new set of repos will be merged with the existing one.
        /// </summary>
        public bool Load(string fileName, bool isImport)
        {
            bool ret = false;
            // Wrap the opening of a repository database with an outer handler
            try
            {
                using (FileStream file = new FileStream(fileName, FileMode.Open))
                {
                    try
                    {
                        // Load list of repos and the default repo string into temporary objects
                        BinaryFormatter rd = new BinaryFormatter();
                        List<ClassRepo> newRepos = (List<ClassRepo>)rd.Deserialize(file);
                        string defaultRepo = (string)rd.Deserialize(file);

                        // WAR: Mono 2.6.7 does not support serialization of a HashSet. At the same time...
                        // Quickly check that each repo is valid (find if at least one is not)
                        bool allOK = true;
                        foreach (ClassRepo repo in newRepos)
                        {
                            allOK &= ClassUtils.DirStat(repo.Root) == ClassUtils.DirStatType.Git;
                            repo.ExpansionReset(null);
                        }

                        // If at least one repo was not valid, give the user a chance to recreate it
                        if (allOK == false)
                        {
                            FormRecreateRepos recreateRepos = new FormRecreateRepos();
                            recreateRepos.Repos = newRepos;
                            // The "Accept" button in the recreate repos dialog will not be enabled
                            // until every repo has been configured properly. User can always cancel
                            // that process in which case we will not load the selected repo.
                            if (recreateRepos.ShowDialog() == DialogResult.Cancel)
                                return false;
                            newRepos = recreateRepos.Repos;
                        }

                        // If the operation is a simple load, assign our object's list of repos
                        // Otherwise, we merge the new set with the existing one
                        if (isImport)
                            Repos.AddRange(newRepos);
                            // After we merge the new set of repos, current/default repo remains the same
                        else
                        {
                            Repos = newRepos;
                            // Upon load, set the current based on the default repo
                            Default = Repos.Find(r => r.Root == defaultRepo);
                            SetCurrent(Default);
                        }
                        ret = true;
                    }
                    catch (Exception ex)
                    {
                        throw new ClassException(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                App.PrintLogMessage(ex.Message, MessageType.Error);
            }
            return ret;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Load or merge a set of repositories from a file.
        /// Returns true is loading succeeded and this class is assigned a new set of repos.
        /// Returns false if loading failed, or was cancelled. The repos in this class did not change.
        /// If the requested operation is a merge, isImport=true, the new set of repos will be merged with the existing one.
        /// </summary>
        public bool Load(string fileName, bool isImport)
        {
            // Wrap the opening of a repository database with an outer handler
            try
            {
                using (FileStream file = new FileStream(fileName, FileMode.Open))
                {
                    try
                    {
                        // Load list of repos and the default repo string into temporary objects
                        BinaryFormatter  rd          = new BinaryFormatter();
                        List <ClassRepo> newRepos    = (List <ClassRepo>)rd.Deserialize(file);
                        string           defaultRepo = (string)rd.Deserialize(file);

                        // WAR: Mono 2.6.7 does not support serialization of a HashSet. At the same time...
                        // Quickly check that each repo is valid (find if at least one is not)
                        bool allOK = true;
                        foreach (ClassRepo repo in newRepos)
                        {
                            allOK &= ClassUtils.DirStat(repo.Path) == ClassUtils.DirStatType.Git;
                            repo.ExpansionReset(null);
                        }

                        // If at least one repo was not valid, give the user a chance to recreate it
                        if (allOK == false)
                        {
                            FormRecreateRepos recreateRepos = new FormRecreateRepos();
                            recreateRepos.Repos = newRepos;
                            // The "Accept" button in the recreate repos dialog will not be enabled
                            // until every repo has been configured properly. User can always cancel
                            // that process in which case we will not load the selected repo.
                            if (recreateRepos.ShowDialog() == DialogResult.Cancel)
                            {
                                return(false);
                            }
                            newRepos = recreateRepos.Repos;
                        }

                        // If the operation is a simple load, assign our object's list of repos
                        // Otherwise, we merge the new set with the existing one
                        if (isImport)
                        {
                            Repos.AddRange(newRepos);
                        }
                        // After we merge the new set of repos, current/default repo remains the same
                        else
                        {
                            Repos = newRepos;
                            // Upon load, set the current based on the default repo
                            Default = Repos.Find(r => r.Path == defaultRepo);
                            SetCurrent(Default);
                        }
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        throw new ClassException(ex.Message);
                    }
                }
            }
            catch (Exception ex)
            {
                App.PrintLogMessage(ex.Message, MessageType.Error);
            }
            return(false);
        }