main.c 27.5 KB
Newer Older
Roger Seguin's avatar
Roger Seguin committed
1 2
/*
 *  Generic Monitor plugin for the Xfce4 panel
3
 *  Main file for the Genmon plugin
Roger Seguin's avatar
Roger Seguin committed
4
 *  Copyright (c) 2004 Roger Seguin <roger_seguin@msn.com>
5
 *                                  <http://rmlx.dyndns.org>
6
 *  Copyright (c) 2006 Julien Devemy <jujucece@gmail.com>
Roger Seguin's avatar
Roger Seguin committed
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
 *  
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.

 *  This library 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
 *  Lesser General Public License for more details.

 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config_gui.h"
#include "cmdspawn.h"


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gtk/gtk.h>

33
#include <libxfce4util/libxfce4util.h>
Roger Seguin's avatar
Roger Seguin committed
34
#include <libxfcegui4/dialogs.h>
35
#include <libxfce4panel/xfce-panel-plugin.h>
Roger Seguin's avatar
Roger Seguin committed
36 37 38 39 40 41 42 43 44 45 46

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>
#include <inttypes.h>
#include <string.h>
#include <errno.h>


47 48
#define PLUGIN_NAME    "GenMon"
#define BORDER    2
Roger Seguin's avatar
Roger Seguin committed
49 50 51 52 53 54


typedef GtkWidget *Widget_t;

typedef struct param_t {
    /* Configurable parameters */
55
    char            acCmd[128]; /* Commandline to spawn */
Roger Seguin's avatar
Roger Seguin committed
56 57 58 59 60 61 62 63
    int             fTitleDisplayed;
    char            acTitle[16];
    uint32_t        iPeriod_ms;
    char            acFont[128];
} param_t;

typedef struct conf_t {
    Widget_t        wTopLevel;
64
    struct gui_t    oGUI; /* Configuration/option dialog */
Roger Seguin's avatar
Roger Seguin committed
65 66 67 68 69 70 71
    struct param_t  oParam;
} conf_t;

typedef struct monitor_t {
    /* Plugin monitor */
    Widget_t        wEventBox;
    Widget_t        wBox;
72
    Widget_t        wImgBox;
Roger Seguin's avatar
Roger Seguin committed
73 74
    Widget_t        wTitle;
    Widget_t        wValue;
75 76 77 78
    Widget_t        wImage;
    Widget_t        wBar;
    Widget_t        wButton;
    Widget_t        wImgButton;
79
    char            onClickCmd[256];
Roger Seguin's avatar
Roger Seguin committed
80 81
} monitor_t;

82 83
typedef struct genmon_t {
    XfcePanelPlugin *plugin;
84
    unsigned int    iTimerId; /* Cyclic update */
Roger Seguin's avatar
Roger Seguin committed
85
    struct conf_t   oConf;
86 87
    struct monitor_t    oMonitor;
    char            acValue[256]; /* Commandline resulting string */
88
} genmon_t;
Roger Seguin's avatar
Roger Seguin committed
89

90
/**************************************************************/
91
static void ExecOnClickCmd (Widget_t p_wSc, void *p_pvPlugin)
92
/* Execute the onClick Command */
93
{
94 95 96
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
    struct monitor_t *poMonitor = &(poPlugin->oMonitor);
    char result[256];
97

98 99
    genmon_SpawnCmd (poMonitor->onClickCmd, result,
        sizeof (poMonitor->onClickCmd), 0);
100
}
Roger Seguin's avatar
Roger Seguin committed
101

102
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
103

