summaryrefslogtreecommitdiff
path: root/invest-applet/invest/invest-applet.c
blob: 979d90fbd01ddf1192afcdaf7ea655998b1c9306 (plain)
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*
 * MATE Invest Applet
 * Copyright (C) 2004-2005 Raphael Slinckx
 * Copyright (C) 2009-2010 Enrico Minack
 * Copyright (C) 2012-2025 MATE developers
 *
 * 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
 */

#include <config.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <gtk/gtk.h>
#include <json-glib/json-glib.h>
#include <libsoup/soup.h>
#include <mate-panel-applet.h>
#include <mate-panel-applet-gsettings.h>
#include "invest-applet.h"
#include "invest-applet-chart.h"

static gchar* create_stock_tooltip (InvestApplet *applet);
static void free_stock_data (InvestApplet *applet);
static gint get_valid_stock_indices (InvestApplet *applet, gint *valid_indices);
static void update_applet_text (InvestApplet *applet, const gchar *message, gdouble change_percent);
static gboolean invest_applet_cycle_stocks (InvestApplet *applet);
static void display_stock_at_index (InvestApplet *applet, gint stock_index);
static void clear_timeout (guint *timeout_id);

G_DEFINE_TYPE (InvestApplet, invest_applet, PANEL_TYPE_APPLET);

#define DEFAULT_UPDATE_INTERVAL 15 /* 15 minutes */
#define DEFAULT_CYCLE_INTERVAL   5 /* 5 seconds */

static void
invest_applet_update_display (InvestApplet *applet)
{
    const gchar *direction_icon;

    if (applet->stock_summary) {
        if (applet->change_percent > 0) {
            direction_icon = "invest_up";
        } else if (applet->change_percent < 0) {
            direction_icon = "invest_down";
        } else {
            direction_icon = "invest_neutral";
        }
    } else {
        direction_icon = "invest_neutral";
    }

    gtk_label_set_text (GTK_LABEL (applet->label), applet->stock_summary);
    gtk_image_set_from_icon_name (GTK_IMAGE (applet->direction_icon), direction_icon, GTK_ICON_SIZE_MENU);
}

static void
on_stock_data_received (SoupSession *session,
                        SoupMessage *msg,
                        gpointer     user_data)
{
    gpointer *user_data_with_index = (gpointer *)user_data;
    InvestApplet *applet = INVEST_APPLET (user_data_with_index[0]);
    gint symbol_index = GPOINTER_TO_INT (user_data_with_index[1]);

    JsonParser *parser = NULL;
    JsonNode *root;
    JsonObject *root_obj;
    GError *error = NULL;

    if (msg->status_code != SOUP_STATUS_OK) {
        g_warning ("Failed to fetch stock data for symbol %d: %s", symbol_index, msg->reason_phrase);
        goto cleanup;
    }

    parser = json_parser_new ();
    if (!json_parser_load_from_data (parser, msg->response_body->data,
                                     msg->response_body->length, &error)) {
        g_warning ("Failed to parse JSON for symbol %d: %s", symbol_index, error->message);
        g_error_free (error);
        goto cleanup;
    }

    root = json_parser_get_root (parser);
    root_obj = json_node_get_object (root);

    /* Parse data for single stock. The expected JSON from the Yahoo Finance API should look like this:
     * {
     *   "chart": {
     *     "result": [{
     *       "meta": {
     *         "currency": "USD",
     *         "symbol": "IBM",
     *         "exchangeName": "NYQ",
     *         "instrumentType": "EQUITY",
     *         "regularMarketPrice": 288.998,
     *         "longName": "International Business Machines Corporation",
     *         "shortName": "International Business Machines",
     *         "previousClose": 291.2,
     *         ...
     *       },
     *       "timestamp": [...],
     *       "indicators": {...}
     *     }],
     *     "error": null
     *   }
     * }
     */
    if (json_object_has_member (root_obj, "chart")) {
        JsonObject *chart = json_object_get_object_member (root_obj, "chart");
        if (json_object_has_member (chart, "result") && !json_object_get_null_member (chart, "result")) {
            JsonArray *results = json_object_get_array_member (chart, "result");
            if (results && json_array_get_length (results) > 0) {
                JsonObject *result = json_array_get_object_element (results, 0);
                if (result) {
                    JsonObject *meta = json_object_get_object_member (result, "meta");

                    if (meta) {
                        gdouble current_price = json_object_get_double_member (meta, "regularMarketPrice");
                        gdouble prev_close = json_object_get_double_member (meta, "previousClose");

                        if (current_price > 0 && prev_close > 0) {
                            gdouble change_percent = ((current_price - prev_close) / prev_close) * 100.0;

                            /* store individual stock data */
                            applet->stock_prices[symbol_index] = current_price;
                            applet->stock_changes[symbol_index] = change_percent;
                            applet->stock_valid[symbol_index] = TRUE;
                        }
                    }
                }
            }
        }
    }

cleanup:
    if (parser) {
        g_object_unref (parser);
    }
    applet->pending_requests--;

    if (applet->pending_requests == 0) {
        if (applet->total_symbols > 0) {
            clear_timeout (&applet->cycle_timeout_id);

            applet->cycle_position = 0;

            gint valid_indices[applet->total_symbols];
            gint valid_count = get_valid_stock_indices (applet, valid_indices);

            if (valid_count > 0) {
                display_stock_at_index (applet, valid_indices[0]);

                /* start cycle timer if we have multiple stocks */
                if (!applet->cycle_timeout_id && valid_count > 1) {
                    applet->cycle_timeout_id = g_timeout_add_seconds (applet->cycle_interval, (GSourceFunc) invest_applet_cycle_stocks, applet);
                }

                gchar *tooltip_text = create_stock_tooltip (applet);
                gtk_widget_set_tooltip_text (GTK_WIDGET (applet), tooltip_text);
                g_free (tooltip_text);
            } else {
                update_applet_text (applet, _("No valid stock data"), 0.0);
            }
        } else {
            update_applet_text (applet, _("No valid stock data"), 0.0);
        }
    }

    g_free (user_data_with_index);
}

