// Copyright 2007, Google Inc. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are met: // // 1. Redistributions of source code must retain the above copyright notice, // this list of conditions and the following disclaimer. // 2. 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. // 3. Neither the name of Google Inc. nor the names of its contributors may be // used to endorse or promote products derived from this software without // specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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. // // Sets up google.gears.*, which is *the only* supported way to access Gears. // // Circumvent this file at your own risk! // // In the future, Gears may automatically define google.gears.* without this // file. Gears may use these objects to transparently fix bugs and compatibility // issues. Applications that use the code below will continue to work seamlessly // when that happens. (function() { // We are already defined. Hooray! if (window.google && google.gears) { return; } var factory = null; // Firefox if (typeof GearsFactory != 'undefined') { factory = new GearsFactory(); } else { // IE try { factory = new ActiveXObject('Gears.Factory'); // privateSetGlobalObject is only required and supported on IE Mobile on // WinCE. if (factory.getBuildInfo().indexOf('ie_mobile') != -1) { factory.privateSetGlobalObject(this); } } catch (e) { // Safari if ((typeof navigator.mimeTypes != 'undefined') && navigator.mimeTypes["application/x-googlegears"]) { factory = document.createElement("object"); factory.style.display = "none"; factory.width = 0; factory.height = 0; factory.type = "application/x-googlegears"; document.documentElement.appendChild(factory); } } } // *Do not* define any objects if Gears is not installed. This mimics the // behavior of Gears defining the objects in the future. if (!factory) { return; } // Now set up the objects, being careful not to overwrite anything. // // Note: In Internet Explorer for Windows Mobile, you can't add properties to // the window object. However, global objects are automatically added as // properties of the window object in all browsers. if (!window.google) { google = {}; } if (!google.gears) { google.gears = {factory: factory}; } })(); var GGCOOKIE_NAME = "wiki_db_GGears"; // Change this to set the name of the managed resource store to create. // You use the name with the createManagedStore, and removeManagedStore, // and openManagedStore APIs. It isn't visible to the user. var STORE_NAME = "wikixpert"; var MANIFEST_FILENAME = "http://wiki.starxpert.fr/wiki/Accueil/index.php?action=ggears_manifest"; var HELPME_FILENAME = "http://wiki.starxpert.fr/wiki/Accueil/index.php?action=ggears_helpme"; // In case we are offline with a disabled store, that's the relative part // of the URL to type to activate the local store var HELPME_URL = "offline"; var localServer; var store; var store2; var blink=false; function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { createCookie(name,"",-1); } function allowedCookie() { createCookie(GGCOOKIE_NAME+'test', 'test'); if (readCookie(GGCOOKIE_NAME+'test')) { eraseCookie(GGCOOKIE_NAME+'test'); return true; } return false; } // Called onload to initialize local server and store variables function initStore() { if (!window.google || !google.gears) { // textOut("Google Gears not found"); return; } //Lets see if the user already authorized GGears if (readCookie(GGCOOKIE_NAME)) { localServer = google.gears.factory.create("beta.localserver"); store = localServer.openManagedStore(STORE_NAME); // if (store) {Here is the place to touch the manifestUrl, if required} menuStore(); // add the version if it's the offline page var elm = document.getElementById("t-offStore"); if (elm.firstChild) { elm.appendChild(document.createTextNode('v: '+store.currentVersion)); } } else { menuStore(); } } // Functions to create an offline helper in case // we are stuck offline with a disabled store // As it's optional we won't throw any error function createHelpmeStoreCallback(url, success, id) { store2.rename(HELPME_FILENAME, HELPME_URL); } function createHelpmeStore() { if (!window.google || !google.gears || !localServer) { return; } store2 = localServer.createStore(STORE_NAME); if (!store2) { return; } store2.capture(HELPME_FILENAME, createHelpmeStoreCallback); } function removeHelpmeStore() { if (!window.google || !google.gears || !localServer) { return; } localServer.removeStore(STORE_NAME); } // Create (or open&update) the managed resource store function createStore() { if (!window.google || !google.gears) { alert("You must install Google Gears first."); return; } if (!allowedCookie()) { alert("You must allow cookies!\nAborting..."); return; } else { createCookie(GGCOOKIE_NAME, 1, 365*10); } if (!localServer) { localServer = google.gears.factory.create("beta.localserver"); } store = localServer.createManagedStore(STORE_NAME); if (!store) { textOut("Error creating store"); } else { store.manifestUrl = MANIFEST_FILENAME; hideItem("t-createStore"); showItem("t-removeStore"); // To not switch to offline mode by default: //store.enabled = false; updateStore(); } } // Update the managed resource store function updateStore() { if (!window.google || !google.gears) { alert("You must install Google Gears first."); return; } textOut("Downloading..."); // Let's also create/refresh our helper page createHelpmeStore(); store.checkForUpdate(); var timerId = window.setInterval(function() { // if store was removed during sync: if (!store) { window.clearInterval(timerId); } else if (store.updateStatus == 0) { window.clearInterval(timerId); textOut("Sync done!\n" + "Version: " + store.currentVersion); menuStore(); setTimeout('textOut("")', 2000); } else if (store.updateStatus == 3) { window.clearInterval(timerId); textOut("Error: " + store.lastErrorMessage); } else { textBlink(); } }, 500); } // Remove the managed resource store. function removeStore() { if (!window.google || !google.gears || !store) { return; } var enabled = store.enabled; localServer.removeManagedStore(STORE_NAME); eraseCookie(GGCOOKIE_NAME); removeHelpmeStore(); // Were we working offline? if (enabled) { hideItem("t-createStore"); hideItem("t-updateStore"); hideItem("t-removeStore"); hideItem("t-enableStore"); hideItem("t-disableStore"); textOut("Store removed\n" + "Going online..."); setTimeout("location.reload(true)", 2000); } else { store = null; menuStore(); textOut("Store removed"); setTimeout('textOut("");', 2000); } } function enableStore() { if (!window.google || !google.gears || !store) { return; } store.enabled = true; menuStore(); setTimeout("location.reload(true)", 5); } function disableStore() { if (!window.google || !google.gears || !store) { return; } store.enabled = false; menuStore(); setTimeout("location.reload(true)", 5); } // Utility function to output some status text. function textOut(s) { var elm = document.getElementById("t-textStore"); while (elm.firstChild) { elm.removeChild(elm.firstChild); } elm.appendChild(document.createTextNode(s)); elm.style.fontWeight='normal'; blink=false; } function textBlink() { var elm = document.getElementById("t-textStore"); if (blink) { elm.style.fontWeight='normal'; blink=false; } else { elm.style.fontWeight='bold'; blink=true; } } // update the menu entries according to the current state function menuStore() { var elm = document.getElementById("p-ggears"); if (elm) { elm.style.visibility='visible'; } if (!readCookie(GGCOOKIE_NAME) || !store) { showItem("t-createStore"); hideItem("t-updateStore"); hideItem("t-removeStore"); hideItem("t-enableStore"); hideItem("t-disableStore"); } else { hideItem("t-createStore"); showItem("t-updateStore"); showItem("t-removeStore"); if (store.enabled) { hideItem("t-enableStore"); showItem("t-disableStore"); } else { hideItem("t-disableStore"); showItem("t-enableStore"); } } } function showItem(s) { var elm = document.getElementById(s); if (elm) { elm.style.display='list-item'; } } function hideItem(s) { var elm = document.getElementById(s); if (elm) { elm.style.display='none'; } }