Querying Places

  • Revision slug: Querying_Places
  • Revision title: Querying Places
  • Revision id: 131253
  • Created:
  • Creator: SylvainPasche
  • Is current revision? No
  • Comment /* The result view interface */ Removed obsolete example and linked to Views

Revision Content

{{template.Fx_minversion_header(3)}}

Firefox history and bookmarks data is accessed using the "Places" query APIs. These APIs provide the ability to execute complex queries against history, bookmarks or both combined (targeting Firefox Alpha 6). The result of a query is an object which will contain a flat list, or tree structure of the matching data. The definitions of the query API and the resulting data structures is available in {{template.Source("toolkit/components/places/public/nsINavHistoryService.idl")}}. This page provides an introduction to some common operations, and example usage of the core APIs.

Executing a query

Places queries have several basic parts:

  • The query object: {{template.Interface("nsINavHistoryQuery")}}, holds the search parameters
  • The query options: {{template.Interface("nsINavHistoryQueryOptions")}}, allows configuration of the search result
  • The history service: {{template.Interface("nsINavHistoryService")}}, executes the query

The first first step is to create the query and options, and fill them with the parameters you want. Use nsINavHistoryService.getNewQuery() and nsINavHistoryService.getNewQueryOptions() to retrieve empty objects. The defaults for these objects will result in a query that returns all of your browser history in a flat list:

var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
                               .getService(Components.interfaces.nsINavHistoryService);

// no query parameters will get all history
// XXX default sorting is... ?
var options = historyService.getNewQueryOptions();

// no query parameters will return everything
var query = historyService.getNewQuery();

// execute the query
var result = historyService.executeQuery(query, options);

Result types

{{template.Interface("nsINavHistoryQueryOptions")}} has a resultType property that allows configuration of the grouping and level of detail to be returned in the results. The different values for this property are listed below. These values are also properties of nsINavHistoryQueryOptions, and are accessed like this: Components.interfaces.nsINavHistoryQueryOptions.RESULTS_AS_VISIT.


  • RESULTS_AS_URI: This is the default, and returns one result node of type RESULT_TYPE_URI for each URI that matches the query. The visit date for each node will be the last visit date for that URL.
  • RESULTS_AS_VISIT: This option returns one entry for each time a page was visited matching the given query. The result may contain duplicate entries for URLs, each with a different date. The nodes are of type RESULT_TYPE_VISIT, and provide a session ID for each visit. This session ID is the same for all pages that were reached by clicking links. A new session starts when the user types a new URL or follows a bookmark (XXX link to more details about what constitutes a session).
  • RESULTS_AS_FULL_VISIT: This is an extended version of RESULT_TYPE_VISIT. The results are of type RESULT_TYPE_FULL_VISIT and have additional information about the visit, such as the referring visit, and how the transition happened (typed, redirect, link, etc).

Basic Query Search Parameters

const unsigned long TIME_RELATIVE_EPOCH = 0 const unsigned long TIME_RELATIVE_TODAY = 1 const unsigned long TIME_RELATIVE_NOW = 2 attribute PRTime beginTime attribute unsigned long beginTimeReference readonly attribute boolean hasBeginTime readonly attribute PRTime absoluteBeginTime attribute PRTime endTime attribute unsigned long endTimeReference readonly attribute boolean hasEndTime readonly attribute PRTime absoluteEndTime attribute AString searchTerms readonly attribute boolean hasSearchTerms attribute long minVisits attribute long maxVisits attribute boolean onlyBookmarked attribute boolean domainIsHost attribute AUTF8String domain readonly attribute boolean hasDomain attribute boolean uriIsPrefix attribute nsIURI uri readonly attribute boolean hasUri attribute boolean annotationIsNot attribute AUTF8String annotation readonly attribute boolean hasAnnotation readonly attribute unsigned long folderCount

Basic Query Configuration Options