/* fetch stock data from Yahoo! Finance for all configured symbols */
static gboolean
invest_applet_update_stocks (gpointer user_data)
{
    InvestApplet *applet = INVEST_APPLET (user_data);
    gchar **symbols;
    gint symbol_count;

    if (!G_IS_SETTINGS (applet->settings)) {
        g_warning ("Settings not available yet");
        return G_SOURCE_CONTINUE;
    }

    symbols = g_settings_get_strv (applet->settings, "stock-symbols");

    if (!symbols || !symbols[0]) {
        update_applet_text (applet, _("No stocks configured"), 0.0);
        g_strfreev (symbols);
        return G_SOURCE_CONTINUE;
    }

    symbol_count = g_strv_length (symbols);

    free_stock_data (applet);

    applet->pending_requests = symbol_count;
    applet->total_symbols = symbol_count;
    applet->stock_symbols = g_strdupv (symbols);
    applet->stock_prices = g_malloc0 (symbol_count * sizeof (gdouble));
    applet->stock_changes = g_malloc0 (symbol_count * sizeof (gdouble));
    applet->stock_valid = g_malloc0 (symbol_count * sizeof (gboolean));

    applet->cycle_position = 0;
    clear_timeout (&applet->cycle_timeout_id);

    /* initialize networking */
    if (!applet->soup_session) {
        applet->soup_session = soup_session_new ();
    }

    /* make separate request for each stock symbol */
    for (gint i = 0; i < symbol_count; i++) {
        gchar *url = g_strdup_printf ("https://query2.finance.yahoo.com/v8/finance/chart/%s", symbols[i]);
        SoupMessage *msg = soup_message_new ("GET", url);

        /* HACK: avoid rate limiting */
        soup_message_headers_replace (msg->request_headers, "User-Agent",
                                      "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36");

        gpointer *user_data_with_index = g_malloc (2 * sizeof (gpointer));
        user_data_with_index[0] = applet;
        user_data_with_index[1] = GINT_TO_POINTER (i);

        soup_session_queue_message (applet->soup_session, msg, on_stock_data_received, user_data_with_index);
        g_free (url);
    }

    g_strfreev (symbols);
    return G_SOURCE_CONTINUE;
}

