mpviewplugins/mpdetailsviewplugin/resources/musiccontext.js
changeset 29 8192e5b5c935
child 32 c163ef0b758d
equal deleted inserted replaced
25:3ec52facab4d 29:8192e5b5c935
       
     1 //Logging for QT
       
     2 
       
     3 // Override the default alert function.
       
     4 alert = function(str) {
       
     5     console.log(str);
       
     6     if(window['context'] && context['owner']){
       
     7         context.owner().debugJs('alert: ' + str);
       
     8     }
       
     9     window.context.owner().errorHandler("Alert", str);
       
    10 }
       
    11 
       
    12 function logme(str){
       
    13     console.log(str);
       
    14     if(window['context'] && context['owner']){
       
    15         context.owner().debugJs('debugJs: ' + str);
       
    16     }
       
    17 }
       
    18 
       
    19 function traceme(){
       
    20     var cf = arguments.callee.caller;
       
    21     logme('-------------- Start traceme ' + new Date().toLocaleTimeString() + ' --------------');
       
    22     var arr = new Array();
       
    23     while (cf) {
       
    24       var fn = cf.toString();
       
    25       fn = fn.substr(0,fn.indexOf('{'));
       
    26       arr.push(fn);
       
    27       cf = cf.caller;
       
    28     }
       
    29     //arr.reverse();
       
    30     for(i=0;i<=arr.length -1;i++){
       
    31         logme(arr[i]);
       
    32     }
       
    33     logme('-------------- End traceme --------------');
       
    34 }
       
    35 
       
    36 
       
    37 window["music"] = window["music"] ||
       
    38 {};
       
    39 
       
    40 (function(){
       
    41     if (typeof window.context === 'undefined') {
       
    42         window['context'] = {
       
    43             token: "hipsu:hipsupass",
       
    44             song: "Queen - Bohemian Rhapsody",
       
    45             objectReservedLength: function(){return 70;},
       
    46             objectContent: function() { return "http://music.ovi.com/ru/ru/pc/Product/Queen/Bohemian-Rhapsody/8019069"; },
       
    47             objectType: function(){ return "URI"; },
       
    48             artist: function() { return "&#9835; Queen"; },
       
    49             title: function() { return "Bohemian Rhapsody"; },
       
    50             username: function() { return "hipsu"; },
       
    51             password: function() { return "hipsupass"; },
       
    52             albumArtBase64: function() { return "https://secure-profile.facebook.com/v227/1739/21/n100000756794479_1863.jpg"; },
       
    53             owner: function() {
       
    54                 return {
       
    55                     errorHandler: function(a,b) {},
       
    56                     clearCache: function() {},
       
    57                     debugJs: function(s) {}
       
    58                 }
       
    59             }
       
    60         }
       
    61     }else{
       
    62        try {
       
    63            logme(context.objectType() + ' | ' + window.context.objectContent() + ' | ' + window.context.objectReservedLength());
       
    64        }catch (e) {
       
    65            logme(e);
       
    66        }
       
    67     }
       
    68 
       
    69 	var musicContext = null;
       
    70 	var publishingPlayer = null;
       
    71     
       
    72     music.getPostContent = function(){
       
    73         traceme();
       
    74         return {
       
    75             reservedLength : window.context.objectReservedLength(),
       
    76 			type : window.context.objectType(), 
       
    77 		    content : window.context.objectContent()
       
    78         };
       
    79     }
       
    80     	
       
    81     // Hard code something for the browser..
       
    82 
       
    83 	/**
       
    84 	 * Check if player is loaded and call error function if not.
       
    85 	 */
       
    86 	music.playerLoaded = function() {
       
    87         traceme();
       
    88 		if (!_playerReady()) {
       
    89             alert("Error loading player scripts");
       
    90 		}
       
    91 	}
       
    92 
       
    93 	/**
       
    94 	 * Call this for player to show UI and initialize connection to SNC
       
    95 	 */
       
    96 	music.initPlayer = function() 
       
    97 	{	
       
    98         traceme();
       
    99 	    // Initialize player    
       
   100 	    publishingPlayer = new ovi.player.publish.Player({
       
   101 	        locale : "en-US",
       
   102 	        credentials : {
       
   103 	            type : "",
       
   104 	            sso_base_url : "",
       
   105 	            token : _getNoa() 
       
   106 	        }
       
   107 	    });
       
   108 	    
       
   109 	    // Show UI 
       
   110 	    publishingPlayer.create(null, function(status, data){
       
   111             traceme();
       
   112 			if (status != publishingPlayer.status.updateservices_ok && status != publishingPlayer.status.show_ok) {
       
   113 				// Delegate errors to native side
       
   114                 window.context.errorHandler(status, data.message);
       
   115 			} 		
       
   116 		});	
       
   117 		
       
   118 	}
       
   119 	
       
   120 	/** 
       
   121 	 * Call this for player to update context area (the song UI).
       
   122 	 */
       
   123 	music.updateContextArea = function() {
       
   124 		// Update artist and title metadata.
       
   125         traceme();
       
   126 	    publishingPlayer.setContextObject(_getMusicContext());
       
   127 	}
       
   128 	
       
   129 	/**
       
   130 	 * Call this function to update metadata (called when music player has retrieved the link) 
       
   131 	 */
       
   132 	music.updateMetadata = function() {
       
   133 	    // Update url at bottom of comment field.
       
   134         traceme();
       
   135 		musicContext.updateObject(music.getPostContent());
       
   136 	}
       
   137 	
       
   138 	/**
       
   139 	 * Call this function to release all resources from player.
       
   140 	 */
       
   141 	music.teardown = function() {
       
   142         traceme();
       
   143 		publishingPlayer.cancel();
       
   144 	}
       
   145 	
       
   146 	// Private functions, read data from native side..
       
   147 	function _playerReady() {
       
   148         traceme();
       
   149         return typeof ovi != "undefined" &&
       
   150             typeof ovi.player != "undefined" &&
       
   151 			typeof ovi.player.publish != "undefinded";
       
   152 	}
       
   153 	
       
   154 	
       
   155 	function _getMusicContext() {
       
   156 	    traceme();            
       
   157 	    musicContext = new ovi.player.contextobject.Player(); 
       
   158         
       
   159         var attachment = music.getPostContent();
       
   160 	
       
   161         logme('music._getMusicContext() | attachment'); 	 
       
   162 
       
   163         logme('music._getMusicContext() | musicContext.create');
       
   164 
       
   165         var sharePlayerArtImage = "";
       
   166         var sharePlayerArtBase64 = window.context.albumArtBase64();
       
   167         if ( sharePlayerArtBase64.length > 0 ) {
       
   168             // TODO: this is temporary solution until base64 defect in QT is fixed.
       
   169             sharePlayerArtImage = "<img class='cover' src='" + sharePlayerArtBase64 + "' width='74px' height='74px' />";
       
   170 //            sharePlayerArtImage = "<img class='cover' src='data:image/png;base64," + sharePlayerArtBase64 + "' width='74px' height='74px' />";
       
   171         }
       
   172         // Create context object
       
   173 	    musicContext.create({
       
   174 	        view: "none",
       
   175 	        data: { 
       
   176 	            miniview: "<ul class='list info-list'>"
       
   177 				/*
       
   178 						// TODO REMOVE, for testing only
       
   179 						+ "<p>username: " + window.context.username() + "/" + window.context.password() + "</p><BR>" 
       
   180 						+ "<input type=\"button\" value=\"Clear JS Cache\" onClick=\"window.context.owner().clearCache();\">"
       
   181                 */
       
   182                         + sharePlayerArtImage
       
   183 						+ "<span class='description'><div class='title'>" + window.context.title() + "</div>"
       
   184 						+ "<div class='artist'>" + window.context.artist() + "</div></span>"
       
   185 					    + "</ul>",
       
   186 	            object: attachment 
       
   187 	        }
       
   188 	    });
       
   189         return musicContext;
       
   190 	}
       
   191 	                        
       
   192 	function _getNoa() {
       
   193         traceme();
       
   194 	    // temporary solution until SSO token get from QT SSO DE (now we fetch username, password and re-authenticate using api.ovi.com)
       
   195 	    var token = (window.context.username)? [window.context.username(),window.context.password()] : window.context.token.split(":");
       
   196         logme(token[0] + "/" + token[1]);
       
   197 	    return {
       
   198             username: token[0],
       
   199 	        password: token[1]
       
   200 	    }           
       
   201 	}	
       
   202 })();