1 /*
  2 Script:
  3 	Deluge.Formatters.ks
  4 
  5 Copyright:
  6 	(C) Damien Churchill 2009 <damoxc@gmail.com>
  7 	This program is free software; you can redistribute it and/or modify
  8 	it under the terms of the GNU General Public License as published by
  9 	the Free Software Foundation; either version 3, or (at your option)
 10 	any later version.
 11 
 12 	This program is distributed in the hope that it will be useful,
 13 	but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 	GNU General Public License for more details.
 16 
 17 	You should have received a copy of the GNU General Public License
 18 	along with this program.  If not, write to:
 19 		The Free Software Foundation, Inc.,
 20 		51 Franklin Street, Fifth Floor
 21 		Boston, MA  02110-1301, USA.
 22 
 23     In addition, as a special exception, the copyright holders give
 24     permission to link the code of portions of this program with the OpenSSL
 25     library.
 26     You must obey the GNU General Public License in all respects for all of
 27     the code used other than OpenSSL. If you modify file(s) with this
 28     exception, you may extend this exception to your version of the file(s),
 29     but you are not obligated to do so. If you do not wish to do so, delete
 30     this exception statement from your version. If you delete this exception
 31     statement from all source files in the program, then also delete it here.
 32 */
 33 
 34 /**
 35  * @description A collection of functions for string formatting values.
 36  * @namespace Deluge.Formatters
 37  */
 38 Deluge.Formatters = {
 39 	/**
 40 	 * Formats a date string in the locale's date representation based on the
 41 	 * systems timezone.
 42 	 *
 43 	 * @param {number} timestamp time in seconds since the Epoch
 44 	 * @returns {string} a string in the locale's date representation or ""
 45 	 * if seconds < 0
 46 	 */
 47 	date: function(timestamp) {
 48 		function zeroPad(num, count) {
 49 			var numZeropad = num + '';
 50 			while (numZeropad.length < count) {
 51 				numZeropad = '0' + numZeropad;
 52 			}
 53 			return numZeropad;
 54 		}
 55 		timestamp = timestamp * 1000;
 56 		var date = new Date(timestamp);
 57 		return String.format('{0}/{1}/{2}', zeroPad(date.getDate(), 2), zeroPad(date.getMonth() + 1, 2), date.getFullYear());
 58 	},
 59 	
 60 	/**
 61 	 * Formats the bytes value into a string with KiB, MiB or GiB units.
 62 	 *
 63 	 * @param {number} bytes the filesize in bytes
 64 	 * @returns {string} formatted string with KiB, MiB or GiB units.
 65 	 */
 66 	size: function(bytes) {
 67 		bytes = bytes / 1024.0;
 68 	
 69 		if (bytes < 1024) { return bytes.toFixed(1)  + ' KiB'; }
 70 		else { bytes = bytes / 1024; }
 71 	
 72 		if (bytes < 1024) { return bytes.toFixed(1)  + ' MiB'; }
 73 		else { bytes = bytes / 1024; }
 74 	
 75 		return bytes.toFixed(1) + ' GiB'
 76 	},
 77 	
 78 	/**
 79 	 * Formats a string to display a transfer speed utilizing {@link Deluge.Formatters.size}
 80 	 *
 81 	 * @param {number} bytes the filesize in bytes
 82 	 * @returns {string} formatted string with KiB, MiB or GiB units.
 83 	 */
 84 	speed: function(bits) {
 85 		return fsize(bits) + '/s'
 86 	},
 87 	
 88 	/**
 89 	 * Formats a string to show time in a human readable form.
 90 	 *
 91 	 * @param {number} time the number of seconds
 92 	 * @returns {string} a formatted time string. will return '' if seconds == 0
 93 	 */
 94 	timeRemaining: function(time) {
 95 		if (time == 0) { return '∞' }
 96 		if (time < 60) { return time + 's'; }
 97 		else { time = time / 60; }
 98 	
 99 		if (time < 60) {
100 			var minutes = Math.floor(time)
101 			var seconds = Math.round(60 * (time - minutes))
102 			if (seconds > 0) {
103 				return minutes + 'm ' + seconds + 's';
104 			} else {
105 				return minutes + 'm'; }
106 			}
107 		else { time = time / 60; }
108 	
109 		if (time < 24) { 
110 			var hours = Math.floor(time)
111 			var minutes = Math.round(60 * (time - hours))
112 			if (minutes > 0) {
113 				return hours + 'h ' + minutes + 'm';
114 			} else {
115 				return hours + 'h';
116 			}			
117 		}
118 		else { time = time / 24; }
119 	
120 		var days = Math.floor(time)
121 		var hours = Math.round(24 * (time - days))
122 		if (hours > 0) {
123 			return days + 'd ' + hours + 'h';
124 		} else {
125 			return days + 'd';
126 		}
127 	},
128 	
129 	/**
130 	 * Simply returns the value untouched, for when no formatting is required.
131 	 *
132 	 * @param value, the value to be displayed
133 	 * @returns the untouched value.
134 	 */
135 	plain: function(value) {
136 		return value;
137 	}
138 }
139 var fsize = Deluge.Formatters.size;
140 var fspeed = Deluge.Formatters.speed;
141 var ftime = Deluge.Formatters.timeRemaining;
142 var fdate = Deluge.Formatters.date;
143 var fplain = Deluge.Formatters.plain;
144