Skip to content

Commit

Permalink
Smart scan of new notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencohn committed Oct 15, 2024
1 parent bff0c3a commit 113a06b
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 12 deletions.
22 changes: 22 additions & 0 deletions OneMore/Commands/Tagging/HashtagDialog.Designer.cs

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

35 changes: 35 additions & 0 deletions OneMore/Commands/Tagging/HashtagDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public HashtagDialog()
"checkAllLink",
"uncheckAllLink",
"scanButton",
"scanLink",
"scheduleButton",
"indexButton=word_Index",
"moveButton=word_Move",
Expand Down Expand Up @@ -79,7 +80,11 @@ public HashtagDialog()
experimental = new SettingsProvider()
.GetCollection("GeneralSheet").Get<bool>("experimental");

scanLink.Left = lastScanLabel.Left;
scanLink.Visible = false;

ShowScanTimes();
CheckForNewNotebooks();

ShowOfflineNotebooks = new SettingsProvider()
.GetCollection(SettingsKey)
Expand All @@ -98,6 +103,30 @@ public HashtagDialog()
public bool ShowOfflineNotebooks { get; private set; }


private void CheckForNewNotebooks()
{
Task.Run(async () =>
{
await using var one = new OneNote();
var books = await one.GetNotebooks();
var ns = books.GetNamespaceOfPrefix(OneNote.Prefix);

var bookIDs = books.Elements(ns + "Notebook")
.Where(e => e.Attribute("isRecycleBin") is null)
.Select(e => e.Attribute("ID").Value);

var provider = new HashtagProvider();
var known = provider.ReadKnownNotebooks();

if (bookIDs.Any(e => !known.Contains(e)))
{
lastScanLabel.Visible = false;
scanLink.Visible = true;
}
});
}


private void ShowScanTimes()
{
var scan = new HashtagProvider().ReadScanTime();
Expand Down Expand Up @@ -291,6 +320,12 @@ private void DoScheduleScan(object sender, EventArgs e)
}


private void ScanDiscovered(object sender, LinkLabelLinkClickedEventArgs e)
{
DoScheduleScan(sender, e);
}


private void Control_Checked(object sender, EventArgs e)
{
var control = sender as HashtagContextControl;
Expand Down
8 changes: 7 additions & 1 deletion OneMore/Commands/Tagging/HashtagNotebook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,11 @@ internal class HashtagNotebook
/// <summary>
/// A collection of HashtagNotebooks
/// </summary>
internal class HashtagNotebooks : List<HashtagNotebook> { }
internal class HashtagNotebooks : List<HashtagNotebook>
{
public bool Contains(string notebookID)
{
return Find(e => e.NotebookID == notebookID) is not null;
}
}
}
49 changes: 39 additions & 10 deletions OneMore/Commands/Tagging/HashtagScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class Statistics
}

public const int DefaultThrottle = 40;
private const int MaxPagesThreshold = 100;

private readonly string lastTime;
private readonly HashtagPageSannerFactory factory;
Expand Down Expand Up @@ -171,24 +172,51 @@ public async Task Scan()

var known = knownNotebooks.Find(n => n.NotebookID == notebookID);

// Filter on two levels...
// Filter on three levels...
//
// knownNotebooks
// If knownNotebooks is empty, then we assume that this is the first scan
// and will allow all; otherwise we only scan known notebooks to avoid
// pulling in large data from newly added notebooks - user must schedule
// a scan to pull in those new notebooks explicitly.
// and will allow all; otherwise only scan known notebooks, also include
// newly added notebooks that are below the size threshold to avoid causing
// OneNote to behave sluggishly - user must schedule a scan to pull in those
// new notebooks explicitly.
//
// notebookFilters
// If notebookFilters is empty then allow any notebook that has passed the
// knownNotebook test; otherwise, the user has explicitly requested a scan
// of notebooks specified in the notebookFilters list.
// of notebooks specified in the notebookFilters list or the notebook is
// within the size threshold.
//
// size
// If there are known notebooks, yet this notebook is not one of them, and
// not filtered, then check the size of the new notebook; if below the set
// threshold then scanit, otherwise user must schedule explicitly.
//