104
static int DisplayCmdOutput (struct genmon_t *p_poPlugin)
Roger Seguin's avatar
Roger Seguin committed
105 106 107 108 109 110 111
 /* Launch the command, get its output and display it in the panel-docked
    text field */
{
    static GtkTooltips *s_poToolTips = 0;

    struct param_t *poConf = &(p_poPlugin->oConf.oParam);
    struct monitor_t *poMonitor = &(p_poPlugin->oMonitor);
112
    char            acToolTips[256];
Roger Seguin's avatar
Roger Seguin committed
113
    int             status;
114 115 116
    char  *begin;
    char  *end;
    int   newVersion=0;
117

Roger Seguin's avatar
Roger Seguin committed
118
    if (!s_poToolTips)
119
        s_poToolTips = gtk_tooltips_new ();
Roger Seguin's avatar
Roger Seguin committed
120
    status = genmon_SpawnCmd (poConf->acCmd, p_poPlugin->acValue,
121
        sizeof (p_poPlugin->acValue), 1);
Roger Seguin's avatar
Roger Seguin committed
122
    if (status == -1)
123 124
        return (-1);

125 126
    /* Normally it's impossible to overflow the buffer because p_poPlugin->acValue is < 256 */ 

127 128 129
    /* Test if the result is an Image or a Text */
    begin=strstr(p_poPlugin->acValue, "<img>");
    end=strstr(p_poPlugin->acValue, "</img>");
130
    if ((begin != NULL) && (end != NULL) && (begin < end) && (end-begin < 256*sizeof(char)))
131 132
    {
        char  buf[256];
133 134 135 136 137 138 139 140 141 142
        /* Get the image path */
        strncpy(buf, begin+5*sizeof(char), end-begin-5*sizeof(char));
        buf[end-begin-5*sizeof(char)]='\0';

        gtk_image_set_from_file (GTK_IMAGE (poMonitor->wImage), buf);
        gtk_image_set_from_file (GTK_IMAGE (poMonitor->wImgButton), buf);

        /* Test if the result has a clickable Image (button) */
        begin=strstr(p_poPlugin->acValue, "<click>");
        end=strstr(p_poPlugin->acValue, "</click>");
143
        if ((begin != NULL) && (end != NULL) && (begin < end) && (end-begin < 256*sizeof(char)))
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
        {
            char  buf[256];
            /* Get the command path */
            strncpy(buf, begin+7*sizeof(char), end-begin-7*sizeof(char));
            buf[end-begin-7*sizeof(char)]='\0';

            strcpy(poMonitor->onClickCmd, buf);

            gtk_widget_show (poMonitor->wButton);
            gtk_widget_show (poMonitor->wImgButton);
            gtk_widget_hide (poMonitor->wImage);
        }
        else
        {
            gtk_widget_hide (poMonitor->wButton);
            gtk_widget_hide (poMonitor->wImgButton);
            gtk_widget_show (poMonitor->wImage);
        }

        newVersion=1;
164 165 166

    }
    else
167
        gtk_widget_hide (poMonitor->wImage);
168 169 170 171

    /* Test if the result is a Text */
    begin=strstr(p_poPlugin->acValue, "<txt>");
    end=strstr(p_poPlugin->acValue, "</txt>");
172
    if ((begin != NULL) && (end != NULL) && (begin < end) && (end-begin < 256*sizeof(char)))
173
    {
174 175 176 177 178 179
        char  buf[256];
        /* Get the text */
        strncpy(buf, begin+5*sizeof(char), end-begin-5*sizeof(char));
        buf[end-begin-5*sizeof(char)]='\0';
        gtk_label_set_text (GTK_LABEL (poMonitor->wValue), buf);
        gtk_widget_show (poMonitor->wValue);
180

181
        newVersion=1;
182 183 184

    }
    else
185
        gtk_widget_hide (poMonitor->wValue);
186 187 188 189

    /* Test if the result is a Bar */
    begin=strstr(p_poPlugin->acValue, "<bar>");
    end=strstr(p_poPlugin->acValue, "</bar>");
190
    if ((begin != NULL) && (end != NULL) && (begin < end) && (end-begin < 256*sizeof(char)))
191
    {
192 193 194 195 196 197
        char  buf[256];
        int value;
        /* Get the text */
        strncpy(buf, begin+5*sizeof(char), end-begin-5*sizeof(char));
        buf[end-begin-5*sizeof(char)]='\0';
        value=atoi(buf);
198 199
        if (value>100)
            value=100;
200 201
        gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(poMonitor->wBar), (float)value/100.0);
        gtk_widget_show (poMonitor->wBar);
202

203
        newVersion=1;
204 205 206

    }
    else
207 208
        gtk_widget_hide (poMonitor->wBar);

209
    if (newVersion == 0)
210 211 212 213
    {
        gtk_widget_show (poMonitor->wValue);
        gtk_label_set_text (GTK_LABEL (poMonitor->wValue),
            p_poPlugin->acValue);
214
    }
215

216 217 218
    /* Test if a ToolTip is given */
    begin=strstr(p_poPlugin->acValue, "<tool>");
    end=strstr(p_poPlugin->acValue, "</tool>");
219
    if ((begin != NULL) && (end != NULL) && (begin < end) && (end-begin < 256*sizeof(char)))
220
    {
221 222
        strncpy(acToolTips, begin+6, end-begin-6);
        acToolTips[end-begin-6]='\0';
223 224
    }
    else
225 226 227 228 229
        sprintf (acToolTips, "%s\n"
            "----------------\n"
            "%s\n"
            "Period (s): %d", poConf->acTitle, poConf->acCmd,
            poConf->iPeriod_ms / 1000);
230

Roger Seguin's avatar
Roger Seguin committed
231
    gtk_tooltips_set_tip (s_poToolTips, GTK_WIDGET (poMonitor->wEventBox),
232
        acToolTips, 0);
Roger Seguin's avatar
Roger Seguin committed
233 234 235

    return (0);

236 237 238
}/* DisplayCmdOutput() */

/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
239 240

