home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / sbMigrate08pre3To08pre4.js < prev    next >
Text File  |  2012-06-08  |  9KB  |  245 lines

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. //
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2008 POTI, Inc.
  8. // http://songbirdnest.com
  9. //
  10. // This file may be licensed under the terms of of the
  11. // GNU General Public License Version 2 (the "GPL").
  12. //
  13. // Software distributed under the License is distributed
  14. // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
  15. // express or implied. See the GPL for the specific language
  16. // governing rights and limitations.
  17. //
  18. // You should have received a copy of the GPL along with this
  19. // program. If not, go to http://www.gnu.org/licenses/gpl.html
  20. // or write to the Free Software Foundation, Inc.,
  21. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. //
  23. // END SONGBIRD GPL
  24. //
  25. */
  26.  
  27. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  28. Components.utils.import("resource://app/jsmodules/ArrayConverter.jsm");
  29. Components.utils.import("resource://app/jsmodules/sbLocalDatabaseMigrationUtils.jsm");
  30. Components.utils.import("resource://app/jsmodules/SBJobUtils.jsm");
  31.  
  32. const Cc = Components.classes;
  33. const Ci = Components.interfaces;
  34. const Cr = Components.results;
  35.  
  36. function LOG(s) {
  37.   dump("----++++----++++sbLibraryMigration " + 
  38.        sbLibraryMigration.versionString + ": " + 
  39.        s + 
  40.        "\n----++++----++++\n");
  41. }
  42.  
  43. function sbLibraryMigration()
  44. {
  45.   SBLocalDatabaseMigrationUtils.BaseMigrationHandler.call(this);
  46.   
  47.   this.fromVersion = 11;
  48.   this.toVersion   = 12;
  49.   this.versionString = this.fromVersion + " to " + 
  50.                        this.toVersion;
  51. }
  52.  
  53. //-----------------------------------------------------------------------------
  54. // sbLocalDatabaseMigration Implementation
  55. //-----------------------------------------------------------------------------
  56.  
  57. sbLibraryMigration.prototype = {
  58.   __proto__: SBLocalDatabaseMigrationUtils.BaseMigrationHandler.prototype,
  59.   classDescription: 'Songbird Migration Handler for converting utf16 to utf8 storage.',
  60.   classID: Components.ID("{E313D2F1-D1BE-4683-963F-5F43C5245C6C}"),  
  61.   contractID: SBLocalDatabaseMigrationUtils.baseHandlerContractID + "utf16 to utf8",
  62.  
  63.   _mOldLibrary: null,
  64.   _databaseGUID: null,
  65.  
  66.   migrate: function sbLibraryMigration_migrate(aLibrary) {
  67.     try{
  68.       this._databaseGUID = aLibrary.databaseGuid;
  69.       this._databaseLocation = aLibrary.databaseLocation;
  70.  
  71.       dump("Migrating " + this._databaseLocation.spec + this._databaseGUID + "\n");
  72.       var dbParentDir = 
  73.         this._databaseLocation.QueryInterface(Ci.nsIFileURL).file;
  74.       var dbEngine = Cc["@songbirdnest.com/Songbird/DatabaseEngine;1"]
  75.                        .getService(Ci.sbIDatabaseEngine);
  76.  
  77.       // Dump the existing data into a temporary file
  78.       var oldLibDumpFile = dbParentDir.clone();
  79.       oldLibDumpFile.append(this._databaseGUID + ".txt");
  80.       if (oldLibDumpFile.exists()) {
  81.         oldLibDumpFile.remove(false);
  82.       }
  83.       oldLibDumpFile.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0777);
  84.  
  85.       dbEngine.dumpDatabase(this._databaseGUID, oldLibDumpFile);
  86.  
  87.       // Close existing DB handles and move the database to a new location
  88.       dbEngine.closeDatabase(this._databaseGUID);
  89.    
  90.       // Move the old database to a new temporary location
  91.       var oldLibraryFile = dbParentDir.clone();
  92.       oldLibraryFile.append(this._databaseGUID + ".db");
  93.     
  94.       // Paranoia, make sure the new temp location for the old database doesn't exist
  95.       var tempOldLibFile = dbParentDir.clone();
  96.       tempOldLibFile.append("old_" + oldLibraryFile.leafName);
  97.       if (tempOldLibFile.exists()) {
  98.         tempOldLibFile.remove(false);
  99.       }
  100.       oldLibraryFile.moveTo(oldLibraryFile.parent, tempOldLibFile.leafName);
  101.  
  102.       var query = this._createQuery();
  103.       query.addQuery("begin");
  104.       this._createLibrary(query);
  105.  
  106.       // Remove all the TABLES from the new library.
  107.       query.addQuery("drop table if exists media_items");
  108.       query.addQuery("drop table if exists media_list_types");
  109.       query.addQuery("drop table if exists properties");
  110.       query.addQuery("drop table if exists resource_properties");
  111.       query.addQuery("drop table if exists library_metadata");
  112.       query.addQuery("drop table if exists simple_media_lists");
  113.       query.addQuery("drop table if exists library_media_item");
  114.       query.addQuery("drop table if exists resource_properties_fts");
  115.  
  116.       // Bug 13033 we don't want to drop this table, we'll ignore the create in the dump
  117.       //query.addQuery("drop table if exists resource_properties_fts_all;");
  118.  
  119.       var retval = {};
  120.       // Read in the dumped out SQL from the dump text file
  121.       var fileInputStream = Cc["@mozilla.org/network/file-input-stream;1"]
  122.                             .createInstance(Ci.nsIFileInputStream);
  123.       fileInputStream.init(oldLibDumpFile, 1, 0, 0);
  124.  
  125.       var inputStream = Cc["@mozilla.org/intl/converter-input-stream;1"]
  126.                           .createInstance(Ci.nsIConverterInputStream);
  127.       inputStream.init(fileInputStream, "UTF-8", 16384,
  128.                      Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  129.       inputStream = inputStream.QueryInterface(Ci.nsIUnicharLineInputStream);
  130.  
  131.       var curLine = {};
  132.       var curQueryStr = "";
  133.       var cont;
  134.       do {
  135.         cont = inputStream.readLine(curLine);
  136.         curQueryStr += curLine.value;
  137.         // If this string ends with a ';', it is the end of the query.
  138.         // Let's push it into the query object.
  139.         if (curQueryStr.charAt(curQueryStr.length - 1) == ";") {
  140.           // check for the resource_properties_fts_all table and skip it.
  141.           // The dump version doesn't work see bug 13033
  142.           if (!curQueryStr.match("INSERT INTO sqlite_master.*resource_properties_fts_all")) {
  143.             query.addQuery(curQueryStr);
  144.           }
  145.           curQueryStr = "";
  146.         }
  147.       } while (cont);
  148.  
  149.       inputStream.close();
  150.  
  151.       query.addQuery("update library_metadata set value = '" 
  152.                      + this.toVersion + "' where name = 'version'");
  153.       query.addQuery("commit");
  154.  
  155.       // Insert the dumped information into the new database.
  156.       query.setAsyncQuery(true);
  157.       query.execute(retval);
  158.       dump("Database execute\n");
  159.       var sip = Cc["@mozilla.org/supports-interface-pointer;1"]
  160.                   .createInstance(Ci.nsISupportsInterfacePointer);
  161.       sip.data = this;
  162.    
  163.       // XXX todo: Localize!
  164.       this._titleText = "Library Migration Helper";
  165.       this._statusText = "Converting Database Format...";
  166.       this.migrationQuery = query;
  167.     
  168.       this.startNotificationTimer();
  169.       SBJobUtils.showProgressDialog(sip.data, null, 0);
  170.       this.stopNotificationTimer();
  171.       // Cleanup
  172.       tempOldLibFile.remove(false);
  173.       oldLibDumpFile.remove(false);
  174.     }
  175.     catch (e) {
  176.       dump("Exception occured: " + e);
  177.       throw e;
  178.     }
  179.   },
  180.  
  181.   _createQuery: function sbLibraryMigration_createQuery() {
  182.     var query = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
  183.                   .createInstance(Ci.sbIDatabaseQuery);
  184.     query.databaseLocation = this._databaseLocation;
  185.     query.setDatabaseGUID(this._databaseGUID);
  186.     
  187.     return query;
  188.   },
  189.   _createLibrary : function sbLibraryMigration_createLibrary(query) {
  190.     try {
  191.         // Now that we know we have appropriate permissions make a new query.
  192.     
  193.       var ioService = Components.classes["@mozilla.org/network/io-service;1"]
  194.                             .getService(Components.interfaces.nsIIOService);
  195.  
  196.       var schemaURI = ioService.newURI("chrome://songbird/content/library/localdatabase/schema.sql", null, null);
  197.       var channel = ioService.newChannelFromURI(schemaURI);
  198.       var stream = channel.open();
  199.  
  200.       var converterStream = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
  201.                                  .createInstance(Components.interfaces.nsIConverterInputStream);
  202.  
  203.       converterStream.init(stream,
  204.                            "UTF-8",
  205.                            8192,
  206.                            Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  207.  
  208.       var response = "";
  209.       var result = {};
  210.       var bytesRead = converterStream.readString(0xFFFFFFFF, result);
  211.   
  212.       while (bytesRead > 0) {
  213.         response += result.value;
  214.         bytesRead = converterStream.readString(0xFFFFFFFF, result);
  215.       }
  216.  
  217.       converterStream.close();
  218.       stream.close();
  219.  
  220.       const colonNewline = ";\n";
  221.       var posStart = 0;
  222.       var posEnd = response.indexOf(colonNewline);
  223.       while (posEnd >= 0) {
  224.         query.addQuery(response.substring(posStart, posEnd));
  225.         posStart = posEnd + 2;
  226.         posEnd = response.indexOf(colonNewline, posStart);
  227.       }
  228.     }
  229.     catch (e) {
  230.       dump("Exception: " + e);
  231.       throw e;
  232.     }
  233.   }
  234. };
  235.  
  236. //-----------------------------------------------------------------------------
  237. // Module
  238. //-----------------------------------------------------------------------------
  239. function NSGetModule(compMgr, fileSpec) {
  240.   return XPCOMUtils.generateModule([
  241.     sbLibraryMigration
  242.   ]);
  243. }
  244.  
  245.