Revision 7803e113 libsylph/procmime.c

b/libsylph/procmime.c
1627 1627
	return list;
1628 1628
}
1629 1629

  
1630
static GList *mailcap_list = NULL;
1631

  
1632
static GList *procmime_parse_mailcap(const gchar *file)
1633
{
1634
	GList *list = NULL;
1635
	FILE *fp;
1636
	gchar buf[BUFFSIZE];
1637
	MailCap *mailcap;
1638

  
1639
	if ((fp = g_fopen(file, "rb")) == NULL) return NULL;
1640

  
1641
	while (fgets(buf, sizeof(buf), fp) != NULL) {
1642
		gint i;
1643
		gchar *p;
1644
		gchar **strv;
1645

  
1646
		p = strchr(buf, '#');
1647
		if (p) *p = '\0';
1648
		g_strstrip(buf);
1649

  
1650
		strv = strsplit_with_quote(buf, ";", 0);
1651
		if (!strv)
1652
			continue;
1653

  
1654
		for (i = 0; strv[i] != NULL; ++i)
1655
			g_strstrip(strv[i]);
1656

  
1657
		if (!strv[0] || *strv[0] == '\0' ||
1658
		    !strv[1] || *strv[1] == '\0') {
1659
			g_strfreev(strv);
1660
			continue;
1661
		}
1662

  
1663
		mailcap = g_new(MailCap, 1);
1664
		mailcap->mime_type = g_strdup(strv[0]);
1665
		mailcap->cmdline_fmt = g_strdup(strv[1]);
1666
		mailcap->needs_terminal = FALSE;
1667

  
1668
		for (i = 0; strv[i] != NULL; ++i) {
1669
			if (strcmp(strv[i], "needsterminal") == 0)
1670
				mailcap->needs_terminal = TRUE;
1671
		}
1672

  
1673
		g_strfreev(strv);
1674

  
1675
		list = g_list_append(list, mailcap);
1676
	}
1677

  
1678
	return list;
1679
}
1680

  
1681
gint procmime_execute_open_file(const gchar *file, const gchar *mime_type)
1682
{
1683
	gchar *mime_type_ = NULL;
1684
	GList *cur;
1685
	MailCap *mailcap;
1686
	gchar *cmdline;
1687
	gint ret = -1;
1688
	static gboolean mailcap_list_init = FALSE;
1689

  
1690
	g_return_val_if_fail(file != NULL, -1);
1691

  
1692
	if (!mime_type ||
1693
	    g_ascii_strcasecmp(mime_type, "application/octet-stream") == 0) {
1694
		gchar *tmp;
1695
		tmp = procmime_get_mime_type(file);
1696
		if (!tmp)
1697
			return -1;
1698
		mime_type_ = g_ascii_strdown(tmp, -1);
1699
		g_free(tmp);
1700
	} else
1701
		mime_type_ = g_ascii_strdown(mime_type, -1);
1702

  
1703
	if (!mailcap_list_init && !mailcap_list) {
1704
		GList *list;
1705
		gchar *path;
1706

  
1707
		path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "mailcap",
1708
				  NULL);
1709
		mailcap_list = procmime_parse_mailcap(path);
1710
		g_free(path);
1711
#ifdef G_OS_WIN32
1712
		path = g_strconcat(get_startup_dir(), G_DIR_SEPARATOR_S "etc"
1713
				   G_DIR_SEPARATOR_S "mailcap", NULL);
1714
		list = procmime_parse_mailcap(path);
1715
		g_free(path);
1716
#else
1717
		if (!mailcap_list) {
1718
			path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
1719
					   ".mailcap", NULL);
1720
			mailcap_list = procmime_parse_mailcap(path);
1721
			g_free(path);
1722
		}
1723
		list = procmime_parse_mailcap(SYSCONFDIR "/mailcap");
1724
		if (!list)
1725
			list = procmime_parse_mailcap("/etc/mailcap");
1726
#endif
1727
		mailcap_list = g_list_concat(mailcap_list, list);
1728

  
1729
		mailcap_list_init = TRUE;
1730
	}
1731

  
1732
	for (cur = mailcap_list; cur != NULL; cur = cur->next) {
1733
		mailcap = (MailCap *)cur->data;
1734

  
1735
		if (!g_pattern_match_simple(mailcap->mime_type, mime_type_))
1736
			continue;
1737
		if (mailcap->needs_terminal)
1738
			continue;
1739

  
1740
		if (str_find_format_times(mailcap->cmdline_fmt, 's') == 1)
1741
			cmdline = g_strdup_printf(mailcap->cmdline_fmt, file);
1742
		else
1743
			cmdline = g_strconcat(mailcap->cmdline_fmt, " \"", file,
1744
					      "\"", NULL);
1745
		ret = execute_command_line(cmdline, TRUE);
1746
		g_free(cmdline);
1747
		break;
1748
	}
1749

  
1750
	g_free(mime_type_);
1751

  
1752
	return ret;
1753
}
1754

  
1755 1630
EncodingType procmime_get_encoding_for_charset(const gchar *charset)
1756 1631
{
1757 1632
	if (!charset)

Also available in: Unified diff