Skip to content

Commit 487c161

Browse files
committed
Refactor Main.cs and OneNoteApplication.cs
1 parent e92c22c commit 487c161

File tree

3 files changed

+109
-169
lines changed

3 files changed

+109
-169
lines changed

Flow.Launcher.Plugin.OneNote/Main.cs

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Threading;
54
using System.Threading.Tasks;
65
using Odotocodot.OneNote.Linq;
@@ -29,13 +28,13 @@ public void OnVisibilityChanged(object _, VisibilityChangedEventArgs e)
2928
{
3029
if (context.CurrentPluginMetadata.Disabled || !e.IsVisible)
3130
{
32-
OneNoteApplication.ReleaseComInstance();
31+
OneNoteApplication.ReleaseComObject();
3332
}
3433
}
3534

3635
private static async Task OneNoteInitAsync(CancellationToken token = default)
3736
{
38-
if (semaphore.CurrentCount == 0 || OneNoteApplication.HasComInstance)
37+
if (semaphore.CurrentCount == 0 || OneNoteApplication.HasComObject)
3938
return;
4039

4140
await semaphore.WaitAsync(token);
@@ -45,64 +44,9 @@ private static async Task OneNoteInitAsync(CancellationToken token = default)
4544
public async Task<List<Result>> QueryAsync(Query query, CancellationToken token)
4645
{
4746
var init = OneNoteInitAsync(token);
47+
4848
if (string.IsNullOrEmpty(query.Search))
49-
{
50-
return new List<Result>
51-
{
52-
new Result
53-
{
54-
Title = "Search OneNote pages",
55-
SubTitle = $"Type \"{settings.Keywords.NotebookExplorer}\" or select this option to search by notebook structure ",
56-
AutoCompleteText = $"{query.ActionKeyword} {settings.Keywords.NotebookExplorer}",
57-
IcoPath = Icons.Logo,
58-
Score = 2000,
59-
Action = c =>
60-
{
61-
context.API.ChangeQuery($"{query.ActionKeyword} {settings.Keywords.NotebookExplorer}");
62-
return false;
63-
},
64-
},
65-
new Result
66-
{
67-
Title = "See recent pages",
68-
SubTitle = $"Type \"{settings.Keywords.RecentPages}\" or select this option to see recently modified pages",
69-
AutoCompleteText = $"{query.ActionKeyword} {settings.Keywords.RecentPages}",
70-
IcoPath = Icons.Recent,
71-
Score = -1000,
72-
Action = c =>
73-
{
74-
context.API.ChangeQuery($"{query.ActionKeyword} {settings.Keywords.RecentPages}");
75-
return false;
76-
},
77-
},
78-
new Result
79-
{
80-
Title = "New quick note",
81-
IcoPath = Icons.NewPage,
82-
Score = -4000,
83-
Action = c =>
84-
{
85-
OneNoteApplication.CreateQuickNote();
86-
return true;
87-
}
88-
},
89-
new Result
90-
{
91-
Title = "Open and sync notebooks",
92-
IcoPath = Icons.Sync,
93-
Score = int.MinValue,
94-
Action = c =>
95-
{
96-
foreach (var notebook in OneNoteApplication.GetNotebooks())
97-
{
98-
OneNoteApplication.SyncItem(notebook);
99-
}
100-
OneNoteApplication.GetNotebooks().First().OpenInOneNote();
101-
return true;
102-
}
103-
},
104-
};
105-
}
49+
return searchManager.EmptyQuery();
10650

10751
await init;
10852

@@ -130,7 +74,7 @@ public void Dispose()
13074
context.API.VisibilityChanged -= OnVisibilityChanged;
13175
semaphore.Dispose();
13276
Icons.Close();
133-
OneNoteApplication.ReleaseComInstance();
77+
OneNoteApplication.ReleaseComObject();
13478
}
13579
}
13680
}

