/****************************************************************************
* SKCD  > skClipboard                                                       *
*---------------------------------------------------------------------------*
* skSelection's goal is to copy easily some text from a document and to     *
* retrieve the clipboard content.                                           *
*---------------------------------------------------------------------------*
* (c) IDM. 2003                                                             *
* Project  : sk                                                             *
* Language : javascript                                                     *
* Created  : MikaelLebrun, 18/02/2003                                       *
****************************************************************************/

//skSelection Object
function skClipboard() {
    this.skiSelection = Components.classes["@idm.fr/sk_selection;1"].createInstance().QueryInterface(Components.interfaces.skISelection);
}

skClipboard.prototype = {
    QueryInterface:function(iid) {
        if (!iid.equals(Components.interfaces.skIClipboard)
            &&!iid.equals(Components.interfaces.nsISupports)) {
            throw Components.results.NS_ERROR_NO_INTERFACE;
        }
        return this;
    },

    /*********************************************************************************************
    FUNCTION : Copy()
    This function put the selected text into the clipboard under two formats, unicode and html given 
    by skSelection.
    ****INPUT:  1/ a window object [pxiWindow - the window in which there's a selection]
                2/ a string [psSavePath - the path of the directory in which downloaded files will be saved]
    ****OUPUT: NOTHING
    ********************************************************************************************/
    Copy:function(pxiWindow, psSavePath) {
        try {
            var xiTrans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
            if (!xiTrans) return;
            //var sHtml = this.skiSelection.GetHtml(pxiWindow, psSavePath, true);
            var sUnicode = this.skiSelection.GetUnicode(pxiWindow);
            if (sUnicode) {
                var xiUnicodeString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                if (xiUnicodeString) {
                    xiUnicodeString.data = sUnicode;
                    xiTrans.addDataFlavor("text/unicode");
                    xiTrans.setTransferData("text/unicode",xiUnicodeString,String(sUnicode).length * 2); // double byte data
                }
            } 
            /*
            if (sHtml) {
                var xiHtmlString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                if (xiHtmlString) {
                    xiHtmlString.data = sHtml;
                    xiTrans.addDataFlavor("text/html");
                    xiTrans.setTransferData("text/html",xiHtmlString,String(sHtml).length * 2); // double byte data
                }
            } 
            */
            var xiClipboard = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
            if (!xiClipboard || (!sUnicode && !sHtml)) return;
            xiClipboard.emptyClipboard(xiClipboard.kGlobalClipboard);
            xiClipboard.setData(xiTrans,null,xiClipboard.kGlobalClipboard);
        } catch (err) {
            dump('Copy : '+ err);
//            this.prompt(this.win, 'Copy', err);
        }
    },
    /*********************************************************************************************
    FUNCTION : CopyNode()
    This function put the node(in input) content into the clipboard under html given 
    by skSelection.
    ****INPUT:  1/ a node object
                2/ a string [psSavePath - the path of the directory in which downloaded files will be saved]
    ****OUPUT: NOTHING
    ********************************************************************************************/
    CopyNode:function(pxiNode, pxiWindow, psSavePath) {

            var xiTrans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
            if (!xiTrans) return;
            //var sHtml = this.skiSelection.GetHtmlNode(pxiNode, psSavePath, true);
            var sUnicode = this.skiSelection.GetUnicodeNode(pxiNode,pxiWindow);
            if (sUnicode) {
                var xiUnicodeString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                if (xiUnicodeString) {
                    xiUnicodeString.data = sUnicode;
                    xiTrans.addDataFlavor("text/unicode");
                    xiTrans.setTransferData("text/unicode",xiUnicodeString,String(sUnicode).length * 2); // double byte data
                }
            } 
            /*
            if (sHtml) {
                var xiHtmlString = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
                if (xiHtmlString) {
                    xiHtmlString.data = sHtml;
                    xiTrans.addDataFlavor("text/html");
                    xiTrans.setTransferData("text/html",xiHtmlString,String(sHtml).length * 2); // double byte data
                }
            }
            */
            var xiClipboard = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
            if (!xiClipboard || (!sUnicode && !sHtml)) return;
            xiClipboard.emptyClipboard(xiClipboard.kGlobalClipboard);
            xiClipboard.setData(xiTrans,null,xiClipboard.kGlobalClipboard);
    },
    /*********************************************************************************************
    FUNCTION : GiveClipboardContent()
    This function return the clipboard content for the desired flavor (a type of content like 
    "text/unicode" or "text/html")
    ****INPUT: a string [psFlavor - the flavor we want to retrieve from the clipboard]
    ****OUPUT: a string [the clipboard content]
    ********************************************************************************************/
    GiveClipboardContent:function(psFlavor) {
        try {
            var xiClipboard = Components.classes["@mozilla.org/widget/clipboard;1"].createInstance(Components.interfaces.nsIClipboard);
            var xiTrans = Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
            if (!xiClipboard || !xiTrans) return "";
            if (this.CanPaste(xiClipboard, psFlavor)) {
                xiTrans.addDataFlavor(psFlavor);
                xiClipboard.getData(xiTrans, xiClipboard.kGlobalClipboard);
                var sContent = new Object ();
                var iContentLength = new Object ();
                xiTrans.getTransferData(psFlavor, sContent, iContentLength);
                if (sContent) 
                    sContent = sContent.value.QueryInterface(Components.interfaces.nsISupportsString);
                if (sContent) 
                    return sContent.data.substring(0,iContentLength.value);
                else
                    return "";
            } else {
                return "";
            }
        } catch (err) { 
            dump('GiveClipboardContent : '+ err);
//            this.prompt(this.win, 'GiveClipboardContent', err);
        }
    },

    /*********************************************************************************************
    FUNCTION : CanPaste()
    This function test if the clipboard contains the given flavor (a type of content like 
    "text/unicode" or "text/html")
    ****INPUT: 1/ a clipboard object [pxiClipboard - an XPCOM nsIClipboard interface]
               2/ a string [psFlavor - the flavor to verify]
    ****OUPUT: a boolean [true if the flavor is contained in the clipboard, false otherwise]
    ********************************************************************************************/
    CanPaste:function(pxiClipboard, psFlavor) {
        var xiFlavorArray = Components.classes["@mozilla.org/supports-array;1"].createInstance(Components.interfaces.nsISupportsArray);
        var xiFlavorString = Components.classes["@mozilla.org/supports-cstring;1"].createInstance(Components.interfaces.nsISupportsCString);
        xiFlavorString.data = psFlavor;
        xiFlavorArray.AppendElement(xiFlavorString);
        return pxiClipboard.hasDataMatchingFlavors(xiFlavorArray, pxiClipboard.kGlobalClipboard);
    }

    /*   
    prompt:function(pxiWindow, psTitle, psText) {
        var xiPromptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].createInstance().QueryInterface(Components.interfaces.nsIPromptService);
        xiPromptService.alert(pxiWindow, psTitle, psText);
    }
    */
}

var skClipboardModule = {
    registerSelf: function(compMgr, fileSpec, location, type) {
        compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
        compMgr.registerFactoryLocation(this.myCID,
                                        "SK Clipboard Component",
                                        this.myProgID, 
                                        fileSpec,
                                        location,
                                        type);
    },

    getClassObject: function(compMgr, cid, iid) {
        if (!cid.equals(this.myCID))
            throw Components.results.NS_ERROR_NO_INTERFACE;
        if (!iid.equals(Components.interfaces.nsIFactory))
            throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
        return this.myFactory;
    },

    myCID: Components.ID("80f2f390-d8c9-4ca3-982d-ec9c24eab406"),

    myProgID: "@idm.fr/sk_clipboard;1",

    myFactory: {
        createInstance: function(outer, iid) {
            if (outer != null)
                throw Components.results.NS_ERROR_NO_AGGREGATION;
            return (new skClipboard()).QueryInterface(iid);
        }
    },

    canUnload: function(compMgr) {
        return true;
    }
};

function NSGetModule(compMgr, fileSpec) { return skClipboardModule; }
