-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcalibredb-library.el
187 lines (165 loc) · 8.04 KB
/
calibredb-library.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
;;; calibredb-library.el --- Library for calibredb -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Damon Chan
;; Author: Damon Chan <[email protected]>
;; URL: https://github.com/chenyanming/calibredb.el
;; Keywords: tools
;; Version: 2.13.0
;; This file is NOT part of GNU Emacs.
;; This program 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.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(require 'calibredb-core)
(defvar calibredb-library-index 0)
(defvar calibredb-virtual-library-index 0)
(declare-function calibredb-ref-default-bibliography "calibredb-utils.el")
(declare-function calibredb-search-refresh-or-resume "calibredb-search.el")
(declare-function calibredb-search-keyword-filter "calibredb-search.el")
(declare-function calibredb-opds-request-page "calibredb-opds.el")
;;;###autoload
(defun calibredb-switch-library ()
"Swich Calibre Library."
(interactive)
(let* ((result (read-file-name "Quick switch library: "))
(db (concat (file-name-as-directory result) "metadata.db")))
(if (file-exists-p db)
(progn
(setq calibredb-root-dir result)
(calibredb-root-dir-quote)
(setq calibredb-db-dir (concat (file-name-as-directory calibredb-root-dir) "metadata.db"))
(calibredb-ref-default-bibliography)
(when (and
(functionp 'sqlite-available-p)
(sqlite-available-p)
(sqlitep calibredb-db-connection))
(funcall 'sqlite-close calibredb-db-connection)
(setq calibredb-db-connection nil))
(calibredb-search-refresh-or-resume))
(message "%s does not exists" db))))
;;;###autoload
(defun calibredb-library-list ()
"Switch library from variable `calibredb-library-alist'.
If under *calibredb-search* buffer, it will auto refresh after
selecting the new item."
(interactive)
(let* ((result (completing-read "Quick switch library: " calibredb-library-alist))
(db (concat (file-name-as-directory result) "metadata.db")))
(if (file-exists-p db)
(progn
(setq calibredb-root-dir result)
(calibredb-root-dir-quote)
(setq calibredb-db-dir (concat (file-name-as-directory calibredb-root-dir) "metadata.db"))
(calibredb-ref-default-bibliography)
(when (and
(functionp 'sqlite-available-p)
(sqlite-available-p)
(sqlitep calibredb-db-connection))
(funcall 'sqlite-close calibredb-db-connection)
(setq calibredb-db-connection nil))
(calibredb-search-refresh-or-resume))
(if (s-contains? "http" result)
(let ((library (-first (lambda (lib)
(s-contains? (car lib) result))
calibredb-library-alist)))
(setq calibredb-root-dir (car library))
(calibredb-opds-request-page result (nth 1 library) (nth 2 library)))
(message "INVALID LIBRARY")))))
(defun calibredb-library-previous ()
"Next library from variable `calibredb-library-alist'.
If under *calibredb-search* buffer, it will auto refresh after
selecting the new item."
(interactive)
(let* ((index (setq calibredb-library-index (if (> calibredb-library-index 0)
(1- calibredb-library-index)
(1- (length calibredb-library-alist)))))
(result (car (nth index calibredb-library-alist)))
(db (concat (file-name-as-directory result) "metadata.db")))
(if (file-exists-p db)
(progn
(setq calibredb-root-dir result)
(calibredb-root-dir-quote)
(setq calibredb-db-dir (concat (file-name-as-directory calibredb-root-dir) "metadata.db"))
(calibredb-ref-default-bibliography)
(when (and
(functionp 'sqlite-available-p)
(sqlite-available-p)
(sqlitep calibredb-db-connection))
(funcall 'sqlite-close calibredb-db-connection)
(setq calibredb-db-connection nil))
(calibredb-search-refresh-or-resume))
(message "%s does not exists" db))))
(defun calibredb-library-next ()
"Next library from variable `calibredb-library-alist'.
If under *calibredb-search* buffer, it will auto refresh after
selecting the new item."
(interactive)
(let* ((index (setq calibredb-library-index (if (< calibredb-library-index (1- (length calibredb-library-alist)))
(1+ calibredb-library-index) 0)))
(result (car (nth index calibredb-library-alist)))
(db (concat (file-name-as-directory result) "metadata.db")))
(if (file-exists-p db)
(progn
(setq calibredb-root-dir result)
(calibredb-root-dir-quote)
(setq calibredb-db-dir (concat (file-name-as-directory calibredb-root-dir) "metadata.db"))
(calibredb-ref-default-bibliography)
(when (and
(functionp 'sqlite-available-p)
(sqlite-available-p)
(sqlitep calibredb-db-connection))
(funcall 'sqlite-close calibredb-db-connection)
(setq calibredb-db-connection nil))
(calibredb-search-refresh-or-resume))
(message "%s does not exists" db))))
(defun calibredb-virtual-library-filter (keyword)
"Filter the virtual library based on KEYWORD."
(setq calibredb-virtual-library-name keyword) ; set calibredb-virtual-library-name
(setq calibredb-tag-filter-p nil)
(setq calibredb-favorite-filter-p nil)
(setq calibredb-author-filter-p nil)
(setq calibredb-date-filter-p nil)
(setq calibredb-format-filter-p nil)
(calibredb-search-keyword-filter
(cdr (assoc keyword calibredb-virtual-library-alist)))
(calibredb-search-header))
(defun calibredb-virtual-library-list ()
"List all virtual libraries."
(interactive)
(if (eq (length calibredb-virtual-library-alist) 0)
(message "No virtual libraries. Set `calibredb-virtual-library-alist' with '((name . keywords))." )
(let ((keyword (completing-read "Switch Virutal Library: " calibredb-virtual-library-alist)))
(calibredb-virtual-library-filter keyword)
(message keyword))))
(defun calibredb-virtual-library-next ()
"Swith to next virtual library."
(interactive)
(if (eq (length calibredb-virtual-library-alist) 0)
(message "No virtual libraries. Set `calibredb-virtual-library-alist' with '((name . keywords))." )
(let* ((index (setq calibredb-virtual-library-index
(if (< calibredb-virtual-library-index (1- (length calibredb-virtual-library-alist)))
(1+ calibredb-virtual-library-index) 0)))
(keyword (car (nth index calibredb-virtual-library-alist))))
(calibredb-virtual-library-filter keyword)
(message keyword))))
(defun calibredb-virtual-library-previous ()
"Swith to previous virtual library."
(interactive)
(if (eq (length calibredb-virtual-library-alist) 0)
(message "No virtual libraries. Set `calibredb-virtual-library-alist' with '((name . keywords))." )
(let* ((index (setq calibredb-virtual-library-index
(if (> calibredb-virtual-library-index 0)
(1- calibredb-virtual-library-index)
(1- (length calibredb-virtual-library-alist)))))
(keyword (car (nth index calibredb-virtual-library-alist))))
(calibredb-virtual-library-filter keyword)
(message keyword))))
(provide 'calibredb-library)
;;; calibredb-library.el ends here