This content covers features introduced in Thunderbird 3

This page describes how to programmatically create a message query using gloda, Thunderbird's global database. See the gloda page for background on the component. See Gloda examples for more code samples.

Creating your first message query...

Import gloda

Gloda's API is exposed in the "Gloda" object that is contributed to your namespace. You can find the file, which includes doxygen markup of sorts, here: https://hg.mozilla.org/comm-central/file/tip/mailnews/db/gloda/modules/gloda.js

Components.utils.import("resource:///modules/gloda/public.js");

Create the query

let query = Gloda.newQuery(Gloda.NOUN_MESSAGE);

Add constraints to the query

Each constraint function takes one or more arguments which are "OR"ed together.  So if we had a "color" constraint such as query.color("red", "green", "blue") would mean any of red, green, or blue is okay. Constraints are "AND"ed together.  So if we had a "shape" constraint such as query.shape("square") and we combined it with the previous color constraint it would match a red square, a green square, or a blue square.

Create a collection from the query

Your listener is notified as messages are added to the collection as the database query completes. You may also see new messages show up if new messages are indexed that meet the constraints.

let myListener = {
  /* called when new items are returned by the database query or freshly indexed */
  onItemsAdded: function myListener_onItemsAdded(aItems, aCollection) {
  },
  /* called when items that are already in our collection get re-indexed */
  onItemsModified: function myListener_onItemsModified(aItems, aCollection) {
  },
  /* called when items that are in our collection are purged from the system */
  onItemsRemoved: function myListener_onItemsRemoved(aItems, aCollection) {
  },
  /* called when our database query completes */
  onQueryCompleted: function myListener_onQueryCompleted(aCollection) {
  }
};
let collection = query.getCollection(myListener);

Message attributes

Look at the pretty messages!

The max number of messages which are returned is governed by a preference:

"mailnews.database.global.search.msg.limit"

Although you can deal with the messages as they show up via the listener, the list of messages in the collection is available in collection.items. Each message is a GlodaMessage, whose database-row attributes are implemented/defined in gloda's datamodel.js. However, many attributes are dynamically contributed by built-in logic (fundattr.js and explattr.js) as well as other extensions.

The database-row attributes are:

Somewhat magic attributes:

Attributes provided by fundattr.js:

Attributes provided by explattr.js:

Want a GlodaMessage when you already have an nsIMsgDBHdr?

Gloda.getMessageCollectionForHeader(aMsrHdr, aListener, aData) returns a collection (with your provided listener and data). Keep in mind that gloda is not infallible and it's conceivable that your collection may end up with no messages in it.

Have a GlodaMessage and want the rest of the messages in the conversation?

glodaMessage.conversation.getMessagesCollection(aListener, aData) returns a collection that should contain all of the messages gloda knows about that belong to the conversation. Keep in mind that duplicate messages can and will appear. (A duplicate message is a message with the same Message-Id header.)