Skip to content

Commit

Permalink
Merge pull request #5 from Odotocodot/dev
Browse files Browse the repository at this point in the history
Fix error due to encrypted sections
  • Loading branch information
Odotocodot authored Feb 28, 2023
2 parents 8079c14 + 2515438 commit 58fb275
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
53 changes: 33 additions & 20 deletions NotebookExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ public class NotebookExplorer
{
private PluginInitContext context;
private OneNotePlugin oneNotePlugin;

private IOneNoteExtNotebook LastSelectedNotebook { get => oneNotePlugin.lastSelectedNotebook; set => oneNotePlugin.lastSelectedNotebook = value; }
private IOneNoteExtSection LastSelectedSection { get => oneNotePlugin.lastSelectedSection; set => oneNotePlugin.lastSelectedSection = value; }

private ResultCreator rc;

private List<Result> NoResults => new List<Result>();

public NotebookExplorer(PluginInitContext context, OneNotePlugin oneNotePlugin, ResultCreator resultCreator)
{
this.context = context;
Expand All @@ -32,14 +38,15 @@ public List<Result> Explore(Query query)

if (string.IsNullOrWhiteSpace(searchString)) // Do a normall notebook search
{
oneNotePlugin.lastSelectedNotebook = null;
LastSelectedNotebook = null;
return OneNoteProvider.NotebookItems.Select(nb => rc.CreateNotebookResult(nb)).ToList();
}

return OneNoteProvider.NotebookItems.Where(nb =>
{
if (oneNotePlugin.lastSelectedNotebook != null && nb.ID == oneNotePlugin.lastSelectedNotebook.ID)
if (LastSelectedNotebook != null && nb.ID == LastSelectedNotebook.ID)
return true;
return TreeQuery(nb.Name, searchString, out highlightData);
})
.Select(nb => rc.CreateNotebookResult(nb, highlightData))
Expand All @@ -49,65 +56,71 @@ public List<Result> Explore(Query query)
searchString = searchStrings[2];

if (!ValidateNotebook(searchStrings[1]))
return new List<Result>();
return NoResults;

if (string.IsNullOrWhiteSpace(searchString))
{
oneNotePlugin.lastSelectedSection = null;
return oneNotePlugin.lastSelectedNotebook.Sections.Select(s => rc.CreateSectionResult(s,oneNotePlugin.lastSelectedNotebook)).ToList();
LastSelectedSection = null;
return LastSelectedNotebook.Sections.Where(s => !s.Encrypted)
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook))
.ToList();
}
return oneNotePlugin.lastSelectedNotebook.Sections.Where(s =>
return LastSelectedNotebook.Sections.Where(s =>
{
if (oneNotePlugin.lastSelectedSection != null && s.ID == oneNotePlugin.lastSelectedSection.ID)
if(s.Encrypted)
return false;
if (LastSelectedSection != null && s.ID == LastSelectedSection.ID)
return true;
return TreeQuery(s.Name, searchString, out highlightData);
})
.Select(s => rc.CreateSectionResult(s, oneNotePlugin.lastSelectedNotebook, highlightData))
.Select(s => rc.CreateSectionResult(s, LastSelectedNotebook, highlightData))
.ToList();

case 4://Searching pages in a section
searchString = searchStrings[3];

if (!ValidateNotebook(searchStrings[1]))
return new List<Result>();
return NoResults;

if (!ValidateSection(searchStrings[2]))
return new List<Result>();
return NoResults;

if (string.IsNullOrWhiteSpace(searchString))
return oneNotePlugin.lastSelectedSection.Pages.Select(pg => rc.CreatePageResult(pg,oneNotePlugin.lastSelectedSection, oneNotePlugin.lastSelectedNotebook)).ToList();
return LastSelectedSection.Pages.Select(pg => rc.CreatePageResult(pg,LastSelectedSection, LastSelectedNotebook)).ToList();

return oneNotePlugin.lastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData))
.Select(pg => rc.CreatePageResult(pg,oneNotePlugin.lastSelectedSection, oneNotePlugin.lastSelectedNotebook, highlightData))
return LastSelectedSection.Pages.Where(pg => TreeQuery(pg.Name, searchString, out highlightData))
.Select(pg => rc.CreatePageResult(pg,LastSelectedSection, LastSelectedNotebook, highlightData))
.ToList();