const unsigned short GROUP_BY_DAY = 0 const unsigned short GROUP_BY_HOST = 1 const unsigned short GROUP_BY_DOMAIN = 2 const unsigned short GROUP_BY_FOLDER = 3 const unsigned short SORT_BY_NONE = 0 const unsigned short SORT_BY_TITLE_ASCENDING = 1 const unsigned short SORT_BY_TITLE_DESCENDING = 2 const unsigned short SORT_BY_DATE_ASCENDING = 3 const unsigned short SORT_BY_DATE_DESCENDING = 4 const unsigned short SORT_BY_URI_ASCENDING = 5 const unsigned short SORT_BY_URI_DESCENDING = 6 const unsigned short SORT_BY_VISITCOUNT_ASCENDING = 7 const unsigned short SORT_BY_VISITCOUNT_DESCENDING = 8 const unsigned short SORT_BY_KEYWORD_ASCENDING = 9 const unsigned short SORT_BY_KEYWORD_DESCENDING = 10 const unsigned short SORT_BY_DATEADDED_ASCENDING = 11 const unsigned short SORT_BY_DATEADDED_DESCENDING = 12 const unsigned short SORT_BY_LASTMODIFIED_ASCENDING = 13 const unsigned short SORT_BY_LASTMODIFIED_DESCENDING = 14 const unsigned short SORT_BY_ANNOTATION_ASCENDING = 15 const unsigned short SORT_BY_ANNOTATION_DESCENDING = 16 const unsigned short RESULTS_AS_URI = 0 const unsigned short RESULTS_AS_VISIT = 1 const unsigned short RESULTS_AS_FULL_VISIT = 2 attribute unsigned short sortingMode attribute AUTF8String sortingAnnotation attribute unsigned short resultType attribute boolean excludeItems attribute boolean excludeQueries attribute boolean excludeReadOnlyFolders attribute boolean expandQueries attribute boolean includeHidden attribute boolean showSessions attribute unsigned long maxResults const unsigned short QUERY_TYPE_HISTORY = 0 const unsigned short QUERY_TYPE_BOOKMARKS = 1 const unsigned short QUERY_TYPE_UNIFIED = 2 attribute unsigned short queryType

Complex Queries

You can pass one or more {{template.Interface("nsINavHistoryQuery")}} objects to executeQueries(). Within one query object, all parameters are ANDed together. The conditions for different query objects are then ORed together. This allows for a simpler implementation and interface than a full logical operation with nested clauses while still being expressive.

Example of querying for any pages I've visited that contain the word "firefox" in the title/URL or that I've visited today from mozilla.org.

// first query object searches for "firefox" in title/URL
var query1 = historyService.getNewQuery();
query1.searchTerms = "firefox";

// second query object searches for visited in past 24 hours AND from mozilla.org
var query2 = historyService.getNewQuery();
query2.beginTimeReference = query2.TIME_RELATIVE_NOW;
query2.beginTime = -24 * 60 * 60 * 1000000; // 24 hours ago in microseconds
query2.endTimeReference = query2.TIME_RELATIVE_NOW;
query2.endTime = 0; // now
query2.domain = "mozilla.org";

var result = historyService.executeQueries([query1, query2], 2, options);
Note: Keyword searching doesn't work correctly across OR queries. The current behavior does the normal query and then selects keywords from the first query and filters all the results. (In other words, the keywords from the first query are ANDed with all queries.) Keywords from subsequent query objects are ignored. This is {{template.Bug(320332)}}.

Bookmark queries

There is a quick-start for doing simple bookmark queries in Accessing Bookmarks.

The contents of bookmark folders can be retrieved by setting the "folders" member in the query object. This item is an array of folder IDs from the bookmark service. Typically, you will only have one folder ID in this list, which will given you the contents of that folder. You can set multiple folders and the result will be the intersection of all the folders.

Warning: There is an option GROUP_BY_FOLDER that purports to have an effect on bookmark queries. This isn't implemented, see {{template.Bug(331487)}}. You should always use this option if you want a bookmarks hierarchy. Lack of this option will be changed to return a flattened list of all bookmark items in all folders returned by the query.

