File: mn-main.c

package info (click to toggle)
mail-notification 5.4.dfsg.1-14
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 6,468 kB
  • ctags: 9,615
  • sloc: ansic: 13,970; sh: 2,770; xml: 2,113; makefile: 57
file content (619 lines) | stat: -rw-r--r-- 15,191 bytes parent folder | download | duplicates (3)
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
 * Mail Notification
 * Copyright (C) 2003-2008 Jean-Yves Lefort <jylefort@brutele.be>
 *
 * 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 3 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 <string.h>
#include <stdlib.h>
#include <signal.h>
#include <glib/gi18n.h>
#include <libgnome/libgnome.h>
#include <libgnomevfs/gnome-vfs.h>
#include <libnotify/notify.h>
#include <dbus/dbus-glib-lowlevel.h>
#if WITH_GMIME
#include <gmime/gmime.h>
#include "mn-gmime-stream-vfs.h"
#endif
#if WITH_MBOX || WITH_MOZILLA || WITH_MH || WITH_MAILDIR || WITH_SYLPHEED
#include "mn-vfs-mailbox.h"
#endif
#if WITH_MAILDIR
#include "mn-maildir-message.h"
#endif
#if WITH_SYLPHEED
#include "mn-sylpheed-message.h"
#endif
#if WITH_EVOLUTION
#include "mn-evolution-message.h"
#endif
#include "mn-conf.h"
#include "mn-util.h"
#include "mn-stock.h"
#include "mn-shell.h"
#include "mn-message.h"
#include "mn-client-dbus.h"
#include "mn-server.h"

#define CALL_SERVER(call)		\
  {					\
    if (! (call))			\
      handle_server_call_error(err);	\
  }

typedef struct
{
  const char	*name;
  gboolean	enabled;
} Component;

static gboolean arg_enable_info = FALSE;

static unsigned int bus_disconnected_timeout_id = 0;

static gboolean bus_disconnected_cb (gpointer data) G_GNUC_NORETURN;

static void
print_components (const Component *components, int n_components)
{
  int i;

  for (i = 0; i < n_components; i++)
    g_print("  %-30s %s\n", components[i].name, components[i].enabled ? _("yes") : _("no"));
}

static void
print_version (void)
{
  /*
   * Here and everywhere else, backends and features are sorted
   * alphabetically.
   */

  static const Component mailbox_backends[] = {
    { "Evolution",		WITH_EVOLUTION		},
    { "Gmail",			WITH_GMAIL		},
    { "IMAP",			WITH_IMAP		},
    { "Maildir",		WITH_MAILDIR		},
    { "mbox",			WITH_MBOX		},
    { "MH",			WITH_MH			},
    { "Mozilla products",	WITH_MOZILLA		},
    { "POP3",			WITH_POP3		},
    { "Sylpheed",		WITH_SYLPHEED		},
    { "Windows Live Hotmail",	WITH_HOTMAIL		},
    { "Yahoo! Mail",		WITH_YAHOO		}
  };

  static const Component pi_features[] = {
    { "IPv6",			WITH_IPV6		},
    { "SASL",			WITH_SASL		},
    { "SSL/TLS",		WITH_SSL		}
  };

  g_print(_("%s version %s\n"), _("Mail Notification"), VERSION);
  g_print("Copyright (C) 2003-2008 Jean-Yves Lefort.\n");

  g_print("\n");

  g_print(_("Mailbox backends:\n"));
  print_components(mailbox_backends, G_N_ELEMENTS(mailbox_backends));

  g_print("\n");

  g_print(_("IMAP and POP3 features:\n"));
  print_components(pi_features, G_N_ELEMENTS(pi_features));
}

static void
info_log_cb (const char *log_domain,
	     GLogLevelFlags log_level,
	     const char *message,
	     gpointer user_data)
{
  if (arg_enable_info)
    g_log_default_handler(log_domain, log_level, message, user_data);
}

static void
init_classes (void)
{
  int i;

#if WITH_GMIME
  g_type_class_ref(MN_TYPE_GMIME_STREAM_VFS);
  g_type_class_ref(GMIME_TYPE_PARSER);
  g_type_class_ref(GMIME_TYPE_STREAM_MEM);
  g_type_class_ref(GMIME_TYPE_MESSAGE);
#endif /* WITH_GMIME */
#if WITH_MBOX || WITH_MOZILLA || WITH_MH || WITH_MAILDIR || WITH_SYLPHEED
  for (i = 0; mn_vfs_mailbox_backend_types[i]; i++)
    g_type_class_ref(mn_vfs_mailbox_backend_types[i]);
#endif
#if WITH_MAILDIR
  g_type_class_ref(MN_TYPE_MAILDIR_MESSAGE);
#endif
#if WITH_SYLPHEED
  g_type_class_ref(MN_TYPE_SYLPHEED_MESSAGE);
#endif
  g_type_class_ref(MN_TYPE_MESSAGE);
}

