/* GKrellM giFT plugin * Copyright (C) 2002, 2003 Tilman Sauerbeck * * This program is free software; you can redistribute it and/or modify * it under the terms of version 2 of the GNU General Public License as * published by the Free Software Foundation. * * 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 #include "common.h" #include "parse.h" static GIOChannel *channel = NULL; static gchar *utf8_to_locale (const gchar *src) { const gchar *charset; if (g_get_charset (&charset)) /* charset is UTF-8 so we don't need to convert src */ return g_strdup (src); else return g_convert_with_fallback (src, strlen(src), charset, "UTF-8", " ", NULL, NULL, NULL); } void gift_daemon_send (gchar *cmd) { Interface *iface; String *buf; g_assert (channel && cmd); iface = interface_new (cmd, NULL); buf = interface_serialize (iface); interface_free (iface); g_io_channel_write_chars (channel, buf->str, -1, NULL, NULL); g_io_channel_flush (channel, NULL); string_free(buf); } gboolean gift_daemon_read() { gchar *buf = NULL; gchar *locale; gboolean cancel = FALSE; g_assert(channel); while (!cancel) switch (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)) { case G_IO_STATUS_NORMAL: if ((locale = utf8_to_locale(buf))) { gift_daemon_parse(locale); g_free(locale); } g_free(buf); break; case G_IO_STATUS_EOF: g_io_channel_unref(channel); channel = NULL; return FALSE; break; default: /* all data read */ cancel = TRUE; break; } return TRUE; } static void io_channel_create(gint fd) { gchar line_term[] = {59, 10}; /* ';\n' */ g_assert(!channel); channel = g_io_channel_unix_new(fd); g_io_channel_set_buffer_size(channel, 0); g_io_channel_set_line_term(channel, line_term, 2); g_io_channel_set_encoding(channel, "ISO-8859-1", NULL); g_io_channel_set_close_on_unref(channel, TRUE); g_io_channel_set_flags(channel, g_io_channel_get_flags(channel) | G_IO_FLAG_NONBLOCK, NULL); } gboolean gift_daemon_connect (gchar *host_name, gint port) { struct sockaddr_in addr; struct hostent *host; gint fd; g_assert(host_name && port); if ((fd = socket(AF_INET, SOCK_STREAM, 0)) != -1) { if (!(host = gethostbyname(host_name))) return FALSE; addr.sin_addr = *(struct in_addr *) host->h_addr; addr.sin_port = g_htons(port); addr.sin_family = AF_INET; if (connect(fd, (struct sockaddr *) &addr, sizeof(addr)) == -1) close (fd); else { io_channel_create(fd); return TRUE; } } return FALSE; }