Blob Blame History Raw
From ee53404ce14aa5e877704cf97456c6400f03596e Mon Sep 17 00:00:00 2001
From: Matej Habrnal <mhabrnal@redhat.com>
Date: Fri, 5 Jun 2015 08:58:15 +0200
Subject: [PATCH] client: add function ask_yes_no_save_result

The function asks users to answer yes or no and allows to save the response to
the user settings in case the user does not want tu be asked next time.

Related to rhbz#986876

Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
---
 src/include/client.h |  4 ++++
 src/lib/client.c     | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+)

diff --git a/src/include/client.h b/src/include/client.h
index f2185d9..ac2cfc6 100644
--- a/src/include/client.h
+++ b/src/include/client.h
@@ -32,6 +32,7 @@
  *   ASK_YES_NO_YESFOREVER Continue?
  */
 #define REPORT_PREFIX_ASK_YES_NO_YESFOREVER "ASK_YES_NO_YESFOREVER "
+#define REPORT_PREFIX_ASK_YES_NO_SAVE_RESULT "ASK_YES_NO_SAVE_RESULT "
 #define REPORT_PREFIX_ASK "ASK "
 #define REPORT_PREFIX_ASK_PASSWORD "ASK_PASSWORD "
 #define REPORT_PREFIX_ALERT "ALERT "
@@ -49,6 +50,9 @@ int ask_yes_no(const char *question);
 #define ask_yes_no_yesforever libreport_ask_yes_no_yesforever
 int ask_yes_no_yesforever(const char *key, const char *question);
 
+#define ask_yes_no_save_resutl libreport_ask_yes_no_save_result
+int ask_yes_no_save_result(const char *key, const char *question);
+
 #define ask libreport_ask
 char *ask(const char *question);
 
diff --git a/src/lib/client.c b/src/lib/client.c
index 452278a..1e988bd 100644
--- a/src/lib/client.c
+++ b/src/lib/client.c
@@ -137,6 +137,68 @@ int ask_yes_no_yesforever(const char *key, const char *question)
     return ((is_slave_mode() && response[0] == 'y') || strncasecmp(yes, response, strlen(yes)) == 0);
 }
 
+int ask_yes_no_save_result(const char *key, const char *question)
+{
+    INITIALIZE_LIBREPORT();
+
+    const char *yes = _("y");
+    const char *no = _("N");
+    const char *forever = _("f");
+    const char *never = _("e");
+
+    {   /* Use response from REPORT_CLIENT_RESPONSE environment variable.
+         *
+         * The forever and never response is not allowed in this case.
+         * There is no serious reason for that, it is just decision.
+         * (It doesn't make much sense to allow the *_forever answer here.)
+         */
+        const char *env_response = getenv("REPORT_CLIENT_RESPONSE");
+        if (env_response)
+            return strncasecmp(yes, env_response, strlen(yes)) == 0;
+    }
+
+    {   /* Load an value for the key from user setting.
+         * YES means forever
+         * NO means never
+         * 'no_value' means ask me
+         */
+        const char *option = get_user_setting(key);
+        if (option)
+            return string_to_bool(option);
+    }
+
+    if (is_slave_mode())
+        printf(REPORT_PREFIX_ASK_YES_NO_SAVE_RESULT "%s %s\n", key, question);
+    else
+        printf("%s [%s/%s/%s/%s] ", question, yes, no, forever, never);
+
+    fflush(stdout);
+
+    if (!is_slave_mode() && is_noninteractive_mode())
+    {
+        putchar('\n');
+        fflush(stdout);
+        return 0;
+    }
+
+    char response[16];
+    if (NULL == fgets(response, sizeof(response), stdin))
+        return 0;
+
+    if ((is_slave_mode() && response[0] == 'f') || strncasecmp(forever, response, strlen(forever)) == 0)
+    {
+        set_user_setting(key, "yes");
+        return 1;
+    }
+    else if ((is_slave_mode() && response[0] == 'e') || strncasecmp(forever, response, strlen(never)) == 0)
+    {
+        set_user_setting(key, "no");
+        return 0;
+    }
+
+    return ((is_slave_mode() && response[0] == 'y') || strncasecmp(yes, response, strlen(yes)) == 0);
+}
+
 char *ask(const char *question)
 {
     if (is_slave_mode())
-- 
2.1.0