sbDurationPropertyUnitConverter.cpp
Go to the documentation of this file.
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 // Property Unit Converter for duration values, ie: m, s, min, hours, days,
28 // weeks, months, years
29 
31 
32 // ctor - register all units
34 {
35  SetStringBundle(NS_LITERAL_STRING("chrome://songbird/locale/songbird.properties"));
37  NS_LITERAL_STRING("us"),
38  NS_LITERAL_STRING("&duration.unit.microseconds"),
39  NS_LITERAL_STRING("&duration.unit.microseconds.short"),
40  PR_TRUE);
42  NS_LITERAL_STRING("ms"),
43  NS_LITERAL_STRING("&duration.unit.milliseconds"),
44  NS_LITERAL_STRING("&duration.unit.milliseconds.short"));
46  NS_LITERAL_STRING("sec"),
47  NS_LITERAL_STRING("&duration.unit.seconds"),
48  NS_LITERAL_STRING("&duration.unit.seconds.short"));
50  NS_LITERAL_STRING("min"),
51  NS_LITERAL_STRING("&duration.unit.minutes"),
52  NS_LITERAL_STRING("&duration.unit.minutes.short"));
54  NS_LITERAL_STRING("h"),
55  NS_LITERAL_STRING("&duration.unit.hours"),
56  NS_LITERAL_STRING("&duration.unit.hours.short"));
58  NS_LITERAL_STRING("d"),
59  NS_LITERAL_STRING("&duration.unit.days"),
60  NS_LITERAL_STRING("&duration.unit.days.short"));
62  NS_LITERAL_STRING("w"),
63  NS_LITERAL_STRING("&duration.unit.weeks"),
64  NS_LITERAL_STRING("&duration.unit.weeks.short"));
66  NS_LITERAL_STRING("m"),
67  NS_LITERAL_STRING("&duration.unit.months"),
68  NS_LITERAL_STRING("&duration.unit.months.short"));
70  NS_LITERAL_STRING("y"),
71  NS_LITERAL_STRING("&duration.unit.years"),
72  NS_LITERAL_STRING("&duration.unit.years.short"));
73 }
74 
75 // dtor
77 {
78 }
79 
80 // convert from the native unit to any of the exposed units
81 NS_IMETHODIMP
83  PRUint32 aUnitID,
84  PRFloat64 &_retVal)
85 {
86  switch (aUnitID) {
88  // native unit, nothing to do
89  break;
91  aValue /= 1000.0;
92  break;
94  aValue /= (1000.0*1000.0);
95  break;
97  aValue /= (1000.0*1000.0*60.0);
98  break;
100  aValue /= (1000.0*1000.0*60.0*60.0);
101  break;
102  case DURATION_UNIT_DAYS:
103  aValue /= (1000.0*1000.0*60.0*60.0*24.0);
104  break;
105  case DURATION_UNIT_WEEKS:
106  aValue /= (1000.0*1000.0*60.0*60.0*24.0*7.0);
107  break;
109  aValue /= (1000.0*1000.0*60.0*60.0*24.0*30.0);
110  break;
111  case DURATION_UNIT_YEARS:
112  aValue /= (1000.0*1000.0*60.0*60.0*24.0*365.0);
113  break;
114  default:
115  return NS_ERROR_INVALID_ARG;
116  }
117  _retVal = aValue;
118  return NS_OK;
119 }
120 
121 // convert from any of the exposed units back to the native unit
122 NS_IMETHODIMP
124  PRUint32 aUnitID,
125  PRFloat64 &_retVal)
126 {
127  switch (aUnitID) {
129  // native unit, nothing to do
130  break;
132  aValue *= 1000.0;
133  break;
135  aValue *= (1000.0*1000.0);
136  break;
138  aValue *= (1000.0*1000.0*60.0);
139  break;
140  case DURATION_UNIT_HOURS:
141  aValue *= (1000.0*1000.0*60.0*60.0);
142  break;
143  case DURATION_UNIT_DAYS:
144  aValue *= (1000.0*1000.0*60*60.0*24.0);
145  break;
146  case DURATION_UNIT_WEEKS:
147  aValue *= (1000.0*1000.0*60.0*60.0*24.0*7.0);
148  break;
150  aValue *= (1000.0*1000.0*60.0*60.0*24.0*30.0);
151  break;
152  case DURATION_UNIT_YEARS:
153  aValue *= (1000.0*1000.0*60.0*60.0*24.0*365.0);
154  break;
155  default:
156  return NS_ERROR_INVALID_ARG;
157  }
158  _retVal = aValue;
159  return NS_OK;
160 }
161 
return NS_OK
NS_IMETHOD ConvertFromNativeToUnit(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
onPageChanged aValue
Definition: FeedWriter.js:1395
NS_IMETHOD ConvertFromUnitToNative(PRFloat64 aValue, PRUint32 aUnitID, PRFloat64 &_retVal)
void RegisterUnit(PRUint32 aUnitInternalID, const nsAString &aUnitExternalID, const nsAString &aUnitName, const nsAString &aUnitShortName, PRBool isNative=PR_FALSE)
void SetStringBundle(const nsAString &aStringBundle)