Ejemplo n.º 1
0
        private void AddAttrib(KmlNode toItem, KmlAttrib beforeItem)
        {
            KmlNode node = toItem;
            string  input;
            string  preset = "";
            bool    loop   = true;

            while (loop && DlgInput.Show("Enter the name for the new attribute:", "NEW attribute", Icons.Add, preset, out input))
            {
                string attrib = input;
                if (attrib.Length > 0 && attrib.IndexOf('=') < 0)
                {
                    attrib = attrib + "=";
                }
                KmlItem item = KmlItem.CreateItem(attrib);
                if (item is KmlAttrib && beforeItem != null)
                {
                    node.InsertBefore(beforeItem, (KmlAttrib)item);
                    loop = false;
                    // View will be refreshed in AttribChanged event
                }
                else if (item is KmlAttrib)
                {
                    node.Add((KmlAttrib)item);
                    loop = false;
                    // View will be refreshed in AttribChanged event
                }
                else
                {
                    DlgMessage.Show("Attribute name is not allowed to be empty or contain following characters: {}", "NEW attribute", Icons.Warning);
                    preset = input;
                    // Input will pop up again while loop == true
                }
            }
        }
Ejemplo n.º 2
0
        private void Load(string filename)
        {
            TabsManager.Load(filename);
            Filename = filename;

            DlgSearch.SearchReset();

            try
            {
                string programFilesX64 = Environment.ExpandEnvironmentVariables("%ProgramW6432%");
                string programFilesX86 = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86);

                bool isProg = filename.StartsWith(programFilesX64, StringComparison.InvariantCultureIgnoreCase) ||
                              filename.StartsWith(programFilesX86, StringComparison.InvariantCultureIgnoreCase);
                bool isAdmin = (new WindowsPrincipal(WindowsIdentity.GetCurrent())).IsInRole(WindowsBuiltInRole.Administrator);

                if (isProg && !isAdmin)
                {
                    DlgMessage.Show("This file is located under 'program files' and hence protected by Windows.\n" +
                                    "So KML will not be able to overwrite it, when you choose to save your changes.\n\n" +
                                    "Please run KML as administrator or manually pick the changed file from 'program data'\n" +
                                    "and put it back to original place.", "Permission warning", (new GuiIcons16()).Warning);
                }
            }
            catch
            {
                ; // something went wrong with reading env-var or admin flag
            }
        }
Ejemplo n.º 3
0
        private void AddChildNode(KmlNode toItem, KmlNode beforeItem)
        {
            KmlNode node = toItem;
            string  input;
            string  preset = "";
            bool    loop   = true;

            while (loop && DlgInput.Show("Enter the tag for the new node:", "NEW node", Icons.Add, preset, out input))
            {
                KmlItem item = KmlItem.CreateItem(input);
                if (item is KmlNode && beforeItem != null)
                {
                    node.InsertBefore(beforeItem, (KmlNode)item);
                    loop = false;
                    // View will be refreshed in ChildrenChanged event
                }
                else if (item is KmlNode)
                {
                    node.Add((KmlNode)item);
                    loop = false;
                    // View will be refreshed in ChildrenChanged event
                }
                else
                {
                    DlgMessage.Show("Tag is not allowed to be empty or contain following characters: ={}", "NEW node", Icons.Warning);
                    preset = input;
                    // Input will pop up again while loop == true
                }
            }
        }
Ejemplo n.º 4
0
        private void NodeInsertBefore_Click(object sender, RoutedEventArgs e)
        {
            KmlNode node = ((sender as MenuItem).DataContext as KmlNode);

            if (node.Parent != null)
            {
                AddChildNode(node.Parent, node);
            }
            else
            {
                DlgMessage.Show("Can not insert, node has no parent", "NEW node", Icons.Warning);
            }
        }
Ejemplo n.º 5
0
 private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
 {
     try
     {
         GuiIcons ico = new GuiIcons16();
         string   txt = "Sorry, an unhandled exception occurred!\n\n" +
                        "Please report follwing details on the KSP forum thread or via GitHub issue:\n\n" + e.Exception.ToString();
         DlgMessage.Show(txt, "KML Exception", ico.Error);
     }
     catch
     {
         // maybe something breaks the DlgMessage, e.g. there is no main window (to center onto), then fall back to MessageBox
         MessageBox.Show(e.Exception.ToString(), "KML Exception");
     }
     e.Handled = true;
 }