static void
invest_applet_preferences_cb (GtkAction    *action,
                              InvestApplet *applet)
{
    GtkWidget *dialog, *content_area, *entry, *label, *spin_button, *refresh_label, *cycle_label, *cycle_spin;
    GtkWidget *hbox1, *hbox2, *hbox3;
    gchar **symbols;
    gchar *symbols_text;
    gint response, refresh_interval, cycle_interval;

    dialog = gtk_dialog_new_with_buttons (_("Investment Applet Preferences"),
                                          NULL,
                                          GTK_DIALOG_MODAL,
                                          _("_Cancel"), GTK_RESPONSE_CANCEL,
                                          _("_OK"), GTK_RESPONSE_OK,
                                          NULL);

    gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
    gtk_window_set_default_size (GTK_WINDOW (dialog), 350, 150);

    content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
    gtk_container_set_border_width (GTK_CONTAINER (content_area), 12);

    /* Stock symbols */
    hbox1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
    label = gtk_label_new (_("Stock symbols:"));
    gtk_label_set_xalign (GTK_LABEL (label), 0.0);
    gtk_widget_set_size_request (label, 120, -1);
    entry = gtk_entry_new ();
    gtk_box_pack_start (GTK_BOX (hbox1), label, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (hbox1), entry, TRUE, TRUE, 0);

    /* Refresh interval */
    hbox2 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
    refresh_label = gtk_label_new (_("Refresh interval (minutes):"));
    gtk_label_set_xalign (GTK_LABEL (refresh_label), 0.0);
    gtk_widget_set_size_request (refresh_label, 120, -1);
    spin_button = gtk_spin_button_new_with_range (1, 60, 1);
    gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spin_button), 0);
    gtk_box_pack_start (GTK_BOX (hbox2), refresh_label, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (hbox2), spin_button, FALSE, FALSE, 0);

    /* Cycle interval */
    hbox3 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
    cycle_label = gtk_label_new (_("Cycle interval (seconds):"));
    gtk_label_set_xalign (GTK_LABEL (cycle_label), 0.0);
    gtk_widget_set_size_request (cycle_label, 120, -1);
    cycle_spin = gtk_spin_button_new_with_range (1, 60, 1);
    gtk_spin_button_set_digits (GTK_SPIN_BUTTON (cycle_spin), 0);
    gtk_box_pack_start (GTK_BOX (hbox3), cycle_label, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (hbox3), cycle_spin, FALSE, FALSE, 0);

    if (!G_IS_SETTINGS (applet->settings)) {
        g_warning ("Settings not available in preferences");
        gtk_widget_destroy (dialog);
        return;
    }

    symbols = g_settings_get_strv (applet->settings, "stock-symbols");
    symbols_text = g_strjoinv (",", symbols);
    gtk_entry_set_text (GTK_ENTRY (entry), symbols_text);
    g_free (symbols_text);
    g_strfreev (symbols);

    refresh_interval = g_settings_get_int (applet->settings, "refresh-interval");
    gtk_spin_button_set_value (GTK_SPIN_BUTTON (spin_button), refresh_interval);

    cycle_interval = g_settings_get_int (applet->settings, "cycle-interval");
    gtk_spin_button_set_value (GTK_SPIN_BUTTON (cycle_spin), cycle_interval);

    /* Pack all rows into content area */
    gtk_box_pack_start (GTK_BOX (content_area), hbox1, FALSE, FALSE, 6);
    gtk_box_pack_start (GTK_BOX (content_area), hbox2, FALSE, FALSE, 6);
    gtk_box_pack_start (GTK_BOX (content_area), hbox3, FALSE, FALSE, 6);

    gtk_widget_show_all (dialog);

    response = gtk_dialog_run (GTK_DIALOG (dialog));

    if (response == GTK_RESPONSE_OK) {
        const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
        gchar **new_symbols = g_strsplit (text, ",", -1);
        gint new_refresh_interval = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (spin_button));
        gint new_cycle_interval = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (cycle_spin));

        /* trim whitespaces */
        for (gint i = 0; new_symbols[i]; i++) {
            g_strstrip (new_symbols[i]);
        }

        g_settings_set_strv (applet->settings, "stock-symbols", (const gchar * const *)new_symbols);
        g_settings_set_int (applet->settings, "refresh-interval", new_refresh_interval);
        g_settings_set_int (applet->settings, "cycle-interval", new_cycle_interval);
        g_strfreev (new_symbols);

        /* Update refresh interval if changed */
        if (new_refresh_interval != applet->refresh_interval) {
            clear_timeout (&applet->update_timeout_id);
            applet->refresh_interval = new_refresh_interval;
            applet->update_timeout_id = g_timeout_add_seconds (new_refresh_interval * 60,
                                                               invest_applet_update_stocks,
                                                               applet);
        }

        /* Update cycle interval if changed */
        if (new_cycle_interval != applet->cycle_interval) {
            clear_timeout (&applet->cycle_timeout_id);
            applet->cycle_interval = new_cycle_interval;
            /* Restart cycle timer with new interval */
            if (applet->total_symbols > 0) {
                applet->cycle_timeout_id = g_timeout_add_seconds (new_cycle_interval, (GSourceFunc) invest_applet_cycle_stocks, applet);
            }
        }

        /* Trigger update */
        invest_applet_update_stocks (applet);

        /* Refresh chart data if chart is visible */
        invest_chart_refresh_data (applet->chart);
    }

    gtk_widget_destroy (dialog);
}

