1 /*
2  * model: A simple and generic data model holding one value per row
3  *
4  * Copyright 2012-2020 Stephan Haller <nomad@froevel.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
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
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  *
21  *
22  */
23 
24 #ifndef __LIBXFDASHBOARD_MODEL__
25 #define __LIBXFDASHBOARD_MODEL__
26 
27 #if !defined(__LIBXFDASHBOARD_H_INSIDE__) && !defined(LIBXFDASHBOARD_COMPILATION)
28 #error "Only <libxfdashboard/libxfdashboard.h> can be included directly."
29 #endif
30 
31 #include <glib-object.h>
32 
33 G_BEGIN_DECLS
34 
35 /* Object declaration: XfdashboardModelIter */
36 #define XFDASHBOARD_TYPE_MODEL_ITER				(xfdashboard_model_iter_get_type())
37 #define XFDASHBOARD_MODEL_ITER(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), XFDASHBOARD_TYPE_MODEL_ITER, XfdashboardModelIter))
38 #define XFDASHBOARD_IS_MODEL_ITER(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj), XFDASHBOARD_TYPE_MODEL_ITER))
39 #define XFDASHBOARD_MODEL_ITER_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), XFDASHBOARD_TYPE_MODEL_ITER, XfdashboardModelIterClass))
40 #define XFDASHBOARD_IS_MODEL_ITER_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), XFDASHBOARD_TYPE_MODEL_ITER))
41 #define XFDASHBOARD_MODEL_ITER_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), XFDASHBOARD_TYPE_MODEL_ITER, XfdashboardModelIterClass))
42 
43 typedef struct _XfdashboardModelIter			XfdashboardModelIter;
44 typedef struct _XfdashboardModelIterClass		XfdashboardModelIterClass;
45 typedef struct _XfdashboardModelIterPrivate		XfdashboardModelIterPrivate;
46 
47 struct _XfdashboardModelIter
48 {
49 	/*< private >*/
50 	/* Parent instance */
51 	GObjectClass					parent_instance;
52 
53 	/* Private structure */
54 	XfdashboardModelIterPrivate		*priv;
55 };
56 
57 struct _XfdashboardModelIterClass
58 {
59 	/*< private >*/
60 	/* Parent class */
61 	GObjectClass					parent_class;
62 
63 	/*< public >*/
64 	/* Virtual functions */
65 };
66 
67 
68 /* Object declaration: XfdashboardModel */
69 #define XFDASHBOARD_TYPE_MODEL				(xfdashboard_model_get_type())
70 #define XFDASHBOARD_MODEL(obj)				(G_TYPE_CHECK_INSTANCE_CAST((obj), XFDASHBOARD_TYPE_MODEL, XfdashboardModel))
71 #define XFDASHBOARD_IS_MODEL(obj)			(G_TYPE_CHECK_INSTANCE_TYPE((obj), XFDASHBOARD_TYPE_MODEL))
72 #define XFDASHBOARD_MODEL_CLASS(klass)		(G_TYPE_CHECK_CLASS_CAST((klass), XFDASHBOARD_TYPE_MODEL, XfdashboardModelClass))
73 #define XFDASHBOARD_IS_MODEL_CLASS(klass)	(G_TYPE_CHECK_CLASS_TYPE((klass), XFDASHBOARD_TYPE_MODEL))
74 #define XFDASHBOARD_MODEL_GET_CLASS(obj)	(G_TYPE_INSTANCE_GET_CLASS((obj), XFDASHBOARD_TYPE_MODEL, XfdashboardModelClass))
75 
76 typedef struct _XfdashboardModel			XfdashboardModel;
77 typedef struct _XfdashboardModelClass		XfdashboardModelClass;
78 typedef struct _XfdashboardModelPrivate		XfdashboardModelPrivate;
79 
80 struct _XfdashboardModel
81 {
82 	/* Parent instance */
83 	GObjectClass					parent_instance;
84 
85 	/* Private structure */
86 	XfdashboardModelPrivate			*priv;
87 };
88 
89 struct _XfdashboardModelClass
90 {
91 	/*< private >*/
92 	/* Parent class */
93 	GObjectClass					parent_class;
94 
95 	/*< public >*/
96 	/* Virtual functions */
97 	void (*row_added)(XfdashboardModel *self,
98 						XfdashboardModelIter *inIter);
99 	void (*row_removed)(XfdashboardModel *self,
100 						XfdashboardModelIter *inIter);
101 	void (*row_changed)(XfdashboardModel *self,
102 						XfdashboardModelIter *inIter);
103 
104 	void (*sort_changed)(XfdashboardModel *self);
105 
106 	void (*filter_changed)(XfdashboardModel *self);
107 };
108 
109 
110 /* Public API */
111 typedef void (*XfdashboardModelForeachFunc)(XfdashboardModelIter *inIter,
112 											gpointer inData,
113 											gpointer inUserData);
114 typedef gint (*XfdashboardModelSortFunc)(XfdashboardModelIter *inLeftIter,
115 											XfdashboardModelIter *inRightIter,
116 											gpointer inUserData);
117 typedef gboolean (*XfdashboardModelFilterFunc)(XfdashboardModelIter *inIter,
118 												gpointer inUserData);
119 
120 GType xfdashboard_model_get_type(void) G_GNUC_CONST;
121 GType xfdashboard_model_iter_get_type(void) G_GNUC_CONST;
122 
123 /* Model creation functions */
124 XfdashboardModel* xfdashboard_model_new(void);
125 XfdashboardModel* xfdashboard_model_new_with_free_data(GDestroyNotify inFreeDataFunc);
126 
127 /* Model information functions */
128 gint xfdashboard_model_get_rows_count(XfdashboardModel *self);
129 
130 /* Model access functions */
131 gpointer xfdashboard_model_get(XfdashboardModel *self, gint inRow);
132 
133 /* Model manipulation functions */
134 gboolean xfdashboard_model_append(XfdashboardModel *self,
135 									gpointer inData,
136 									XfdashboardModelIter **outIter);
137 gboolean xfdashboard_model_prepend(XfdashboardModel *self,
138 									gpointer inData,
139 									XfdashboardModelIter **outIter);
140 gboolean xfdashboard_model_insert(XfdashboardModel *self,
141 									gint inRow,
142 									gpointer inData,
143 									XfdashboardModelIter **outIter);
144 gboolean xfdashboard_model_set(XfdashboardModel *self,
145 								gint inRow,
146 								gpointer inData,
147 								XfdashboardModelIter **outIter);
148 gboolean xfdashboard_model_remove(XfdashboardModel *self, gint inRow);
149 void xfdashboard_model_remove_all(XfdashboardModel *self);
150 
151 /* Model foreach functions */
152 void xfdashboard_model_foreach(XfdashboardModel *self,
153 								XfdashboardModelForeachFunc inForeachCallback,
154 								gpointer inUserData);
155 
156 /* Model sort functions */
157 gboolean xfdashboard_model_is_sorted(XfdashboardModel *self);
158 void xfdashboard_model_set_sort(XfdashboardModel *self,
159 								XfdashboardModelSortFunc inSortCallback,
160 								gpointer inUserData,
161 								GDestroyNotify inUserDataDestroyCallback);
162 void xfdashboard_model_resort(XfdashboardModel *self);
163 
164 /* Model filter functions */
165 gboolean xfdashboard_model_is_filtered(XfdashboardModel *self);
166 void xfdashboard_model_set_filter(XfdashboardModel *self,
167 									XfdashboardModelFilterFunc inFilterCallback,
168 									gpointer inUserData,
169 									GDestroyNotify inUserDataDestroyCallback);
170 gboolean xfdashboard_model_filter_row(XfdashboardModel *self, gint inRow);
171 
172 
173 /* Model iterator functions */
174 XfdashboardModelIter* xfdashboard_model_iter_new(XfdashboardModel *inModel);
175 XfdashboardModelIter* xfdashboard_model_iter_new_for_row(XfdashboardModel *inModel, gint inRow);
176 XfdashboardModelIter* xfdashboard_model_iter_copy(XfdashboardModelIter *self);
177 gboolean xfdashboard_model_iter_next(XfdashboardModelIter *self);
178 gboolean xfdashboard_model_iter_prev(XfdashboardModelIter *self);
179 gboolean xfdashboard_model_iter_move_to_row(XfdashboardModelIter *self, gint inRow);
180 
181 /* Model iterator information functions */
182 XfdashboardModel* xfdashboard_model_iter_get_model(XfdashboardModelIter *self);
183 guint xfdashboard_model_iter_get_row(XfdashboardModelIter *self);
184 
185 /* Model iterator access functions */
186 gpointer xfdashboard_model_iter_get(XfdashboardModelIter *self);
187 
188 /* Model iterator manipulation functions */
189 gboolean xfdashboard_model_iter_set(XfdashboardModelIter *self, gpointer inData);
190 gboolean xfdashboard_model_iter_remove(XfdashboardModelIter *self);
191 
192 /* Model filter functions */
193 gboolean xfdashboard_model_iter_filter(XfdashboardModelIter *self);
194 
195 G_END_DECLS
196 
197 #endif	/* __LIBXFDASHBOARD_MODEL__ */
198