static gboolean SetTimer (void *p_pvPlugin)
241 242 243
/* Recurrently update the panel-docked monitor through a timer */
/* Warning : should not be called directly (except the 1st time) */
/* To avoid multiple timers */
Roger Seguin's avatar
Roger Seguin committed
244
{
245
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
246 247 248
    struct param_t *poConf = &(poPlugin->oConf.oParam);

    DisplayCmdOutput (poPlugin);
249 250

    if (poPlugin->iTimerId == 0)
251
    {
252
        poPlugin->iTimerId = g_timeout_add (poConf->iPeriod_ms,
253
            (GSourceFunc) SetTimer, poPlugin);
254 255 256
        return FALSE;
    }

257
    return TRUE;
258
}/* SetTimer() */
Roger Seguin's avatar
Roger Seguin committed
259

260
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
261

262
static genmon_t *genmon_create_control (XfcePanelPlugin *plugin)
263 264
/* Plugin API */
/* Create the plugin */
Roger Seguin's avatar
Roger Seguin committed
265
{
266
    struct genmon_t *poPlugin;
Roger Seguin's avatar
Roger Seguin committed
267 268
    struct param_t *poConf;
    struct monitor_t *poMonitor;
269
    int size = xfce_panel_plugin_get_size (plugin);
Roger Seguin's avatar
Roger Seguin committed
270

271 272
    poPlugin = g_new (genmon_t, 1);
    memset (poPlugin, 0, sizeof (genmon_t));
Roger Seguin's avatar
Roger Seguin committed
273 274 275
    poConf = &(poPlugin->oConf.oParam);
    poMonitor = &(poPlugin->oMonitor);

276 277
    poPlugin->plugin = plugin;
    
Roger Seguin's avatar
Roger Seguin committed
278 279 280 281 282 283 284 285 286 287 288 289 290
    strcpy (poConf->acCmd, "");
    strcpy (poConf->acTitle, "(genmon)");

    poConf->fTitleDisplayed = 1;

    poConf->iPeriod_ms = 30 * 1000;
    poPlugin->iTimerId = 0;

    strcpy (poConf->acFont, "(default)");

    poMonitor->wEventBox = gtk_event_box_new ();
    gtk_widget_show (poMonitor->wEventBox);

291 292
    xfce_panel_plugin_add_action_widget (plugin, poMonitor->wEventBox);
    
293 294
    if (xfce_panel_plugin_get_orientation (plugin) == GTK_ORIENTATION_HORIZONTAL)
        poMonitor->wBox = gtk_hbox_new (FALSE, 0);
295
    else
296
        poMonitor->wBox = gtk_vbox_new (FALSE, 0);
297 298
    gtk_widget_show (poMonitor->wBox);
    gtk_container_set_border_width (GTK_CONTAINER
299
        (poMonitor->wBox), BORDER);
300
    gtk_container_add (GTK_CONTAINER (poMonitor->wEventBox),
301
        poMonitor->wBox);
Roger Seguin's avatar
Roger Seguin committed
302

303 304
    poMonitor->wTitle = gtk_label_new (poConf->acTitle);
    if (poConf->fTitleDisplayed)
305
        gtk_widget_show (poMonitor->wTitle);
306
    gtk_box_pack_start (GTK_BOX (poMonitor->wBox),
307
        GTK_WIDGET (poMonitor->wTitle), FALSE, FALSE, 0);
Roger Seguin's avatar
Roger Seguin committed
308

309
    /* Create a Box to put image and text 
310 311 312 313 314 315
     * If panel size less < 25 place icon near the text
     */
    if (size < 25)
        poMonitor->wImgBox = gtk_hbox_new (FALSE, 0);
    else
        poMonitor->wImgBox = gtk_vbox_new (FALSE, 0);
316 317
    gtk_widget_show (poMonitor->wImgBox);
    gtk_container_set_border_width (GTK_CONTAINER
318
        (poMonitor->wImgBox), 0);
319
    gtk_container_add (GTK_CONTAINER (poMonitor->wBox),
320 321
        poMonitor->wImgBox);

322 323 324
    /* Add Image */   
    poMonitor->wImage = gtk_image_new ();
    gtk_box_pack_start (GTK_BOX (poMonitor->wImgBox),
325
        GTK_WIDGET (poMonitor->wImage), TRUE, FALSE, 0);
326 327

    /* Add Button */ 
328
    /*
329 330 331 332
     * xfce_create_panel_button() look better instead of gtk_button_new ()
     * poMonitor->wButton = gtk_button_new ();
     */
    poMonitor->wButton = (Widget_t) xfce_create_panel_button ();
333
    gtk_widget_set_size_request (poMonitor->wButton, size, size);
334
    gtk_box_pack_start (GTK_BOX (poMonitor->wImgBox),
335
        GTK_WIDGET (poMonitor->wButton), TRUE, FALSE, 0);
336 337 338

    /* Add Image Button*/   
    poMonitor->wImgButton = gtk_image_new ();
339
    gtk_widget_set_size_request (poMonitor->wImgButton, size-1, size-1);
340 341 342 343
    gtk_container_add (GTK_CONTAINER (poMonitor->wButton), poMonitor->wImgButton);
    gtk_container_set_border_width (GTK_CONTAINER (poMonitor->wButton), 0);

    /* Add Value */
344 345
    poMonitor->wValue = gtk_label_new ("");
    gtk_widget_show (poMonitor->wValue);
346
    gtk_box_pack_start (GTK_BOX (poMonitor->wImgBox),
347
        GTK_WIDGET (poMonitor->wValue), TRUE, FALSE, 0);
348 349 350

    /* Add Bar */   
    poMonitor->wBar = gtk_progress_bar_new();
351
    gtk_box_pack_start (GTK_BOX (poMonitor->wBox),
352
        GTK_WIDGET (poMonitor->wBar), FALSE, FALSE, 0);
353 354
    /*gtk_widget_show (poMonitor->wBar);*/
    if (xfce_panel_plugin_get_orientation (plugin) == GTK_ORIENTATION_HORIZONTAL)
355
        gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(poMonitor->wBar), GTK_PROGRESS_BOTTOM_TO_TOP);
