Mercurial > geeqie
comparison src/editors.c @ 444:452b7ffeb7ad
Minor cleanup and code redundancy reduction.
author | zas_ |
---|---|
date | Sun, 20 Apr 2008 13:42:22 +0000 |
parents | e7361d06d83a |
children | 48c8e49b571c |
comparison
equal
deleted
inserted
replaced
443:e7361d06d83a | 444:452b7ffeb7ad |
---|---|
234 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL) | 234 while (g_io_channel_read_chars(source, buf, sizeof(buf), &count, NULL) == G_IO_STATUS_NORMAL) |
235 { | 235 { |
236 if (!g_utf8_validate(buf, count, NULL)) | 236 if (!g_utf8_validate(buf, count, NULL)) |
237 { | 237 { |
238 gchar *utf8; | 238 gchar *utf8; |
239 | |
239 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL); | 240 utf8 = g_locale_to_utf8(buf, count, NULL, NULL, NULL); |
240 if (utf8) | 241 if (utf8) |
241 { | 242 { |
242 editor_verbose_window_fill(ed->vd, utf8, -1); | 243 editor_verbose_window_fill(ed->vd, utf8, -1); |
243 g_free(utf8); | 244 g_free(utf8); |
286 p = fd->path; | 287 p = fd->path; |
287 else | 288 else |
288 { | 289 { |
289 while(work) | 290 while(work) |
290 { | 291 { |
292 GList *work2; | |
291 gchar *ext = work->data; | 293 gchar *ext = work->data; |
292 work = work->next; | 294 work = work->next; |
293 | 295 |
294 if (strcmp(ext, "*") == 0 || | 296 if (strcmp(ext, "*") == 0 || |
295 strcasecmp(ext, fd->extension) == 0) | 297 strcasecmp(ext, fd->extension) == 0) |
296 { | 298 { |
297 p = fd->path; | 299 p = fd->path; |
298 break; | 300 break; |
299 } | 301 } |
300 | 302 |
301 GList *work2 = fd->sidecar_files; | 303 work2 = fd->sidecar_files; |
302 | |
303 while(work2) | 304 while(work2) |
304 { | 305 { |
305 FileData *sfd = work2->data; | 306 FileData *sfd = work2->data; |
306 work2 = work2->next; | 307 work2 = work2->next; |
307 | 308 |
322 if (fd->change && fd->change->dest) | 323 if (fd->change && fd->change->dest) |
323 p = fd->change->dest; | 324 p = fd->change->dest; |
324 else | 325 else |
325 p = ""; | 326 p = ""; |
326 } | 327 } |
328 | |
327 while (*p != '\0') | 329 while (*p != '\0') |
328 { | 330 { |
329 /* must escape \, ", `, and $ to avoid problems, | 331 /* must escape \, ", `, and $ to avoid problems, |
330 * we assume system shell supports bash-like escaping | 332 * we assume system shell supports bash-like escaping |
331 */ | 333 */ |
426 p++; | 428 p++; |
427 | 429 |
428 /* for example "%f" or "%{.crw;.raw;.cr2}f" */ | 430 /* for example "%f" or "%{.crw;.raw;.cr2}f" */ |
429 if (*p == '{') | 431 if (*p == '{') |
430 { | 432 { |
433 gchar *end; | |
434 | |
431 p++; | 435 p++; |
432 gchar *end = strchr(p, '}'); | 436 end = strchr(p, '}'); |
433 if (!end) | 437 if (!end) |
434 { | 438 { |
435 flags |= EDITOR_ERROR_SYNTAX; | 439 flags |= EDITOR_ERROR_SYNTAX; |
436 goto err; | 440 goto err; |
437 } | 441 } |
483 if (output) | 487 if (output) |
484 { | 488 { |
485 /* use whole list */ | 489 /* use whole list */ |
486 GList *work = list; | 490 GList *work = list; |
487 gboolean ok = FALSE; | 491 gboolean ok = FALSE; |
492 | |
488 while (work) | 493 while (work) |
489 { | 494 { |
490 FileData *fd = work->data; | 495 FileData *fd = work->data; |
491 pathl = editor_command_path_parse(fd, PATH_FILE, extensions); | 496 pathl = editor_command_path_parse(fd, PATH_FILE, extensions); |
492 | 497 |
587 ed->pid = pid; | 592 ed->pid = pid; |
588 } | 593 } |
589 | 594 |
590 if (ed->vd) | 595 if (ed->vd) |
591 { | 596 { |
592 | |
593 if (!ok) | 597 if (!ok) |
594 { | 598 { |
595 gchar *buf; | 599 gchar *buf; |
596 | 600 |
597 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template); | 601 buf = g_strdup_printf(_("Failed to run command:\n%s\n"), template); |
599 g_free(buf); | 603 g_free(buf); |
600 | 604 |
601 } | 605 } |
602 else | 606 else |
603 { | 607 { |
604 | |
605 GIOChannel *channel_output; | 608 GIOChannel *channel_output; |
606 GIOChannel *channel_error; | 609 GIOChannel *channel_error; |
610 | |
607 channel_output = g_io_channel_unix_new(standard_output); | 611 channel_output = g_io_channel_unix_new(standard_output); |
608 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL); | 612 g_io_channel_set_flags(channel_output, G_IO_FLAG_NONBLOCK, NULL); |
609 | 613 |
610 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, | 614 g_io_add_watch_full(channel_output, G_PRIORITY_HIGH, G_IO_IN | G_IO_ERR | G_IO_HUP, |
611 editor_verbose_io_cb, ed, NULL); | 615 editor_verbose_io_cb, ed, NULL); |
625 return ed->flags & EDITOR_ERROR_MASK; | 629 return ed->flags & EDITOR_ERROR_MASK; |
626 } | 630 } |
627 | 631 |
628 static gint editor_command_next_start(EditorData *ed) | 632 static gint editor_command_next_start(EditorData *ed) |
629 { | 633 { |
630 | |
631 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1); | 634 if (ed->vd) editor_verbose_window_fill(ed->vd, "\n", 1); |
632 | 635 |
633 if (ed->list && ed->count < ed->total) | 636 if (ed->list && ed->count < ed->total) |
634 { | 637 { |
635 FileData *fd; | 638 FileData *fd; |
674 | 677 |
675 if (ed->flags & EDITOR_FOR_EACH) | 678 if (ed->flags & EDITOR_FOR_EACH) |
676 { | 679 { |
677 /* handle the first element from the list */ | 680 /* handle the first element from the list */ |
678 GList *fd_element = ed->list; | 681 GList *fd_element = ed->list; |
682 | |
679 ed->list = g_list_remove_link(ed->list, fd_element); | 683 ed->list = g_list_remove_link(ed->list, fd_element); |
680 if (ed->callback) | 684 if (ed->callback) |
681 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data); | 685 cont = ed->callback(ed->list ? ed : NULL, ed->flags, fd_element, ed->data); |
682 filelist_free(fd_element); | 686 filelist_free(fd_element); |
683 } | 687 } |
694 return ed->flags & EDITOR_ERROR_MASK; | 698 return ed->flags & EDITOR_ERROR_MASK; |
695 else if (cont == EDITOR_CB_SKIP) | 699 else if (cont == EDITOR_CB_SKIP) |
696 return editor_command_done(ed); | 700 return editor_command_done(ed); |
697 else | 701 else |
698 return editor_command_next_start(ed); | 702 return editor_command_next_start(ed); |
699 | |
700 } | 703 } |
701 | 704 |
702 static gint editor_command_done(EditorData *ed) | 705 static gint editor_command_done(EditorData *ed) |
703 { | 706 { |
704 gint flags; | 707 gint flags; |
705 const gchar *text; | |
706 | 708 |
707 if (ed->vd) | 709 if (ed->vd) |
708 { | 710 { |
711 const gchar *text; | |
712 | |
709 if (ed->count == ed->total) | 713 if (ed->count == ed->total) |
710 { | 714 { |
711 text = _("done"); | 715 text = _("done"); |
712 } | 716 } |
713 else | 717 else |
772 editor_command_next_start(ed); | 776 editor_command_next_start(ed); |
773 /* errors from editor_command_next_start will be handled via callback */ | 777 /* errors from editor_command_next_start will be handled via callback */ |
774 return flags & EDITOR_ERROR_MASK; | 778 return flags & EDITOR_ERROR_MASK; |
775 } | 779 } |
776 | 780 |
781 static gint is_valid_editor_command(gint n) | |
782 { | |
783 return (n >= 0 && n < GQ_EDITOR_SLOTS | |
784 && options->editor_command[n] | |
785 && strlen(options->editor_command[n]) > 0); | |
786 } | |
787 | |
777 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data) | 788 gint start_editor_from_filelist_full(gint n, GList *list, EditorCallback cb, gpointer data) |
778 { | 789 { |
779 gchar *command; | 790 gchar *command; |
780 gint error; | 791 gint error; |
781 | 792 |
782 if (n < 0 || n >= GQ_EDITOR_SLOTS || !list || | 793 if (!list) return FALSE; |
783 !options->editor_command[n] || | 794 if (!is_valid_editor_command(n)) return FALSE; |
784 strlen(options->editor_command[n]) == 0) return FALSE; | |
785 | 795 |
786 command = g_locale_from_utf8(options->editor_command[n], -1, NULL, NULL, NULL); | 796 command = g_locale_from_utf8(options->editor_command[n], -1, NULL, NULL, NULL); |
787 error = editor_command_start(command, options->editor_name[n], list, cb, data); | 797 error = editor_command_start(command, options->editor_name[n], list, cb, data); |
788 g_free(command); | 798 g_free(command); |
789 return error; | 799 return error; |
792 gint start_editor_from_filelist(gint n, GList *list) | 802 gint start_editor_from_filelist(gint n, GList *list) |
793 { | 803 { |
794 return start_editor_from_filelist_full(n, list, NULL, NULL); | 804 return start_editor_from_filelist_full(n, list, NULL, NULL); |
795 } | 805 } |
796 | 806 |
797 | |
798 gint start_editor_from_file_full(gint n, FileData *fd, EditorCallback cb, gpointer data) | 807 gint start_editor_from_file_full(gint n, FileData *fd, EditorCallback cb, gpointer data) |
799 { | 808 { |
800 GList *list; | 809 GList *list; |
801 gint error; | 810 gint error; |
802 | 811 |
813 return start_editor_from_file_full(n, fd, NULL, NULL); | 822 return start_editor_from_file_full(n, fd, NULL, NULL); |
814 } | 823 } |
815 | 824 |
816 gint editor_window_flag_set(gint n) | 825 gint editor_window_flag_set(gint n) |
817 { | 826 { |
818 if (n < 0 || n >= GQ_EDITOR_SLOTS || | 827 if (!is_valid_editor_command(n)) return TRUE; |
819 !options->editor_command[n] || | |
820 strlen(options->editor_command[n]) == 0) return TRUE; | |
821 | 828 |
822 return (editor_command_parse(options->editor_command[n], NULL, NULL) & EDITOR_KEEP_FS); | 829 return (editor_command_parse(options->editor_command[n], NULL, NULL) & EDITOR_KEEP_FS); |
823 } | 830 } |
824 | |
825 | 831 |
826 const gchar *editor_get_error_str(gint flags) | 832 const gchar *editor_get_error_str(gint flags) |
827 { | 833 { |
828 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty."); | 834 if (flags & EDITOR_ERROR_EMPTY) return _("Editor template is empty."); |
829 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax."); | 835 if (flags & EDITOR_ERROR_SYNTAX) return _("Editor template has incorrect syntax."); |