/* main.c *
 * Copyright (C) 2017 Davi da Silva Böger *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <gio/gio.h>
#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>

guint signals_handled_normal = 0;
guint signals_handled_delayed = 0;

void
signal_handler (GSettings* settings,
                gchar*     key)
{
  gboolean is_delay_mode;
  g_object_get (settings, "delay-apply", &is_delay_mode, NULL);

  if (is_delay_mode)
    signals_handled_delayed++;
  else
    signals_handled_normal++;
}

int
main (int   argc,
      char *argv[])
{
  GSettingsBackend* memory_backend = g_memory_settings_backend_new ();
  GSettings* settings = g_settings_new_with_backend ("org.gnome.desktop.calendar", memory_backend);

  g_settings_reset (settings, "show-weekdate");

  g_signal_connect (settings, "changed::show-weekdate", G_CALLBACK(signal_handler), NULL);

  g_assert_cmpuint (signals_handled_normal, ==, 0);
  g_assert_cmpuint (signals_handled_delayed, ==, 0);

  g_settings_set_boolean (settings, "show-weekdate", TRUE);
  g_settings_reset (settings, "show-weekdate");
  g_assert_cmpuint (signals_handled_normal, ==, 2); // memory backend emits changed for both write and reset

  g_settings_delay (settings);
  g_settings_set_boolean (settings, "show-weekdate", TRUE);
  g_assert_cmpuint (signals_handled_delayed, ==, 1); // delayed backend emits changed on write...
  g_settings_reset (settings, "show-weekdate");
  g_settings_apply (settings);
  g_assert_cmpuint (signals_handled_delayed, ==, 2); // ... but not on reset, even after apply (https://bugzilla.gnome.org/show_bug.cgi?id=791290)

  return 0;
}