default:
return new List<Result>();
return NoResults;
}

}

private bool ValidateNotebook(string notebookName)
{
if (oneNotePlugin.lastSelectedNotebook == null)
if (LastSelectedNotebook == null)
{
var notebook = OneNoteProvider.NotebookItems.FirstOrDefault(nb => nb.Name == notebookName);
if (notebook == null)
return false;
oneNotePlugin.lastSelectedNotebook = notebook;
LastSelectedNotebook = notebook;
return true;
}
return true;
}

private bool ValidateSection(string sectionName)
{
if (oneNotePlugin.lastSelectedSection == null) //Check if section is valid
if (LastSelectedSection == null) //Check if section is valid
{
var section = oneNotePlugin.lastSelectedNotebook.Sections.FirstOrDefault(s => s.Name == sectionName);
if (section == null)
var section = LastSelectedNotebook.Sections.FirstOrDefault(s => s.Name == sectionName);
if (section == null || section.Encrypted)
return false;
oneNotePlugin.lastSelectedSection = section;
LastSelectedSection = section;
return true;
}
return true;
Expand Down
14 changes: 9 additions & 5 deletions ResultCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class ResultCreator
private OneNoteItemInfo notebookInfo;
private OneNoteItemInfo sectionInfo;

private IOneNoteExtNotebook LastSelectedNotebook { get => oneNotePlugin.lastSelectedNotebook; set => oneNotePlugin.lastSelectedNotebook = value; }
private IOneNoteExtSection LastSelectedSection { get => oneNotePlugin.lastSelectedSection; set => oneNotePlugin.lastSelectedSection = value; }


public ResultCreator(PluginInitContext context, OneNotePlugin oneNotePlugin)
{
this.context = context;
Expand Down Expand Up @@ -41,8 +45,8 @@ public Result CreatePageResult(IOneNotePage page, IOneNoteSection section, IOneN
TitleHighlightData = highlightingData,
Action = c =>
{
oneNotePlugin.lastSelectedNotebook = null;
oneNotePlugin.lastSelectedSection = null;
LastSelectedNotebook = null;
LastSelectedSection = null;
page.OpenInOneNote();
return true;
},
Expand All @@ -66,8 +70,8 @@ public Result CreateSectionResult(IOneNoteExtSection section, IOneNoteExtNoteboo
IcoPath = sectionInfo.GetIcon(section.Color.Value),
Action = c =>
{
oneNotePlugin.lastSelectedSection = section;
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{oneNotePlugin.lastSelectedNotebook.Name}\\{section.Name}\\");
LastSelectedSection = section;
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{LastSelectedNotebook.Name}\\{section.Name}\\");
return false;
},
};
Expand All @@ -83,7 +87,7 @@ public Result CreateNotebookResult(IOneNoteExtNotebook notebook, List<int> highl
IcoPath = notebookInfo.GetIcon(notebook.Color.Value),
Action = c =>
{
oneNotePlugin.lastSelectedNotebook = notebook;
LastSelectedNotebook = notebook;
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {Constants.StructureKeyword}{notebook.Name}\\");
return false;
},
Expand Down
2 changes: 1 addition & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Name": "OneNote",
"Description": "Search OneNote notes",
"Author": "Odotocodot",
"Version": "1.0.1",
"Version": "1.0.2",
"Language": "csharp",
"Website": "https://github.com/Odotocodot/Flow.Launcher.Plugin.OneNote",
"IcoPath": "Images/logo.png",
Expand Down

0 comments on commit 58fb275

Please sign in to comment.