<?xml version="1.0" encoding="UTF-8"?>
<Module>
  <ModulePrefs title="Open Social 0.8 API"
               description="Part of the implementation of the OpenSocial 0.8 API"
               author="Friend Connect"
               author_email="friendconnect-feedback+gadgets@google.com"
               thumbnail="https://www.google.com/friendconnect/scs/images/thumb/osapi-0.8.xml"
               screenshot="https://www.google.com/friendconnect/scs/images/thumb/osapi-0.8.xml"
               >
    <!-- This gadget has no ui and should not be touched by end users, therefore should not be localized. -->
    <Require feature="opensocial-0.8" />
    <Require feature="views" />
    <Require feature="skins" />
    <Optional feature="content-rewrite">
      <Param name="exclude-urls">.*</Param>
    </Optional>
  </ModulePrefs>
  <Content type="html"><![CDATA[
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <style type="text/css">
      body { background: transparent; }
    </style>
    <!-- needed to call the finishSignIn api -->
    <script type="text/javascript" src="https://www.google.com/friendconnect/script/gadget_util.js?d=0.1-7063da8e_1245d196_0d90e69e_229f7965_a8d53ba6.7">
    </script>
    <script type="text/javascript">
    // register the RPC OpenSocial proxy
    gadgets.rpc.register('FetchOpenSocialData', processOsApi);
    gadgets.rpc.register('RequestFriendConnectExtension',
      function (command) {
       throw 'unknown command:' + command;
    });

    var txnId = gadgets.views.getParams()['txnId'];
    if (txnId) {
      // get the API to invoke
      gadgets.rpc.call(null, 'FetchQueuedCommand', function(data) {
        if (data) {
          processOsApi(data);
        } else {
          throw "Unable to fetch command with txn: " + txnId;
        }
      }, txnId);
    } else {
      // register that this api proxy gadget has been loaded
      gadgets.rpc.call(null, 'api_loaded', null, shindig.auth.getSecurityToken());
    }

    function processOsApi(rpcPostData) {
      var st = encodeURIComponent(shindig.auth.getSecurityToken());
      var url = document.location.protocol + "//" +
          document.location.host + '/ps/api/rpc?st=' + st;

      // the chunk size
      var pageSize = (goog.peoplesense.util.userAgent.IE ? 500 : 2000);

      var callback = function (rpcResponseData) {
        // turn the object into a string for chuncking
        var jsonString = gadgets.json.stringify(rpcResponseData);
        var index = 0;
        while ((index*pageSize) < jsonString.length) {
          var chunk =
            jsonString.substring(index * pageSize,(index + 1) * pageSize);
          // send each chunck
          gadgets.rpc.call(null, 'FetchOpenSocialDataResponse', null,
            {'txn':rpcPostData['txn'], 'index': index, 'chunk':chunk });
          index++;
        }

        //  send the terminating chunk with totals
        gadgets.rpc.call(null, 'FetchOpenSocialDataResponse', null,
          {'txn':rpcPostData['txn'],
           'index': index, 'total': index, 'done':true,
           'chunk':''});
      };

      gadgets.io.makeNonProxiedRequest(url, callback,
          { 'CONTENT_TYPE': 'JSON',
            'METHOD': 'POST',
            'POST_DATA': rpcPostData['POST_DATA'] },
          'application/json');
    }
    </script>
    </head>
    <body dir='__BIDI_DIR__'>
    </body>
    </html>
  ]]></Content>
</Module>
