branch: externals/calibre
commit 7aa4c8d3dd667e7d0c055cb5523037080db4db77
Author: Kjartan Oli Agustsson <[email protected]>
Commit: Kjartan Oli Agustsson <[email protected]>

    Add ability to transfer books to external device
    
    * calibre-device.el: New file.
    (calibre-device--process-buffer): New constant.
    (calibre-device-send-book): New function.
    (calibre-device-send-books): New function.
    (calibre-device-eject): New function.
    * calibre.el (calibre-ebook-device-executable): New user option.
    * doc/calibre.texi (Interacting with external reading devices): New
      section.
---
 calibre-device.el | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 calibre.el        |  5 ++++
 doc/calibre.texi  | 16 ++++++++++++
 3 files changed, 98 insertions(+)

diff --git a/calibre-device.el b/calibre-device.el
new file mode 100644
index 0000000000..464569a810
--- /dev/null
+++ b/calibre-device.el
@@ -0,0 +1,77 @@
+;;; calibre-device.el --- Interact with external reading devices  -*- 
lexical-binding: t; -*-
+
+;; Copyright (C) 2024  Free Software Foundation, Inc.
+
+;; This file is part of calibre.el.
+
+;; calibre.el is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; calibre.el is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with calibre.el.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;; This file file contains the infrastructure to interact with external
+;; reading devices, such as transfering books to those devices.
+
+;;; Code:
+(require 'calibre-book)
+(require 'calibre-core)
+
+(defconst calibre-device--process-buffer "*ebook-device*"
+  "The name of the buffer containing output from ebook-device processes.")
+
+(defun calibre-device-send-book (book &optional force)
+  "Transfer BOOK to an external device.
+
+Overwrite an existing file if FORCE is non-nil."
+  (let* ((library-path (calibre-book--file book
+                                           (calibre-book--pick-format book)))
+         (name (file-relative-name
+                library-path
+               (expand-file-name (calibre-book-path book)
+                                 (calibre--library))))
+         (device-path
+          (file-name-concat "dev:/"
+                            (car (calibre-book-authors book))
+                            name)))
+    (make-process
+     :name "ebook-device - transfer"
+     :command `(,calibre-ebook-device-executable
+                "cp" ,@(if force '("--force") nil) ,library-path ,device-path)
+     :buffer (get-buffer-create calibre-device--process-buffer)
+     :sentinel (lambda (_ event)
+                 (unless (string= event "finished\n")
+                   (message "Error transfering \"%s\" to device" 
(calibre-book-title book)))))))
+
+(defun calibre-device-send-books (books &optional force)
+  "Transfer BOOKS to an external device.
+
+Overwrite existing files if FORCE is non-nil."
+  (interactive (list (calibre--get-active-books)
+                     current-prefix-arg)
+               calibre-librar-mode)
+  (dolist (book books)
+    (calibre-device-send-book book force)))
+
+(defun calibre-device-eject ()
+  "Eject an external device."
+  (interactive)
+  (make-process
+   :name "ebook-device - eject"
+   :command `(,calibre-ebook-device-executable "eject")
+   :buffer (get-buffer-create calibre-device--process-buffer)
+   :sentinel (lambda (_ event)
+               (if (string= event "finished\n")
+                   (message "Device ejected")
+                 (message "Error ejecting device")))))
+
+(provide 'calibre-device)
+;;; calibre-device.el ends here
diff --git a/calibre.el b/calibre.el
index ad6a55145d..875ef2b1eb 100644
--- a/calibre.el
+++ b/calibre.el
@@ -39,6 +39,11 @@
   :type 'string
   :package-version '("calibre" . "1.0.4"))
 
+(defcustom calibre-ebook-device-executable "ebook-device"
+  "The ebook-device executable to use."
+  :type 'string
+  :package-version '("calibre" . "1.5.0"))
+
 (defcustom calibre-libraries nil
   "An alist mapping library names to directories."
   :type '(repeat :tag "Libraries" (cons :tag "Library"
diff --git a/doc/calibre.texi b/doc/calibre.texi
index 998d6bede2..721c67ffe9 100644
--- a/doc/calibre.texi
+++ b/doc/calibre.texi
@@ -47,6 +47,7 @@ This manual is for calibre.el (version @value{VERSION},
 * Getting started::
 * Viewing your library::
 * Modifying your library::
+* Interacting with external reading devices::
 * GNU Free Documentation License::
 * Index::
 @end menu
@@ -404,6 +405,21 @@ will mark the book for removal.  Once you have marked all 
the books you
 wish to remove, press @kbd{x} (@code{calibre-library-execute}) to remove
 them.
 
+@node Interacting with external reading devices
+@chapter Interacting with external reading devices
+Calibre has the ability to interact with external reading devices,
+allowing to manage the books stored on these devices.  calibre.el now
+supports a limited subset of this functionality in the form of the
+ability to transfer books from your library to an external device.
+
+To do this call @code{calibre-device-send-books} which will transfer all
+marked books, or the book at point if no books are marked, to an
+external device.  The transfer is performed asynchronously so it will
+not block Emacs but it can take some time to complete.
+
+Once the transfer is complete call @code{calibre-device-eject} to safely
+eject the external device before unplugging it from your computer.
+
 @include fdl.texi
 
 @node Index

Reply via email to