356
    else
357
        gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(poMonitor->wBar), GTK_PROGRESS_LEFT_TO_RIGHT);
358 359

    return poPlugin;
360
}/* genmon_create_control() */
Roger Seguin's avatar
Roger Seguin committed
361

362
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
363

364
static void genmon_free (XfcePanelPlugin *plugin, genmon_t *poPlugin)
365
/* Plugin API */
Roger Seguin's avatar
Roger Seguin committed
366
{
367
    TRACE ("genmon_free()\n");
Roger Seguin's avatar
Roger Seguin committed
368 369

    if (poPlugin->iTimerId)
370
        g_source_remove (poPlugin->iTimerId);
Roger Seguin's avatar
Roger Seguin committed
371
    g_free (poPlugin);
372
}/* genmon_free() */
Roger Seguin's avatar
Roger Seguin committed
373

374
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
375 376 377

static int SetMonitorFont (void *p_pvPlugin)
{
378
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
379 380 381
    struct monitor_t *poMonitor = &(poPlugin->oMonitor);
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    const char     *pcFont = poConf->acFont;
382
    PangoFontDescription *poFont;
Roger Seguin's avatar
Roger Seguin committed
383

384 385
    if (*pcFont == '(') /* Default font */
        return (1);
Roger Seguin's avatar
Roger Seguin committed
386 387
    poFont = pango_font_description_from_string (poConf->acFont);
    if (!poFont)
388
        return (-1);
Roger Seguin's avatar
Roger Seguin committed
389 390 391
    gtk_widget_modify_font (poMonitor->wTitle, poFont);
    gtk_widget_modify_font (poMonitor->wValue, poFont);
    return (0);
392
}/* SetMonitorFont() */
Roger Seguin's avatar
Roger Seguin committed
393

394
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
395

396 397 398 399 400 401
/* Configuration Keywords */
#define CONF_USE_LABEL    "UseLabel"
#define CONF_LABEL_TEXT    "Text"
#define CONF_CMD    "Command"
#define CONF_UPDATE_PERIOD    "UpdatePeriod"
#define CONF_FONT    "Font"
Roger Seguin's avatar
Roger Seguin committed
402

403
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
404

405
static void genmon_read_config (XfcePanelPlugin *plugin, genmon_t *poPlugin)
406 407 408
/* Plugin API */
/* Executed when the panel is started - Read the configuration
   previously stored in xml file */
Roger Seguin's avatar
Roger Seguin committed
409 410 411
{
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    struct monitor_t *poMonitor = &(poPlugin->oMonitor);
412 413 414
    const char           *pc;
    char           *file;
    XfceRc         *rc;
415

416 417
    if (!(file = xfce_panel_plugin_lookup_rc_file (plugin)))
        return;
418

419 420 421 422 423
    rc = xfce_rc_simple_open (file, TRUE);
    g_free (file);

    if (!rc)
        return;
424

425 426 427 428
    if ((pc = xfce_rc_read_entry (rc, (CONF_CMD), NULL))) {
        memset (poConf->acCmd, 0, sizeof (poConf->acCmd));
        strncpy (poConf->acCmd, pc, sizeof (poConf->acCmd) - 1);
    }
Roger Seguin's avatar
Roger Seguin committed
429

430 431 432 433 434
    poConf->fTitleDisplayed = xfce_rc_read_int_entry (rc, (CONF_USE_LABEL), 1);
    if (poConf->fTitleDisplayed)
        gtk_widget_show (GTK_WIDGET (poMonitor->wTitle));
    else
        gtk_widget_hide (GTK_WIDGET (poMonitor->wTitle));
Roger Seguin's avatar
Roger Seguin committed
435

436 437 438 439 440 441
    if ((pc = xfce_rc_read_entry (rc, (CONF_LABEL_TEXT), NULL))) {
        memset (poConf->acTitle, 0, sizeof (poConf->acTitle));
        strncpy (poConf->acTitle, pc, sizeof (poConf->acTitle) - 1);
        gtk_label_set_text (GTK_LABEL (poMonitor->wTitle),
                            poConf->acTitle);
    }
Roger Seguin's avatar
Roger Seguin committed
442

443 444
    poConf->iPeriod_ms = 
        xfce_rc_read_int_entry (rc, (CONF_UPDATE_PERIOD), 30 * 1000);
Roger Seguin's avatar
Roger Seguin committed
445

446 447 448
    if ((pc = xfce_rc_read_entry (rc, (CONF_FONT), NULL))) {
        memset (poConf->acFont, 0, sizeof (poConf->acFont));
        strncpy (poConf->acFont, pc, sizeof (poConf->acFont) - 1);
Roger Seguin's avatar
Roger Seguin committed
449
    }
450 451

    xfce_rc_close (rc);
452
}/* genmon_read_config() */
Roger Seguin's avatar
Roger Seguin committed
453

