root / src / gallery.c @ 1ecb1bbed16ca46035aa6486b301ab5228791fe5
History | View | Annotate | Download (25.8 kB)
| 1 | /*
|
|---|---|
| 2 | * Copyright (C) 2006 Tuomas Kulve <tuomas@kulve.fi> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2, or (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | * USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include <config.h> |
| 22 | #endif
|
| 23 | |
| 24 | #include "main.h" |
| 25 | #include "gallery.h" |
| 26 | #include "image.h" |
| 27 | #include "magick.h" |
| 28 | #include "widgets.h" |
| 29 | #include "vfs.h" |
| 30 | #include "xml.h" |
| 31 | #include "html.h" |
| 32 | |
| 33 | #include <glib.h> |
| 34 | #include <gtk/gtk.h> |
| 35 | #include <string.h> /* memset, strcmp */ |
| 36 | #include <strings.h> /* rindex */ |
| 37 | |
| 38 | |
| 39 | |
| 40 | static gboolean _make_thumbnails(struct data *data); |
| 41 | static gboolean _make_webimages(struct data *data); |
| 42 | |
| 43 | void
|
| 44 | gallery_init(struct data *data)
|
| 45 | {
|
| 46 | |
| 47 | g_assert(data != NULL);
|
| 48 | |
| 49 | g_debug("in gallery_init");
|
| 50 | |
| 51 | if (data->gal != NULL) |
| 52 | gallery_free(data); |
| 53 | |
| 54 | data->gal = g_new0(struct gallery, 1); |
| 55 | |
| 56 | data->current_img = NULL; /* no currently selected image */ |
| 57 | |
| 58 | data->gal->edited = FALSE; |
| 59 | data->gal->images = NULL;
|
| 60 | |
| 61 | /* Set default values */
|
| 62 | data->gal->uri = g_strdup("");
|
| 63 | data->gal->name = g_strdup("");
|
| 64 | data->gal->desc = g_strdup("");
|
| 65 | data->gal->base_dir = g_strdup(data->output_dir); |
| 66 | data->gal->dir_name = g_strdup("pwgallery");
|
| 67 | data->gal->output_dir = g_strdup_printf("%s/%s", data->gal->base_dir,
|
| 68 | data->gal->dir_name); |
| 69 | data->gal->page_gen = data->page_gen; |
| 70 | data->gal->page_gen_prog = g_strdup(data->page_gen_prog); |
| 71 | data->gal->templ_index = g_strdup(data->templ_index); |
| 72 | data->gal->templ_indeximg = g_strdup(data->templ_indeximg); |
| 73 | data->gal->templ_indexgen = g_strdup(data->templ_indexgen); |
| 74 | data->gal->templ_image = g_strdup(data->templ_image); |
| 75 | data->gal->templ_gen = g_strdup(data->templ_gen); |
| 76 | data->gal->thumb_w = data->thumb_w; |
| 77 | data->gal->image_h = data->image_h; |
| 78 | data->gal->image_h2 = data->image_h2; |
| 79 | data->gal->image_h3 = data->image_h3; |
| 80 | data->gal->image_h4 = data->image_h4; |
| 81 | data->gal->remove_exif = data->remove_exif; |
| 82 | data->gal->rename = data->rename; |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | void
|
| 88 | gallery_free(struct data *data)
|
| 89 | {
|
| 90 | GSList *list; |
| 91 | struct image *img;
|
| 92 | |
| 93 | g_assert(data != NULL);
|
| 94 | |
| 95 | if (data->gal == NULL) |
| 96 | return;
|
| 97 | |
| 98 | g_debug("in gallery_free");
|
| 99 | |
| 100 | /* free images */
|
| 101 | if (data->gal->images != NULL) |
| 102 | {
|
| 103 | list = data->gal->images; |
| 104 | while (list)
|
| 105 | {
|
| 106 | img = list->data; |
| 107 | image_free(img); |
| 108 | list->data = NULL;
|
| 109 | list = list->next; |
| 110 | } |
| 111 | g_slist_free(data->gal->images); |
| 112 | data->gal->images = NULL;
|
| 113 | } |
| 114 | |
| 115 | /* free other fields */
|
| 116 | g_free(data->gal->uri); |
| 117 | data->gal->uri = NULL;
|
| 118 | |
| 119 | g_free(data->gal->name); |
| 120 | data->gal->name = NULL;
|
| 121 | |
| 122 | g_free(data->gal->desc); |
| 123 | data->gal->desc = NULL;
|
| 124 | |
| 125 | g_free(data->gal->output_dir); |
| 126 | data->gal->output_dir = NULL;
|
| 127 | |
| 128 | g_free(data->gal->base_dir); |
| 129 | data->gal->base_dir = NULL;
|
| 130 | |
| 131 | g_free(data->gal->dir_name); |
| 132 | data->gal->dir_name = NULL;
|
| 133 | |
| 134 | g_free(data->gal->page_gen_prog); |
| 135 | data->gal->page_gen_prog = NULL;
|
| 136 | |
| 137 | g_free(data->gal->templ_index); |
| 138 | data->gal->templ_index = NULL;
|
| 139 | |
| 140 | g_free(data->gal->templ_image); |
| 141 | data->gal->templ_image = NULL;
|
| 142 | |
| 143 | g_free(data->gal->templ_indeximg); |
| 144 | data->gal->templ_indeximg = NULL;
|
| 145 | |
| 146 | g_free(data->gal->templ_indexgen); |
| 147 | data->gal->templ_indexgen = NULL;
|
| 148 | |
| 149 | g_free(data->gal->templ_gen); |
| 150 | data->gal->templ_gen = NULL;
|
| 151 | |
| 152 | g_free(data->gal); |
| 153 | data->gal = NULL;
|
| 154 | } |
| 155 | |
| 156 | |
| 157 | |
| 158 | void
|
| 159 | gallery_new(struct data *data)
|
| 160 | {
|
| 161 | int result;
|
| 162 | GtkWidget *dialog, *label; |
| 163 | |
| 164 | g_assert(data != NULL );
|
| 165 | |
| 166 | g_debug("in gallery_new");
|
| 167 | |
| 168 | /* if gallery is modified, ask if it should be saved before
|
| 169 | * creating a new one */ |
| 170 | if (data->gal->edited == TRUE)
|
| 171 | {
|
| 172 | dialog = gtk_dialog_new_with_buttons(_("Save changes?"),
|
| 173 | GTK_WINDOW(data->top_window), |
| 174 | GTK_DIALOG_MODAL | |
| 175 | GTK_DIALOG_DESTROY_WITH_PARENT, |
| 176 | GTK_STOCK_CANCEL, |
| 177 | GTK_RESPONSE_CANCEL, |
| 178 | GTK_STOCK_NO, |
| 179 | GTK_RESPONSE_NO, |
| 180 | GTK_STOCK_YES, |
| 181 | GTK_RESPONSE_YES, |
| 182 | NULL);
|
| 183 | |
| 184 | |
| 185 | label = gtk_label_new(_("Gallery has been modified.\n"
|
| 186 | "Save changes before creating "
|
| 187 | "the new gallery?"));
|
| 188 | |
| 189 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), |
| 190 | label); |
| 191 | gtk_widget_show(label); |
| 192 | |
| 193 | result = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 194 | gtk_widget_destroy (dialog); |
| 195 | |
| 196 | switch(result)
|
| 197 | {
|
| 198 | case GTK_RESPONSE_CANCEL:
|
| 199 | return;
|
| 200 | case GTK_RESPONSE_YES:
|
| 201 | gallery_save(data); |
| 202 | break;
|
| 203 | default: /* do nothing on NO response */ |
| 204 | break;
|
| 205 | } |
| 206 | } |
| 207 | |
| 208 | gallery_free(data); |
| 209 | gallery_init(data); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | |
| 214 | void
|
| 215 | gallery_open(struct data *data)
|
| 216 | {
|
| 217 | GtkWidget *dialog; |
| 218 | int result;
|
| 219 | gchar *uri; |
| 220 | guchar *xml_content; |
| 221 | gsize xml_content_len; |
| 222 | |
| 223 | g_assert(data != NULL );
|
| 224 | |
| 225 | g_debug("in gallery_open");
|
| 226 | |
| 227 | /* if gallery is modified, ask if it should be saved before
|
| 228 | * opening a different one */ |
| 229 | if (data->gal->edited == TRUE)
|
| 230 | {
|
| 231 | GtkWidget *label; |
| 232 | dialog = gtk_dialog_new_with_buttons(_("Save changes?"),
|
| 233 | GTK_WINDOW(data->top_window), |
| 234 | GTK_DIALOG_MODAL | |
| 235 | GTK_DIALOG_DESTROY_WITH_PARENT, |
| 236 | GTK_STOCK_CANCEL, |
| 237 | GTK_RESPONSE_CANCEL, |
| 238 | GTK_STOCK_NO, |
| 239 | GTK_RESPONSE_NO, |
| 240 | GTK_STOCK_YES, |
| 241 | GTK_RESPONSE_YES, |
| 242 | NULL);
|
| 243 | |
| 244 | |
| 245 | label = gtk_label_new(_("Gallery has been modified.\n"
|
| 246 | "Save changes before opening "
|
| 247 | "another gallery?"));
|
| 248 | |
| 249 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), |
| 250 | label); |
| 251 | gtk_widget_show(label); |
| 252 | |
| 253 | result = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 254 | gtk_widget_destroy (dialog); |
| 255 | |
| 256 | switch(result)
|
| 257 | {
|
| 258 | case GTK_RESPONSE_CANCEL:
|
| 259 | return;
|
| 260 | case GTK_RESPONSE_YES:
|
| 261 | gallery_save(data); |
| 262 | break;
|
| 263 | default: /* do nothing on no-response */ |
| 264 | break;
|
| 265 | } |
| 266 | } |
| 267 | |
| 268 | gallery_free(data); |
| 269 | gallery_init(data); |
| 270 | |
| 271 | dialog = gtk_file_chooser_dialog_new(_("Open Gallery"),
|
| 272 | GTK_WINDOW(data->top_window), |
| 273 | GTK_FILE_CHOOSER_ACTION_OPEN, |
| 274 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
| 275 | GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, |
| 276 | NULL);
|
| 277 | |
| 278 | gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), |
| 279 | data->gal_dir); |
| 280 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE); |
| 281 | |
| 282 | /* cancel pressed, destroy the dialog and return */
|
| 283 | if (gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT)
|
| 284 | {
|
| 285 | gtk_widget_destroy (dialog); |
| 286 | return;
|
| 287 | } |
| 288 | |
| 289 | uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); |
| 290 | g_assert(uri != NULL); /* FIXME: better error handling? */ |
| 291 | |
| 292 | gtk_widget_destroy (dialog); |
| 293 | |
| 294 | g_free(data->gal->uri); |
| 295 | data->gal->uri = uri; |
| 296 | |
| 297 | /* read the gallery to memory */
|
| 298 | vfs_read_file(data, data->gal->uri, &xml_content, &xml_content_len); |
| 299 | |
| 300 | /* create a new gallery structure based on xml file */
|
| 301 | xml_gal_parse(data, xml_content, xml_content_len); |
| 302 | g_free(xml_content); |
| 303 | |
| 304 | /* update the image text etc shown in the main window */
|
| 305 | widgets_set_image_information(data, data->current_img); |
| 306 | |
| 307 | /* no need to save an gallery that is just opened */
|
| 308 | data->gal->edited = FALSE; |
| 309 | |
| 310 | } |
| 311 | |
| 312 | |
| 313 | |
| 314 | void
|
| 315 | gallery_save(struct data *data)
|
| 316 | {
|
| 317 | guchar *xml_content; |
| 318 | gsize xml_content_size; |
| 319 | |
| 320 | g_assert(data != NULL );
|
| 321 | |
| 322 | g_debug("in gallery_save");
|
| 323 | |
| 324 | /* if uri not set yet, call "save as", and it will call this function
|
| 325 | * again with the uri set.. */ |
| 326 | if (strlen(data->gal->uri) == 0) |
| 327 | {
|
| 328 | gallery_save_as(data); |
| 329 | return;
|
| 330 | } |
| 331 | |
| 332 | xml_content = xml_gal_write(data, &xml_content_size); |
| 333 | |
| 334 | vfs_write_file(data, data->gal->uri, xml_content, xml_content_size); |
| 335 | |
| 336 | /* not edited anymore */
|
| 337 | data->gal->edited = FALSE; |
| 338 | } |
| 339 | |
| 340 | |
| 341 | |
| 342 | void
|
| 343 | gallery_save_as(struct data *data)
|
| 344 | {
|
| 345 | GtkWidget *dialog; |
| 346 | gchar *uri; |
| 347 | |
| 348 | g_assert(data != NULL );
|
| 349 | |
| 350 | g_debug("in gallery_save_as");
|
| 351 | |
| 352 | dialog = gtk_file_chooser_dialog_new(_("Save Gallery As"),
|
| 353 | GTK_WINDOW(data->top_window), |
| 354 | GTK_FILE_CHOOSER_ACTION_SAVE, |
| 355 | GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, |
| 356 | GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, |
| 357 | NULL);
|
| 358 | |
| 359 | /* FIXME: gtk_file_chooser_set_do_overwrite_confirmation ()? */
|
| 360 | gtk_file_chooser_set_current_folder_uri(GTK_FILE_CHOOSER(dialog), |
| 361 | data->gal_dir); |
| 362 | gtk_file_chooser_set_select_multiple(GTK_FILE_CHOOSER(dialog), FALSE); |
| 363 | |
| 364 | /* cancel, destroy dialog and return */
|
| 365 | if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_CANCEL)
|
| 366 | {
|
| 367 | gtk_widget_destroy (dialog); |
| 368 | return;
|
| 369 | } |
| 370 | |
| 371 | uri = gtk_file_chooser_get_uri(GTK_FILE_CHOOSER(dialog)); |
| 372 | g_assert(uri != NULL); /* CHECKME: better error handling? */ |
| 373 | |
| 374 | gtk_widget_destroy (dialog); |
| 375 | |
| 376 | g_free(data->gal->uri); |
| 377 | |
| 378 | |
| 379 | /* add .xml if it is not written there by the user */
|
| 380 | if (g_str_has_suffix(uri, ".xml") == FALSE) |
| 381 | data->gal->uri = g_strdup_printf("%s.xml", uri);
|
| 382 | else
|
| 383 | data->gal->uri = g_strdup(uri); |
| 384 | |
| 385 | g_free(uri); |
| 386 | |
| 387 | /* FIXME: check for existing file */
|
| 388 | |
| 389 | /* now we have the new uri, save the gallery */
|
| 390 | gallery_save(data); |
| 391 | } |
| 392 | |
| 393 | |
| 394 | |
| 395 | void
|
| 396 | gallery_make(struct data *data)
|
| 397 | {
|
| 398 | GtkWidget *dialog; |
| 399 | gint result; |
| 400 | |
| 401 | g_assert(data != NULL );
|
| 402 | |
| 403 | g_debug("in gallery_make");
|
| 404 | |
| 405 | /* Make sure that dir_name is non-empty (to avoid e.g. trying to
|
| 406 | rename file://tmp */ |
| 407 | if (data->gal->dir_name[0] == '\0') { |
| 408 | GtkWidget *label; |
| 409 | dialog = gtk_dialog_new_with_buttons(_("Specify directory name!"),
|
| 410 | GTK_WINDOW(data->top_window), |
| 411 | GTK_DIALOG_MODAL | |
| 412 | GTK_DIALOG_DESTROY_WITH_PARENT, |
| 413 | GTK_STOCK_OK, |
| 414 | GTK_RESPONSE_OK, |
| 415 | NULL);
|
| 416 | |
| 417 | |
| 418 | label = gtk_label_new(_("You must specify directory name for the "
|
| 419 | "gallery.\n"
|
| 420 | "Check from menu: Gallery -> Settings.\n"));
|
| 421 | |
| 422 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), |
| 423 | label); |
| 424 | gtk_widget_show(label); |
| 425 | |
| 426 | result = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 427 | gtk_widget_destroy (dialog); |
| 428 | |
| 429 | return;
|
| 430 | } |
| 431 | |
| 432 | /* if gallery is modified, ask if it should be saved before making
|
| 433 | * it or cancel the making. */ |
| 434 | if (data->gal->edited == TRUE) {
|
| 435 | GtkWidget *label; |
| 436 | dialog = gtk_dialog_new_with_buttons(_("Save changes?"),
|
| 437 | GTK_WINDOW(data->top_window), |
| 438 | GTK_DIALOG_MODAL | |
| 439 | GTK_DIALOG_DESTROY_WITH_PARENT, |
| 440 | GTK_STOCK_CANCEL, |
| 441 | GTK_RESPONSE_CANCEL, |
| 442 | GTK_STOCK_SAVE, |
| 443 | GTK_RESPONSE_YES, |
| 444 | NULL);
|
| 445 | |
| 446 | |
| 447 | label = gtk_label_new(_("Gallery has been modified.\n"
|
| 448 | "Changes must be saved before continuing.\n"));
|
| 449 | |
| 450 | gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog)->vbox), |
| 451 | label); |
| 452 | gtk_widget_show(label); |
| 453 | |
| 454 | result = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 455 | gtk_widget_destroy (dialog); |
| 456 | |
| 457 | switch(result) {
|
| 458 | case GTK_RESPONSE_CANCEL:
|
| 459 | return;
|
| 460 | case GTK_RESPONSE_YES:
|
| 461 | gallery_save(data); |
| 462 | break;
|
| 463 | default: /* There are only cancel and yes responses.. */ |
| 464 | g_assert(421 == 0); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /* make top level directory for gallery */
|
| 469 | if (vfs_is_dir(data, data->gal->output_dir) == TRUE) {
|
| 470 | gchar *dir; |
| 471 | int i = 1; |
| 472 | do {
|
| 473 | dir = g_strdup_printf("%s.%d", data->gal->output_dir, i);
|
| 474 | if(vfs_is_dir(data, dir) == FALSE) {
|
| 475 | break;
|
| 476 | } |
| 477 | g_free(dir); |
| 478 | ++i; |
| 479 | } while(1); |
| 480 | |
| 481 | vfs_rename(data, data->gal->output_dir, dir); |
| 482 | g_free(dir); |
| 483 | } |
| 484 | |
| 485 | vfs_mkdir(data, data->gal->output_dir); |
| 486 | |
| 487 | widgets_set_progress(data, 0, _("Creating gallery")); |
| 488 | |
| 489 | /* make thumbnails */
|
| 490 | if (!_make_thumbnails(data)) {
|
| 491 | widgets_set_progress(data, 0, _("Failed!")); |
| 492 | return;
|
| 493 | } |
| 494 | |
| 495 | /* make webimages */
|
| 496 | if (!_make_webimages(data)) {
|
| 497 | widgets_set_progress(data, 0, _("Failed!")); |
| 498 | return;
|
| 499 | } |
| 500 | |
| 501 | /* make index page */
|
| 502 | if (!html_make_index_page(data)) {
|
| 503 | widgets_set_progress(data, 0, _("Failed!")); |
| 504 | return;
|
| 505 | } |
| 506 | |
| 507 | /* make image pages */
|
| 508 | if (!html_make_image_pages(data)) {
|
| 509 | widgets_set_progress(data, 0, _("Failed!")); |
| 510 | return;
|
| 511 | } |
| 512 | |
| 513 | widgets_set_progress(data, 0, _("Idle")); |
| 514 | } |
| 515 | |
| 516 | |
| 517 | |
| 518 | void
|
| 519 | gallery_add_new_images(struct data *data, GSList *uris)
|
| 520 | {
|
| 521 | GtkWidget *pbar; |
| 522 | struct image *img;
|
| 523 | GSList *first; |
| 524 | gint tot_files, file_counter; |
| 525 | gchar p_text[128];
|
| 526 | |
| 527 | g_assert(data != NULL);
|
| 528 | g_assert(uris != NULL);
|
| 529 | |
| 530 | g_debug("in gallery_add_new_images");
|
| 531 | |
| 532 | first = uris; |
| 533 | tot_files = g_slist_length(uris); /* number of images to add */
|
| 534 | file_counter = 0;
|
| 535 | |
| 536 | pbar = glade_xml_get_widget( data->glade, "progressbar_status");
|
| 537 | g_assert(pbar != NULL);
|
| 538 | |
| 539 | widgets_set_status(data, _("Adding images"));
|
| 540 | |
| 541 | /* Add images */
|
| 542 | while (uris) {
|
| 543 | img = image_open(data, uris->data); |
| 544 | if (img != NULL) { |
| 545 | /* update progress */
|
| 546 | g_snprintf(p_text, 128, "%d/%d", file_counter++, tot_files); |
| 547 | widgets_set_progress(data, (gfloat)file_counter/(gfloat)tot_files, |
| 548 | p_text); |
| 549 | data->gal->images = g_slist_append(data->gal->images, img); |
| 550 | |
| 551 | gtk_widget_show( img->image ); |
| 552 | gtk_widget_show( img->button ); |
| 553 | } |
| 554 | uris = uris->next; |
| 555 | } |
| 556 | |
| 557 | g_slist_free(first); |
| 558 | |
| 559 | /* select first image, if there was no images before this addition */
|
| 560 | if (data->current_img == NULL) { |
| 561 | if (data->gal->images != NULL) { |
| 562 | data->current_img = (struct image *)(data->gal->images->data);
|
| 563 | /* update the image text etc shown in the main window */
|
| 564 | widgets_set_image_information(data, data->current_img); |
| 565 | } else {
|
| 566 | /* nothing to do, still no images */
|
| 567 | return ;
|
| 568 | } |
| 569 | } |
| 570 | |
| 571 | widgets_update_table(data); |
| 572 | |
| 573 | /* total files now in the gallery */
|
| 574 | tot_files = g_slist_length(data->gal->images); |
| 575 | |
| 576 | /* set progress and status */
|
| 577 | widgets_set_progress(data, 0, _("Idle")); |
| 578 | g_snprintf(p_text, 128, "%d %s", tot_files, |
| 579 | tot_files == 1 ? _("Image") : _("Images")); |
| 580 | widgets_set_status(data, p_text); |
| 581 | |
| 582 | /* gallery edited */
|
| 583 | data->gal->edited = TRUE; |
| 584 | |
| 585 | } |
| 586 | |
| 587 | |
| 588 | |
| 589 | void
|
| 590 | gallery_open_images(struct data *data, GSList *imgs)
|
| 591 | {
|
| 592 | GtkWidget *pbar; |
| 593 | struct image *img, *tmpimg;
|
| 594 | GSList *first; |
| 595 | gint tot_files, file_counter; |
| 596 | gchar p_text[128];
|
| 597 | |
| 598 | g_assert(data != NULL);
|
| 599 | /* imgs can be null */
|
| 600 | |
| 601 | g_debug("in gallery_open_images");
|
| 602 | |
| 603 | first = imgs; |
| 604 | tot_files = g_slist_length(imgs); /* number of images to open */
|
| 605 | file_counter = 0;
|
| 606 | |
| 607 | pbar = glade_xml_get_widget( data->glade, "progressbar_status");
|
| 608 | g_assert(pbar != NULL);
|
| 609 | |
| 610 | widgets_set_status(data, _("Opening images"));
|
| 611 | |
| 612 | /* Open images */
|
| 613 | while (imgs)
|
| 614 | {
|
| 615 | tmpimg = imgs->data; |
| 616 | img = image_open(data, g_strdup(tmpimg->uri)); |
| 617 | if (img != NULL) |
| 618 | {
|
| 619 | /* update progress */
|
| 620 | g_snprintf(p_text, 128, "%d/%d", file_counter++, tot_files); |
| 621 | widgets_set_progress(data, (gfloat)file_counter/(gfloat)tot_files, |
| 622 | p_text); |
| 623 | data->gal->images = g_slist_append(data->gal->images, img); |
| 624 | |
| 625 | gtk_widget_show( img->image ); |
| 626 | gtk_widget_show( img->button ); |
| 627 | |
| 628 | /* set image values from */
|
| 629 | g_free(img->text); |
| 630 | img->text = g_strdup(tmpimg->text); |
| 631 | img->gamma = tmpimg->gamma; |
| 632 | img->rotate = tmpimg->rotate; |
| 633 | img->nomodify = tmpimg->nomodify; |
| 634 | |
| 635 | } |
| 636 | image_free(tmpimg); |
| 637 | imgs = imgs->next; |
| 638 | } |
| 639 | |
| 640 | g_slist_free(first); |
| 641 | |
| 642 | /* select first image of the gallery */
|
| 643 | data->current_img = (struct image *)(data->gal->images->data);
|
| 644 | |
| 645 | widgets_update_table(data); |
| 646 | |
| 647 | /* total files now in the gallery */
|
| 648 | tot_files = g_slist_length(data->gal->images); |
| 649 | |
| 650 | /* set progress and status */
|
| 651 | widgets_set_progress(data, 0, _("Idle")); |
| 652 | g_snprintf(p_text, 128, "%d %s", tot_files, |
| 653 | tot_files == 1 ? _("Image") : _("Images")); |
| 654 | widgets_set_status(data, p_text); |
| 655 | |
| 656 | /* data->gal->edited is set in gallery_open() */
|
| 657 | } |
| 658 | |
| 659 | |
| 660 | |
| 661 | void
|
| 662 | gallery_remove_image(struct data *data, struct image *img) |
| 663 | {
|
| 664 | gint current_no; |
| 665 | gint tot_files; |
| 666 | gchar p_text[128];
|
| 667 | GSList *tmplist; |
| 668 | |
| 669 | g_assert(data != NULL);
|
| 670 | g_assert(img != NULL);
|
| 671 | |
| 672 | g_debug("in gallery_remove_image");
|
| 673 | |
| 674 | /* get the index number of the currently selected image */
|
| 675 | current_no = g_slist_index(data->gal->images, img); |
| 676 | g_assert(current_no > -1);
|
| 677 | |
| 678 | /* free image and remove it from the list */
|
| 679 | data->gal->images = g_slist_remove(data->gal->images, img); |
| 680 | image_free(img); |
| 681 | |
| 682 | /* set currently selected image to the next one of the deleted image */
|
| 683 | tmplist = g_slist_nth(data->gal->images, current_no); |
| 684 | |
| 685 | /* select the last image if deleted one was the last one */
|
| 686 | if (tmplist != NULL) |
| 687 | {
|
| 688 | data->current_img = tmplist->data; |
| 689 | } |
| 690 | else
|
| 691 | {
|
| 692 | tmplist = g_slist_last(data->gal->images); |
| 693 | if (tmplist != NULL) |
| 694 | data->current_img = tmplist->data; |
| 695 | else
|
| 696 | data->current_img = NULL;
|
| 697 | } |
| 698 | |
| 699 | /* update the thumbnail list */
|
| 700 | widgets_update_table(data); |
| 701 | |
| 702 | /* total files now in the gallery */
|
| 703 | tot_files = g_slist_length(data->gal->images); |
| 704 | |
| 705 | /* update the image text etc shown in the main window */
|
| 706 | widgets_set_image_information(data, data->current_img); |
| 707 | |
| 708 | /* set progress and status */
|
| 709 | g_snprintf(p_text, 128, "%d %s", tot_files, |
| 710 | tot_files == 1 ? _("Image") : _("Images")); |
| 711 | widgets_set_status(data, p_text); |
| 712 | |
| 713 | data->gal->edited = TRUE; |
| 714 | } |
| 715 | |
| 716 | |
| 717 | |
| 718 | gboolean |
| 719 | gallery_image_selected(GtkWidget *widget, |
| 720 | GdkEventButton *event, |
| 721 | gpointer user_data) |
| 722 | {
|
| 723 | GSList *imgs; |
| 724 | struct data *data;
|
| 725 | struct image *img = NULL; |
| 726 | |
| 727 | g_assert(widget != NULL);
|
| 728 | g_assert(event != NULL);
|
| 729 | g_assert(user_data != NULL);
|
| 730 | |
| 731 | g_debug("in gallery_image_selected");
|
| 732 | |
| 733 | data = user_data; |
| 734 | |
| 735 | /* on doubleclick we already have set everything on first
|
| 736 | * click. Now just show the web image */ |
| 737 | if (event->type == GDK_2BUTTON_PRESS)
|
| 738 | {
|
| 739 | widgets_set_status(data, "Showing preview..");
|
| 740 | magick_show_preview(data, data->current_img, data->gal->image_h); |
| 741 | widgets_set_status(data, "Idle");
|
| 742 | return FALSE;
|
| 743 | } |
| 744 | |
| 745 | /* Find out which button was pressed by going through all image
|
| 746 | * buttons and matching pointer address. |
| 747 | * CHECKME: This should be probably implemented in some more sane way |
| 748 | */ |
| 749 | imgs = data->gal->images; |
| 750 | while (imgs)
|
| 751 | {
|
| 752 | img = imgs->data; |
| 753 | if ((GtkWidget*)(img->button) == widget)
|
| 754 | break;
|
| 755 | imgs = imgs->next; |
| 756 | } |
| 757 | |
| 758 | /* the image must be found since it was clicked.. */
|
| 759 | g_assert(img != NULL);
|
| 760 | |
| 761 | /* save previously selected image's (if any) text */
|
| 762 | gallery_image_save_text(data); |
| 763 | |
| 764 | data->current_img = img; |
| 765 | |
| 766 | /* update the image text etc shown in the main window */
|
| 767 | widgets_set_image_information(data, data->current_img); |
| 768 | |
| 769 | return FALSE; /* let others handle the click too */ |
| 770 | } |
| 771 | |
| 772 | |
| 773 | |
| 774 | void
|
| 775 | gallery_image_save_text(struct data *data)
|
| 776 | {
|
| 777 | gchar *new_text; |
| 778 | g_assert(data != NULL);
|
| 779 | |
| 780 | g_debug("in gallery_image_save_text");
|
| 781 | |
| 782 | /* nothing to save if no images */
|
| 783 | if (data->current_img == NULL) |
| 784 | return;
|
| 785 | |
| 786 | new_text = widgets_image_get_text(data); |
| 787 | |
| 788 | /* if the text is not changed */
|
| 789 | if (strcmp(new_text, data->current_img->text) == 0) |
| 790 | {
|
| 791 | g_free(new_text); |
| 792 | return;
|
| 793 | } |
| 794 | |
| 795 | /* replace old text with the new one and mark gallery as edited */
|
| 796 | g_free(data->current_img->text); |
| 797 | data->current_img->text = new_text; |
| 798 | data->gal->edited = TRUE; |
| 799 | } |
| 800 | |
| 801 | |
| 802 | /*
|
| 803 | * |
| 804 | * Static functions |
| 805 | * |
| 806 | */ |
| 807 | |
| 808 | /*
|
| 809 | * Make thumbnails for the gallery. |
| 810 | */ |
| 811 | static gboolean
|
| 812 | _make_thumbnails(struct data *data)
|
| 813 | {
|
| 814 | gchar *dir_uri; |
| 815 | GSList *images; |
| 816 | gint tot, i; |
| 817 | |
| 818 | g_assert(data != NULL);
|
| 819 | |
| 820 | g_debug("in _make_thumbnails");
|
| 821 | |
| 822 | tot = g_slist_length(data->gal->images); |
| 823 | i = 0;
|
| 824 | |
| 825 | /* make the thumbnail directory */
|
| 826 | dir_uri = g_strdup_printf("%s/thumbnails", data->gal->output_dir);
|
| 827 | vfs_mkdir(data, dir_uri); |
| 828 | |
| 829 | /* make the thumbnails for all images in gallery */
|
| 830 | images = data->gal->images; |
| 831 | while(images != NULL) { |
| 832 | gchar *thumb_uri; |
| 833 | struct image *image = images->data;
|
| 834 | gfloat frac; |
| 835 | gchar progress[256];
|
| 836 | |
| 837 | thumb_uri = g_strdup_printf("%s/%s.%s", dir_uri,
|
| 838 | image->basefilename, image->ext); |
| 839 | |
| 840 | /* make the thumbnail and save it to a file */
|
| 841 | if (magick_make_thumbnail(data, image, thumb_uri) == FALSE) {
|
| 842 | g_free(thumb_uri); |
| 843 | g_free(dir_uri); |
| 844 | return FALSE;
|
| 845 | } |
| 846 | g_free(thumb_uri); |
| 847 | |
| 848 | images = images->next; |
| 849 | /* update status */
|
| 850 | ++i; |
| 851 | snprintf(progress, 256, "%s: %d/%d", _("Creating thumbnails"), i, tot); |
| 852 | frac = (gfloat)i/(gfloat)tot; |
| 853 | g_debug("frac: %f", frac);
|
| 854 | widgets_set_progress(data, frac, progress); |
| 855 | } |
| 856 | g_free(dir_uri); |
| 857 | |
| 858 | return TRUE;
|
| 859 | } |
| 860 | |
| 861 | |
| 862 | |
| 863 | /*
|
| 864 | * Make webimages for the gallery. |
| 865 | */ |
| 866 | static gboolean
|
| 867 | _make_webimages(struct data *data)
|
| 868 | {
|
| 869 | gint image_index = 0;
|
| 870 | gint tot, i; |
| 871 | |
| 872 | g_assert(data != NULL);
|
| 873 | |
| 874 | g_debug("in _make_webimages");
|
| 875 | |
| 876 | tot = g_slist_length(data->gal->images); |
| 877 | |
| 878 | /* go through all sizes */
|
| 879 | while(++image_index < 5) { |
| 880 | gchar *dir_uri; |
| 881 | GSList *images; |
| 882 | gint image_h = -1;
|
| 883 | |
| 884 | i = 0; /* zero image counter */ |
| 885 | |
| 886 | /* FIXME: ugly. Sizes should be in a list */
|
| 887 | switch(image_index) {
|
| 888 | case 1: |
| 889 | image_h = data->gal->image_h; |
| 890 | break;
|
| 891 | case 2: |
| 892 | image_h = data->gal->image_h2; |
| 893 | break;
|
| 894 | case 3: |
| 895 | image_h = data->gal->image_h3; |
| 896 | break;
|
| 897 | case 4: |
| 898 | image_h = data->gal->image_h4; |
| 899 | break;
|
| 900 | default:
|
| 901 | /* we shouldn't be here */
|
| 902 | g_error("_make_webimages: Too many image sizes");
|
| 903 | } |
| 904 | |
| 905 | /* make only images with specified size */
|
| 906 | if (image_h == 0) { |
| 907 | continue;
|
| 908 | } |
| 909 | |
| 910 | /* make the webimage directory */
|
| 911 | if (image_index == 1) { |
| 912 | /* default size images to "images" dir for backward compability */
|
| 913 | dir_uri = g_strdup_printf("%s/images", data->gal->output_dir);
|
| 914 | } else {
|
| 915 | dir_uri = g_strdup_printf("%s/images_%d",
|
| 916 | data->gal->output_dir, image_h); |
| 917 | } |
| 918 | vfs_mkdir(data, dir_uri); |
| 919 | |
| 920 | /* make the webimages for all images in gallery */
|
| 921 | images = data->gal->images; |
| 922 | while(images != NULL) { |
| 923 | |
| 924 | gchar *img_uri; |
| 925 | struct image *image = images->data;
|
| 926 | gfloat frac; |
| 927 | gchar progress[256];
|
| 928 | |
| 929 | img_uri = g_strdup_printf("%s/%s.%s", dir_uri, image->basefilename,
|
| 930 | image->ext); |
| 931 | |
| 932 | /* make the webimage and save it to a file */
|
| 933 | if (magick_make_webimage(data, image, img_uri, image_h) == FALSE) {
|
| 934 | g_free(img_uri); |
| 935 | g_free(dir_uri); |
| 936 | return FALSE;
|
| 937 | } |
| 938 | g_free(img_uri); |
| 939 | |
| 940 | images = images->next; |
| 941 | |
| 942 | /* update status */
|
| 943 | ++i; |
| 944 | snprintf(progress, 256, "%s %d: %d/%d", |
| 945 | _("Creating images"), image_h, i, tot);
|
| 946 | frac = (gfloat)i/(gfloat)tot; |
| 947 | g_debug("frac: %f", frac);
|
| 948 | widgets_set_progress(data, frac, progress); |
| 949 | } |
| 950 | g_free(dir_uri); |
| 951 | } |
| 952 | return TRUE;
|
| 953 | } |
| 954 | |
| 955 | |
| 956 | |
| 957 | /* Emacs indentatation information
|
| 958 | Local Variables: |
| 959 | indent-tabs-mode:nil |
| 960 | tab-width:4 |
| 961 | c-set-offset:4 |
| 962 | c-basic-offset:4 |
| 963 | End: |
| 964 | */ |