[storage] Add simple db manager
[abstract.git] / view / aaTreeViewBalance.cpp
blob5fefbb934efd858fbb6bf9eaaf9ab93d59f54473
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) 2007 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 "nsServiceManagerUtils.h"
26 #include "nsStringAPI.h"
27 #include "nsTextFormatter.h"
29 /* Unfrozen API */
30 #include "nsIArray.h"
31 #include "nsArrayUtils.h"
32 #include "nsITreeColumns.h"
33 #include "nsIAtom.h"
34 #include "nsIAtomService.h"
36 /* Project includes */
37 #include "aaIEntity.h"
38 #include "aaIFlow.h"
39 #include "aaIResource.h"
40 #include "aaIBalance.h"
41 #include "aaILoadQuery.h"
42 #include "aaViewUtils.h"
43 #include "aaTreeViewBalance.h"
45 aaTreeViewBalance::aaTreeViewBalance()
49 aaTreeViewBalance::~aaTreeViewBalance()
53 NS_IMPL_ISUPPORTS_INHERITED0(aaTreeViewBalance, aaTreeView)
55 /* Helpers */
56 nsresult
57 aaTreeViewBalance::getColumn0(aaIBalance *balance, PRUint32 flags,
58 nsAString & _retval)
60 nsresult rv;
62 nsCOMPtr<aaIFlow> flow;
63 rv = balance->GetFlow(getter_AddRefs( flow ));
64 NS_ENSURE_TRUE(flow, NS_OK);
66 return flow->GetTag(_retval);
69 nsresult
70 aaTreeViewBalance::getColumn1(aaIBalance *balance, PRUint32 flags,
71 nsAString & _retval)
73 nsresult rv;
75 nsCOMPtr<aaIFlow> flow;
76 rv = balance->GetFlow(getter_AddRefs( flow ));
77 NS_ENSURE_TRUE(flow, NS_OK);
79 nsCOMPtr<aaIEntity> entity;
80 rv = flow->GetEntity(getter_AddRefs( entity ));
81 if (!entity)
82 return NS_OK;
84 return entity->GetTag(_retval);
87 nsresult
88 aaTreeViewBalance::getColumn2(aaIBalance *balance, PRUint32 flags,
89 nsAString & _retval)
91 nsresult rv;
93 nsCOMPtr<aaIResource> resource;
94 rv = balance->GetResource(getter_AddRefs( resource ));
95 if (!resource)
96 return NS_OK;
98 return resource->GetTag(_retval);
101 nsresult
102 aaTreeViewBalance::getColumn3(aaIBalance *balance, PRUint32 flags,
103 nsAString & _retval)
105 PRBool side;
106 double value;
107 balance->GetSide(&side);
108 if (! side)
109 return NS_OK;
110 if (flags)
111 balance->GetValue(&value);
112 else
113 balance->GetAmount(&value);
114 assignDouble(_retval, value);
115 return NS_OK;
118 nsresult
119 aaTreeViewBalance::getColumn4(aaIBalance *balance, PRUint32 flags,
120 nsAString & _retval)
122 PRBool side;
123 double value;
124 balance->GetSide(&side);
125 if (side)
126 return NS_OK;
127 if (flags)
128 balance->GetValue(&value);
129 else
130 balance->GetAmount(&value);
131 assignDouble(_retval, value);
132 return NS_OK;
135 nsresult (*const
136 aaTreeViewBalance::colFuncs[])(aaIBalance *, PRUint32 flags,
137 nsAString &) = {
138 &aaTreeViewBalance::getColumn0,
139 &aaTreeViewBalance::getColumn1,
140 &aaTreeViewBalance::getColumn2,
141 &aaTreeViewBalance::getColumn3,
142 &aaTreeViewBalance::getColumn4
145 /* Private functions */
146 nsresult
147 aaTreeViewBalance::doGetCellText(PRInt32 row, nsITreeColumn *col,
148 nsAString & _retval)
150 nsresult rv;
151 _retval.Truncate();
153 PRInt32 ci = getColumnIndex(col, sizeof(colFuncs));
154 NS_ENSURE_TRUE(ci >= 0, NS_OK);
156 nsCOMPtr<aaIBalance> balance(do_QueryElementAt(mDataSet, row, &rv));
157 NS_ENSURE_SUCCESS(rv, rv);
158 NS_ENSURE_TRUE(balance, NS_OK);
160 return colFuncs[ci](balance, mFlags, _retval);