454
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
455

456
static void genmon_write_config (XfcePanelPlugin *plugin, genmon_t *poPlugin)
457 458
/* Plugin API */
/* Write plugin configuration into xml file */
Roger Seguin's avatar
Roger Seguin committed
459 460
{
    struct param_t *poConf = &(poPlugin->oConf.oParam);
461 462 463 464 465 466 467 468
    XfceRc *rc;
    char *file;

    if (!(file = xfce_panel_plugin_save_location (plugin, TRUE)))
        return;
    
    rc = xfce_rc_simple_open (file, FALSE);
    g_free (file);
Roger Seguin's avatar
Roger Seguin committed
469

470 471
    if (!rc)
        return;
Roger Seguin's avatar
Roger Seguin committed
472

473
    TRACE ("genmon_write_config()\n");
Roger Seguin's avatar
Roger Seguin committed
474

475
    xfce_rc_write_entry (rc, CONF_CMD, poConf->acCmd);
Roger Seguin's avatar
Roger Seguin committed
476

477
    xfce_rc_write_int_entry (rc, CONF_USE_LABEL, poConf->fTitleDisplayed);
Roger Seguin's avatar
Roger Seguin committed
478

479
    xfce_rc_write_entry (rc, CONF_LABEL_TEXT, poConf->acTitle);
Roger Seguin's avatar
Roger Seguin committed
480

481
    xfce_rc_write_int_entry (rc, CONF_UPDATE_PERIOD, poConf->iPeriod_ms);
Roger Seguin's avatar
Roger Seguin committed
482

483 484 485
    xfce_rc_write_entry (rc, CONF_FONT, poConf->acFont);

    xfce_rc_close (rc);
486
}/* genmon_write_config() */
Roger Seguin's avatar
Roger Seguin committed
487

488
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
489 490

static void SetCmd (Widget_t p_wTF, void *p_pvPlugin)
491 492
/* GUI callback setting the command to be spawn, whose output will 
   be displayed using the panel-docked monitor */
Roger Seguin's avatar
Roger Seguin committed
493
{
494
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
495 496 497 498 499
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    const char     *pcCmd = gtk_entry_get_text (GTK_ENTRY (p_wTF));

    memset (poConf->acCmd, 0, sizeof (poConf->acCmd));
    strncpy (poConf->acCmd, pcCmd, sizeof (poConf->acCmd) - 1);
500
}/* SetCmd() */
Roger Seguin's avatar
Roger Seguin committed
501

502
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
503 504

static void ToggleTitle (Widget_t p_w, void *p_pvPlugin)
505
/* GUI callback turning on/off the monitor bar legend */
Roger Seguin's avatar
Roger Seguin committed
506
{
507
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
508 509 510 511 512
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    struct gui_t   *poGUI = &(poPlugin->oConf.oGUI);
    struct monitor_t *poMonitor = &(poPlugin->oMonitor);

    poConf->fTitleDisplayed =
513
        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (p_w));
Roger Seguin's avatar
Roger Seguin committed
514
    gtk_widget_set_sensitive (GTK_WIDGET (poGUI->wTF_Title),
515
        poConf->fTitleDisplayed);
Roger Seguin's avatar
Roger Seguin committed
516
    if (poConf->fTitleDisplayed)
517
        gtk_widget_show (GTK_WIDGET (poMonitor->wTitle));
Roger Seguin's avatar
Roger Seguin committed
518
    else
519 520
        gtk_widget_hide (GTK_WIDGET (poMonitor->wTitle));
}/* ToggleTitle() */
Roger Seguin's avatar
Roger Seguin committed
521

522
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
523 524

static void SetLabel (Widget_t p_wTF, void *p_pvPlugin)
525
/* GUI callback setting the legend of the monitor */
Roger Seguin's avatar
Roger Seguin committed
526
{
527
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
528 529 530 531 532 533 534
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    struct monitor_t *poMonitor = &(poPlugin->oMonitor);
    const char     *acTitle = gtk_entry_get_text (GTK_ENTRY (p_wTF));

    memset (poConf->acTitle, 0, sizeof (poConf->acTitle));
    strncpy (poConf->acTitle, acTitle, sizeof (poConf->acTitle) - 1);
    gtk_label_set_text (GTK_LABEL (poMonitor->wTitle), poConf->acTitle);
535
}/* SetLabel() */
Roger Seguin's avatar
Roger Seguin committed
536