For sorting, you will generally want to use SORT_BY_NONE (the default) since this will return items in their "natural" order as specified by the user in the bookmarks manager. Other sortings will work, however.

For bookmark queries you will generally want no query parameters to retrieve all items from the requested folder(s). When you specify exactly one folder, GROUP_BY_FOLDER, and no query parameters, the system will be more efficient querying and keeping the results up-to-date since this maps to exactly one bookmark folder.

var bookmarkService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
                                .getService(Components.interfaces.nsINavBookmarksService);
// |query| and |options| are objects created in the previous section
query.setFolders([bookmarkService.toolbarRoot], 1);
options.setGroupingMode([options.GROUP_BY_FOLDER], 1);
var result = historyService.executeQuery(query, options);

Serializing queries

Query and options objects can be serialized into a string starting with "place:" using queriesToQueryString. The resulting string can be stored or bookmarked. When a "place:" URI is bookmarked, it will expand to the results of the query when it is opened by the user. The original objects can be deserialized from the string using queryStringToQueries.

Be careful, queryStringToQueries may not return any query objects if the string was empty. Your code should handle this case. There will always be an options structure returned. If no options were specified, it will have the default values. If there were no query parameters specified but the input string was not empty (there were options) you may get one query object returned, containing the default query values.

Example of serializing and deserializing two queries and an options object:

var queryString = historyService.queriesToQueryString([query1, query2], 2, options);

var queriesRef = { };
var queryCountRef = { };
var optionsRef = { };
historyService.queryStringToQueries(queryString, queriesRef, queryCountRef, optionsRef);
// now use queriesRef.value, optionsRef.value

A reference for the terms available for "place:" URIs is available here.

Using the results

The most common way to use results is to implement a view. There is a built-in view that will put results in tree controls, and you can also implement your own. See Places:Views for more on this. This section discusses how to access the result directly, for example, if you are creating your own view or are processing the results instead of displaying them.

Note: Be careful when accessing nodes and do not keep references to them around. Notifications sent to the result from the history and bookmarks system, as well as commands executed by the programmer such as sorting may cause the structure to change and nodes may be inserted, removed, or replaced.

The {{template.Interface("nsINavHistoryResult")}} object returned by executeQuery()/executeQueries() contains the list of matches to the given history or bookmarks query. These results are contained in a tree structure made up of nodes. A node's type can be retrieved using its type attribute. This type tells you what interface you can QueryInterface the node to in order to get at more detailed information:

  • nsINavHistoryResultNode: Base class for all nodes. Contains URI, title, and other general info.
  • nsINavHistoryVisitResultNode: Derived from nsINavHistoryResultNode, contains session information.
  • nsINavHistoryFullVisitResultNode: Derived from nsINavHistoryVisitResultNode, contains information about how the user navigated to this page. Note: currently unimplemented, see {{template.Bug(320831)}}.
  • nsINavHistoryContainerResultNode: General container node giving access to its children. Derived from nsINavHistoryResultNode. Bookmark folders are of this type.
  • nsINavHistoryQueryResultNode: A type of container representing a query of the history system. It allows you to get the query options and parameters.

Example of detecting the type of a node

