branch: externals/calibre
commit 6ddfd7048ddc3e559b030acbc2569bfa86553387
Author: Kjartan Óli Ágústsson <[email protected]>
Commit: Kjartan Óli Ágústsson <[email protected]>

    Add ability to have multiple libraries
    
    * calibre-book.el (calibre-book--file): Use the location of the
    current library.
    * calibre-db.el (calibre--db): Set calibre--db to nil, and issue a
    warning if the appropriate database file does not exist.
    (calibre-db--get-books): Return nil if calibre--db returns nil,
    instead of attempting to query an invalid database.
    * calibre-library.el (calibre-library--execute): Use the location of
    the current library
    * calibre.el (calibre-library-dir): Deleted
    (calibre-libraries): Created
    (calibre--library-names): Created
    (calibre--library): Created
    (calibre-select-library): Created
---
 calibre-book.el    |  2 +-
 calibre-db.el      | 21 ++++++++++++++-------
 calibre-library.el |  2 +-
 calibre.el         | 36 ++++++++++++++++++++++++++++++------
 4 files changed, 46 insertions(+), 15 deletions(-)

diff --git a/calibre-book.el b/calibre-book.el
index 8d01a5722f..9ac46483f0 100644
--- a/calibre-book.el
+++ b/calibre-book.el
@@ -130,7 +130,7 @@ BOOK is a `calibre-book'."
   "Return the path to BOOK in FORMAT."
   (with-slots (path file-name) book
     (message "%S" file-name)
-    (file-name-concat calibre-library-dir
+    (file-name-concat (calibre--library)
                       path
                       (message "%s.%s" file-name format))))
 
diff --git a/calibre-db.el b/calibre-db.el
index 0b41d0ee93..10a00213da 100644
--- a/calibre-db.el
+++ b/calibre-db.el
@@ -28,19 +28,26 @@
 (defun calibre--db ()
   "Return the metadata database."
   (unless calibre--db
-    (setf calibre--db
-          (sqlite-open
-           (file-name-concat calibre-library-dir "metadata.db"))))
+    (let ((file-name (file-name-concat (calibre--library) "metadata.db")))
+      (if (not (file-exists-p file-name))
+          (progn
+            (display-warning 'calibre (format "Metedata database %s does not 
exist.  Add some books to the library to create it." file-name))
+            (setf calibre--db nil))
+        (setf calibre--db
+              (sqlite-open
+               file-name)))))
   calibre--db)
 
 (defun calibre-db--get-books ()
   "Return all books in the Calibre library `calibre-library-dir'."
-  (mapcar #'calibre-make-book
-          (sqlite-select (calibre--db)
-                         "SELECT books.id, title, series.name, series_index, 
timestamp, pubdate, last_modified, path
+  (if (not (calibre--db))
+      nil
+    (mapcar #'calibre-make-book
+            (sqlite-select (calibre--db)
+                           "SELECT books.id, title, series.name, series_index, 
timestamp, pubdate, last_modified, path
 FROM books
 LEFT JOIN books_series_link sl ON books.id = sl.book
-LEFT JOIN series ON sl.series = series.id;")))
+LEFT JOIN series ON sl.series = series.id;"))))
 
 (defun calibre-db--get-book-authors (id)
   "Return a list of authors for the book identified by ID."
diff --git a/calibre-library.el b/calibre-library.el
index 06db37960b..2c2979880a 100644
--- a/calibre-library.el
+++ b/calibre-library.el
@@ -75,7 +75,7 @@ ARGS should be a list of strings.  SENTINEL is a process 
sentinel to install."
       (error "Could not find calibredb")
     (make-process
      :name "calibre"
-     :command `("calibredb" "--with-library" ,calibre-library-dir ,@args)
+     :command `("calibredb" "--with-library" ,(calibre--library) ,@args)
      :sentinel sentinel)))
 
 (defun calibre-library-mark-remove (&optional _num)
diff --git a/calibre.el b/calibre.el
index 6b1b43decc..b1f5b1549a 100644
--- a/calibre.el
+++ b/calibre.el
@@ -64,14 +64,38 @@ column should have."
                   (integer :tag "Width")))
   :package-version '("calibre" . "0.1.0"))
 
-(defcustom calibre-library-dir "~/Documents/Calibre"
-  "The root directory of the Calibre library."
-  :type 'directory
-  :set (lambda (symbol value)
-         (set-default symbol value)
-         (setf calibre--db nil))
+(defcustom calibre-libraries nil
+  "An alist mapping library names to directories."
+  :type '(repeat :tag "Libraries" (cons :tag "Library"
+                                   (string :tag "Name")
+                                   (directory :tag "Location")))
   :package-version '("calibre" . "0.1.0"))
 
+(defun calibre--library-names ()
+  "Return a list of the names of defined libraries."
+  (mapcar #'car calibre-libraries))
+
+(defvar calibre--library nil
+  "The active library.")
+
+(defun calibre-select-library (&optional library)
+  "Prompt the user to select a library from `calibre-libraries'.
+If LIBRARY is non-nil, select that instead."
+  (interactive)
+  (setf calibre--library (if library
+                            library
+                           (completing-read "Library: " 
(calibre--library-names) nil t))
+        calibre--db nil
+        calibre--books nil)
+  (calibre-library--refresh t))
+
+(defun calibre--library ()
+  "Return the active library.
+If no library is active, prompt the user to select one."
+  (unless calibre--library
+    (calibre-select-library))
+  (alist-get calibre--library calibre-libraries nil nil #'string=))
+
 (defcustom calibre-format-preferences '(pdf epub)
   "The preference order of file formats."
   :type '(repeat symbol :tag "Format")

Reply via email to