537
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
538 539

static void SetPeriod (Widget_t p_wSc, void *p_pvPlugin)
540
/* Set the update period - To be used by the timer */
Roger Seguin's avatar
Roger Seguin committed
541
{
542
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
543 544 545 546 547 548
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    float           r;

    TRACE ("SetPeriod()\n");
    r = gtk_spin_button_get_value (GTK_SPIN_BUTTON (p_wSc));
    poConf->iPeriod_ms = (r * 1000);
549
}/* SetPeriod() */
Roger Seguin's avatar
Roger Seguin committed
550

551
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
552

553
static void UpdateConf (void *p_pvPlugin)
554
/* Called back when the configuration/options window is closed */
Roger Seguin's avatar
Roger Seguin committed
555
{
556
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
557 558 559 560 561 562 563
    struct conf_t  *poConf = &(poPlugin->oConf);
    struct gui_t   *poGUI = &(poConf->oGUI);

    TRACE ("UpdateConf()\n");
    SetCmd (poGUI->wTF_Cmd, poPlugin);
    SetLabel (poGUI->wTF_Title, poPlugin);
    SetMonitorFont (poPlugin);
564 565
    /* Force to regenerate a timer */
    poPlugin->iTimerId = 0;
566
}/* UpdateConf() */
Roger Seguin's avatar
Roger Seguin committed
567

568
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
569 570

static void About (Widget_t w, void *unused)
571
/* Called back when the About button in clicked */
Roger Seguin's avatar
Roger Seguin committed
572
{
573
    xfce_info (_("%s %s - Generic Monitor\n"
574 575 576 577 578 579
        "Cyclically spawns a script/program, captures its output "
        "and displays the resulting string in the panel\n\n"
        "(c) 2004 Roger Seguin <roger_seguin@msn.com>\n"
        "(c) 2006 Julien Devemy <jujucece@gmail.com>"),
        PACKAGE, VERSION);
}/* About() */
Roger Seguin's avatar
Roger Seguin committed
580

581
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
582 583 584

static void ChooseFont (Widget_t p_wPB, void *p_pvPlugin)
{
585
    struct genmon_t *poPlugin = (genmon_t *) p_pvPlugin;
Roger Seguin's avatar
Roger Seguin committed
586 587 588 589 590 591 592
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    Widget_t        wDialog;
    const char     *pcFont = poConf->acFont;
    int             iResponse;

    wDialog = gtk_font_selection_dialog_new (_("Font Selection"));
    gtk_window_set_transient_for (GTK_WINDOW (wDialog),
593 594 595 596
        GTK_WINDOW (poPlugin->oConf.wTopLevel));
    if (*pcFont != '(') /* Default font */
        gtk_font_selection_dialog_set_font_name (GTK_FONT_SELECTION_DIALOG
            (wDialog), pcFont);
Roger Seguin's avatar
Roger Seguin committed
597 598
    iResponse = gtk_dialog_run (GTK_DIALOG (wDialog));
    if (iResponse == GTK_RESPONSE_OK) {
599 600 601 602 603 604
        pcFont = gtk_font_selection_dialog_get_font_name
            (GTK_FONT_SELECTION_DIALOG (wDialog));
        if (pcFont && (strlen (pcFont) < sizeof (poConf->acFont) - 1)) {
            strcpy (poConf->acFont, pcFont);
            gtk_button_set_label (GTK_BUTTON (p_wPB), poConf->acFont);
        }
Roger Seguin's avatar
Roger Seguin committed
605 606
    }
    gtk_widget_destroy (wDialog);
607
}/* ChooseFont() */
Roger Seguin's avatar
Roger Seguin committed
608

609
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
610

611
static void genmon_dialog_response (GtkWidget *dlg, int response, 
612
    genmon_t *genmon)
613 614 615 616 617
{
    UpdateConf (genmon);
    gtk_widget_destroy (dlg);
    xfce_panel_plugin_unblock_menu (genmon->plugin);
    genmon_write_config (genmon->plugin, genmon);
618 619
    /* Do not wait the next timer to update display */
    DisplayCmdOutput (genmon);
620 621 622
}

static void genmon_create_options (XfcePanelPlugin *plugin,
623 624 625
    genmon_t *poPlugin)