var Ci = Components.interfaces;
switch(node.type) {
  case node.RESULT_TYPE_URI:
    dump("URI result " + node.uri + "\n");
    break;
  case node.RESULT_TYPE_VISIT:
    var visit = node.QueryInterface(Ci.nsINavHistoryVisitResultNode);
    dump("Visit result " + node.uri + " session = " + visit.sessionId + "\n");
    break;
  case node.RESULT_TYPE_FULL_VISIT:
    var fullVisit = node.QueryInterface(Ci.nsINavHistoryFullVisitResultNode);
    dump("Full visit result " + node.uri + " session = " + fullVisit.sessionId + " transitionType = " +
         fullVisit.transitionType + "\n");
    break;
  case node.RESULT_TYPE_HOST:
    var container = node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    dump("Host " + container.title + "\n");
    break;
  case node.RESULT_TYPE_REMOTE_CONTAINER:
    var container = node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    dump("Remote container " + container.title + " type = " + container.remoteContainerType + "\n");
    break;
  case node.RESULT_TYPE_QUERY:
    var query = node.QueryInterface(Ci.nsINavHistoryQueryResultNode);
    dump("Query, place URI = " + query.uri + "\n");
    break;
  case node.RESULT_TYPE_FOLDER:
    // Note that folder nodes are of type nsINavHistoryContainerResultNode by default, but
    // can be QI'd to nsINavHistoryQueryResultNode to access the query and options that
    // created it.
    dump("Folder " + node.title + " id = " + node.itemId + "\n");
    break;
  case node.RESULT_TYPE_SEPARATOR:
    dump("-----------\n");
    break;
}

Containers

Containers hold lists of other containers and result nodes. Each result has a container representing the root of the query. It can be retrieved using the root attribute of the result. For general queries, this root container is a {{template.Interface("nsINavHistoryQueryResultNode")}} with the query parameters and options that you supplied in the original query. For queries mapping to one bookmark folder, this will be a {{template.Interface("nsINavHistoryContainerResultNode")}}.

Containers can be open or closed. This corresponds to the open and closed state in a tree view, and can also be mapped to showing and hiding menus. To get at a container's contents, you must first open the container. Most container types populate themselves lazily, so opening a container actually corresponds to executing the given query. While a container is open, it will listen to the history and bookmarks systems' notifications and modify their contents to keep themselves up-to-date. For this reason, it is best to close a container as soon as you are done with it, since it will give better performance. If you close a container and re-open it before any history or bookmark change notifications come, the results will generally still be there and this operation will be fast.

Example of traversing a container:

var cont = result.root;
cont.containerOpen = true;
for (var i = 0; i < cont.childCount; i ++) {
  var node = cont.getChild(i);
  dump(node.title + "\n");
}
cont.containerOpen = false;

The result view interface

If you are mapping a result into UI, you can implement the {{template.Interface("nsINavHistoryResultViewer")}} interface and attach it to the result with the nsINavHistoryResult.viewer attribute. This viewer will be called when the result tree changes, either as a result of user action or as a result of notifications from the bookmarks and history systems. Your implementation would then reflect these changes in the UI.

A prepackaged view interface for a {{template.Interface("nsITreeBoxObject")}} is provided that manages the complex view requirements of a tree. This object's interface is {{template.Interface("nsINavHistoryResultTreeViewer")}} (a descendent of {{template.Interface("nsINavHistoryResultViewer")}}). See Places:Views for more information and examples.

{{ wiki.languages( { "fr": "fr/Places/Syst\u00e8me_de_requ\u00eates" } ) }}

Revision Source

<p>
{{template.Fx_minversion_header(3)}}
</p><p>Firefox history and bookmarks data is accessed using the "<a href="en/Places">Places</a>" query APIs. These APIs provide the ability to execute complex queries against history, bookmarks or both combined (targeting Firefox Alpha 6). The result of a query is an object which will contain a flat list, or tree structure of the matching data. The definitions of the query API and the resulting data structures is available in {{template.Source("toolkit/components/places/public/nsINavHistoryService.idl")}}. This page provides an introduction to some common operations, and example usage of the core APIs.
</p>
<h3 name="Executing_a_query"> Executing a query </h3>
<p>Places queries have several basic parts:
</p>
<ul><li> The query object: {{template.Interface("nsINavHistoryQuery")}}, holds the search parameters
</li><li> The query options: {{template.Interface("nsINavHistoryQueryOptions")}}, allows configuration of the search result
</li><li> The history service: {{template.Interface("nsINavHistoryService")}}, executes the query
</li></ul>
<p>The first first step is to create the query and options, and fill them with the parameters you want. Use <code>nsINavHistoryService.getNewQuery()</code> and <code>nsINavHistoryService.getNewQueryOptions()</code> to retrieve empty objects. The defaults for these objects will result in a query that returns all of your browser history in a flat list:
</p>
<pre>var historyService = Components.classes["@mozilla.org/browser/nav-history-service;1"]
                               .getService(Components.interfaces.nsINavHistoryService);

