mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 21:13:23 -07:00
OneNote: changed the behavior for the default destination, we create a new page in the unfiled section. Also changed the names of the destination, included the Notebook & Section
This commit is contained in:
parent
87f4d83fd2
commit
0f63bf837f
3 changed files with 202 additions and 42 deletions
|
@ -69,7 +69,7 @@ namespace GreenshotOfficePlugin {
|
||||||
if (page == null) {
|
if (page == null) {
|
||||||
return "Microsoft OneNote";
|
return "Microsoft OneNote";
|
||||||
} else {
|
} else {
|
||||||
return page.PageName;
|
return page.DisplayName;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,21 +110,21 @@ namespace GreenshotOfficePlugin {
|
||||||
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
|
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
|
||||||
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
|
ExportInformation exportInformation = new ExportInformation(this.Designation, this.Description);
|
||||||
|
|
||||||
if (page != null) {
|
if (page == null) {
|
||||||
// No page selected, take the current
|
|
||||||
List<OneNotePage> pages = OneNoteExporter.GetPages();
|
|
||||||
if(pages == null || pages.Count == 0) {
|
|
||||||
return exportInformation;
|
|
||||||
}
|
|
||||||
page = pages[0];
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
OneNoteExporter.ExportToPage(surface, page);
|
exportInformation.ExportMade = OneNoteExporter.ExportToNewPage(surface);
|
||||||
exportInformation.ExportMade = true;
|
} catch(Exception ex) {
|
||||||
} catch (Exception ex) {
|
|
||||||
exportInformation.ErrorMessage = ex.Message;
|
exportInformation.ErrorMessage = ex.Message;
|
||||||
LOG.Error(ex);
|
LOG.Error(ex);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
exportInformation.ExportMade = OneNoteExporter.ExportToPage(surface, page);
|
||||||
|
} catch(Exception ex) {
|
||||||
|
exportInformation.ErrorMessage = ex.Message;
|
||||||
|
LOG.Error(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
return exportInformation;
|
return exportInformation;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,25 +18,16 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
|
|
||||||
using Greenshot.Interop;
|
|
||||||
using System.Reflection;
|
|
||||||
using System.Xml;
|
|
||||||
using System.IO;
|
|
||||||
using System.Drawing;
|
|
||||||
using System.Drawing.Imaging;
|
|
||||||
using Greenshot.Plugin;
|
using Greenshot.Plugin;
|
||||||
using GreenshotPlugin.Core;
|
using GreenshotPlugin.Core;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Xml;
|
||||||
|
|
||||||
namespace Greenshot.Interop.Office {
|
namespace Greenshot.Interop.Office {
|
||||||
public class OneNotePage {
|
|
||||||
public string PageName { get; set; }
|
|
||||||
public string PageID { get; set; }
|
|
||||||
public bool IsCurrentlyViewed { get; set; }
|
|
||||||
}
|
|
||||||
public class OneNoteExporter {
|
public class OneNoteExporter {
|
||||||
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(OneNoteExporter));
|
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(OneNoteExporter));
|
||||||
private const string XML_IMAGE_CONTENT = "<one:Image format=\"png\"><one:Size width=\"{1}.0\" height=\"{2}.0\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
|
private const string XML_IMAGE_CONTENT = "<one:Image format=\"png\"><one:Size width=\"{1}.0\" height=\"{2}.0\" isSetByUser=\"true\" /><one:Data>{0}</one:Data></one:Image>";
|
||||||
|
@ -44,23 +35,129 @@ namespace Greenshot.Interop.Office {
|
||||||
private const string ONENOTE_NAMESPACE_2007 = "http://schemas.microsoft.com/office/onenote/2007/onenote";
|
private const string ONENOTE_NAMESPACE_2007 = "http://schemas.microsoft.com/office/onenote/2007/onenote";
|
||||||
private const string ONENOTE_NAMESPACE_2010 = "http://schemas.microsoft.com/office/onenote/2010/onenote";
|
private const string ONENOTE_NAMESPACE_2010 = "http://schemas.microsoft.com/office/onenote/2010/onenote";
|
||||||
|
|
||||||
public static void ExportToPage(ISurface surfaceToUpload, OneNotePage page) {
|
/// <summary>
|
||||||
|
/// Create a new page in the "unfiled notes section", with the title of the capture, and export the capture there.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="surfaceToUpload">ISurface</param>
|
||||||
|
/// <returns>bool true if export worked</returns>
|
||||||
|
public static bool ExportToNewPage(ISurface surfaceToUpload) {
|
||||||
|
using(IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance<IOneNoteApplication>()) {
|
||||||
|
OneNotePage newPage = new OneNotePage();
|
||||||
|
string unfiledNotesSectionID = GetSectionID(oneNoteApplication, SpecialLocation.slUnfiledNotesSection);
|
||||||
|
if(unfiledNotesSectionID != null) {
|
||||||
|
string pageId = "";
|
||||||
|
oneNoteApplication.CreateNewPage(unfiledNotesSectionID, out pageId, NewPageStyle.npsDefault);
|
||||||
|
newPage.ID = pageId;
|
||||||
|
// Set the new name, this is automatically done in the export to page
|
||||||
|
newPage.Name = surfaceToUpload.CaptureDetails.Title;
|
||||||
|
return ExportToPage(oneNoteApplication, surfaceToUpload, newPage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Can be used to change the title of a page
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oneNoteApplication"></param>
|
||||||
|
/// <param name="pageId"></param>
|
||||||
|
/// <param name="title"></param>
|
||||||
|
private static void UpdatePageTitle(IOneNoteApplication oneNoteApplication, string pageId, string title) {
|
||||||
|
try {
|
||||||
|
string pageXML = "";
|
||||||
|
oneNoteApplication.GetPageContent(pageId, out pageXML, PageInfo.piAll, XMLSchema.xsCurrent);
|
||||||
|
XmlDocument doc = new XmlDocument();
|
||||||
|
doc.LoadXml(pageXML);
|
||||||
|
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(doc.NameTable);
|
||||||
|
namespaceManager.AddNamespace("one", ONENOTE_NAMESPACE_2010);
|
||||||
|
|
||||||
|
doc.SelectSingleNode("//one:T", namespaceManager).InnerText = title;
|
||||||
|
// Update the page
|
||||||
|
oneNoteApplication.UpdatePageContent(doc.OuterXml, DateTime.MinValue, XMLSchema.xs2010, true);
|
||||||
|
|
||||||
|
} catch(Exception ex) {
|
||||||
|
LOG.Warn("Couldn't set page title.", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Export the capture to the specified page
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="surfaceToUpload">ISurface</param>
|
||||||
|
/// <param name="page">OneNotePage</param>
|
||||||
|
/// <returns>bool true if everything worked</returns>
|
||||||
|
public static bool ExportToPage(ISurface surfaceToUpload, OneNotePage page) {
|
||||||
|
using(IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance<IOneNoteApplication>()) {
|
||||||
|
return ExportToPage(oneNoteApplication, surfaceToUpload, page);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Export the capture to the specified page
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oneNoteApplication">IOneNoteApplication</param>
|
||||||
|
/// <param name="surfaceToUpload">ISurface</param>
|
||||||
|
/// <param name="page">OneNotePage</param>
|
||||||
|
/// <returns>bool true if everything worked</returns>
|
||||||
|
private static bool ExportToPage(IOneNoteApplication oneNoteApplication, ISurface surfaceToUpload, OneNotePage page) {
|
||||||
|
if(oneNoteApplication == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
using (MemoryStream pngStream = new MemoryStream()) {
|
using (MemoryStream pngStream = new MemoryStream()) {
|
||||||
SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
|
SurfaceOutputSettings pngOutputSettings = new SurfaceOutputSettings(OutputFormat.png, 100, false);
|
||||||
ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings);
|
ImageOutput.SaveToStream(surfaceToUpload, pngStream, pngOutputSettings);
|
||||||
string base64String = Convert.ToBase64String(pngStream.GetBuffer());
|
string base64String = Convert.ToBase64String(pngStream.GetBuffer());
|
||||||
string imageXmlStr = string.Format(XML_IMAGE_CONTENT, base64String, surfaceToUpload.Image.Width, surfaceToUpload.Image.Height);
|
string imageXmlStr = string.Format(XML_IMAGE_CONTENT, base64String, surfaceToUpload.Image.Width, surfaceToUpload.Image.Height);
|
||||||
string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.PageID, ONENOTE_NAMESPACE_2010, page.PageName });
|
string pageChangesXml = string.Format(XML_OUTLINE, new object[] { imageXmlStr, page.ID, ONENOTE_NAMESPACE_2010, page.Name });
|
||||||
using (IOneNoteApplication oneNoteApplication = COMWrapper.GetOrCreateInstance<IOneNoteApplication>()) {
|
|
||||||
LOG.InfoFormat("Sending XML: {0}", pageChangesXml);
|
LOG.InfoFormat("Sending XML: {0}", pageChangesXml);
|
||||||
oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false);
|
oneNoteApplication.UpdatePageContent(pageChangesXml, DateTime.MinValue, XMLSchema.xs2010, false);
|
||||||
try {
|
try {
|
||||||
oneNoteApplication.NavigateTo(page.PageID, null, false);
|
oneNoteApplication.NavigateTo(page.ID, null, false);
|
||||||
} catch(Exception ex) {
|
} catch(Exception ex) {
|
||||||
LOG.Warn("Unable to navigate to the target page", ex);
|
LOG.Warn("Unable to navigate to the target page", ex);
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Retrieve the Section ID for the specified special location
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="oneNoteApplication"></param>
|
||||||
|
/// <param name="specialLocation">SpecialLocation</param>
|
||||||
|
/// <returns>string with section ID</returns>
|
||||||
|
private static string GetSectionID(IOneNoteApplication oneNoteApplication, SpecialLocation specialLocation) {
|
||||||
|
if(oneNoteApplication == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
string unfiledNotesPath = "";
|
||||||
|
oneNoteApplication.GetSpecialLocation(specialLocation, out unfiledNotesPath);
|
||||||
|
|
||||||
|
string notebookXml = "";
|
||||||
|
oneNoteApplication.GetHierarchy("", HierarchyScope.hsPages, out notebookXml, XMLSchema.xs2010);
|
||||||
|
if(!string.IsNullOrEmpty(notebookXml)) {
|
||||||
|
LOG.Debug(notebookXml);
|
||||||
|
StringReader reader = null;
|
||||||
|
try {
|
||||||
|
reader = new StringReader(notebookXml);
|
||||||
|
using(XmlTextReader xmlReader = new XmlTextReader(reader)) {
|
||||||
|
while(xmlReader.Read()) {
|
||||||
|
if("one:Section".Equals(xmlReader.Name)) {
|
||||||
|
string id = xmlReader.GetAttribute("ID");
|
||||||
|
string path = xmlReader.GetAttribute("path");
|
||||||
|
if(unfiledNotesPath.Equals(path)) {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if(reader != null) {
|
||||||
|
reader.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -81,16 +178,36 @@ namespace Greenshot.Interop.Office {
|
||||||
reader = new StringReader(notebookXml);
|
reader = new StringReader(notebookXml);
|
||||||
using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
|
using (XmlTextReader xmlReader = new XmlTextReader(reader)) {
|
||||||
reader = null;
|
reader = null;
|
||||||
|
OneNoteSection currentSection = null;
|
||||||
|
OneNoteNotebook currentNotebook = null;
|
||||||
while (xmlReader.Read()) {
|
while (xmlReader.Read()) {
|
||||||
|
if("one:Notebook".Equals(xmlReader.Name)) {
|
||||||
|
string id = xmlReader.GetAttribute("ID");
|
||||||
|
if(id != null && (currentNotebook == null || !id.Equals(currentNotebook.ID))) {
|
||||||
|
currentNotebook = new OneNoteNotebook();
|
||||||
|
currentNotebook.ID = xmlReader.GetAttribute("ID");
|
||||||
|
currentNotebook.Name = xmlReader.GetAttribute("name");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if("one:Section".Equals(xmlReader.Name)) {
|
||||||
|
string id = xmlReader.GetAttribute("ID");
|
||||||
|
if(id != null && (currentSection == null || !id.Equals(currentSection.ID))) {
|
||||||
|
currentSection = new OneNoteSection();
|
||||||
|
currentSection.ID = xmlReader.GetAttribute("ID");
|
||||||
|
currentSection.Name = xmlReader.GetAttribute("name");
|
||||||
|
currentSection.Parent = currentNotebook;
|
||||||
|
}
|
||||||
|
}
|
||||||
if ("one:Page".Equals(xmlReader.Name)) {
|
if ("one:Page".Equals(xmlReader.Name)) {
|
||||||
// Skip deleted items
|
// Skip deleted items
|
||||||
if("true".Equals(xmlReader.GetAttribute("isInRecycleBin"))) {
|
if("true".Equals(xmlReader.GetAttribute("isInRecycleBin"))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
OneNotePage page = new OneNotePage();
|
OneNotePage page = new OneNotePage();
|
||||||
page.PageName = xmlReader.GetAttribute("name");
|
page.Parent = currentSection;
|
||||||
page.PageID = xmlReader.GetAttribute("ID");
|
page.Name = xmlReader.GetAttribute("name");
|
||||||
if(page.PageID == null || page.PageName == null) {
|
page.ID = xmlReader.GetAttribute("ID");
|
||||||
|
if(page.ID == null || page.Name == null) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
page.IsCurrentlyViewed = "true".Equals(xmlReader.GetAttribute("isCurrentlyViewed"));
|
page.IsCurrentlyViewed = "true".Equals(xmlReader.GetAttribute("isCurrentlyViewed"));
|
||||||
|
@ -113,7 +230,7 @@ namespace Greenshot.Interop.Office {
|
||||||
if(p1.IsCurrentlyViewed || p2.IsCurrentlyViewed) {
|
if(p1.IsCurrentlyViewed || p2.IsCurrentlyViewed) {
|
||||||
return p2.IsCurrentlyViewed.CompareTo(p1.IsCurrentlyViewed);
|
return p2.IsCurrentlyViewed.CompareTo(p1.IsCurrentlyViewed);
|
||||||
}
|
}
|
||||||
return p1.PageName.CompareTo(p2.PageName);
|
return p1.DisplayName.CompareTo(p2.DisplayName);
|
||||||
});
|
});
|
||||||
return pages;
|
return pages;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Greenshot.Interop.Office {
|
namespace Greenshot.Interop.Office {
|
||||||
|
@ -27,13 +28,15 @@ namespace Greenshot.Interop.Office {
|
||||||
[ComProgId("OneNote.Application")]
|
[ComProgId("OneNote.Application")]
|
||||||
public interface IOneNoteApplication : ICommon {
|
public interface IOneNoteApplication : ICommon {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Make sure that the notebookXml is of type string, e.g. "", otherwise a type error occurs.
|
/// Make sure that the out variables are filled with a string, e.g. "", otherwise a type error occurs.
|
||||||
/// For more info on the methods: http://msdn.microsoft.com/en-us/library/gg649853.aspx
|
/// For more info on the methods: http://msdn.microsoft.com/en-us/library/gg649853.aspx
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void GetHierarchy(string startNode, HierarchyScope scope, out string notebookXml, XMLSchema schema);
|
void GetHierarchy(string startNode, HierarchyScope scope, out string notebookXml, XMLSchema schema);
|
||||||
|
void GetSpecialLocation(SpecialLocation specialLocation, out string specialLocationPath);
|
||||||
void UpdatePageContent(string pageChangesXml, DateTime dateExpectedLastModified, XMLSchema schema, bool force);
|
void UpdatePageContent(string pageChangesXml, DateTime dateExpectedLastModified, XMLSchema schema, bool force);
|
||||||
void GetPageContent(string pageId, out string pageXml, PageInfo pageInfoToExport, XMLSchema schema);
|
void GetPageContent(string pageId, out string pageXml, PageInfo pageInfoToExport, XMLSchema schema);
|
||||||
void NavigateTo(string hierarchyObjectID, string objectId, bool newWindow);
|
void NavigateTo(string hierarchyObjectID, string objectId, bool newWindow);
|
||||||
|
void CreateNewPage(string sectionID, out string pageID, NewPageStyle newPageStyle);
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum PageInfo {
|
public enum PageInfo {
|
||||||
|
@ -57,4 +60,44 @@ namespace Greenshot.Interop.Office {
|
||||||
xs2010 = 1,
|
xs2010 = 1,
|
||||||
xsCurrent= xs2010
|
xsCurrent= xs2010
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum SpecialLocation : int {
|
||||||
|
slBackupFolder = 0,
|
||||||
|
slUnfiledNotesSection = 1,
|
||||||
|
slDefaultNotebookFolder = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum NewPageStyle {
|
||||||
|
npsDefault = 0,
|
||||||
|
npsBlankPageWithTitle = 1,
|
||||||
|
npsBlankPageNoTitle = 2
|
||||||
|
}
|
||||||
|
|
||||||
|
public class OneNotePage {
|
||||||
|
public OneNoteSection Parent { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string ID { get; set; }
|
||||||
|
public bool IsCurrentlyViewed { get; set; }
|
||||||
|
public string DisplayName {
|
||||||
|
get {
|
||||||
|
OneNoteNotebook notebook = Parent.Parent;
|
||||||
|
if(string.IsNullOrEmpty(notebook.Name)) {
|
||||||
|
return string.Format("{0} / {1}", Parent.Name, Name);
|
||||||
|
} else {
|
||||||
|
return string.Format("{0} / {1} / {2}", Parent.Parent.Name, Parent.Name, Name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class OneNoteSection {
|
||||||
|
public OneNoteNotebook Parent { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string ID { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public class OneNoteNotebook {
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string ID { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue