This is just some static backup of the original site, don't expect every link to work!

source: chrome/content/v_identity/vI_upgrade.js @ d1f668

lite_0.1multiEditng_0.6ng_0.6_helpng_0.8ng_0.9
Last change on this file since d1f668 was d1f668, checked in by rene <rene@…>, 13 years ago

implementation of SMTP storage / change of interface

  • Property mode set to 100644
File size: 15.3 KB
Line 
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
25var vI_upgrade = {
26    preferences : Components.classes["@mozilla.org/preferences-service;1"]
27            .getService(Components.interfaces.nsIPrefService)
28            .getBranch("extensions.virtualIdentity."),
29           
30    versionChecker : Components.classes["@mozilla.org/xpcom/version-comparator;1"]
31            .getService(Components.interfaces.nsIVersionComparator),
32
33    init : function() {
34        vI_upgrade.__initRequirements();
35        document.documentElement.getButton("cancel").setAttribute("hidden", "true")
36    },
37
38    __initRequirements : function() {
39        vI_notificationBar.dump("") // this initialises the debug-area
40        vI_rdfDatasource.init(); // just to be sure that Datasource is initialised
41    },
42   
43    // this function checks for the chance to ugrade without shoing the complete wizard
44    // if so, perform the upgrade and return true
45    // by default the wizard is not shown if it is a one-version-forward upgrade
46    quick_upgrade : function() {
47        // seamonkey doesn't have a extensionmanager, so read version of extension from hidden version-label
48        var currentVersion = vI_rdfDatasource.getCurrentExtFileVersion()
49        if (!currentVersion) return false;
50        currentVersion = currentVersion.split(/\./);
51        var nextVersion = currentVersion[0] + "." + currentVersion[1] + "."
52        if (currentVersion[2].match(/pre/))
53            nextVersion += parseInt(currentVersion[2])
54        else nextVersion += eval(parseInt(currentVersion[2])+1)
55        var extVersion = document.getElementById("extVersion").getAttribute("value");
56               
57        // don't show the dialog if we do a one-step upgrade
58        if (vI_upgrade.versionChecker.compare(extVersion, nextVersion) <= 0) {
59            vI_notificationBar.dump("starting quick_upgrade.\n")
60            vI_upgrade.__initRequirements();
61            vI_upgrade.__upgrade();
62            return true;
63        }
64        return false;
65    },
66
67    prepare : function(elem) {
68        document.documentElement.getButton('back').setAttribute('hidden','true');
69        document.documentElement.getButton('next').focus();
70        var pageid = elem.getAttribute("pageid");
71        var browser = document.getElementById('vITextBox.' + pageid)
72        if (browser) 
73            browser.outputString =
74                    document.getElementById('vITextBoxBundle').getString('vident.' + pageid);
75    },
76   
77    __upgrade : function() {
78        if (vI_rdfDatasource.extUpgradeRequired()) vI_upgrade.extUpgrade();
79        if (vI_rdfDatasource.rdfUpgradeRequired()) vI_upgrade.rdfUpgrade();
80       
81        vI_account.cleanupSystem();
82    },         
83
84    upgrade : function() {
85        vI_notificationBar.dump("starting upgrade.\n\n")
86        document.getElementById("upgradeWizard").setAttribute("canAdvance", "false")
87        document.documentElement.getButton('next').setAttribute('disabled','true');
88       
89        vI_upgrade.__upgrade();
90   
91        vI_notificationBar.dump("\n\nupgrade finished.\n");
92       
93        document.documentElement.getButton('next').setAttribute('disabled','false');
94        document.getElementById("upgradeWizard").setAttribute("canAdvance", "true")
95    },
96   
97    rdfUpgrade : function() {
98        var currentVersion = vI_rdfDatasource.getCurrentRDFFileVersion();
99        vI_notificationBar.dump("checking for previous version of rdf, found " + 
100            currentVersion + "\nrdf-upgrade required.\n")
101        switch (currentVersion) {
102            case "0.0.1":
103            case "0.0.2":
104                vI_upgrade.__createRDFContainers(); // no break
105            default:
106                vI_upgrade.__tagDefaultSMTP();
107        }
108        vI_rdfDatasource.storeRDFVersion();
109        vI_notificationBar.dump("rdf-upgrade to " + vI_rdfDatasource.getCurrentRDFFileVersion() + " done.\n\n");
110    },
111   
112    // only used for upgrade to 0.0.3 - loop through all ressources.
113    __transferAllResources : function () {
114        vI_notificationBar.dump("upgrade: transferAllResources ");
115        var enumerator = vI_rdfDatasource.rdfDataSource.GetAllResources();
116        while (enumerator && enumerator.hasMoreElements()) {
117            var resource = enumerator.getNext();
118            resource.QueryInterface(Components.interfaces.nsIRDFResource);
119           
120            var type; var name;
121            if (resource.ValueUTF8.match(new RegExp(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSEmail + "/", "i")))
122                { type = "email"; name = RegExp.rightContext }
123            else if (resource.ValueUTF8.match(new RegExp(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSNewsgroup + "/", "i")))
124                { type = "newsgroup"; name = RegExp.rightContext }
125            else if (resource.ValueUTF8.match(new RegExp(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSMaillist + "/", "i")))
126                { type = "maillist"; name = RegExp.rightContext }
127            else continue;
128           
129            var container = vI_rdfDatasource.getContainer(type);
130            vI_rdfDatasource.__setRDFValue(resource, "name", name);
131           
132            if (container.IndexOf(resource) == -1) container.AppendElement(resource);
133       
134            vI_notificationBar.dump(".");
135        }
136        vI_notificationBar.dump("\n");
137    },
138
139    __tagDefaultSMTP: function() {
140        vI_notificationBar.dump("upgrade: tagDefaultSMTP ");
141        for each (treeType in Array("email", "maillist", "newsgroup", "filter")) {
142            var enumerator = vI_rdfDatasource.getContainer(treeType).GetElements();
143            while (enumerator && enumerator.hasMoreElements()) {
144                var resource = enumerator.getNext();
145                resource.QueryInterface(Components.interfaces.nsIRDFResource);
146                var smtp = vI_rdfDatasource.__getRDFValue(resource, "smtp")
147                if (!smtp || smtp == "") vI_rdfDatasource.__setRDFValue(resource, "smtp", DEFAULT_SMTP_TAG);
148                vI_notificationBar.dump(".");
149            }
150        }
151        vI_notificationBar.dump("\n");
152    },
153
154    __createRDFContainers: function() {
155        var rdfContainerUtils = Components.classes["@mozilla.org/rdf/container-utils;1"].
156            getService(Components.interfaces.nsIRDFContainerUtils);
157
158        var storageRes = vI_rdfDatasource.rdfService
159            .GetResource(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSStorage);
160        var emailRes = vI_rdfDatasource.rdfService
161            .GetResource(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSEmail);
162        var maillistRes = vI_rdfDatasource.rdfService
163            .GetResource(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSMaillist);
164        var newsgroupRes = vI_rdfDatasource.rdfService
165            .GetResource(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSNewsgroup);
166        var filterRes = vI_rdfDatasource.rdfService
167            .GetResource(vI_rdfDatasource.rdfNS + vI_rdfDatasource.rdfNSFilter);
168        vI_rdfDatasource.__setRDFValue(emailRes, "name", "E-Mail");
169        vI_rdfDatasource.__setRDFValue(maillistRes, "name", "Mailing-List");
170        vI_rdfDatasource.__setRDFValue(newsgroupRes, "name", "Newsgroup");
171        vI_rdfDatasource.__setRDFValue(filterRes, "name", "Filter");
172
173        rdfContainerUtils.MakeBag(vI_rdfDatasource.rdfDataSource, storageRes);
174        rdfContainerUtils.MakeBag(vI_rdfDatasource.rdfDataSource, emailRes);
175        rdfContainerUtils.MakeBag(vI_rdfDatasource.rdfDataSource, maillistRes);
176        rdfContainerUtils.MakeBag(vI_rdfDatasource.rdfDataSource, newsgroupRes);
177        // use a sequence for the filters, order does matter
178        rdfContainerUtils.MakeSeq(vI_rdfDatasource.rdfDataSource, filterRes);
179       
180        var container = Components.classes["@mozilla.org/rdf/container;1"].
181            createInstance(Components.interfaces.nsIRDFContainer);
182       
183        // initialize container with storageRes
184        container.Init(vI_rdfDatasource.rdfDataSource, storageRes);
185        // append all new containers to storageRes
186        if (container.IndexOf(emailRes) == -1) container.AppendElement(emailRes);
187        if (container.IndexOf(maillistRes) == -1) container.AppendElement(maillistRes);
188        if (container.IndexOf(newsgroupRes) == -1) container.AppendElement(newsgroupRes);
189        if (container.IndexOf(filterRes) == -1) container.AppendElement(filterRes);
190       
191        vI_rdfDatasource.__initContainers();
192       
193        vI_upgrade.__transferAllResources();
194    },
195   
196    extUpgrade : function() {
197        var currentVersion = vI_rdfDatasource.getCurrentExtFileVersion();
198        vI_notificationBar.dump("checking for previous version, found " + 
199            currentVersion + "\nextension-upgrade required.\n")
200        switch (currentVersion) {
201            case null:
202                vI_upgrade.__transferAllVIdentityABookToRDF(); // no break
203            default:
204                vI_upgrade.__transferMovedUserPrefs(currentVersion);
205                vI_upgrade.__removeObsoleteUserPrefs(currentVersion);
206        }
207        vI_rdfDatasource.storeExtVersion();
208        vI_notificationBar.dump("extension-upgrade to " + vI_rdfDatasource.getCurrentExtFileVersion() + " done.\n\n");
209    },
210       
211    __transferMovedUserPrefs : function(currentVersion) {
212        // transfer renamed preferences
213        var transferPrefs = [   { version : "0.5.3",
214                    prefs : Array({ sourcePref : "smart_reply_ask", targetPref : "idSelection_ask" },
215                        { sourcePref : "smart_reply_ask_always", targetPref : "idSelection_ask_always" },
216                        { sourcePref : "smart_reply_autocreate", targetPref : "idSelection_autocreate" },
217                        { sourcePref : "smart_timestamp", targetPref : "autoTimestamp" },
218                        { sourcePref : "storage_prefer_smart_reply", targetPref : "idSelection_storage_prefer_smart_reply" },
219                        { sourcePref : "storage_ignore_smart_reply", targetPref : "idSelection_storage_ignore_smart_reply" }) }];
220        // remove obsolete preference-tree virtualIdentity
221        for (var i = 0; i < transferPrefs.length; i++) {
222            // if former version of extension was at least 0.5.0, start with WizardPage 0.5.2
223            if (!currentVersion || (vI_upgrade.versionChecker.compare(currentVersion, transferPrefs[i].version) < 0)) {
224                // remove any obsolete preferences under extensions.virtualIdentity
225                vI_notificationBar.dump("transfer changed preferences of pre-" + transferPrefs[i].version + " release:\n")
226                for each (transferPref in transferPrefs[i].prefs) {
227                    try {   vI_upgrade.preferences.setBoolPref(transferPref.targetPref, 
228                            vI_upgrade.preferences.getBoolPref(transferPref.sourcePref));
229                        vI_upgrade.preferences.clearUserPref(transferPref.sourcePref);
230                        vI_notificationBar.dump(".") 
231                    }
232                    catch (e) { };
233                }
234                vI_notificationBar.dump("done.\n")
235            }
236        }
237    },
238   
239    __removeObsoleteUserPrefs : function(currentVersion) {
240        var obsoletePrefs = [   { version : "0.5.0",
241                    prefs : Array("aBook_use", "aBook_storedefault", "aBook_dont_update_multiple",
242                    "aBook_show_switch", "aBook_warn_update", "aBook_use_for_smart_reply",
243                    "aBook_prefer_smart_reply", "aBook_ignore_smart_reply", "aBook_warn_vI_replace",
244                    "aBook_use_non_vI", "aBook_notification", "storeVIdentity", "experimental",
245                    "storage_use_for_smart_reply") },
246                    { version : "0.5.3", prefs : Array("storage_use_for_smart_reply") },
247                    { version : "0.5.6", prefs : Array("copyEnigmailSettings") } ];
248        // remove obsolete preference-tree virtualIdentity
249        for (var i = 0; i < obsoletePrefs.length; i++) {
250            // if former version of extension was at least 0.5.0, start with WizardPage 0.5.2
251            if (!currentVersion || (vI_upgrade.versionChecker.compare(currentVersion, obsoletePrefs[i].version) < 0)) {
252                // remove any obsolete preferences under extensions.virtualIdentity
253                vI_notificationBar.dump("removing obsolete preferences of pre-" + obsoletePrefs[i].version + " release:\n")
254                for each (pref in obsoletePrefs[i].prefs) {
255                    try { vI_upgrade.preferences.clearUserPref(pref); vI_notificationBar.dump(".") }
256                    catch (e) { };
257                }
258                vI_notificationBar.dump("done.\n")
259            }
260        }
261    },
262
263    CardFields : Array("Custom1", "Custom2", "Custom3", "Custom4", "Notes"),
264    // --------------------------------------------------------------------
265    // remove all VirtualIdentity-related Information from the AddressBook
266    // and transfer it to the RDF File.
267    __transferAllVIdentityABookToRDF : function() {
268        var returnVar = { prop : null, counter : 0, warning : true }
269        for each (returnVar.prop in vI_upgrade.CardFields) {
270            var queryString = "?(or(" +returnVar.prop + ",c,vIdentity:))";
271            returnVar.prop = returnVar.prop.toLowerCase();
272            returnVar = vI_storage._walkTroughCards(queryString,vI_upgrade.__transferVIdentityABookToRDF, returnVar )
273        }
274        vI_notificationBar.dump("\ntransferred " + returnVar.counter + " VirtualIdentity information items from AddressBook to RDF.\n")
275    },
276   
277    __transferVIdentityABookToRDF: function(addrbook, Card, returnVar) {
278        if (!Card[returnVar.prop].match(/^vIdentity:/)) return returnVar;
279        if (returnVar.warning) {
280            vI_notificationBar.dump("transferring VirtualIdentity information from AddressBook to RDF file,\nthis might take a while:\n");
281            returnVar.warning = false
282        }
283       
284        var newFullEmail=Card[returnVar.prop].replace(/vIdentity: /,"");
285        var infoIndex = newFullEmail.indexOf(" (id")
286        if (!infoIndex) infoIndex = newFullEmail.indexOf(" (smtp")
287        var info = null; var id= null; var smtp = null;
288        if ( infoIndex != -1) {
289            info = newFullEmail.substr(infoIndex+2).replace(/\)/,"").split(/,/)
290            newFullEmail = newFullEmail.substr(0, infoIndex);
291        }
292        if ( info && info[0] ) id = info[0];
293        if ( info && info[1] ) smtp = info[1];
294       
295        var splitted = vI_upgrade.__parseAddress(newFullEmail);
296        //~ alert(splitted.email + "++" + splitted.name + "++" + splitted.combinedName)
297       
298        var localIdentityData = new identityData(splitted.email, splitted.name, id, smtp, null)
299       
300        vI_rdfDatasource.updateRDF(vI_helper.combineNames(Card.displayName, Card.primaryEmail),
301                        "email", localIdentityData, true, true, null, null)
302        if (Card.secondEmail.replace(/^\s+|\s+$/g,""))
303            vI_rdfDatasource.updateRDF(vI_helper.combineNames(Card.displayName, Card.secondEmail),
304                    "email", localIdentityData, true, true, null, null)
305       
306        Card[returnVar.prop] = "";
307        Card.editCardToDatabase("");
308        vI_notificationBar.dump(".");
309        return { prop: returnVar.prop, counter : ++returnVar.counter, warning : returnVar.warning };
310    },
311   
312    // by now in vI, not accessible from here. Best change all references to vI_helper.
313    __parseAddress : function(address) {
314        //~ vI_notificationBar.dump("## v_identity: getAddress: parsing '" + address + "'\n")
315        var name = ""; email = "";
316        // prefer an email address separated with < >, only if not found use any other
317        if (address.match(/<\s*[^>\s]*@[^>\s]*\s*>/) || address.match(/<?\s*[^>\s]*@[^>\s]*\s*>?/) || address.match(/$/)) {
318            name = RegExp.leftContext + RegExp.rightContext
319            email = RegExp.lastMatch
320            email = email.replace(/\s+|<|>/g,"")
321            name = name.replace(/^\s+|\s+$/g,"")
322        }
323        //~ vI_notificationBar.dump("## v_identity: getAddress: address '" + address + "' name '" +
324            //~ name + "' email '" + email + "'\n");
325        return { name: name,
326             email: email,
327             combinedName: name + " <" + email + ">"}
328    },
329
330    openURL : function(aURL) {
331            var uri = Components.classes["@mozilla.org/network/standard-url;1"].createInstance(Components.interfaces.nsIURI);
332            var protocolSvc = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"].getService(Components.interfaces.nsIExternalProtocolService);
333
334            uri.spec = aURL;
335            protocolSvc.loadUrl(uri);
336        }
337}
338// start init only if wizard is shown, so it is done in vI_upgrade.xul
339// window.addEventListener('load', vI_upgrade.init, true);
Note: See TracBrowser for help on using the repository browser.