// BEGIN FLOCK GPL
//
// Copyright Flock Inc. 2005-2009
// http://flock.com
//
// This file may be used under the terms of the
// GNU General Public License Version 2 or later (the "GPL"),
// http://www.gnu.org/licenses/gpl.html
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
//
// END FLOCK GPL

const CC = Components.classes;
const CI = Components.interfaces;
const CU = Components.utils;
const CR = Components.results;

CU.import("resource:///modules/FlockXPCOMUtils.jsm");

const FM_CONTRACTID = "@flock.com/media-webpageapi;1";
const FM_CLASSID    = Components.ID("{fbfcc2fa-e23a-428e-98aa-d5ba69d928a7}");
const FM_CLASSNAME  = "Flock Media Webpage API";

const MODULE_NAME = "Media Webpage API";

/*************************************************************************
 * Component: flockMediaWebpageAPI
 *************************************************************************/

function flockMediaWebpageAPI() {
}

/*************************************************************************
 * flockMediaWebpageAPI: XPCOM Component Creation
 *************************************************************************/

flockMediaWebpageAPI.prototype = new FlockXPCOMUtils.genericComponent(
  FM_CLASSNAME,
  FM_CLASSID,
  FM_CONTRACTID,
  flockMediaWebpageAPI,
  CI.nsIClassInfo.SINGLETON,
  [
    CI.flockIMediaWebpageAPI,
    CI.nsISecurityCheckedComponent
  ]
);

/*************************************************************************
 * flockMediaWebpageAPI: private members
 *************************************************************************/

flockMediaWebpageAPI.prototype._openMediaQuery =
function flockMediaWebpageAPI__openMediaQuery(aService,
                                              aQuery,
                                              aTitle)
{
  var wm = CC["@mozilla.org/appshell/window-mediator;1"]
           .getService(CI.nsIWindowMediator);
  var win = wm.getMostRecentWindow("navigator:browser");

  win.mediabar.loadQuery(aService, aQuery, aTitle);
}

/*************************************************************************
 * flockMediaWebpageAPI: flockIMediaWebpageAPI implementation
 *************************************************************************/

flockMediaWebpageAPI.prototype.open =
function flockMediaWebpageAPI_open() {
  var wm = CC["@mozilla.org/appshell/window-mediator;1"]
           .getService(CI.nsIWindowMediator);
  var win = wm.getMostRecentWindow("navigator:browser");

  win.mediabar.ensureOpen();
}

flockMediaWebpageAPI.prototype.openFeed =
function flockMediaWebpageAPI_openFeed(aTitle, aURL) {
  var query = "special:" + encodeURIComponent(aURL);
  this._openMediaQuery("mediarss", query, aTitle);
}

flockMediaWebpageAPI.prototype.openPerson =
function flockMediaWebpageAPI_openPerson(aService, aAccountId, aTitle) {
  var query = "user:" + aAccountId;

  this._openMediaQuery(aService, query, aTitle);
}

/*************************************************************************
 * flockMediaWebpageAPI: nsISecurityChecked implementation
 *************************************************************************/

flockMediaWebpageAPI.prototype.canCreateWrapper =
function canCreateWrapper(aIID) {
  return "AllAccess";
}

flockMediaWebpageAPI.prototype.canCallMethod =
function canCallMethod(aIID, methodName) {
  switch (methodName) {
    case "open":
    case "openFeed":
    case "openPerson":
      return "AllAccess";
  }

  return "NoAccess";
}

flockMediaWebpageAPI.prototype.canGetProperty =
function canGetProperty(aIID, propertyName) {
  return "NoAccess";
}

flockMediaWebpageAPI.prototype.canSetProperty =
function canSetProperty(aIID, propertyName) {
  return "NoAccess";
}

/*************************************************************************
 * XPCOM Support - Module Construction
 *************************************************************************/

// Create array of components.
var componentsArray = [flockMediaWebpageAPI];

// Generate a module for XPCOM to find.
var NSGetModule = FlockXPCOMUtils.generateNSGetModule(MODULE_NAME,
                                                      componentsArray);
