license_manager.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright 2012-2013 AGR Audio, Industria e Comercio LTDA. <contato@moddevices.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. function LicenseManager() {
  18. this.index_by_uri = {};
  19. this.index_by_id = {};
  20. }
  21. // Stores a license in internal index
  22. LicenseManager.prototype.addLicense = function(license) {
  23. this.index_by_uri[license.plugin_uri] = license
  24. this.index_by_id[license.id] = license
  25. }
  26. // Gets a list of licenses available, sends it to MOD,
  27. // receives a list of missing license IDs and
  28. // install all missing licenses.
  29. LicenseManager.prototype.addLicenses = function(license_info, callback) {
  30. var self = this;
  31. for (var i in license_info)
  32. self.addLicense(license_info[i]);
  33. $.ajax({
  34. url: '/effect/licenses/list',
  35. method: 'POST',
  36. data: JSON.stringify(license_info),
  37. dataType: 'json',
  38. success: function(missing) {
  39. self.installLicenses(missing, callback)
  40. }
  41. });
  42. }
  43. // Asynchronously download a list of licenses and saves them to MOD
  44. LicenseManager.prototype.installLicenses = function(missingLicenseIds, callback) {
  45. var self = this;
  46. var installedLicenses = missingLicenseIds.length
  47. if (installedLicenses == 0) {
  48. callback(installedLicenses)
  49. return
  50. }
  51. var installNext = function() {
  52. if (missingLicenseIds.length == 0) {
  53. $.ajax({
  54. url: '/effect/refresh',
  55. cache: false,
  56. dataType: 'json',
  57. success: function() {
  58. callback(installedLicenses)
  59. }
  60. });
  61. return
  62. }
  63. self.installLicense(missingLicenseIds.pop(), installNext);
  64. }
  65. installNext();
  66. }
  67. // Download single license from cloud and send to MOD
  68. LicenseManager.prototype.installLicense = function(licenseId, callback) {
  69. var self = this;
  70. var success = function() {
  71. var uri = self.index_by_id[licenseId].plugin_uri;
  72. desktop.license(uri);
  73. callback();
  74. }
  75. $.ajax({
  76. url: SITEURL + '/licenses/' + licenseId,
  77. method: 'GET',
  78. headers: {
  79. 'Authorization' : 'MOD ' + desktop.cloudAccessToken
  80. },
  81. success: function(data) {
  82. $.ajax({
  83. url: '/effect/licenses/save/' + licenseId,
  84. cache: false,
  85. data: data,
  86. method: 'POST',
  87. success: success,
  88. error: function() {
  89. new Notification('error', 'Could not save plugin license, please contact support', 8000);
  90. callback();
  91. }
  92. })
  93. },
  94. error: function() {
  95. new Notification('error', 'Could not get plugin license from Cloud, please contact support', 8000);
  96. callback();
  97. }
  98. });
  99. }
  100. // Checks if a plugin is licensed
  101. LicenseManager.prototype.licensed = function(uri) {
  102. return !! this.index_by_uri[uri];
  103. }