@@ -24,7 +24,11 @@ public enum CollectionType
2424 SourceCollection ,
2525 }
2626
27- public delegate BookCollection Factory ( string path , CollectionType collectionType ) ; //autofac uses this
27+ public delegate BookCollection Factory (
28+ string path ,
29+ CollectionType collectionType ,
30+ CollectionSettings collectionSettings = null
31+ ) ; //autofac uses this
2832
2933 public EventHandler CollectionChanged ;
3034
@@ -37,6 +41,8 @@ public enum CollectionType
3741 private static HashSet < string > _changingFolders = new HashSet < string > ( ) ;
3842 private BloomWebSocketServer _webSocketServer ;
3943
44+ private CollectionSettings _collectionSettings ;
45+
4046 public static event EventHandler CollectionCreated ;
4147
4248 //for moq only
@@ -53,13 +59,15 @@ public BookCollection(
5359 CollectionType collectionType ,
5460 BookSelection bookSelection ,
5561 TeamCollectionManager tcm = null ,
62+ CollectionSettings collectionSettings = null ,
5663 BloomWebSocketServer webSocketServer = null
5764 )
5865 {
5966 _path = path ;
6067 _bookSelection = bookSelection ;
6168 _tcManager = tcm ;
6269 _webSocketServer = webSocketServer ;
70+ _collectionSettings = collectionSettings ;
6371
6472 Type = collectionType ;
6573
@@ -239,11 +247,13 @@ public void DeleteBook(Book.BookInfo bookInfo)
239247 /// <param name="bookInfo"></param>
240248 public void HandleBookDeletedFromCollection ( string folderPath )
241249 {
242- var infoToDelete = _bookInfos . FirstOrDefault ( b => b . FolderPath == folderPath ) ;
243- //Debug.Assert(_bookInfos.Contains(bookInfo)); this will occur if we delete a book from the BloomLibrary section
244- if ( infoToDelete != null ) // for paranoia. We shouldn't be trying to delete a book that isn't there.
245- _bookInfos . Remove ( infoToDelete ) ;
246-
250+ lock ( _bookInfoLock )
251+ {
252+ var infoToDelete = _bookInfos . FirstOrDefault ( b => b . FolderPath == folderPath ) ;
253+ //Debug.Assert(_bookInfos.Contains(bookInfo)); this will occur if we delete a book from the BloomLibrary section
254+ if ( infoToDelete != null ) // for paranoia. We shouldn't be trying to delete a book that isn't there.
255+ _bookInfos . Remove ( infoToDelete ) ;
256+ }
247257 if ( CollectionChanged != null )
248258 CollectionChanged . Invoke ( this , null ) ;
249259 }
@@ -325,28 +335,34 @@ public string PathToDirectory
325335
326336 public void UpdateBookInfo ( BookInfo info )
327337 {
328- var oldIndex = _bookInfos . FindIndex ( i => i . Id == info . Id ) ;
329- IComparer < string > comp = new NaturalSortComparer < string > ( ) ;
330- var newKey = Path . GetFileName ( info . FolderPath ) ;
331- if ( oldIndex >= 0 )
338+ lock ( _bookInfoLock )
332339 {
333- // optimize: very often the new one will belong at the same index,
334- // if that's the case we could just replace.
335- _bookInfos . RemoveAt ( oldIndex ) ;
336- }
340+ var oldIndex = _bookInfos . FindIndex ( i => i . Id == info . Id ) ;
341+ IComparer < string > comp = new NaturalSortComparer < string > ( ) ;
342+ var newKey = Path . GetFileName ( info . FolderPath ) ;
343+ if ( oldIndex >= 0 )
344+ {
345+ // optimize: very often the new one will belong at the same index,
346+ // if that's the case we could just replace.
347+ _bookInfos . RemoveAt ( oldIndex ) ;
348+ }
337349
338- int newIndex = _bookInfos . FindIndex ( x =>
339- comp . Compare ( newKey , Path . GetFileName ( x . FolderPath ) ) <= 0
340- ) ;
341- if ( newIndex < 0 )
342- newIndex = _bookInfos . Count ;
343- _bookInfos . Insert ( newIndex , info ) ;
350+ int newIndex = _bookInfos . FindIndex ( x =>
351+ comp . Compare ( newKey , Path . GetFileName ( x . FolderPath ) ) <= 0
352+ ) ;
353+ if ( newIndex < 0 )
354+ newIndex = _bookInfos . Count ;
355+ _bookInfos . Insert ( newIndex , info ) ;
356+ }
344357 NotifyCollectionChanged ( ) ;
345358 }
346359
347360 public void AddBookInfo ( BookInfo bookInfo )
348361 {
349- _bookInfos . Add ( bookInfo ) ;
362+ lock ( _bookInfoLock )
363+ {
364+ _bookInfos . Add ( bookInfo ) ;
365+ }
350366 NotifyCollectionChanged ( ) ;
351367 }
352368
@@ -356,22 +372,25 @@ public void AddBookInfo(BookInfo bookInfo)
356372 /// <param name="bookInfo"></param>
357373 public void InsertBookInfo ( BookInfo bookInfo )
358374 {
359- IComparer < string > comparer = new NaturalSortComparer < string > ( ) ;
360- for ( int i = 0 ; i < _bookInfos . Count ; i ++ )
375+ lock ( _bookInfoLock )
361376 {
362- var compare = comparer . Compare ( _bookInfos [ i ] . FolderPath , bookInfo . FolderPath ) ;
363- if ( compare == 0 )
364- {
365- _bookInfos [ i ] = bookInfo ; // Replace
366- return ;
367- }
368- if ( compare > 0 )
377+ IComparer < string > comparer = new NaturalSortComparer < string > ( ) ;
378+ for ( int i = 0 ; i < _bookInfos . Count ; i ++ )
369379 {
370- _bookInfos . Insert ( i , bookInfo ) ;
371- return ;
380+ var compare = comparer . Compare ( _bookInfos [ i ] . FolderPath , bookInfo . FolderPath ) ;
381+ if ( compare == 0 )
382+ {
383+ _bookInfos [ i ] = bookInfo ; // Replace
384+ return ;
385+ }
386+ if ( compare > 0 )
387+ {
388+ _bookInfos . Insert ( i , bookInfo ) ;
389+ return ;
390+ }
372391 }
392+ _bookInfos . Add ( bookInfo ) ;
373393 }
374- _bookInfos . Add ( bookInfo ) ;
375394 }
376395
377396 private bool BackupFileExists ( string folderPath )
@@ -412,7 +431,10 @@ private void AddBookInfo(string folderPath)
412431 )
413432 ? _bookSelection . CurrentSelection . BookInfo
414433 : new BookInfo ( folderPath , editable , sc ) ;
415-
434+ bookInfo . ThumbnailLabel = bookInfo . GetBestDisplayTitle (
435+ _collectionSettings ,
436+ _bookSelection . CurrentSelection
437+ ) ;
416438 _bookInfos . Add ( bookInfo ) ;
417439 }
418440 catch ( Exception e )
@@ -500,7 +522,10 @@ private void WatcherOnChange(object sender, FileSystemEventArgs fileSystemEventA
500522 {
501523 if ( _watcherIsDisabled )
502524 return ;
503- _bookInfos = null ; // Possibly obsolete; next request will update it.
525+ lock ( _bookInfoLock )
526+ {
527+ _bookInfos = null ; // Possibly obsolete; next request will update it.
528+ }
504529 DebounceFolderChanged ( fileSystemEventArgs . FullPath ) ;
505530 }
506531
0 commit comments