/* Plugin API */
/* Create/pop up the configuration/options GUI */
Roger Seguin's avatar
Roger Seguin committed
626
{
627
    GtkWidget *dlg, *header, *vbox;
Roger Seguin's avatar
Roger Seguin committed
628 629 630 631
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    struct gui_t   *poGUI = &(poPlugin->oConf.oGUI);
    const char     *pcFont = poConf->acFont;

632 633 634 635 636
    TRACE ("genmon_create_options()\n");

    xfce_panel_plugin_block_menu (plugin);
    
    dlg = gtk_dialog_new_with_buttons (_("Configuration"), 
637 638 639 640 641 642
        GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (plugin))),
        GTK_DIALOG_DESTROY_WITH_PARENT |
        GTK_DIALOG_NO_SEPARATOR,
        GTK_STOCK_CLOSE, GTK_RESPONSE_OK,
        NULL);

643
    g_signal_connect (dlg, "response", G_CALLBACK (genmon_dialog_response),
644
        poPlugin);
645 646

    gtk_container_set_border_width (GTK_CONTAINER (dlg), 2);
647

648 649 650 651 652
    header = xfce_create_header (NULL, _("Generic Monitor"));
    gtk_widget_set_size_request (GTK_BIN (header)->child, -1, 32);
    gtk_container_set_border_width (GTK_CONTAINER (header), BORDER - 2);
    gtk_widget_show (header);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), header,
653 654
        FALSE, TRUE, 0);

655 656 657 658
    vbox = gtk_vbox_new(FALSE, BORDER);
    gtk_container_set_border_width (GTK_CONTAINER (vbox), BORDER - 2);
    gtk_widget_show(vbox);
    gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dlg)->vbox), vbox,
659 660
    TRUE, TRUE, 0);

661 662 663
    poPlugin->oConf.wTopLevel = dlg;

    (void) genmon_CreateConfigGUI (GTK_WIDGET (vbox), poGUI);
Roger Seguin's avatar
Roger Seguin committed
664 665

    g_signal_connect (GTK_WIDGET (poGUI->wPB_About), "clicked",
666
        G_CALLBACK (About), 0);
Roger Seguin's avatar
Roger Seguin committed
667 668

    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (poGUI->wTB_Title),
669
        poConf->fTitleDisplayed);
Roger Seguin's avatar
Roger Seguin committed
670
    gtk_widget_set_sensitive (GTK_WIDGET (poGUI->wTF_Title),
671
        poConf->fTitleDisplayed);
Roger Seguin's avatar
Roger Seguin committed
672
    g_signal_connect (GTK_WIDGET (poGUI->wTB_Title), "toggled",
673
        G_CALLBACK (ToggleTitle), poPlugin);
Roger Seguin's avatar
Roger Seguin committed
674 675 676

    gtk_entry_set_text (GTK_ENTRY (poGUI->wTF_Cmd), poConf->acCmd);
    g_signal_connect (GTK_WIDGET (poGUI->wTF_Cmd), "activate",
677
        G_CALLBACK (SetCmd), poPlugin);
Roger Seguin's avatar
Roger Seguin committed
678 679 680

    gtk_entry_set_text (GTK_ENTRY (poGUI->wTF_Title), poConf->acTitle);
    g_signal_connect (GTK_WIDGET (poGUI->wTF_Title), "activate",
681
        G_CALLBACK (SetLabel), poPlugin);
Roger Seguin's avatar
Roger Seguin committed
682 683

    gtk_spin_button_set_value (GTK_SPIN_BUTTON (poGUI->wSc_Period),
684
        ((double) poConf->iPeriod_ms / 1000));
Roger Seguin's avatar
Roger Seguin committed
685
    g_signal_connect (GTK_WIDGET (poGUI->wSc_Period), "value_changed",
686
        G_CALLBACK (SetPeriod), poPlugin);
Roger Seguin's avatar
Roger Seguin committed
687

688 689 690
    if (*pcFont != '(') /* Default font */
        gtk_button_set_label (GTK_BUTTON (poGUI->wPB_Font),
        poConf->acFont);
Roger Seguin's avatar
Roger Seguin committed
691
    g_signal_connect (G_OBJECT (poGUI->wPB_Font), "clicked",
692
        G_CALLBACK (ChooseFont), poPlugin);
Roger Seguin's avatar
Roger Seguin committed
693

694
    gtk_widget_show (dlg);
695
}/* genmon_create_options() */
Roger Seguin's avatar
Roger Seguin committed
696

697
/**************************************************************/
Roger Seguin's avatar
Roger Seguin committed
698

699
static void genmon_set_orientation (XfcePanelPlugin *plugin, 
700 701 702 703
    GtkOrientation p_iOrientation,
    genmon_t *poPlugin)
