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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
/*
* Copyright (C) 2004-2006 Red Hat, Inc.
* Copyright (C) 2006, 2008 Vincent Untz
* Copyright © 2013 Canonical Limited
*
* Program written by Ray Strode <rstrode@redhat.com>
* Vincent Untz <vuntz@gnome.org>
* Ryan Lortie <desrt@desrt.ca>
*
* update-desktop-database 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.
*
* update-desktop-database 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 update-desktop-database; see the file COPYING. If not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite
* 330, Boston, MA 02111-1307, USA.
*/
#include <config.h>
#include <glib.h>
#include <glib/gi18n.h>
#include "mime-cache.h"
#define CACHE_FILENAME "mimeinfo.cache"
static gboolean verbose = FALSE, quiet = FALSE;
static void
log_handler (const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{
if (g_str_equal (log_domain, G_LOG_DOMAIN))
{
if (verbose || ((log_level & G_LOG_LEVEL_WARNING) && !quiet))
g_printerr ("%s\n", message);
}
else
g_log_default_handler (log_domain, log_level, message, NULL);
}
static gboolean
update_database (const char *desktop_dir,
GError **error)
{
gboolean success;
char *cache_file;
GBytes *cache;
cache = mime_cache_build (desktop_dir, error);
if (!cache)
return FALSE;
cache_file = g_build_filename (desktop_dir, CACHE_FILENAME, NULL);
success = g_file_set_contents (cache_file,
g_bytes_get_data (cache, NULL),
g_bytes_get_size (cache),
error);
g_bytes_unref (cache);
g_free (cache_file);
return success;
}
static const char **
get_default_search_path (void)
{
static char **args = NULL;
const char * const *data_dirs;
int i;
if (args != NULL)
return (const char **) args;
data_dirs = g_get_system_data_dirs ();
for (i = 0; data_dirs[i] != NULL; i++);
args = g_new (char *, i + 1);
for (i = 0; data_dirs[i] != NULL; i++)
args[i] = g_build_filename (data_dirs[i], "applications", NULL);
args[i] = NULL;
return (const char **) args;
}
static void
print_desktop_dirs (const char **dirs)
{
char *directories;
directories = g_strjoinv (", ", (char **) dirs);
g_debug (_("Search path is now: [%s]\n"), directories);
g_free (directories);
}
int
main (int argc,
char **argv)
{
GError *error;
GOptionContext *context;
const char **desktop_dirs;
int i;
gboolean found_processable_dir;
const GOptionEntry options[] =
{
{ "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet,
N_("Do not display any information about processing and "
"updating progress"), NULL},
{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
N_("Display more information about processing and updating progress"),
NULL},
{ G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &desktop_dirs,
NULL, N_("[DIRECTORY...]") },
{ NULL }
};
context = g_option_context_new ("");
g_option_context_set_summary (context, _("Build cache database of MIME types handled by desktop files."));
g_option_context_add_main_entries (context, options, NULL);
desktop_dirs = NULL;
error = NULL;
g_option_context_parse (context, &argc, &argv, &error);
if (error != NULL) {
g_printerr ("%s\n", error->message);
g_printerr (_("Run \"%s --help\" to see a full list of available command line options.\n"), argv[0]);
g_error_free (error);
return 1;
}
g_log_set_default_handler (log_handler, NULL);
if (desktop_dirs == NULL || desktop_dirs[0] == NULL)
desktop_dirs = get_default_search_path ();
print_desktop_dirs (desktop_dirs);
found_processable_dir = FALSE;
for (i = 0; desktop_dirs[i] != NULL; i++)
{
error = NULL;
update_database (desktop_dirs[i], &error);
if (error != NULL)
{
g_warning (_("Could not create cache file in \"%s\": %s\n"), desktop_dirs[i], error->message);
g_error_free (error);
error = NULL;
}
else
found_processable_dir = TRUE;
}
g_option_context_free (context);
if (!found_processable_dir)
{
char *directories;
directories = g_strjoinv (", ", (char **) desktop_dirs);
g_warning (_("The databases in [%s] could not be updated.\n"), directories);
g_free (directories);
return 1;
}
return 0;
}
|