// no query parameters will get all history
// XXX default sorting is... ?
var options = historyService.getNewQueryOptions();

// no query parameters will return everything
var query = historyService.getNewQuery();

// execute the query
var result = historyService.executeQuery(query, options);

</pre>
<h4 name="Result_types"> Result types </h4>
<p>{{template.Interface("nsINavHistoryQueryOptions")}} has a <code>resultType</code> property that allows configuration of the grouping and level of detail to be returned in the results. The different values for this property are listed below. These values are also properties of <code>nsINavHistoryQueryOptions</code>, and are accessed like this: <code>Components.interfaces.nsINavHistoryQueryOptions.RESULTS_AS_VISIT</code>.
</p><p><br>
</p>
<ul><li> <b>RESULTS_AS_URI</b>: This is the default, and returns one result node of type <code>RESULT_TYPE_URI</code> for each URI that matches the query. The visit date for each node will be the last visit date for that URL.
</li><li> <b>RESULTS_AS_VISIT</b>: This option returns one entry for each time a page was visited matching the given query. The result may contain duplicate entries for URLs, each with a different date. The nodes are of type <code>RESULT_TYPE_VISIT</code>, and provide a session ID for each visit. This session ID is the same for all pages that were reached by clicking links. A new session starts when the user types a new URL or follows a bookmark (XXX link to more details about what constitutes a session).
</li><li> <b>RESULTS_AS_FULL_VISIT</b>: This is an extended version of <code>RESULT_TYPE_VISIT</code>. The results are of type <code>RESULT_TYPE_FULL_VISIT</code> and have additional information about the visit, such as the referring visit, and how the transition happened (typed, redirect, link, etc).
</li></ul>
<h4 name="Basic_Query_Search_Parameters"> Basic Query Search Parameters </h4>
<p><span class="comment">const unsigned long 	TIME_RELATIVE_EPOCH = 0 const unsigned long 	TIME_RELATIVE_TODAY = 1 const unsigned long 	TIME_RELATIVE_NOW = 2 attribute PRTime 	beginTime attribute unsigned long 	beginTimeReference readonly attribute boolean 	hasBeginTime readonly attribute PRTime 	absoluteBeginTime attribute PRTime 	endTime attribute unsigned long 	endTimeReference readonly attribute boolean 	hasEndTime readonly attribute PRTime 	absoluteEndTime attribute AString 	searchTerms readonly attribute boolean 	hasSearchTerms attribute long 	minVisits attribute long 	maxVisits attribute boolean 	onlyBookmarked attribute boolean 	domainIsHost attribute AUTF8String 	domain readonly attribute boolean 	hasDomain attribute boolean 	uriIsPrefix attribute nsIURI 	uri readonly attribute boolean 	hasUri attribute boolean 	annotationIsNot attribute AUTF8String 	annotation readonly attribute boolean 	hasAnnotation readonly attribute unsigned long 	folderCount</span>
</p>
<h4 name="Basic_Query_Configuration_Options"> Basic Query Configuration Options </h4>
<p><span class="comment">const unsigned short 	GROUP_BY_DAY = 0 const unsigned short 	GROUP_BY_HOST = 1 const unsigned short 	GROUP_BY_DOMAIN = 2 const unsigned short 	GROUP_BY_FOLDER = 3 const unsigned short 	SORT_BY_NONE = 0 const unsigned short 	SORT_BY_TITLE_ASCENDING = 1 const unsigned short 	SORT_BY_TITLE_DESCENDING = 2 const unsigned short 	SORT_BY_DATE_ASCENDING = 3 const unsigned short 	SORT_BY_DATE_DESCENDING = 4 const unsigned short 	SORT_BY_URI_ASCENDING = 5 const unsigned short 	SORT_BY_URI_DESCENDING = 6 const unsigned short 	SORT_BY_VISITCOUNT_ASCENDING = 7 const unsigned short 	SORT_BY_VISITCOUNT_DESCENDING = 8 const unsigned short 	SORT_BY_KEYWORD_ASCENDING = 9 const unsigned short 	SORT_BY_KEYWORD_DESCENDING = 10 const unsigned short 	SORT_BY_DATEADDED_ASCENDING = 11 const unsigned short 	SORT_BY_DATEADDED_DESCENDING = 12 const unsigned short 	SORT_BY_LASTMODIFIED_ASCENDING = 13 const unsigned short 	SORT_BY_LASTMODIFIED_DESCENDING = 14 const unsigned short 	SORT_BY_ANNOTATION_ASCENDING = 15 const unsigned short 	SORT_BY_ANNOTATION_DESCENDING = 16 const unsigned short 	RESULTS_AS_URI = 0 const unsigned short 	RESULTS_AS_VISIT = 1 const unsigned short 	RESULTS_AS_FULL_VISIT = 2 attribute unsigned short 	sortingMode attribute AUTF8String 	sortingAnnotation attribute unsigned short 	resultType attribute boolean 	excludeItems attribute boolean 	excludeQueries attribute boolean 	excludeReadOnlyFolders attribute boolean 	expandQueries attribute boolean 	includeHidden attribute boolean 	showSessions attribute unsigned long 	maxResults const unsigned short 	QUERY_TYPE_HISTORY = 0 const unsigned short 	QUERY_TYPE_BOOKMARKS = 1 const unsigned short 	QUERY_TYPE_UNIFIED = 2 attribute unsigned short 	queryType</span>
</p>
<h4 name="Complex_Queries"> Complex Queries </h4>
<p>You can pass one or more {{template.Interface("nsINavHistoryQuery")}} objects to <code>executeQueries()</code>. Within one query object, all parameters are <i>AND</i>ed together. The conditions for different query objects are then <i>OR</i>ed together. This allows for a simpler implementation and interface than a full logical operation with nested clauses while still being expressive.
</p><p>Example of querying for any pages I've visited that contain the word "firefox" in the title/URL or that I've visited today from mozilla.org.
</p>
<pre>// first query object searches for "firefox" in title/URL
var query1 = historyService.getNewQuery();
query1.searchTerms = "firefox";