static void
invest_applet_refresh_cb (GtkAction    *action,
                          InvestApplet *applet)
{
    invest_applet_update_stocks (applet);
}

static void
invest_applet_show_chart_cb (GtkAction    *action,
                             InvestApplet *applet)
{
    if (invest_chart_is_visible (applet->chart)) {
        invest_chart_hide (applet->chart);
    } else {
        invest_chart_show (applet->chart);
    }
}

static void
invest_applet_help_cb (GtkAction    *action,
                       InvestApplet *applet)
{
    GError *error = NULL;

    gtk_show_uri_on_window (NULL, "help:mate-invest-applet", gtk_get_current_event_time (), &error);

    if (error) {
        GtkWidget *dialog = gtk_message_dialog_new (NULL,
                                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                                    GTK_MESSAGE_ERROR,
                                                    GTK_BUTTONS_CLOSE,
                                                    _("Could not display help"));
        gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                  "%s", error->message);
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);
        g_error_free (error);
    }
}

static void
invest_applet_about_cb (GtkAction    *action,
                        InvestApplet *applet)
{
    const gchar *authors[] = {
        "Raphael Slinckx <raphael@slinckx.net>",
        "Enrico Minack <enrico-minack@gmx.de>",
        "MATE developers",
        NULL
    };

    gtk_show_about_dialog (NULL,
                           "program-name", _("Invest"),
                           "logo-icon-name", "mate-invest-applet",
                           "version", VERSION,
                           "comments", _("Track your invested money."),
                           "copyright", "Copyright \xc2\xa9 2004-2005 Raphael Slinckx\nCopyright \xc2\xa9 2009-2010 Enrico Minack\nCopyright \xc2\xa9 2012-2025 MATE developers",
                           "authors", authors,
                           NULL);
}

static gboolean
invest_applet_cycle_stocks (InvestApplet *applet)
{
    if (applet->total_symbols == 0) {
        return G_SOURCE_CONTINUE;
    }

    /* find valid stocks */
    gint valid_indices[applet->total_symbols];
    gint valid_count = get_valid_stock_indices (applet, valid_indices);

    if (valid_count == 0) {
        update_applet_text (applet, _("No valid stock data"), 0.0);
        return G_SOURCE_CONTINUE;
    }

    if (valid_count == 1) {
        /* single stock, nothing to cycle, stop timer */
        return G_SOURCE_REMOVE;
    }

    /* multiple stocks, cycle to next stock */
    applet->cycle_position = (applet->cycle_position + 1) % valid_count;

    /* display current stock */
    display_stock_at_index (applet, valid_indices[applet->cycle_position]);

    return G_SOURCE_CONTINUE;
}

static gboolean
invest_applet_button_press (GtkWidget      *widget,
                            GdkEventButton *event,
                            InvestApplet   *applet)
{
    if (event->button == 1 && event->type == GDK_BUTTON_PRESS) {
        if (invest_chart_is_visible (applet->chart)) {
            invest_chart_hide (applet->chart);
        } else {
            invest_chart_show (applet->chart);
        }
        return TRUE;
    }

    return FALSE;
}

