home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / jsmodules / sbLocalDatabaseMigrationUtils.jsm < prev    next >
Text File  |  2012-06-08  |  6KB  |  207 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. EXPORTED_SYMBOLS = ["SBLocalDatabaseMigrationUtils"];
  28.  
  29. const Cc = Components.classes;
  30. const Ci = Components.interfaces;
  31. const Cr = Components.results;
  32. const Cu = Components.utils;
  33.  
  34. Cu.import("resource://gre/modules/XPCOMUtils.jsm");
  35. Cu.import("resource://app/jsmodules/SBJobUtils.jsm");
  36. Cu.import("resource://app/jsmodules/SBTimer.jsm");
  37.  
  38. var SBLocalDatabaseMigrationUtils = {
  39.   baseHandlerContractID: "@songbirdnest.com/Songbird/Library/LocalDatabase/Migration/Handler/"
  40. }
  41.  
  42. /**
  43.  *
  44.  */
  45. SBLocalDatabaseMigrationUtils.BaseMigrationHandler = function() {
  46.   SBJobUtils.JobBase.call(this);
  47. }
  48.  
  49. SBLocalDatabaseMigrationUtils.BaseMigrationHandler.prototype = {
  50.   __proto__               : SBJobUtils.JobBase.prototype,
  51.   
  52.   QueryInterface          : XPCOMUtils.generateQI(
  53.       [Ci.sbIJobProgress, Ci.sbIJobCancelable, 
  54.        Ci.sbILocalDatabaseMigrationHandler, Ci.nsIClassInfo]),
  55.  
  56.   /** nsIClassInfo, so callers don't have to QI from JS **/
  57.   
  58.   classDescription        : 'Songbird Base Local Database Migration Class',
  59.   classID                 : null,
  60.   contractID              : null,
  61.   flags                   : Ci.nsIClassInfo.MAIN_THREAD_ONLY,
  62.   implementationLanguage  : Ci.nsIProgrammingLanguage.JAVASCRIPT,
  63.   getHelperForLanguage    : function(aLanguage) { return null; },
  64.   getInterfaces : function(count) {
  65.     var interfaces = [Ci.sbIJobProgress,
  66.                       Ci.sbIJobCancelable,
  67.                       Ci.sbILocalDatabaseMigrationHandler,
  68.                       Ci.nsIClassInfo,
  69.                       Ci.nsISupports
  70.                      ];
  71.     count.value = interfaces.length;
  72.     return interfaces;
  73.   },
  74.   
  75.   migrationQuery: null,
  76.  
  77.   //
  78.   // Helpers to pump progress
  79.   //
  80.   _notifyJobsTimer: null,
  81.   
  82.   startNotificationTimer: function BaseMigrationHandler_startNotificationTimer(aTimeout) {
  83.     if(this._notifyJobsTimer) {
  84.       this._notifyJobsTimer.cancel();
  85.       this._notifyJobsTimer = null;
  86.     }
  87.     
  88.     if(aTimeout == null) {
  89.       aTimeout = 66;
  90.     }
  91.     
  92.     var self = this;
  93.     function notifyListeners() {
  94.       self.notifyJobProgressListeners();
  95.     }
  96.     
  97.     this._notifyJobsTimer = new SBTimer(notifyListeners,
  98.                                         aTimeout, 
  99.                                         Ci.nsITimer.TYPE_REPEATING_SLACK);
  100.   },
  101.   
  102.   stopNotificationTimer: function BaseMigrationHandler_stopNotificationTimer() {
  103.     if(this._notifyJobsTimer) {
  104.       this._notifyJobsTimer.cancel();
  105.       this._notifyJobsTimer = null;
  106.     }
  107.   },
  108.   
  109.   //
  110.   // sbIJobProgress overrides
  111.   //
  112.   get status() {
  113.     if(this.migrationQuery) {
  114.       var executing = this.migrationQuery.isExecuting();
  115.       if(executing) {
  116.         return Ci.sbIJobProgress.STATUS_RUNNING;
  117.       }
  118.       
  119.       var complete = ( this.migrationQuery.getQueryCount() == 0 );
  120.                       
  121.       if(!executing) {
  122.         if (complete) {
  123.           return Ci.sbIJobProgress.STATUS_SUCCEEDED;
  124.         }
  125.         else {
  126.           return Ci.sbIJobProgress.STATUS_FAILED;
  127.         }
  128.       }
  129.     }
  130.     
  131.     return this._status;
  132.   },
  133.   
  134.   get progress() {
  135.     if(this.migrationQuery) {
  136.       return this.migrationQuery.currentQuery();
  137.     }
  138.     
  139.     return this._progress;    
  140.   },
  141.   
  142.   get total() {
  143.     if(this.migrationQuery && !this._total) {
  144.       // this number will be less than the original number of queries added
  145.       // but is the number of remaining queries as of the first call to this function
  146.       // which is close enough for simply tracking progress of a long sql job.
  147.       this._total = this.migrationQuery.getQueryCount() + this.migrationQuery.currentQuery();
  148.     }
  149.  
  150.     return this._total;
  151.   },
  152.   
  153.   get canCancel() {
  154.     if(this.migrationQuery) {
  155.       return true;
  156.     }
  157.     
  158.     return this._canCancel;
  159.   },
  160.   
  161.   cancel: function BaseMigrationHandler_cancel() {
  162.     if(this.migrationQuery) {
  163.       this.migrationQuery.abort();
  164.     }
  165.     
  166.     return;
  167.   },
  168.  
  169.   // Create a query including an open transaction that
  170.   // will start by setting the new library version  
  171.   createMigrationQuery: function sbLibraryMigration_createMigrationQuery(aLibrary) {
  172.     var query = Cc["@songbirdnest.com/Songbird/DatabaseQuery;1"]
  173.                   .createInstance(Ci.sbIDatabaseQuery);
  174.     query.databaseLocation = aLibrary.databaseLocation;
  175.     query.setDatabaseGUID(aLibrary.databaseGuid);
  176.  
  177.     query.addQuery("begin");
  178.     
  179.     // Update the schema version to the destination version.
  180.     query.addQuery("update library_metadata set value = '" 
  181.                    + this.toVersion + "' where name = 'version'");
  182.  
  183.     return query;
  184.   },
  185.   
  186.   //
  187.   // sbILocalDatabaseMigrationHandler
  188.   //
  189.   
  190.   /**
  191.    * Set this to the version the handler can migrate from.
  192.    */
  193.   fromVersion : 0,
  194.   
  195.   /**
  196.    * Set this to the version the handler migrates to.
  197.    */
  198.   toVersion   : 0,
  199.   
  200.   /**
  201.    * Override this method to provide migration of a library.
  202.    */
  203.   migrate     : function BaseMigrationHandler_migrate(aLibrary) {
  204.     throw Cr.NS_ERROR_NOT_IMPLEMENTED;
  205.   },
  206. }
  207.