#if WITH_GMIME
static void
init_gmime (void)
{
  GPtrArray *array;
  GSList *fallback_charsets;
  GSList *l;

  g_mime_init(0);

  array = g_ptr_array_new();

  fallback_charsets = mn_conf_get_string_list(MN_CONF_FALLBACK_CHARSETS);

  MN_LIST_FOREACH(l, fallback_charsets)
    {
      const char *charset = l->data;

      if (! strcmp(charset, "user"))
	{
	  const char *user_charset;

	  g_get_charset(&user_charset);
	  g_ptr_array_add(array, g_strdup(user_charset));
	}
      else
	g_ptr_array_add(array, g_strdup(charset));
    }

  mn_g_slist_free_deep(fallback_charsets);

  g_ptr_array_add(array, NULL);	/* canary */

  g_mime_set_user_charsets((const char **) array->pdata);

  /*
   * Note that because of
   * http://bugzilla.gnome.org/show_bug.cgi?id=509434, we do not free
   * the strings of the array (since GMime does not copy them).
   */
  g_ptr_array_free(array, TRUE);
}
#endif /* WITH_GMIME */

static void
handle_server_call_error (GError *err)
{
  g_return_if_fail(err != NULL);

  mn_show_fatal_error_dialog(NULL, _("Unable to contact the running Mail Notification instance: %s."), err->message);
}

static gboolean
has_icon_path (const char *path)
{
  char **paths;
  int i;
  gboolean has = FALSE;

  gtk_icon_theme_get_search_path(gtk_icon_theme_get_default(), &paths, NULL);
  for (i = 0; paths[i]; i++)
    if (! strcmp(paths[i], path))
      {
	has = TRUE;
	break;
      }
  g_strfreev(paths);

  return has;
}

/*
 * This is needed when MN is not installed in the standard prefix (as
 * is the case for my test builds).
 */
static void
ensure_icon_path (void)
{
  static const char *icon_path = DATADIR G_DIR_SEPARATOR_S "icons";

  if (! has_icon_path(icon_path))
    gtk_icon_theme_prepend_search_path(gtk_icon_theme_get_default(), icon_path);
}

static void
report_option_ignored (const char *option_name)
{
  g_return_if_fail(option_name != NULL);

  g_message(_("%s option ignored since Mail Notification is not already running"), option_name);
}

static gboolean
bus_disconnected_cb (gpointer data)
{
  mn_show_fatal_error_dialog(NULL, _("The connection to the D-Bus session bus was lost."));
}

static DBusHandlerResult
session_bus_filter_cb (DBusConnection *conn, DBusMessage *message, void *user_data)
{
  if (dbus_message_is_signal(message, DBUS_INTERFACE_LOCAL, "Disconnected"))
    {
      /*
       * If the user is logging out there is no reason to display the
       * error dialog, so wait a bit, hoping that we will be killed
       * before the timeout triggers.
       */

      if (bus_disconnected_timeout_id == 0)
	bus_disconnected_timeout_id = gdk_threads_add_timeout(5000, bus_disconnected_cb, NULL);

      return DBUS_HANDLER_RESULT_HANDLED;
    }

  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}

static DBusGConnection *
connect_to_session_bus (void)
{
  GError *err = NULL;
  DBusGConnection *bus;
  DBusConnection *raw_bus;

  bus = dbus_g_bus_get(DBUS_BUS_SESSION, &err);
  if (! bus)
    {
      mn_show_fatal_error_dialog(NULL, _("Unable to connect to the D-Bus session bus: %s."), err->message);
      g_error_free(err);
    }

  raw_bus = dbus_g_connection_get_connection(bus);

  dbus_connection_set_exit_on_disconnect(raw_bus, FALSE);

  if (! dbus_connection_add_filter(raw_bus, session_bus_filter_cb, NULL, NULL))
    /* too unlikely to be worth a translation */
    mn_show_fatal_error_dialog(NULL, "Unable to add a D-Bus filter: not enough memory.");

  return bus;
}

