transport: pass correct channel
[abstract.git] / view / aaTreeViewLedger.cpp
blob20db25b3d3ce33efa812782acb9ae9b00375a006
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
3 /*
4 * Copyright (C) 2008 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include "xpcom-config.h"
24 #include "nsCOMPtr.h"
25 #include "nsComponentManagerUtils.h"
26 #include "nsServiceManagerUtils.h"
27 #include "nsStringAPI.h"
28 #include "nsTextFormatter.h"
30 /* Unfrozen API */
31 #include "nsIArray.h"
32 #include "nsArrayUtils.h"
33 #include "nsITreeColumns.h"
34 #include "nsIAtom.h"
35 #include "nsIAtomService.h"
36 #include "nsIStringBundle.h"
38 /* Project includes */
39 #include "aaAmountUtils.h"
40 #include "aaIResource.h"
41 #include "aaIFlow.h"
42 #include "aaIFact.h"
43 #include "aaITransaction.h"
44 #include "aaISqlTransaction.h"
45 #include "aaReportKeys.h"
46 #include "aaViewUtils.h"
47 #include "aaTreeViewLedger.h"
49 aaTreeViewLedger::aaTreeViewLedger()
51 mComplexLoader = do_CreateInstance(AA_GENERALLEDGER_CONTRACT);
54 aaTreeViewLedger::~aaTreeViewLedger()
58 NS_IMPL_ISUPPORTS_INHERITED0(aaTreeViewLedger, aaTreeView)
60 /* Virtual overrides */
61 NS_IMETHODIMP
62 aaTreeViewLedger::GetRowCount(PRInt32 *aRowCount)
64 NS_ENSURE_ARG_POINTER(aRowCount);
65 *aRowCount = 0;
66 if (! mDataSet)
67 return NS_OK;
69 nsresult rv;
70 rv = mDataSet->GetLength((PRUint32 *) aRowCount);
71 NS_ENSURE_SUCCESS(rv, rv);
73 *aRowCount *= 2;
74 return NS_OK;
77 /* Helpers */
78 nsresult
79 aaTreeViewLedger::getColumn0(aaITransaction *txn, PRUint32 flags,
80 nsAString & _retval)
82 if (flags)
83 return NS_OK;
85 assignDate(_retval, txn->PickFact()->PickTime());
86 return NS_OK;
89 nsresult
90 aaTreeViewLedger::getColumn1(aaITransaction *txn, PRUint32 flags,
91 nsAString & _retval)
93 if (flags)
94 return NS_OK;
96 nsCOMPtr<aaIResource> resource;
97 aaIFlow *flow = txn->PickFact()->PickTakeFrom();
98 if (flow && flow->PickId()) {
99 txn->PickFact()->PickTakeFrom()->GetGiveResource(
100 getter_AddRefs(resource));
101 } else {
102 txn->PickFact()->PickGiveTo()->GetTakeResource(
103 getter_AddRefs(resource));
106 if (resource)
107 return resource->GetTag(_retval);
109 return NS_OK;
112 nsresult
113 aaTreeViewLedger::getColumn2(aaITransaction *txn, PRUint32 flags,
114 nsAString & _retval)
116 if (flags)
117 return NS_OK;
119 assignDouble(_retval, txn->PickFact()->PickAmount());
120 return NS_OK;
123 nsresult
124 aaTreeViewLedger::getColumn3(aaITransaction *txn, PRUint32 flags,
125 nsAString & _retval)
127 nsresult rv;
128 nsCOMPtr<nsIStringBundleService> heap = do_GetService(
129 NS_STRINGBUNDLE_CONTRACTID, &rv);
130 NS_ENSURE_SUCCESS(rv, rv);
132 nsCOMPtr<nsIStringBundle> bundle;
133 rv = heap->CreateBundle("chrome://abstract/locale/abstract.properties",
134 getter_AddRefs( bundle ));
135 NS_ENSURE_SUCCESS(rv, rv);
137 PRUnichar *w;
138 if (flags)
139 rv = bundle->GetStringFromName(NS_LITERAL_STRING("terms.debit").get(),
140 &w);
141 else
142 rv = bundle->GetStringFromName(NS_LITERAL_STRING("terms.credit").get(),
143 &w);
145 _retval.Assign(w);
146 return NS_OK;
149 nsresult
150 aaTreeViewLedger::getColumn4(aaITransaction *txn, PRUint32 flags,
151 nsAString & _retval)
153 if (!flags) {
154 if (txn->PickFact()->PickTakeFrom())
155 txn->PickFact()->PickTakeFrom()->GetTag(_retval);
156 } else {
157 if (txn->PickFact()->PickGiveTo())
158 txn->PickFact()->PickGiveTo()->GetTag(_retval);
160 return NS_OK;
163 nsresult
164 aaTreeViewLedger::getColumn5(aaITransaction *txn, PRUint32 flags,
165 nsAString & _retval)
167 if (isZero(txn->PickFact()->PickAmount()))
168 return NS_OK;
170 if (flags) {
171 assignDouble(_retval, (txn->PickValue() + txn->PickEarnings())
172 / txn->PickFact()->PickAmount());
173 } else {
174 assignDouble(_retval, (txn->PickValue())
175 / txn->PickFact()->PickAmount());
177 return NS_OK;
180 nsresult
181 aaTreeViewLedger::getColumn6(aaITransaction *txn, PRUint32 flags,
182 nsAString & _retval)
184 if (!flags)
185 return NS_OK;
187 assignDouble(_retval, txn->PickValue() + txn->PickEarnings());
188 return NS_OK;
191 nsresult
192 aaTreeViewLedger::getColumn7(aaITransaction *txn, PRUint32 flags,
193 nsAString & _retval)
195 if (flags)
196 return NS_OK;
198 assignDouble(_retval, txn->PickValue());
199 return NS_OK;
202 nsresult (*const
203 aaTreeViewLedger::colFuncs[])(aaITransaction *, PRUint32 flags,
204 nsAString &) = {
205 &aaTreeViewLedger::getColumn0,
206 &aaTreeViewLedger::getColumn1,
207 &aaTreeViewLedger::getColumn2,
208 &aaTreeViewLedger::getColumn3,
209 &aaTreeViewLedger::getColumn4,
210 &aaTreeViewLedger::getColumn5,
211 &aaTreeViewLedger::getColumn6,
212 &aaTreeViewLedger::getColumn7
215 /* Private functions */
216 nsresult
217 aaTreeViewLedger::doGetCellText(PRInt32 row, nsITreeColumn *col,
218 nsAString & _retval)
220 nsresult rv;
221 _retval.Truncate();
223 PRInt32 ci = getColumnIndex(col, sizeof(colFuncs));
224 NS_ENSURE_TRUE(ci >= 0, NS_OK);
226 nsCOMPtr<aaITransaction> transaction(do_QueryElementAt(mDataSet,
227 getIndexFromRow(row), &rv));
228 NS_ENSURE_SUCCESS(rv, rv);
229 NS_ENSURE_TRUE(transaction, NS_OK);
231 return colFuncs[ci](transaction, getOffsetFromRow(row), _retval);