|
| 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 "nip4.h" |
| 35 | + |
| 36 | +G_DEFINE_TYPE(Group, group, VALUE_TYPE) |
| 37 | + |
| 38 | +static gboolean group_save_list(PElement *list, char *filename); |
| 39 | + |
| 40 | +/* Exported, since main.c uses this to save 'main' to a file. @filename is |
| 41 | + * incremented. |
| 42 | + */ |
| 43 | +gboolean |
| 44 | +group_save_item(PElement *item, char *filename) |
| 45 | +{ |
| 46 | + gboolean result; |
| 47 | + Imageinfo *ii; |
| 48 | + char buf[FILENAME_MAX]; |
| 49 | + |
| 50 | + /* We don't want $VAR etc. in the filename we pass down to the file |
| 51 | + * ops. |
| 52 | + */ |
| 53 | + g_strlcpy(buf, filename, FILENAME_MAX); |
| 54 | + path_expand(buf); |
| 55 | + |
| 56 | + if (!heap_is_instanceof(CLASS_GROUP, item, &result)) |
| 57 | + return FALSE; |
| 58 | + if (result) { |
| 59 | + PElement value; |
| 60 | + |
| 61 | + if (!class_get_member(item, MEMBER_VALUE, NULL, &value) || |
| 62 | + !group_save_list(&value, filename)) |
| 63 | + return FALSE; |
| 64 | + } |
| 65 | + |
| 66 | + if (!heap_is_instanceof(CLASS_IMAGE, item, &result)) |
| 67 | + return FALSE; |
| 68 | + if (result) { |
| 69 | + PElement value; |
| 70 | + |
| 71 | + if (!class_get_member(item, MEMBER_VALUE, NULL, &value) || |
| 72 | + !heap_get_image(&value, &ii)) |
| 73 | + return FALSE; |
| 74 | + if (vips_image_write_to_file(ii->image, buf, NULL)) { |
| 75 | + error_vips_all(); |
| 76 | + return FALSE; |
| 77 | + } |
| 78 | + |
| 79 | + increment_filename(filename); |
| 80 | + } |
| 81 | + |
| 82 | + if (PEISIMAGE(item)) { |
| 83 | + if (!heap_get_image(item, &ii)) |
| 84 | + return FALSE; |
| 85 | + if (vips_image_write_to_file(ii->image, buf, NULL)) { |
| 86 | + error_vips_all(); |
| 87 | + return FALSE; |
| 88 | + } |
| 89 | + |
| 90 | + increment_filename(filename); |
| 91 | + } |
| 92 | + |
| 93 | + if (PEISLIST(item)) { |
| 94 | + if (!group_save_list(item, filename)) |
| 95 | + return FALSE; |
| 96 | + } |
| 97 | + |
| 98 | + return TRUE; |
| 99 | +} |
| 100 | + |
| 101 | +static gboolean |
| 102 | +group_save_list(PElement *list, char *filename) |
| 103 | +{ |
| 104 | + int i; |
| 105 | + int length; |
| 106 | + |
| 107 | + if ((length = heap_list_length(list)) < 0) |
| 108 | + return FALSE; |
| 109 | + |
| 110 | + for(i = 0; i < length; i++) { |
| 111 | + PElement item; |
| 112 | + |
| 113 | + if (!heap_list_index(list, i, &item) || |
| 114 | + !group_save_item(&item, filename)) |
| 115 | + return FALSE; |
| 116 | + } |
| 117 | + |
| 118 | + return TRUE; |
| 119 | +} |
| 120 | + |
| 121 | +static gboolean |
| 122 | +group_graphic_save(Classmodel *classmodel, |
| 123 | + GtkWindow *parent, const char *filename) |
| 124 | +{ |
| 125 | + Group *group = GROUP(classmodel); |
| 126 | + Row *row = HEAPMODEL(group)->row; |
| 127 | + PElement *root = &row->expr->root; |
| 128 | + char buf[FILENAME_MAX]; |
| 129 | + |
| 130 | + /* We are going to increment the filename ... make sure there's some |
| 131 | + * space at the end of the string. |
| 132 | + */ |
| 133 | + g_strlcpy(buf, filename, FILENAME_MAX - 5); |
| 134 | + |
| 135 | + if (!group_save_item(root, buf)) |
| 136 | + return FALSE; |
| 137 | + |
| 138 | + return TRUE; |
| 139 | +} |
| 140 | + |
| 141 | +static void |
| 142 | +group_class_init(GroupClass *class) |
| 143 | +{ |
| 144 | + ClassmodelClass *classmodel_class = (ClassmodelClass *) class; |
| 145 | + |
| 146 | + /* Create signals. |
| 147 | + */ |
| 148 | + |
| 149 | + classmodel_class->graphic_save = group_graphic_save; |
| 150 | + |
| 151 | + model_register_loadable(MODEL_CLASS(class)); |
| 152 | +} |
| 153 | + |
| 154 | +static void |
| 155 | +group_init(Group *group) |
| 156 | +{ |
| 157 | + iobject_set(IOBJECT(group), CLASS_GROUP, NULL); |
| 158 | +} |
| 159 | + |
0 commit comments