/*************************************************************************************************************************/ // IMAGE public void LoadImage() { if (this.imagepath != "" && Os.FileExists(this.imagepath)) { try { string ext = ""; ext = Os.GetExtension(this.imagepath).ToLower(); if (ext == ".jpg" || ext == ".png" || ext == ".ico" || ext == ".bmp") { this.image = Media.GetImage(this.imagepath); if (ext != ".ico") { this.image.MakeTransparent(Color.White); } this.height = this.image.Height; this.width = this.image.Width; this.isimage = true; } } catch (Exception ex) { Program.log.Write("load image from xml error: " + ex.Message); } } else { this.imagepath = ""; } }
/// <summary> /// load global config file from json file</summary> private void LoadConfigFile() { try { Program.log.Write("loadConfigFile: path:" + this.optionsFilePath); string inputJSON = Os.ReadAllText(this.optionsFilePath); if (Os.FileExists(this.optionsFilePath)) { string xml = Os.GetFileContent(this.optionsFilePath); XmlReaderSettings xws = new XmlReaderSettings { CheckCharacters = false }; using (XmlReader xr = XmlReader.Create(new StringReader(xml), xws)) { XElement root = XElement.Load(xr); this.LoadParams(root); } } } catch (Exception ex) { Program.log.Write("loadConfigFile: " + ex.Message); } }
/// <summary> /// get parent directory of FileName path </summary> public static string GetFileDirectory(string FileName) { if (FileName.Trim().Length > 0 && Os.FileExists(FileName)) { return(new FileInfo(FileName).Directory.FullName); } return(null); }
/*************************************************************************************************************************/ // Recent files /// <summary> /// add path to recent files</summary> public void AddRecentFile(String path) { if (Os.FileExists(path)) { this.recentFiles.Remove(path); this.recentFiles.Insert(0, path); } }
/// <summary> /// check if diagramPath file path has good extension </summary> public static bool IsDiagram(string diagramPath) { diagramPath = NormalizePath(diagramPath); if (Os.FileExists(diagramPath) && Path.GetExtension(diagramPath).ToLower() == ".diagram") { return(true); } return(false); }
/*************************************************************************************************************************/ // FILE EXTENSION /// <summary> /// get file extension</summary> public static string GetExtension(string file) { string ext = ""; if (file != "" && Os.FileExists(file)) { ext = Path.GetExtension(file).ToLower(); } return(ext); }
/// <summary> /// meke filePath relative to currentPath. /// If is set inCurrentDir path is converted to relative only /// if currentPath is parent of filePath</summary> public static string MakeRelative(string filePath, string currentPath, bool inCurrentDir = true) { filePath = filePath.Trim(); currentPath = currentPath.Trim(); if (currentPath == "") { return(filePath); } if (!Os.FileExists(filePath) && !Os.DirectoryExists(filePath)) { return(filePath); } filePath = Os.GetFullPath(filePath); if (Os.FileExists(currentPath)) { currentPath = Os.GetDirectoryName(currentPath); } if (!Os.DirectoryExists(currentPath)) { return(filePath); } currentPath = Os.GetFullPath(currentPath); Uri pathUri = new Uri(filePath); // Folders must end in a slash if (!currentPath.EndsWith(Path.DirectorySeparatorChar.ToString())) { currentPath += Path.DirectorySeparatorChar; } int pos = filePath.ToLower().IndexOf(currentPath.ToLower()); if (inCurrentDir && pos != 0) // skip files outside of currentPath { return(filePath); } Uri folderUri = new Uri(currentPath); return(Uri.UnescapeDataString( folderUri.MakeRelativeUri(pathUri) .ToString() .Replace('/', Path.DirectorySeparatorChar) )); }
/// <summary> /// remove old not existing diagrams from recent files</summary> public void RemoveOldRecentFiles() { List <String> newRecentFiles = new List <String>(); foreach (String path in this.recentFiles) { if (Os.FileExists(path)) { newRecentFiles.Add(path); } } this.recentFiles = newRecentFiles; }
/// <summary> /// open path in system if exist </summary> public static void OpenPathInSystem(string path) { if (Os.FileExists(path)) // OPEN FILE { try { string parent_diectory = Os.GetFileDirectory(path); System.Diagnostics.Process.Start(parent_diectory); } catch (Exception ex) { Program.log.Write("openPathInSystem open file: error:" + ex.Message); } } else if (Os.DirectoryExists(path)) // OPEN DIRECTORY { try { System.Diagnostics.Process.Start(path); } catch (Exception ex) { Program.log.Write("openPathInSystem open directory: error:" + ex.Message); } } }
/*************************************************************************************************************************/ /// <summary> /// load global config file from portable file configuration or global file configuration /// </summary> /// <param name="parameters">reference to parameter object</param> public ProgramOptionsFile(ProgramOptions programOptions) { this.programOptions = programOptions; // use local config file this.optionsFilePath = Os.Combine(Os.GetCurrentApplicationDirectory(), this.configFileName); // use global config file if local version not exist if (!Os.FileExists(this.optionsFilePath)) { this.optionsFilePath = Os.Combine( this.GetGlobalConfigDirectory(), this.configFileName ); } // open config file if exist if (Os.FileExists(this.optionsFilePath)) { this.LoadConfigFile(); } else { string globalConfigDirectory = Os.Combine( Os.GetApplicationsDirectory(), this.configFileDirectory ); // create global config directory if not exist if (!Os.DirectoryExists(globalConfigDirectory)) { Os.CreateDirectory(globalConfigDirectory); } // if config file dosn't exist create one with default values this.SaveConfigFile(); } }
/*************************************************************************************************************************/ // FILE OPERATIONS /// <summary> /// check if path is file</summary> public static bool IsFile(string path) { return(Os.FileExists(path)); }
/// <summary> /// open existing diagram or create new empty diagram /// Create diagram model and then open diagram view on this model</summary> public bool OpenDiagram(String FilePath = "") //UID1771511767 { Program.log.Write("Program : OpenDiagram: " + FilePath); if (passwordForm != null) // prevent open diagram if another diagram triing open { return(false); } // open new empty diagram in main process if (FilePath == "" && !server.mainProcess) { // if server already exist in system, send him message whitch open empty diagram server.SendMessage("open:"); return(false); } // open diagram in current program instance if (FilePath == "" && server.mainProcess) { // create new model Diagram emptyDiagram = new Diagram(this); Diagrams.Add(emptyDiagram); // open diagram view on diagram model emptyDiagram.OpenDiagramView(); return(false); } // open existing diagram file if (!Os.FileExists(FilePath)) { return(false); } FilePath = Os.NormalizedFullPath(FilePath); // if server already exist in system, send him message whitch open diagram file if (!server.mainProcess) { FilePath = Os.GetFullPath(FilePath); server.SendMessage("open:" + FilePath); //UID1105610325 return(false); } // open diagram in current program instance // check if file is already opened in current instance bool alreadyOpen = false; foreach (Diagram openedDiagram in Diagrams) { if (openedDiagram.FileName != FilePath) { continue; } openedDiagram.FocusToView(); alreadyOpen = true; break; } if (alreadyOpen) { return(false); } Diagram diagram = new Diagram(this); //UID8780020416 lock (diagram) { // create new model if (!diagram.OpenFile(FilePath)) { return(false); } this.programOptions.AddRecentFile(FilePath); #if !MONO RecentFiles.AddToRecentlyUsedDocs(FilePath); // add to system recent files #endif Diagrams.Add(diagram); // open diagram view on diagram model DiagramView newDiagram = diagram.OpenDiagramView(); //UID3015837184 this.plugins.OpenDiagramAction(diagram); //UID0290845816 Program.log.Write("bring focus"); Media.BringToFront(newDiagram); //UID4510272263 } return(true); }
/// <summary> /// process comand line arguments</summary> public void ParseCommandLineArguments(string[] args) // [PARSE] [COMMAND LINE] UID5172911205 { // options - create new file with given name if not exist bool CommandLineCreateIfNotExistFile = false; bool ShowCommandLineHelp = false; bool ShowDebugConsole = false; // list of diagram files names for open List <String> CommandLineOpen = new List <String>(); String arg; for (int i = 0; i < args.Length; i++) { //skip application name if (i == 0) { continue; } // current processing argument arg = args[i]; // [COMAND LINE] [CREATE] oprions create new file with given name if not exist if (arg == "-h" || arg == "--help" || arg == "/?") { ShowCommandLineHelp = true; break; } if (arg == "-c" || arg == "--console") { ShowDebugConsole = true; break; } if (arg == "-e") { CommandLineCreateIfNotExistFile = true; break; } // [COMAND LINE] [OPEN] check if argument is diagram file if (Os.GetExtension(arg).ToLower() == ".diagram") { CommandLineOpen.Add(arg); break; } Program.log.Write("bed commmand line argument: " + arg); } if (ShowDebugConsole) { this.ShowConsole(); } // open diagram given as arguments if (ShowCommandLineHelp) { String help = "diagram -h --help /? >> show this help\n" + "diagram -c --console /? >> show debug console\n" + "diagram -e {filename} >> create file if not exist\n" + "diagram {filepath} {filepath} >> open existing file\n"; MessageBox.Show(help, "Command line parameters"); return; } if (CommandLineOpen.Count == 0) { if (this.programOptions.defaultDiagram != "" && Os.FileExists(this.programOptions.defaultDiagram)) { this.OpenDiagram(this.programOptions.defaultDiagram); // open default diagram if default diagram is set } else if (this.programOptions.openLastFile && this.programOptions.recentFiles.Count > 0 && Os.FileExists(this.programOptions.recentFiles[0])) { this.OpenDiagram(this.programOptions.recentFiles[0]); // open last file if user option is enabled UID2130542088 } else { this.OpenDiagram(); //open empty diagram UID5981683893 } return; } for (int i = 0; i < CommandLineOpen.Count; i++) { string file = CommandLineOpen[i]; // tray create diagram file if command line option is set if (CommandLineCreateIfNotExistFile && !Os.FileExists(file)) { try { Os.CreateEmptyFile(file); } catch (Exception ex) { Program.log.Write("create empty diagram file error: " + ex.Message); } } if (Os.FileExists(file)) { this.OpenDiagram(file); //UID2130542088 } } // cose application if is not diagram model opened this.CloseEmptyApplication(); }