static void
invest_applet_destroy (GtkWidget    *widget,
                       InvestApplet *applet)
{
    clear_timeout (&applet->update_timeout_id);
    clear_timeout (&applet->cycle_timeout_id);

    if (applet->chart) {
        if (invest_chart_is_visible (applet->chart)) {
            invest_chart_hide (applet->chart);
        }
        invest_chart_free (applet->chart);
        applet->chart = NULL;
    }

    if (applet->soup_session) {
        g_object_unref (applet->soup_session);
        applet->soup_session = NULL;
    }

    if (applet->settings) {
        g_object_unref (applet->settings);
        applet->settings = NULL;
    }

    free_stock_data (applet);
}

static void
invest_applet_init (InvestApplet *applet)
{
    GtkWidget *hbox;

    /* Settings will be initialized in the factory function */
    applet->settings = NULL;
    applet->refresh_interval = DEFAULT_UPDATE_INTERVAL;
    applet->cycle_interval = DEFAULT_CYCLE_INTERVAL;

    applet->pending_requests = 0;
    applet->total_symbols = 0;
    applet->stock_symbols = NULL;
    applet->stock_prices = NULL;
    applet->stock_changes = NULL;
    applet->stock_valid = NULL;

    /* init stock cycling in panel display */
    applet->cycle_position = 0;
    applet->cycle_timeout_id = 0;

    /* init chart functionality */
    applet->chart = invest_chart_new (applet);

    /* init networking */
    applet->soup_session = soup_session_new ();

    /* UI */
    hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 4);

    applet->direction_icon = gtk_image_new_from_icon_name ("dialog-information", GTK_ICON_SIZE_MENU);
    applet->label = gtk_label_new (_("Loading..."));

    gtk_box_pack_start (GTK_BOX (hbox), applet->direction_icon, FALSE, FALSE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), applet->label, FALSE, FALSE, 0);

    gtk_container_add (GTK_CONTAINER (applet), hbox);

    g_signal_connect (G_OBJECT (applet), "button-press-event",
                      G_CALLBACK (invest_applet_button_press), applet);
    g_signal_connect (G_OBJECT (applet), "destroy",
                      G_CALLBACK (invest_applet_destroy), applet);

    const GtkActionEntry menu_actions[] = {
        { "InvestRefresh", "view-refresh", N_("_Refresh"), NULL, NULL, G_CALLBACK (invest_applet_refresh_cb) },
        { "InvestShowChart", "invest_neutral", N_("_Show Chart"), NULL, NULL, G_CALLBACK (invest_applet_show_chart_cb) },
        { "InvestPreferences", "preferences-system", N_("_Preferences"), NULL, NULL, G_CALLBACK (invest_applet_preferences_cb) },
        { "InvestHelp", "help-browser", N_("_Help"), NULL, NULL, G_CALLBACK (invest_applet_help_cb) },
        { "InvestAbout", "help-about", N_("_About"), NULL, NULL, G_CALLBACK (invest_applet_about_cb) }
    };

    GtkActionGroup *action_group = gtk_action_group_new ("Invest Applet Actions");
    gtk_action_group_set_translation_domain (action_group, GETTEXT_PACKAGE);
    gtk_action_group_add_actions (action_group, menu_actions, G_N_ELEMENTS (menu_actions), applet);

    const gchar *ui = "<menuitem name=\"Invest Refresh\" action=\"InvestRefresh\" />"
                      "<menuitem name=\"Invest Show Chart\" action=\"InvestShowChart\" />"
                      "<separator />"
                      "<menuitem name=\"Invest Preferences\" action=\"InvestPreferences\" />"
                      "<menuitem name=\"Invest Help\" action=\"InvestHelp\" />"
                      "<menuitem name=\"Invest About\" action=\"InvestAbout\" />";

    mate_panel_applet_setup_menu (MATE_PANEL_APPLET (applet), ui, action_group);
    g_object_unref (action_group);

    gtk_widget_show_all (GTK_WIDGET (applet));
}

static void
invest_applet_class_init (InvestAppletClass *class)
{
}

