Thunderbird - go offline programatically

Talk about add-ons and extension development.
Post Reply
Kaven01
Posts: 3
Joined: September 1st, 2016, 12:09 am

Thunderbird - go offline programatically

Post by Kaven01 »

Hello, is it possible to switch offline/online state programmatically from extension? Documentation is very limited and I can't find answer there.

For me, it would be even possible to use some kind of automation and switch offline status from outside Thunderbird. But it seems that extension is correct way to do it.
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Thunderbird - go offline programatically

Post by morat »

You could create a command line handler with an extension.

Usage:

thunderbird.exe -offline
ThunderbirdPortable.exe -offline

Instructions:

* create tweaks.zip file (contains install.rdf, chrome.manifest, commandline.js)
* rename tweaks.zip file to tweaks.xpi
* install tweaks.xpi file

Source:

* install.rdf

Code: Select all

<?xml version="1.0"?>

<RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  xmlns:em="http://www.mozilla.org/2004/em-rdf#">

  <Description about="urn:mozilla:install-manifest">
    <em:id>tweaks@example.com</em:id>
    <em:type>2</em:type>
    <em:unpack>true</em:unpack>
    <em:name>Tweaks</em:name>
    <em:version>1.0</em:version>
    <em:creator>morat</em:creator>
    <em:description>Personalize Thunderbird.</em:description>
    <em:homepageURL>http://forums.mozillazine.org/viewtopic.php?f=19&t=3021875</em:homepageURL>

    <!-- Thunderbird -->
    <em:targetApplication>
      <Description>
        <em:id>{3550f703-e582-4d05-9a08-453d09bdfdc6}</em:id>
        <em:minVersion>38.0</em:minVersion>
        <em:maxVersion>99.0</em:maxVersion>
      </Description>
    </em:targetApplication>

  </Description>

</RDF>
* chrome.manifest

Code: Select all

content tweaks .
locale tweaks en-US .
skin tweaks classic/1.0 .

component {3550f703-e582-4d05-9a08-453d09bdfdc6} commandline.js
contract @mozilla.org/commandlinehandler/general-startup;1?type=tweaks {3550f703-e582-4d05-9a08-453d09bdfdc6}
category command-line-handler m-tweaks @mozilla.org/commandlinehandler/general-startup;1?type=tweaks
* commandline.js

Code: Select all

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cr = Components.results;
var Cu = Components.utils;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

function TweaksCommandLineHandler() {}

TweaksCommandLineHandler.prototype = {
  classID: Components.ID("{3550f703-e582-4d05-9a08-453d09bdfdc6}"),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsICommandLineHandler]),
  handle: function (commandLine) {
    if (commandLine.handleFlag("offline", false)) {
      if (commandLine.state == Ci.nsICommandLine.STATE_REMOTE_AUTO) {
        // toggle the offline status
        // preference offline.download.download_messages
        // 0: ask (default), 1: always download, 2: never download
        var win = Services.wm.getMostRecentWindow("mail:3pane");
        win.MailOfflineMgr.toggleOfflineStatus();
      }
      commandLine.preventDefault = true;
    }
  },
  helpInfo: "  -offline           Toggle offline status.\n",
};

var NSGetFactory = XPCOMUtils.generateNSGetFactory([TweaksCommandLineHandler]);
Thunderbird 45.3.0
Windows 7 SP1 32-bit
Kaven01
Posts: 3
Joined: September 1st, 2016, 12:09 am

Re: Thunderbird - go offline programatically

Post by Kaven01 »

Wow, that was EXACTLY what I wanted, neatly packed as a present :-) Thank you very much! Can some info about Services.wm.getMostRecentWindow and win.MailOfflineMgr.toggleOfflineStatus() be found in documentation? Maybe I will have to do some addons, and these things are just what I could not find.
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Thunderbird - go offline programatically

Post by morat »

You can use the DOM Inspector extension to find the command.

File > Inspect Chrome Document > chrome://messenger/content/messenger.xul
Edit > Find Nodes > Search By Attribute

Attribute: label
Value: Work Offline

Code: Select all

<!ENTITY offlineGoOfflineCmd.label "Work Offline">
http://dxr.mozilla.org/comm-esr45/sourc ... senger.dtd

Code: Select all

<menuitem id="goOfflineMenuItem"
          type="checkbox"
          label="&offlineGoOfflineCmd.label;"
          accesskey="&offlineGoOfflineCmd.accesskey;"
          oncommand="MailOfflineMgr.toggleOfflineStatus();"/>
http://dxr.mozilla.org/comm-esr45/sourc ... verlay.xul

Finding already opened windows
http://developer.mozilla.org/en-US/docs ... ed_windows
http://developer.mozilla.org/en-US/docs ... ntWindow()
Kaven01
Posts: 3
Joined: September 1st, 2016, 12:09 am

Re: Thunderbird - go offline programatically

Post by Kaven01 »

I will have to study it more, but I'm begining to understand. Thanks!
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Thunderbird - go offline programatically

Post by morat »

You're welcome.
Post Reply