Blame | Last modification | View Log | RSS feed
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml;
using OneNoteAddIn;
using System.Runtime.InteropServices;
using Microsoft.Office.Interop.OneNote;
using Microsoft.Win32;
using com.jamiehill;
namespace OneNoteInsertFile {
[Guid("90f74a17-351b-4d81-85a1-740d3652de8a")]
public class OneNoteInsertFile : OneNoteAddIn.IOneNoteAddIn {
/*
public bool OnEvent([In] OneNote.OneNoteAddIn_Event evt, [In] string strParameter) {
return true;
}
*/
public bool OnClick([In] string activePageId) {
//if (!gotDefaults) setDefaults();
try {
setDefaults();
} catch (Exception e) {
MessageBox.Show("setDefaults: "+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return true;
}
Form1 form1 = new Form1(this);
DialogResult dr = form1.ShowDialog();
form1.Dispose();
if (dr.ToString().Equals("Cancel")) return true;
try {
InsertFileOnActivePage(activePageId,form1);
} catch (Exception e) {
MessageBox.Show(form1.Owner,"InsertFileOnActivePage: "+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return true;
}
return true;
}
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;
private bool gotDefaults = false;
//private ApplicationClass oneNote = null;
//private static string oneNoteNamespace = "http://schemas.microsoft.com/office/onenote/2007/onenote";
public OneNoteInsertFile() {
onu = new JPHOneNoteUtilities();
}
public void setDefaults() {
RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Office\12.0\OneNote\AddIns\OneNote 2007 Insert File");
Object defaultTemplate = rk.GetValue("DefaultTemplate", "template.txt");
Object templateDirectory = rk.GetValue("TemplateDirectory", "templates");
Object cbx = rk.GetValue("InsertAtTheTop", "1");
defaultSelection = defaultTemplate.ToString();
string templated = templateDirectory.ToString();
defaultChecked = (cbx.ToString().Equals("1")) ? true : false;
DirectoryInfo di = new DirectoryInfo(templated);
if (di.Exists) {
defaultTemplateDir = di;
} else {
string path = onu.GetDefaultNotebookPath();
di = new DirectoryInfo(path + @"\" + templated);
if (di.Exists) {
defaultTemplateDir = di;
} else {
di = new DirectoryInfo(path + @"\" + 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++;
}
}
}
gotDefaults = true;
}
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(string activePageId, Form1 form1) {
//OneNoteXMLObject onxo = onu.GetOneNoteXMLObjectById(onu.GetNodeSet("Page"), ref activePageId);
string page = onu.GetPageContent(activePageId);
XmlDocument xmlDoc = null;
XmlNamespaceManager nsmgr = null;
onu.LoadXmlDoc(ref page, out xmlDoc, out nsmgr);
XmlNode pn = xmlDoc.SelectSingleNode("//one:Page[@ID=\"" + activePageId + "\"]", 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));
}
}
}