1 /* an input group ... put/get methods
2  */
3 
4 /*
5 
6     Copyright (C) 1991-2003 The National Gallery
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License along
19     with this program; if not, write to the Free Software Foundation, Inc.,
20     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21 
22  */
23 
24 /*
25 
26     These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
27 
28  */
29 
30 /*
31 #define DEBUG
32  */
33 
34 #include "ip.h"
35 
36 static ValueClass *parent_class = NULL;
37 
38 static gboolean
39 group_save_list( PElement *list, char *filename );
40 
41 /* Exported, since main.c uses this to save 'main' to a file. @filename is
42  * incremented.
43  */
44 gboolean
group_save_item(PElement * item,char * filename)45 group_save_item( PElement *item, char *filename )
46 {
47 	gboolean result;
48 	Imageinfo *ii;
49 	char buf[FILENAME_MAX];
50 
51 	/* We don't want $VAR etc. in the filename we pass down to the file
52 	 * ops.
53 	 */
54 	im_strncpy( buf, filename, FILENAME_MAX );
55 	path_expand( buf );
56 
57 	if( !heap_is_instanceof( CLASS_GROUP, item, &result ) )
58 		return( FALSE );
59 	if( result ) {
60 		PElement value;
61 
62 		if( !class_get_member( item, MEMBER_VALUE, NULL, &value ) ||
63 			!group_save_list( &value, filename ) )
64 			return( FALSE );
65 	}
66 
67 	if( !heap_is_instanceof( CLASS_IMAGE, item, &result ) )
68 		return( FALSE );
69 	if( result ) {
70 		PElement value;
71 
72 		filesel_add_mode( buf );
73 
74 		if( !class_get_member( item, MEMBER_VALUE, NULL, &value ) ||
75 			!heap_get_image( &value, &ii ) ||
76 			!imageinfo_write( ii, buf ) )
77 			return( FALSE );
78 
79 		increment_filename( filename );
80 	}
81 
82 	if( !heap_is_instanceof( CLASS_MATRIX, item, &result ) )
83 		return( FALSE );
84 	if( result ) {
85 		DOUBLEMASK *dmask;
86 
87 		if( !(dmask = matrix_ip_to_dmask( item )) )
88 			return( FALSE );
89 		if( im_write_dmask_name( dmask, buf ) ) {
90 			error_vips_all();
91 			IM_FREEF( im_free_dmask, dmask );
92 
93 			return( FALSE );
94 		}
95 		IM_FREEF( im_free_dmask, dmask );
96 
97 		increment_filename( filename );
98 	}
99 
100 	if( PEISIMAGE( item ) ) {
101 		filesel_add_mode( buf );
102 
103 		if( !heap_get_image( item, &ii ) ||
104 			!imageinfo_write( ii, buf ) )
105 			return( FALSE );
106 
107 		increment_filename( filename );
108 	}
109 
110 	if( PEISLIST( item ) ) {
111 		if( !group_save_list( item, filename ) )
112 			return( FALSE );
113 	}
114 
115 	return( TRUE );
116 }
117 
118 static gboolean
group_save_list(PElement * list,char * filename)119 group_save_list( PElement *list, char *filename )
120 {
121 	int i;
122 	int length;
123 
124 	if( (length = heap_list_length( list )) < 0 )
125 		return( FALSE );
126 
127 	for( i = 0; i < length; i++ ) {
128 		PElement item;
129 
130 		if( !heap_list_index( list, i, &item ) ||
131 			!group_save_item( &item, filename ) )
132 			return( FALSE );
133 	}
134 
135 	return( TRUE );
136 }
137 
138 static gboolean
group_graphic_save(Classmodel * classmodel,GtkWidget * parent,const char * filename)139 group_graphic_save( Classmodel *classmodel,
140 	GtkWidget *parent, const char *filename )
141 {
142 	Group *group = GROUP( classmodel );
143 	Row *row = HEAPMODEL( group )->row;
144 	PElement *root = &row->expr->root;
145 	char buf[FILENAME_MAX];
146 
147 	/* We are going to increment the filename ... make sure there's some
148 	 * space at the end of the string.
149 	 */
150 	im_strncpy( buf, filename, FILENAME_MAX - 5 );
151 
152 	if( !group_save_item( root, buf ) )
153 		return( FALSE );
154 
155 	return( TRUE );
156 }
157 
158 static void
group_class_init(GroupClass * class)159 group_class_init( GroupClass *class )
160 {
161 	ClassmodelClass *classmodel_class = (ClassmodelClass *) class;
162 
163 	parent_class = g_type_class_peek_parent( class );
164 
165 	/* Create signals.
166 	 */
167 
168 	classmodel_class->graphic_save = group_graphic_save;
169 
170 	model_register_loadable( MODEL_CLASS( class ) );
171 }
172 
173 static void
group_init(Group * group)174 group_init( Group *group )
175 {
176 	iobject_set( IOBJECT( group ), CLASS_GROUP, NULL );
177 }
178 
179 GType
group_get_type(void)180 group_get_type( void )
181 {
182 	static GType type = 0;
183 
184 	if( !type ) {
185 		static const GTypeInfo info = {
186 			sizeof( GroupClass ),
187 			NULL,           /* base_init */
188 			NULL,           /* base_finalize */
189 			(GClassInitFunc) group_class_init,
190 			NULL,           /* class_finalize */
191 			NULL,           /* class_data */
192 			sizeof( Group ),
193 			32,             /* n_preallocs */
194 			(GInstanceInitFunc) group_init,
195 		};
196 
197 		type = g_type_register_static( TYPE_VALUE,
198 			"Group", &info, 0 );
199 	}
200 
201 	return( type );
202 }
203