Subversion Repositories vs2008

Rev

Blame | Last modification | View Log | RSS feed

using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.OneNote;
using System.Xml;

namespace com.jamiehill {
    public class JPHOneNoteUtilities {
        private ApplicationClass oneNote = null;
        //private static string oneNoteNamespace = "http://schemas.microsoft.com/office/onenote/12/2004/onenote";
        private static string oneNoteNamespace = "http://schemas.microsoft.com/office/onenote/2007/onenote";
        private Hashtable nodeSet = new Hashtable();
        
        public JPHOneNoteUtilities() {
            oneNote = new ApplicationClass();
            nodeSet["Notebook"] = "one:Notebook";
            nodeSet["Folder"] = "one:Folder";
            nodeSet["Section"] = "one:Section";
            nodeSet["Page"] = "one:Page";
        }

        /*
        ~JPHOneNoteUtilities() {
            oneNote = null;
        }
        */

        public string GetOneNoteNamespace() {
            return (oneNoteNamespace);
        }

        /* Recursive parser.  I haven't gotten around to building a data structure to hold the information.  
         * This would make implementing an XML browser a piece of cake */
        /*
        public void GetOneNoteTree(bool refresh) {
            if (refresh || oneNoteTree.Count == 0) {
                if (oneNoteTree.Count != 0) {
                    try {
                        oneNoteTree.Clear();
                    }  catch { 
                        throw new SystemException("GetOneNoteTree clear failed: "+ e);
                    }
                }

                InitializeOneNoteTree();
            }
        }

        private void InitializeOneNoteTree() {
            string xml = null;
            this.GetPages(out xml);
            
            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            LoadXmlDoc(ref xml, out xmlDoc, out nsmgr);

            XmlNodeList nl = xmlDoc.GetElementsByTagName("one:Notebook");

            RecurseOneNoteTree(nl[0], 0);
        }

        private static void RecurseOneNoteTree(XmlNode n, int level) {
            if (n == null) return;

            string indent = null;
            for (int i = 0; i < level; i++) indent += "  ";
         
            OneNoteXMLObject onxo = GetOneNoteXMLAttributes(n);
            //System.Console.WriteLine(onxo);
         
            string name = (n.Name == null) ? "Untitled page" : n.Name;
            string value = (n.Attributes.GetNamedItem("name") == null) ? "Untitled" : n.Attributes.GetNamedItem("name").Value;
            System.Console.WriteLine(indent + (level+1) + ": " + value + " -> " + name + " -> " + n.HasChildNodes);

            if (n.HasChildNodes) {
                RecurseOneNoteTree(n.FirstChild, level+1);
                RecurseOneNoteTree(n.NextSibling, level);
            } else {
                XmlNode n_next = n.NextSibling;
                XmlNode n_last = null;
                while (true) {
                    if (n_next == null || n_next == n_last) break;
                    n_last = n_next;

                    OneNoteXMLObject onxo1 = GetOneNoteXMLAttributes(n);
                    
                    string name1 = (n_next.Name == null) ? "Untitled page" : n_next.Name;
                    string value1 = (n_next.Attributes.GetNamedItem("name") == null) ? "Untitled" : n_next.Attributes.GetNamedItem("name").Value;
                    System.Console.WriteLine(indent + (level+1) + ": " + value1 + " -> " + name1 + " -> " + n_next.HasChildNodes);
                    
                    n_next = n_next.NextSibling;
                }
            }
        }
        */

        public static OneNoteXMLObject[] GetOneNoteXMLAttributes(XmlNodeList nl) {
            int count = nl.Count;
            OneNoteXMLObject[] onxo = new OneNoteXMLObject[count];

            for (int i=0; i<count; i++) {
                onxo[i] = GetOneNoteXMLAttributes(nl[i]);
            }

            return (onxo);
        }

        public static OneNoteXMLObject GetOneNoteXMLAttributes(XmlNode n) {
            OneNoteXMLObject onxo = new OneNoteXMLObject(null, null, null, null, null, null, null, false);
            if (n.Attributes.GetNamedItem("ID") != null) onxo.id = n.Attributes.GetNamedItem("ID").Value;
            if (n.Attributes.GetNamedItem("name") != null) onxo.name = n.Attributes.GetNamedItem("name").Value;
            if (n.Attributes.GetNamedItem("nickname") != null) 
                onxo.nickname = n.Attributes.GetNamedItem("nickname").Value;
            if (n.Attributes.GetNamedItem("path") != null) onxo.path = n.Attributes.GetNamedItem("path").Value;
            if (n.Attributes.GetNamedItem("lastModifiedTime") != null) 
                onxo.timestamp = n.Attributes.GetNamedItem("lastModifiedTime").Value;
            if (n.Attributes.GetNamedItem("color") != null) onxo.color = n.Attributes.GetNamedItem("color").Value;
            if (n.Attributes.GetNamedItem("isCurrentlyViewed") != null)
                onxo.currentlyViewed = (string.Compare(n.Attributes.GetNamedItem("isCurrentlyViewed").Value, "true", true) == 0) ? true : false;
            if (n.Name != null) onxo.type = n.Name;

            return (onxo);
        }