Flow.Launcher.Plugin.OneNote/SearchManager.cs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,69 @@ private void AddCreateNewOneNoteItemResults(List<Result> results, IOneNoteItem p
182182
}
183183
}
184184
#endregion
185+
186+
public List<Result> EmptyQuery()
187+
{
188+
return new List<Result>
189+
{
190+
new Result
191+
{
192+
Title = "Search OneNote pages",
193+
SubTitle = $"Type \"{settings.Keywords.NotebookExplorer}\" or select this option to search by notebook structure ",
194+
AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {settings.Keywords.NotebookExplorer}",
195+
IcoPath = Icons.Logo,
196+
Score = 2000,
197+
Action = c =>
198+
{
199+
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {settings.Keywords.NotebookExplorer}");
200+
return false;
201+
},
202+
},
203+
new Result
204+
{
205+
Title = "See recent pages",
206+
SubTitle = $"Type \"{settings.Keywords.RecentPages}\" or select this option to see recently modified pages",
207+
AutoCompleteText = $"{context.CurrentPluginMetadata.ActionKeyword} {settings.Keywords.RecentPages}",
208+
IcoPath = Icons.Recent,
209+
Score = -1000,
210+
Action = c =>
211+
{
212+
context.API.ChangeQuery($"{context.CurrentPluginMetadata.ActionKeyword} {settings.Keywords.RecentPages}");
213+
return false;
214+
},
215+
},
216+
new Result
217+
{
218+
Title = "New quick note",
219+
IcoPath = Icons.NewPage,
220+
Score = -4000,
221+
Action = c =>
222+
{
223+
OneNoteApplication.CreateQuickNote();
224+
return true;
225+
}
226+
},
227+
new Result
228+
{
229+
Title = "Open and sync notebooks",
230+
IcoPath = Icons.Sync,
231+
Score = int.MinValue,
232+
Action = c =>
233+
{
234+
foreach (var notebook in OneNoteApplication.GetNotebooks())
235+
{
236+
notebook.Sync();
237+
}
238+
OneNoteApplication.GetNotebooks()
239+
.GetPages()
240+
.OrderByDescending(pg => pg.LastModified)
241+
.First()
242+
.OpenInOneNote();
243+
return true;
244+
}
245+
},
246+
};
247+
}
185248
public List<Result> DefaultSearch(string query)
186249
{
187250
//Check for invalid start of query i.e. symbols
@@ -195,7 +258,6 @@ public List<Result> DefaultSearch(string query)
195258

196259
return ResultCreator.NoMatchesFoundResult();
197260
}
198-
199261
public List<Result> TitleSearch(string query, IEnumerable<IOneNoteItem> currentCollection, IOneNoteItem parent = null)
200262
{
201263
if (query.Length == settings.Keywords.TitleSearch.Length && parent == null)
@@ -242,7 +304,6 @@ public List<Result> RecentPages(string query)
242304
})
243305
.ToList();
244306
}
245-
246307
public List<Result> ContextMenu(Result selectedResult)
247308
{
248309
var results = new List<Result>();
@@ -288,7 +349,6 @@ private bool FuzzySearch(string itemName, string search, out List<int> highlight
288349
score = matchResult.Score;
289350
return matchResult.IsSearchPrecisionScoreMet();
290351
}
291-
292352
private bool SettingsCheck(IOneNoteItem item)
293353
{
294354
bool success = true;

Odotocodot.OneNote.Linq/OneNoteApplication.cs

Lines changed: 41 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,161 +1,97 @@
1-
using System.Collections.Generic;
2-
using System.Diagnostics;
1+
using System;
2+
using System.Collections.Generic;
33
using System.Runtime.InteropServices;
44
using System.Threading;
55
using Microsoft.Office.Interop.OneNote;
66

77
namespace Odotocodot.OneNote.Linq
88
{
99
/// <summary>
10-
/// A static wrapper class around the <see cref="Application"/> class, allowing for easy acquirement and
11-
/// release of a OneNote COM object, whilst avoiding duplicate instances. In addition to exposing the
10+
/// A static wrapper class around the <see cref="Application"/> class, allowing for <see cref="Lazy{T}">lazy</see> acquirement and
11+
/// release of a OneNote COM object. In addition to exposing the
1212
/// <see href="https://learn.microsoft.com/en-us/office/client-developer/onenote/application-interface-onenote">OneNote's API</see>
1313
/// </summary>
1414
/// <remarks>A <see cref="Application">OneNote COM object</see> is required to access any of the OneNote API.</remarks>
1515
public static class OneNoteApplication
1616
{
17-
private static Application oneNote;
18-
private static bool hasComInstance = false;
17+
private static Lazy<Application> lazyOneNote = new (() => new Application(), LazyThreadSafetyMode.ExecutionAndPublication);
18+
private static Application OneNote => lazyOneNote.Value;
1919
/// <summary>
2020
/// Indicates whether the class has a usable <see cref="Application">COM instance</see>.
2121
/// </summary>
22-
/// <remarks>When <see langword="true"/> a OneNote process should be visible in the Task Manager.</remarks>
22+
/// <remarks>When <see langword="true"/> a "Microsoft OneNote" process should be visible in the Task Manager.</remarks>
2323
/// <seealso cref="Init"/>
24-
/// <seealso cref="ReleaseComInstance"/>
25-
public static bool HasComInstance => hasComInstance;
24+
/// <seealso cref="ReleaseComObject"/>
25+
public static bool HasComObject => lazyOneNote.IsValueCreated;
2626

2727
/// <summary>
28-
/// Initialises the static class by acquiring a <see cref="Application">OneNote COM object</see>.
28+
/// Forceable initialises the static class by acquiring a <see cref="Application">OneNote COM object</see>.
2929
/// </summary>
3030
/// <exception cref="COMException">Thrown if an error occurred when trying to get the
3131
/// <see cref="Application">OneNote COM object</see> or the number of attempts in doing
3232
/// so exceeded the limit.</exception>
33-
/// <seealso cref="HasComInstance"/>
34-
/// <seealso cref="ReleaseComInstance"/>
33+
/// <seealso cref="HasComObject"/>
34+
/// <seealso cref="ReleaseComObject"/>
3535
public static void Init()
3636
{
37-
int attempt = 0;
38-
39-
while (!hasComInstance)
37+
if (!lazyOneNote.IsValueCreated)
4038
{
41-
try
42-
{
43-
oneNote = new Application();
44-
hasComInstance = oneNote != null && Marshal.IsComObject(oneNote);
45-
}
46-
catch (COMException ex) when (attempt++ < 3)
47-
{
48-
Trace.TraceError(ex.Message);
49-
Thread.Sleep(100);
50-
}
51-
catch (COMException ex) when (attempt == 3)
52-
{
53-
throw new COMException("Unable to acquire a OneNote COM object", ex);
54-
}
39+
_ = OneNote;
5540
}
5641
}
5742
/// <inheritdoc cref="OneNoteParser.GetNotebooks(IApplication)"/>
58-
public static IEnumerable<OneNoteNotebook> GetNotebooks()
59-
{
60-
Init();
61-
return OneNoteParser.GetNotebooks(oneNote);
62-
}
43+
public static IEnumerable<OneNoteNotebook> GetNotebooks() => OneNoteParser.GetNotebooks(OneNote);
44+
6345
/// <inheritdoc cref="OneNoteParser.OpenInOneNote(IApplication, IOneNoteItem)"/>
64-
public static void OpenInOneNote(IOneNoteItem item)
65-
{
66-
Init();
67-
OneNoteParser.OpenInOneNote(oneNote, item);
68-
}
46+
public static void OpenInOneNote(IOneNoteItem item) => OneNoteParser.OpenInOneNote(OneNote, item);
47+
6948
/// <inheritdoc cref="OneNoteParser.SyncItem(IApplication, IOneNoteItem)"/>
70-
public static void SyncItem(IOneNoteItem item)
71-
{
72-
Init();
73-
OneNoteParser.SyncItem(oneNote, item);
74-
}
49+
public static void SyncItem(IOneNoteItem item) => OneNoteParser.SyncItem(OneNote, item);
50+
7551
/// <inheritdoc cref="OneNoteParser.GetPageContent(IApplication, OneNotePage)"/>
76-
public static string GetPageContent(OneNotePage page)
77-
{
78-
Init();
79-
return OneNoteParser.GetPageContent(oneNote, page);
80-
}
52+
public static string GetPageContent(OneNotePage page)=> OneNoteParser.GetPageContent(OneNote, page);
8153

8254
/// <inheritdoc cref="OneNoteParser.FindPages(IApplication, string)"/>
83-
public static IEnumerable<OneNotePage> FindPages(string search)
84-
{
85-
Init();
86-
return OneNoteParser.FindPages(oneNote, search);
87-
}
55+
public static IEnumerable<OneNotePage> FindPages(string search) => OneNoteParser.FindPages(OneNote, search);
56+
8857
/// <inheritdoc cref="OneNoteParser.FindPages(IApplication, string, IOneNoteItem)"/>
89-
public static IEnumerable<OneNotePage> FindPages(IOneNoteItem scope, string search)
90-
{
91-
Init();
92-
return OneNoteParser.FindPages(oneNote, search, scope);
93-
}
58+
public static IEnumerable<OneNotePage> FindPages(IOneNoteItem scope, string search) => OneNoteParser.FindPages(OneNote, search, scope);
9459

9560
/// <inheritdoc cref="OneNoteParser.GetDefaultNotebookLocation(IApplication)"/>
96-
public static string GetDefaultNotebookLocation()
97-
{
98-
Init();
99-
return OneNoteParser.GetDefaultNotebookLocation(oneNote);
100-
}
61+
public static string GetDefaultNotebookLocation() => OneNoteParser.GetDefaultNotebookLocation(OneNote);
10162

10263
/// <inheritdoc cref="OneNoteParser.CreateQuickNote(IApplication, bool)"/>
103-
public static void CreateQuickNote()
104-
{
105-
Init();
106-
OneNoteParser.CreateQuickNote(oneNote, true);
107-
}
64+
public static void CreateQuickNote() => OneNoteParser.CreateQuickNote(OneNote, true);
10865

10966
/// <inheritdoc cref="OneNoteParser.CreatePage(IApplication, OneNoteSection, string, bool)"/>
110-
public static void CreatePage(OneNoteSection section, string pageTitle)
111-
{
112-
Init();
113-
OneNoteParser.CreatePage(oneNote, section, pageTitle, true);
114-
}
67+
public static void CreatePage(OneNoteSection section, string pageTitle) => OneNoteParser.CreatePage(OneNote, section, pageTitle, true);
68+
11569
/// <inheritdoc cref="OneNoteParser.CreateSection(IApplication, OneNoteSectionGroup, string, bool)"/>
116-
public static void CreateSection(OneNoteSectionGroup parent, string sectionName)
117-
{
118-
Init();
119-
OneNoteParser.CreateSection(oneNote, parent, sectionName, true);
120-
}
70+
public static void CreateSection(OneNoteSectionGroup parent, string sectionName) => OneNoteParser.CreateSection(OneNote, parent, sectionName, true);
71+
12172
/// <inheritdoc cref="OneNoteParser.CreateSection(IApplication, OneNoteNotebook, string, bool)"/>
122-
public static void CreateSection(OneNoteNotebook parent, string sectionName)
123-
{
124-
Init();
125-
OneNoteParser.CreateSection(oneNote, parent, sectionName, true);
126-
}
73+
public static void CreateSection(OneNoteNotebook parent, string sectionName) => OneNoteParser.CreateSection(OneNote, parent, sectionName, true);
74+
12775
/// <inheritdoc cref="OneNoteParser.CreateSectionGroup(IApplication, OneNoteSectionGroup, string, bool)"/>
128-
public static void CreateSectionGroup(OneNoteSectionGroup parent, string sectionGroupName)
129-
{
130-
Init();
131-
OneNoteParser.CreateSectionGroup(oneNote, parent, sectionGroupName, true);
132-
}
76+
public static void CreateSectionGroup(OneNoteSectionGroup parent, string sectionGroupName) => OneNoteParser.CreateSectionGroup(OneNote, parent, sectionGroupName, true);
77+
13378
/// <inheritdoc cref="OneNoteParser.CreateSectionGroup(IApplication, OneNoteNotebook, string, bool)"/>
134-
public static void CreateSectionGroup(OneNoteNotebook parent, string sectionGroupName)
135-
{
136-
Init();
137-
OneNoteParser.CreateSectionGroup(oneNote, parent, sectionGroupName, true);
138-
}
79+
public static void CreateSectionGroup(OneNoteNotebook parent, string sectionGroupName) => OneNoteParser.CreateSectionGroup(OneNote, parent, sectionGroupName, true);
13980

14081
/// <inheritdoc cref="OneNoteParser.CreateNotebook(IApplication, string, bool)"/>
141-
public static void CreateNotebook(string notebookName)
142-
{
143-
Init();
144-
OneNoteParser.CreateNotebook(oneNote, notebookName, true);
145-
}
82+
public static void CreateNotebook(string notebookName) => OneNoteParser.CreateNotebook(OneNote, notebookName, true);
14683

14784
/// <summary>
14885
/// Releases the <see cref="Application">OneNote COM object</see> freeing memory.
14986
/// </summary>
15087
/// <seealso cref="Init"/>
151-
/// <seealso cref="HasComInstance"/>
152-
public static void ReleaseComInstance()
88+
/// <seealso cref="HasComObject"/>
89+
public static void ReleaseComObject()
15390
{
154-
if (hasComInstance)
91+
if (HasComObject)
15592
{
156-
Marshal.ReleaseComObject(oneNote);
157-
oneNote = null;
158-
hasComInstance = false;
93+
Marshal.ReleaseComObject(OneNote);
94+
lazyOneNote = new(() => new Application(), LazyThreadSafetyMode.ExecutionAndPublication);
15995
}
16096
}
16197
}

0 commit comments

Comments
 (0)