/*
 * A memory management bug in DBusGProxy
 * (https://bugs.freedesktop.org/show_bug.cgi?id=14030) prevents us
 * from unreferencing the proxy, so provide an eternal singleton
 * proxy.
 */
static DBusGProxy *
get_bus_proxy (DBusGConnection *bus)
{
  DBusGProxy *proxy;

  g_return_val_if_fail(bus != NULL, NULL);

  proxy = dbus_g_proxy_new_for_name(bus,
				    DBUS_SERVICE_DBUS,
				    DBUS_PATH_DBUS,
				    DBUS_INTERFACE_DBUS);

#if WITH_EVOLUTION
  /* needed by MNEvolutionClient */
  dbus_g_proxy_add_signal(proxy,
			  "NameOwnerChanged",
			  G_TYPE_STRING, /* service_name */
			  G_TYPE_STRING, /* old_owner */
			  G_TYPE_STRING, /* new_owner */
			  G_TYPE_INVALID);
#endif

  return proxy;
}

int
main (int argc, char **argv)
{
  gboolean arg_version = FALSE;
  gboolean arg_display_properties = FALSE;
  gboolean arg_display_about = FALSE;
  gboolean arg_consider_new_mail_as_read = FALSE;
  gboolean arg_update = FALSE;
  gboolean arg_print_summary = FALSE;
  gboolean arg_unset_obsolete_configuration = FALSE;
  gboolean arg_quit = FALSE;
  const GOptionEntry options[] = {
    {
      "version",
      'v',
      0,
      G_OPTION_ARG_NONE,
      &arg_version,
      N_("Show version information"),
      NULL
    },
    {
      "enable-info",
      'i',
      0,
      G_OPTION_ARG_NONE,
      &arg_enable_info,
      N_("Enable informational output"),
      NULL
    },
    {
      "display-properties",
      'p',
      0,
      G_OPTION_ARG_NONE,
      &arg_display_properties,
      N_("Display the properties dialog"),
      NULL
    },
    {
      "display-about",
      'a',
      0,
      G_OPTION_ARG_NONE,
      &arg_display_about,
      N_("Display the about dialog"),
      NULL
    },
    {
      "consider-new-mail-as-read",
      'r',
      0,
      G_OPTION_ARG_NONE,
      &arg_consider_new_mail_as_read,
      N_("Consider new mail as read"),
      NULL
    },
    {
      "update",
      'u',
      0,
      G_OPTION_ARG_NONE,
      &arg_update,
      N_("Update the mail status"),
      NULL
    },
    {
      "print-summary",
      's',
      0,
      G_OPTION_ARG_NONE,
      &arg_print_summary,
      N_("Print a XML mail summary"),
      NULL
    },
    {
      "unset-obsolete-configuration",
      '\0',
      0,
      G_OPTION_ARG_NONE,
      &arg_unset_obsolete_configuration,
      N_("Unset obsolete GConf configuration"),
      NULL
    },
    {
      "quit",
      'q',
      0,
      G_OPTION_ARG_NONE,
      &arg_quit,
      N_("Quit Mail Notification"),
      NULL
    },
    { NULL }
  };
  GOptionContext *option_context;
  DBusGConnection *bus;
  DBusGProxy *bus_proxy;

  g_log_set_fatal_mask(NULL, G_LOG_LEVEL_CRITICAL);
  g_log_set_handler(NULL, G_LOG_LEVEL_INFO, info_log_cb, NULL);

#ifdef ENABLE_NLS
  bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR);
  bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
  textdomain(GETTEXT_PACKAGE);
