]> git.donarmstrong.com Git - dactyl.git/blob - teledactyl/content/addressbook.js
2a7dd1e5b9cdfa00279362834f7a931849cb0d4e
[dactyl.git] / teledactyl / content / addressbook.js
1 // Copyright (c) 2008 by Christian Dietrich <stettberger@dokucode.de>
2 //
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
5 "use strict";
6
7 const Addressbook = Module("addressbook", {
8     init: function () {
9     },
10
11     // TODO: add option for a format specifier, like:
12     // :set displayname=%l, %f
13     generateDisplayName: function (firstName, lastName) {
14         if (firstName && lastName)
15             return lastName + ", " + firstName;
16         else if (firstName)
17             return firstName;
18         else if (lastName)
19             return lastName;
20         else
21             return "";
22     },
23
24     getDirectoryFromURI: function (uri) services.rdf.GetResource(uri).QueryInterface(Ci.nsIAbDirectory),
25
26     add: function (address, firstName, lastName, displayName) {
27         const personalAddressbookURI = "moz-abmdbdirectory://abook.mab";
28         let directory = this.getDirectoryFromURI(personalAddressbookURI);
29         let card = Cc["@mozilla.org/addressbook/cardproperty;1"].createInstance(Ci.nsIAbCard);
30
31         if (!address || !directory || !card)
32             return false;
33
34         card.primaryEmail = address;
35         card.firstName = firstName;
36         card.lastName = lastName;
37         card.displayName = displayName;
38
39         return directory.addCard(card);
40     },
41
42     // TODO: add telephone number support
43     list: function (filter, newMail) {
44         let addresses = [];
45         let dirs = services.abManager.directories;
46         let lowerFilter = filter.toLowerCase();
47
48         while (dirs.hasMoreElements()) {
49             let addrbook = dirs.getNext().QueryInterface(Ci.nsIAbDirectory);
50             let cards = addrbook.childCards;
51             while (cards.hasMoreElements()) {
52                 let card = cards.getNext().QueryInterface(Ci.nsIAbCard);
53                 //var mail = card.primaryEmail || ""; //XXX
54                 let displayName = card.displayName;
55                 if (!displayName)
56                     displayName = this.generateDisplayName(card.firstName, card.lastName);
57
58                 if (displayName.toLowerCase().indexOf(lowerFilter) > -1
59                     || card.primaryEmail.toLowerCase().indexOf(lowerFilter) > -1)
60                         addresses.push([displayName, card.primaryEmail]);
61             }
62         }
63
64         if (addresses.length < 1) {
65             if (!filter)
66                 dactyl.echoerr("Exxx: No contacts", commandline.FORCE_SINGLELINE);
67             else
68                 dactyl.echoerr("Exxx: No contacts matching string '" + filter + "'", commandline.FORCE_SINGLELINE);
69             return false;
70         }
71
72         if (newMail) {
73             // Now we have to create a new message
74             let args = {};
75             args.to = addresses.map(
76                 function (address) "\"" + address[0].replace(/"/g, "") + " <" + address[1] + ">\""
77             ).join(", ");
78
79             mail.composeNewMail(args);
80         }
81         else {
82             let list = template.tabular(["Name", "Address"], [],
83                 [[util.clip(address[0], 50), address[1]] for ([, address] in Iterator(addresses))]
84             );
85             commandline.echo(list, commandline.HL_NORMAL, commandline.FORCE_MULTILINE);
86         }
87         return true;
88     }
89 }, {
90 }, {
91     commands: function () {
92         commands.add(["con[tact]"],
93             "Add an address book entry",
94             function (args) {
95                 let mailAddr    = args[0]; // TODO: support more than one email address
96                 let firstName   = args["-firstname"] || null;
97                 let lastName    = args["-lastname"] || null;
98                 let displayName = args["-name"] || null;
99                 if (!displayName)
100                     displayName = this.generateDisplayName(firstName, lastName);
101
102                 if (addressbook.add(mailAddr, firstName, lastName, displayName))
103                     dactyl.echomsg("Added address: " + displayName + " <" + mailAddr + ">", 1, commandline.FORCE_SINGLELINE);
104                 else
105                     dactyl.echoerr("Exxx: Could not add contact `" + mailAddr + "'", commandline.FORCE_SINGLELINE);
106
107             },
108             {
109                 argCount: "+",
110                 options: [{ names: ["-firstname", "-f"], type: CommandOption.STRING, description: "The first name of the contact"   },
111                           { names: ["-lastname", "-l"],  type: CommandOption.STRING, description: "The last name of the contact"    },
112                           { names: ["-name", "-n"],      type: CommandOption.STRING, description: "The display name of the contact" }]
113             });
114
115         commands.add(["contacts", "addr[essbook]"],
116             "List or open multiple addresses",
117             function (args) { addressbook.list(args.string, args.bang); },
118             { bang: true });
119     },
120     mappings: function () {
121         var myModes = config.mailModes;
122
123         mappings.add(myModes, ["a"],
124             "Open a prompt to save a new addressbook entry for the sender of the selected message",
125             function () {
126                 try {
127                     var to = gDBView.hdrForFirstSelectedMessage.mime2DecodedAuthor;
128                 }
129                 catch (e) {
130                     dactyl.beep();
131                 }
132
133                 if (!to)
134                     return;
135
136                 let address = to.substring(to.indexOf("<") + 1, to.indexOf(">"));
137
138                 let displayName = to.substr(0, to.indexOf("<") - 1);
139                 if (/^\S+\s+\S+\s*$/.test(displayName)) {
140                     let names = displayName.split(/\s+/);
141                     displayName = "-firstname=" + names[0].replace(/"/g, "")
142                                 + " -lastname=" + names[1].replace(/"/g, "");
143                 }
144                 else
145                     displayName = "-name=\"" + displayName.replace(/"/g, "") + "\"";
146
147                 CommandExMode().open("contact " + address + " " + displayName);
148             });
149     },
150     services: function initServices(dactyl, modules, window) {
151         services.add("abManager", "@mozilla.org/abmanager;1", Ci.nsIAbManager);
152     }
153 });
154
155 // vim: set fdm=marker sw=4 ts=4 et: