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

  1. /*
  2. //
  3. // BEGIN SONGBIRD GPL
  4. //
  5. // This file is part of the Songbird web player.
  6. //
  7. // Copyright(c) 2005-2009 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. Components.utils.import("resource://app/jsmodules/sbProperties.jsm");
  32.  
  33. const Cc = Components.classes;
  34. const Ci = Components.interfaces;
  35. const Cr = Components.results;
  36.  
  37. const FROM_VERSION = 20;
  38. const TO_VERSION = 21;
  39.  
  40. function LOG(s) {
  41.   dump("----++++----++++sbLibraryMigration " +
  42.        FROM_VERSION + " to " + TO_VERSION + ": " +
  43.        s +
  44.        "\n----++++----++++\n");
  45. }
  46.  
  47. function sbLibraryMigration()
  48. {
  49.   SBLocalDatabaseMigrationUtils.BaseMigrationHandler.call(this);
  50.   this._errors = [];
  51. }
  52.  
  53. //-----------------------------------------------------------------------------
  54. // sbLocalDatabaseMigration Implementation
  55. //-----------------------------------------------------------------------------
  56.  
  57. sbLibraryMigration.prototype = {
  58.   __proto__: SBLocalDatabaseMigrationUtils.BaseMigrationHandler.prototype,
  59.   classDescription: 'Songbird Migration Handler, version ' +
  60.                      FROM_VERSION + ' to ' + TO_VERSION,
  61.   classID: Components.ID("{925d55b1-9fed-451b-857b-3cdb08150d8c}"),
  62.   contractID: SBLocalDatabaseMigrationUtils.baseHandlerContractID +
  63.               FROM_VERSION + 'to' + TO_VERSION,
  64.  
  65.   _errors: [],
  66.   _progress: 0,
  67.   _total: 0,
  68.  
  69.   fromVersion: FROM_VERSION,
  70.   toVersion: TO_VERSION,
  71.   migrate: function sbLibraryMigration_migrate(aLibrary) {
  72.     if (!("@songbirdnest.com/Songbird/MetadataHandler/WMA;1" in Cc)) {
  73.       // no WMA metadata handler - nothing to do here
  74.       var query = this.createMigrationQuery(aLibrary);
  75.       query.addQuery("commit");
  76.       query.setAsyncQuery(false);
  77.       if (query.execute() != 0) {
  78.         throw("Query failed: " + query.getLastError());
  79.       }
  80.       return;
  81.     }
  82.     try{
  83.       this.startNotificationTimer();
  84.       this._databaseGUID = aLibrary.databaseGuid;
  85.       this._databaseLocation = aLibrary.databaseLocation;
  86.  
  87.       // Run a query to figure out the property ID for the new property
  88.       var query = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
  89.                     .createInstance(Ci.sbIDatabaseQuery);
  90.       query.databaseLocation = aLibrary.databaseLocation;
  91.       query.setDatabaseGUID(aLibrary.databaseGuid);
  92.  
  93.       query.addQuery(<>
  94.         INSERT OR IGNORE INTO properties
  95.           (property_name) VALUES ("{SBProperties.isDRMProtected}");</>);
  96.       query.addQuery(<>
  97.         SELECT property_id FROM properties
  98.           WHERE property_name = "{SBProperties.isDRMProtected}";
  99.         </>);
  100.       query.setAsyncQuery(false);
  101.       if (query.execute() != 0) {
  102.         throw "Failed to create property";
  103.       }
  104.       var propertyId = query.getResultObject().getRowCell(0, 0);
  105.  
  106.       // Run a query to look for all m4p files and assume they are protected :(
  107.       query = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
  108.                     .createInstance(Ci.sbIDatabaseQuery);
  109.       query.databaseLocation = aLibrary.databaseLocation;
  110.       query.setDatabaseGUID(aLibrary.databaseGuid);
  111.  
  112.       query.addQuery(<>
  113.         INSERT OR REPLACE INTO resource_properties(
  114.           media_item_id, property_id, obj, obj_searchable, obj_sortable)
  115.           SELECT media_item_id, "{propertyId}", "1", "1", "1"
  116.             FROM media_items
  117.             WHERE content_url LIKE "%.m4p";
  118.         </>);
  119.       query.setAsyncQuery(false);
  120.       if (query.execute() != 0) {
  121.         throw "Media item fetch failed";
  122.       }
  123.  
  124.       // Run a query to look for all wma files :(
  125.       query = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
  126.                     .createInstance(Ci.sbIDatabaseQuery);
  127.       query.databaseLocation = aLibrary.databaseLocation;
  128.       query.setDatabaseGUID(aLibrary.databaseGuid);
  129.  
  130.       query.addQuery(<>SELECT guid, content_url FROM media_items
  131.                          WHERE content_url LIKE "file://%.wma"
  132.                            COLLATE NOCASE</>);
  133.       query.setAsyncQuery(false);
  134.       if (query.execute() != 0) {
  135.         throw "Media item fetch failed";
  136.       }
  137.       var result = query.getResultObject();
  138.  
  139.       function addChunk(guids) {
  140.         return String(<>
  141.           INSERT OR REPLACE INTO resource_properties(
  142.             media_item_id, property_id, obj, obj_searchable, obj_sortable)
  143.             SELECT media_item_id, "{propertyId}", "1", "1", "1"
  144.               FROM media_items
  145.               WHERE guid in ("{guids.join('", "')}");
  146.           </>);
  147.       }
  148.  
  149.       // find the guids of the tracks with DRM
  150.       var handler = Cc["@songbirdnest.com/Songbird/MetadataHandler/WMA;1"]
  151.                       .createInstance(Ci.sbIMetadataHandlerWMA);
  152.       var ioService = Cc["@mozilla.org/network/io-service;1"]
  153.                         .getService(Ci.nsIIOService);
  154.       this._total = result.getRowCount();
  155.       const CHUNK_SIZE = 1000;
  156.       let guids = [];
  157.       // Run a query that will mark the library as migrated
  158.       query = this.createMigrationQuery(aLibrary);
  159.       for (this._progress = 0; this._progress < this._total; ++this._progress) {
  160.         let spec = result.getRowCell(this._progress, 1);
  161.         try {
  162.           let uri = ioService.newURI();
  163.           if (!(uri instanceof Ci.nsIFileURL)) {
  164.             // HUH? This shouldn't have matched our query...
  165.             this._errors.push(spec);
  166.             continue;
  167.           }
  168.           let file = uri.file;
  169.           if (!file.exists()) {
  170.             continue;
  171.           }
  172.           if (handler.isDRMProtected(file.path)) {
  173.             guids.push(result.getRowCell(this._progress, 0));
  174.           }
  175.         } catch (e) {
  176.           // silently eat all exceptions
  177.           this._errors.push(spec);
  178.         }
  179.         if (guids.length >= CHUNK_SIZE) {
  180.           query.addQuery(addChunk(guids));
  181.           guids = [];
  182.         }
  183.       }
  184.  
  185.       if (guids.length > 0) {
  186.         query.addQuery(addChunk(guids));
  187.       }
  188.       query.addQuery("commit");
  189.       query.setAsyncQuery(false);
  190.       if (query.execute() != 0) {
  191.         throw("Query failed: " + query.getLastError());
  192.       }
  193.     }
  194.     catch (e) {
  195.       LOG("Exception occured: " + e);
  196.       throw e;
  197.     }
  198.     finally {
  199.       this.stopNotificationTimer();
  200.     }
  201.   },
  202.  
  203. ///// sbIJobProgress
  204.   get status() Ci.sbIJobProgress.STATUS_RUNNING,
  205.   get blocked() false,
  206.   get progress() this._progress,
  207.   get total() this._total,
  208.   get errorCount() this._errors.length,
  209.   getErrorMessages: function sbLibMig20to21_getErrorMessages()
  210.     ArrayConverter.StringEnumerator(this._errors),
  211.   /// addJobProgressListener / removeJobProgressListener not implemented
  212. ///// sbIJobProgressCancellable
  213.   get canCancel() false,
  214.   cancel: function sbLibMig20to21_cancel() { throw Cr.NS_ERROR_NOT_IMPLEMENTED }
  215. };
  216.  
  217. //-----------------------------------------------------------------------------
  218. // Module
  219. //-----------------------------------------------------------------------------
  220. function NSGetModule(compMgr, fileSpec) {
  221.   return XPCOMUtils.generateModule([
  222.     sbLibraryMigration
  223.   ]);
  224. }
  225.  
  226.