File: transaction_list_sort.c

package info (click to toggle)
grisbi 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 22,604 kB
  • ctags: 6,257
  • sloc: ansic: 117,322; sh: 11,246; makefile: 785; perl: 370; xml: 11
file content (260 lines) | stat: -rw-r--r-- 8,134 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* ************************************************************************** */
/*                                                                            */
/*     Copyright (C)    2000-2008 Cédric Auger (cedric@grisbi.org)            */
/*          http://www.grisbi.org                                             */
/*                                                                            */
/*  This program is free software; you can redistribute it and/or modify      */
/*  it under the terms of the GNU General Public License as published by      */
/*  the Free Software Foundation; either version 2 of the License, or         */
/*  (at your option) any later version.                                       */
/*                                                                            */
/*  This program is distributed in the hope that it will be useful,           */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/*  GNU General Public License for more details.                              */
/*                                                                            */
/*  You should have received a copy of the GNU General Public License         */
/*  along with this program; if not, write to the Free Software               */
/*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
/*                                                                            */
/* ************************************************************************** */



/**
 * \file transaction_list_sort.c
 * this contains the sorting functions to use with the CustomList
 * Grisbi shouldn't work directly on the CustomList except by those files
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "include.h"
#include <glib/gi18n.h>

/*START_INCLUDE*/
#include "transaction_list_sort.h"
#include "custom_list.h"
#include "gsb_data_account.h"
#include "gsb_data_transaction.h"
#include "gsb_reconcile_list.h"
#include "gsb_transactions_list.h"
#include "gsb_transactions_list_sort.h"
#include "navigation.h"
#include "transaction_model.h"
#include "utils_dates.h"
#include "erreur.h"
/*END_INCLUDE*/

/*START_STATIC*/
static gboolean transaction_list_sort_get_initial_sort ( void );
/*END_STATIC*/

/*START_EXTERN*/
extern GtkTreeViewColumn *transactions_tree_view_columns[CUSTOM_MODEL_VISIBLE_COLUMNS];
/*END_EXTERN*/



/**
 * Sort the list according to the current column and sort type
 * sort the filtered model because we don't need to sort not showed columns
 * so filter the model before calling this function
 *
 * for now, only sort mother transactions, if need the children too,
 * need to go into each record structure and sort the children.
 * could be time consuming...
 *
 * to update all the tree view, use gsb_transactions_list_update_tree_view instead
 *
 * \param
 *
 * \return
 * */
void transaction_list_sort (void)
{
    GtkTreePath *path;
    gint        *neworder, i;
    CustomList *custom_list;

    devel_debug (NULL);
    custom_list = transaction_model_get_model ();
    g_return_if_fail ( custom_list != NULL );

    /* resort */
    if (custom_list -> user_sort_reconcile)
        g_qsort_with_data(custom_list->visibles_rows,
                        custom_list->num_visibles_rows,
                        sizeof(CustomRecord*),
                        (GCompareDataFunc) gsb_reconcile_list_sort_func,
                        custom_list);
    else if ( transaction_list_sort_get_initial_sort ( ) && custom_list -> sort_order == GTK_SORT_ASCENDING )
        return;
    else
        g_qsort_with_data(custom_list->visibles_rows,
                        custom_list->num_visibles_rows,
                        sizeof(CustomRecord*),
                        (GCompareDataFunc) gsb_transactions_list_sort,
                        custom_list);

    /* let other objects know about the new order */
    neworder = g_new0(gint, custom_list->num_visibles_rows);

    for (i = 0; i < custom_list->num_visibles_rows; ++i)
    {
        neworder[i] = (custom_list->visibles_rows[i])->filtered_pos;
        (custom_list->visibles_rows[i])->filtered_pos = i;
    }

    path = gtk_tree_path_new();

    gtk_tree_model_rows_reordered(GTK_TREE_MODEL(custom_list), path, NULL, neworder);

    gtk_tree_path_free(path);
    g_free(neworder);
}



