calendar.c 3.31 KB
Newer Older
Victor Ananjesky's avatar
Victor Ananjesky committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * This file is part of YAD.
 *
 * YAD 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.
 *
 * YAD 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 YAD. If not, see <http://www.gnu.org/licenses/>.
 *
17
 * Copyright (C) 2008-2022, Victor Ananjevsky <victor@sanana.kiev.ua>
Victor Ananjesky's avatar
Victor Ananjesky committed
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
 */

#include <errno.h>

#include "yad.h"

static GtkWidget *calendar;

static GHashTable *details;

static void
parse_details ()
{
  FILE *f;

  details = g_hash_table_new (g_str_hash, g_str_equal);

  /* open details file */
  f = fopen (options.calendar_data.details, "r");
  if (f == NULL)
    {
      g_printerr (_("Cannot open file '%s': %s\n"), options.common_data.uri, g_strerror (errno));
      return;
    }

  /* read details file */
  while (!feof (f))
    {
Victor Ananjevsky's avatar
Victor Ananjevsky committed
46
      gchar buf[4096];
Victor Ananjesky's avatar
Victor Ananjesky committed
47 48 49

      /* read string */
      memset (buf, 0, 4096);
50 51
      if (fgets (buf, 4096, f) == NULL)
        break;
Victor Ananjesky's avatar
Victor Ananjesky committed
52 53
      if (strlen (buf) > 0)
        {
Victor Ananjevsky's avatar
Victor Ananjevsky committed
54
          gchar **dtl = g_strsplit (buf, " ", 2);
Victor Ananjesky's avatar
Victor Ananjesky committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
          g_hash_table_insert (details, dtl[0], dtl[1]);
        }
    }

  fclose (f);
}

static gchar *
get_details (GtkCalendar * cal, guint year, guint month, guint day, gpointer data)
{
  GDate *d;
  gchar time_string[128];
  gchar *str = NULL;

  d = g_date_new_dmy (day, month + 1, year);
  if (g_date_valid (d))
    {
      g_date_strftime (time_string, 127, options.common_data.date_format, d);
      str = (gchar *) g_hash_table_lookup (details, time_string);
    }
  g_date_free (d);

  if (str)
    return g_strdup (str);
Victor Ananjevsky's avatar
Victor Ananjevsky committed
79 80

  return NULL;
Victor Ananjesky's avatar
Victor Ananjesky committed
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
}

static void
double_click_cb (GtkWidget * w, gpointer data)
{
  if (options.plug == -1)
    yad_exit (options.data.def_resp);
}

GtkWidget *
calendar_create_widget (GtkWidget * dlg)
{
  GtkWidget *w;
  gint cal_opts;

  w = calendar = gtk_calendar_new ();
  gtk_widget_set_name (w, "yad-calendar-widget");

  if (options.calendar_data.month > 0 || options.calendar_data.year > 0)
    gtk_calendar_select_month (GTK_CALENDAR (w), options.calendar_data.month - 1, options.calendar_data.year);
  if (options.calendar_data.day > 0)
    gtk_calendar_select_day (GTK_CALENDAR (w), options.calendar_data.day);

  if (options.calendar_data.details)
    {
      parse_details ();
      gtk_calendar_set_detail_func (GTK_CALENDAR (w), get_details, NULL, NULL);
    }

  cal_opts = GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES;
  if (options.calendar_data.weeks)
    cal_opts |= GTK_CALENDAR_SHOW_WEEK_NUMBERS;
  gtk_calendar_set_display_options (GTK_CALENDAR (w), cal_opts);

  g_signal_connect (w, "day-selected-double-click", G_CALLBACK (double_click_cb), dlg);

  return w;
}

void
calendar_print_result (void)
{
  guint day, month, year;
  gchar time_string[128];
  GDate *date = NULL;

  gtk_calendar_get_date (GTK_CALENDAR (calendar), &year, &month, &day);
  date = g_date_new_dmy (day, month + 1, year);
  g_date_strftime (time_string, 127, options.common_data.date_format, date);
  g_print ("%s\n", time_string);
}