Changeset 2bdd22


Ignore:
Timestamp:
Sep 11, 2018, 12:52:40 PM (3 years ago)
Author:
rene <rene@…>
Branches:
ng_0.9
Children:
668208
Parents:
91994f
Message:

fixed broken rdf-import (again)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • modules/vI_rdfDatasource.js

    r91994f r2bdd22  
    6767};
    6868
    69 function rdfDatasource(currentWindow, rdfFileName, dontRegisterObserver) {
     69function rdfDatasource(currentWindow, rdfFileName, dontRegisterObserver, dontChange = false) {
    7070  this._currentWindow = currentWindow;
    7171  this._rdfFileName = rdfFileName;
    7272  this._dontRegisterObserver = dontRegisterObserver;
    73   if (this._rdfFileName) this.init();
     73  if (this._rdfFileName) this.init(dontChange);
    7474  if (!this._dontRegisterObserver) this.AccountManagerObserver.register(this);
    7575  this._extVersion = get3PaneWindow().virtualIdentityExtension.extensionVersion;
     
    9393  _rdfNSAccounts: "vIAccounts",
    9494  _rdfNSIdentities: "vIAccounts/id",
     95  // required for import and update of 0.10/0.0.6 rdf
     96  _rdfNSSMTPservers: "vIAccounts/smtp",
    9597
    9698  _virtualIdentityID: "{dddd428e-5ac8-4a81-9f78-276c734f75b8}",
     
    111113
    112114  _identityContainer: Components.classes["@mozilla.org/rdf/container;1"]
     115    .createInstance(Components.interfaces.nsIRDFContainer),
     116
     117  // required for import and update of 0.10/0.0.6 rdf
     118  _smtpContainer: Components.classes["@mozilla.org/rdf/container;1"]
    113119    .createInstance(Components.interfaces.nsIRDFContainer),
    114120
     
    125131    case "identity":
    126132      return this._identityContainer;
     133    // required for import and update of 0.10/0.0.6 rdf
     134    case "smtp":
     135      return this._smtpContainer;
    127136    }
    128137    return null;
    129138  },
    130139
    131   init: function () {
     140  init: function (dontChange = false) {
    132141    //         Log.debug("init.");
    133142
     
    135144    if (!this._rdfDataSource) return;
    136145    this._initContainers();
    137     if (this.rdfUpgradeRequired()) this.rdfUpgrade();
    138 
    139     // store version everytime to recognize downgrades later
    140     this.storeRDFVersion();
     146    if (!dontChange) {
     147      if (this.rdfUpgradeRequired()) this.rdfUpgrade();
     148
     149      // store version everytime to recognize downgrades later
     150      this.storeRDFVersion();
     151    }
    141152
    142153    //         this.refreshAccountInfo();
     
    185196        .GetResource(this._rdfNS + this._rdfNSIdentities);
    186197      this._identityContainer.Init(this._rdfDataSource, storageRes);
     198      storageRes = this._rdfService
     199        .GetResource(this._rdfNS + this._rdfNSSMTPservers);
     200      this._smtpContainer.Init(this._rdfDataSource, storageRes);
    187201    } catch (e) {};
    188202  },
     
    256270    var identityRes = this._rdfService
    257271      .GetResource(this._rdfNS + this._rdfNSIdentities);
    258 
     272    var smtpRes = this._rdfService
     273      .GetResource(this._rdfNS + this._rdfNSSMTPservers);
    259274    this._setRDFValue(accountRes, "name", "Accounts");
    260275    this._setRDFValue(identityRes, "name", "Identities");
     276    this._setRDFValue(smtpRes, "name", "SMTP-Server");
    261277
    262278    rdfContainerUtils.MakeBag(this._rdfDataSource, accountRes);
    263279    rdfContainerUtils.MakeBag(this._rdfDataSource, identityRes);
     280    rdfContainerUtils.MakeBag(this._rdfDataSource, smtpRes);
    264281
    265282    var accountContainer = Components.classes["@mozilla.org/rdf/container;1"].
     
    270287    // append all new containers to accountRes
    271288    if (accountContainer.IndexOf(identityRes) == -1) accountContainer.AppendElement(identityRes);
     289    if (accountContainer.IndexOf(smtpRes) == -1) accountContainer.AppendElement(smtpRes);
    272290
    273291    this._initContainers();
     
    375393      this._unsetRDFValue(resource, "email", this._getRDFValue(resource, "email"))
    376394      this._identityContainer.RemoveElement(resource, false);
     395    }
     396
     397    enumerator = this._smtpContainer.GetElements();
     398    while (enumerator && enumerator.hasMoreElements()) {
     399      var resource = enumerator.getNext();
     400      resource.QueryInterface(Components.interfaces.nsIRDFResource);
     401      this._unsetRDFValue(resource, "label", this._getRDFValue(resource, "label"))
     402      this._unsetRDFValue(resource, "hostname", this._getRDFValue(resource, "hostname"))
     403      this._unsetRDFValue(resource, "username", this._getRDFValue(resource, "username"))
     404      this._smtpContainer.RemoveElement(resource, false);
    377405    }
    378406  },
     
    447475
    448476  repairMismatch: function (type, mismatchItems) {
    449     // type might be 'id' or 'smtpid' - dependent on trigger
     477    // type might be 'identity' or 'smtp' or 'smtpid' - dependent on trigger
    450478    for (var i = 0; i < mismatchItems.length; i++) {
    451       Log.debug("repairAccountMismatch change " + mismatchItems[i].oldkey + " into " + mismatchItems[i].key);
     479      if (mismatchItems[i].key)
     480        Log.debug("repairAccountMismatch change " + mismatchItems[i].oldkey + " into " + mismatchItems[i].key);
     481      else
     482        Log.debug("repairAccountMismatch remove " + mismatchItems[i].oldkey);
    452483      // search relevant Identities
    453484      for (let treeType of Array("email", "maillist", "newsgroup", "filter")) {
     
    456487          var resource = enumerator.getNext();
    457488          resource.QueryInterface(Components.interfaces.nsIRDFResource);
    458          
    459489          // old smtp (stored in pre-0.10 rdf) and id have to match, set new id
    460490          if (type == 'smtpid') {
     
    462492            var id = mismatchItems[i].oldkey.split("-")[1];
    463493            if (this._getRDFValue(resource, 'id') == id && this._getRDFValue(resource, 'smtp') == smtp) {
    464               if (mismatchItems[i].key == "") this._unsetRDFValue(resource, 'id', id)
     494              if (!mismatchItems[i].key) this._unsetRDFValue(resource, 'id', id)
    465495              else this._setRDFValue(resource, 'id', mismatchItems[i].key)
    466496            }
    467497          }
    468           // 'id' replacement
    469           else if (this._getRDFValue(resource, 'id') == mismatchItems[i].oldkey) {
    470             if (mismatchItems[i].key == "") this._unsetRDFValue(resource, 'id', mismatchItems[i].oldkey)
    471             else this._setRDFValue(resource, 'id', mismatchItems[i].key)
     498          // 'identity' replacement
     499          else if (type == 'identity') {
     500            if (this._getRDFValue(resource, 'id') == mismatchItems[i].oldkey) {
     501              if (!mismatchItems[i].key) this._unsetRDFValue(resource, 'id', mismatchItems[i].oldkey)
     502              else this._setRDFValue(resource, 'id', mismatchItems[i].key)
     503            }
     504          }
     505          // 'smtp' replacement
     506          else if (type == 'smtp') {
     507            if (this._getRDFValue(resource, 'smtp') == mismatchItems[i].oldkey) {
     508              if (!mismatchItems[i].key) this._unsetRDFValue(resource, 'smtp', mismatchItems[i].oldkey)
     509              else this._setRDFValue(resource, 'smtp', mismatchItems[i].key)
     510            }
    472511          }
    473512        }
    474513      }
     514    }
     515  },
     516
     517  getRelevantSMTPs: function () {
     518    var relevantSMTPs = new Object();
     519    // search relevant SMTPs
     520    for (let treeType of Array("email", "maillist", "newsgroup", "filter")) {
     521      var enumerator = this.getContainer(treeType).GetElements();
     522      while (enumerator && enumerator.hasMoreElements()) {
     523        var resource = enumerator.getNext();
     524        resource.QueryInterface(Components.interfaces.nsIRDFResource);
     525        var smtp = this._getRDFValue(resource, "smtp")
     526        if (smtp && smtp != DEFAULT_SMTP_TAG) {
     527          if (!relevantSMTPs[smtp]) relevantSMTPs[smtp] = 1;
     528          else relevantSMTPs[smtp] += 1;
     529        }
     530      }
     531    }
     532    return relevantSMTPs;
     533  },
     534
     535  searchSmtpMismatch: function () {
     536    Log.debug("searchSmtpMismatch");
     537
     538    var relevantSMTPs = this.getRelevantSMTPs();
     539    var mismatchSMTPs = [];
     540
     541    for (var smtp in relevantSMTPs) {
     542      var servers, smtpService = Components.classes["@mozilla.org/messengercompose/smtp;1"]
     543        .getService(Components.interfaces.nsISmtpService);
     544      // check for new https://hg.mozilla.org/comm-central/rev/fab9e5145cd4 smtpService
     545      if (typeof (smtpService.servers) == "object") servers = smtpService.servers;
     546      else servers = smtpService.smtpServers;
     547
     548      var found = false;
     549      while (servers && servers.hasMoreElements()) {
     550        var server = servers.getNext();
     551        if (server instanceof Components.interfaces.nsISmtpServer &&
     552          !server.redirectorType && smtp == server.key) {
     553          found = true;
     554          break;
     555        }
     556      }
     557      var resource = this._rdfService.GetResource(this._rdfNS + this._rdfNSSMTPservers + "/" + smtp);
     558      var rdfSMTPlabel = this._getRDFValue(resource, "label");
     559      var rdfHostname = this._getRDFValue(resource, "hostname");
     560      var rdfUsername = this._getRDFValue(resource, "username")
     561      if (!found || rdfSMTPlabel != (server.description ? server.description : server.hostname) && rdfHostname != server.hostname)
     562        mismatchSMTPs.push({
     563          oldkey: smtp,
     564          label: rdfSMTPlabel,
     565          ext1: rdfHostname,
     566          ext2: rdfUsername,
     567          count: relevantSMTPs[smtp],
     568          key: ""
     569        })
     570    }
     571    if (mismatchSMTPs.length > 0) {
     572      Log.debug(" found mismatches on smtp(s).");
     573      get3PaneWindow().openDialog("chrome://v_identity/content/vI_rdfAccountMismatchDialog.xul", 0,
     574        "chrome, dialog, modal, alwaysRaised, resizable=yes", "smtp", mismatchSMTPs,
     575        /* callback: */
     576        this).focus();
     577      return true;
     578    } else {
     579      Log.debug(" found no mismatch");
     580      return false;
    475581    }
    476582  },
     
    490596        this._setRDFValue(resource, "fullName", identity.fullName);
    491597        this._setRDFValue(resource, "email", identity.email);
     598        if (identity.smtpServerKey)
     599          this._setRDFValue(resource, "smtp", identity.smtpServerKey);
     600        else
     601          this._setRDFValue(resource, "smtp", virtualIdentityExtension.DEFAULT_SMTP_TAG);
    492602
    493603        var position = this._identityContainer.IndexOf(resource); // check for index in new recType
     
    496606      }
    497607    }
     608
     609    function storeSmtp(server, parent) {
     610      //             Log.debug("storeAccounts smtp store id " + server.key);
     611      var resource = parent._rdfService.GetResource(parent._rdfNS + parent._rdfNSSMTPservers + "/" + server.key);
     612      parent._setRDFValue(resource, "label", (server.description ? server.description : server.hostname));
     613      parent._setRDFValue(resource, "hostname", server.hostname);
     614      parent._setRDFValue(resource, "username", server.username);
     615      var position = parent._smtpContainer.IndexOf(resource); // check for index in new recType
     616      if (position != -1) parent._smtpContainer.InsertElementAt(resource, position, false);
     617      else parent._smtpContainer.AppendElement(resource);
     618    }
     619
     620    var servers, smtpService = Components.classes["@mozilla.org/messengercompose/smtp;1"]
     621      .getService(Components.interfaces.nsISmtpService);
     622    // check for new https://hg.mozilla.org/comm-central/rev/fab9e5145cd4 smtpService
     623    if (typeof (smtpService.servers) == "object") servers = smtpService.servers;
     624    else servers = smtpService.smtpServers;
     625
     626    while (servers && servers.hasMoreElements()) {
     627      var server = servers.getNext();
     628      if (server instanceof Components.interfaces.nsISmtpServer && !server.redirectorType) storeSmtp(server, this);
     629    }
     630
     631    //         Log.debug("storeAccounts done");
    498632  },
    499633
     
    828962    _uninstall: false,
    829963    observe: function (subject, topic, data) {
    830       if (topic == "am-acceptChanges") {
    831         Log.debug("account/identity changes observed");
     964      if (topic == "am-smtpChanges" || topic == "am-acceptChanges") {
     965        Log.debug("account/smtp changes observed");
    832966        this.self.searchIdentityMismatch();
    833967        this.self.refreshAccountInfo();
     
    839973      var obsService = Components.classes["@mozilla.org/observer-service;1"].
    840974      getService(Components.interfaces.nsIObserverService)
     975      obsService.addObserver(this, "am-smtpChanges", false);
    841976      obsService.addObserver(this, "am-acceptChanges", false);
    842977    },
     
    846981      getService(Components.interfaces.nsIObserverService)
    847982      try {
     983        obsService.removeObserver(this, "am-smtpChanges");
    848984        obsService.removeObserver(this, "am-acceptChanges");
    849985      } catch (e) {};
     
    10281164  },
    10291165
     1166  _getMatchingSMTP: function (label, hostname, username) {
     1167    var servers, smtpService = Components.classes["@mozilla.org/messengercompose/smtp;1"]
     1168      .getService(Components.interfaces.nsISmtpService);
     1169    // check for new https://hg.mozilla.org/comm-central/rev/fab9e5145cd4 smtpService
     1170    if (typeof (smtpService.servers) == "object") servers = smtpService.servers;
     1171    else servers = smtpService.smtpServers;
     1172
     1173    while (servers && servers.hasMoreElements()) {
     1174      var server = servers.getNext();
     1175      if (server instanceof Components.interfaces.nsISmtpServer && !server.redirectorType)
     1176        if (label == (server.description ? server.description : server.hostname) || (hostname == server.hostname && username == server.username))
     1177          return server.key;
     1178    }
     1179    return null;
     1180  },
     1181
    10301182  _translateRelevantIDs: function () {
    10311183    var relevantIDs = this._rdfImportDataSource.getRelevantIDs();
     
    10671219  //--------------------------------------------------------------------------------------------------------
    10681220  //only required for update from pre 0.10 versions
     1221
     1222  _translateRelevantSMTPs: function () {
     1223    var relevantSMTPs = this._rdfImportDataSource.getRelevantSMTPs();
     1224    for (var smtp in relevantSMTPs) {
     1225      var resource = this._rdfService.GetResource(this._rdfImportDataSource._rdfNS + this._rdfImportDataSource._rdfNSSMTPservers + "/" + smtp);
     1226      var values = {
     1227        smtp: null,
     1228        label: null,
     1229        hostname: null,
     1230        username: null
     1231      }
     1232      values.label = this._rdfImportDataSource._getRDFValue(resource, "label");
     1233      values.hostname = this._rdfImportDataSource._getRDFValue(resource, "hostname");
     1234      values.username = this._rdfImportDataSource._getRDFValue(resource, "username");
     1235      values.smtp = this._getMatchingSMTP(values.label, values.hostname, values.username);
     1236      values.smtp = values.smtp ? values.smtp : "import_" + smtp;
     1237      relevantSMTPs[smtp] = values;
     1238      Log.debug("import: translate relevant SMTP from previous '" + smtp + "' to current '" + relevantSMTPs[smtp].smtp + "'");
     1239    }
     1240    return relevantSMTPs;
     1241  },
     1242
     1243  _storeMappedSMTPs: function (relevantSMTPs) {
     1244    for (var smtp in relevantSMTPs) {
     1245      if (relevantSMTPs[smtp].smtp == "import_" + smtp) {
     1246        var resource = this._rdfService
     1247          .GetResource(this._rdfDataSource._rdfNS + this._rdfDataSource._rdfNSSMTPservers + "/" + relevantSMTPs[smtp].smtp);
     1248        this._rdfDataSource._setRDFValue(resource, "label", relevantSMTPs[smtp].label);
     1249        this._rdfDataSource._setRDFValue(resource, "hostname", relevantSMTPs[smtp].hostname);
     1250        this._rdfDataSource._setRDFValue(resource, "username", relevantSMTPs[smtp].username);
     1251
     1252        var position = this._rdfDataSource._smtpContainer.IndexOf(resource); // check for index in new recType
     1253        if (position != -1) this._rdfDataSource._smtpContainer.InsertElementAt(resource, position, false);
     1254        else this._rdfDataSource._smtpContainer.AppendElement(resource);
     1255      }
     1256    }
     1257  },
     1258
    10691259  _rdfNSSMTPservers: "vIAccounts/smtp",
    10701260
     
    10751265    // search relevant SMTPs
    10761266    for (let treeType of Array("email", "maillist", "newsgroup", "filter")) {
    1077       var enumerator = this._rdfImportDataSource.getContainer(treeType).GetElements();
     1267      var enumerator = this._rdfDataSource.getContainer(treeType).GetElements();
    10781268      while (enumerator && enumerator.hasMoreElements()) {
    10791269        var resource = enumerator.getNext();
    10801270        resource.QueryInterface(Components.interfaces.nsIRDFResource);
    1081         var smtp = this._rdfImportDataSource._getRDFValue(resource, "smtp")
     1271        var smtp = this._rdfDataSource._getRDFValue(resource, "smtp")
    10821272        if (smtp) {
    1083           var id = this._rdfImportDataSource._getRDFValue(resource, "id")
     1273          var id = this._rdfDataSource._getRDFValue(resource, "id")
    10841274          if (id) {
    10851275            if (!SMTPIDCombinations[smtp + "-" + id]) SMTPIDCombinations[smtp + "-" + id] = 1;
     
    11131303          break;
    11141304
    1115       var resource = this._rdfImportDataSource._rdfService.GetResource(this._rdfImportDataSource._rdfNS + this._rdfNSSMTPservers + "/" + smtp);
    1116       var rdfSMTPlabel = this._rdfImportDataSource._getRDFValue(resource, "label");
     1305      var resource = this._rdfDataSource._rdfService.GetResource(this._rdfDataSource._rdfNS + this._rdfNSSMTPservers + "/" + smtp);
     1306      var rdfSMTPlabel = this._rdfDataSource._getRDFValue(resource, "label");
    11171307
    11181308      var servers = MailServices.smtp.servers;
     
    11301320      SMTPIDCombinations.push({
    11311321        oldkey: smtpid,
    1132         label: rdfSMTPlabel + " / ",
     1322        label: rdfSMTPlabel,
    11331323        ext1: AccountManager.getIdentity(id).identityName,
    1134         ext2: "(" + smtpName + ")",
     1324        ext2: smtpName,
    11351325        count: relevantSMTPs[smtpid],
    11361326        key: ""
     
    11421332        "chrome, dialog, modal, alwaysRaised, resizable=yes", "smtpid", SMTPIDCombinations,
    11431333        /* callback: */
    1144         this._rdfImportDataSource).focus();
     1334        this._rdfDataSource).focus();
    11451335      return true;
    11461336    } else {
    11471337      Log.debug(" found no mismatch");
    11481338      return false;
     1339    }
     1340  },
     1341
     1342  _removeSMTPEntries: function () {
     1343    // search and remove all smtp-entries
     1344    for (let treeType of Array("email", "maillist", "newsgroup", "filter")) {
     1345      var enumerator = this._rdfDataSource.getContainer(treeType).GetElements();
     1346      while (enumerator && enumerator.hasMoreElements()) {
     1347        var resource = enumerator.getNext();
     1348        this._rdfDataSource._unsetRDFValue(resource, 'smtp', this._rdfDataSource._getRDFValue(resource, 'smtp'))
     1349      }
    11491350    }
    11501351  },
     
    11951396
    11961397    // init Datasources
    1197     this._rdfImportDataSource = new rdfDatasource(this._currentWindow, importRdfDataFile.leafName, true);
     1398    this._rdfImportDataSource = new rdfDatasource(this._currentWindow, importRdfDataFile.leafName, true, true);
    11981399
    11991400    // search matching IDs for anyones used in import-file
    12001401    var relevantIDs = this._translateRelevantIDs();
    1201 
    1202     // extension from version 0.10 on does not use any extra SMTP information anymore
    1203     // and uses the SMTP-server from base identity instead.
    1204     // check if any collisions between previously stored SMTP and base identity exists and solve this.
    1205     Log.debug("import: search SMTP-ID mismatches");
    1206     this._searchSmtpIdMismatch();
     1402    var relevantSMTPs = this._translateRelevantSMTPs();
    12071403
    12081404    Log.debug("import: preparation done.");
     
    12101406    for (let treeType of Array("email", "maillist", "newsgroup", "filter")) {
    12111407      // re-initialize importDataSource to point rdfService to the right Resources
    1212       this._rdfImportDataSource = new rdfDatasource(this._currentWindow, importRdfDataFile.leafName, true);
     1408      this._rdfImportDataSource = new rdfDatasource(this._currentWindow, importRdfDataFile.leafName, true, true);
    12131409      var container = this._rdfImportDataSource.getContainer(treeType)
    12141410      if (container.GetCount() == 0) continue;
     
    12271423        var fullName = this._rdfImportDataSource._getRDFValue(resource, "fullName")
    12281424        var id = this._rdfImportDataSource._getRDFValue(resource, "id")
     1425       
    12291426        id = id ? relevantIDs[id].id : null
     1427    var smtp = this._rdfImportDataSource._getRDFValue(resource, "smtp")
     1428        smtp = (smtp && smtp != DEFAULT_SMTP_TAG) ? relevantSMTPs[smtp].smtp : smtp
     1429         
    12301430        var localIdentityData = new identityData(this._currentWindow, email, fullName, id,
    12311431          new identityDataExtras(this._currentWindow, this._rdfImportDataSource, resource))
     
    12341434        var resource = this._rdfDataSource._getRDFResourceForVIdentity(name, treeType);
    12351435        if (id) this._rdfDataSource._setRDFValue(resource, "id", id); // localIdentityData can only store valid id's, this one might be a temporary invalid id
     1436        if (smtp) this._rdfDataSource._setRDFValue(resource, "smtp", smtp); // localIdentityData can only store valid smtp's, this one might be a temporary invalid smtp
    12361437      }
    12371438    }
     
    12431444
    12441445    Log.debug("import: cleaning ID storages:");
    1245     this._rdfDataSource = new rdfDatasource(this._currentWindow, this._rdfFileName, true);
    12461446
    12471447    this._storeMappedIDs(relevantIDs);
    12481448    this._rdfDataSource.searchIdentityMismatch();
     1449    this._storeMappedSMTPs(relevantSMTPs);
     1450    this._rdfDataSource.searchSmtpMismatch();
     1451
     1452    // extension from version 0.10 on does not use any extra SMTP information anymore
     1453    // and uses the SMTP-server from base identity instead.
     1454    // check if any collisions between previously stored SMTP and base identity exists and solve this.
     1455    Log.debug("import: search SMTP-ID mismatches");
     1456    this._searchSmtpIdMismatch();
     1457   
     1458    this._removeSMTPEntries();
    12491459
    12501460    this._rdfDataSource.refreshAccountInfo();
Note: See TracChangeset for help on using the changeset viewer.