Code cleanup and stability sprint. Also, more robust search for calibre.

This commit is contained in:
Storm Dragon
2026-02-27 13:50:58 -05:00
parent 2cfb01549b
commit b5f1ec4bed
6 changed files with 324 additions and 99 deletions
+53 -8
View File
@@ -91,6 +91,20 @@ class BookmarkManager:
bookPath = str(Path(bookPath).resolve())
return hashlib.sha256(bookPath.encode()).hexdigest()[:16]
def _get_unique_named_bookmark_name(self, cursor, bookId, baseName):
"""Return an unused bookmark name for a given book."""
candidateName = baseName
suffixIndex = 2
while True:
cursor.execute('''
SELECT 1 FROM named_bookmarks
WHERE book_id = ? AND name = ?
''', (bookId, candidateName))
if cursor.fetchone() is None:
return candidateName
candidateName = f"{baseName} ({suffixIndex})"
suffixIndex += 1
def save_bookmark(self, bookPath, bookTitle, chapterIndex, paragraphIndex, sentenceIndex=0, audioPosition=0.0):
"""
Save bookmark for a book
@@ -322,13 +336,23 @@ class BookmarkManager:
row = cursor.fetchone()
if row:
bookmarkId = row[0]
cursor.execute('''
UPDATE named_bookmarks
SET name = ?, chapter_index = ?, paragraph_index = ?, audio_position = ?,
"server_created_at" = ?, created_at = ?
WHERE id = ?
''', (name, chapterIndex, paragraphIndex, audioPosition,
serverCreatedAt, timestamp, bookmarkId))
try:
cursor.execute('''
UPDATE named_bookmarks
SET name = ?, chapter_index = ?, paragraph_index = ?, audio_position = ?,
"server_created_at" = ?, created_at = ?
WHERE id = ?
''', (name, chapterIndex, paragraphIndex, audioPosition,
serverCreatedAt, timestamp, bookmarkId))
except sqlite3.IntegrityError:
# Keep the existing local name if requested server name collides.
cursor.execute('''
UPDATE named_bookmarks
SET chapter_index = ?, paragraph_index = ?, audio_position = ?,
"server_created_at" = ?, created_at = ?
WHERE id = ?
''', (chapterIndex, paragraphIndex, audioPosition,
serverCreatedAt, timestamp, bookmarkId))
conn.commit()
return bookmarkId
@@ -345,11 +369,32 @@ class BookmarkManager:
return cursor.lastrowid
cursor.execute('''
SELECT id FROM named_bookmarks
SELECT id, server_library_item_id, server_time FROM named_bookmarks
WHERE book_id = ? AND name = ?
''', (bookId, name))
row = cursor.fetchone()
bookmarkId = row[0] if row else None
existingServerItemId = row[1] if row else None
existingServerTime = row[2] if row else None
hasServerIdentity = serverLibraryItemId is not None and serverTime is not None
if bookmarkId and hasServerIdentity:
sameServerBookmark = (
existingServerItemId == serverLibraryItemId and
existingServerTime == serverTime
)
if not sameServerBookmark:
uniqueName = self._get_unique_named_bookmark_name(cursor, bookId, name)
cursor.execute('''
INSERT INTO named_bookmarks
(book_id, name, chapter_index, paragraph_index, audio_position, created_at,
server_library_item_id, server_time, server_created_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (bookId, uniqueName, chapterIndex, paragraphIndex, audioPosition, timestamp,
serverLibraryItemId, serverTime, serverCreatedAt))
conn.commit()
return cursor.lastrowid
if bookmarkId:
cursor.execute('''
UPDATE named_bookmarks