        public string GetNodeSet(string ns) {
            return ((string)nodeSet[ns]);
        }

        public OneNoteXMLObject CreatePage(ref OneNoteXMLObject onxo) {
            string id = null;

            try {
                oneNote.CreateNewPage(onxo.id, out id, NewPageStyle.npsBlankPageWithTitle);
            } catch (Exception e) {
                throw new SystemException("Create page failed: "+ e);
            }

            OneNoteXMLObject nonxo = GetOneNoteXMLObjectById(GetNodeSet("Page"), ref id);

            return (nonxo);
        }

        public void UpdatePage(string oneNoteXML) {
            try {
                oneNote.UpdatePageContent(oneNoteXML, System.DateTime.MinValue);
            } catch (Exception e) {
                throw new SystemException("Update page failed: " + e);
            }
        }

        public void DeletePage(ref OneNoteXMLObject onxo) {
            try {
                oneNote.DeleteHierarchy(onxo.id, System.DateTime.MinValue);
            } catch (Exception e) {
                throw new SystemException("Delete page failed: " + e);
            }
        }

        public OneNoteXMLObject GetOneNoteXMLObjectForActivePage() {
            string oneNoteXML = null;

            GetPages(out oneNoteXML);

            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            LoadXmlDoc(ref oneNoteXML, out xmlDoc, out nsmgr);

            //XmlElement xmlActivePage = (XmlElement)xmlDoc.SelectSingleNode("//one:Page[@isCurrentlyViewed=\"true\"]", nsmgr);
            XmlNode n = xmlDoc.SelectSingleNode("//one:Page[@isCurrentlyViewed=\"true\"]", nsmgr);
            
            return (GetOneNoteXMLAttributes(n));
        }

        public OneNoteXMLObject[] GetOneNoteXMLObjectsByPage(string ns, ref string name) {
            string oneNoteXML = null;

            GetPages(out oneNoteXML);

            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            LoadXmlDoc(ref oneNoteXML, out xmlDoc, out nsmgr);

            //XmlNode n = xmlDoc.SelectSingleNode("//" + ns + "[@name='" + name + "']", nsmgr);
            XmlNodeList nl = xmlDoc.SelectNodes("//" + ns + "[@name='" + name + "']", nsmgr);

            return (GetOneNoteXMLAttributes(nl));
        }

        public OneNoteXMLObject GetOneNoteXMLObjectByPage(string ns, ref string name) {
            string oneNoteXML = null;

            GetPages(out oneNoteXML);

            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            LoadXmlDoc(ref oneNoteXML, out xmlDoc, out nsmgr);

            XmlNode n = xmlDoc.SelectSingleNode("//" + ns + "[@name='" + name + "']", nsmgr);

            return (GetOneNoteXMLAttributes(n));
        }

        public string GetPageContent(string id) {
            return(GetPageContent(id, PageInfo.piBinaryData));
        }

        public string GetPageContent(string id, PageInfo pi) {
            string oneNoteXML = null;
            oneNote.GetPageContent(id, out oneNoteXML, pi);

            /*
            StreamWriter os = new StreamWriter(@"c:\pagecontent.xml", false);
            os.Write(oneNoteXML);
            os.Close();
            System.Console.WriteLine(oneNoteXML);
            */

            return (oneNoteXML);
        }

        public OneNoteXMLObject GetOneNoteXMLObjectById(string ns, ref string id) {
            string oneNoteXML = null;

            GetPages(out oneNoteXML);

            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            LoadXmlDoc(ref oneNoteXML, out xmlDoc, out nsmgr);

            XmlNode n = xmlDoc.SelectSingleNode("//"+ns+"[@ID='" + id + "']", nsmgr);

            return (GetOneNoteXMLAttributes(n));
        }

