Skip to content

Commit

Permalink
1189 cross machine links (stevencohn#1481)
Browse files Browse the repository at this point in the history
* custom SearchMeta
stevencohn#1189

* fallback to page if can't find paragraph
stevencohn#1189
  • Loading branch information
stevencohn authored and weissm committed Jul 29, 2024
1 parent 56284a9 commit 5abd036
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 22 deletions.
30 changes: 29 additions & 1 deletion OneMore/Commands/Tools/ShowXmlDialog.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions OneMore/Commands/Tools/ShowXmlDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public ShowXmlDialog()
});
}

functionBox.SelectedIndex = 0;
manualPanel.Location = pageOptionsPanel.Location;

((Control)manualTab).Enabled = false;
}

Expand Down Expand Up @@ -710,7 +712,8 @@ await ShowHierarchy(nbPagesBox, "one.GetNotebook(OneNote.Scope.Pages)",
break;

case 5:
enabled = false;
enabled = true;
RunManual(sender, e);
break;
}

Expand Down Expand Up @@ -813,8 +816,8 @@ private void ManualInputChanged(object sender, EventArgs e)
// validate the input...

queryButton.Enabled =
objectIdBox.Text.Trim().Length > 0 &&
functionBox.SelectedIndex >= 0;
functionBox.SelectedIndex < 3 || // allow empty for GetNotebook/GetSection/GetPage
(objectIdBox.Text.Trim().Length > 0);
}


Expand All @@ -829,15 +832,30 @@ private async void RunManual(object sender, EventArgs e)
switch (functionBox.SelectedIndex)
{
case 0:
content = await one.GetNotebook(objectIdBox.Text, OneNote.Scope.Pages);
content = await one.GetNotebook(
string.IsNullOrEmpty(objectIdBox.Text)
? one.CurrentNotebookId
: objectIdBox.Text,
OneNote.Scope.Pages);
break;

case 1:
content = await one.GetSection(objectIdBox.Text);
content = await one.GetSection(
string.IsNullOrEmpty(objectIdBox.Text)
? one.CurrentSectionId
: objectIdBox.Text);
break;

case 2:
content = (await one.GetPage(objectIdBox.Text, OneNote.PageDetail.BinaryData))?.Root;
content = (await one.GetPage(
string.IsNullOrEmpty(objectIdBox.Text)
? one.CurrentPageId
: objectIdBox.Text,
OneNote.PageDetail.BinaryData))?.Root;
break;

case 3:
content = (await one.SearchMeta(string.Empty, objectIdBox.Text, false));
break;
}

Expand Down
5 changes: 4 additions & 1 deletion OneMore/Commands/Tools/ShowXmlDialog.resx
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAABy
CwAAAk1TRnQBSQFMAgEBBgEAAYgBAgGIAQIBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
CwAAAk1TRnQBSQFMAgEBBgEAAZABAgGQAQIBEAEAARABAAT/AQkBAAj/AUIBTQE2AQQGAAE2AQQCAAEo
AwABQAMAASADAAEBAQABCAYAAQgYAAGAAgABgAMAAoABAAGAAwABgAEAAYABAAKAAgADwAEAAcAB3AHA
AQAB8AHKAaYBAAEzBQABMwEAATMBAAEzAQACMwIAAxYBAAMcAQADIgEAAykBAANVAQADTQEAA0IBAAM5
AQABgAF8Af8BAAJQAf8BAAGTAQAB1gEAAf8B7AHMAQABxgHWAe8BAAHWAucBAAGQAakBrQIAAf8BMwMA
Expand Down Expand Up @@ -179,6 +179,9 @@
<metadata name="tooltip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>142, 11</value>
</metadata>
<metadata name="tooltip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>142, 11</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>
Expand Down
70 changes: 56 additions & 14 deletions OneMore/OneNote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,35 @@ public string GetHyperlink(string pageId, string objectId)
// they are consistent on a single machine, probably using some hardware
// based heuristics I presume
logger.WriteLine("GetHyperlink, object does not exist. Possible cross-machine query");
}
else
{
logger.WriteLine("GetHyperlink error", exc);
return null;
}
}

logger.WriteLine("GetHyperlink error", exc);
return null;
// second try to target just page itself to work around cross-machine confusion
if (!string.IsNullOrEmpty(objectId))
{
try
{
onenote.GetHyperlinkToObject(pageId, string.Empty, out var hyperlink);
return hyperlink.SafeUrlEncode();
}
catch (Exception exc)
{
if (exc.HResult == ObjectDoesNotExist)
{
logger.WriteLine("GetHyperlink, object does not exist. Second try failed");
return null;
}

logger.WriteLine("GetHyperlink error", exc);
}
}

return null;
}


Expand Down Expand Up @@ -621,7 +644,7 @@ public async Task<XElement> GetNotebooks(Scope scope = Scope.Notebooks)
await InvokeWithRetry(() =>
{
onenote.GetHierarchy(
string.Empty, (HierarchyScope)scope, out var xml, XMLSchema.xs2013);
string.Empty, (HierarchyScope)scope, out var xml, XMLSchema.xs2013);

if (!string.IsNullOrEmpty(xml))
{
Expand Down Expand Up @@ -1329,18 +1352,26 @@ public async Task<XElement> SearchMeta(
{
string xml = null;

/*
* FindMeta is hardly 100% accurate. It's also not much more efficient than getting
* the hierarchy and then filtering on meta elements. So let's do that instead!
*
await InvokeWithRetry(() =>
{
onenote.FindMeta(nodeId, name, out xml, true, XMLSchema.xs2013);
});
*/

await InvokeWithRetry(() =>
{
onenote.GetHierarchy(nodeId, HierarchyScope.hsPages, out xml, XMLSchema.xs2013);
});

#pragma warning disable S2583 // Conditionally executed code should be reachable
if (string.IsNullOrWhiteSpace(xml))
{
// only case was immediately after an Office upgrade but...
return null;
}
#pragma warning restore S2583

XElement hierarchy;
try
Expand All @@ -1355,17 +1386,28 @@ await InvokeWithRetry(() =>
return null;
}

if (includeRecycleBin)
{
return hierarchy;
}

// ignore recycle bins
var ns = hierarchy.GetNamespaceOfPrefix(Prefix);

// prune tree, leaving only pages with named meta element

hierarchy.Descendants(ns + "Page")
.Where(e => !e.Elements(ns + "Meta").Attributes("name").Any(a => a.Value == name))
.Remove();

hierarchy.Descendants(ns + "Section")
.Where(e => !e.HasElements)
.Remove();

hierarchy.Descendants(ns + "SectionGroup")
.Where(e => e.Attribute("isRecycleBin") != null)
.ToList()
.ForEach(e => e.Remove());
.Where(e => !e.Descendants(ns + "Page").Any() ||
// ignore recycle bins
(!includeRecycleBin && e.Attributes("isRecycleBin").Any())
)
.Remove();

hierarchy.Elements(ns + "Notebook")
.Where(e => !e.HasElements)
.Remove();

return hierarchy;
}
Expand Down

0 comments on commit 5abd036

Please sign in to comment.