// second query object searches for visited in past 24 hours AND from mozilla.org
var query2 = historyService.getNewQuery();
query2.beginTimeReference = query2.TIME_RELATIVE_NOW;
query2.beginTime = -24 * 60 * 60 * 1000000; // 24 hours ago in microseconds
query2.endTimeReference = query2.TIME_RELATIVE_NOW;
query2.endTime = 0; // now
query2.domain = "mozilla.org";

var result = historyService.executeQueries([query1, query2], 2, options);
</pre>
<div class="note"><b>Note</b>: Keyword searching doesn't work correctly across <i>OR</i> queries. The current behavior does the normal query and then selects keywords from the first query and filters all the results. (In other words, the keywords from the first query are <i>AND</i>ed with all queries.) Keywords from subsequent query objects are ignored. This is {{template.Bug(320332)}}.</div>
<h4 name="Bookmark_queries"> Bookmark queries </h4>
<p>There is a quick-start for doing simple bookmark queries in <a href="en/Places/Accessing_Bookmarks">Accessing Bookmarks</a>.
</p><p>The contents of bookmark folders can be retrieved by setting the "folders" member in the query object. This item is an array of folder IDs from the bookmark service. Typically, you will only have one folder ID in this list, which will given you the contents of that folder. You can set multiple folders and the result will be the intersection of all the folders.
</p>
<div class="warning"><b>Warning</b>: There is an option GROUP_BY_FOLDER that purports to have an effect on bookmark queries. This isn't implemented, see {{template.Bug(331487)}}. You should always use this option if you want a bookmarks hierarchy. Lack of this option will be changed to return a flattened list of all bookmark items in all folders returned by the query.</div>
<p>For sorting, you will generally want to use <code>SORT_BY_NONE</code> (the default) since this will return items in their "natural" order as specified by the user in the bookmarks manager. Other sortings will work, however.
</p><p>For bookmark queries you will generally want no query parameters to retrieve all items from the requested folder(s). When you specify exactly one folder, <code>GROUP_BY_FOLDER</code>, and no query parameters, the system will be more efficient querying and keeping the results up-to-date since this maps to exactly one bookmark folder.
</p>
<pre>var bookmarkService = Components.classes["@mozilla.org/browser/nav-bookmarks-service;1"]
                                .getService(Components.interfaces.nsINavBookmarksService);