/**
 * change the sort column and/or the sort order
 * do nothing to the tree view, use gsb_transactions_list_update_tree_view to update it
 * 	or transaction_list_sort
 *
 * \param custom_list the CustomList
 * \param new_sort_col		column number to use to sort the list
 * \param new_sort_order	GTK_SORT_DESCENDING / GTK_SORT_ASCENDING
 *
 * \return
 * */
void transaction_list_sort_set_column ( gint new_sort_col,
                        GtkSortType new_sort_order )
{
    CustomList *custom_list;

    custom_list = transaction_model_get_model ();

    devel_debug_int (new_sort_col);

    g_return_if_fail ( custom_list != NULL );

    if (new_sort_col >= CUSTOM_MODEL_VISIBLE_COLUMNS)
    {
	g_warning (_("Asked to sort by column %d, wich is bigger than the visible columns (%d)"),
		   new_sort_col,
		   CUSTOM_MODEL_VISIBLE_COLUMNS );
	return;
    }

    /* update the indicator */
    gtk_tree_view_column_set_sort_indicator (transactions_tree_view_columns[custom_list -> sort_col],
					     FALSE);
    gtk_tree_view_column_set_sort_indicator (transactions_tree_view_columns[new_sort_col],
					     TRUE);
    gtk_tree_view_column_set_sort_order (transactions_tree_view_columns[new_sort_col],
					 new_sort_order );

    /* save the new values */
    custom_list -> sort_col = new_sort_col;
    custom_list -> sort_order = new_sort_order;
}



/**
 * change the sort column and/or the sort order
 *
 * \param custom_list the CustomList
 * \param sort_col	column number to use to sort the list
 * \param sort_order	GTK_SORT_DESCENDING / GTK_SORT_ASCENDING
 *
 * \return
 * */
void transaction_list_sort_get_column ( gint *sort_col,
                        GtkSortType *sort_order )
{
    CustomList *custom_list;

    custom_list = transaction_model_get_model ();

    g_return_if_fail ( custom_list != NULL );

    if (sort_col)
	*sort_col = custom_list -> sort_col;
    if (sort_order)
	*sort_order = custom_list -> sort_order;
}



/**
 * set if the sorting function is normal (by columns) or for reconciliation
 *
 * \param use_reconcile_sort	TRUE to sort for reconciliation, FALSE for column sort
 *
 * \return
 * */
void transaction_list_sort_set_reconcile_sort ( gboolean use_reconcile_sort )
{
    CustomList *custom_list;

    custom_list = transaction_model_get_model ();

    devel_debug_int (use_reconcile_sort);

    g_return_if_fail ( custom_list != NULL );

    custom_list -> user_sort_reconcile = use_reconcile_sort;

    /* update the indicator */
    gtk_tree_view_column_set_sort_indicator (transactions_tree_view_columns[custom_list -> sort_col],
					     !use_reconcile_sort );
}



/**
 * get if the sorting function is normal (by columns) or for reconciliation
 *
 * \param
 *
 * \return TRUE if the list is sorted for reconciliation, FALSE if column sort
 * */
gboolean transaction_list_sort_get_reconcile_sort ( void )
{
    CustomList *custom_list;

    custom_list = transaction_model_get_model ();

    g_return_val_if_fail ( custom_list != NULL, FALSE );

    return custom_list -> user_sort_reconcile;
}


/**
 * get if the sorting function is initial (by date or value_date) or not
 *
 * \param
 *
 * \return TRUE if the list is sorted by date or value_date, FALSE if not
 * */
gboolean transaction_list_sort_get_initial_sort ( void )
{
    gint account_number;
    gint element_number;

    account_number = gsb_gui_navigation_get_current_account ();
    if (account_number == -1)
        return FALSE;

    element_number = gsb_data_account_get_element_sort ( account_number,
                        gsb_data_account_get_sort_column ( account_number ) );

    if ( element_number == ELEMENT_VALUE_DATE )
        return TRUE;
    else
        return FALSE;
}