Subversion Repositories vs2008

Rev

Blame | Last modification | View Log | RSS feed

using System;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using com.jamiehill;
using System.Windows.Forms;

namespace OneNoteInsertFile {
    public class OneNoteInsertFile {
        private string defaultTemplatePostfix = "templates";
        private DirectoryInfo defaultTemplateDir = null;
        private string defaultSelection = null;
        private int defaultSelectionIndex = 0;
        private string defaultFileExt = ".txt";
        private JPHOneNoteUtilities onu = null;
        private Hashtable fileInfo = new Hashtable();
        private FileInfo[] files = null;
        private XY topPosition = new XY(25, 72);
        private bool defaultChecked = false;

        public OneNoteInsertFile() {
            onu = new JPHOneNoteUtilities();
        }

        public void setDefaults(string[] defs) {
            int len = defs.Length;
            string templated = null;
            for (int i=0; i < len; i++) {
                if (defs[i].Equals("-e")) {
                    if (len >= i+1 && Regex.IsMatch(defs[i+1],@"^\.")) {
                        defaultFileExt = defs[i+1];
                    } else if (len >= i+1) {
                        defaultFileExt = "."+defs[i+1];
                    }
                } else if (defs[i].Equals("-s")) {
                    if (len >= i+1) {
                        defaultSelection = defs[i+1];
                    }
                } else if (defs[i].Equals("-t")) {
                    if (len >= i + 1) {
                        templated = defs[i + 1];
                    }
                } else if (defs[i].Equals("-c")) {
                    defaultChecked = true;
                }
            }

            DirectoryInfo di = new DirectoryInfo(templated);
            if (di.Exists) {
                defaultTemplateDir = di;
            } else {
                di = new DirectoryInfo(onu.GetDefaultNotebookPath() + @"\" + templated);
                if (di.Exists) {
                    defaultTemplateDir = di;
                } else {
                    di = new DirectoryInfo(onu.GetDefaultNotebookPath() + @"\" + defaultTemplatePostfix);
                    if (di.Exists) defaultTemplateDir = di;
                }
            }

            if (defaultTemplateDir == null) throw new Exception("Failed to find the template directory. Please make sure it is valid and exists.\nYou specify the location using the command line argument -t.");

            int k = 0;
            if (defaultTemplateDir != null) {
                files = defaultTemplateDir.GetFiles();
                Array.Sort(files, new CompareFileNameByExtension());
                if (files.Length <= 0) {
                    defaultSelectionIndex = -1;
                    defaultFileExt = null;
                    defaultSelection = null;
                } else {
                    foreach (FileInfo fi in files) {
                        Hashtable ht = new Hashtable();
                        ht["FileInfo"] = fi;
                        ht["Index"] = k;
                        fileInfo[fi.Extension.ToLower()] = (Hashtable)ht;
                        if (defaultSelection.Equals(fi.Name)) defaultSelectionIndex = k;
                        k++;
                    }
                }
            }
        }

        public bool GetCheckboxState() {
            return (defaultChecked);
        }

        public FileInfo[] GetTemplateFiles() {
            return (files);
        }

        public Hashtable GetTemplateFileInfo() {
            return (fileInfo);
        }

        public DirectoryInfo GetTemplateDir() {
            return (defaultTemplateDir);
        }

        public string GetDefaultSelection() {
            return (defaultSelection);
        }

        public int GetDefaultSelectionIndex() {
            return (defaultSelectionIndex);
        }

        public string GetDefaultFileExt() {
            return (defaultFileExt);
        }

        public void InsertFileOnActivePage(Form1 form1) {
            OneNoteXMLObject onxo = onu.GetOneNoteXMLObjectForActivePage();

            string page = onu.GetPageContent(onxo.id);

            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            onu.LoadXmlDoc(ref page, out xmlDoc, out nsmgr);

            XmlNode pn = xmlDoc.SelectSingleNode("//one:Page[@ID=\"" + onxo.id + "\"]", nsmgr);

            string filename = form1.GetFilename();
            string templateFile = form1.GetTemplateName();
            
            string temp = Environment.GetEnvironmentVariable("TEMP");
            if (temp == null || temp.Length <= 0) temp = defaultTemplateDir.FullName;

            if (filename == null || filename.Length <= 0) throw new Exception("Filename was not specified.");

            FileInfo fi = new FileInfo(temp+@"\"+filename);

            if (templateFile == null || templateFile.Length <= 0) {
                // If we get here, then we need to assume the user knows what they are doing and create the empty file.
                if (fi.Extension.Length <= 0) fi = new FileInfo(temp + @"\" + filename + defaultFileExt);
                StreamWriter os1 = new StreamWriter(fi.FullName);
                os1.Close();
            } else {
                FileInfo tfi = new FileInfo(defaultTemplateDir.FullName + @"\" + templateFile);
                if (tfi.Exists) {
                    if (fi.Extension.Length <= 0) fi = new FileInfo(temp + @"\" + filename + tfi.Extension);
                    tfi.CopyTo(fi.FullName, true);
                } else throw new Exception("Template "+tfi.Name+" does not exist.");
            }

            XmlNode xn = xmlDoc.CreateNode(XmlNodeType.Element, "one:InsertedFile", onu.GetOneNoteNamespace());

            XmlAttribute xa = xmlDoc.CreateAttribute("preferredName");
            xa.Value = fi.Name;
            xn.Attributes.Append(xa);
            
            xa = xmlDoc.CreateAttribute("pathSource");
            xa.Value = fi.FullName;
            xn.Attributes.Append(xa);

            if (form1.GetInsertLocation()) {
                XmlNode pos = xmlDoc.CreateNode(XmlNodeType.Element, "one:Position", onu.GetOneNoteNamespace());
                XmlAttribute x = xmlDoc.CreateAttribute("x");
                x.Value = topPosition.x.ToString();
                pos.Attributes.Append(x);

                x = xmlDoc.CreateAttribute("y");
                x.Value = topPosition.y.ToString();
                pos.Attributes.Append(x);

                xn.AppendChild(pos);
            }

            pn.AppendChild(xn);

            onu.UpdatePage("<?xml version=\"1.0\"?>\n" + pn.OuterXml);

            fi.Delete();
        }
    }

    class CompareFileNameByExtension : IComparer {
        public int Compare(object x, object y) {
            FileInfo f1 = (FileInfo)x;
            FileInfo f2 = (FileInfo)y;

            //return (f1.Name.CompareTo(f2.Name));
            return (f1.Extension.CompareTo(f2.Extension));
        }
    }

    class MyMain {
        static void Main(string[] args) {
            OneNoteInsertFile onif = new OneNoteInsertFile();
            DialogResult dr;

            try {
                onif.setDefaults(args);
            } catch (Exception e) {
                dr = MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Environment.Exit(0);
            }

            Form1 form1 = new Form1(onif);
            dr = form1.ShowDialog();

            if (dr.ToString().Equals("Cancel")) Environment.Exit(0);

            try {
                onif.InsertFileOnActivePage(form1);
            } catch (Exception e) {
                dr = MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                Environment.Exit(0);
            }
        }
    }
}