js/yubi.js
changeset 0 52d30e6014a0
equal deleted inserted replaced
-1:000000000000 0:52d30e6014a0
       
     1 
       
     2 /*######################################################################
       
     3 ### I N I T I A L I Z A T I O N
       
     4 ######################################################################*/
       
     5 var Y = global.Y;
       
     6 
       
     7 // Global jQuery callback queues.  Used across the application to trigger actions
       
     8 // on various events.
       
     9 //
       
    10 Y.callback = function( name, flags ) {
       
    11         if ( ! Y.callbacks ) Y.callbacks = {};
       
    12 
       
    13         // Create a new callback queue if one doesn't already exist
       
    14         // for the provided +name+.
       
    15         //
       
    16         if ( ! Y.callbacks[name] ) {
       
    17                 if ( ! flags ) flags = 'unique';
       
    18                 Y.callbacks[name] = $.Callbacks( flags );
       
    19         }
       
    20 
       
    21         return Y.callbacks[name];
       
    22 };
       
    23 
       
    24 // Node's gui interface.
       
    25 Y.gui = require( 'nw.gui' );
       
    26 
       
    27 
       
    28 // Execute yubi external processes.
       
    29 //
       
    30 Y.yubiexec = function( cmd, args ) {
       
    31 
       
    32 	// Add the dynamic lib path for libyubi.
       
    33 	//
       
    34 	var lib = './yubi/' + process.platform + '/lib';
       
    35 	var env = process.env;
       
    36 	env[ 'DYLD_LIBRARY_PATH' ] = env[ 'DYLD_LIBRARY_PATH' ] + ':' + lib;
       
    37 	// FIXME:  need to add linux and windows
       
    38 
       
    39 	if ( ! args ) args = [];
       
    40 	var binary = './yubi/' + process.platform + '/bin/' + cmd;
       
    41 	Y.fs.chmodSync( binary, 0755 );
       
    42 	var child = Y.spawn( binary, args, { env: env });
       
    43 
       
    44 	if ( ! child ) {
       
    45 		Y.callback( cmd + '-fatal' ).fire();
       
    46 		return;
       
    47 	}
       
    48 
       
    49 	// Send callbacks when data is received.
       
    50 	//
       
    51 	child.stdout.on( 'data', function(data) {
       
    52 		Y.callback( cmd ).fire( data );
       
    53 	});
       
    54 	child.stderr.on( 'data', function(data) {
       
    55 		Y.callback( cmd + '-error' ).fire( data );
       
    56 	});
       
    57 
       
    58 	// Log exit code.
       
    59 	//
       
    60 	child.on( 'close', function(code) {
       
    61 		console.log( cmd + ' process exited with code: ' + code);
       
    62 	});
       
    63 };
       
    64 
       
    65 
       
    66 // Callback behaviors for ykinfo execution.
       
    67 //
       
    68 Y.callback( 'ykinfo' ).add( function(data) {
       
    69 	Y.keyinfo = {};
       
    70 	$.each( data.toString().split("\n"), function(i, info) {
       
    71 		var match = info.match( /^(\w+):\s+(.+)$/ );
       
    72 		if ( match ) Y.keyinfo[ match[1] ] = match[2];
       
    73 	});
       
    74 });
       
    75 Y.callback( 'ykinfo-fatal' ).add( function() { Y.keyinfo = {} } );
       
    76 Y.callback( 'ykinfo-error' ).add( function() { Y.keyinfo = {} } );
       
    77