1 | /* ***** BEGIN LICENSE BLOCK ***** |
---|
2 | This program is free software; you can redistribute it and/or modify |
---|
3 | it under the terms of the GNU General Public License as published by |
---|
4 | the Free Software Foundation; either version 2 of the License, or |
---|
5 | (at your option) any later version. |
---|
6 | |
---|
7 | This program is distributed in the hope that it will be useful, |
---|
8 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
10 | GNU General Public License for more details. |
---|
11 | |
---|
12 | You should have received a copy of the GNU General Public License |
---|
13 | along with this program; if not, write to the Free Software |
---|
14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
15 | |
---|
16 | The Original Code is the Virtual Identity Extension. |
---|
17 | |
---|
18 | The Initial Developer of the Original Code is Rene Ejury. |
---|
19 | Portions created by the Initial Developer are Copyright (C) 2007 |
---|
20 | the Initial Developer. All Rights Reserved. |
---|
21 | |
---|
22 | Contributor(s): |
---|
23 | * ***** END LICENSE BLOCK ***** */ |
---|
24 | |
---|
25 | var EXPORTED_SYMBOLS = ["rdfDatasource", "rdfDatasourceAccess", "rdfDatasourceImporter"] |
---|
26 | |
---|
27 | Components.utils.import("resource://v_identity/vI_log.js"); |
---|
28 | let Log = setupLogging("virtualIdentity.rdfDatasource"); |
---|
29 | |
---|
30 | Components.utils.import("resource://v_identity/vI_prefs.js"); |
---|
31 | Components.utils.import("resource://v_identity/vI_identityData.js"); |
---|
32 | Components.utils.import("resource://gre/modules/Services.jsm"); |
---|
33 | |
---|
34 | function get3PaneWindow() { |
---|
35 | return Components.classes['@mozilla.org/appshell/window-mediator;1'] |
---|
36 | .getService(Components.interfaces.nsIWindowMediator) |
---|
37 | .getMostRecentWindow("mail:3pane"); |
---|
38 | }; |
---|
39 | |
---|
40 | function rdfDatasource(rdfFileName, dontRegisterObserver) { |
---|
41 | this._rdfFileName = rdfFileName; |
---|
42 | if (this._rdfFileName) this.init(); |
---|
43 | if (!dontRegisterObserver) this.AccountManagerObserver.register(); |
---|
44 | this._extVersion = get3PaneWindow().virtualIdentityExtension.extensionVersion; |
---|
45 | } |
---|
46 | |
---|
47 | rdfDatasource.prototype = { |
---|
48 | _extVersion : null, |
---|
49 | _rdfVersion : "0.0.5", |
---|
50 | _rdfService : Components.classes["@mozilla.org/rdf/rdf-service;1"] |
---|
51 | .getService(Components.interfaces.nsIRDFService), |
---|
52 | _rdfDataSource : null, |
---|
53 | _rdfFileName : null, |
---|
54 | _rdfNS : "http://virtual-id.absorb.it/", |
---|
55 | _rdfNSStorage : "vIStorage", |
---|
56 | _rdfNSEmail : "vIStorage/email", |
---|
57 | _rdfNSMaillist : "vIStorage/maillist", |
---|
58 | _rdfNSNewsgroup : "vIStorage/newsgroup", |
---|
59 | _rdfNSFilter : "vIStorage/filter", |
---|
60 | _rdfNSAccounts : "vIAccounts", |
---|
61 | _rdfNSIdentities : "vIAccounts/id", |
---|
62 | _rdfNSSMTPservers : "vIAccounts/smtp", |
---|
63 | |
---|
64 | _virtualIdentityID : "{dddd428e-5ac8-4a81-9f78-276c734f75b8}", |
---|
65 | |
---|
66 | _emailContainer : Components.classes["@mozilla.org/rdf/container;1"] |
---|
67 | .createInstance(Components.interfaces.nsIRDFContainer), |
---|
68 | |
---|
69 | _maillistContainer : Components.classes["@mozilla.org/rdf/container;1"] |
---|
70 | .createInstance(Components.interfaces.nsIRDFContainer), |
---|
71 | |
---|
72 | _newsgroupContainer : Components.classes["@mozilla.org/rdf/container;1"] |
---|
73 | .createInstance(Components.interfaces.nsIRDFContainer), |
---|
74 | |
---|
75 | _filterContainer : Components.classes["@mozilla.org/rdf/container;1"] |
---|
76 | .createInstance(Components.interfaces.nsIRDFContainer), |
---|
77 | |
---|
78 | _identityContainer : Components.classes["@mozilla.org/rdf/container;1"] |
---|
79 | .createInstance(Components.interfaces.nsIRDFContainer), |
---|
80 | |
---|
81 | _smtpContainer : Components.classes["@mozilla.org/rdf/container;1"] |
---|
82 | .createInstance(Components.interfaces.nsIRDFContainer), |
---|
83 | |
---|
84 | getContainer : function (type) { |
---|
85 | switch (type) { |
---|
86 | case "email": return this._emailContainer; |
---|
87 | case "maillist": return this._maillistContainer; |
---|
88 | case "newsgroup": return this._newsgroupContainer; |
---|
89 | case "filter": return this._filterContainer; |
---|
90 | case "identity": return this._identityContainer; |
---|
91 | case "smtp": return this._smtpContainer; |
---|
92 | } |
---|
93 | return null; |
---|
94 | }, |
---|
95 | |
---|
96 | init: function() { |
---|
97 | // Log.debug("init.\n"); |
---|
98 | |
---|
99 | this._openRdfDataSource(); |
---|
100 | if (!this._rdfDataSource) return; |
---|
101 | this._initContainers(); |
---|
102 | if (this.rdfUpgradeRequired()) this.rdfUpgrade(); |
---|
103 | |
---|
104 | // store version everytime to recognize downgrades later |
---|
105 | this.storeRDFVersion(); |
---|
106 | |
---|
107 | // this.refreshAccountInfo(); |
---|
108 | // Log.debug("init done.\n"); |
---|
109 | }, |
---|
110 | |
---|
111 | _openRdfDataSource: function() { |
---|
112 | // if (!this._rdfFileName || this._rdfDataSource); |
---|
113 | var protoHandler = Components.classes["@mozilla.org/network/protocol;1?name=file"] |
---|
114 | .getService(Components.interfaces.nsIFileProtocolHandler) |
---|
115 | var newFile = Components.classes["@mozilla.org/file/local;1"] |
---|
116 | .createInstance(Components.interfaces.nsILocalFile); |
---|
117 | |
---|
118 | var file = Components.classes["@mozilla.org/file/directory_service;1"] |
---|
119 | .getService(Components.interfaces.nsIProperties) |
---|
120 | .get("ProfD", Components.interfaces.nsIFile); |
---|
121 | var delimiter = (file.path.match(/\\/))?"\\":"/"; |
---|
122 | |
---|
123 | newFile.initWithPath(file.path + delimiter + this._rdfFileName); |
---|
124 | var fileURI = protoHandler.newFileURI(newFile); |
---|
125 | |
---|
126 | Log.debug("init: read rdf from '" + fileURI.spec + "'\n"); |
---|
127 | |
---|
128 | this._rdfDataSource = |
---|
129 | this._rdfService.GetDataSourceBlocking(fileURI.spec); |
---|
130 | |
---|
131 | // Log.debug("read rdf from '" + fileURI.spec + "' done." + this._rdfService + "\n"); |
---|
132 | }, |
---|
133 | |
---|
134 | _initContainers: function() { |
---|
135 | try { // will possibly fail before upgrade |
---|
136 | var storageRes = this._rdfService |
---|
137 | .GetResource(this._rdfNS + this._rdfNSEmail); |
---|
138 | this._emailContainer.Init(this._rdfDataSource, storageRes); |
---|
139 | storageRes = this._rdfService |
---|
140 | .GetResource(this._rdfNS + this._rdfNSMaillist); |
---|
141 | this._maillistContainer.Init(this._rdfDataSource, storageRes); |
---|
142 | storageRes = this._rdfService |
---|
143 | .GetResource(this._rdfNS + this._rdfNSNewsgroup); |
---|
144 | this._newsgroupContainer.Init(this._rdfDataSource, storageRes); |
---|
145 | storageRes = this._rdfService |
---|
146 | .GetResource(this._rdfNS + this._rdfNSFilter); |
---|
147 | this._filterContainer.Init(this._rdfDataSource, storageRes); |
---|
148 | storageRes = this._rdfService |
---|
149 | .GetResource(this._rdfNS + this._rdfNSIdentities); |
---|
150 | this._identityContainer.Init(this._rdfDataSource, storageRes); |
---|
151 | storageRes = this._rdfService |
---|
152 | .GetResource(this._rdfNS + this._rdfNSSMTPservers); |
---|
153 | this._smtpContainer.Init(this._rdfDataSource, storageRes); |
---|
154 | } catch (e) { }; |
---|
155 | }, |
---|
156 | |
---|
157 | // ****************************************************************************************** |
---|
158 | // ************** BEGIN RDF UPGRADE CODE ********************************************** |
---|
159 | rdfUpgradeRequired: function() { |
---|
160 | var oldRdfVersion = this.getCurrentRDFFileVersion(); |
---|
161 | var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"] |
---|
162 | .getService(Components.interfaces.nsIVersionComparator); |
---|
163 | return (!oldRdfVersion || versionChecker.compare(oldRdfVersion, this._rdfVersion) < 0) |
---|
164 | }, |
---|
165 | // ************** RDF UPGRADE CODE **************************************************** |
---|
166 | extUpgradeRequired: function() { |
---|
167 | var oldExtVersion = this.getCurrentExtFileVersion() |
---|
168 | var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"] |
---|
169 | .getService(Components.interfaces.nsIVersionComparator); |
---|
170 | return (!oldExtVersion || versionChecker.compare(oldExtVersion, this._extVersion) < 0) |
---|
171 | }, |
---|
172 | // ************** RDF UPGRADE CODE **************************************************** |
---|
173 | rdfUpgrade : function() { |
---|
174 | var currentVersion = this.getCurrentRDFFileVersion(); |
---|
175 | Log.debug("checking for previous version of rdf, found " + |
---|
176 | currentVersion + "\nrdf-upgrade required.\n") |
---|
177 | switch (currentVersion) { |
---|
178 | case null: |
---|
179 | case "0.0.1": |
---|
180 | case "0.0.2": |
---|
181 | this._createRDFContainers(); // no break |
---|
182 | case "0.0.3": |
---|
183 | this._tagDefaultSMTP(); |
---|
184 | case "0.0.4": |
---|
185 | default: |
---|
186 | this._createAccountInfoContainers(); |
---|
187 | } |
---|
188 | this.storeRDFVersion(); |
---|
189 | Log.debug("rdf-upgrade to " + this.getCurrentRDFFileVersion() + " done.\n\n"); |
---|
190 | }, |
---|
191 | // ************** RDF UPGRADE CODE **************************************************** |
---|
192 | // only used for upgrade to 0.0.3 - loop through all ressources. |
---|
193 | _transferAllResources : function () { |
---|
194 | Log.debug("upgrade: transferAllResources "); |
---|
195 | var enumerator = this._rdfDataSource.GetAllResources(); |
---|
196 | while (enumerator && enumerator.hasMoreElements()) { |
---|
197 | var resource = enumerator.getNext(); |
---|
198 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
199 | |
---|
200 | var type; var name; |
---|
201 | if (resource.ValueUTF8.match(new RegExp(this._rdfNS + this._rdfNSEmail + "/", "i"))) |
---|
202 | { type = "email"; name = RegExp.rightContext } |
---|
203 | else if (resource.ValueUTF8.match(new RegExp(this._rdfNS + this._rdfNSNewsgroup + "/", "i"))) |
---|
204 | { type = "newsgroup"; name = RegExp.rightContext } |
---|
205 | else if (resource.ValueUTF8.match(new RegExp(this._rdfNS + this._rdfNSMaillist + "/", "i"))) |
---|
206 | { type = "maillist"; name = RegExp.rightContext } |
---|
207 | else continue; |
---|
208 | |
---|
209 | var container = this.getContainer(type); |
---|
210 | this._setRDFValue(resource, "name", name); |
---|
211 | |
---|
212 | if (container.IndexOf(resource) == -1) container.AppendElement(resource); |
---|
213 | |
---|
214 | Log.debug("."); |
---|
215 | } |
---|
216 | Log.debug("\n"); |
---|
217 | }, |
---|
218 | // ************** RDF UPGRADE CODE **************************************************** |
---|
219 | _tagDefaultSMTP: function() { |
---|
220 | Log.debug("upgrade: tagDefaultSMTP "); |
---|
221 | for each (treeType in Array("email", "maillist", "newsgroup", "filter")) { |
---|
222 | var enumerator = this.getContainer(treeType).GetElements(); |
---|
223 | while (enumerator && enumerator.hasMoreElements()) { |
---|
224 | var resource = enumerator.getNext(); |
---|
225 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
226 | var smtp = this._getRDFValue(resource, "smtp") |
---|
227 | if (!smtp || smtp == "") this._setRDFValue(resource, "smtp", DEFAULT_SMTP_TAG); |
---|
228 | Log.debug("."); |
---|
229 | } |
---|
230 | } |
---|
231 | Log.debug("\n"); |
---|
232 | }, |
---|
233 | // ************** RDF UPGRADE CODE **************************************************** |
---|
234 | _createAccountInfoContainers: function() { |
---|
235 | Log.debug("upgrade: createAccountInfoContainers \n"); |
---|
236 | var rdfContainerUtils = Components.classes["@mozilla.org/rdf/container-utils;1"]. |
---|
237 | getService(Components.interfaces.nsIRDFContainerUtils); |
---|
238 | |
---|
239 | var accountRes = this._rdfService |
---|
240 | .GetResource(this._rdfNS + this._rdfNSAccounts); |
---|
241 | var identityRes = this._rdfService |
---|
242 | .GetResource(this._rdfNS + this._rdfNSIdentities); |
---|
243 | var smtpRes = this._rdfService |
---|
244 | .GetResource(this._rdfNS + this._rdfNSSMTPservers); |
---|
245 | this._setRDFValue(accountRes, "name", "Accounts"); |
---|
246 | this._setRDFValue(identityRes, "name", "Identities"); |
---|
247 | this._setRDFValue(smtpRes, "name", "SMTP-Server"); |
---|
248 | |
---|
249 | rdfContainerUtils.MakeBag(this._rdfDataSource, accountRes); |
---|
250 | rdfContainerUtils.MakeBag(this._rdfDataSource, identityRes); |
---|
251 | rdfContainerUtils.MakeBag(this._rdfDataSource, smtpRes); |
---|
252 | |
---|
253 | var accountContainer = Components.classes["@mozilla.org/rdf/container;1"]. |
---|
254 | createInstance(Components.interfaces.nsIRDFContainer); |
---|
255 | |
---|
256 | // initialize container with accountRes |
---|
257 | accountContainer.Init(this._rdfDataSource, accountRes); |
---|
258 | // append all new containers to accountRes |
---|
259 | if (accountContainer.IndexOf(identityRes) == -1) accountContainer.AppendElement(identityRes); |
---|
260 | if (accountContainer.IndexOf(smtpRes) == -1) accountContainer.AppendElement(smtpRes); |
---|
261 | |
---|
262 | this._initContainers(); |
---|
263 | this.refreshAccountInfo(); |
---|
264 | }, |
---|
265 | // ************** RDF UPGRADE CODE **************************************************** |
---|
266 | _createRDFContainers: function() { |
---|
267 | Log.debug("upgrade: createRDFContainers "); |
---|
268 | var rdfContainerUtils = Components.classes["@mozilla.org/rdf/container-utils;1"]. |
---|
269 | getService(Components.interfaces.nsIRDFContainerUtils); |
---|
270 | |
---|
271 | var storageRes = this._rdfService |
---|
272 | .GetResource(this._rdfNS + this._rdfNSStorage); |
---|
273 | var emailRes = this._rdfService |
---|
274 | .GetResource(this._rdfNS + this._rdfNSEmail); |
---|
275 | var maillistRes = this._rdfService |
---|
276 | .GetResource(this._rdfNS + this._rdfNSMaillist); |
---|
277 | var newsgroupRes = this._rdfService |
---|
278 | .GetResource(this._rdfNS + this._rdfNSNewsgroup); |
---|
279 | var filterRes = this._rdfService |
---|
280 | .GetResource(this._rdfNS + this._rdfNSFilter); |
---|
281 | this._setRDFValue(emailRes, "name", "E-Mail"); |
---|
282 | this._setRDFValue(maillistRes, "name", "Mailing-List"); |
---|
283 | this._setRDFValue(newsgroupRes, "name", "Newsgroup"); |
---|
284 | this._setRDFValue(filterRes, "name", "Filter"); |
---|
285 | |
---|
286 | rdfContainerUtils.MakeBag(this._rdfDataSource, storageRes); |
---|
287 | rdfContainerUtils.MakeBag(this._rdfDataSource, emailRes); |
---|
288 | rdfContainerUtils.MakeBag(this._rdfDataSource, maillistRes); |
---|
289 | rdfContainerUtils.MakeBag(this._rdfDataSource, newsgroupRes); |
---|
290 | // use a sequence for the filters, order does matter |
---|
291 | rdfContainerUtils.MakeSeq(this._rdfDataSource, filterRes); |
---|
292 | |
---|
293 | var container = Components.classes["@mozilla.org/rdf/container;1"]. |
---|
294 | createInstance(Components.interfaces.nsIRDFContainer); |
---|
295 | |
---|
296 | // initialize container with storageRes |
---|
297 | container.Init(this._rdfDataSource, storageRes); |
---|
298 | // append all new containers to storageRes |
---|
299 | if (container.IndexOf(emailRes) == -1) container.AppendElement(emailRes); |
---|
300 | if (container.IndexOf(maillistRes) == -1) container.AppendElement(maillistRes); |
---|
301 | if (container.IndexOf(newsgroupRes) == -1) container.AppendElement(newsgroupRes); |
---|
302 | if (container.IndexOf(filterRes) == -1) container.AppendElement(filterRes); |
---|
303 | |
---|
304 | this._initContainers(); |
---|
305 | |
---|
306 | this._transferAllResources(); |
---|
307 | }, |
---|
308 | // ************** END RDF UPGRADE CODE ************************************************ |
---|
309 | // ****************************************************************************************** |
---|
310 | |
---|
311 | getCurrentRDFFileVersion: function() { |
---|
312 | return this._getRDFValue( |
---|
313 | this._rdfService.GetResource(this._rdfNS + "virtualIdentity"), "rdfVersion"); |
---|
314 | }, |
---|
315 | |
---|
316 | getCurrentExtFileVersion: function() { |
---|
317 | return this._getRDFValue( |
---|
318 | this._rdfService.GetResource(this._rdfNS + "virtualIdentity"), "version"); |
---|
319 | }, |
---|
320 | |
---|
321 | storeRDFVersion: function() { |
---|
322 | this._setRDFValue( |
---|
323 | this._rdfService.GetResource(this._rdfNS + "virtualIdentity"), "rdfVersion", |
---|
324 | this._rdfVersion); |
---|
325 | this._flush(); |
---|
326 | }, |
---|
327 | |
---|
328 | storeExtVersion: function() { |
---|
329 | this._setRDFValue( |
---|
330 | this._rdfService.GetResource(this._rdfNS + "virtualIdentity"), "version", this._extVersion) |
---|
331 | this._flush(); |
---|
332 | }, |
---|
333 | |
---|
334 | clean : function() { |
---|
335 | this.AccountManagerObserver.unregister(); |
---|
336 | this._flush(); |
---|
337 | }, |
---|
338 | |
---|
339 | _flush : function() { |
---|
340 | this._rdfDataSource.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource); |
---|
341 | this._rdfDataSource.Flush(); |
---|
342 | }, |
---|
343 | |
---|
344 | refreshAccountInfo : function() { |
---|
345 | try { // will possibly fail before upgrade |
---|
346 | this.cleanAccountInfo(); this.storeAccountInfo(); |
---|
347 | } catch (e) {}; |
---|
348 | }, |
---|
349 | |
---|
350 | cleanAccountInfo : function() { |
---|
351 | Log.debug("cleanAccountInfo\n"); |
---|
352 | |
---|
353 | var enumerator = this._identityContainer.GetElements(); |
---|
354 | while (enumerator && enumerator.hasMoreElements()) { |
---|
355 | var resource = enumerator.getNext(); |
---|
356 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
357 | this._unsetRDFValue(resource, "identityName", this._getRDFValue(resource, "identityName")) |
---|
358 | this._unsetRDFValue(resource, "fullName", this._getRDFValue(resource, "fullName")) |
---|
359 | this._unsetRDFValue(resource, "email", this._getRDFValue(resource, "email")) |
---|
360 | this._identityContainer.RemoveElement(resource, false); |
---|
361 | } |
---|
362 | |
---|
363 | enumerator = this._smtpContainer.GetElements(); |
---|
364 | while (enumerator && enumerator.hasMoreElements()) { |
---|
365 | var resource = enumerator.getNext(); |
---|
366 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
367 | this._unsetRDFValue(resource, "label", this._getRDFValue(resource, "label")) |
---|
368 | this._unsetRDFValue(resource, "hostname", this._getRDFValue(resource, "hostname")) |
---|
369 | this._unsetRDFValue(resource, "username", this._getRDFValue(resource, "username")) |
---|
370 | this._smtpContainer.RemoveElement(resource, false); |
---|
371 | } |
---|
372 | }, |
---|
373 | |
---|
374 | getRelevantIDs : function() { |
---|
375 | var relevantIDs = new Object(); |
---|
376 | // search relevant Identities |
---|
377 | for each (treeType in Array("email", "maillist", "newsgroup", "filter")) { |
---|
378 | var enumerator = this.getContainer(treeType).GetElements(); |
---|
379 | while (enumerator && enumerator.hasMoreElements()) { |
---|
380 | var resource = enumerator.getNext(); |
---|
381 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
382 | var id = this._getRDFValue(resource, "id") |
---|
383 | if (id) { |
---|
384 | if (!relevantIDs[id]) relevantIDs[id] = 1; else relevantIDs[id] += 1; |
---|
385 | } |
---|
386 | } |
---|
387 | } |
---|
388 | return relevantIDs; |
---|
389 | }, |
---|
390 | |
---|
391 | searchIdentityMismatch : function() { |
---|
392 | Log.debug("searchIdentityMismatch"); |
---|
393 | |
---|
394 | var relevantIDs = this.getRelevantIDs(); |
---|
395 | var mismatchIDs = []; |
---|
396 | |
---|
397 | var AccountManager = Components.classes["@mozilla.org/messenger/account-manager;1"] |
---|
398 | .getService(Components.interfaces.nsIMsgAccountManager); |
---|
399 | for (var id in relevantIDs) { |
---|
400 | var found = false; |
---|
401 | for (var i = 0; i < AccountManager.accounts.Count(); i++) { |
---|
402 | var account = AccountManager.accounts.GetElementAt(i) |
---|
403 | .QueryInterface(Components.interfaces.nsIMsgAccount); |
---|
404 | for (var j = 0; j < account.identities.Count(); j++) { |
---|
405 | var identity = account.identities.GetElementAt(j).QueryInterface(Components.interfaces.nsIMsgIdentity); |
---|
406 | if (id == identity.key) { found = true; break; } |
---|
407 | } |
---|
408 | if (found) break; |
---|
409 | } |
---|
410 | var resource = this._rdfService.GetResource(this._rdfNS + this._rdfNSIdentities + "/" + id); |
---|
411 | var rdfIdentityName = this._getRDFValue(resource, "identityName"); |
---|
412 | var rdfEmail = this._getRDFValue(resource, "email"); |
---|
413 | var rdfFullName = this._getRDFValue(resource, "fullName") |
---|
414 | |
---|
415 | if ( !found || rdfIdentityName != identity.identityName && rdfEmail != identity.email) |
---|
416 | mismatchIDs.push( { oldkey: id, label : rdfIdentityName, ext1: rdfEmail, ext2: rdfFullName, count: relevantIDs[id], key: "" } ) |
---|
417 | } |
---|
418 | if (mismatchIDs.length > 0) { |
---|
419 | Log.debug(" found mismatches on id(s).\n"); |
---|
420 | get3PaneWindow().openDialog("chrome://v_identity/content/vI_rdfAccountMismatchDialog.xul",0, |
---|
421 | "chrome, dialog, modal, alwaysRaised, resizable=yes", "identity", mismatchIDs, |
---|
422 | /* callback chance: */ this).focus(); |
---|
423 | return true; |
---|
424 | } |
---|
425 | else { |
---|
426 | Log.debug(" found no mismatch\n"); |
---|
427 | return false; |
---|
428 | } |
---|
429 | }, |
---|
430 | |
---|
431 | repairAccountMismatch : function(type, mismatchItems) { |
---|
432 | var keyField = (type == "identity")?"id":"smtp" // field to change is 'id' or 'smtp' dependent on type |
---|
433 | for (var i = 0; i < mismatchItems.length; i++) { |
---|
434 | Log.debug("repairAccountMismatch change " + mismatchItems[i].oldkey + " into " + mismatchItems[i].key + ": "); |
---|
435 | // search relevant Identities |
---|
436 | for each (treeType in Array("email", "maillist", "newsgroup", "filter")) { |
---|
437 | var enumerator = this.getContainer(treeType).GetElements(); |
---|
438 | while (enumerator && enumerator.hasMoreElements()) { |
---|
439 | var resource = enumerator.getNext(); |
---|
440 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
441 | if (this._getRDFValue(resource, keyField) == mismatchItems[i].oldkey) { |
---|
442 | if (mismatchItems[i].key == "") this._unsetRDFValue(resource, keyField, mismatchItems[i].oldkey) |
---|
443 | else this._setRDFValue(resource, keyField, mismatchItems[i].key) |
---|
444 | Log.debug("."); |
---|
445 | } |
---|
446 | } |
---|
447 | } |
---|
448 | Log.debug("\n"); |
---|
449 | } |
---|
450 | }, |
---|
451 | |
---|
452 | getRelevantSMTPs : function() { |
---|
453 | var relevantSMTPs = new Object(); |
---|
454 | // search relevant SMTPs |
---|
455 | for each (treeType in Array("email", "maillist", "newsgroup", "filter")) { |
---|
456 | var enumerator = this.getContainer(treeType).GetElements(); |
---|
457 | while (enumerator && enumerator.hasMoreElements()) { |
---|
458 | var resource = enumerator.getNext(); |
---|
459 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
460 | var smtp = this._getRDFValue(resource, "smtp") |
---|
461 | if (smtp && smtp != DEFAULT_SMTP_TAG) { |
---|
462 | if (!relevantSMTPs[smtp]) relevantSMTPs[smtp] = 1; else relevantSMTPs[smtp] += 1; |
---|
463 | } |
---|
464 | } |
---|
465 | } |
---|
466 | return relevantSMTPs; |
---|
467 | }, |
---|
468 | |
---|
469 | searchSmtpMismatch : function() { |
---|
470 | Log.debug("searchSmtpMismatch"); |
---|
471 | |
---|
472 | var relevantSMTPs = this.getRelevantSMTPs(); |
---|
473 | var mismatchSMTPs = []; |
---|
474 | |
---|
475 | for (var smtp in relevantSMTPs) { |
---|
476 | var servers = Components.classes["@mozilla.org/messengercompose/smtp;1"] |
---|
477 | .getService(Components.interfaces.nsISmtpService).smtpServers; |
---|
478 | var found = false; |
---|
479 | while (servers && servers.hasMoreElements()) { |
---|
480 | var server = servers.getNext(); |
---|
481 | if (server instanceof Components.interfaces.nsISmtpServer && |
---|
482 | !server.redirectorType && smtp == server.key) { |
---|
483 | found = true; break; |
---|
484 | } |
---|
485 | } |
---|
486 | var resource = this._rdfService.GetResource(this._rdfNS + this._rdfNSSMTPservers + "/" + smtp); |
---|
487 | var rdfSMTPlabel = this._getRDFValue(resource, "label"); |
---|
488 | var rdfHostname = this._getRDFValue(resource, "hostname"); |
---|
489 | var rdfUsername = this._getRDFValue(resource, "username") |
---|
490 | if (!found || rdfSMTPlabel != (server.description?server.description:server.hostname) && rdfHostname != server.hostname) |
---|
491 | mismatchSMTPs.push( { oldkey: smtp, label : rdfSMTPlabel, ext1: rdfHostname, ext2: rdfUsername, count: relevantSMTPs[smtp], key: "" } ) |
---|
492 | } |
---|
493 | if (mismatchSMTPs.length > 0) { |
---|
494 | Log.debug(" found mismatches on smtp(s).\n"); |
---|
495 | get3PaneWindow().openDialog("chrome://v_identity/content/vI_rdfAccountMismatchDialog.xul",0, |
---|
496 | "chrome, dialog, modal, alwaysRaised, resizable=yes", "smtp", mismatchSMTPs, |
---|
497 | /* callback: */ this).focus(); |
---|
498 | return true; |
---|
499 | } |
---|
500 | else { |
---|
501 | Log.debug(" found no mismatch\n"); |
---|
502 | return false; |
---|
503 | } |
---|
504 | }, |
---|
505 | |
---|
506 | storeAccountInfo : function() { |
---|
507 | Log.debug("storeAccounts\n"); |
---|
508 | |
---|
509 | var AccountManager = Components.classes["@mozilla.org/messenger/account-manager;1"] |
---|
510 | .getService(Components.interfaces.nsIMsgAccountManager); |
---|
511 | for (let i = 0; i < AccountManager.accounts.Count(); i++) { |
---|
512 | var account = AccountManager.accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount); |
---|
513 | for (let j = 0; j < account.identities.Count(); j++) { |
---|
514 | var identity = account.identities.QueryElementAt(j, Components.interfaces.nsIMsgIdentity); |
---|
515 | // Log.debug("storeAccounts identity store id " + identity.key + "\n"); |
---|
516 | |
---|
517 | var resource = this._rdfService.GetResource(this._rdfNS + this._rdfNSIdentities + "/" + identity.key); |
---|
518 | this._setRDFValue(resource, "identityName", identity.identityName); |
---|
519 | this._setRDFValue(resource, "fullName", identity.fullName); |
---|
520 | this._setRDFValue(resource, "email", identity.email); |
---|
521 | |
---|
522 | var position = this._identityContainer.IndexOf(resource); // check for index in new recType |
---|
523 | if (position != -1) this._identityContainer.InsertElementAt(resource, position, false); |
---|
524 | else this._identityContainer.AppendElement(resource); |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | function storeSmtp(server, parent) { |
---|
529 | // Log.debug("storeAccounts smtp store id " + server.key + "\n"); |
---|
530 | var resource = parent._rdfService.GetResource(parent._rdfNS + parent._rdfNSSMTPservers + "/" + server.key); |
---|
531 | parent._setRDFValue(resource, "label", (server.description?server.description:server.hostname)); |
---|
532 | parent._setRDFValue(resource, "hostname", server.hostname); |
---|
533 | parent._setRDFValue(resource, "username", server.username); |
---|
534 | var position = parent._smtpContainer.IndexOf(resource); // check for index in new recType |
---|
535 | if (position != -1) parent._smtpContainer.InsertElementAt(resource, position, false); |
---|
536 | else parent._smtpContainer.AppendElement(resource); |
---|
537 | } |
---|
538 | |
---|
539 | var servers = Components.classes["@mozilla.org/messengercompose/smtp;1"] |
---|
540 | .getService(Components.interfaces.nsISmtpService).smtpServers; |
---|
541 | while (servers && servers.hasMoreElements()) { |
---|
542 | var server = servers.getNext(); |
---|
543 | if (server instanceof Components.interfaces.nsISmtpServer && !server.redirectorType) storeSmtp(server, this); |
---|
544 | } |
---|
545 | |
---|
546 | // Log.debug("storeAccounts done\n"); |
---|
547 | }, |
---|
548 | |
---|
549 | export : function(rdfFileName) { |
---|
550 | var filePicker = Components.classes["@mozilla.org/filepicker;1"] |
---|
551 | .createInstance(Components.interfaces.nsIFilePicker); |
---|
552 | filePicker.init(get3PaneWindow(), "", Components.interfaces.nsIFilePicker.modeSave); |
---|
553 | filePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll | Components.interfaces.nsIFilePicker.filterText ); |
---|
554 | filePicker.appendFilter("RDF Files","*.rdf"); |
---|
555 | |
---|
556 | if (filePicker.show() != Components.interfaces.nsIFilePicker.returnCancel) { |
---|
557 | var rdfDataFile = Components.classes["@mozilla.org/file/local;1"] |
---|
558 | .createInstance(Components.interfaces.nsILocalFile); |
---|
559 | var file = Components.classes["@mozilla.org/file/directory_service;1"] |
---|
560 | .getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile); |
---|
561 | var delimiter = (file.path.match(/\\/))?"\\":"/"; |
---|
562 | rdfDataFile.initWithPath(file.path + delimiter + rdfFileName); |
---|
563 | |
---|
564 | rdfDataFile.copyTo(filePicker.file.parent,filePicker.file.leafName); |
---|
565 | } |
---|
566 | }, |
---|
567 | |
---|
568 | _getRDFResourceForVIdentity : function (recDescription, recType) { |
---|
569 | if (!this._rdfDataSource) return null; |
---|
570 | if (!recDescription) { |
---|
571 | Log.debug("_getRDFResourceForVIdentity: no Recipient given.\n"); |
---|
572 | return null; |
---|
573 | } |
---|
574 | var _rdfNSRecType = null |
---|
575 | switch (recType) { |
---|
576 | case "email": _rdfNSRecType = this._rdfNSEmail; break; |
---|
577 | case "newsgroup" : _rdfNSRecType = this._rdfNSNewsgroup; break; |
---|
578 | case "maillist" : _rdfNSRecType = this._rdfNSMaillist; break; |
---|
579 | case "filter" : _rdfNSRecType = this._rdfNSFilter; break; |
---|
580 | } |
---|
581 | return this._rdfService.GetResource(this._rdfNS + _rdfNSRecType + "/" + recDescription); |
---|
582 | |
---|
583 | |
---|
584 | }, |
---|
585 | |
---|
586 | removeVIdentityFromRDF : function (resource, recType) { |
---|
587 | // Log.debug("removeVIdentityFromRDF " + resource.ValueUTF8 + ".\n"); |
---|
588 | this._unsetRDFValue(resource, "email", this._getRDFValue(resource, "email")) |
---|
589 | this._unsetRDFValue(resource, "fullName", this._getRDFValue(resource, "fullName")) |
---|
590 | this._unsetRDFValue(resource, "id", this._getRDFValue(resource, "id")) |
---|
591 | this._unsetRDFValue(resource, "smtp", this._getRDFValue(resource, "smtp")) |
---|
592 | this._unsetRDFValue(resource, "name", this._getRDFValue(resource, "name")) |
---|
593 | |
---|
594 | let self = this; |
---|
595 | var extras = new identityDataExtras(self, resource) |
---|
596 | extras.loopThroughExtras( |
---|
597 | function (extra) { |
---|
598 | extra.value = self._unsetRDFValue(resource, extra.field, extra.value) }); |
---|
599 | |
---|
600 | this.getContainer(recType).RemoveElement(resource, true); |
---|
601 | }, |
---|
602 | |
---|
603 | _unsetRDFValue : function (resource, field, value) { |
---|
604 | // Log.debug("_unsetRDFValue " + this._rdfService + " " + this._rdfDataSource + "\n"); |
---|
605 | var predicate = this._rdfService.GetResource(this._rdfNS + "rdf#" + field); |
---|
606 | var name = this._rdfService.GetLiteral(value?value:""); |
---|
607 | var target = this._rdfDataSource.GetTarget(resource, predicate, true); |
---|
608 | if (target instanceof Components.interfaces.nsIRDFLiteral) { |
---|
609 | this._rdfDataSource.Unassert(resource, predicate, name, true); |
---|
610 | return null; |
---|
611 | } |
---|
612 | else return value; |
---|
613 | }, |
---|
614 | |
---|
615 | // this will be used from rdfDataTree to get all RDF values, callFunction is vI.rdfDataTreeCollection.__addNewDatum |
---|
616 | readAllEntriesFromRDF : function (addNewDatum, treeType, idData) { |
---|
617 | // Log.debug("readAllEntriesFromRDF " + this._rdfService + " " + this._rdfDataSource + " " + this + "\n"); |
---|
618 | var enumerator = this.getContainer(treeType).GetElements(); |
---|
619 | while (enumerator && enumerator.hasMoreElements()) { |
---|
620 | var resource = enumerator.getNext(); |
---|
621 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
622 | var name = this._getRDFValue(resource, "name") |
---|
623 | var email = this._getRDFValue(resource, "email") |
---|
624 | var fullName = this._getRDFValue(resource, "fullName") |
---|
625 | var id = this._getRDFValue(resource, "id") |
---|
626 | var smtp = this._getRDFValue(resource, "smtp") |
---|
627 | if (!smtp) smtp = NO_SMTP_TAG; |
---|
628 | let self = this; |
---|
629 | var localIdentityData = new identityData(email, fullName, id, smtp, new identityDataExtras(self, resource)) |
---|
630 | addNewDatum (resource, name, localIdentityData, idData) |
---|
631 | } |
---|
632 | }, |
---|
633 | |
---|
634 | __getDescriptionAndType : function (recipient, recipientType) { |
---|
635 | if (recipientType == "addr_newsgroups") return { recDesc : recipient, recType : "newsgroup" } |
---|
636 | else if (this.__isMailingList(recipient)) { |
---|
637 | Log.debug("__getDescriptionAndType: '" + recipient + "' is MailList\n"); |
---|
638 | return { recDesc : this.__getMailListName(recipient), recType : "maillist" } |
---|
639 | } |
---|
640 | else { |
---|
641 | Log.debug("__getDescriptionAndType: '" + recipient + "' is no MailList\n"); |
---|
642 | var localIdentityData = new identityData(recipient, null, null, null, null, null, null); |
---|
643 | return { recDesc : localIdentityData.combinedName, recType : "email" } |
---|
644 | } |
---|
645 | }, |
---|
646 | |
---|
647 | // -------------------------------------------------------------------- |
---|
648 | // check if recipient is a mailing list. |
---|
649 | // Similiar to Thunderbird, if there are muliple cards with the same displayName the mailinglist is preferred |
---|
650 | // see also https://bugzilla.mozilla.org/show_bug.cgi?id=408575 |
---|
651 | __isMailingList: function(recipient) { |
---|
652 | let abManager = Components.classes["@mozilla.org/abmanager;1"] |
---|
653 | .getService(Components.interfaces.nsIAbManager); |
---|
654 | let allAddressBooks = abManager.directories; |
---|
655 | while (allAddressBooks.hasMoreElements()) { |
---|
656 | let ab = allAddressBooks.getNext(); |
---|
657 | if (ab instanceof Components.interfaces.nsIAbDirectory && !ab.isRemote) { |
---|
658 | let abdirectory = abManager.getDirectory(ab.URI + |
---|
659 | "?(and(DisplayName,=," + encodeURIComponent(this.__getMailListName(recipient)) + ")(IsMailList,=,TRUE))"); |
---|
660 | if (abdirectory) { |
---|
661 | try { // just try, sometimes there are no childCards at all... |
---|
662 | let cards = abdirectory.childCards; |
---|
663 | if (cards.hasMoreElements()) return true; // only interested if there is at least one element... |
---|
664 | } catch(e) { } |
---|
665 | } |
---|
666 | } |
---|
667 | } |
---|
668 | return false; |
---|
669 | }, |
---|
670 | |
---|
671 | // -------------------------------------------------------------------- |
---|
672 | |
---|
673 | __getMailListName : function(recipient) { |
---|
674 | if (recipient.match(/<[^>]*>/) || recipient.match(/$/)) { |
---|
675 | var mailListName = RegExp.leftContext + RegExp.rightContext |
---|
676 | mailListName = mailListName.replace(/^\s+|\s+$/g,"") |
---|
677 | } |
---|
678 | return mailListName; |
---|
679 | }, |
---|
680 | |
---|
681 | findMatchingFilter : function (recipient, recipientType) { |
---|
682 | var recDescription = this.__getDescriptionAndType(recipient, recipientType).recDesc; |
---|
683 | Log.debug("findMatchingFilter for " + recDescription + ".\n"); |
---|
684 | var enumerator = this._filterContainer.GetElements(); |
---|
685 | while (enumerator && enumerator.hasMoreElements()) { |
---|
686 | var resource = enumerator.getNext(); |
---|
687 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
688 | var filter = this._getRDFValue(resource, "name"); |
---|
689 | |
---|
690 | const filterType = { None : 0, RegExp : 1, StrCmp : 2 } |
---|
691 | var recentfilterType; |
---|
692 | |
---|
693 | if (filter == "") continue; |
---|
694 | if (/^\/(.*)\/$/.exec(filter)) |
---|
695 | { Log.debug("findMatchingFilter with RegExp '" |
---|
696 | + filter.replace(/\\/g,"\\\\") + "'\n"); recentfilterType = filterType.RegExp; } |
---|
697 | else { Log.debug("findMatchingFilter, compare with '" |
---|
698 | + filter + "'\n"); recentfilterType = filterType.StrCmp; } |
---|
699 | |
---|
700 | switch (recentfilterType) { |
---|
701 | case filterType.RegExp: |
---|
702 | try { /^\/(.*)\/$/.exec(filter); |
---|
703 | if (recDescription.match(new RegExp(RegExp.$1,"i"))) { |
---|
704 | Log.debug("findMatchingFilter found stored data.\n"); |
---|
705 | return this._readVIdentityFromRDF(resource); |
---|
706 | } |
---|
707 | } |
---|
708 | catch(vErr) { }; break; |
---|
709 | case filterType.StrCmp: |
---|
710 | if (recDescription.toLowerCase().indexOf(filter.toLowerCase()) != -1) { |
---|
711 | Log.debug("findMatchingFilter found stored data.\n"); |
---|
712 | return this._readVIdentityFromRDF(resource); |
---|
713 | } |
---|
714 | break; |
---|
715 | } |
---|
716 | } |
---|
717 | Log.debug("findMatchingFilter no match found.\n"); |
---|
718 | return null; |
---|
719 | }, |
---|
720 | |
---|
721 | readVIdentityFromRDF : function (recipient, recipientType) { |
---|
722 | var storedRecipient = this.__getDescriptionAndType(recipient, recipientType); |
---|
723 | var email = this._rdfService.GetResource(this._rdfNS + "rdf#email"); |
---|
724 | var resource = this._getRDFResourceForVIdentity(storedRecipient.recDesc, storedRecipient.recType); |
---|
725 | if (!resource) return null; |
---|
726 | if (!this._rdfDataSource.hasArcOut(resource, email)) { |
---|
727 | // no data available --> give up. |
---|
728 | Log.debug("readVIdentityFromRDF no data found.\n"); |
---|
729 | return null; |
---|
730 | } |
---|
731 | Log.debug("readVIdentityFromRDF found stored data.\n"); |
---|
732 | |
---|
733 | return this._readVIdentityFromRDF(resource); |
---|
734 | }, |
---|
735 | |
---|
736 | _readVIdentityFromRDF : function (resource) { |
---|
737 | var email = this._getRDFValue(resource, "email") |
---|
738 | var fullName = this._getRDFValue(resource, "fullName") |
---|
739 | var id = this._getRDFValue(resource, "id") |
---|
740 | var smtp = this._getRDFValue(resource, "smtp") |
---|
741 | if (!smtp) smtp = NO_SMTP_TAG; |
---|
742 | |
---|
743 | Log.debug("email='" + email + |
---|
744 | "' fullName='" + fullName + "' id='" + id + "' smtp='" + smtp + "'\n"); |
---|
745 | let self = this; |
---|
746 | var localIdentityData = new identityData(email, fullName, id, smtp, new identityDataExtras(self, resource)) |
---|
747 | return localIdentityData; |
---|
748 | }, |
---|
749 | |
---|
750 | _getRDFValue : function (resource, field) { |
---|
751 | // Log.debug("_getRDFValue " + this._rdfService + " " + this._rdfDataSource + " " + this + "\n"); |
---|
752 | var predicate = this._rdfService.GetResource(this._rdfNS + "rdf#" + field); |
---|
753 | var target = this._rdfDataSource.GetTarget(resource, predicate, true); |
---|
754 | if (target instanceof Components.interfaces.nsIRDFLiteral) return target.Value |
---|
755 | else return null; |
---|
756 | }, |
---|
757 | |
---|
758 | updateRDFFromVIdentity : function(identityData, recipientName, recipientType) { |
---|
759 | var recipient = this.__getDescriptionAndType(recipientName, recipientType) |
---|
760 | this.updateRDF(recipient.recDesc, recipient.recType, identityData, |
---|
761 | vIprefs.get("storage_store_base_id"), |
---|
762 | vIprefs.get("storage_store_SMTP"), |
---|
763 | null, null); |
---|
764 | }, |
---|
765 | |
---|
766 | removeRDF : function (recDescription, recType) { |
---|
767 | var resource = this._getRDFResourceForVIdentity(recDescription, recType); |
---|
768 | if (!resource) return null; |
---|
769 | this.removeVIdentityFromRDF(resource, recType); |
---|
770 | return resource; |
---|
771 | }, |
---|
772 | |
---|
773 | updateRDF : function (recDescription, recType, localIdentityData, storeBaseID, storeSMTP, prevRecDescription, prevRecType) { |
---|
774 | // Log.debug("(" + this._rdfNS + "): updateRDF recDescription=" + recDescription + " localIdentityData.email=" + localIdentityData.email + ".\n"); |
---|
775 | |
---|
776 | // if (!localIdentityData.email) { |
---|
777 | // Log.debug("updateRDF: no Sender-email for Recipient, aborting.\n"); |
---|
778 | // return; |
---|
779 | // } |
---|
780 | if (!recDescription || recDescription.length == 0) return; |
---|
781 | |
---|
782 | if (!prevRecDescription) prevRecDescription = recDescription; |
---|
783 | if (!prevRecType) prevRecType = recType; |
---|
784 | |
---|
785 | var resource = this._getRDFResourceForVIdentity(prevRecDescription, prevRecType); |
---|
786 | if (!resource) return; |
---|
787 | // Log.debug("updateRDF " + resource.ValueUTF8 + ".\n"); |
---|
788 | |
---|
789 | var position = this.getContainer(recType).IndexOf(resource); // check for index in new recType |
---|
790 | this.removeVIdentityFromRDF(resource, prevRecType); |
---|
791 | |
---|
792 | resource = this._getRDFResourceForVIdentity(recDescription, recType); |
---|
793 | |
---|
794 | this._setRDFValue(resource, "email", localIdentityData.email); |
---|
795 | this._setRDFValue(resource, "fullName", localIdentityData.fullName); |
---|
796 | if (storeBaseID) |
---|
797 | this._setRDFValue(resource, "id", localIdentityData.id.key); |
---|
798 | else this._unsetRDFValue(resource, "id", this._getRDFValue(resource, "id")) |
---|
799 | if (storeSMTP && localIdentityData.smtp.key != NO_SMTP_TAG) |
---|
800 | this._setRDFValue(resource, "smtp", localIdentityData.smtp.key); |
---|
801 | else this._unsetRDFValue(resource, "smtp", this._getRDFValue(resource, "smtp")) |
---|
802 | this._setRDFValue(resource, "name", recDescription); |
---|
803 | |
---|
804 | if (localIdentityData.extras) { |
---|
805 | let self = this; |
---|
806 | localIdentityData.extras.loopThroughExtras( |
---|
807 | function (extra) { |
---|
808 | extra.value = self._setRDFValue(resource, extra.field, extra.value) }); |
---|
809 | Log.debug("extras: " + localIdentityData.extras.status() + "\n"); |
---|
810 | } |
---|
811 | |
---|
812 | Log.debug("updateRDF add " + resource.ValueUTF8 + " at position " + position + ".\n"); |
---|
813 | if (position != -1) this.getContainer(recType).InsertElementAt(resource, position, true); |
---|
814 | else this.getContainer(recType).AppendElement(resource); |
---|
815 | }, |
---|
816 | |
---|
817 | _setRDFValue : function (resource, field, value) { |
---|
818 | // Log.debug("_setRDFValue " + resource.ValueUTF8 + " " + field + " " + value + ".\n"); |
---|
819 | if (!value) return value; // return if some value was not set. |
---|
820 | // Log.debug("_setRDFValue " + this._rdfService + " " + this._rdfDataSource + "\n"); |
---|
821 | var predicate = this._rdfService.GetResource(this._rdfNS + "rdf#" + field); |
---|
822 | var name = this._rdfService.GetLiteral(value); |
---|
823 | var target = this._rdfDataSource.GetTarget(resource, predicate, true); |
---|
824 | |
---|
825 | if (target instanceof Components.interfaces.nsIRDFLiteral) |
---|
826 | this._rdfDataSource.Change(resource, predicate, target, name); |
---|
827 | else this._rdfDataSource.Assert(resource, predicate, name, true); |
---|
828 | return value; |
---|
829 | }, |
---|
830 | |
---|
831 | // code adapted from http://xulsolutions.blogspot.com/2006/07/creating-uninstall-script-for.html |
---|
832 | AccountManagerObserver : { |
---|
833 | _uninstall : false, |
---|
834 | observe : function(subject, topic, data) { |
---|
835 | if (topic == "am-smtpChanges" || topic == "am-acceptChanges") { |
---|
836 | Log.debug("account/smtp changes observed\n"); |
---|
837 | this.searchIdentityMismatch(); |
---|
838 | this.searchSmtpMismatch(); |
---|
839 | this.refreshAccountInfo(); |
---|
840 | } |
---|
841 | }, |
---|
842 | register : function() { |
---|
843 | Log.debug("register AccountManagerObserver\n"); |
---|
844 | var obsService = Components.classes["@mozilla.org/observer-service;1"]. |
---|
845 | getService(Components.interfaces.nsIObserverService) |
---|
846 | obsService.addObserver(this, "am-smtpChanges", false); |
---|
847 | obsService.addObserver(this, "am-acceptChanges", false); |
---|
848 | }, |
---|
849 | unregister : function() { |
---|
850 | var obsService = Components.classes["@mozilla.org/observer-service;1"]. |
---|
851 | getService(Components.interfaces.nsIObserverService) |
---|
852 | try { |
---|
853 | obsService.removeObserver(this, "am-smtpChanges"); |
---|
854 | obsService.removeObserver(this, "am-acceptChanges"); |
---|
855 | } catch(e) { }; |
---|
856 | } |
---|
857 | } |
---|
858 | } |
---|
859 | |
---|
860 | |
---|
861 | function rdfDatasourceAccess() { |
---|
862 | this._rdfDataSource = new rdfDatasource("virtualIdentity.rdf", false); |
---|
863 | this.stringBundle = Services.strings.createBundle("chrome://v_identity/locale/v_identity.properties"); |
---|
864 | } |
---|
865 | |
---|
866 | rdfDatasourceAccess.prototype = { |
---|
867 | _rdfDataSource : null, |
---|
868 | stringBundle : null, |
---|
869 | |
---|
870 | clean : function() { |
---|
871 | this._rdfDataSource.clean(); |
---|
872 | }, |
---|
873 | |
---|
874 | updateVIdentityFromStorage : function(recipientName, recipientType, currentIdentity, currentIdentityIsVid, isNotFirstInputElement) { |
---|
875 | var localIdentities = new identityCollection(); |
---|
876 | localIdentities.addWithoutDuplicates(this._rdfDataSource.readVIdentityFromRDF(recipientName, recipientType)); |
---|
877 | if (localIdentities.number == 1) Log.debug("using data from direct match\n"); |
---|
878 | localIdentities.addWithoutDuplicates(this._rdfDataSource.findMatchingFilter(recipientName, recipientType)); |
---|
879 | |
---|
880 | var returnValue = {}; returnValue.identityCollection = localIdentities; returnValue.result = "drop"; |
---|
881 | if (localIdentities.number == 0) { |
---|
882 | Log.debug("updateVIdentityFromStorage no usable Storage-Data found.\n"); |
---|
883 | } |
---|
884 | else { |
---|
885 | Log.debug("compare with current Identity\n"); |
---|
886 | if (vIprefs.get("storage_getOneOnly") && // if requested to retrieve only storageID for first recipient entered |
---|
887 | isNotFirstInputElement && // and it is now not the first recipient entered |
---|
888 | !localIdentities.identityDataCollection[0].equalsIdentity(currentIdentity, false).equal) { // and this id is different than the current used one |
---|
889 | StorageNotification.info(this.stringBundle.GetStringFromName("vident.smartIdentity.vIStorageCollidingIdentity")); |
---|
890 | // returnValue.result = "drop"; // this is the default value |
---|
891 | } |
---|
892 | // only update fields if new Identity is different than old one. |
---|
893 | else { |
---|
894 | Log.debug("updateVIdentityFromStorage check if storage-data matches current Identity.\n"); |
---|
895 | var compResult = localIdentities.identityDataCollection[0].equalsIdentity(currentIdentity, true); |
---|
896 | if (!compResult.equal) { |
---|
897 | var warning = this.__getWarning("replaceVIdentity", recipientName, compResult.compareMatrix); |
---|
898 | if ( !currentIdentityIsVid || |
---|
899 | !vIprefs.get("storage_warn_vI_replace") || |
---|
900 | (this.__askWarning(warning) == "accept")) { |
---|
901 | returnValue.result = "accept"; |
---|
902 | } |
---|
903 | } |
---|
904 | else { |
---|
905 | returnValue.result = "equal"; |
---|
906 | } |
---|
907 | } |
---|
908 | } |
---|
909 | return returnValue; |
---|
910 | }, |
---|
911 | |
---|
912 | storeVIdentityToAllRecipients : function(identityData, recipients) { |
---|
913 | var multipleRecipients = (recipients.length > 1); |
---|
914 | var dontUpdateMultipleNoEqual = (vIprefs.get("storage_dont_update_multiple") && multipleRecipients) |
---|
915 | Log.debug("storeVIdentityToAllRecipients dontUpdateMultipleNoEqual='" + dontUpdateMultipleNoEqual + "'\n") |
---|
916 | |
---|
917 | for (var j = 0; j < recipients.length; j++) { |
---|
918 | var returnValue = this.__updateStorageFromVIdentity(identityData, recipients[j].recipient, recipients[j].recipientType, dontUpdateMultipleNoEqual); |
---|
919 | if (returnValue.update != "accept") break; |
---|
920 | } |
---|
921 | return returnValue; |
---|
922 | }, |
---|
923 | |
---|
924 | getVIdentityFromAllRecipients : function(allIdentities, recipients) { |
---|
925 | if (!vIprefs.get("storage")) |
---|
926 | { Log.debug("Storage deactivated\n"); return; } |
---|
927 | var initnumber = allIdentities.number; |
---|
928 | for (var j = 0; j < recipients.length; j++) { |
---|
929 | allIdentities.addWithoutDuplicates(this._rdfDataSource.readVIdentityFromRDF(recipients[j].recipient, recipients[j].recipientType)); |
---|
930 | allIdentities.addWithoutDuplicates(this._rdfDataSource.findMatchingFilter(recipients[j].recipient, recipients[j].recipientType)); |
---|
931 | } |
---|
932 | Log.debug("found " + (allIdentities.number-initnumber) + " address(es)\n") |
---|
933 | }, |
---|
934 | |
---|
935 | __updateStorageFromVIdentity : function(identityData, recipient, recipientType, dontUpdateMultipleNoEqual) { |
---|
936 | Log.debug("__updateStorageFromVIdentity.\n") |
---|
937 | var storageDataByType = this._rdfDataSource.readVIdentityFromRDF(recipient, recipientType); |
---|
938 | var storageDataByFilter = this._rdfDataSource.findMatchingFilter(recipient, recipientType); |
---|
939 | |
---|
940 | // update (storing) of data by type is required if there is |
---|
941 | // no data stored by type (or different data stored) and no equal filter found |
---|
942 | var storageDataByTypeCompResult = storageDataByType?storageDataByType.equalsIdentity(identityData, true):null; |
---|
943 | var storageDataByTypeEqual = (storageDataByType && storageDataByTypeCompResult.equal); |
---|
944 | var storageDataByFilterEqual = (storageDataByFilter && storageDataByFilter.equalsIdentity(identityData, false).equal); |
---|
945 | |
---|
946 | var doUpdate = "accept"; |
---|
947 | if ( (!storageDataByType && !storageDataByFilterEqual) || |
---|
948 | (!storageDataByTypeEqual && !storageDataByFilterEqual && !dontUpdateMultipleNoEqual) ) { |
---|
949 | Log.debug("__updateStorageFromVIdentity updating\n") |
---|
950 | if (storageDataByType && !storageDataByTypeEqual && vIprefs.get("storage_warn_update")) { |
---|
951 | Log.debug("__updateStorageFromVIdentity overwrite warning\n"); |
---|
952 | doUpdate = this.__askWarning(this.__getWarning("updateStorage", recipient, storageDataByTypeCompResult.compareMatrix)); |
---|
953 | } |
---|
954 | } |
---|
955 | if (doUpdate == "accept") this._rdfDataSource.updateRDFFromVIdentity(identityData, recipient, recipientType); |
---|
956 | return { update : doUpdate, storedIdentity : storageDataByType }; |
---|
957 | }, |
---|
958 | |
---|
959 | __getWarning : function(warningCase, recipient, compareMatrix) { |
---|
960 | var warning = { title: null, recLabel : null, recipient : null, warning : null, css: null, query : null, class : null }; |
---|
961 | warning.title = this.stringBundle.GetStringFromName("vident." + warningCase + ".title") |
---|
962 | warning.recLabel = this.stringBundle.GetStringFromName("vident." + warningCase + ".recipient") + ":"; |
---|
963 | warning.recipient = recipient; |
---|
964 | warning.warning = |
---|
965 | "<table class='" + warningCase + "'><thead><tr><th class='col1'/>" + |
---|
966 | "<th class='col2'>" + this.stringBundle.GetStringFromName("vident." + warningCase + ".currentIdentity") + "</th>" + |
---|
967 | "<th class='col3'>" + this.stringBundle.GetStringFromName("vident." + warningCase + ".storedIdentity") + "</th>" + |
---|
968 | "</tr></thead>" + |
---|
969 | "<tbody>" + compareMatrix + "</tbody>" + |
---|
970 | "</table>" |
---|
971 | warning.css = "vI.DialogBrowser.css"; |
---|
972 | warning.query = this.stringBundle.GetStringFromName("vident." + warningCase + ".query"); |
---|
973 | warning.class = warningCase; |
---|
974 | return warning; |
---|
975 | }, |
---|
976 | |
---|
977 | __askWarning : function(warning) { |
---|
978 | var retVar = { returnValue: null }; |
---|
979 | var answer = get3PaneWindow().openDialog("chrome://v_identity/content/vI_Dialog.xul","", |
---|
980 | "chrome, dialog, modal, alwaysRaised, resizable=yes", |
---|
981 | warning, retVar) |
---|
982 | dump("retVar.returnValue=" + retVar.returnValue + "\n") |
---|
983 | return retVar.returnValue; |
---|
984 | }, |
---|
985 | } |
---|
986 | |
---|
987 | |
---|
988 | // create with name of the file to import into |
---|
989 | function rdfDatasourceImporter(rdfFileName) { |
---|
990 | this._rdfFileName = rdfFileName; |
---|
991 | if (this._rdfFileName) this.import(); |
---|
992 | } |
---|
993 | |
---|
994 | rdfDatasourceImporter.prototype = { |
---|
995 | _rdfService : Components.classes["@mozilla.org/rdf/rdf-service;1"] |
---|
996 | .getService(Components.interfaces.nsIRDFService), |
---|
997 | _rdfDataSource : null, |
---|
998 | _rdfFileName : null, |
---|
999 | _rdfImportDataSource : null, |
---|
1000 | |
---|
1001 | _getMatchingIdentity : function(name, email, fullName) { |
---|
1002 | var AccountManager = Components.classes["@mozilla.org/messenger/account-manager;1"] |
---|
1003 | .getService(Components.interfaces.nsIMsgAccountManager); |
---|
1004 | for (let i = 0; i < AccountManager.accounts.Count(); i++) { |
---|
1005 | var account = AccountManager.accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount); |
---|
1006 | for (let j = 0; j < account.identities.Count(); j++) { |
---|
1007 | var identity = account.identities.QueryElementAt(j, Components.interfaces.nsIMsgIdentity); |
---|
1008 | if (name == identity.identityName || (fullName == identity.fullName && email == identity.email)) return identity.key; |
---|
1009 | } |
---|
1010 | } |
---|
1011 | return null; |
---|
1012 | }, |
---|
1013 | |
---|
1014 | _getMatchingSMTP : function(label, hostname, username) { |
---|
1015 | var servers = Components.classes["@mozilla.org/messengercompose/smtp;1"] |
---|
1016 | .getService(Components.interfaces.nsISmtpService).smtpServers; |
---|
1017 | while (servers && servers.hasMoreElements()) { |
---|
1018 | var server = servers.getNext(); |
---|
1019 | if (server instanceof Components.interfaces.nsISmtpServer && !server.redirectorType) |
---|
1020 | if (label == (server.description?server.description:server.hostname) || (hostname == server.hostname && username == server.username)) |
---|
1021 | return server.key; |
---|
1022 | } |
---|
1023 | return null; |
---|
1024 | }, |
---|
1025 | |
---|
1026 | _translateRelevantIDs : function() { |
---|
1027 | var relevantIDs = this._rdfImportDataSource.getRelevantIDs(); |
---|
1028 | for (var id in relevantIDs) { |
---|
1029 | var resource = this._rdfService.GetResource(this._rdfImportDataSource._rdfNS + this._rdfImportDataSource._rdfNSIdentities + "/" + id); |
---|
1030 | var values = { id : null, identityName : null, email : null, fullName : null } |
---|
1031 | values.identityName = this._rdfImportDataSource._getRDFValue(resource, "identityName"); |
---|
1032 | values.email = this._rdfImportDataSource._getRDFValue(resource, "email"); |
---|
1033 | values.fullName = this._rdfImportDataSource._getRDFValue(resource, "fullName"); |
---|
1034 | values.id = this._getMatchingIdentity(values.identityName, values.email, values.fullName); |
---|
1035 | values.id = values.id?values.id:"import_" + id |
---|
1036 | relevantIDs[id] = values; |
---|
1037 | Log.debug("import: translate relevant ID from previous '" + id + "' to current '" + relevantIDs[id].id + "'\n"); |
---|
1038 | } |
---|
1039 | return relevantIDs; |
---|
1040 | }, |
---|
1041 | |
---|
1042 | _storeMappedIDs : function(relevantIDs) { |
---|
1043 | for (var id in relevantIDs) { |
---|
1044 | if (relevantIDs[id].id == "import_" + id) { |
---|
1045 | var resource = this._rdfService |
---|
1046 | .GetResource(this._rdfDataSource._rdfNS + this._rdfDataSource._rdfNSIdentities + "/" + relevantIDs[id].id); |
---|
1047 | this._rdfDataSource._setRDFValue(resource, "identityName", relevantIDs[id].identityName); |
---|
1048 | this._rdfDataSource._setRDFValue(resource, "fullName", relevantIDs[id].fullName); |
---|
1049 | this._rdfDataSource._setRDFValue(resource, "email", relevantIDs[id].email); |
---|
1050 | |
---|
1051 | var position = this._rdfDataSource._identityContainer.IndexOf(resource); // check for index in new recType |
---|
1052 | if (position != -1) this._rdfDataSource._identityContainer.InsertElementAt(resource, position, false); |
---|
1053 | else this._rdfDataSource._identityContainer.AppendElement(resource); |
---|
1054 | } |
---|
1055 | } |
---|
1056 | }, |
---|
1057 | |
---|
1058 | _translateRelevantSMTPs : function() { |
---|
1059 | var relevantSMTPs = this._rdfImportDataSource.getRelevantSMTPs(); |
---|
1060 | for (var smtp in relevantSMTPs) { |
---|
1061 | var resource = this._rdfService.GetResource(this._rdfImportDataSource._rdfNS + this._rdfImportDataSource._rdfNSSMTPservers + "/" + smtp); |
---|
1062 | var values = { smtp : null, label : null, hostname : null, username : null } |
---|
1063 | values.label = this._rdfImportDataSource._getRDFValue(resource, "label"); |
---|
1064 | values.hostname = this._rdfImportDataSource._getRDFValue(resource, "hostname"); |
---|
1065 | values.username = this._rdfImportDataSource._getRDFValue(resource, "username"); |
---|
1066 | values.smtp = this._getMatchingSMTP(values.label, values.hostname, values.username); |
---|
1067 | values.smtp = values.smtp?values.smtp:"import_" + smtp; |
---|
1068 | relevantSMTPs[smtp] = values; |
---|
1069 | Log.debug("import: translate relevant SMTP from previous '" + smtp + "' to current '" + relevantSMTPs[smtp].smtp + "'\n"); |
---|
1070 | } |
---|
1071 | return relevantSMTPs; |
---|
1072 | }, |
---|
1073 | |
---|
1074 | _storeMappedSMTPs : function(relevantSMTPs) { |
---|
1075 | for (var smtp in relevantSMTPs) { |
---|
1076 | if (relevantSMTPs[smtp].smtp == "import_" + smtp) { |
---|
1077 | var resource = this._rdfService |
---|
1078 | .GetResource(this._rdfDataSource._rdfNS + this._rdfDataSource._rdfNSSMTPservers + "/" + relevantSMTPs[smtp].smtp); |
---|
1079 | this._rdfDataSource._setRDFValue(resource, "label", relevantSMTPs[smtp].label); |
---|
1080 | this._rdfDataSource._setRDFValue(resource, "hostname", relevantSMTPs[smtp].hostname); |
---|
1081 | this._rdfDataSource._setRDFValue(resource, "username", relevantSMTPs[smtp].username); |
---|
1082 | |
---|
1083 | var position = this._rdfDataSource._smtpContainer.IndexOf(resource); // check for index in new recType |
---|
1084 | if (position != -1) this._rdfDataSource._smtpContainer.InsertElementAt(resource, position, false); |
---|
1085 | else this._rdfDataSource._smtpContainer.AppendElement(resource); |
---|
1086 | } |
---|
1087 | } |
---|
1088 | }, |
---|
1089 | |
---|
1090 | import : function() { |
---|
1091 | var filePicker = Components.classes["@mozilla.org/filepicker;1"] |
---|
1092 | .createInstance(Components.interfaces.nsIFilePicker); |
---|
1093 | |
---|
1094 | filePicker.init(get3PaneWindow(), "", Components.interfaces.nsIFilePicker.modeOpen); |
---|
1095 | filePicker.appendFilter("RDF Files","*.rdf"); |
---|
1096 | filePicker.appendFilters(Components.interfaces.nsIFilePicker.filterText | Components.interfaces.nsIFilePicker.filterAll ); |
---|
1097 | |
---|
1098 | if (filePicker.show() == Components.interfaces.nsIFilePicker.returnOK) { |
---|
1099 | Log.debug("\nIMPORT\nimport: preparation:\n"); |
---|
1100 | |
---|
1101 | var importRdfDataFile = Components.classes["@mozilla.org/file/local;1"] |
---|
1102 | .createInstance(Components.interfaces.nsILocalFile); |
---|
1103 | var file = Components.classes["@mozilla.org/file/directory_service;1"] |
---|
1104 | .getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile); |
---|
1105 | var delimiter = (file.path.match(/\\/))?"\\":"/"; |
---|
1106 | importRdfDataFile.initWithPath(file.path + delimiter + this._rdfFileName + "_import"); |
---|
1107 | filePicker.file.copyTo(importRdfDataFile.parent,importRdfDataFile.leafName); |
---|
1108 | |
---|
1109 | Log.debug("import: copied file from " + filePicker.file.path + " to " + importRdfDataFile.path + "'\n"); |
---|
1110 | |
---|
1111 | // init Datasources |
---|
1112 | this._rdfImportDataSource = new rdfDatasource(importRdfDataFile.leafName, true); |
---|
1113 | |
---|
1114 | // search matching IDs and SMTPs for anyones used in import-file |
---|
1115 | var relevantIDs = this._translateRelevantIDs(); |
---|
1116 | var relevantSMTPs = this._translateRelevantSMTPs(); |
---|
1117 | |
---|
1118 | Log.debug("import: preparation done.\n"); |
---|
1119 | Log.debug("import: starting import:\n"); |
---|
1120 | |
---|
1121 | for each (treeType in Array("email", "maillist", "newsgroup", "filter")) { |
---|
1122 | // re-initialize importDataSource to point rdfService to the right Resources |
---|
1123 | this._rdfImportDataSource = new rdfDatasource(importRdfDataFile.leafName, true); |
---|
1124 | var container = this._rdfImportDataSource.getContainer(treeType) |
---|
1125 | if (container.GetCount() == 0) continue; |
---|
1126 | Log.debug("importing " + treeType + ": " + container.GetCount()+ " datasets from " + this._rdfImportDataSource._rdfDataSource.URI + "\n"); |
---|
1127 | var enumerator = container.GetElements(); |
---|
1128 | // re-initialize dataSource to point rdfService to the right Resources |
---|
1129 | this._rdfDataSource = new rdfDatasource(this._rdfFileName, true); |
---|
1130 | var count = 0; |
---|
1131 | while (enumerator.hasMoreElements()) { |
---|
1132 | var resource = enumerator.getNext(); count += 1; |
---|
1133 | resource.QueryInterface(Components.interfaces.nsIRDFResource); |
---|
1134 | Log.debug(" " + count + " "); |
---|
1135 | var name = this._rdfImportDataSource._getRDFValue(resource, "name") |
---|
1136 | var email = this._rdfImportDataSource._getRDFValue(resource, "email") |
---|
1137 | var fullName = this._rdfImportDataSource._getRDFValue(resource, "fullName") |
---|
1138 | var id = this._rdfImportDataSource._getRDFValue(resource, "id") |
---|
1139 | id = id?relevantIDs[id].id:null |
---|
1140 | var smtp = this._rdfImportDataSource._getRDFValue(resource, "smtp") |
---|
1141 | smtp = (smtp && smtp != DEFAULT_SMTP_TAG)?relevantSMTPs[smtp].smtp:smtp |
---|
1142 | var localIdentityData = new identityData(email, fullName, id, smtp, new identityDataExtras(this._rdfImportDataSource, resource)) |
---|
1143 | |
---|
1144 | this._rdfDataSource.updateRDF(name, treeType, localIdentityData, false, false, null, null) |
---|
1145 | var resource = this._rdfDataSource._getRDFResourceForVIdentity(name, treeType); |
---|
1146 | if (id) this._rdfDataSource._setRDFValue(resource, "id", id); // localIdentityData can only store valid id's, this one might be a temporary invalid id |
---|
1147 | if (smtp) this._rdfDataSource._setRDFValue(resource, "smtp", smtp); // localIdentityData can only store valid smtp's, this one might be a temporary invalid smtp |
---|
1148 | } |
---|
1149 | } |
---|
1150 | |
---|
1151 | Log.debug("import: removing temporary file " + importRdfDataFile.path + ".\n"); |
---|
1152 | this._rdfImportDataSource = null; importRdfDataFile.remove(false); |
---|
1153 | Log.debug("import: import done.\n"); |
---|
1154 | |
---|
1155 | Log.debug("import: cleaning ID/SMTP storages:\n"); |
---|
1156 | this._rdfDataSource = new rdfDatasource(this._rdfFileName, true); |
---|
1157 | |
---|
1158 | this._storeMappedIDs(relevantIDs); |
---|
1159 | this._rdfDataSource.searchIdentityMismatch(); |
---|
1160 | this._storeMappedSMTPs(relevantSMTPs); |
---|
1161 | this._rdfDataSource.searchSmtpMismatch(); |
---|
1162 | |
---|
1163 | this._rdfDataSource.refreshAccountInfo(); |
---|
1164 | this._rdfDataSource.clean(); |
---|
1165 | this._rdfDataSource = null; |
---|
1166 | Log.debug("import: cleaning ID/SMTP storages done.\n"); |
---|
1167 | Log.debug("IMPORT DONE.\n"); |
---|
1168 | } |
---|
1169 | } |
---|
1170 | } |
---|