1 /**
2  * @brief GncTreeView implementation for Scheduled Transaction List.
3  * @author Copyright (C) 2007 Joshua Sled <jsled@asynchronous.org>
4  **/
5 /********************************************************************
6  * This program is free software; you can redistribute it and/or    *
7  * modify it under the terms of version 2 and/or version 3 of the GNU General Public *
8  * License as published by the Free Software Foundation.            *
9  *
10  * As a special exception, permission is granted to link the binary
11  * module resultant from this code with the OpenSSL project's
12  * "OpenSSL" library (or modified versions of it that use the same
13  * license as the "OpenSSL" library), and distribute the linked
14  * executable.  You must obey the GNU General Public License in all
15  * respects for all of the code used other than "OpenSSL". If you
16  * modify this file, you may extend this exception to your version
17  * of the file, but you are not obligated to do so. If you do not
18  * wish to do so, delete this exception statement from your version
19  * of this file.
20  *                                                                  *
21  * This program is distributed in the hope that it will be useful,  *
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
24  * GNU General Public License for more details.                     *
25  *                                                                  *
26  * You should have received a copy of the GNU General Public License*
27  * along with this program; if not, contact:                        *
28  *                                                                  *
29  * Free Software Foundation           Voice:  +1-617-542-5942       *
30  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
31  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
32  *                                                                  *
33  *******************************************************************/
34 
35 #include <config.h>
36 
37 #include <gtk/gtk.h>
38 #include <glib/gi18n.h>
39 #include <string.h>
40 
41 #include "gnc-tree-view.h"
42 #include "gnc-tree-view-sx-list.h"
43 #include "gnc-sx-list-tree-model-adapter.h"
44 
45 #define LOG_MOD "gnc.ui.tree-view.sx-list"
46 static QofLogModule log_module = LOG_MOD;
47 #undef G_LOG_DOMAIN
48 #define G_LOG_DOMAIN LOG_MOD
49 
50 static void gnc_tree_view_sx_list_class_init(GncTreeViewSxListClass *klass);
51 static void gnc_tree_view_sx_list_init(GncTreeViewSxList *view);
52 static void gnc_tree_view_sx_list_dispose(GObject *object);
53 static void gnc_tree_view_sx_list_finalize(GObject *object);
54 
55 typedef struct GncTreeViewSxListPrivate
56 {
57     GtkTreeModel *tree_model;
58     gboolean disposed;
59 } GncTreeViewSxListPrivate;
60 
61 #define GNC_TREE_VIEW_SX_LIST_GET_PRIVATE(o)  \
62    ((GncTreeViewSxListPrivate*)g_type_instance_get_private((GTypeInstance*)o, GNC_TYPE_TREE_VIEW_SX_LIST))
63 
64 static GObjectClass *parent_class = NULL;
65 
G_DEFINE_TYPE_WITH_PRIVATE(GncTreeViewSxList,gnc_tree_view_sx_list,GNC_TYPE_TREE_VIEW)66 G_DEFINE_TYPE_WITH_PRIVATE(GncTreeViewSxList, gnc_tree_view_sx_list, GNC_TYPE_TREE_VIEW)
67 
68 static void
69 gnc_tree_view_sx_list_class_init(GncTreeViewSxListClass *klass)
70 {
71     GObjectClass *o_class;
72 
73     parent_class = g_type_class_peek_parent (klass);
74 
75     o_class = G_OBJECT_CLASS (klass);
76 
77     o_class->dispose =  gnc_tree_view_sx_list_dispose;
78     o_class->finalize = gnc_tree_view_sx_list_finalize;
79 }
80 
81 static void
gnc_tree_view_sx_list_init(GncTreeViewSxList * view)82 gnc_tree_view_sx_list_init (GncTreeViewSxList *view)
83 {
84     ; /* nop */
85 }
86 
87 static void
gnc_tree_view_sx_list_dispose(GObject * object)88 gnc_tree_view_sx_list_dispose(GObject *object)
89 {
90     GncTreeViewSxList *view;
91     GncTreeViewSxListPrivate *priv;
92 
93     gnc_leave_return_if_fail (object != NULL);
94     gnc_leave_return_if_fail (GNC_IS_TREE_VIEW_SX_LIST (object));
95 
96     view = GNC_TREE_VIEW_SX_LIST (object);
97     priv = GNC_TREE_VIEW_SX_LIST_GET_PRIVATE(view);
98 
99     if (priv->disposed)
100         return;
101     priv->disposed = TRUE;
102 
103     g_object_unref(G_OBJECT(priv->tree_model));
104     priv->tree_model = NULL;
105 
106     if (G_OBJECT_CLASS (parent_class)->dispose)
107         (* G_OBJECT_CLASS (parent_class)->dispose) (object);
108 }
109 
110 static void
gnc_tree_view_sx_list_finalize(GObject * object)111 gnc_tree_view_sx_list_finalize(GObject *object)
112 {
113     gnc_leave_return_if_fail(object != NULL);
114     gnc_leave_return_if_fail(GNC_IS_TREE_VIEW_SX_LIST (object));
115 
116     if (G_OBJECT_CLASS(parent_class)->finalize)
117         (* G_OBJECT_CLASS(parent_class)->finalize) (object);
118 }
119 
120 GtkTreeView*
gnc_tree_view_sx_list_new(GncSxInstanceModel * sx_instances)121 gnc_tree_view_sx_list_new(GncSxInstanceModel *sx_instances)
122 {
123     GncTreeView *view;
124     GtkTreeViewColumn *col;
125     GncTreeViewSxListPrivate *priv;
126 
127     view = (GncTreeView*)g_object_new(GNC_TYPE_TREE_VIEW_SX_LIST, NULL);
128     g_object_set(view, "name", "gnc-id-sx-list-tree", NULL);
129 
130     priv = GNC_TREE_VIEW_SX_LIST_GET_PRIVATE(view);
131 
132     priv->tree_model = GTK_TREE_MODEL(gnc_sx_list_tree_model_adapter_new(sx_instances));
133     gtk_tree_view_set_model (GTK_TREE_VIEW (view), GTK_TREE_MODEL(priv->tree_model));
134 
135     col = gnc_tree_view_add_text_column(view, _("Name"), "name", NULL,
136                                         "Semi-Monthly Paycheck",
137                                         SXLTMA_COL_NAME, -1, NULL);
138     g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
139 
140     col = gnc_tree_view_add_toggle_column(view, _("Enabled"),
141                                           C_("Single-character short column-title form of 'Enabled'", "E"),
142                                           "enabled", SXLTMA_COL_ENABLED,
143                                           GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
144                                           NULL, NULL);
145     g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
146 
147     col = gnc_tree_view_add_text_column(view, _("Frequency"), "frequency", NULL,
148                                         "Weekly (x3): -------",
149                                         SXLTMA_COL_FREQUENCY, -1, NULL);
150     g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
151 
152     col = gnc_tree_view_add_text_column(view, _("Last Occur"), "last-occur", NULL,
153                                         "2007-01-02",
154                                         SXLTMA_COL_LAST_OCCUR, -1, NULL);
155     g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
156 
157     col = gnc_tree_view_add_text_column(view, _("Next Occur"), "next-occur", NULL,
158                                         "2007-01-02",
159                                         SXLTMA_COL_NEXT_OCCUR, -1, NULL);
160     g_object_set_data(G_OBJECT(col), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
161 
162     gnc_tree_view_configure_columns(view);
163 
164     gtk_widget_show(GTK_WIDGET(view));
165     return GTK_TREE_VIEW(view);
166 }
167 
168 SchedXaction*
gnc_tree_view_sx_list_get_sx_from_path(GncTreeViewSxList * view,GtkTreePath * path)169 gnc_tree_view_sx_list_get_sx_from_path(GncTreeViewSxList *view, GtkTreePath *path)
170 {
171     GtkTreeIter iter;
172     GncTreeViewSxListPrivate *priv = GNC_TREE_VIEW_SX_LIST_GET_PRIVATE(view);
173     gtk_tree_model_get_iter(GTK_TREE_MODEL(priv->tree_model), &iter, path);
174     return gnc_sx_list_tree_model_adapter_get_sx_instances(
175                GNC_SX_LIST_TREE_MODEL_ADAPTER(priv->tree_model), &iter)->sx;
176 }
177