summaryrefslogtreecommitdiff
path: root/src/evbp-mime.c
blob: 0cf1f73dae07c5ca903bc4d419ab20e4cfe905dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (C) 2011, 2012  Dan Nicholson <dbn.lists@gmail.com>
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301 USA.
 */

#include <config.h>
#include "evbp-mime.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include <evince-document.h>

static const struct {
    const gchar *mime;
    const gchar *exts;
} mime_suffix[] = {
    { "application/postscript", "ps" },
    { "application/x-bzpostscript", "ps.bz2" },
    { "application/x-gzpostscript", "ps.gz" },
    { "image/x-eps", "eps,epsi,epsf" },
    { "image/x-bzeps", "eps.bz2,epsi.bz2,epsf.bz2" },
    { "image/x-gzeps", "eps.gz,epsi.gz,epsf.gz" },
    { "image/tiff", "tif,tiff" },
    { "application/pdf", "pdf" },
    { "application/x-bzpdf", "pdf.bz2" },
    { "application/x-gzpdf", "pdf.gz" },
    { "application/x-ext-pdf", "pdf" },
    { "application/x-dvi", "dvi" },
    { "application/x-bzdvi", "dvi.bz2" },
    { "application/x-gzdvi", "dvi.gz" },
    { "application/x-cbr", "cbr" },
    { "application/x-cbz", "cbz" },
    { "application/x-cb7", "cb7" },
    { "application/x-cbt", "cbt" },
    { "image/vnd.djvu", "djvu,djv" },
};
static const int n_mime_suffix = sizeof(mime_suffix) / sizeof(mime_suffix[0]);

static const gchar *
get_suffix(const gchar *type)
{
    int i;

    for (i = 0; i < n_mime_suffix; i++)
        if (strcmp(type, mime_suffix[i].mime) == 0)
            return mime_suffix[i].exts;
    return "";
}

static void
add_one_mime(EvTypeInfo *type, gpointer udata)
{
    GString *mime_desc = udata;
    const gchar **mime;
    const gchar *suffix;

    for (mime = type->mime_types; *mime; mime++) {
        suffix = get_suffix(*mime);
        if (!suffix)
            continue;
        g_string_append_printf(mime_desc, "%s%s:%s:%s",
                               (mime_desc->len > 0) ? ";" : "",
                               *mime, suffix, type->desc);
    }
}

static GString *
build_mime_description(GList *types)
{
    GString *mimes = g_string_new(NULL);

    g_list_foreach(types, (GFunc)add_one_mime, mimes);
    return mimes;
}

GString *
evbp_mime_get_description(void)
{
    GList *types;
    GString *mimes;

    g_debug("%s", __func__);

    types = ev_backends_manager_get_all_types_info();
    mimes = build_mime_description(types);

    g_list_foreach(types, (GFunc)g_free, NULL);
    g_list_free(types);

    return mimes;
}