static gboolean
invest_applet_factory (MatePanelApplet *applet,
                       const gchar     *iid,
                       gpointer         data)
{
    if (!g_strcmp0 (iid, "InvestApplet")) {
        InvestApplet *invest_applet = INVEST_APPLET (applet);

#ifndef ENABLE_IN_PROCESS
        g_set_application_name (_("Investment Applet"));
#endif
        gtk_window_set_default_icon_name ("mate-invest-applet");

        /* Set applet flags first */
        mate_panel_applet_set_flags (applet, MATE_PANEL_APPLET_EXPAND_MINOR);

        /* Initialize settings after applet is set up */
        invest_applet->settings = mate_panel_applet_settings_new (applet, "org.mate.panel.applet.invest");

        /* Load refresh interval from settings */
        if (G_IS_SETTINGS (invest_applet->settings)) {
            invest_applet->refresh_interval = g_settings_get_int (invest_applet->settings, "refresh-interval");
            invest_applet->cycle_interval = g_settings_get_int (invest_applet->settings, "cycle-interval");
        }

        /* Start periodic updates */
        invest_applet->update_timeout_id = g_timeout_add_seconds (invest_applet->refresh_interval * 60,
                                                                  invest_applet_update_stocks,
                                                                  invest_applet);

        /* Load initial data */
        invest_applet_update_stocks (invest_applet);
        invest_applet_update_display (invest_applet);

        return TRUE;
    }

    return FALSE;
}

static gchar*
create_stock_tooltip (InvestApplet *applet)
{
    GString *tooltip = g_string_new ("");
    gint valid_count = 0;

    g_string_append (tooltip, _("Portfolio Summary:\n"));

    for (gint i = 0; i < applet->total_symbols; i++) {
        if (applet->stock_valid[i]) {
            g_string_append_printf (tooltip, "%s: $%.2f (%.2f%%)\n",
                                    applet->stock_symbols[i],
                                    applet->stock_prices[i],
                                    applet->stock_changes[i]);
            valid_count++;
        } else {
            g_string_append_printf (tooltip, "%s: No data\n",
                                    applet->stock_symbols[i]);
        }
    }

    if (valid_count == 0) {
        g_string_free (tooltip, TRUE);
        return g_strdup (_("No valid stock data"));
    }

    return g_string_free (tooltip, FALSE);
}

static void
free_stock_data (InvestApplet *applet)
{
    g_strfreev (applet->stock_symbols);
    g_free (applet->stock_prices);
    g_free (applet->stock_changes);
    g_free (applet->stock_valid);
    g_free (applet->stock_summary);

    applet->stock_symbols = NULL;
    applet->stock_prices = NULL;
    applet->stock_changes = NULL;
    applet->stock_valid = NULL;
    applet->stock_summary = NULL;
}

static gint
get_valid_stock_indices (InvestApplet *applet, gint *valid_indices)
{
    gint valid_count = 0;

    for (gint i = 0; i < applet->total_symbols; i++) {
        if (applet->stock_valid[i]) {
            valid_indices[valid_count] = i;
            valid_count++;
        }
    }

    return valid_count;
}

static void
display_stock_at_index (InvestApplet *applet, gint stock_index)
{
    if (stock_index < 0 || stock_index >= applet->total_symbols || !applet->stock_symbols) {
        g_warning ("Invalid stock index %d (total: %d)", stock_index, applet->total_symbols);
        return;
    }

    // Remove the '=' symbol from the stock symbol (used for currency conversions)
    gchar *stock_symbol = g_strdup (applet->stock_symbols[stock_index]);
    if (strchr (applet->stock_symbols[stock_index], '=')) {
        g_free (stock_symbol);
        stock_symbol = g_strndup (applet->stock_symbols[stock_index], strchr (applet->stock_symbols[stock_index], '=') - applet->stock_symbols[stock_index]);
    }
    gchar *message = g_strdup_printf ("%s: $%.2f",
                                      stock_symbol,
                                      applet->stock_prices[stock_index]);
    update_applet_text (applet, message, applet->stock_changes[stock_index]);
    g_free (stock_symbol);
    g_free (message);
}

static void
clear_timeout (guint *timeout_id)
{
    if (*timeout_id) {
        g_source_remove (*timeout_id);
        *timeout_id = 0;
    }
}

static void
update_applet_text (InvestApplet *applet, const gchar *message, gdouble change_percent)
{
    g_free (applet->stock_summary);
    applet->stock_summary = g_strdup (message);
    applet->change_percent = change_percent;
    invest_applet_update_display (applet);
}

PANEL_APPLET_FACTORY ("InvestAppletFactory",
                      INVEST_TYPE_APPLET,
                      "Investment applet",
                      invest_applet_factory,
                      NULL)