#endif

  g_set_application_name(_("Mail Notification"));

  g_thread_init(NULL);
  if (! g_thread_supported())
    /*
     * We cannot use mn_fatal_error_dialog() because gtk_init() has
     * not been called yet.
     */
    g_critical(_("multi-threading is not available"));
  gdk_threads_init();

  GDK_THREADS_ENTER();

  option_context = g_option_context_new(NULL);
  g_option_context_add_main_entries(option_context, options, GETTEXT_PACKAGE);

  gnome_program_init(PACKAGE,
		     VERSION,
		     LIBGNOME_MODULE,
		     argc,
		     argv,
		     GNOME_PARAM_HUMAN_READABLE_NAME, _("Mail Notification"),
		     GNOME_PROGRAM_STANDARD_PROPERTIES,
		     GNOME_PARAM_GOPTION_CONTEXT, option_context,
		     NULL);

  gtk_init(&argc, &argv);

  if (arg_version)
    {
      print_version();
      goto end;
    }

  if (arg_unset_obsolete_configuration)
    {
      mn_conf_unset_obsolete();
      goto end;
    }

  ensure_icon_path();
  gtk_window_set_default_icon_name("mail-notification");

  mn_stock_init();

  bus = connect_to_session_bus();
  bus_proxy = get_bus_proxy(bus);

  if (mn_server_start(bus, bus_proxy)) /* not already running */
    {
      if (arg_quit)
	g_message(_("Mail Notification is not running"));
      else
	{
	  mn_mailbox_init_types();
#if WITH_MBOX || WITH_MOZILLA || WITH_MH || WITH_MAILDIR || WITH_SYLPHEED
	  mn_vfs_mailbox_init_types();
#endif

	  /* mn-client-session uses sockets, we don't want to die on SIGPIPE */
	  signal(SIGPIPE, SIG_IGN);

	  if (! gnome_vfs_init())
	    mn_show_fatal_error_dialog(NULL, _("Unable to initialize the GnomeVFS library."));

#if 0
	  gnome_authentication_manager_init();
#endif

	  /* must be called before init_gmime() */
	  mn_conf_init();

#if WITH_GMIME
	  init_gmime();
#endif

	  if (! notify_init(_("Mail Notification")))
	    mn_show_error_dialog(NULL,
				 _("An initialization error has occurred in Mail Notification"),
				 _("Unable to initialize the notification library. Message popups will not be displayed."));

	  /*
	   * Work around
	   * http://bugzilla.gnome.org/show_bug.cgi?id=64764:
	   * initialize the classes we will be using concurrently
	   * before any thread is created.
	   */
	  init_classes();

	  mn_shell_new(bus, bus_proxy);

	  /* also display the properties dialog if there are no mailboxes */
	  if (! mn_shell->mailboxes->list)
	    arg_display_properties = TRUE;

	  if (arg_display_properties)
	    mn_shell_show_properties_dialog(mn_shell, 0);
	  if (arg_display_about)
	    mn_shell_show_about_dialog(mn_shell, 0);
	  if (arg_consider_new_mail_as_read)
	    report_option_ignored("--consider-new-mail-as-read");
	  if (arg_update)
	    report_option_ignored("--update");
	  if (arg_print_summary)
	    report_option_ignored("--print-summary");

	  /* in case no window has been displayed */
	  gdk_notify_startup_complete();

	  gtk_main();
	}
    }
  else				/* already running */
    {
      DBusGProxy *proxy;
      GError *err = NULL;

      proxy = dbus_g_proxy_new_for_name(bus,
					MN_SERVER_SERVICE,
					MN_SERVER_PATH,
					MN_SERVER_INTERFACE);

      if (arg_quit)
	{
	  g_message(_("quitting Mail Notification"));
	  CALL_SERVER(org_gnome_MailNotification_quit(proxy, &err));
	}
      else
	{
	  /* also display the properties dialog if there are no mailboxes */
	  if (! arg_display_properties)
	    {
	      gboolean has;
	      CALL_SERVER(org_gnome_MailNotification_has_mailboxes(proxy, &has, &err));
	      arg_display_properties = ! has;
	    }

	  if (arg_display_properties)
	    CALL_SERVER(org_gnome_MailNotification_display_properties(proxy, &err));
	  if (arg_display_about)
	    CALL_SERVER(org_gnome_MailNotification_display_about(proxy, &err));
	  if (arg_consider_new_mail_as_read)
	    {
	      g_message(_("considering new mail as read"));
	      CALL_SERVER(org_gnome_MailNotification_consider_new_mail_as_read(proxy, &err));
	    }
	  if (arg_update)
	    {
	      g_message(_("updating the mail status"));
	      CALL_SERVER(org_gnome_MailNotification_update(proxy, &err));
	    }
	  if (arg_print_summary)
	    {
	      char *summary;

	      CALL_SERVER(org_gnome_MailNotification_get_summary(proxy, &summary, &err));
	      g_print("%s", summary);
	      g_free(summary);
	    }

	  if (! (arg_display_properties
		 || arg_display_about
		 || arg_consider_new_mail_as_read
		 || arg_update
		 || arg_print_summary))
	    g_message(_("Mail Notification is already running"));
	}

      /*
       * Do not unref the proxy, since it might break when the
       * DBusGProxy memory management issue is fixed
       * (https://bugs.freedesktop.org/show_bug.cgi?id=14030).
       */

      /* no window has been displayed by this process */
      gdk_notify_startup_complete();
    }

 end:
  GDK_THREADS_LEAVE();

  return 0;
}