Ejemplo n.º 6
0
        private void AttribInsertBefore_Click(object sender, RoutedEventArgs e)
        {
            // TODO GuiTreeAttrib.AttribInsertBefore_Click(): Almost same code as private GuiTreeNode.AddAttrib()

            KmlAttrib beforeItem = ((sender as MenuItem).DataContext as KmlAttrib);
            KmlNode   node       = beforeItem.Parent;

            if (node != null)
            {
                string input;
                string preset = "";
                bool   loop   = true;
                while (loop && DlgInput.Show("Enter the name for the new attribute:", "NEW attribute", Icons.Add, preset, out input))
                {
                    string attrib = input;
                    if (attrib.Length > 0 && attrib.IndexOf('=') < 0)
                    {
                        attrib = attrib + "=";
                    }
                    KmlItem item = KmlItem.CreateItem(attrib);
                    if (item is KmlAttrib && beforeItem != null)
                    {
                        node.InsertBefore(beforeItem, (KmlAttrib)item);
                        loop = false;
                        // View will be refreshed in AttribChanged event
                    }
                    else if (item is KmlAttrib)
                    {
                        node.Add((KmlAttrib)item);
                        loop = false;
                        // View will be refreshed in AttribChanged event
                    }
                    else
                    {
                        DlgMessage.Show("Attribute name is not allowed to be empty or contain following characters: {}", "NEW attribute", Icons.Warning);
                        preset = input;
                        // Input will pop up again while loop == true
                    }
                }
            }
            else
            {
                DlgMessage.Show("Can not insert, attribute has no parent", "NEW attribute", Icons.Warning);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Write the given list of root items to a data file.
        /// Child items of these roots a written recursively.
        /// If a file with given name already exists, it is renamed for backup
        /// to "zKMLBACKUP&lt;timestamp&gt;-&lt;filename&gt;" in same directory.
        /// </summary>
        /// <param name="filename">The full path and filename of the data file to write</param>
        /// <param name="items">A list of root / top level KmlItems</param>
        public static void WriteFile(string filename, List <KmlItem> items)
        {
            try
            {
                string Backupname = "";
                if (File.Exists(filename))
                {
                    string dir       = Path.GetDirectoryName(filename) + @"\";
                    string name      = Path.GetFileNameWithoutExtension(filename);
                    string ext       = Path.GetExtension(filename);
                    string timestamp = string.Format("{0:yyyyMMddHHmmss}", DateTime.Now);
                    Backupname = dir + "zKMLBACKUP" + timestamp + "-" + name + ext;
                    File.Move(filename, Backupname);
                }

                // Explicit setting UTF8 doesn't look the same, if I compare loaded and saved with WinMerge
                // StreamWriter file = new StreamWriter(Filename, false, Encoding.UTF8);
                StreamWriter file = new StreamWriter(filename);
                try
                {
                    foreach (KmlItem item in items)
                    {
                        WriteFile(file, item, 0);
                    }
                    file.Close();
                }
                catch (Exception e)
                {
                    file.Close();
                    if (Backupname.Length > 0)
                    {
                        File.Delete(filename);
                        File.Move(Backupname, filename);
                    }
                    throw e;
                }
            }
            catch (Exception e)
            {
                DlgMessage.Show("Error saving to " + filename + "\n\n" + e.Message);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Parse a KSP persistence file and return a list of the root nodes.
        /// In general there may be more than one item not containing other items.
        /// With correct KSP persistence data this doesn't happen and the list will
        /// usually contain just one item.
        /// </summary>
        /// <param name="filename">The full path and filename of the data file to read</param>
        /// <returns>A list of root / top level KmlItems</returns>
        public static List <KmlItem> ParseFile(string filename)
        {
            List <KmlItem> list = new List <KmlItem>();

            try
            {
                // Explicit setting UTF8 doesn't look the same, if I compare loaded and saved with MinMerge
                // StreamReader file = new StreamReader(Filename, Encoding.UTF8);
                StreamReader file = new StreamReader(filename);
                list.AddRange(ParseFile(file));
                file.Close();
                CallFinalize(list);
            }
            catch (Exception e)
            {
                DlgMessage.Show("Error loading from " + filename + "\n\n" + e.Message);
            }

            return(list);
        }
Ejemplo n.º 9
0
        private void AttribPasteBefore_Click(object sender, RoutedEventArgs e)
        {
            KmlAttrib attrib = ((sender as MenuItem).DataContext as KmlAttrib);

            if (attrib.Parent != null)
            {
                var textNode = Clipboard.GetText(TextDataFormat.UnicodeText);

                var items = KmlItem.ParseItems(new StringReader(textNode)).Where(i => i is KmlAttrib).ToList();

                if (!items.Any())
                {
                    DlgMessage.Show("Can not paste attribute from clipboard", "Paste attribute", Icons.Warning);
                }

                attrib.Parent.InsertBeforeRange(attrib, items);
            }
            else
            {
                DlgMessage.Show("Can not insert, attribute has no parent", "Paste attribute", Icons.Warning);
            }
        }