/* * * Top of Copyright * * *
(c) 2009 John F. Kelley (except as noted).  All rights reserved.

The following terms are based on the BSD License:
http://www.opensource.org/licenses/bsd-license.php

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the
distribution.

* Neither the name of copyright holder nor the names of the copyright holder's employer may be used
to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* * * End of Copyright * * */

function checkClipboard() {
  var old = getClipboard(true)
  if (-1 == old) return false;

  var testst = "_testclipboard_"
  setClipboard(testst,true)
  var istest = getClipboard(true)
  if ("undefined" != typeof old) setClipboard(old,true)
  if (istest == testst) return true;
  else return false;
  }


function getClipboard(silent) {
  if (window.clipboardData) return(window.clipboardData.getData('Text'));

  else if (window.netscape) {
    try {
      netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
      var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
      }
    catch (error) { return errClipboard(silent) }
    if (!clip) return "";

    try {
      var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
      }
    catch (error) { return errClipboard(silent) }
    if (!trans) return "";

    trans.addDataFlavor('text/unicode');
    clip.getData(trans,clip.kGlobalClipboard);
    var str = new Object();
    var len = new Object();

    try { trans.getTransferData('text/unicode',str,len); }
    catch(error) { return errClipboard(silent); }

    if (str) {
      if (Components.interfaces.nsISupportsWString) str=str.value.QueryInterface(Components.interfaces.nsISupportsWString);
      else if (Components.interfaces.nsISupportsString) str=str.value.QueryInterface(Components.interfaces.nsISupportsString);
      else str = null;
      }
    if (str) return(str.data.substring(0,len.value / 2));
    else return ""
    }
  else return errClipboard(silent);
  }

function setClipboard(meintext,silent) {
  if (window.clipboardData) window.clipboardData.setData('Text',meintext);

  else if (window.netscape) {
   try {
     netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
     var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
     }
   catch (error) { return errClipboard(silent) }
   if (!clip) return false;

   try {
     var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
     }
   catch (error) { return errClipboard(silent) }
   if (!trans) return false;

   trans.addDataFlavor('text/unicode');
   var str = new Object();
   var len = new Object();
   var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
   var copytext=meintext;
   str.data=copytext;
   trans.setTransferData("text/unicode",str,copytext.length*2);
   var clipid=Components.interfaces.nsIClipboard;
   if (!clip) return false;
   clip.setData(trans,null,clipid.kGlobalClipboard);
   return true;
   }
  else return errClipboard(silent);
  }

function errClipboard(silent) {
  if (!silent) {
    var stx = 'You have attempted to use the clipboard.  Your browser is not configured to support that.\n\n'
    stx += 'If you are using FireFox:\n  1. Point your browser to url: about:config\n  '
    stx += '2. Type into Filter box: signed\n  3. Double click "signed.applets.codebase_principal_support"\n  '
    stx += '4. This will set it to "true"\n  5. Try your operation again.'
    alert(stx)
    return -1
    }
  }
