Example #1
0
        internal static void PrepareBrand(EM_UI_MainForm emptyForm)
        {
            try
            {
                string brandName  = GetProjectNameOrDefault();
                string brandTitle = Properties.Resources.ResourceManager.GetString("brand_title_" + brandName);

                if (String.IsNullOrEmpty(brandTitle))
                {
                    brandTitle = brandName = "EUROMOD";
                    // For now, completely go silent if the brand is wrong. To warn, uncomment the line below:
                    //UserInfoHandler.ShowError($"File '{projectSettingsFullPath}' does not contain a valid Project Name.{Environment.NewLine}Alternative brand is ignored.");
                }

                DefGeneral.BRAND_TITLE = brandTitle;
                DefGeneral.BRAND_NAME  = brandName;
                SetBackgroundImage(emptyForm);
                SetIcon(emptyForm);
                emptyForm.btnRun.Caption            = $"Run{Environment.NewLine}{DefGeneral.BRAND_TITLE}";
                emptyForm.barText_PoweredBy.Caption = DefGeneral.IsAlternativeBrand() ? GetPoweredByText() : string.Empty;
            }
            catch (Exception exception)
            {
                UserInfoHandler.ShowException(exception, "Adapting to alternative Brand failed!", false);
            }
        }
        internal static bool CaptureFile(string fullFileName, ref bool openedReadOnly)
        {
            try
            {
                openedReadOnly = false;
                string inUseFilePath = GetInUseFilePath(fullFileName);
                if (File.Exists(inUseFilePath))
                {
                    string       usedBy; using (StreamReader reader = new StreamReader(inUseFilePath)) usedBy = reader.ReadLine();
                    DialogResult userDecision = UserInfoHandler.GetInfo(usedBy + Environment.NewLine +
                                                                        "Do you want to open this country file in read-only mode?" + Environment.NewLine + Environment.NewLine +
                                                                        "Click 'Yes' to open in read-only mode." + Environment.NewLine +
                                                                        "If you know this lock has been caused by an error, click 'No' to unlock it and open in read-write mode." + Environment.NewLine +
                                                                        "Click 'Cancel' if you do not want to open this country file." + Environment.NewLine, MessageBoxButtons.YesNoCancel);
                    if (userDecision == DialogResult.Cancel)
                    {
                        return(false);
                    }
                    if (userDecision == DialogResult.Yes)
                    {
                        openedReadOnly = true;
                        return(true);
                    }
                }

                using (StreamWriter writer = new StreamWriter(inUseFilePath))
                    writer.WriteLine(fullFileName + " is currently being edited by user '" + System.Environment.UserName + "'.");
            }
            catch (Exception exception)
            {
                UserInfoHandler.ShowException(exception);
            }
            return(true); //do not stop opening, just because something goes wrong with lock-file-generating
        }
        static bool FolderRecursivly(string sourceFolderPath, string destinationFolderPath, ref bool undo, string renameFolderTo = "")
        {
            sourceFolderPath      = EMPath.AddSlash(sourceFolderPath);
            destinationFolderPath = EMPath.AddSlash(destinationFolderPath);

            try
            {
                DirectoryInfo sourceFolder = new DirectoryInfo(sourceFolderPath);
                if (renameFolderTo == string.Empty)
                {
                    destinationFolderPath = EMPath.AddSlash(destinationFolderPath + sourceFolder.Name);
                }
                else
                {
                    destinationFolderPath = EMPath.AddSlash(destinationFolderPath + renameFolderTo);
                }

                //check if trying to copy in same parent folder
                if (destinationFolderPath.ToLower() == sourceFolderPath.ToLower())
                {
                    UserInfoHandler.ShowError("Cannot duplicate folder '" + sourceFolder.Name + "' in its parent folder.");
                    return(false);
                }

                //check if source folder already exists at destination path
                if (Directory.Exists(destinationFolderPath))
                {
                    if (UserInfoHandler.GetInfo("Folder '" + destinationFolderPath + "' already exists. Should it be deleted?", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    {
                        return(false);
                    }
                    Directory.Delete(destinationFolderPath, true);
                }

                //create source folder at destination path
                Directory.CreateDirectory(destinationFolderPath);
                undo = true;

                //copy all files directly under this folder
                FileInfo[] subFiles = sourceFolder.GetFiles("*.*");
                if (subFiles != null)
                {
                    foreach (FileInfo subFile in subFiles)
                    {
                        File.Copy(subFile.FullName, destinationFolderPath + subFile.Name);
                    }
                }
                //find all subfolders under this folder for recursive call
                foreach (DirectoryInfo subFolder in sourceFolder.GetDirectories())
                {
                    FolderRecursivly(subFolder.FullName, destinationFolderPath, ref undo);
                }
            }
            catch (Exception exception)
            {
                UserInfoHandler.ShowException(exception, "Close and reopen the user interface and try again.");
                return(false);
            }
            return(true);
        }
 internal static void ReleaseFile(string fullFileName)
 {
     try
     {
         string inUseFilePath = GetInUseFilePath(fullFileName);
         if (File.Exists(inUseFilePath))
         {
             File.Delete(inUseFilePath);
         }
     }
     catch (Exception exception)
     {
         UserInfoHandler.ShowException(exception);
     }
 }
Example #5
0
        // this reads the ProjectSettings.xml if it exists and returns the ProjectName field if it exists, or null otherwise
        private static string GetProjectNameOrDefault()
        {
            string brandName = null;
            string projectSettingsFullPath = new EMPath(EM_AppContext.FolderEuromodFiles).GetProjectSettingsPath(true);

            if (File.Exists(projectSettingsFullPath))
            {
                try
                {
                    using (XmlReader xmlReader = XmlReader.Create(projectSettingsFullPath, new XmlReaderSettings()
                    {
                        ConformanceLevel = ConformanceLevel.Fragment
                    }))
                    {
                        xmlReader.Read();
                        while (xmlReader.NodeType != XmlNodeType.None && (xmlReader.NodeType != XmlNodeType.Element || xmlReader.Name != "ProjectSettings"))
                        {
                            xmlReader.Read();
                        }
                        if (xmlReader.NodeType == XmlNodeType.None)
                        {
                            UserInfoHandler.ShowError($"File '{projectSettingsFullPath}' does not contain a valid XML structure." + Environment.NewLine + "Alternative brand is ignored.");
                        }
                        XElement settingsElement = XElement.ReadFrom(xmlReader) as XElement;
                        foreach (XElement xe in settingsElement.Elements())
                        {
                            if (xe.Value == null)
                            {
                                continue;
                            }
                            switch (GetXEleName(xe))
                            {
                            case BRAND_ELEMENT_PROJECT_NAME: brandName = xe.Value; break;

                            default: break;     // unknown tags are simply ignored for now
                            }
                        }
                        ;
                    }
                }
                catch { }
            }
            return(String.IsNullOrEmpty(brandName) ? "EUROMOD" : brandName);  // return the brand or
        }
        //Helper
        internal static bool Folder(string sourceFolderPath, string destinationFolderPath, string renameFolderTo = "")
        {
            bool undo = false;

            if (!FolderRecursivly(sourceFolderPath, destinationFolderPath, ref undo, renameFolderTo))
            {
                if (!undo)
                {
                    return(false);
                }
                try
                {
                    //undo any copying that already happened
                    DirectoryInfo sourceFolder = new DirectoryInfo(sourceFolderPath);
                    if (renameFolderTo == string.Empty)
                    {
                        destinationFolderPath = EMPath.AddSlash(destinationFolderPath + sourceFolder.Name);
                    }
                    else
                    {
                        destinationFolderPath = EMPath.AddSlash(destinationFolderPath + renameFolderTo);
                    }

                    if (Directory.Exists(destinationFolderPath))
                    {
                        Directory.Delete(destinationFolderPath, true);
                    }
                }
                catch (Exception exception)
                {
                    UserInfoHandler.ShowException(exception);
                }
                return(false);
            }
            return(true);
        }