Mercurial > emacs
annotate lisp/net/eudcb-bbdb.el @ 87498:566965810b9e
*** empty log message ***
author | Magnus Henoch <mange@freemail.hu> |
---|---|
date | Mon, 31 Dec 2007 00:57:51 +0000 |
parents | d63d925838d5 |
children | 107ccd98fa12 53108e6cea98 |
rev | line source |
---|---|
27313 | 1 ;;; eudcb-bbdb.el --- Emacs Unified Directory Client - BBDB Backend |
2 | |
74509 | 3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, |
75347 | 4 ;; 2005, 2006, 2007 Free Software Foundation, Inc. |
27313 | 5 |
42778
6d743d659035
New maintainer. Change author's address.
Pavel Janík <Pavel@Janik.cz>
parents:
42574
diff
changeset
|
6 ;; Author: Oscar Figueiredo <oscar@cpe.fr> |
6d743d659035
New maintainer. Change author's address.
Pavel Janík <Pavel@Janik.cz>
parents:
42574
diff
changeset
|
7 ;; Maintainer: Pavel Janík <Pavel@Janik.cz> |
42574 | 8 ;; Keywords: comm |
27313 | 9 |
10 ;; This file is part of GNU Emacs. | |
11 | |
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 | |
78230
84cf1e2214c5
Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents:
75347
diff
changeset
|
14 ;; the Free Software Foundation; either version 3, or (at your option) |
27313 | 15 ;; any later version. |
16 | |
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. | |
21 | |
22 ;; You should have received a copy of the GNU General Public License | |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
64085 | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02110-1301, USA. | |
27313 | 26 |
27 ;;; Commentary: | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
28 ;; This library provides an interface to use BBDB as a backend of |
27313 | 29 ;; the Emacs Unified Directory Client. |
30 | |
31 ;;; Code: | |
32 | |
33 (require 'eudc) | |
34 (if (not (featurep 'bbdb)) | |
35 (load-library "bbdb")) | |
36 (if (not (featurep 'bbdb-com)) | |
37 (load-library "bbdb-com")) | |
38 | |
39 ;;{{{ Internal cooking | |
40 | |
41 ;; I don't like this but mapcar does not accept a parameter to the function and | |
42 ;; I don't want to use mapcar* | |
43 (defvar eudc-bbdb-current-query nil) | |
44 (defvar eudc-bbdb-current-return-attributes nil) | |
45 | |
46 (defvar eudc-bbdb-attributes-translation-alist | |
47 '((name . lastname) | |
48 (email . net) | |
49 (phone . phones)) | |
50 "Alist mapping EUDC attribute names to BBDB names.") | |
51 | |
52 (eudc-protocol-set 'eudc-query-function 'eudc-bbdb-query-internal 'bbdb) | |
53 (eudc-protocol-set 'eudc-list-attributes-function nil 'bbdb) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
54 (eudc-protocol-set 'eudc-protocol-attributes-translation-alist |
27313 | 55 'eudc-bbdb-attributes-translation-alist 'bbdb) |
56 (eudc-protocol-set 'eudc-bbdb-conversion-alist nil 'bbdb) | |
57 (eudc-protocol-set 'eudc-protocol-has-default-query-attributes nil 'bbdb) | |
58 | |
59 (defun eudc-bbdb-format-query (query) | |
60 "Format a EUDC query alist into a list suitable to `bbdb-search'." | |
61 (let* ((firstname (cdr (assq 'firstname query))) | |
62 (lastname (cdr (assq 'lastname query))) | |
63 (name (or (and firstname lastname | |
64 (concat firstname " " lastname)) | |
65 firstname | |
66 lastname)) | |
67 (company (cdr (assq 'company query))) | |
68 (net (cdr (assq 'net query))) | |
69 (notes (cdr (assq 'notes query))) | |
70 (phone (cdr (assq 'phone query)))) | |
71 (list name company net notes phone))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
72 |
27313 | 73 |
74 (defun eudc-bbdb-filter-non-matching-record (record) | |
75 "Return RECORD if it matches `eudc-bbdb-current-query', nil otherwise." | |
76 (catch 'unmatch | |
77 (progn | |
85228
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
78 (mapc |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
79 (function |
27313 | 80 (lambda (condition) |
81 (let ((attr (car condition)) | |
82 (val (cdr condition)) | |
83 (case-fold-search t) | |
84 bbdb-val) | |
85 (or (and (memq attr '(firstname lastname aka company phones addresses net)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
86 (progn |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
87 (setq bbdb-val |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
88 (eval (list (intern (concat "bbdb-record-" |
27313 | 89 (symbol-name attr))) |
90 'record))) | |
91 (if (listp bbdb-val) | |
92 (if eudc-bbdb-enable-substring-matches | |
93 (eval `(or ,@(mapcar '(lambda (subval) | |
94 (string-match val | |
95 subval)) | |
96 bbdb-val))) | |
97 (member (downcase val) | |
98 (mapcar 'downcase bbdb-val))) | |
99 (if eudc-bbdb-enable-substring-matches | |
100 (string-match val bbdb-val) | |
101 (string-equal (downcase val) (downcase bbdb-val)))))) | |
102 (throw 'unmatch nil))))) | |
103 eudc-bbdb-current-query) | |
104 record))) | |
105 | |
86816
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
106 ;; External. |
86843
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
107 (declare-function bbdb-phone-location "ext:bbdb" t) ; via bbdb-defstruct |
86816
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
108 (declare-function bbdb-phone-string "ext:bbdb" (phone)) |
86843
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
109 (declare-function bbdb-record-phones "ext:bbdb" t) ; via bbdb-defstruct |
87024
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
110 (declare-function bbdb-address-streets "ext:bbdb" t) ; via bbdb-defstruct |
86843
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
111 (declare-function bbdb-address-city "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
112 (declare-function bbdb-address-state "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
113 (declare-function bbdb-address-zip "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
114 (declare-function bbdb-address-location "ext:bbdb" t) ; via bbdb-defstruct |
9e7d6c65162b
(bbdb-phone-location, bbdb-record-phones)
Glenn Morris <rgm@gnu.org>
parents:
86816
diff
changeset
|
115 (declare-function bbdb-record-addresses "ext:bbdb" t) ; via bbdb-defstruct |
86816
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
116 (declare-function bbdb-records "ext:bbdb" |
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
117 (&optional dont-check-disk already-in-db-buffer)) |
4a5a63e12e5a
(bbdb-phone-location, bbdb-phone-string)
Glenn Morris <rgm@gnu.org>
parents:
85228
diff
changeset
|
118 |
27313 | 119 (defun eudc-bbdb-extract-phones (record) |
120 (mapcar (function | |
121 (lambda (phone) | |
122 (if eudc-bbdb-use-locations-as-attribute-names | |
123 (cons (intern (bbdb-phone-location phone)) | |
124 (bbdb-phone-string phone)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
125 (cons 'phones (format "%s: %s" |
27313 | 126 (bbdb-phone-location phone) |
127 (bbdb-phone-string phone)))))) | |
128 (bbdb-record-phones record))) | |
129 | |
130 (defun eudc-bbdb-extract-addresses (record) | |
131 (let (s c val) | |
87024
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
132 (mapcar (lambda (address) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
133 (setq c (bbdb-address-streets address)) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
134 (dotimes (n 3) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
135 (unless (zerop (length (setq s (nth n c)))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
136 (setq val (concat val s "\n")))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
137 (setq c (bbdb-address-city address) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
138 s (bbdb-address-state address)) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
139 (setq val (concat val |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
140 (if (and (> (length c) 0) (> (length s) 0)) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
141 (concat c ", " s) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
142 c) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
143 " " |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
144 (bbdb-address-zip address))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
145 (if eudc-bbdb-use-locations-as-attribute-names |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
146 (cons (intern (bbdb-address-location address)) val) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
147 (cons 'addresses (concat (bbdb-address-location address) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
148 "\n" val)))) |
d63d925838d5
(bbdb-address-streets): Declare as a function.
Glenn Morris <rgm@gnu.org>
parents:
86843
diff
changeset
|
149 (bbdb-record-addresses record)))) |
27313 | 150 |
151 (defun eudc-bbdb-format-record-as-result (record) | |
152 "Format the BBDB RECORD as a EUDC query result record. | |
153 The record is filtered according to `eudc-bbdb-current-return-attributes'" | |
154 (let ((attrs (or eudc-bbdb-current-return-attributes | |
155 '(firstname lastname aka company phones addresses net notes))) | |
156 attr | |
157 eudc-rec | |
158 val) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
159 (while (prog1 |
27313 | 160 (setq attr (car attrs)) |
161 (setq attrs (cdr attrs))) | |
162 (cond | |
163 ((eq attr 'phones) | |
164 (setq val (eudc-bbdb-extract-phones record))) | |
165 ((eq attr 'addresses) | |
166 (setq val (eudc-bbdb-extract-addresses record))) | |
167 ((memq attr '(firstname lastname aka company net notes)) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
168 (setq val (eval |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
169 (list (intern |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
170 (concat "bbdb-record-" |
27313 | 171 (symbol-name attr))) |
172 'record)))) | |
173 (t | |
174 (setq val "Unknown BBDB attribute"))) | |
175 (if val | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
176 (cond |
27313 | 177 ((memq attr '(phones addresses)) |
178 (setq eudc-rec (append val eudc-rec))) | |
179 ((and (listp val) | |
180 (= 1 (length val))) | |
181 (setq eudc-rec (cons (cons attr (car val)) eudc-rec))) | |
182 ((> (length val) 0) | |
183 (setq eudc-rec (cons (cons attr val) eudc-rec))) | |
184 (t | |
185 (error "Unexpected attribute value"))))) | |
186 (nreverse eudc-rec))) | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
187 |
27313 | 188 |
189 | |
190 (defun eudc-bbdb-query-internal (query &optional return-attrs) | |
191 "Query BBDB with QUERY. | |
49598
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
192 QUERY is a list of cons cells (ATTR . VALUE) where ATTRs should be valid |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
193 BBDB attribute names. |
0d8b17d428b5
Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents:
42778
diff
changeset
|
194 RETURN-ATTRS is a list of attributes to return, defaulting to |
27313 | 195 `eudc-default-return-attributes'." |
196 | |
197 (let ((eudc-bbdb-current-query query) | |
198 (eudc-bbdb-current-return-attributes return-attrs) | |
199 (query-attrs (eudc-bbdb-format-query query)) | |
200 bbdb-attrs | |
201 (records (bbdb-records)) | |
202 result | |
203 filtered) | |
204 ;; BBDB ORs its query attributes while EUDC ANDs them, hence we need to | |
205 ;; call bbdb-search iteratively on the returned records for each of the | |
206 ;; requested attributes | |
207 (while (and records (> (length query-attrs) 0)) | |
208 (setq bbdb-attrs (append bbdb-attrs (list (car query-attrs)))) | |
209 (if (car query-attrs) | |
210 (setq records (eval `(bbdb-search ,(quote records) ,@bbdb-attrs)))) | |
211 (setq query-attrs (cdr query-attrs))) | |
85228
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
212 (mapc (function |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
213 (lambda (record) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
214 (setq filtered (eudc-filter-duplicate-attributes record)) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
215 ;; If there were duplicate attributes reverse the order of the |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
216 ;; record so the unique attributes appear first |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
217 (if (> (length filtered) 1) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
218 (setq filtered (mapcar (function |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
219 (lambda (rec) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
220 (reverse rec))) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
221 filtered))) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
222 (setq result (append result filtered)))) |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
223 (delq nil |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
224 (mapcar 'eudc-bbdb-format-record-as-result |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
225 (delq nil |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
226 (mapcar 'eudc-bbdb-filter-non-matching-record |
631c2c083474
(eudc-bbdb-filter-non-matching-record)
Glenn Morris <rgm@gnu.org>
parents:
78230
diff
changeset
|
227 records))))) |
27313 | 228 result)) |
229 | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
230 ;;}}} |
27313 | 231 |
232 ;;{{{ High-level interfaces (interactive functions) | |
233 | |
234 (defun eudc-bbdb-set-server (dummy) | |
235 "Set the EUDC server to BBDB." | |
236 (interactive) | |
237 (eudc-set-server dummy 'bbdb) | |
238 (message "BBDB server selected")) | |
239 | |
42570
78a4068d960a
Remove unnecessary whitespaces.
Pavel Janík <Pavel@Janik.cz>
parents:
27313
diff
changeset
|
240 ;;}}} |
27313 | 241 |
242 | |
243 (eudc-register-protocol 'bbdb) | |
244 | |
245 (provide 'eudcb-bbdb) | |
246 | |
52401 | 247 ;;; arch-tag: 38276208-75de-4dbc-ba6f-8db684c32e0a |
27313 | 248 ;;; eudcb-bbdb.el ends here |