// |query| and |options| are objects created in the previous section
query.setFolders([bookmarkService.toolbarRoot], 1);
options.setGroupingMode([options.GROUP_BY_FOLDER], 1);
var result = historyService.executeQuery(query, options);
</pre>
<h3 name="Serializing_queries"> Serializing queries </h3>
<p>Query and options objects can be serialized into a string starting with "place:" using <code>queriesToQueryString</code>. The resulting string can be stored or bookmarked. When a "place:" URI is bookmarked, it will expand to the results of the query when it is opened by the user. The original objects can be deserialized from the string using <code>queryStringToQueries</code>.
</p><p>Be careful, <code>queryStringToQueries</code> may not return any query objects if the string was empty. Your code should handle this case. There will always be an options structure returned. If no options were specified, it will have the default values. If there were no query parameters specified but the input string was not empty (there were options) you may get one query object returned, containing the default query values.
</p><p>Example of serializing and deserializing two queries and an options object:
</p>
<pre>var queryString = historyService.queriesToQueryString([query1, query2], 2, options);

var queriesRef = { };
var queryCountRef = { };
var optionsRef = { };
historyService.queryStringToQueries(queryString, queriesRef, queryCountRef, optionsRef);
// now use queriesRef.value, optionsRef.value
</pre>
<p>A reference for the terms available for "place:" URIs is available <a href="en/Places/PlaceURIs">here</a>.
</p>
<h3 name="Using_the_results"> Using the results </h3>
<p>The most common way to use results is to implement a view. There is a built-in view that will put results in tree controls, and you can also implement your own. See <a href="en/Places/Views">Places:Views</a> for more on this. This section discusses how to access the result directly, for example, if you are creating your own view or are processing the results instead of displaying them.
</p><p><i>Note: Be careful when accessing nodes and do not keep references to them around. Notifications sent to the result from the history and bookmarks system, as well as commands executed by the programmer such as sorting may cause the structure to change and nodes may be inserted, removed, or replaced.</i>
</p><p>The {{template.Interface("nsINavHistoryResult")}} object returned by <code>executeQuery()</code>/<code>executeQueries()</code> contains the list of matches to the given history or bookmarks query. These results are contained in a tree structure made up of nodes. A node's type can be retrieved using its <code>type</code> attribute. This type tells you what interface you can <code>QueryInterface</code> the node to in order to get at more detailed information:
</p>
<ul><li> <b>nsINavHistoryResultNode</b>: Base class for all nodes. Contains URI, title, and other general info.
</li><li> <b>nsINavHistoryVisitResultNode</b>: Derived from <code>nsINavHistoryResultNode</code>, contains session information.
</li><li> <b>nsINavHistoryFullVisitResultNode</b>: Derived from <code>nsINavHistoryVisitResultNode</code>, contains information about how the user navigated to this page. <i>Note: currently unimplemented, see {{template.Bug(320831)}}.</i>
</li><li> <b>nsINavHistoryContainerResultNode</b>: General container node giving access to its children. Derived from <code>nsINavHistoryResultNode</code>. Bookmark folders are of this type.
</li><li> <b>nsINavHistoryQueryResultNode</b>: A type of container representing a query of the history system. It allows you to get the query options and parameters.
</li></ul>
<p>Example of detecting the type of a node
</p>
<pre>var Ci = Components.interfaces;
switch(node.type) {
  case node.RESULT_TYPE_URI:
    dump("URI result " + node.uri + "\n");
    break;
  case node.RESULT_TYPE_VISIT:
    var visit = node.QueryInterface(Ci.nsINavHistoryVisitResultNode);
    dump("Visit result " + node.uri + " session = " + visit.sessionId + "\n");
    break;
  case node.RESULT_TYPE_FULL_VISIT:
    var fullVisit = node.QueryInterface(Ci.nsINavHistoryFullVisitResultNode);
    dump("Full visit result " + node.uri + " session = " + fullVisit.sessionId + " transitionType = " +
         fullVisit.transitionType + "\n");
    break;
  case node.RESULT_TYPE_HOST:
    var container = node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    dump("Host " + container.title + "\n");
    break;
  case node.RESULT_TYPE_REMOTE_CONTAINER:
    var container = node.QueryInterface(Ci.nsINavHistoryContainerResultNode);
    dump("Remote container " + container.title + " type = " + container.remoteContainerType + "\n");
    break;
  case node.RESULT_TYPE_QUERY:
    var query = node.QueryInterface(Ci.nsINavHistoryQueryResultNode);
    dump("Query, place URI = " + query.uri + "\n");
    break;
  case node.RESULT_TYPE_FOLDER:
    // Note that folder nodes are of type nsINavHistoryContainerResultNode by default, but
    // can be QI'd to nsINavHistoryQueryResultNode to access the query and options that
    // created it.
    dump("Folder " + node.title + " id = " + node.itemId + "\n");
    break;
  case node.RESULT_TYPE_SEPARATOR:
    dump("-----------\n");
    break;
}
</pre>
<h4 name="Containers"> Containers </h4>
<p>Containers hold lists of other containers and result nodes. Each result has a container representing the root of the query. It can be retrieved using the <code>root</code> attribute of the result. For general queries, this root container is a {{template.Interface("nsINavHistoryQueryResultNode")}} with the query parameters and options that you supplied in the original query. For queries mapping to one bookmark folder, this will be a {{template.Interface("nsINavHistoryContainerResultNode")}}.
</p><p>Containers can be open or closed. This corresponds to the open and closed state in a tree view, and can also be mapped to showing and hiding menus. To get at a container's contents, you must first open the container. Most container types populate themselves lazily, so opening a container actually corresponds to executing the given query. While a container is open, it will listen to the history and bookmarks systems' notifications and modify their contents to keep themselves up-to-date. For this reason, it is best to close a container as soon as you are done with it, since it will give better performance. If you close a container and re-open it before any history or bookmark change notifications come, the results will generally still be there and this operation will be fast.
</p><p>Example of traversing a container:
</p>
<pre>var cont = result.root;
cont.containerOpen = true;
for (var i = 0; i &lt; cont.childCount; i ++) {
  var node = cont.getChild(i);
  dump(node.title + "\n");
}
cont.containerOpen = false;
</pre>
<h4 name="The_result_view_interface"> The result view interface </h4>
<p>If you are mapping a result into UI, you can implement the {{template.Interface("nsINavHistoryResultViewer")}} interface and attach it to the result with the <code>nsINavHistoryResult.viewer</code> attribute. This viewer will be called when the result tree changes, either as a result of user action or as a result of notifications from the bookmarks and history systems. Your implementation would then reflect these changes in the UI.
</p><p>A prepackaged view interface for a {{template.Interface("nsITreeBoxObject")}} is provided that manages the complex view requirements of a tree. This object's interface is {{template.Interface("nsINavHistoryResultTreeViewer")}} (a descendent of {{template.Interface("nsINavHistoryResultViewer")}}). See <a href="en/Places/Views">Places:Views</a> for more information and examples.
</p>
<div class="noinclude">
</div>
{{ wiki.languages( { "fr": "fr/Places/Syst\u00e8me_de_requ\u00eates" } ) }}
Revert to this revision