[dba5ed]: src / desktop_integration.c Maximize Restore History

Download this file

desktop_integration.c    536 lines (416 with data), 13.7 kB

  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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
/*
* Copyright (C) 2002-2005 the xine-project
*
* 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 St, Fifth Floor, Boston, MA 02110-1301
* USA
*
* $Id: desktop_integration.c,v 1.21 2006/03/27 22:11:24 dsalt Exp $
*
* helper functions to register gxine with kde and gnome
* (and possibly cde in the future)
*
* most code stolen from openoffice
*
* oo_1.0.1_src/sysui/oounix/office/gnome
*/
#include "globals.h"
#include <stdlib.h>
#include <stdio.h>
#include <dlfcn.h>
#include <alloca.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include "desktop_integration.h"
#include "utils.h"
typedef enum { GNOME_VFS_OK } GnomeVFSResult; /* incomplete */
/*
* the bunch of gnome vfs functions needed
*/
struct gnome_vfs_functions
{
void (* application_registry_remove_application) ( const char *app_id );
void (* application_registry_add_mime_type) ( const char *app_id, const char *mime_type );
void (* application_registry_set_value) ( const char *app_id, const char *key, const char *value);
void (* application_registry_set_bool_value) ( const char *app_id, const char *key, int value);
void (* application_registry_remove_mime_type) ( const char *app_id, const char *mime_type );
#ifndef GNOME_VFS_DISABLE_DEPRECATED
void (* mime_freeze) ( void );
void (* mime_thaw) ( void );
void (* mime_registered_mime_type_delete) ( const char * mime_type );
GnomeVFSResult (* mime_set_default_application) ( const char *mime_type, const char *application_id );
GnomeVFSResult (* mime_set_registered_type_key) ( const char *mime_type, const char *key, const char *data );
GnomeVFSResult (* mime_set_value) ( const char *mime_type, const char *key, const char *value );
#endif
};
/*
* the mime info as data structure
*/
struct mime_info
{
const char * mime_type;
const char * description;
const char * extensions;
const char * icon_filename;
};
/*
* make the code read as normal gnome app
*/
#define gnome_vfs_application_registry_remove_application gnome_vfs->application_registry_remove_application
#define gnome_vfs_application_registry_add_mime_type gnome_vfs->application_registry_add_mime_type
#define gnome_vfs_application_registry_remove_mime_type gnome_vfs->application_registry_remove_mime_type
#define gnome_vfs_application_registry_set_value gnome_vfs->application_registry_set_value
#define gnome_vfs_application_registry_set_bool_value gnome_vfs->application_registry_set_bool_value
#define gnome_vfs_mime_thaw gnome_vfs->mime_thaw
#define gnome_vfs_mime_freeze gnome_vfs->mime_freeze
static void * gnome_vfs_handle = NULL;
static struct gnome_vfs_functions * gnome_vfs = NULL;
/*
* load the gnome-vfs library and initialize the function pointers
*/
typedef void (*func_t) ();
static func_t get_func (const char *name)
{
func_t func = (func_t) dlsym (gnome_vfs_handle, name);
if (func)
return func;
fprintf (stderr, "%s.\n", dlerror());
return (func_t) 0;
}
#define GET_FUNC(VAR,NAME) \
if (!(VAR = (typeof (VAR)) get_func ((NAME)))) \
return 0
#define GET_FUNC_S(VAR) \
if (!(gnome_vfs->VAR = (typeof (gnome_vfs->VAR)) \
get_func ("gnome_vfs_" #VAR))) \
return 0
static int gnome_vfs_init (void)
{
void (*gnome_vfs_init_func) (void) = NULL;
void (*gnome_vfs_mime_get_value) (const char *, const char *) = NULL;
/* load the gnomevfs library */
gnome_vfs_handle = dlopen ("libgnomevfs-2.so.0", RTLD_LAZY);
if (!gnome_vfs_handle)
{
fprintf (stderr, "%s.\n", dlerror());
return 0;
}
gnome_vfs = ( struct gnome_vfs_functions * ) calloc( 1, sizeof( struct gnome_vfs_functions ) );
/* this call causes gnome-vfs to become initialized */
GET_FUNC (gnome_vfs_init_func, "gnome_vfs_init");
gnome_vfs_init_func ();
GET_FUNC (gnome_vfs_mime_get_value, "gnome_vfs_mime_get_value");
gnome_vfs_mime_get_value( "text/plain", "description" );
/* extract needed symbols from the library */
GET_FUNC_S (mime_freeze);
GET_FUNC_S (mime_thaw);
GET_FUNC_S (application_registry_remove_application);
GET_FUNC_S (application_registry_add_mime_type);
GET_FUNC_S (application_registry_set_value);
GET_FUNC_S (application_registry_set_bool_value);
GET_FUNC_S (application_registry_remove_mime_type);
return 1;
}
/*
* release the gnome-vfs library and free the function pointer struct
*/
#undef GET_FUNC
#define GET_FUNC(VAR,NAME) \
if ((VAR = (typeof (VAR)) get_func ((NAME))) == NULL) \
return
static void gnome_vfs_shutdown (void)
{
/* release the gnomevfs library */
if (gnome_vfs_handle)
{
void (*gnome_vfs_application_registry_sync) (void) = NULL;
/* extract and call shutdown function */
GET_FUNC (gnome_vfs_application_registry_sync, "gnome_vfs_application_registry_sync");
gnome_vfs_application_registry_sync ();
dlclose (gnome_vfs_handle);
}
free (gnome_vfs);
}
#ifdef USE_INTEGRATION_WIZARD
static char *report = NULL;
static char *section = NULL;
void di_registration_flush (void)
{
free (report);
report = NULL;
section = NULL;
}
static void __attribute__ ((format (printf, 1, 2)))
di_report (const char *fmt, ...)
{
char *msg, *markup;
va_list ap, aq;
if (section)
{
asreprintf (&report, _("%s%s<i>Errors while registering with %s:</i>\n"),
report ? : "", report ? "\n" : "", section);
section = NULL;
}
va_start (ap, fmt);
va_copy (aq, ap);
g_printerr (_("gxine desktop integration: "));
vfprintf (stderr, fmt, ap);
va_end (ap);
fprintf (stderr, "\n");
msg = g_strdup_vprintf (fmt, aq);
va_end (aq);
markup = g_markup_printf_escaped ("%s", msg);
free (msg);
asreprintf (&report, "%s\n%s", report, markup);
free (markup);
}
const char *di_registration_report (void)
{
return report;
}
static FILE *di_fopen (const char *fname)
{
FILE *f = fopen (fname, "w");
if (!f)
di_report (_("can't open file %s for writing: %s"), fname, strerror (errno));
return f;
}
static void di_fclose (FILE *f, const char *fname)
{
if (ferror (f))
di_report (_("error while writing to file %s: %s\n"), fname, strerror (errno));
if (fclose (f))
di_report (_("error when closing file %s: %s\n"), fname, strerror (errno));
}
/*
* add the installed application to the gnome application info registry
*/
static GnomeVFSResult register_application( const char * application_id,
const char * product_name,
const char * product_version,
const char * command,
const char * install_path ) {
char * command_full = (char *) alloca( strlen( install_path ) + strlen( command ) + 10 );
char * product = (char *) alloca( strlen( product_name ) + strlen( product_version ) + 2 );
sprintf( command_full, "%s/%s", install_path, command );
sprintf( product, "%s %s", product_name, product_version );
/* entry will be created automatically */
gnome_vfs_application_registry_set_value( application_id, "name", product );
gnome_vfs_application_registry_set_value( application_id, "command", command_full );
gnome_vfs_application_registry_set_bool_value( application_id, "can_open_multiple_files", 1 );
gnome_vfs_application_registry_set_bool_value( application_id, "requires_terminal", 0 );
return GNOME_VFS_OK;
}
void di_register_gnome (void) {
char *mime_types;
section = "GNOME";
if (!gnome_vfs_init()) {
di_report (_("GNOME initialisation failed"));
return;
}
/* freeze the lists to enable multiple updates */
gnome_vfs_mime_freeze();
register_application ("gxine", "gxine", VERSION, "gxine", bindir);
/*
* iterate over all mime types xine supports
* and register it as a handler for each of them
*/
mime_types = xine_get_mime_types (xine);
while (mime_types) {
char *next = mime_types;
if ( (next=strchr (mime_types, ';')) ) {
char *end;
*next=0;
end = strchr (mime_types, ':');
if (end) {
*end=0;
g_print (_("desktop_integration: registering xine for MIME type ���%s���\n"),
mime_types);
gnome_vfs_application_registry_add_mime_type ("gxine", mime_types );
}
}
mime_types = next ? next + 1 : NULL;
}
/* force the user.mime and user.keys to be written */
gnome_vfs_mime_thaw();
gnome_vfs_shutdown();
}
void di_register_kde (void) {
gchar *fname;
gchar *kdehome;
FILE *f;
char *mime_types;
section = "KDE";
kdehome = getenv ("KDEHOME");
if (kdehome)
fname = g_strconcat (kdehome, "/share/applnk/Multimedia",
NULL);
else
fname = g_strconcat (g_get_home_dir(), "/.kde/share/applnk/Multimedia",
NULL);
if (ensure_path_exists (fname, 0755))
{
di_report (_("can't create directory %s: %s"), fname, strerror (errno));
g_free (fname);
return;
}
g_free (fname);
/*
* produce gxine.desktop
*/
if (kdehome)
fname = g_strconcat (kdehome, "/share/applnk/Multimedia/gxine.desktop",
NULL);
else
fname = g_strconcat (g_get_home_dir(), "/.kde/share/applnk/Multimedia/gxine.desktop",
NULL);
f = di_fopen (fname);
if (!f) {
g_free (fname);
return;
}
fprintf (f, "[Desktop Entry]\n"
"Version=1.0\n"
"Encoding=UTF-8\n"
"Name=gxine\n"
"GenericName=Video Player\n"
"Comment=Play films and songs, or watch digital TV\n"
"MultipleArgs=true\n"
"Terminal=false\n"
"Icon=gxine\n"
"Exec=%s/gxine\n"
"Type=Application\n"
"StartupNotify=true\n"
"Categories=GTK;Application;AudioVideo;\n"
"MimeType=",
bindir);
/*
* iterate over all mime types xine supports
* and register it as a handler for each of them
*/
mime_types = xine_get_mime_types (xine);
while (mime_types) {
char *next = mime_types;
if ( (next=strchr (mime_types, ';')) ) {
char *end;
*next=0;
end = strchr (mime_types, ':');
if (end) {
*end=0;
g_print (_("desktop_integration: KDE registering xine for MIME type ���%s���\n"),
mime_types);
fprintf (f, "%s;", mime_types);
}
}
mime_types = next ? next + 1 : NULL;
}
fprintf (f, "\n");
di_fclose (f, fname);
g_free (fname);
}
void di_register_mailcap (void) {
char *mc;
gchar *fname;
int n;
FILE *f;
char *mime_types;
section = "mailcap";
fname = g_strconcat (g_get_home_dir(), "/.mailcap", NULL);
mc = read_entire_file (fname, &n);
f = di_fopen (fname);
if (!f) {
g_free (fname);
return;
}
if (mc)
fwrite (mc, n, 1, f);
/*
* iterate over all mime types xine supports
* and register it as a handler for each of them
*/
mime_types = xine_get_mime_types (xine);
while (mime_types) {
char *next = mime_types;
if ( (next=strchr (mime_types, ';')) ) {
char *end;
*next=0;
end = strchr (mime_types, ':');
if (end) {
*end=0;
if (!mc || !strstr (mc, mime_types)) {
g_print (_("desktop_integration: mailcap: registering xine for MIME type ���%s���\n"),
mime_types);
fprintf (f, "%s;%s/gxine %%s\n", mime_types,
bindir);
} else { /* MIME type already registered */
g_print (_("desktop_integration: %s already has a handler\n"),
mime_types);
}
}
}
mime_types = next ? next + 1 : NULL;
}
fprintf (f, "\n");
di_fclose (f, fname);
g_free (fname);
}
void di_register_mozilla (void)
{
gchar *fname, *sname;
char buf[FILENAME_MAX];
unsigned int i;
struct stat st;
static const char *dir[] = { "/.mozilla", "/.mozilla/firefox", "/.firefox" };
section = "Mozilla";
sname = g_strconcat (plugindir, "/gxineplugin.so", NULL);
if (stat (sname, &st))
return;
g_print (_("wizards: installing browser plugin...\n"));
for (i = 0; i < G_N_ELEMENTS (dir); ++i)
{
/* just make sure the directory exists */
fname = g_strconcat (g_get_home_dir(), dir[i], "/plugins", NULL);
if (ensure_path_exists (fname, 0755))
{
di_report (_("can't create directory %s: %s"), fname, strerror (errno));
goto next;
}
g_free (fname);
fname = g_strconcat (g_get_home_dir(), dir[i], "/plugins/gxineplugin.so", NULL);
buf[0] = 0;
/* If the destination object exists, check if it's a symlink and if it's
* pointing to the right place. If any of those are not true, we unlink
* the object (if it exists) and create a fresh symlink in its place.
*/
if (readlink (fname, buf, sizeof (buf)) &&
errno != EINVAL && errno != ENOENT)
{
di_report (_("symlink check on %s failed: %s"), fname, strerror (errno));
goto next;
}
if (!strcmp (buf, sname))
goto next;
if (unlink (fname) && errno != ENOENT)
di_report (_("cannot unlink %s: %s"), fname, strerror (errno));
if (symlink (sname, fname) && errno != EEXIST)
di_report (_("cannot link %s to %s: %s"), sname, fname, strerror (errno));
next:
g_free (fname);
}
g_free (sname);
}
#endif /* USE_INTEGRATION_WIZARD */