/* Plugin API */
/* Invoked when the panel changes orientation */
Roger Seguin's avatar
Roger Seguin committed
704 705 706 707
{
    struct param_t *poConf = &(poPlugin->oConf.oParam);
    struct monitor_t *poMonitor = &(poPlugin->oMonitor);

708
    TRACE ("genmon_set_orientation()\n");
Roger Seguin's avatar
Roger Seguin committed
709 710

    gtk_container_remove (GTK_CONTAINER (poMonitor->wEventBox),
711
        GTK_WIDGET (poMonitor->wBox));
712
    if (p_iOrientation == GTK_ORIENTATION_HORIZONTAL)
713
        poMonitor->wBox = gtk_hbox_new (FALSE, 0);
Roger Seguin's avatar
Roger Seguin committed
714
    else
715
        poMonitor->wBox = gtk_vbox_new (FALSE, 0);
Roger Seguin's avatar
Roger Seguin committed
716 717
    gtk_widget_show (poMonitor->wBox);
    gtk_container_set_border_width (GTK_CONTAINER
718
        (poMonitor->wBox), BORDER);
Roger Seguin's avatar
Roger Seguin committed
719
    gtk_container_add (GTK_CONTAINER (poMonitor->wEventBox),
720
        poMonitor->wBox);
Roger Seguin's avatar
Roger Seguin committed
721 722 723

    poMonitor->wTitle = gtk_label_new (poConf->acTitle);
    if (poConf->fTitleDisplayed)
724
        gtk_widget_show (poMonitor->wTitle);
Roger Seguin's avatar
Roger Seguin committed
725
    gtk_box_pack_start (GTK_BOX (poMonitor->wBox),
726
        GTK_WIDGET (poMonitor->wTitle), FALSE, FALSE, 0);
Roger Seguin's avatar
Roger Seguin committed
727

728 729 730 731
    /* Create a VBox to put image and text */
    poMonitor->wImgBox = gtk_vbox_new (FALSE, 0);
    gtk_widget_show (poMonitor->wImgBox);
    gtk_container_set_border_width (GTK_CONTAINER
732
        (poMonitor->wImgBox), 0);
733
    gtk_container_add (GTK_CONTAINER (poMonitor->wBox),
734
        poMonitor->wImgBox);
735 736 737 738
 
    /* Add Image */   
    poMonitor->wImage = gtk_image_new ();
    gtk_box_pack_start (GTK_BOX (poMonitor->wImgBox),
739
        GTK_WIDGET (poMonitor->wImage), TRUE, FALSE, 0);
740 741 742 743

    /* Add Button */   
    poMonitor->wButton = gtk_button_new ();
    gtk_box_pack_start (GTK_BOX (poMonitor->wImgBox),
744
        GTK_WIDGET (poMonitor->wButton), TRUE, FALSE, 0);
745 746 747 748 749 750 751

    /* Add Image Button*/   
    poMonitor->wImgButton = gtk_image_new ();
    gtk_container_add (GTK_CONTAINER (poMonitor->wButton), poMonitor->wImgButton);
    gtk_container_set_border_width (GTK_CONTAINER (poMonitor->wButton), 0);

     /* Add Value */
Roger Seguin's avatar
Roger Seguin committed
752 753
    poMonitor->wValue = gtk_label_new ("");
    gtk_widget_show (poMonitor->wValue);
754
    gtk_box_pack_start (GTK_BOX (poMonitor->wImgBox),
755
        GTK_WIDGET (poMonitor->wValue), TRUE, FALSE, 0);
Roger Seguin's avatar
Roger Seguin committed
756

757 758 759
    /* Add Bar */   
    poMonitor->wBar = gtk_progress_bar_new();
    gtk_box_pack_start (GTK_BOX (poMonitor->wBox),
760
        GTK_WIDGET (poMonitor->wBar), FALSE, FALSE, 0);
761 762
    /*gtk_widget_show (poMonitor->wBar);*/
    if (p_iOrientation == GTK_ORIENTATION_HORIZONTAL)
763
        gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(poMonitor->wBar), GTK_PROGRESS_BOTTOM_TO_TOP);
764
    else
765 766
        gtk_progress_bar_set_orientation(GTK_PROGRESS_BAR(poMonitor->wBar), GTK_PROGRESS_LEFT_TO_RIGHT);

Roger Seguin's avatar
Roger Seguin committed
767 768
    SetMonitorFont (poPlugin);

769
}/* genmon_set_orientation() */
Roger Seguin's avatar
Roger Seguin committed
770

771
/**************************************************************/
772
static gboolean genmon_set_size (XfcePanelPlugin *plugin, int size, genmon_t *poPlugin)
773 774
/* Plugin API */
/* Set the size of the panel-docked monitor */
775 776
{

777
    struct monitor_t *poMonitor = &(poPlugin->oMonitor);
778

779 780 781 782 783 784 785 786 787 788
    if (xfce_panel_plugin_get_orientation (plugin) == GTK_ORIENTATION_HORIZONTAL)
    {
        if (size>BORDER)
            gtk_widget_set_size_request(GTK_WIDGET(poMonitor->wBar),8, size-BORDER*2);
    }
    else
    {
        if (size>BORDER)
            gtk_widget_set_size_request(GTK_WIDGET(poMonitor->wBar), size-BORDER*2, 8);
    }
789

790 791
    return TRUE;
}/* genmon_set_size() */