// known is empty so accept any and all
var accepted = knownNotebooks.Count == 0;
if (!accepted)
{
if (notebookFilters is null)
{
// known notebook so accept it
accepted = known is not null;
if (!accepted)
{
// notebook size is within threshold?
// this may load the notebook twice, but small cost
var populated = await one.GetNotebook(notebookID, OneNote.Scope.Pages);
accepted = populated.Descendants(ns + "Page")
.Count(e => e.Attribute("isInRecycleBin") is null) < MaxPagesThreshold;
}
}
else
{
accepted = notebookFilters.Contains(notebookID);
}
}

if (knownNotebooks.Count == 0 ||
(notebookFilters is null
? known is not null
: notebookFilters.Contains(notebookID)))
if (accepted)
{
//logger.Verbose($"scanning notebook {notebookID} \"{name}\"");

Expand Down Expand Up @@ -233,7 +261,8 @@ public async Task Scan()
int totalPages = 0;
var ns = one.GetNamespace(parent);

var sectionRefs = parent.Elements(ns + "Section")
// Descendants allows diving into SectionGroups
var sectionRefs = parent.Descendants(ns + "Section")
.Where(e =>
e.Attribute("isRecycleBin") is null &&
e.Attribute("isInRecycleBin") is null &&
Expand Down
1 change: 0 additions & 1 deletion OneMore/Commands/Tagging/HashtagService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ protected virtual async Task StartupLoop()
{
try
{
logger.Debug("StartupLoop() Scan()");
await Scan();

errors = 0;
Expand Down
9 changes: 9 additions & 0 deletions OneMore/Properties/Resources.Designer.cs

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

4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.ar-SA.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,10 @@
<value>إفحص الآن</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>تم اكتشاف دفاتر ملاحظات جديدة. انقر هنا للمسح الضوئي.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>جدولة المسح</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.de-DE.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,10 @@ Die Suche nach Hashtags ist erst möglich, wenn dies abgeschlossen ist.</value>
<value>Scanne jetzt</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>Neue Notizbücher entdeckt. Klicken Sie hier, um zu scannen.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>Planen Sie den Scan</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.es-ES.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,10 @@ La búsqueda de hashtags no estará disponible hasta que se complete.</value>
<value>Escanear ahora</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>Nuevos cuadernos descubiertos. Haga clic aquí para escanear.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>Programar escaneo</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.fr-FR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,10 @@ La recherche de hashtags n'est pas disponible tant qu'elle n'est pas terminée.<
<value>Scanne maintenant</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>Nouveaux cahiers découverts. Cliquez ici pour numériser.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>Planifier une analyse</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.he-IL.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ Total Row Font
<value>לסרוק עכשיו</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>התגלו מחברות חדשות. לחץ כאן לסריקה.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>תזמון סריקה</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.ja-JP.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,10 @@ OneNoteファイル (*.one)</value>
<value>今すぐスキャン</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>新しいノート発見。スキャンするにはここをクリックしてください。</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>スキャンをスケジュール</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.nl-NL.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ Zoeken naar hashtags is pas beschikbaar als dit is voltooid.</value>
<value>Scan nu</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>Nieuwe notitieboekjes ontdekt. Klik hier om te scannen.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>Plan scannen</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.pl-PL.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ Wyszukiwanie hashtagów nie jest możliwe, dopóki nie zostanie zakończone.</va
<value>Skanuj teraz</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>Odkryto nowe notesy. Kliknij tutaj, aby zeskanować.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>Zaplanuj skanowanie</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.pt-BR.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ A pesquisa de hashtags não estará disponível até que isso seja concluído.</
<value>Escaneie agora</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>Novos cadernos descobertos. Clique aqui para digitalizar.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>Agendar verificação</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ Searching for hashtags is unavailable until this is completed.</value>
<value>Scan Now</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>New notebooks discovered. Click here to scan.</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>Schedule Scan</value>
<comment>menu item</comment>
Expand Down
4 changes: 4 additions & 0 deletions OneMore/Properties/Resources.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,10 @@ OneNote 文件 (*.one)</value>
<value>现在扫描</value>
<comment>menu button</comment>
</data>
<data name="HashtagDialog_scanLink.Text" xml:space="preserve">
<value>发现新笔记本。点击这里扫描。</value>
<comment>link label</comment>
</data>
<data name="HashtagDialog_scheduleButton.Text" xml:space="preserve">
<value>安排扫描</value>
<comment>menu item</comment>
Expand Down

0 comments on commit 113a06b

Please sign in to comment.