claws-mail/src/mimeview.c

1751 lines
48 KiB
C
Raw Normal View History

2001-04-19 14:21:46 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
2003-04-05 10:58:21 +02:00
* Copyright (C) 1999-2003 Hiroyuki Yamamoto
2001-04-19 14:21:46 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "defs.h"
#include <glib.h>
#include <gdk/gdkkeysyms.h>
2002-02-25 10:25:17 +01:00
#include <gtk/gtknotebook.h>
2001-04-19 14:21:46 +02:00
#include <gtk/gtkscrolledwindow.h>
#include <gtk/gtkctree.h>
#include <gtk/gtkvbox.h>
#include <gtk/gtkhbox.h>
#include <gtk/gtkvpaned.h>
2001-04-19 14:21:46 +02:00
#include <gtk/gtktext.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkmenu.h>
#include <gtk/gtkdnd.h>
#include <gtk/gtkselection.h>
#include <gtk/gtktooltips.h>
#include <gtk/gtkcontainer.h>
#include <gtk/gtkbutton.h>
2001-04-19 14:21:46 +02:00
#include <stdio.h>
#ifndef HAVE_APACHE_FNMATCH
/* kludge: apache's fnmatch clashes with <regex.h>, don't include
* fnmatch.h */
#include <fnmatch.h>
#endif
2001-04-19 14:21:46 +02:00
#include "intl.h"
#include "main.h"
#include "mimeview.h"
#include "textview.h"
#include "procmime.h"
#include "summaryview.h"
#include "menu.h"
#include "filesel.h"
#include "alertpanel.h"
2001-05-10 13:19:38 +02:00
#include "inputdialog.h"
2001-04-19 14:21:46 +02:00
#include "utils.h"
#include "gtkutils.h"
#include "prefs_common.h"
#include "rfc2015.h"
#include "stock_pixmap.h"
#include "gtk/gtkvscrollbutton.h"
2001-04-19 14:21:46 +02:00
typedef enum
{
COL_MIMETYPE = 0,
COL_SIZE = 1,
COL_NAME = 2
} MimeViewColumnPos;
#define N_MIMEVIEW_COLS 3
static void mimeview_set_multipart_tree (MimeView *mimeview,
MimeInfo *mimeinfo,
GtkCTreeNode *parent);
static GtkCTreeNode *mimeview_append_part (MimeView *mimeview,
MimeInfo *partinfo,
GtkCTreeNode *parent);
static void mimeview_show_message_part (MimeView *mimeview,
MimeInfo *partinfo);
static void mimeview_change_view_type (MimeView *mimeview,
MimeViewType type);
2001-04-19 14:21:46 +02:00
static void mimeview_clear (MimeView *mimeview);
static void mimeview_selected (GtkCTree *ctree,
GtkCTreeNode *node,
gint column,
MimeView *mimeview);
static void mimeview_start_drag (GtkWidget *widget,
gint button,
GdkEvent *event,
MimeView *mimeview);
2002-01-29 11:03:33 +01:00
static gint mimeview_button_pressed (GtkWidget *widget,
2001-04-19 14:21:46 +02:00
GdkEventButton *event,
MimeView *mimeview);
2002-01-29 11:03:33 +01:00
static gint mimeview_key_pressed (GtkWidget *widget,
2001-04-19 14:21:46 +02:00
GdkEventKey *event,
MimeView *mimeview);
static void mimeview_drag_data_get (GtkWidget *widget,
GdkDragContext *drag_context,
GtkSelectionData *selection_data,
guint info,
guint time,
MimeView *mimeview);
static void mimeview_display_as_text (MimeView *mimeview);
static void mimeview_save_as (MimeView *mimeview);
static void mimeview_save_all (MimeView *mimeview);
2001-04-19 14:21:46 +02:00
static void mimeview_launch (MimeView *mimeview);
2001-05-10 13:19:38 +02:00
static void mimeview_open_with (MimeView *mimeview);
2001-04-19 14:21:46 +02:00
static void mimeview_view_file (const gchar *filename,
2001-05-10 13:19:38 +02:00
MimeInfo *partinfo,
const gchar *cmdline);
static gboolean icon_clicked_cb (GtkWidget *button,
GdkEventButton *event,
MimeView *mimeview);
static void icon_selected (MimeView *mimeview,
gint num,
MimeInfo *partinfo);
static gint icon_key_pressed (GtkWidget *button,
GdkEventKey *event,
MimeView *mimeview);
static void toggle_icon (GtkToggleButton *button,
MimeView *mimeview);
static void icon_list_append_icon (MimeView *mimeview,
MimeInfo *mimeinfo);
static void icon_list_create (MimeView *mimeview,
MimeInfo *mimeinfo);
static void icon_list_clear (MimeView *mimeview);
static void icon_list_toggle_by_mime_info (MimeView *mimeview,
MimeInfo *mimeinfo);
static gboolean icon_list_select_by_number(MimeView *mimeview,
gint number);
static void mime_toggle_button_cb (GtkWidget *button,
MimeView *mimeview);
2003-10-05 12:25:17 +02:00
static gboolean part_button_pressed (MimeView *mimeview,
GdkEventButton *event,
MimeInfo *partinfo);
static void icon_scroll_size_allocate_cb(GtkWidget *widget,
GtkAllocation *layout_size,
MimeView *mimeview);
2001-04-19 14:21:46 +02:00
static GtkItemFactoryEntry mimeview_popup_entries[] =
{
{N_("/_Open"), NULL, mimeview_launch, 0, NULL},
2001-05-10 13:19:38 +02:00
{N_("/Open _with..."), NULL, mimeview_open_with, 0, NULL},
2001-04-19 14:21:46 +02:00
{N_("/_Display as text"), NULL, mimeview_display_as_text, 0, NULL},
{N_("/_Save as..."), NULL, mimeview_save_as, 0, NULL},
{N_("/Save _all..."), NULL, mimeview_save_all, 0, NULL}
2001-04-19 14:21:46 +02:00
#if USE_GPGME
,
{N_("/_Check signature"), NULL, mimeview_check_signature, 0, NULL}
#endif
};
static GtkTargetEntry mimeview_mime_types[] =
{
{"text/uri-list", 0, 0}
};
GSList *mimeviewer_factories;
GSList *mimeviews;
MimeView *mimeview_create(MainWindow *mainwin)
2001-04-19 14:21:46 +02:00
{
MimeView *mimeview;
2002-02-25 10:25:17 +01:00
GtkWidget *notebook;
GtkWidget *vbox;
2001-04-19 14:21:46 +02:00
GtkWidget *paned;
GtkWidget *scrolledwin;
GtkWidget *ctree;
GtkWidget *mime_notebook;
2001-04-19 14:21:46 +02:00
GtkWidget *popupmenu;
GtkWidget *ctree_mainbox;
GtkWidget *mime_toggle;
GtkWidget *icon_mainbox;
GtkWidget *icon_scroll;
GtkWidget *icon_vbox;
GtkWidget *arrow;
GtkWidget *scrollbutton;
GtkWidget *hbox;
GtkTooltips *tooltips;
2001-04-19 14:21:46 +02:00
GtkItemFactory *popupfactory;
2002-02-08 11:07:11 +01:00
gchar *titles[N_MIMEVIEW_COLS];
2001-04-19 14:21:46 +02:00
gint n_entries;
gint i;
2002-08-15 09:38:17 +02:00
debug_print("Creating MIME view...\n");
2001-04-19 14:21:46 +02:00
mimeview = g_new0(MimeView, 1);
2002-02-08 11:07:11 +01:00
titles[COL_MIMETYPE] = _("MIME Type");
titles[COL_SIZE] = _("Size");
titles[COL_NAME] = _("Name");
2002-02-25 10:25:17 +01:00
notebook = gtk_notebook_new();
gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
gtk_notebook_set_show_tabs(GTK_NOTEBOOK(notebook), FALSE);
gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
2002-02-25 10:25:17 +01:00
vbox = gtk_vbox_new(FALSE, 0);
gtk_container_add(GTK_CONTAINER(notebook), vbox);
2001-04-19 14:21:46 +02:00
scrolledwin = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
GTK_POLICY_AUTOMATIC,
GTK_POLICY_ALWAYS);
ctree = gtk_sctree_new_with_titles(N_MIMEVIEW_COLS, 0, titles);
2001-06-03 12:34:31 +02:00
gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
2001-04-19 14:21:46 +02:00
gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_NONE);
gtk_clist_set_column_justification(GTK_CLIST(ctree), COL_SIZE,
GTK_JUSTIFY_RIGHT);
gtk_clist_set_column_width(GTK_CLIST(ctree), COL_MIMETYPE, 240);
gtk_clist_set_column_width(GTK_CLIST(ctree), COL_SIZE, 90);
2001-04-19 14:21:46 +02:00
for (i = 0; i < N_MIMEVIEW_COLS; i++)
GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(ctree)->column[i].button,
GTK_CAN_FOCUS);
gtk_container_add(GTK_CONTAINER(scrolledwin), ctree);
2003-10-05 12:10:30 +02:00
g_signal_connect(G_OBJECT(ctree), "tree_select_row",
G_CALLBACK(mimeview_selected), mimeview);
g_signal_connect(G_OBJECT(ctree), "button_press_event",
G_CALLBACK(mimeview_button_pressed), mimeview);
g_signal_connect(G_OBJECT(ctree), "key_press_event",
G_CALLBACK(mimeview_key_pressed), mimeview);
g_signal_connect(G_OBJECT (ctree),"start_drag",
G_CALLBACK (mimeview_start_drag), mimeview);
g_signal_connect(G_OBJECT(ctree), "drag_data_get",
G_CALLBACK(mimeview_drag_data_get), mimeview);
mime_notebook = gtk_notebook_new();
gtk_widget_show(mime_notebook);
GTK_WIDGET_UNSET_FLAGS(mime_notebook, GTK_CAN_FOCUS);
gtk_notebook_set_show_tabs(GTK_NOTEBOOK(mime_notebook), FALSE);
gtk_notebook_set_show_border(GTK_NOTEBOOK(mime_notebook), FALSE);
2003-10-05 12:10:30 +02:00
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), 0);
icon_vbox = gtk_vbox_new(FALSE, 2);
icon_scroll = gtk_layout_new(NULL, NULL);
gtk_layout_put(GTK_LAYOUT(icon_scroll), icon_vbox, 0, 0);
scrollbutton = gtk_vscrollbutton_new(gtk_layout_get_vadjustment(GTK_LAYOUT(icon_scroll)));
mime_toggle = gtk_toggle_button_new();
arrow = gtk_arrow_new(GTK_ARROW_LEFT, GTK_SHADOW_NONE);
gtk_widget_show(arrow);
gtk_container_add(GTK_CONTAINER(mime_toggle), arrow);
2003-10-05 12:10:30 +02:00
g_signal_connect(G_OBJECT(mime_toggle), "toggled",
G_CALLBACK(mime_toggle_button_cb), mimeview);
icon_mainbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(icon_mainbox), mime_toggle, FALSE, FALSE, 0);
gtk_box_pack_start(GTK_BOX(icon_mainbox), icon_scroll, TRUE, TRUE, 3);
gtk_box_pack_end(GTK_BOX(icon_mainbox), scrollbutton, FALSE, FALSE, 0);
2003-10-05 12:10:30 +02:00
g_signal_connect(G_OBJECT(icon_mainbox), "size_allocate",
G_CALLBACK(icon_scroll_size_allocate_cb), mimeview);
ctree_mainbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(ctree_mainbox), scrolledwin, TRUE, TRUE, 0);
n_entries = sizeof(mimeview_popup_entries) /
sizeof(mimeview_popup_entries[0]);
popupmenu = menu_create_items(mimeview_popup_entries, n_entries,
"<MimeView>", &popupfactory, mimeview);
tooltips = gtk_tooltips_new();
gtk_tooltips_set_delay(tooltips, 0);
paned = gtk_vpaned_new();
gtk_paned_set_gutter_size(GTK_PANED(paned), 6);
gtk_paned_pack1(GTK_PANED(paned), ctree_mainbox, FALSE, TRUE);
gtk_paned_pack2(GTK_PANED(paned), mime_notebook, TRUE, TRUE);
gtk_container_add(GTK_CONTAINER(notebook), paned);
hbox = gtk_hbox_new(FALSE, 0);
gtk_box_pack_start(GTK_BOX(hbox), notebook, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(hbox), icon_mainbox, FALSE, FALSE, 0);
2001-04-19 14:21:46 +02:00
gtk_widget_show_all(hbox);
gtk_widget_hide(ctree_mainbox);
2001-04-19 14:21:46 +02:00
mimeview->notebook = notebook;
mimeview->vbox = vbox;
mimeview->hbox = hbox;
mimeview->paned = paned;
mimeview->scrolledwin = scrolledwin;
mimeview->ctree = ctree;
mimeview->mime_notebook = mime_notebook;
mimeview->popupmenu = popupmenu;
mimeview->popupfactory = popupfactory;
mimeview->type = -1;
mimeview->ctree_mainbox = ctree_mainbox;
mimeview->icon_scroll = icon_scroll;
mimeview->icon_vbox = icon_vbox;
mimeview->icon_mainbox = icon_mainbox;
mimeview->icon_count = 0;
mimeview->mainwin = mainwin;
mimeview->tooltips = tooltips;
mimeview->oldsize = 60;
mimeview->mime_toggle = mime_toggle;
2001-04-19 14:21:46 +02:00
mimeview->target_list = gtk_target_list_new(mimeview_mime_types, 1);
mimeviews = g_slist_prepend(mimeviews, mimeview);
2001-04-19 14:21:46 +02:00
return mimeview;
}
void mimeview_init(MimeView *mimeview)
{
2003-06-30 08:21:21 +02:00
textview_init(mimeview->textview);
gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
GTK_WIDGET_PTR(mimeview->textview));
2001-04-19 14:21:46 +02:00
}
/*
* Check whether the message is OpenPGP signed
*/
#if USE_GPGME
static gboolean mimeview_is_signed(MimeView *mimeview)
{
2002-02-28 11:59:50 +01:00
MimeInfo *partinfo = NULL;
2001-04-19 14:21:46 +02:00
debug_print("mimeview_is signed of %p\n", mimeview);
if (!mimeview) return FALSE;
if (!mimeview->opened) return FALSE;
debug_print("mimeview_is_signed: open\n" );
if (!mimeview->file) return FALSE;
debug_print("mimeview_is_signed: file\n" );
partinfo = gtk_ctree_node_get_row_data
(GTK_CTREE(mimeview->ctree), mimeview->opened);
2001-04-19 14:21:46 +02:00
g_return_val_if_fail(partinfo != NULL, FALSE);
/* walk the tree and see whether there is a signature somewhere */
do {
if (rfc2015_has_signature(partinfo))
return TRUE;
} while ((partinfo = partinfo->parent) != NULL);
debug_print("mimeview_is_signed: FALSE\n" );
return FALSE;
}
static void set_unchecked_signature(MimeInfo *mimeinfo)
{
MimeInfo *sig_partinfo;
sig_partinfo = rfc2015_find_signature(mimeinfo);
if (sig_partinfo == NULL) return;
g_free(sig_partinfo->sigstatus);
sig_partinfo->sigstatus =
2003-04-30 14:57:39 +02:00
g_strdup(_("Right-click here to verify the signature"));
2001-04-19 14:21:46 +02:00
g_free(sig_partinfo->sigstatus_full);
sig_partinfo->sigstatus_full = NULL;
}
2001-04-27 22:27:24 +02:00
#endif /* USE_GPGME */
2001-04-19 14:21:46 +02:00
void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo,
const gchar *file)
{
GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
GtkCTreeNode *node;
FILE *fp;
mimeview_clear(mimeview);
2002-02-25 10:25:17 +01:00
textview_clear(mimeview->messageview->textview);
2001-04-19 14:21:46 +02:00
g_return_if_fail(file != NULL);
g_return_if_fail(mimeinfo != NULL);
mimeview->mimeinfo = mimeinfo;
mimeview->file = g_strdup(file);
#if USE_GPGME
2003-05-07 09:36:15 +02:00
if (prefs_common.auto_check_signatures && gpg_started) {
if ((fp = fopen(file, "rb")) == NULL) {
FILE_OP_ERROR(file, "fopen");
return;
}
2001-04-19 14:21:46 +02:00
rfc2015_check_signature(mimeinfo, fp);
2003-05-07 09:36:15 +02:00
fclose(fp);
} else
2001-04-19 14:21:46 +02:00
set_unchecked_signature(mimeinfo);
#endif
2003-10-05 12:10:30 +02:00
g_signal_handlers_block_by_func(G_OBJECT(ctree), mimeview_selected,
mimeview);
2001-04-19 14:21:46 +02:00
mimeview_set_multipart_tree(mimeview, mimeinfo, NULL);
icon_list_create(mimeview, mimeinfo);
2001-04-19 14:21:46 +02:00
2003-10-05 12:10:30 +02:00
g_signal_handlers_unblock_by_func(G_OBJECT(ctree),
mimeview_selected, mimeview);
2001-04-19 14:21:46 +02:00
2001-09-20 13:53:09 +02:00
/* search first text part */
for (node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
node != NULL; node = GTK_CTREE_NODE_NEXT(node)) {
MimeInfo *partinfo;
partinfo = gtk_ctree_node_get_row_data(ctree, node);
2001-12-15 19:30:33 +01:00
if (partinfo &&
(partinfo->mime_type == MIME_TEXT ||
2002-03-02 11:02:42 +01:00
partinfo->mime_type == MIME_TEXT_HTML))
2001-09-20 13:53:09 +02:00
break;
}
2002-03-02 11:02:42 +01:00
textview_show_message(mimeview->messageview->textview, mimeinfo, file);
2002-02-25 10:25:17 +01:00
2001-09-20 13:53:09 +02:00
if (!node)
node = GTK_CTREE_NODE(GTK_CLIST(ctree)->row_list);
if (node) {
2001-04-19 14:21:46 +02:00
gtk_ctree_select(ctree, node);
icon_list_toggle_by_mime_info
(mimeview, gtk_ctree_node_get_row_data(ctree, node));
2001-09-20 13:53:09 +02:00
gtkut_ctree_set_focus_row(ctree, node);
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mimeview->mime_toggle)))
gtk_widget_grab_focus(mimeview->ctree);
2001-04-19 14:21:46 +02:00
}
}
void mimeview_destroy(MimeView *mimeview)
{
GSList *cur;
for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
MimeViewer *viewer = (MimeViewer *) cur->data;
gtk_container_remove(GTK_CONTAINER(mimeview->mime_notebook),
GTK_WIDGET(viewer->get_widget(viewer)));
viewer->destroy_viewer(viewer);
}
g_slist_free(mimeview->viewers);
gtk_target_list_unref(mimeview->target_list);
2001-04-19 14:21:46 +02:00
procmime_mimeinfo_free_all(mimeview->mimeinfo);
g_free(mimeview->file);
g_free(mimeview);
mimeviews = g_slist_remove(mimeviews, mimeview);
2001-04-19 14:21:46 +02:00
}
2003-04-22 15:53:19 +02:00
MimeInfo *mimeview_get_selected_part(MimeView *mimeview)
{
if (gtk_notebook_get_current_page
(GTK_NOTEBOOK(mimeview->notebook)) == 0)
return NULL;
return gtk_ctree_node_get_row_data
(GTK_CTREE(mimeview->ctree), mimeview->opened);
}
2001-04-19 14:21:46 +02:00
static void mimeview_set_multipart_tree(MimeView *mimeview,
MimeInfo *mimeinfo,
GtkCTreeNode *parent)
{
2003-05-07 09:36:15 +02:00
GtkCTreeNode *node;
2001-12-22 17:09:08 +01:00
2001-04-19 14:21:46 +02:00
g_return_if_fail(mimeinfo != NULL);
2001-12-22 17:09:08 +01:00
if (mimeinfo->children)
2003-05-07 09:36:15 +02:00
mimeinfo = mimeinfo->children;
while (mimeinfo != NULL) {
node = mimeview_append_part(mimeview, mimeinfo, parent);
if (mimeinfo->children)
mimeview_set_multipart_tree(mimeview, mimeinfo, node);
else if (mimeinfo->sub &&
mimeinfo->sub->mime_type != MIME_TEXT &&
mimeinfo->sub->mime_type != MIME_TEXT_HTML)
mimeview_set_multipart_tree(mimeview, mimeinfo->sub,
node);
mimeinfo = mimeinfo->next;
}
2001-04-19 14:21:46 +02:00
}
static gchar *get_part_name(MimeInfo *partinfo)
{
#if USE_GPGME
if (partinfo->sigstatus)
return partinfo->sigstatus;
else
#endif
if (partinfo->name)
return partinfo->name;
else if (partinfo->filename)
return partinfo->filename;
else if (partinfo->description)
return partinfo->description;
else
return "";
}
static gchar *get_part_description(MimeInfo *partinfo)
{
if (partinfo->description)
return partinfo->description;
else if (partinfo->name)
return partinfo->name;
else if (partinfo->filename)
return partinfo->filename;
2001-04-19 14:21:46 +02:00
else
return "";
}
static GtkCTreeNode *mimeview_append_part(MimeView *mimeview,
MimeInfo *partinfo,
GtkCTreeNode *parent)
{
GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
GtkCTreeNode *node;
gchar *str[N_MIMEVIEW_COLS];
str[COL_MIMETYPE] =
partinfo->content_type ? partinfo->content_type : "";
str[COL_SIZE] = to_human_readable(partinfo->size);
if (prefs_common.attach_desc)
str[COL_NAME] = get_part_description(partinfo);
else
str[COL_NAME] = get_part_name(partinfo);
2001-04-19 14:21:46 +02:00
node = gtk_ctree_insert_node(ctree, parent, NULL, str, 0,
NULL, NULL, NULL, NULL,
FALSE, TRUE);
gtk_ctree_node_set_row_data(ctree, node, partinfo);
return node;
}
static void mimeview_show_message_part(MimeView *mimeview, MimeInfo *partinfo)
{
FILE *fp;
const gchar *fname;
#if USE_GPGME
MimeInfo *pi;
#endif
if (!partinfo) return;
#if USE_GPGME
for (pi = partinfo; pi && !pi->plaintextfile ; pi = pi->parent)
;
fname = pi ? pi->plaintextfile : mimeview->file;
#else
fname = mimeview->file;
#endif /* USE_GPGME */
if (!fname) return;
2002-03-14 11:17:32 +01:00
if ((fp = fopen(fname, "rb")) == NULL) {
2001-04-19 14:21:46 +02:00
FILE_OP_ERROR(fname, "fopen");
return;
}
if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) {
FILE_OP_ERROR(mimeview->file, "fseek");
fclose(fp);
return;
}
mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
textview_show_part(mimeview->textview, partinfo, fp);
fclose(fp);
}
static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *content_type)
{
GSList *cur;
MimeViewerFactory *factory = NULL;
MimeViewer *viewer = NULL;
/*
* FNM_CASEFOLD is a GNU extension
* if its not defined copy the string to the stack and
* convert the copy to lower case
*/
#ifndef FNM_CASEFOLD
#define FNM_CASEFOLD 0
Xstrdup_a(content_type, content_type, return NULL);
g_strdown((gchar *)content_type);
#endif
for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
MimeViewerFactory *curfactory = cur->data;
gint i = 0;
while (curfactory->content_types[i] != NULL) {
debug_print("%s\n", curfactory->content_types[i]);
if(!fnmatch(curfactory->content_types[i], content_type, FNM_CASEFOLD)) {
factory = curfactory;
break;
}
i++;
}
if (factory != NULL)
break;
}
if (factory == NULL)
return NULL;
for (cur = mimeview->viewers; cur != NULL; cur = g_slist_next(cur)) {
MimeViewer *curviewer = cur->data;
if (curviewer->factory == factory)
return curviewer;
}
viewer = factory->create_viewer();
gtk_container_add(GTK_CONTAINER(mimeview->mime_notebook),
GTK_WIDGET(viewer->get_widget(viewer)));
mimeview->viewers = g_slist_append(mimeview->viewers, viewer);
return viewer;
}
static MimeViewer *get_viewer_for_mimeinfo(MimeView *mimeview, MimeInfo *partinfo)
{
gchar *content_type = NULL;
MimeViewer *viewer = NULL;
if ((partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM) &&
(partinfo->name != NULL)) {
content_type = procmime_get_mime_type(partinfo->name);
} else {
content_type = g_strdup(partinfo->content_type);
}
if (content_type != NULL) {
viewer = get_viewer_for_content_type(mimeview, content_type);
g_free(content_type);
}
return viewer;
}
static gboolean mimeview_show_part(MimeView *mimeview, MimeInfo *partinfo)
{
MimeViewer *viewer;
viewer = get_viewer_for_mimeinfo(mimeview, partinfo);
if (viewer == NULL) {
if (mimeview->mimeviewer != NULL)
mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
mimeview->mimeviewer = NULL;
return FALSE;
}
if (mimeview->mimeviewer != viewer) {
if (mimeview->mimeviewer != NULL)
mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
mimeview->mimeviewer = viewer;
mimeview_change_view_type(mimeview, MIMEVIEW_VIEWER);
}
viewer->show_mimepart(viewer, mimeview->file, partinfo);
2003-10-05 12:10:30 +02:00
#warning FIXME_GTK2 Is it correct?
return FALSE;
}
2001-04-19 14:21:46 +02:00
static void mimeview_change_view_type(MimeView *mimeview, MimeViewType type)
{
TextView *textview = mimeview->textview;
if ((mimeview->type != MIMEVIEW_VIEWER) &&
(mimeview->type == type)) return;
2001-04-19 14:21:46 +02:00
switch (type) {
case MIMEVIEW_TEXT:
gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
GTK_WIDGET_PTR(textview)));
2001-04-19 14:21:46 +02:00
break;
case MIMEVIEW_VIEWER:
gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->mime_notebook),
gtk_notebook_page_num(GTK_NOTEBOOK(mimeview->mime_notebook),
GTK_WIDGET(mimeview->mimeviewer->get_widget(mimeview->mimeviewer))));
break;
2001-04-19 14:21:46 +02:00
default:
return;
}
mimeview->type = type;
}
static void mimeview_clear(MimeView *mimeview)
{
GtkCList *clist = GTK_CLIST(mimeview->ctree);
procmime_mimeinfo_free_all(mimeview->mimeinfo);
mimeview->mimeinfo = NULL;
gtk_clist_clear(clist);
textview_clear(mimeview->textview);
if (mimeview->mimeviewer != NULL)
mimeview->mimeviewer->clear_viewer(mimeview->mimeviewer);
2001-04-19 14:21:46 +02:00
mimeview->opened = NULL;
g_free(mimeview->file);
mimeview->file = NULL;
icon_list_clear(mimeview);
2002-02-25 10:25:17 +01:00
if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(mimeview->mime_toggle)))
gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0);
2001-04-19 14:21:46 +02:00
}
static void mimeview_selected(GtkCTree *ctree, GtkCTreeNode *node, gint column,
MimeView *mimeview)
{
MimeInfo *partinfo;
if (mimeview->opened == node) return;
mimeview->opened = node;
gtk_ctree_node_moveto(ctree, node, -1, 0.5, 0);
partinfo = gtk_ctree_node_get_row_data(ctree, node);
if (!partinfo) return;
2001-06-03 12:34:31 +02:00
/* ungrab the mouse event */
if (GTK_WIDGET_HAS_GRAB(ctree)) {
gtk_grab_remove(GTK_WIDGET(ctree));
if (gdk_pointer_is_grabbed())
gdk_pointer_ungrab(GDK_CURRENT_TIME);
}
mimeview->textview->default_text = FALSE;
if (!mimeview_show_part(mimeview, partinfo)) {
switch (partinfo->mime_type) {
case MIME_TEXT:
case MIME_TEXT_HTML:
case MIME_TEXT_ENRICHED:
case MIME_MESSAGE_RFC822:
case MIME_MULTIPART:
mimeview_show_message_part(mimeview, partinfo);
break;
default:
mimeview->textview->default_text = TRUE;
mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
2001-04-19 14:21:46 +02:00
#if USE_GPGME
if (g_strcasecmp(partinfo->content_type,
"application/pgp-signature") == 0)
textview_show_signature_part(mimeview->textview,
partinfo);
else
2001-04-19 14:21:46 +02:00
#endif
textview_show_mime_part(mimeview->textview, partinfo);
break;
}
2001-04-19 14:21:46 +02:00
}
}
2002-02-25 10:25:17 +01:00
static void mimeview_start_drag(GtkWidget *widget, gint button,
GdkEvent *event, MimeView *mimeview)
2001-04-19 14:21:46 +02:00
{
GdkDragContext *context;
MimeInfo *partinfo;
g_return_if_fail(mimeview != NULL);
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
2001-04-19 14:21:46 +02:00
if (partinfo->filename == NULL && partinfo->name == NULL) return;
context = gtk_drag_begin(widget, mimeview->target_list,
2001-04-19 14:21:46 +02:00
GDK_ACTION_COPY, button, event);
gtk_drag_set_icon_default(context);
}
2002-01-29 11:03:33 +01:00
static gint mimeview_button_pressed(GtkWidget *widget, GdkEventButton *event,
2001-04-19 14:21:46 +02:00
MimeView *mimeview)
{
GtkCList *clist = GTK_CLIST(widget);
gint row, column;
2002-01-29 11:03:33 +01:00
if (!event) return FALSE;
2001-04-19 14:21:46 +02:00
if (event->button == 2 || event->button == 3) {
if (!gtk_clist_get_selection_info(clist, event->x, event->y,
&row, &column))
2002-01-29 11:03:33 +01:00
return FALSE;
2001-04-19 14:21:46 +02:00
gtk_clist_unselect_all(clist);
gtk_clist_select_row(clist, row, column);
gtkut_clist_set_focus_row(clist, row);
}
part_button_pressed(mimeview, event, mimeview_get_selected_part(mimeview));
return TRUE;
}
2001-04-19 14:21:46 +02:00
2003-10-05 12:25:17 +02:00
static gboolean part_button_pressed(MimeView *mimeview, GdkEventButton *event,
MimeInfo *partinfo)
{
2001-04-19 14:21:46 +02:00
if (event->button == 2 ||
(event->button == 1 && event->type == GDK_2BUTTON_PRESS)) {
/* call external program for image, audio or html */
mimeview_launch(mimeview);
2003-10-05 12:25:17 +02:00
return TRUE;
2001-04-19 14:21:46 +02:00
} else if (event->button == 3) {
if (partinfo && (partinfo->mime_type == MIME_TEXT ||
partinfo->mime_type == MIME_TEXT_HTML ||
2001-11-28 23:54:26 +01:00
partinfo->mime_type == MIME_TEXT_ENRICHED ||
2001-04-19 14:21:46 +02:00
partinfo->mime_type == MIME_MESSAGE_RFC822 ||
partinfo->mime_type == MIME_IMAGE ||
partinfo->mime_type == MIME_MULTIPART))
menu_set_sensitive(mimeview->popupfactory,
"/Display as text", FALSE);
else
menu_set_sensitive(mimeview->popupfactory,
"/Display as text", TRUE);
2001-08-01 09:22:54 +02:00
if (partinfo &&
partinfo->mime_type == MIME_APPLICATION_OCTET_STREAM)
menu_set_sensitive(mimeview->popupfactory,
"/Open", FALSE);
else
menu_set_sensitive(mimeview->popupfactory,
"/Open", TRUE);
2001-04-19 14:21:46 +02:00
#if USE_GPGME
menu_set_sensitive(mimeview->popupfactory,
"/Check signature",
mimeview_is_signed(mimeview));
#endif
gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo", partinfo);
2001-04-19 14:21:46 +02:00
gtk_menu_popup(GTK_MENU(mimeview->popupmenu),
NULL, NULL, NULL, NULL,
event->button, event->time);
2003-10-05 12:25:17 +02:00
return TRUE;
2001-04-19 14:21:46 +02:00
}
2002-01-29 11:03:33 +01:00
2003-10-05 12:25:17 +02:00
return FALSE;
2002-01-29 11:03:33 +01:00
}
2002-01-29 11:03:33 +01:00
void mimeview_pass_key_press_event(MimeView *mimeview, GdkEventKey *event)
{
mimeview_key_pressed(mimeview->ctree, event, mimeview);
2001-04-19 14:21:46 +02:00
}
#define BREAK_ON_MODIFIER_KEY() \
if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) != 0) break
2003-10-05 12:10:30 +02:00
#warning FIXME_GTK2
#if 0
2002-01-29 11:03:33 +01:00
#define KEY_PRESS_EVENT_STOP() \
if (gtk_signal_n_emissions_by_name \
(GTK_OBJECT(ctree), "key_press_event") > 0) { \
gtk_signal_emit_stop_by_name(GTK_OBJECT(ctree), \
"key_press_event"); \
}
2003-10-05 12:10:30 +02:00
#else
#define KEY_PRESS_EVENT_STOP()
#endif
2002-01-29 11:03:33 +01:00
static gint mimeview_key_pressed(GtkWidget *widget, GdkEventKey *event,
2001-04-19 14:21:46 +02:00
MimeView *mimeview)
{
SummaryView *summaryview;
2002-01-29 11:03:33 +01:00
GtkCTree *ctree = GTK_CTREE(widget);
2001-04-19 14:21:46 +02:00
GtkCTreeNode *node;
2002-01-29 11:03:33 +01:00
if (!event) return FALSE;
if (!mimeview->opened) return FALSE;
2001-04-19 14:21:46 +02:00
switch (event->keyval) {
case GDK_space:
2002-01-29 11:03:33 +01:00
if (textview_scroll_page(mimeview->textview, FALSE))
return TRUE;
2001-04-19 14:21:46 +02:00
node = GTK_CTREE_NODE_NEXT(mimeview->opened);
if (node) {
gtk_sctree_unselect_all(GTK_SCTREE(ctree));
gtk_sctree_select(GTK_SCTREE(ctree), node);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
}
break;
case GDK_BackSpace:
textview_scroll_page(mimeview->textview, TRUE);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
case GDK_Return:
textview_scroll_one_line(mimeview->textview,
(event->state & GDK_MOD1_MASK) != 0);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
case GDK_n:
case GDK_N:
BREAK_ON_MODIFIER_KEY();
if (!GTK_CTREE_NODE_NEXT(mimeview->opened)) break;
2002-01-29 11:03:33 +01:00
KEY_PRESS_EVENT_STOP();
2003-10-05 12:10:30 +02:00
g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
2001-04-19 14:21:46 +02:00
GTK_SCROLL_STEP_FORWARD, 0.0);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
case GDK_p:
case GDK_P:
BREAK_ON_MODIFIER_KEY();
if (!GTK_CTREE_NODE_PREV(mimeview->opened)) break;
2002-01-29 11:03:33 +01:00
KEY_PRESS_EVENT_STOP();
2003-10-05 12:10:30 +02:00
g_signal_emit_by_name(G_OBJECT(ctree), "scroll_vertical",
2001-04-19 14:21:46 +02:00
GTK_SCROLL_STEP_BACKWARD, 0.0);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
case GDK_y:
BREAK_ON_MODIFIER_KEY();
2002-01-29 11:03:33 +01:00
KEY_PRESS_EVENT_STOP();
2001-04-19 14:21:46 +02:00
mimeview_save_as(mimeview);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
case GDK_t:
BREAK_ON_MODIFIER_KEY();
2002-01-29 11:03:33 +01:00
KEY_PRESS_EVENT_STOP();
2001-04-19 14:21:46 +02:00
mimeview_display_as_text(mimeview);
return TRUE;
2001-04-19 14:21:46 +02:00
case GDK_l:
BREAK_ON_MODIFIER_KEY();
2002-01-29 11:03:33 +01:00
KEY_PRESS_EVENT_STOP();
2001-04-19 14:21:46 +02:00
mimeview_launch(mimeview);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
default:
2002-03-07 14:22:39 +01:00
break;
2001-04-19 14:21:46 +02:00
}
2002-01-29 11:03:33 +01:00
if (!mimeview->messageview->mainwin) return FALSE;
2001-04-19 14:21:46 +02:00
summaryview = mimeview->messageview->mainwin->summaryview;
summary_pass_key_press_event(summaryview, event);
2002-01-29 11:03:33 +01:00
return TRUE;
2001-04-19 14:21:46 +02:00
}
static void mimeview_drag_data_get(GtkWidget *widget,
GdkDragContext *drag_context,
GtkSelectionData *selection_data,
guint info,
guint time,
MimeView *mimeview)
{
gchar *filename, *uriname;
2003-10-05 12:10:30 +02:00
const gchar *bname;
2001-04-19 14:21:46 +02:00
MimeInfo *partinfo;
if (!mimeview->opened) return;
if (!mimeview->file) return;
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
2001-04-19 14:21:46 +02:00
if (!partinfo) return;
if (!partinfo->filename && !partinfo->name) return;
filename = partinfo->filename ? partinfo->filename : partinfo->name;
2003-10-05 12:10:30 +02:00
bname = g_basename(filename);
if (*bname == '\0') return;
2001-04-19 14:21:46 +02:00
filename = g_strconcat(get_mime_tmp_dir(), G_DIR_SEPARATOR_S,
2003-10-05 12:10:30 +02:00
bname, NULL);
2001-04-19 14:21:46 +02:00
if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
alertpanel_error
(_("Can't save the part of multipart message."));
uriname = g_strconcat("file:/", filename, NULL);
gtk_selection_data_set(selection_data, selection_data->target, 8,
uriname, strlen(uriname));
g_free(uriname);
g_free(filename);
}
static void mimeview_save_all(MimeView *mimeview)
{
gchar *dirname;
gchar *defname = NULL;
MimeInfo *partinfo;
MimeInfo *attachment;
gchar buf[1024];
if (!mimeview->opened) return;
if (!mimeview->file) return;
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
g_return_if_fail(partinfo != NULL);
dirname = filesel_select_file(_("Save as"), defname);
if (!dirname) return;
/* return to first children */
2003-02-22 12:47:14 +01:00
if (!partinfo->parent->children) return; /* multipart container? */
attachment = partinfo->parent->children->next;
/* for each attachment, extract it in the selected dir. */
while (attachment != NULL) {
2002-08-06 00:46:46 +02:00
static guint subst_cnt = 1;
gchar *attachdir;
2003-02-22 12:47:14 +01:00
gchar *attachname = g_strdup(get_part_name(attachment));
AlertValue aval = G_ALERTDEFAULT;
gchar *res;
2002-08-06 00:46:46 +02:00
2003-02-22 12:47:14 +01:00
if (!attachname || !strlen(attachname))
attachname = g_strdup_printf("noname.%d",subst_cnt++);
2002-08-06 00:46:46 +02:00
subst_chars(attachname, ":?*&|<>\t\r\n", '_');
g_snprintf(buf, sizeof(buf), "%s%s",
dirname,
(attachname[0] == G_DIR_SEPARATOR)
? &attachname[1]
: attachname);
2003-02-22 12:47:14 +01:00
subst_chars(buf, "/\\", G_DIR_SEPARATOR);
2002-08-06 00:46:46 +02:00
attachdir = g_dirname(buf);
make_dir_hier(attachdir);
g_free(attachdir);
if (is_file_exist(buf)) {
res = g_strdup_printf(_("Overwrite existing file '%s'?"),
attachname);
aval = alertpanel(_("Overwrite"), res, _("OK"),
_("Cancel"), NULL);
g_free(res);
}
g_free(attachname);
2003-02-22 12:47:14 +01:00
if ((G_ALERTDEFAULT != aval) || (procmime_get_part(buf, mimeview->file, attachment) < 0))
2002-08-06 00:46:46 +02:00
alertpanel_error(_("Can't save the part of multipart message."));
attachment = attachment->next;
}
}
2001-04-19 14:21:46 +02:00
static void mimeview_display_as_text(MimeView *mimeview)
{
MimeInfo *partinfo;
if (!mimeview->opened) return;
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
if (!partinfo) {
partinfo = (MimeInfo *) gtk_object_get_data
(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo");
gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo", NULL);
}
2003-04-22 15:53:19 +02:00
g_return_if_fail(partinfo != NULL);
2001-04-19 14:21:46 +02:00
mimeview_show_message_part(mimeview, partinfo);
}
static void mimeview_save_as(MimeView *mimeview)
{
gchar *filename;
2002-01-16 12:48:36 +01:00
gchar *defname = NULL;
2001-04-19 14:21:46 +02:00
MimeInfo *partinfo;
gchar *res;
2001-04-19 14:21:46 +02:00
if (!mimeview->opened) return;
if (!mimeview->file) return;
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
if (!partinfo) {
partinfo = (MimeInfo *) gtk_object_get_data
(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo");
gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo", NULL);
}
2001-04-19 14:21:46 +02:00
g_return_if_fail(partinfo != NULL);
2002-01-16 12:48:36 +01:00
if (partinfo->filename)
defname = partinfo->filename;
else if (partinfo->name) {
Xstrdup_a(defname, partinfo->name, return);
subst_for_filename(defname);
}
filename = filesel_select_file(_("Save as"), defname);
2001-04-19 14:21:46 +02:00
if (!filename) return;
if (is_file_exist(filename)) {
AlertValue aval;
res = g_strdup_printf(_("Overwrite existing file '%s'?"),
filename);
aval = alertpanel(_("Overwrite"), res, _("OK"),
_("Cancel"), NULL);
g_free(res);
2001-04-19 14:21:46 +02:00
if (G_ALERTDEFAULT != aval) return;
}
if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
alertpanel_error
(_("Can't save the part of multipart message."));
}
static void mimeview_launch(MimeView *mimeview)
{
MimeInfo *partinfo;
gchar *filename;
if (!mimeview->opened) return;
if (!mimeview->file) return;
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
if (!partinfo) {
partinfo = (MimeInfo *) gtk_object_get_data
(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo");
gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo", NULL);
}
2001-04-19 14:21:46 +02:00
g_return_if_fail(partinfo != NULL);
filename = procmime_get_tmp_file_name(partinfo);
if (procmime_get_part(filename, mimeview->file, partinfo) < 0)
alertpanel_error
(_("Can't save the part of multipart message."));
else
2001-05-10 13:19:38 +02:00
mimeview_view_file(filename, partinfo, NULL);
2001-04-19 14:21:46 +02:00
g_free(filename);
}
2001-05-10 13:19:38 +02:00
static void mimeview_open_with(MimeView *mimeview)
{
MimeInfo *partinfo;
gchar *filename;
2001-11-07 11:29:45 +01:00
gchar *cmd;
2001-05-10 13:19:38 +02:00
if (!mimeview->opened) return;
if (!mimeview->file) return;
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
if (!partinfo) {
partinfo = (MimeInfo *) gtk_object_get_data
(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo");
gtk_object_set_data(GTK_OBJECT(mimeview->popupmenu),
"pop_partinfo", NULL);
}
2001-05-10 13:19:38 +02:00
g_return_if_fail(partinfo != NULL);
filename = procmime_get_tmp_file_name(partinfo);
2001-11-07 11:29:45 +01:00
if (procmime_get_part(filename, mimeview->file, partinfo) < 0) {
2001-05-10 13:19:38 +02:00
alertpanel_error
(_("Can't save the part of multipart message."));
2001-11-07 11:29:45 +01:00
g_free(filename);
return;
}
if (!prefs_common.mime_open_cmd_history)
prefs_common.mime_open_cmd_history =
add_history(NULL, prefs_common.mime_open_cmd);
cmd = input_dialog_combo
(_("Open with"),
_("Enter the command line to open file:\n"
"(`%s' will be replaced with file name)"),
prefs_common.mime_open_cmd,
2002-04-21 11:24:27 +02:00
prefs_common.mime_open_cmd_history,
TRUE);
2001-11-07 11:29:45 +01:00
if (cmd) {
mimeview_view_file(filename, partinfo, cmd);
g_free(prefs_common.mime_open_cmd);
prefs_common.mime_open_cmd = cmd;
prefs_common.mime_open_cmd_history =
add_history(prefs_common.mime_open_cmd_history, cmd);
2001-05-10 13:19:38 +02:00
}
g_free(filename);
}
static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
const gchar *cmdline)
2001-04-19 14:21:46 +02:00
{
static gchar *default_image_cmdline = "display '%s'";
static gchar *default_audio_cmdline = "play '%s'";
2002-12-20 08:33:23 +01:00
static gchar *default_html_cmdline = DEFAULT_BROWSER_CMD;
2001-08-01 09:22:54 +02:00
static gchar *mime_cmdline = "metamail -d -b -x -c %s '%s'";
2001-04-19 14:21:46 +02:00
gchar buf[1024];
gchar m_buf[1024];
2001-05-10 13:19:38 +02:00
const gchar *cmd;
const gchar *def_cmd;
const gchar *p;
2001-04-19 14:21:46 +02:00
2001-05-10 13:19:38 +02:00
if (cmdline) {
cmd = cmdline;
def_cmd = NULL;
} else if (MIME_APPLICATION_OCTET_STREAM == partinfo->mime_type) {
2001-04-19 14:21:46 +02:00
return;
} else if (MIME_IMAGE == partinfo->mime_type) {
cmd = prefs_common.mime_image_viewer;
def_cmd = default_image_cmdline;
} else if (MIME_AUDIO == partinfo->mime_type) {
cmd = prefs_common.mime_audio_player;
def_cmd = default_audio_cmdline;
} else if (MIME_TEXT_HTML == partinfo->mime_type) {
cmd = prefs_common.uri_cmd;
def_cmd = default_html_cmdline;
} else {
g_snprintf(m_buf, sizeof(m_buf), mime_cmdline,
partinfo->content_type, "%s");
cmd = m_buf;
def_cmd = NULL;
}
if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
!strchr(p + 2, '%'))
g_snprintf(buf, sizeof(buf), cmd, filename);
else {
if (cmd)
g_warning("MIME viewer command line is invalid: `%s'", cmd);
2001-04-19 14:21:46 +02:00
if (def_cmd)
g_snprintf(buf, sizeof(buf), def_cmd, filename);
else
return;
}
2001-09-13 15:38:32 +02:00
execute_command_line(buf, TRUE);
2001-04-19 14:21:46 +02:00
}
#if USE_GPGME
static void update_node_name(GtkCTree *ctree, GtkCTreeNode *node,
gpointer data)
{
MimeInfo *partinfo;
gchar *part_name;
partinfo = gtk_ctree_node_get_row_data(ctree, node);
g_return_if_fail(partinfo != NULL);
part_name = get_part_name(partinfo);
gtk_ctree_node_set_text(ctree, node, COL_NAME, part_name);
}
static void mimeview_update_names(MimeView *mimeview)
{
GtkCTree *ctree = GTK_CTREE(mimeview->ctree);
gtk_ctree_pre_recursive(ctree, NULL, update_node_name, NULL);
}
static void mimeview_update_signature_info(MimeView *mimeview)
{
MimeInfo *partinfo;
if (!mimeview) return;
if (!mimeview->opened) return;
2003-04-22 15:53:19 +02:00
partinfo = mimeview_get_selected_part(mimeview);
2001-04-19 14:21:46 +02:00
if (!partinfo) return;
if (g_strcasecmp(partinfo->content_type,
"application/pgp-signature") == 0) {
mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
textview_show_signature_part(mimeview->textview, partinfo);
}
}
void mimeview_check_signature(MimeView *mimeview)
2001-04-19 14:21:46 +02:00
{
MimeInfo *mimeinfo;
FILE *fp;
g_return_if_fail (mimeview_is_signed(mimeview));
2002-11-03 21:37:45 +01:00
g_return_if_fail (gpg_started);
2001-04-19 14:21:46 +02:00
mimeinfo = gtk_ctree_node_get_row_data
(GTK_CTREE(mimeview->ctree), mimeview->opened);
2001-04-19 14:21:46 +02:00
g_return_if_fail(mimeinfo != NULL);
g_return_if_fail(mimeview->file != NULL);
while (mimeinfo->parent)
mimeinfo = mimeinfo->parent;
2002-03-14 11:17:32 +01:00
if ((fp = fopen(mimeview->file, "rb")) == NULL) {
2001-04-19 14:21:46 +02:00
FILE_OP_ERROR(mimeview->file, "fopen");
return;
}
rfc2015_check_signature(mimeinfo, fp);
fclose(fp);
mimeview_update_names(mimeview);
mimeview_update_signature_info(mimeview);
2002-08-06 11:34:13 +02:00
textview_show_message(mimeview->messageview->textview, mimeinfo,
mimeview->file);
2003-07-20 11:14:33 +02:00
2003-07-20 11:24:12 +02:00
mimeview_update(mimeview);
2001-04-19 14:21:46 +02:00
}
2001-04-27 22:27:24 +02:00
#endif /* USE_GPGME */
void mimeview_register_viewer_factory(MimeViewerFactory *factory)
{
mimeviewer_factories = g_slist_append(mimeviewer_factories, factory);
}
static gint cmp_viewer_by_factroy(gconstpointer a, gconstpointer b)
{
return ((MimeViewer *) a)->factory == (MimeViewerFactory *) b ? 0 : -1;
}
void mimeview_unregister_viewer_factory(MimeViewerFactory *factory)
{
GSList *mimeview_list, *viewer_list;
for (mimeview_list = mimeviews; mimeview_list != NULL; mimeview_list = g_slist_next(mimeview_list)) {
MimeView *mimeview = (MimeView *) mimeview_list->data;
if (mimeview->mimeviewer && mimeview->mimeviewer->factory == factory) {
mimeview_change_view_type(mimeview, MIMEVIEW_TEXT);
mimeview->mimeviewer = NULL;
}
while ((viewer_list = g_slist_find_custom(mimeview->viewers, factory, cmp_viewer_by_factroy)) != NULL) {
MimeViewer *mimeviewer = (MimeViewer *) viewer_list->data;
mimeviewer->destroy_viewer(mimeviewer);
mimeview->viewers = g_slist_remove(mimeview->viewers, mimeviewer);
}
}
mimeviewer_factories = g_slist_remove(mimeviewer_factories, factory);
}
static gboolean icon_clicked_cb (GtkWidget *button, GdkEventButton *event, MimeView *mimeview)
{
gint num;
MimeInfo *partinfo;
num = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
if (event->button == 1) {
icon_selected(mimeview, num, partinfo);
gtk_widget_grab_focus(button);
if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button)))
toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
else
gtk_signal_emit_stop_by_name(GTK_OBJECT(button), "button_press_event");
}
part_button_pressed(mimeview, event, partinfo);
2003-10-05 12:25:17 +02:00
return FALSE;
}
static void icon_selected (MimeView *mimeview, gint num, MimeInfo *partinfo)
{
if (num == 1 && (partinfo->mime_type == MIME_TEXT ||
partinfo->mime_type == MIME_TEXT_HTML)) {
gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 0);
/* don't set the ctree, as it will unload the plugin, and
* we want to be able to switch quickly between the text
* part and the attachment */
} else {
GtkCTreeNode *node;
gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 1);
node = gtk_ctree_find_by_row_data(GTK_CTREE(mimeview->ctree), NULL, partinfo);
if (node)
gtk_ctree_select(GTK_CTREE(mimeview->ctree), node);
}
}
2003-10-05 12:10:30 +02:00
#warning FIXME_GTK2
#if 0
#undef KEY_PRESS_EVENT_STOP
#define KEY_PRESS_EVENT_STOP() \
if (gtk_signal_n_emissions_by_name \
(GTK_OBJECT(button), "key_press_event") > 0) { \
gtk_signal_emit_stop_by_name(GTK_OBJECT(button), \
"key_press_event"); \
}
2003-10-05 12:10:30 +02:00
#endif
static gint icon_key_pressed(GtkWidget *button, GdkEventKey *event,
MimeView *mimeview)
{
gint num;
MimeInfo *partinfo;
SummaryView *summaryview;
TextView *textview;
num = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(button), "icon_number"));
partinfo = gtk_object_get_data(GTK_OBJECT(button), "partinfo");
if (!event) return FALSE;
if (gtk_notebook_get_current_page(GTK_NOTEBOOK(mimeview->notebook)) == 0)
textview = mimeview->messageview->textview;
else
textview = mimeview->textview;
switch (event->keyval) {
case GDK_space:
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
/* stop the button being untoggled */
KEY_PRESS_EVENT_STOP();
if (textview_scroll_page(textview, FALSE))
return TRUE;
if (icon_list_select_by_number(mimeview, num + 1))
return TRUE;
} else {
icon_selected(mimeview, num, partinfo);
toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
return TRUE;
}
break;
case GDK_BackSpace:
textview_scroll_page(textview, TRUE);
return TRUE;
case GDK_Return:
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
KEY_PRESS_EVENT_STOP();
textview_scroll_one_line(textview,
(event->state & GDK_MOD1_MASK) != 0);
return TRUE;
} else {
icon_selected(mimeview, num, partinfo);
toggle_icon(GTK_TOGGLE_BUTTON(button), mimeview);
return TRUE;
}
case GDK_n:
case GDK_N:
BREAK_ON_MODIFIER_KEY();
if (icon_list_select_by_number(mimeview, num + 1))
KEY_PRESS_EVENT_STOP();
return TRUE;
break;
case GDK_p:
case GDK_P:
BREAK_ON_MODIFIER_KEY();
if (icon_list_select_by_number(mimeview, num - 1))
KEY_PRESS_EVENT_STOP();
return TRUE;
break;
case GDK_y:
BREAK_ON_MODIFIER_KEY();
if (gtk_notebook_get_current_page(GTK_NOTEBOOK(mimeview->notebook)) == 0)
break;
KEY_PRESS_EVENT_STOP();
mimeview_save_as(mimeview);
return TRUE;
case GDK_t:
BREAK_ON_MODIFIER_KEY();
if (gtk_notebook_get_current_page(GTK_NOTEBOOK(mimeview->notebook)) == 0)
break;
KEY_PRESS_EVENT_STOP();
mimeview_display_as_text(mimeview);
return TRUE;
case GDK_l:
BREAK_ON_MODIFIER_KEY();
if (gtk_notebook_get_current_page(GTK_NOTEBOOK(mimeview->notebook)) == 0)
break;
KEY_PRESS_EVENT_STOP();
mimeview_launch(mimeview);
return TRUE;
default:
break;
}
if (!mimeview->messageview->mainwin) return FALSE;
summaryview = mimeview->messageview->mainwin->summaryview;
summary_pass_key_press_event(summaryview, event);
return TRUE;
}
static void toggle_icon(GtkToggleButton *button, MimeView *mimeview)
{
GList *child;
2003-10-05 12:10:30 +02:00
child = gtk_container_get_children(GTK_CONTAINER(mimeview->icon_vbox));
for (; child != NULL; child = g_list_next(child)) {
if (GTK_IS_TOGGLE_BUTTON(child->data) &&
GTK_TOGGLE_BUTTON(child->data) != button &&
gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(child->data)))
gtk_toggle_button_set_active
(GTK_TOGGLE_BUTTON(child->data),
FALSE);
}
}
static void icon_list_append_icon (MimeView *mimeview, MimeInfo *mimeinfo)
{
GtkWidget *pixmap;
GtkWidget *vbox;
GtkWidget *button;
gchar *tip;
gchar *desc = NULL;
StockPixmap stockp;
vbox = mimeview->icon_vbox;
mimeview->icon_count++;
button = gtk_toggle_button_new();
gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
gtk_object_set_data(GTK_OBJECT(button), "icon_number",
GINT_TO_POINTER(mimeview->icon_count));
gtk_object_set_data(GTK_OBJECT(button), "partinfo",
mimeinfo);
switch (mimeinfo->mime_type) {
case MIME_TEXT:
case MIME_MESSAGE_RFC822:
stockp = STOCK_PIXMAP_MIME_TEXT_PLAIN;
break;
case MIME_TEXT_HTML:
stockp = STOCK_PIXMAP_MIME_TEXT_HTML;
break;
case MIME_APPLICATION:
#ifdef USE_GPGME
if (mimeinfo->content_type
&& g_strcasecmp(mimeinfo->content_type,
"application/pgp-signature") == 0) {
if (mimeinfo->sigstatus_full) {
desc = mimeinfo->sigstatus;
if (mimeinfo->sig_ok)
stockp = STOCK_PIXMAP_MIME_GPG_PASSED;
else if (mimeinfo->sig_unknown)
stockp = STOCK_PIXMAP_MIME_GPG_UNKNOWN;
else if (mimeinfo->sig_expired
|| mimeinfo->key_expired)
stockp = STOCK_PIXMAP_MIME_GPG_EXPIRED;
else
stockp = STOCK_PIXMAP_MIME_GPG_FAILED;
} else
stockp = STOCK_PIXMAP_MIME_GPG_SIGNED;
} else
#endif
stockp = STOCK_PIXMAP_MIME_APPLICATION;
break;
case MIME_APPLICATION_OCTET_STREAM:
stockp = STOCK_PIXMAP_MIME_APPLICATION_OCTET_STREAM;
break;
case MIME_IMAGE:
stockp = STOCK_PIXMAP_MIME_IMAGE;
break;
case MIME_AUDIO:
stockp = STOCK_PIXMAP_MIME_AUDIO;
break;
case MIME_TEXT_ENRICHED:
stockp = STOCK_PIXMAP_MIME_TEXT_ENRICHED;
break;
default:
stockp = STOCK_PIXMAP_MIME_UNKNOWN;
break;
}
pixmap = stock_pixmap_widget(mimeview->mainwin->window, stockp);
gtk_container_add(GTK_CONTAINER(button), pixmap);
if (!desc) {
if (prefs_common.attach_desc)
desc = get_part_description(mimeinfo);
else
desc = get_part_name(mimeinfo);
}
if (desc && *desc)
tip = g_strdup_printf("%s\n%s\n%s", desc, mimeinfo->content_type,
to_human_readable(mimeinfo->size));
else
tip = g_strdup_printf("%s\n%s", mimeinfo->content_type,
to_human_readable(mimeinfo->size));
gtk_tooltips_set_tip(mimeview->tooltips, button, tip, NULL);
g_free(tip);
gtk_widget_show_all(button);
2003-10-05 12:10:30 +02:00
g_signal_connect(G_OBJECT(button), "button_press_event",
G_CALLBACK(icon_clicked_cb), mimeview);
g_signal_connect(G_OBJECT(button), "key_press_event",
G_CALLBACK(icon_key_pressed), mimeview);
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
}
static void icon_list_clear (MimeView *mimeview)
{
GList *child;
GtkAdjustment *adj;
child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
for (; child != NULL; child = g_list_next(child)) {
gtkut_container_remove(GTK_CONTAINER(mimeview->icon_vbox),
GTK_WIDGET(child->data));
}
mimeview->icon_count = 0;
adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
adj->value = adj->lower;
gtk_signal_emit_by_name(GTK_OBJECT (adj), "value_changed");
}
static void icon_list_toggle_by_mime_info(MimeView *mimeview,
MimeInfo *mimeinfo)
{
GList *child;
child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
for (; child != NULL; child = g_list_next(child)) {
if (GTK_IS_TOGGLE_BUTTON(child->data) &&
gtk_object_get_data(GTK_OBJECT(child->data),
"partinfo") == (gpointer)mimeinfo) {
toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
gtk_toggle_button_set_active
(GTK_TOGGLE_BUTTON(child->data), TRUE);
}
}
}
/*!
*\brief Used to 'click' the next or previous icon.
*
*\return true if the icon 'number' exists and was selected.
*/
static gboolean icon_list_select_by_number(MimeView *mimeview,
gint number)
{
GList *child;
if (number == 0) return FALSE;
child = gtk_container_children(GTK_CONTAINER(mimeview->icon_vbox));
for (; child != NULL; child = g_list_next(child)) {
if (GTK_IS_TOGGLE_BUTTON(child->data) &&
GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(child->data),
"icon_number")) == number) {
icon_selected(mimeview, number,
(MimeInfo*)gtk_object_get_data(GTK_OBJECT(child->data),
"partinfo"));
toggle_icon(GTK_TOGGLE_BUTTON(child->data), mimeview);
gtk_toggle_button_set_active
(GTK_TOGGLE_BUTTON(child->data), TRUE);
gtk_widget_grab_focus(GTK_WIDGET(child->data));
return TRUE;
}
}
return FALSE;
}
static void icon_scroll_size_allocate_cb(GtkWidget *widget,
2003-10-05 12:25:17 +02:00
GtkAllocation *size, MimeView *mimeview)
{
GtkAllocation *mainbox_size;
GtkAllocation *vbox_size;
GtkAllocation *layout_size;
GtkAdjustment *adj;
adj = gtk_layout_get_vadjustment(GTK_LAYOUT(mimeview->icon_scroll));
mainbox_size = &mimeview->icon_mainbox->allocation;
vbox_size = &mimeview->icon_vbox->allocation;
layout_size = &mimeview->icon_scroll->allocation;
2003-10-05 12:25:17 +02:00
#warning FIXME_GTK2 /* this code cause hang up. */
#if 0
/* centralise the vbox */
gtk_layout_move(GTK_LAYOUT(mimeview->icon_scroll), mimeview->icon_vbox,
(mainbox_size->width - vbox_size->width)/2, 0);
2003-10-05 12:25:17 +02:00
#endif
gtk_layout_set_size(GTK_LAYOUT(mimeview->icon_scroll),
GTK_LAYOUT(mimeview->icon_scroll)->width,
MAX(vbox_size->height, layout_size->height));
adj->step_increment = 5;
}
static void icon_list_create(MimeView *mimeview, MimeInfo *mimeinfo)
{
GtkRequisition size;
gint width;
g_return_if_fail(mimeinfo != NULL);
if (mimeinfo->children)
mimeinfo = mimeinfo->children;
while (mimeinfo != NULL) {
if (mimeinfo->children)
icon_list_create(mimeview, mimeinfo);
else if (mimeinfo->sub &&
mimeinfo->sub->mime_type != MIME_TEXT &&
mimeinfo->sub->mime_type != MIME_TEXT_HTML)
icon_list_create(mimeview, mimeinfo->sub);
else
icon_list_append_icon(mimeview, mimeinfo);
mimeinfo = mimeinfo->next;
}
gtk_widget_size_request(mimeview->icon_vbox, &size);
width = size.width + 4;
if (width > mimeview->icon_mainbox->requisition.width) {
2003-10-05 12:10:30 +02:00
gtk_widget_set_size_request(mimeview->icon_mainbox,
width, -1);
}
}
static void mime_toggle_button_cb (GtkWidget *button, MimeView *mimeview)
{
gtk_widget_ref(button);
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) {
gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_RIGHT,
GTK_SHADOW_NONE);
gtk_widget_hide(mimeview->icon_mainbox);
gtk_widget_show(mimeview->ctree_mainbox);
gtk_paned_set_position(GTK_PANED(mimeview->paned), mimeview->oldsize);
gtkut_container_remove(GTK_CONTAINER(mimeview->icon_mainbox),
button);
gtk_box_pack_end(GTK_BOX(mimeview->ctree_mainbox),
button, FALSE, FALSE, 0);
gtk_notebook_set_page(GTK_NOTEBOOK(mimeview->notebook), 1);
} else {
gtk_arrow_set(GTK_ARROW(GTK_BIN(button)->child), GTK_ARROW_LEFT,
GTK_SHADOW_NONE);
mimeview->oldsize = mimeview->ctree_mainbox->allocation.height;
gtk_widget_hide(mimeview->ctree_mainbox);
gtk_widget_show(mimeview->icon_mainbox);
gtk_paned_set_position(GTK_PANED(mimeview->paned), 0);
gtkut_container_remove(GTK_CONTAINER(mimeview->ctree_mainbox),
button);
gtk_box_pack_start(GTK_BOX(mimeview->icon_mainbox),
button, FALSE, FALSE, 0);
gtk_box_reorder_child(GTK_BOX(button->parent), button, 0);
if (mimeview->opened)
icon_list_toggle_by_mime_info
(mimeview, gtk_ctree_node_get_row_data(GTK_CTREE(mimeview->ctree),
mimeview->opened));
}
gtk_widget_grab_focus(button);
gtk_widget_unref(button);
}
void mimeview_update (MimeView *mimeview) {
if (mimeview && mimeview->mimeinfo) {
icon_list_clear(mimeview);
icon_list_create(mimeview, mimeview->mimeinfo);
}
}