Bug 194095 - Fix address book import from CSV files with more fields than Thunderbird has. r=mkmelin
authorGeoff Lankow <geoff@darktrojan.net>
Wed, 03 Mar 2021 22:05:31 +0000
changeset 31840 e71240e4406f60d9ac8000f50c45466373ed33dd
parent 31839 1d767781ecbaf30a720aa0dd0798f17865255dad
child 31841 af9d501aef3a331b263444b869f4941c14f66ca1
push id18526
push usergeoff@darktrojan.net
push dateWed, 03 Mar 2021 22:07:45 +0000
treeherdercomm-central@3b1645fde19a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmkmelin
bugs194095
Bug 194095 - Fix address book import from CSV files with more fields than Thunderbird has. r=mkmelin Differential Revision: https://phabricator.services.mozilla.com/D106869
mailnews/import/content/fieldMapImport.js
mailnews/import/src/nsTextImport.cpp
--- a/mailnews/import/content/fieldMapImport.js
+++ b/mailnews/import/content/fieldMapImport.js
@@ -60,52 +60,68 @@ function IndexInMap(index) {
   return false;
 }
 
 function ListFields() {
   if (top.fieldMap == null) {
     return;
   }
 
-  var count = top.fieldMap.mapSize;
-  var index;
+  // Add rows for every mapped field.
+  let count = top.fieldMap.mapSize;
   for (let i = 0; i < count; i++) {
-    index = top.fieldMap.GetFieldMap(i);
+    let index = top.fieldMap.GetFieldMap(i);
+    if (index == -1) {
+      continue;
+    }
     AddFieldToList(
       top.fieldMap.GetFieldDescription(index),
       index,
       top.fieldMap.GetFieldActive(i)
     );
   }
 
+  // Add rows every possible field we don't already have a row for.
   count = top.fieldMap.numMozFields;
   for (let i = 0; i < count; i++) {
     if (!IndexInMap(i)) {
       AddFieldToList(top.fieldMap.GetFieldDescription(i), i, false);
     }
   }
+
+  // Add dummy rows if the data has more fields than Thunderbird does.
+  let data = top.addInterface.GetData("sampleData-0");
+  if (!(data instanceof Ci.nsISupportsString)) {
+    return;
+  }
+  count = data.data.split("\n").length;
+  for (let i = gListbox.itemCount; i < count; i++) {
+    AddFieldToList(null, -1, false);
+  }
 }
 
 function CreateField(name, index, on) {
   var item = document.createXULElement("richlistitem");
   item.setAttribute("align", "center");
   item.setAttribute("field-index", index);
   item.setAttribute("allowevents", "true");
 
   var checkboxCell = document.createXULElement("hbox");
   checkboxCell.setAttribute("style", "width: var(--column1width)");
   let checkbox = document.createXULElement("checkbox");
-  if (on) {
+  if (!name) {
+    checkbox.disabled = true;
+  } else if (on) {
     checkbox.setAttribute("checked", "true");
   }
   checkboxCell.appendChild(checkbox);
 
   var firstCell = document.createXULElement("label");
   firstCell.setAttribute("style", "width: var(--column2width)");
-  firstCell.setAttribute("value", name);
+  firstCell.setAttribute("value", name || "");
 
   var secondCell = document.createXULElement("label");
   secondCell.setAttribute("class", "importsampledata");
   secondCell.setAttribute("flex", "1");
 
   item.appendChild(checkboxCell);
   item.appendChild(firstCell);
   item.appendChild(secondCell);
--- a/mailnews/import/src/nsTextImport.cpp
+++ b/mailnews/import/src/nsTextImport.cpp
@@ -577,16 +577,19 @@ NS_IMETHODIMP ImportAddressImpl::InitFie
           if (!(*pStr)) break;
           while (*pStr && (*pStr >= '0') && (*pStr <= '9')) {
             fNum *= 10;
             fNum += (*pStr - '0');
             pStr++;
           }
           while (*pStr && (*pStr != ',')) pStr++;
           if (*pStr == ',') pStr++;
+          if (!active) {
+            fNum *= -1; // Re-add the stripped minus sign.
+          }
           fieldMap->SetFieldMap(-1, fNum);
           fieldMap->SetFieldActive(fIndex, active);
           fIndex++;
         }
         if (!fIndex) {
           int num;
           fieldMap->GetNumMozFields(&num);
           fieldMap->DefaultFieldMap(num);