        public OneNoteXMLObject GetUnfiledNotesSection() {
            string path = null;
            string oneNoteXML = null;
            oneNote.GetSpecialLocation(SpecialLocation.slUnfiledNotesSection, out path);

            GetSections(out oneNoteXML);

            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            LoadXmlDoc(ref oneNoteXML, out xmlDoc, out nsmgr);

            XmlNode n = xmlDoc.SelectSingleNode("//one:Section[@path='" + path + "']", nsmgr);
            
            return (GetOneNoteXMLAttributes(n));
        }

        public string GetDefaultNotebookPath() {
            string path = null;
            oneNote.GetSpecialLocation(SpecialLocation.slDefaultNotebookFolder, out path);
            return (path);
        }

        public void GetNotebooks(out string oneNoteXML) {
            oneNote.GetHierarchy(null, HierarchyScope.hsNotebooks, out oneNoteXML);
        }

        public void GetSections(out string oneNoteXML) {
            oneNote.GetHierarchy(null, HierarchyScope.hsSections, out oneNoteXML);
        }

        public void GetPages(out string oneNoteXML) {
            oneNote.GetHierarchy(null, HierarchyScope.hsPages, out oneNoteXML);
        }

        public void LoadXmlDoc(ref string xml, out XmlDocument xd, out XmlNamespaceManager nsmgr) {
            xd = new XmlDocument();
            xd.LoadXml(xml);
            nsmgr = new XmlNamespaceManager(xd.NameTable);
            nsmgr.AddNamespace("one", this.GetOneNoteNamespace());
        }

        public void GetNotebookNames(out string[] notebookNames) {
            string xml = null;
            this.GetNotebooks(out xml);

            XmlDocument xmlDoc = null;
            XmlNamespaceManager nsmgr = null;
            LoadXmlDoc(ref xml, out xmlDoc, out nsmgr);

            XmlNodeList nl = xmlDoc.GetElementsByTagName("one:Notebook");

            int nl_len = nl.Count;
            notebookNames = new string[nl_len];
            for (int i = 0; i < nl_len; i++) {
                notebookNames[i] = nl[i].Attributes.GetNamedItem("name").Value;
            }
        }
    }

    public struct OneNoteXMLObject {
        private string xID;
        private string xType;
        private string xName;
        private string xNickname;
        private string xPath;
        private string xTimestamp;
        private string xColor;
        private bool xCurrentlyViewed;
        
        public OneNoteXMLObject(string xid, string xtype, string xname, string xnn, string xpath, string xts, string xcolor, bool xcv) {
            xID = (xid != null) ? xid : null;
            xType = (xid != null) ? xtype : null;
            xName = (xname != null) ? xname : null;
            xNickname = (xnn != null) ? xnn : null;
            xPath = (xpath != null) ? xpath : null;
            xTimestamp = (xts != null) ? xts : null;
            xColor = (xcolor != null) ? xcolor : null;
            xCurrentlyViewed = xcv; // (string.Compare(xcv, "true", true) == 0 || string.Compare(xcv, "1") == 0) ? true : false;
        }

        public string id {
            get {
                return xID;
            }
            set {
                xID = value;
            }
        }

        public string type {
            get {
                return xType;
            }
            set {
                xType = value;
            }
        }

        public string name {
            get {
                return xName;
            }
            set {
                xName = value;
            }
        }

        public string nickname {
            get {
                return xNickname;
            }
            set {
                xNickname = value;
            }
        }

        public string path {
            get {
                return xPath;
            }
            set {
                xPath = value;
            }
        }

        public string timestamp {
            get {
                return xTimestamp;
            }
            set {
                xTimestamp = value;
            }
        }

        public string color {
            get {
                return xColor;
            }
            set {
                xColor = value;
            }
        }

        public bool currentlyViewed {
            get {
                return xCurrentlyViewed;
            }
            set {
                xCurrentlyViewed = value;
            }
        }

        public override string ToString() {
            return (String.Format("OneNoteXMLObject: {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}", 
                xType, xName, xNickname, xID, xPath, xTimestamp, xColor, xCurrentlyViewed.ToString()));
        }
    }

    public struct XY {
        private int xx;
        private int yy;

        public XY(int x, int y) {
            xx = x;
            yy = y;
        }

        public int x {
            get {
                return xx;
            }
            set {
                xx = value;
            }
        }

        public int y {
            get {
                return yy;
            }
            set {
                yy = value;
            }
        }

        public override string ToString() {
            return (String.Format("Width: {0} x Height: {1}", xx, yy));
        }
    }
}