2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16 * See the COPYING file for license information.
18 * Guillaume Chazarain <guichaz@yahoo.fr>
26 * +--------------------------------+
27 * | The .gliv format specification |
28 * +--------------------------------+
30 * Newlines are here just for clarity. All numbers are represented in a string.
31 * So 123 is actually represented: '1''2''3''\0'
32 * PixelData is a char* so it should be endian independant, testing will tell...
36 * GlivCollectionFile ::=
45 * "1" (Currently there is only one version)
74 * Path, and HashKey are '0' terminated strings.
75 * FileCount, PathLength, HashKeyLength, GdkColorspace, AlphaChannel,
76 * BitsPerSample, ThumbWidth, ThumbHeight, RowStride and PixelDataLength
77 * are '\0' terminated strings representing decimal numbers.
80 #include <stdio.h> /* FILE, fopen(), fread(), fwrite(), fclose() */
81 #include <string.h> /* strlen(), strrchr() */
82 #include <errno.h> /* errno */
83 #include <ctype.h> /* isdigit() */
84 #include <time.h> /* time_t, time() */
87 #include "collection.h"
90 #include "thumbnails.h"
91 #include "str_utils.h"
92 #include "files_list.h"
93 #include "next_image.h"
94 #include "gliv_image.h"
97 #include "decompression.h"
98 #include "callbacks.h"
99 #include "rendering.h"
102 /* Max filename length in the progress window. */
103 #define MAX_DISPLAYED_NAME_LENGTH 30
105 extern gliv_image
*current_image
;
107 /* Let the user choose a filename using a file selection dialog. */
108 static const gchar
*get_a_file(const gchar
* title
)
110 GtkFileSelection
*dialog
;
112 const gchar
*filename
;
114 dialog
= GTK_FILE_SELECTION(gtk_file_selection_new(title
));
116 gtk_file_selection_set_select_multiple(dialog
, TRUE
);
117 response
= gtk_dialog_run(GTK_DIALOG(dialog
));
119 if (response
== GTK_RESPONSE_OK
)
120 filename
= gtk_file_selection_get_filename(dialog
);
124 gtk_widget_destroy(GTK_WIDGET(dialog
));
128 /*** Progress dialog ***/
130 static GtkLabel
*file_label
;
131 static GtkLabel
*ratio_label
;
132 static GtkProgressBar
*progress
;
133 static GtkWindow
*progress_window
;
134 static GtkLabel
*elapsed_label
, *total_label
, *remaining_label
;
135 static time_t start_time
;
137 static gboolean
set_true(gboolean
* ptr
)
143 static GtkLabel
*add_time_display(GtkTable
* table
, const gchar
* text
,
146 GtkLabel
*name_label
, *time_label
;
148 name_label
= GTK_LABEL(gtk_label_new(text
));
149 gtk_widget_show(GTK_WIDGET(name_label
));
151 time_label
= GTK_LABEL(gtk_label_new(NULL
));
152 gtk_widget_show(GTK_WIDGET(time_label
));
154 gtk_table_attach_defaults(table
, GTK_WIDGET(name_label
),
157 gtk_table_attach_defaults(table
, GTK_WIDGET(time_label
),
163 static void create_progress_dialog(const gchar
* filename
, gboolean serialize
,
170 file_label
= GTK_LABEL(gtk_label_new(NULL
));
171 gtk_widget_show(GTK_WIDGET(file_label
));
173 ratio_label
= GTK_LABEL(gtk_label_new(NULL
));
174 gtk_widget_show(GTK_WIDGET(ratio_label
));
177 progress
= GTK_PROGRESS_BAR(gtk_progress_bar_new());
178 gtk_widget_show(GTK_WIDGET(progress
));
180 cancel
= GTK_BUTTON(gtk_button_new_from_stock(GTK_STOCK_CANCEL
));
183 g_signal_connect(cancel
, "clicked",
184 G_CALLBACK(cancel_using_tree
), data
);
186 g_signal_connect_swapped(cancel
, "clicked", G_CALLBACK(set_true
), data
);
188 gtk_widget_show(GTK_WIDGET(cancel
));
190 table
= GTK_TABLE(gtk_table_new(7, 2, TRUE
));
191 gtk_table_attach_defaults(table
, GTK_WIDGET(file_label
), 0, 2, 0, 1);
192 gtk_table_attach_defaults(table
, GTK_WIDGET(ratio_label
), 0, 2, 1, 2);
193 gtk_table_attach_defaults(table
, GTK_WIDGET(progress
), 0, 2, 2, 3);
195 elapsed_label
= add_time_display(table
, _("Elapsed time"), 3);
196 remaining_label
= add_time_display(table
, _("Remaining time"), 4);
197 total_label
= add_time_display(table
, _("Total time"), 5);
199 gtk_table_attach_defaults(table
, GTK_WIDGET(cancel
), 1, 2, 6, 7);
201 gtk_widget_show(GTK_WIDGET(table
));
203 progress_window
= GTK_WINDOW(gtk_window_new(GTK_WINDOW_TOPLEVEL
));
206 g_signal_connect(progress_window
, "delete-event",
207 G_CALLBACK(cancel_using_tree
), NULL
);
209 g_signal_connect_swapped(progress_window
, "delete-event",
210 G_CALLBACK(set_true
), data
);
213 title
= g_strdup_printf(_("Saving collection: %s"),
214 filename_to_utf8(filename
));
215 gtk_window_set_title(progress_window
, title
);
218 gtk_window_set_title(progress_window
, _("Loading GLiv collection"));
220 gtk_container_add(GTK_CONTAINER(progress_window
), GTK_WIDGET(table
));
221 gtk_widget_show(GTK_WIDGET(progress_window
));
222 start_time
= time(NULL
);
225 static gchar
*time2str(time_t t
)
227 gint hours
= t
/ 3600;
228 gint minutes
= (t
- hours
* 3600) / 60;
229 gint seconds
= t
% 60;
231 return g_strdup_printf("%02d:%02d:%02d", hours
, minutes
, seconds
);
234 static void update_times(time_t elapsed_time
,
235 time_t remaining_time
, time_t total_time
)
239 text
= time2str(elapsed_time
);
240 gtk_label_set_text(elapsed_label
, text
);
243 text
= time2str(remaining_time
);
244 gtk_label_set_text(remaining_label
, text
);
247 text
= time2str(total_time
);
248 gtk_label_set_text(total_label
, text
);
252 static void update_progress(const gchar
* filename
, gint id
, gint count
)
255 static GTimeVal last
= { 0, 0 };
256 time_t current_time
, elapsed_time
, total_time
, remaining_time
;
260 if (filename
== NULL
) {
262 gchar
*msg
= _("Inserting files...");
264 last
.tv_sec
= last
.tv_usec
= 0;
265 gtk_label_set_text(file_label
, msg
);
269 g_get_current_time(&now
);
271 if (last
.tv_sec
!= 0 || last
.tv_usec
!= 0) {
272 /* Not the first time. */
275 g_get_current_time(&now
);
276 usec_diff
= (now
.tv_sec
- last
.tv_sec
) * G_USEC_PER_SEC
+
277 now
.tv_usec
- last
.tv_usec
;
279 if (usec_diff
< G_USEC_PER_SEC
/ 10)
280 /* Lower the redrawing load. */
286 base
= strrchr(filename
, '/');
293 if (len
> MAX_DISPLAYED_NAME_LENGTH
) {
297 begin
= g_strndup(base
, MAX_DISPLAYED_NAME_LENGTH
/ 2 - 3);
298 end
= base
+ len
- MAX_DISPLAYED_NAME_LENGTH
/ 2 + 3;
299 text
= g_strconcat(begin
, " ... ", end
, NULL
);
302 gtk_label_set_text(file_label
, text
);
305 gtk_label_set_text(file_label
, base
);
308 ratio_text
= g_strdup_printf("%d / %d", id
, count
- 1);
309 gtk_label_set_text(ratio_label
, ratio_text
);
312 gtk_progress_bar_set_fraction(progress
, (gdouble
) id
/ (count
- 1));
314 current_time
= time(NULL
);
315 elapsed_time
= current_time
- start_time
;
319 total_time
= elapsed_time
* (count
- 1) / id
;
321 remaining_time
= total_time
- elapsed_time
;
323 update_times(elapsed_time
, remaining_time
, total_time
);
327 static void destroy_progress_dialog(void)
329 if (progress_window
!= NULL
) {
330 if (GTK_IS_WIDGET(progress_window
))
331 gtk_widget_destroy(GTK_WIDGET(progress_window
));
332 progress_window
= NULL
;
337 * In the serialization and deserialization,
338 * returning FALSE means aborting it.
341 /*** Serialization ***/
347 gchar
*cwd
; /* To avoid relative filenames. */
348 } serialization_data
;
350 /* Attempts to put a string in the serialized file. */
351 #define WRITE(file, str) \
353 gchar * __str__ = (str); \
354 gint len = strlen(__str__) + 1; \
356 if (fwrite(__str__, 1, len, (file)) != len) \
360 /* Attempts to put a number (in string form) in the serialized file. */
361 #define WRITE_NB(file, nb) \
363 gchar *str = g_strdup_printf("%d%c", (nb), '\0'); \
364 gint len = strlen(str) + 1; \
366 if (fwrite(str, 1, len, (file)) != len) { \
374 static gboolean
print_header(serialization_data
* work
)
376 WRITE(work
->file
, "GLiv <COLLECTION>");
377 WRITE(work
->file
, "1"); /* Version */
378 WRITE_NB(work
->file
, work
->count
); /* FileCount */
383 static gboolean
print_footer(FILE * file
)
385 WRITE(file
, "</COLLECTION>");
390 /* Write the MaybeThumb part of an entry. */
391 static gboolean
write_thumb(tree_item
* item
, FILE * file
)
393 gint height
, rowstride
, len
;
396 if (fill_thumbnail(item
) == FALSE
) {
402 height
= gdk_pixbuf_get_height(thumb
);
403 rowstride
= gdk_pixbuf_get_rowstride(thumb
);
404 len
= height
* rowstride
;
407 WRITE_NB(file
, strlen(item
->thumb_key
)); /* HashKeyLength */
408 WRITE(file
, item
->thumb_key
); /* HashKey */
409 WRITE_NB(file
, gdk_pixbuf_get_colorspace(thumb
)); /* GdkColorspace */
410 WRITE_NB(file
, gdk_pixbuf_get_has_alpha(thumb
)); /* AlphaChannel */
411 WRITE_NB(file
, gdk_pixbuf_get_bits_per_sample(thumb
)); /* BitsPerSample */
412 WRITE_NB(file
, gdk_pixbuf_get_width(thumb
)); /* ThumbWidth */
413 WRITE_NB(file
, height
); /* ThumbHeight */
414 WRITE_NB(file
, rowstride
); /* RowStride */
415 WRITE_NB(file
, len
); /* PixelDataLength */
418 return fwrite(gdk_pixbuf_get_pixels(thumb
), 1, len
, file
) == len
;
421 static gboolean
serialize_file(GNode
* tree
, serialization_data
* work
)
423 tree_item
*item
= tree
->data
;
424 FILE *file
= work
->file
;
427 update_progress(item
->path
, work
->file_id
, work
->count
);
429 WRITE(file
, "<FILE>");
431 path
= get_absolute_filename(item
->path
);
432 WRITE_NB(file
, strlen(path
)); /* PathLength */
433 WRITE(file
, path
); /* Path */
436 if (write_thumb(item
, file
) == FALSE
) /* MaybeThumb */
439 WRITE(file
, "</FILE>");
445 /* The tree is reversed, so we traverse it in the reverse direction. */
446 static gboolean
reverse_traverse_leafs(GNode
* tree
,
447 GNodeTraverseFunc func
, gpointer data
)
451 if (G_NODE_IS_LEAF(tree
))
452 return func(tree
, data
);
454 child
= g_node_last_child(tree
);
455 while (child
!= NULL
) {
456 if (canceled_using_tree())
459 if (reverse_traverse_leafs(child
, func
, data
) == FALSE
)
468 #define FILE_ERROR(filename) \
470 const gchar *message = g_strerror(errno); \
471 message = g_strconcat((filename), ": ", message, NULL); \
472 DIALOG_MSG(message); \
475 void serialize_collection(void)
479 const gchar
*filename
;
480 serialization_data
*work
;
487 filename
= get_a_file(_("Choose a file to save the collection"));
488 if (filename
== NULL
) {
493 file
= fopen(filename
, "w");
495 FILE_ERROR(filename
);
500 create_progress_dialog(filename
, TRUE
, NULL
);
502 work
= g_new(serialization_data
, 1);
505 work
->count
= get_list_length();
506 work
->cwd
= g_get_current_dir();
508 if (print_header(work
) == FALSE
)
511 res
= reverse_traverse_leafs(tree
,
512 (GNodeTraverseFunc
) serialize_file
, work
);
514 if (res
== FALSE
|| work
->file_id
!= work
->count
)
517 if (print_footer(work
->file
) == FALSE
)
520 if (fclose(work
->file
)) {
529 if (canceled_using_tree() == FALSE
|| remove(filename
) < 0)
530 FILE_ERROR(filename
);
533 if (work
->file
!= NULL
)
539 destroy_progress_dialog();
542 /*** Deserialization ***/
544 /* Checks that file starts with magic. */
545 static gboolean
check_magic(const gchar
* magic
, FILE * file
)
548 gint size
= strlen(magic
) + 1;
549 gchar
*buffer
= g_new(gchar
, size
);
551 if (fread(buffer
, 1, size
, file
) != size
) {
556 res
= buffer
[size
- 1] == '\0' && g_str_equal(magic
, buffer
);
562 /* Reads a number represented by a string. -1 => ERROR */
563 static gint
read_number(FILE * file
)
566 gint read_char
= fgetc(file
);
568 while (isdigit(read_char
)) {
569 number
= number
* 10 + read_char
- '0';
570 read_char
= fgetc(file
);
573 if (read_char
== '\0')
579 #define READ_THUMB_NB(file, var) \
581 var = read_number(file); \
586 static gboolean
read_pixbuf(FILE * file
, GdkPixbuf
** thumb
)
588 GdkColorspace colorspace
;
590 gint bits_per_sample
, width
, height
, rowstride
;
594 READ_THUMB_NB(file
, colorspace
);
595 READ_THUMB_NB(file
, has_alpha
);
596 if (has_alpha
!= FALSE
&& has_alpha
!= TRUE
)
599 READ_THUMB_NB(file
, bits_per_sample
);
600 READ_THUMB_NB(file
, width
);
601 READ_THUMB_NB(file
, height
);
602 READ_THUMB_NB(file
, rowstride
);
603 READ_THUMB_NB(file
, pixel_length
);
605 pixel
= g_new(guchar
, pixel_length
);
606 if (fread(pixel
, 1, pixel_length
, file
) != pixel_length
) {
611 *thumb
= gdk_pixbuf_new_from_data(pixel
, colorspace
, has_alpha
,
612 bits_per_sample
, width
, height
, rowstride
,
613 (GdkPixbufDestroyNotify
) g_free
, NULL
);
615 if (*thumb
== NULL
) {
623 static gboolean
read_thumb(FILE * file
, gchar
** hash_key
, GdkPixbuf
** thumb
)
625 gint has_thumb
, hash_key_length
;
627 has_thumb
= read_number(file
);
634 hash_key_length
= read_number(file
);
635 if (hash_key_length
<= 0)
639 *hash_key
= g_new(gchar
, hash_key_length
);
641 if (fread(*hash_key
, 1, hash_key_length
, file
) != hash_key_length
||
642 (*hash_key
)[hash_key_length
- 1] != '\0') {
648 return read_pixbuf(file
, thumb
);
651 static tree_item
*read_entry(FILE * file
)
655 gchar
*path
, *hash_key
= NULL
;
656 GdkPixbuf
*thumb
= NULL
;
658 if (check_magic("<FILE>", file
) == FALSE
)
661 len
= read_number(file
);
666 path
= g_new(gchar
, len
);
667 if (fread(path
, 1, len
, file
) != len
|| path
[len
- 1] != '\0') {
672 if (read_thumb(file
, &hash_key
, &thumb
) == FALSE
) {
677 if (check_magic("</FILE>", file
) == FALSE
) {
680 if (hash_key
!= NULL
)
681 g_object_unref(thumb
);
686 item
= g_new(tree_item
, 1);
689 item
->thumb_key
= hash_key
;
695 #define CHECK_MAGIC(file, str) \
697 gboolean res = check_magic((str), (file)); \
702 gint
load_dot_gliv_from_file(FILE * file
, gboolean reverse
)
705 tree_item
**items
= NULL
;
706 gchar
**filenames
= NULL
;
707 gboolean ok
= TRUE
, cancel
= FALSE
;
708 gint nb_inserted
= -1;
710 CHECK_MAGIC(file
, "GLiv <COLLECTION>");
711 CHECK_MAGIC(file
, "1");
713 count
= read_number(file
);
717 create_progress_dialog(NULL
, FALSE
, &cancel
);
719 items
= g_new(tree_item
*, count
);
720 for (i
= 0; i
< count
; i
++) {
723 item
= read_entry(file
);
728 update_progress(item
->path
, i
, count
);
737 CHECK_MAGIC(file
, "</COLLECTION>");
739 update_progress(NULL
, count
- 1, count
);
740 filenames
= g_new(gchar
*, count
);
741 for (i
= 0; i
< count
; i
++) {
743 filenames
[i
] = items
[count
- 1 - i
]->path
;
745 filenames
[i
] = items
[i
]->path
;
747 if (items
[i
]->thumb_key
!= NULL
)
748 collection_add_thumbnail(items
[i
]->thumb_key
, items
[i
]->thumb
);
751 nb_inserted
= insert_after_current(filenames
, count
,
752 FALSE
, FALSE
, FALSE
, FALSE
);
762 g_free(items
[i
]->path
);
764 g_free(items
[i
]->thumb_key
);
765 g_object_unref(items
[i
]->thumb
);
771 destroy_progress_dialog();
775 gint
load_dot_gliv(const gchar
* filename
, gboolean reverse
)
781 loader
= get_loader(filename
);
782 if (loader
== LOADER_DECOMP_DOT_GLIV
)
783 nb_inserted
= load_compressed_collection(filename
, reverse
);
786 file
= fopen(filename
, "r");
788 FILE_ERROR(filename
);
792 nb_inserted
= load_dot_gliv_from_file(file
, reverse
);
796 if (nb_inserted
< 0) {
797 DIALOG_MSG(_("%s is not a GLiv collection"),
798 filename_to_utf8(filename
));
805 void deserialize_collection(void)
807 const gchar
*filename
;
809 filename
= get_a_file(_("Choose a collection to load"));
810 if (filename
== NULL
)
813 if (load_dot_gliv(filename
, FALSE
) == 0)
816 if (current_image
== NULL
)
819 destroy_next_image();
820 fill_ident(current_image
);
821 refresh(REFRESH_STATUS
);