/* gdm-motd.c - Message of the Day support in gnome-session. Copyright (C) 1998, 1999 Tom Tromey 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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include #include #include #ifdef HAVE_LIBNOTIFY #include #endif #include "gsm-motd.h" static gchar *motd_contents = NULL; static GnomeVFSMonitorHandle *monitor_handle = NULL; static void display_message_of_the_day_dialog (void) { GtkWidget *dialog; if (motd_contents) { dialog = gtk_message_dialog_new_with_markup (NULL, 0, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "%s\n%s", _("Message of the Day"), motd_contents); g_signal_connect (G_OBJECT (dialog), "response", G_CALLBACK (gtk_widget_destroy), NULL); gtk_widget_show (dialog); } } #ifdef HAVE_LIBNOTIFY static void display_message_of_the_day_notification (void) { static NotifyIcon *icon = NULL; if (!motd_contents) return; if (!notify_is_initted () && !notify_init (_("Session Manager"))) { g_printerr ("Could not contact the notification daemon"); display_message_of_the_day_dialog (); } else { if (!icon) icon = notify_icon_new_from_uri ("gtk-help"); if (!notify_send_notification (NULL, "device", NOTIFY_URGENCY_NORMAL, _("Message of the Day"), motd_contents, icon, FALSE, 0, NULL, NULL, 0)) { display_message_of_the_day_dialog (); } } } #endif static void motd_changed_cb (GnomeVFSMonitorHandle *handle, const gchar *monitor_uri, const gchar *info_uri, GnomeVFSMonitorEventType event_type, gpointer user_data) { gchar *contents; gsize length; switch (event_type) { case GNOME_VFS_MONITOR_EVENT_CHANGED : case GNOME_VFS_MONITOR_EVENT_CREATED : if (g_file_get_contents ("/etc/motd", &contents, &length, NULL)) { if (length > 0) { g_free (motd_contents); motd_contents = contents; #ifdef HAVE_LIBNOTIFY display_message_of_the_day_notification (); #else display_message_of_the_day_dialog (); #endif } } break; case GNOME_VFS_MONITOR_EVENT_DELETED : g_free (motd_contents); motd_contents = NULL; break; } } static gboolean on_login_cb (gpointer user_data) { #ifdef HAVE_LIBNOTIFY display_message_of_the_day_notification (); #else display_message_of_the_day_dialog (); #endif return FALSE; } void gsm_motd_start (void) { gchar *contents; gsize length; /* load the MOTD file */ if (gnome_vfs_monitor_add (&monitor_handle, "file:///etc/motd", GNOME_VFS_MONITOR_FILE, (GnomeVFSMonitorCallback) motd_changed_cb, NULL) != GNOME_VFS_OK) { g_printerr ("Failed to setup monitor for /etc/motd file"); } if (g_file_get_contents ("/etc/motd", &contents, &length, NULL)) { if (length > 0) motd_contents = contents; } /* install a timeout to show the message first time */ g_timeout_add (8000, (GSourceFunc) on_login_cb, NULL); } void gsm_motd_stop (void) { if (monitor_handle) { gnome_vfs_monitor_cancel (monitor_handle); monitor_handle = NULL; } if (motd_contents) { g_free (motd_contents); motd_contents = NULL; } }