Search code examples
javascriptxulthunderbirdthunderbird-addon

How to programmatically add a column to all message list views in Thunderbird


I have created an extension that adds a new column to the main message list, basically following the New Column Howto. Now I would like to place the column directly before the subject column, which appeared to be possible by not persisting the ordinal field and adding a insertbefore attribute. However, I still need to manually choose the column from the column chooser to make it visible, and I also need to do this for each folder. Is there a way to insert this automatically to all message views just before the subject column? My goal is that the column appears automatically in all possible message view when the extension is installed.

My XUL overlay currently looks like this:

<overlay id="colovl"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script type="application/x-javascript" src="chrome://myext/content/column_overlay.js"/>
  <tree id="threadTree">
    <treecols id="threadCols">
    <splitter class="tree-splitter" />
    <treecol id="MyCol" insertbefore="subjectCol" fixed="true" 
           flex="2" hidden="false"
           class="treecol-image"
           label="MyCol" tooltiptext="Click to sort by MyCol" />
    </treecols>
  </tree>
</overlay>

Solution

  • Thunderbird 3.1 has an user exposed action to apply the current's folder column layout to other folders and their subfolders. As a user you can use this action through the folder configuration menu's "Apply columns to ..." action.

    This means what you want to do is doable.

    The implementation for this action is contained in threadPaneColumnPicker.xml. Browsing through the source code I'd say the interesting part starts at line 167. I copied parts of the code:

    let destFolder = event.originalTarget._folder;
    let parent = event.originalTarget.parentNode;
    while (parent != noChildrenPopup && parent != yesChildrenPopup) {
       parent = parent.parentNode;
    }
    
    // Get the current folder's column state.
    let propName = gFolderDisplay.PERSISTED_COLUMN_PROPERTY_NAME;
    let dbFolderInfo =
      gFolderDisplay.displayedFolder.msgDatabase.dBFolderInfo;
    let columnStateString = dbFolderInfo.getCharProperty(propName);
    // Now propagate appropriately...
    if (useChildren) {
      // Generate an observer notification when we have finished configuring
      // all folders.  This is currently done for the benefit of our mozmill
      // tests.
      function observerCallback() {
        let obsService =
           Components.classes["@mozilla.org/observer-service;1"]
                     .getService(Components.interfaces.nsIObserverService);
        obsService.notifyObservers(gFolderDisplay.displayedFolder,
                                   "msg-folder-columns-propagated", "");
      }
      MailUtils.setStringPropertyOnFolderAndDescendents(propName,
                                                        columnStateString,
                                                        destFolder,
                                                        observerCallback);
    }
    

    I'm not sure that you can reuse the code as-is but it should give you inspiration for a starting point.