/******************************************************************************
 *            Copyright (c) 2006-2011 Michel Gutierrez. All Rights Reserved.
 ******************************************************************************/

EXPORTED_SYMBOLS=["DWHelperSecretHelperProcService"];

Components.utils['import']("resource://dwhelper/util-service.jsm");

var DWHelperSecretHelperProcService = {
	mProcessor: null,
	start: function() { this.get(); },
	get: function() {
		try {
			if(this.mProcessor==null) {
				this.mProcessor=new SehProc();
			}
		} catch(e) {
			dump("!!! [SecretHelperProcService] "+e+"\n");
		}
		return this.mProcessor;
	}
}

/**
* Object constructor
*/
function SehProc() {
	try {
		//dump("[SehProc] constructor\n");
		var prefService=Components.classes["@mozilla.org/preferences-service;1"]
		                                   .getService(Components.interfaces.nsIPrefService);
		this.pref=prefService.getBranch("dwhelper.");
		var seh=this.pref.getBoolPref("seh-intro-proc.enable");
		if(seh) {	
			try { // for windows only
				var reg=Components.classes["@mozilla.org/windows-registry-key;1"]
					.createInstance(Components.interfaces.nsIWindowsRegKey);
				this.core=Components.classes["@downloadhelper.net/core;1"].
					getService(Components.interfaces.dhICore);
				this.core.registerProcessor(this);
			} catch(e) {}
		}
	} catch(e) {
		dump("[SehProc] !!! constructor: "+e+"\n");
	}
}

SehProc.prototype = {
		get name() { return "sec-download"; },
		get provider() { return "SecretHelperHelper"; },
		get title() { return Util.getText("processor.sec-download.title"); },
		get description() { return Util.getText("processor.sec-download.description"); },
		get enabled() { return true; },
		get weight() { return 90; },
}

SehProc.prototype.canHandle=function(desc) {
	//dump("[SehProc] canHandle()\n");
	if(desc.has("media-url"))
		return true;
	else
		return false;
}

SehProc.prototype.requireDownload=function(desc) {
	//dump("[SehProc] requireDownload()\n");
	return false;
}

SehProc.prototype.preDownload=function(desc) {
	//dump("[SehProc] preDownload()\n");
	return true;
}

SehProc.prototype.handle=function(desc) {
	//dump("[SehProc] handle()\n");
    var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                                .getService(Components.interfaces.nsIWindowMediator);
	var w = wm.getMostRecentWindow("navigator:browser");
	w.open('http://www.downloadhelper.net/secrethelper.php');
}

SehProc.prototype.QueryInterface = function(iid) {
    if (iid.equals(Components.interfaces.nsISupports) || 
       	iid.equals(Components.interfaces.dhIProcessor)
    	) {
    		return this;
        }
    throw Components.results.NS_ERROR_NO_INTERFACE;
}
