static void Main() { SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_SYSTEM_DPI_AWARE); Environment.CurrentDirectory = new FileInfo(Application.ExecutablePath).DirectoryName; MainLog = new MessageLog(); ResManager.Init(); MainLog.SetOutputFile(new FileInfo(ResManager.GetLogfileName())); MainLog.AddLong(0, MessageType.MSG, "Program started", "current directory: " + Environment.CurrentDirectory + "\n\nargs:\n" + string.Join("\n", Environment.GetCommandLineArgs()) + "\n\nfiles:\n" + string.Join("\n", Directory.GetFiles("."))); MainLog.AddLong(0, MessageType.MSG, "Build info", ResManager.GetText("buildinfo.txt")); Configuration.StoreAsDefault(); Configuration.LoadConfig(); Style.SetStyle(); ResManager.DeleteOldLogs(Configuration.NumberOfLogs); TmpManager.Init(); Language.Init(); MainLog.Add(MessageType.MSG, "Set Pointer Gamma"); PointerManager.Gamma = Configuration.PreassureGamma; MainLog.Add(MessageType.MSG, "Load Window Icon"); WindowIcon = Properties.Resources.KritzelIcon;//Icon.FromHandle(Properties.Resources.KritzelIcon.GetHicon()); MainLog.Add(MessageType.MSG, "Look for temporary Data"); RestoreData = TmpManager.OvertakeFirstUnused(); MainLog.Add(MessageType.MSG, "Enable Window"); if (Configuration.EnableVisualStyle) { Application.EnableVisualStyles(); } Application.SetCompatibleTextRenderingDefault(false); //DebugInterface.Start(); Application.Run(new MainWindow()); //DebugInterface.Stop(); HTTPCast.StopCasting(); }
public static void LoadConfig() { MessageLog loadingLog = new MessageLog(Program.MainLog); Program.MainLog.Add(MessageType.MSG, "Loading Configurations from '{0}'", FILENAME); if (!File.Exists(FILENAME)) { Program.MainLog.Add(MessageType.MSG, "File does not exist, creating file..."); loadingLog.Add(MessageType.MSG, "File '{0}' doesnt exist", FILENAME); SaveConfig(); return; } FileStream stream = File.OpenRead(FILENAME); StreamReader reader = new StreamReader(stream); Type type = typeof(Configuration); string logText = ""; while (!reader.EndOfStream) { string line = reader.ReadLine(); int sep = line.IndexOf(SEPARATOR); if (sep > 0 && !line.StartsWith("#")) { string key = line.Substring(0, sep); string value = line.Substring(sep + 1); logText += key + "=" + value + "\n"; FieldInfo field = type.GetField(key); bool parameterExists = false; if (field != null && field.IsStatic) { IEnumerable <CP> cps = field.GetCustomAttributes <CP>(); if (cps.Count() > 0) { parameterExists = true; Type ft = field.FieldType; bool canParse; if (ft == typeof(bool)) { bool v; canParse = bool.TryParse(value, out v); field.SetValue(null, v); } else if (ft == typeof(int)) { int v; canParse = int.TryParse(value, out v); field.SetValue(null, v); } else if (ft == typeof(float)) { float v; canParse = Util.TrySToF(value, out v); field.SetValue(null, v); } else if (ft == typeof(string)) { field.SetValue(null, value); canParse = true; } else if (ft.IsEnum) { try { object v = Enum.Parse(ft, value, true); field.SetValue(null, v); canParse = true; } catch (Exception) { canParse = false; } } else { canParse = true; loadingLog.Add(MessageType.ERROR, "Internal Error: cant parse key '{0}' to type '{1}'", key, ft.FullName); } if (!canParse) { loadingLog.Add(MessageType.ERROR, "Cant parse value '{0}' for key '{0}", value, key); } } } if (!parameterExists) { loadingLog.Add(MessageType.WARN, "The Configuration Parameter '{0}' does not exists", key); } } } Program.MainLog.AddLong(1, MessageType.MSG, "Configuration loaded", logText); reader.Close(); stream.Close(); stream.Dispose(); }
public void LoadFromString(string txt, MessageLog log) { lock (this) { StringReader input = new StringReader(txt); Line line = null; lines.Clear(); using (XmlReader xml = XmlReader.Create(input)) { while (xml.Read()) { if (xml.NodeType == XmlNodeType.Element) { if (xml.Name == "Line") { string typeN = xml.GetAttribute("type"); string param = xml.GetAttribute("params"); string color = null; try { color = xml.GetAttribute("color"); } catch (Exception) { } Type t = Assembly.GetCallingAssembly().GetType(typeN); line = t.GetConstructor(new Type[0]).Invoke(new object[0]) as Line; if (line != null) { if (line is Forms.TransformableForm) { try { var strMat = xml.GetAttribute("matrix"); ((Forms.TransformableForm)line).Transformation = Matrix3x3.LoadFromString(strMat); } catch (Exception) { } } line.FromParamString(param); line.CalcSpline(); line.CalculateBounds(); } if (color == null) { line.Brush = PBrush.CreateSolid(Color.Black); } else { line.Brush = PBrush.CreateSolid(ColorTranslator.FromHtml(color)); } lines.Add(line); } else if (xml.Name == "Brush") { PBrush brush = PBrush.FromStrings( xml.GetAttribute("type"), xml.GetAttribute("color"), xml.GetAttribute("nums")); if (line != null) { line.Brush = brush; } } else if (xml.Name == "Format") { float w = 1; float h = 1; float border = 15; Util.TrySToF(xml.GetAttribute("w"), out w); Util.TrySToF(xml.GetAttribute("h"), out h); Util.TrySToF(xml.GetAttribute("border"), out border); Border = border; Format = new PageFormat(w, h); string backgroundName = xml.GetAttribute("background"); Type backgroundType = Type.GetType(backgroundName); Backgrounds.Background bgr; if (backgroundType == null) { bgr = null; if (backgroundName != "null") { log?.Add(MessageType.WARN, "Background '{0}'", backgroundName); } } else { bgr = (Backgrounds.Background)backgroundType .GetConstructor(new Type[0]) .Invoke(new object[0]); } Background = bgr; } else if (xml.Name == "CreationTime") { bool show = xml.GetAttribute("show") == "true"; long time = 0; long.TryParse(xml.GetAttribute("date"), out time); ShowDate = show; CreationTime = DateTime.FromFileTime(time); } else if (xml.Name == "Name") { Name = xml.GetAttribute("value"); } else if (xml.Name == "Filter") { string filterName = xml.ReadElementContentAsString(); if (Enum.TryParse <ColorFilter>(filterName, out ColorFilter filter)) { Filter = filter; } else { log?.Add(0, MessageType.WARN, "Cant parse the ColorFilter '{0}'", filterName); } } } else if (xml.NodeType == XmlNodeType.EndElement) { if (xml.Name == "Line") { line = null; } } } } } }
public MessageLog(MessageLog parent) { this.parent = parent; }
public bool LoadDocument(string path, MessageLog log) { log?.Add(MessageType.MSG, "Loading File '{0}'", path); DirectoryInfo dir = TmpManager.GetTmpDir(); if (path != null) { ZipFile zip = new ZipFile(path); if (dir.Exists) { dir.RemoveAll(); } try { zip.ExtractAll(dir.FullName); } catch (BadPasswordException) { Dialogues.PasswordDialog dialog = new Dialogues.PasswordDialog(); var res = dialog.ShowDialog(out string pw, delegate(string _password) { try { zip.Password = _password; zip.ExtractAll(dir.FullName); return(true); } catch (BadPasswordException) { return(false); } }); if (res == DialogResult.Cancel) { return(false); } } } FileInfo docFile = new FileInfo(dir + "\\document.kritzel"); XmlReader reader = XmlReader.Create(docFile.FullName); XmlDocument doc = new XmlDocument(); doc.Load(reader); reader.Close(); XmlNode root = doc.LastChild; XmlNode pagesNode = root.GetNode("Pages"); //Bitmap[] bgrs = new Bitmap[0]; PdfDocument pdfDoc = null; bool loadPdf = false; if (File.Exists(dir + "\\.pdf")) { //bgrs = MupdfSharp.PageRenderer.Render(dir + "\\.pdf", Dialogues.PDFImporter.PAGETHEIGHTPIXEL); pdfDoc = PdfSharp.Pdf.IO.PdfReader.Open(dir + "\\.pdf", PdfDocumentOpenMode.Modify | PdfDocumentOpenMode.Import); loadPdf = true; } int ind = 1; foreach (XmlNode xmlPage in pagesNode.GetNodes("Page")) { string src = xmlPage.Attributes.GetNamedItem("src").InnerText; string text = File.ReadAllText(dir.FullName + "\\" + src); KPage page = new KPage(this); log?.Add(MessageType.MSG, "Loading Page {0}", ind++); page.LoadFromString(text, log); int pdfInd; int.TryParse(xmlPage.Attributes["pdf"].Value, out pdfInd); if (pdfInd >= 0 && loadPdf) { //page.BackgroundImage = new Renderer.Image(bgrs[pdfInd]); page.OriginalPage = pdfDoc.Pages[pdfInd]; page.PdfRenderPath = TmpManager.NewFilename(TmpManager.GetTmpDir() + "\\render", "page", ".png"); } Pages.Add(page); } var defForm = doc.GetElementsByTagName("DefaultFormat"); if (defForm.Count > 0) { DefaultFormat = defForm[0].InnerText; } if (loadPdf) { pdfDoc.Close(); pdfDoc.Dispose(); } this.FilePath = path; SetCurrentStateAsSaved(); return(true); }
public bool SaveDocument(string path) { try { Console.WriteLine("Saving file to ", path != null ? path : "temp"); MessageLog log = new MessageLog(Program.MainLog); if (path == null) { log.Add(MessageType.MSG, "Saving file to temp dir"); } else { log.Add(MessageType.MSG, "Saving file to {0}", path); } List <FileInfo> files = new List <FileInfo>(); DirectoryInfo dir = TmpManager.GetTmpDir(); if (dir.Exists) { dir.RemoveAll(); } dir.Create(); FileInfo docFile = new FileInfo(dir + "\\document.kritzel"); XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; settings.IndentChars = " "; settings.NewLineChars = "\n"; files.Add(docFile); using (XmlWriter writer = XmlWriter.Create(docFile.FullName, settings)) { PdfDocument pdfDoc = new PdfDocument(); writer.WriteStartElement("Document"); writer.WriteElementString("DefaultFormat", DefaultFormat); writer.WriteStartElement("Pages"); for (int i = 0; i < Pages.Count; i++) { writer.WriteStartElement("Page"); FileInfo pageFile = new FileInfo(dir + "\\Page" + i + ".xml"); string page = Pages[i].SaveToString(); File.WriteAllText(pageFile.FullName, page); files.Add(pageFile); writer.WriteAttributeString("src", pageFile.Name); string pdf = "-1"; if (Pages[i].OriginalPage != null) { string pdfName = "Page" + i + ".pdf"; pdf = "" + pdfDoc.Pages.Count; pdfDoc.AddPage(Pages[i].OriginalPage); } writer.WriteAttributeString("pdf", pdf); writer.WriteEndElement(); // Page } writer.WriteEndElement(); // Pages writer.WriteEndElement(); // Document writer.Close(); if (pdfDoc.Pages.Count > 0) { files.Add(new FileInfo(dir + "\\.pdf")); pdfDoc.Save(dir + "\\.pdf"); } if (path != null) { ZipFile zFile = new ZipFile(); foreach (FileInfo file in files) { zFile.AddFile(file.FullName, ""); } zFile.Save(path); SetCurrentStateAsSaved(); } } if (path != null) { this.FilePath = path; } } catch (Exception e) { Program.MainLog.Add(e); Dialogues.MsgBox.ShowOk(Language.GetText("Error.saveFile") + "\n" + e.Message); return(false); } return(true); }