1 ;;; eudcb-bbdb.el --- Emacs Unified Directory Client - BBDB Backend
3 ;; Copyright (C) 1998-2013 Free Software Foundation, Inc.
5 ;; Author: Oscar Figueiredo <oscar@cpe.fr>
6 ;; Maintainer: Pavel JanÃk <Pavel@Janik.cz>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This library provides an interface to use BBDB as a backend of
27 ;; the Emacs Unified Directory Client.
32 (if (not (featurep 'bbdb
))
33 (load-library "bbdb"))
34 (if (not (featurep 'bbdb-com
))
35 (load-library "bbdb-com"))
37 ;;{{{ Internal cooking
39 ;; I don't like this but mapcar does not accept a parameter to the function and
40 ;; I don't want to use mapcar*
41 (defvar eudc-bbdb-current-query nil
)
42 (defvar eudc-bbdb-current-return-attributes nil
)
44 (defvar eudc-bbdb-attributes-translation-alist
48 "Alist mapping EUDC attribute names to BBDB names.")
50 (eudc-protocol-set 'eudc-query-function
'eudc-bbdb-query-internal
'bbdb
)
51 (eudc-protocol-set 'eudc-list-attributes-function nil
'bbdb
)
52 (eudc-protocol-set 'eudc-protocol-attributes-translation-alist
53 'eudc-bbdb-attributes-translation-alist
'bbdb
)
54 (eudc-protocol-set 'eudc-bbdb-conversion-alist nil
'bbdb
)
55 (eudc-protocol-set 'eudc-protocol-has-default-query-attributes nil
'bbdb
)
57 (defun eudc-bbdb-format-query (query)
58 "Format a EUDC query alist into a list suitable to `bbdb-search'."
59 (let* ((firstname (cdr (assq 'firstname query
)))
60 (lastname (cdr (assq 'lastname query
)))
61 (name (or (and firstname lastname
62 (concat firstname
" " lastname
))
65 (company (cdr (assq 'company query
)))
66 (net (cdr (assq 'net query
)))
67 (notes (cdr (assq 'notes query
)))
68 (phone (cdr (assq 'phone query
))))
69 (list name company net notes phone
)))
72 (defun eudc-bbdb-filter-non-matching-record (record)
73 "Return RECORD if it matches `eudc-bbdb-current-query', nil otherwise."
76 (dolist (condition eudc-bbdb-current-query
)
77 (let ((attr (car condition
))
81 (or (and (memq attr
'(firstname lastname aka company phones
85 (eval (list (intern (concat "bbdb-record-"
89 (if eudc-bbdb-enable-substring-matches
90 (eval `(or ,@(mapcar (lambda (subval)
91 (string-match val subval
))
93 (member (downcase val
)
94 (mapcar 'downcase bbdb-val
)))
95 (if eudc-bbdb-enable-substring-matches
96 (string-match val bbdb-val
)
97 (string-equal (downcase val
) (downcase bbdb-val
))))))
98 (throw 'unmatch nil
))))
102 (declare-function bbdb-phone-location
"ext:bbdb" t
) ; via bbdb-defstruct
103 (declare-function bbdb-phone-string
"ext:bbdb" (phone))
104 (declare-function bbdb-record-phones
"ext:bbdb" t
) ; via bbdb-defstruct
105 (declare-function bbdb-address-streets
"ext:bbdb" t
) ; via bbdb-defstruct
106 (declare-function bbdb-address-city
"ext:bbdb" t
) ; via bbdb-defstruct
107 (declare-function bbdb-address-state
"ext:bbdb" t
) ; via bbdb-defstruct
108 (declare-function bbdb-address-zip
"ext:bbdb" t
) ; via bbdb-defstruct
109 (declare-function bbdb-address-location
"ext:bbdb" t
) ; via bbdb-defstruct
110 (declare-function bbdb-record-addresses
"ext:bbdb" t
) ; via bbdb-defstruct
111 (declare-function bbdb-records
"ext:bbdb"
112 (&optional dont-check-disk already-in-db-buffer
))
114 (defun eudc-bbdb-extract-phones (record)
117 (if eudc-bbdb-use-locations-as-attribute-names
118 (cons (intern (bbdb-phone-location phone
))
119 (bbdb-phone-string phone
))
120 (cons 'phones
(format "%s: %s"
121 (bbdb-phone-location phone
)
122 (bbdb-phone-string phone
))))))
123 (bbdb-record-phones record
)))
125 (defun eudc-bbdb-extract-addresses (record)
127 (mapcar (lambda (address)
128 (setq c
(bbdb-address-streets address
))
130 (unless (zerop (length (setq s
(nth n c
))))
131 (setq val
(concat val s
"\n"))))
132 (setq c
(bbdb-address-city address
)
133 s
(bbdb-address-state address
))
134 (setq val
(concat val
135 (if (and (> (length c
) 0) (> (length s
) 0))
139 (bbdb-address-zip address
)))
140 (if eudc-bbdb-use-locations-as-attribute-names
141 (cons (intern (bbdb-address-location address
)) val
)
142 (cons 'addresses
(concat (bbdb-address-location address
)
144 (bbdb-record-addresses record
))))
146 (defun eudc-bbdb-format-record-as-result (record)
147 "Format the BBDB RECORD as a EUDC query result record.
148 The record is filtered according to `eudc-bbdb-current-return-attributes'"
149 (let ((attrs (or eudc-bbdb-current-return-attributes
150 '(firstname lastname aka company phones addresses net notes
)))
155 (setq attr
(car attrs
))
156 (setq attrs
(cdr attrs
)))
159 (setq val
(eudc-bbdb-extract-phones record
)))
160 ((eq attr
'addresses
)
161 (setq val
(eudc-bbdb-extract-addresses record
)))
162 ((memq attr
'(firstname lastname aka company net notes
))
165 (concat "bbdb-record-"
169 (error "Unknown BBDB attribute")))
171 ((or (not val
) (equal val
""))) ; do nothing
172 ((memq attr
'(phones addresses
))
173 (setq eudc-rec
(append val eudc-rec
)))
176 (setq eudc-rec
(cons (cons attr
(car val
)) eudc-rec
)))
178 (setq eudc-rec
(cons (cons attr val
) eudc-rec
)))
180 (error "Unexpected attribute value"))))
181 (nreverse eudc-rec
)))
185 (defun eudc-bbdb-query-internal (query &optional return-attrs
)
186 "Query BBDB with QUERY.
187 QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid
188 BBDB attribute names.
189 RETURN-ATTRS is a list of attributes to return, defaulting to
190 `eudc-default-return-attributes'."
192 (let ((eudc-bbdb-current-query query
)
193 (eudc-bbdb-current-return-attributes return-attrs
)
194 (query-attrs (eudc-bbdb-format-query query
))
196 (records (bbdb-records))
199 ;; BBDB ORs its query attributes while EUDC ANDs them, hence we need to
200 ;; call bbdb-search iteratively on the returned records for each of the
201 ;; requested attributes
202 (while (and records
(> (length query-attrs
) 0))
203 (setq bbdb-attrs
(append bbdb-attrs
(list (car query-attrs
))))
204 (if (car query-attrs
)
205 (setq records
(eval `(bbdb-search ,(quote records
) ,@bbdb-attrs
))))
206 (setq query-attrs
(cdr query-attrs
)))
209 (setq filtered
(eudc-filter-duplicate-attributes record
))
210 ;; If there were duplicate attributes reverse the order of the
211 ;; record so the unique attributes appear first
212 (if (> (length filtered
) 1)
213 (setq filtered
(mapcar (function
217 (setq result
(append result filtered
))))
219 (mapcar 'eudc-bbdb-format-record-as-result
221 (mapcar 'eudc-bbdb-filter-non-matching-record
227 ;;{{{ High-level interfaces (interactive functions)
229 (defun eudc-bbdb-set-server (dummy)
230 "Set the EUDC server to BBDB."
232 (eudc-set-server dummy
'bbdb
)
233 (message "BBDB server selected"))
238 (eudc-register-protocol 'bbdb
)
240 (provide 'eudcb-bbdb
)
242 ;;; eudcb-bbdb.el ends here