[PATCH 1/2] cli: abstract common config get/set code
authorJani Nikula <jani@nikula.org>
Sun, 18 Aug 2013 15:20:04 +0000 (18:20 +0300)
committerW. Trevor King <wking@tremily.us>
Fri, 7 Nov 2014 17:56:26 +0000 (09:56 -0800)
4e/064edad52986af8010252ef91986c7fd01ddcc [new file with mode: 0644]

diff --git a/4e/064edad52986af8010252ef91986c7fd01ddcc b/4e/064edad52986af8010252ef91986c7fd01ddcc
new file mode 100644 (file)
index 0000000..bb9cd1d
--- /dev/null
@@ -0,0 +1,196 @@
+Return-Path: <jani@nikula.org>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+       by olra.theworths.org (Postfix) with ESMTP id 578C7421161\r
+       for <notmuch@notmuchmail.org>; Sun, 18 Aug 2013 08:20:09 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+       tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+       by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+       with ESMTP id tF2-jhiNVfu0 for <notmuch@notmuchmail.org>;\r
+       Sun, 18 Aug 2013 08:20:03 -0700 (PDT)\r
+Received: from mail-bk0-f42.google.com (mail-bk0-f42.google.com\r
+       [209.85.214.42]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+       (No client certificate requested)\r
+       by olra.theworths.org (Postfix) with ESMTPS id A2643431FDB\r
+       for <notmuch@notmuchmail.org>; Sun, 18 Aug 2013 08:20:03 -0700 (PDT)\r
+Received: by mail-bk0-f42.google.com with SMTP id my10so1149927bkb.15\r
+       for <notmuch@notmuchmail.org>; Sun, 18 Aug 2013 08:20:02 -0700 (PDT)\r
+X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
+       d=google.com; s=20120113;\r
+       h=x-gm-message-state:from:to:cc:subject:date:message-id;\r
+       bh=TBmMXK6A1uOEclkvE4Y9Fkd7wwtTW/kPnPShADis+z8=;\r
+       b=JtkT3tlft7sSpM59+W98hXJnLltIhEa5mgmqhPev/bjyTB4WQLbBcS1SMdy2vtilZF\r
+       KAKJBDs9wkkVnTaizV7lYBhOQ0srVIGDp6IKB0nqWC2rWLefESO0A4gGrivzFJGeEbVD\r
+       gukAmvNMN0gJHXky6jLOHHyeRPhH/VpB6jyzUBeH0b0J7svxZvPVkZhAJ1d2BHhCA/yY\r
+       a0n+xSWplbYyvy3EkU0Xi0fYGxqJZedyU8DvDLMCZJb1hAfUiO2z8wYT1D2Qb2K8wWic\r
+       IMjFPVMTPFOFHLtn9vs5tIGV9r2Pnp3UJcmyCldqIMsHIDe9MlhM2BShC1rKEsbeyU5Y\r
+       F07g==\r
+X-Gm-Message-State:\r
+ ALoCoQk9uEvZlGyq8kGQuSmrzA36DauMGDLM7WUaZ2C/0jZV302hIuNSpgXJGHpLnNNO45LpLWTC\r
+X-Received: by 10.204.70.1 with SMTP id b1mr4827999bkj.3.1376839202312;\r
+       Sun, 18 Aug 2013 08:20:02 -0700 (PDT)\r
+Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi.\r
+       [88.195.111.91])\r
+       by mx.google.com with ESMTPSA id ku9sm951140bkb.1.1969.12.31.16.00.00\r
+       (version=TLSv1.2 cipher=RC4-SHA bits=128/128);\r
+       Sun, 18 Aug 2013 08:20:01 -0700 (PDT)\r
+From: Jani Nikula <jani@nikula.org>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH 1/2] cli: abstract common config get/set code\r
+Date: Sun, 18 Aug 2013 18:20:04 +0300\r
+Message-Id: <1376839205-5115-1-git-send-email-jani@nikula.org>\r
+X-Mailer: git-send-email 1.7.10.4\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+       <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+       <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 18 Aug 2013 15:20:09 -0000\r
+\r
+Pretty straightforward abstraction similar to get/set list.\r
+---\r
+ notmuch-config.c |   80 ++++++++++++++++++++----------------------------------\r
+ 1 file changed, 29 insertions(+), 51 deletions(-)\r
+\r
+diff --git a/notmuch-config.c b/notmuch-config.c\r
+index befe9b5..305d213 100644\r
+--- a/notmuch-config.c\r
++++ b/notmuch-config.c\r
+@@ -496,6 +496,29 @@ notmuch_config_is_new (notmuch_config_t *config)\r
+     return config->is_new;\r
+ }\r
\r
++static const char *\r
++_config_get (notmuch_config_t *config, char **field,\r
++           const char *group, const char *key)\r
++{\r
++    if (*field == NULL) {\r
++      char *value;\r
++      value = g_key_file_get_string (config->key_file, group, key, NULL);\r
++      if (value) {\r
++          *field = talloc_strdup (config, value);\r
++          free (value);\r
++      }\r
++    }\r
++    return *field;\r
++}\r
++\r
++static void\r
++_config_set (notmuch_config_t *config, char **field,\r
++           const char *group, const char *key, const char *value)\r
++{\r
++    g_key_file_set_string (config->key_file, group, key, value);\r
++    talloc_free (*field);\r
++    *field = NULL;\r
++}\r
\r
+ static const char **\r
+ _config_get_list (notmuch_config_t *config,\r
+@@ -542,85 +565,40 @@ _config_set_list (notmuch_config_t *config,\r
+ const char *\r
+ notmuch_config_get_database_path (notmuch_config_t *config)\r
+ {\r
+-    char *path;\r
+-\r
+-    if (config->database_path == NULL) {\r
+-      path = g_key_file_get_string (config->key_file,\r
+-                                    "database", "path", NULL);\r
+-      if (path) {\r
+-          config->database_path = talloc_strdup (config, path);\r
+-          free (path);\r
+-      }\r
+-    }\r
+-\r
+-    return config->database_path;\r
++    return _config_get (config, &config->database_path, "database", "path");\r
+ }\r
\r
+ void\r
+ notmuch_config_set_database_path (notmuch_config_t *config,\r
+                                 const char *database_path)\r
+ {\r
+-    g_key_file_set_string (config->key_file,\r
+-                         "database", "path", database_path);\r
+-\r
+-    talloc_free (config->database_path);\r
+-    config->database_path = NULL;\r
++    _config_set (config, &config->database_path, "database", "path", database_path);\r
+ }\r
\r
+ const char *\r
+ notmuch_config_get_user_name (notmuch_config_t *config)\r
+ {\r
+-    char *name;\r
+-\r
+-    if (config->user_name == NULL) {\r
+-      name = g_key_file_get_string (config->key_file,\r
+-                                    "user", "name", NULL);\r
+-      if (name) {\r
+-          config->user_name = talloc_strdup (config, name);\r
+-          free (name);\r
+-      }\r
+-    }\r
+-\r
+-    return config->user_name;\r
++    return _config_get (config, &config->user_name, "user", "name");\r
+ }\r
\r
+ void\r
+ notmuch_config_set_user_name (notmuch_config_t *config,\r
+                             const char *user_name)\r
+ {\r
+-    g_key_file_set_string (config->key_file,\r
+-                         "user", "name", user_name);\r
+-\r
+-    talloc_free (config->user_name);\r
+-    config->user_name = NULL;\r
++    _config_set (config, &config->user_name, "user", "name", user_name);\r
+ }\r
\r
+ const char *\r
+ notmuch_config_get_user_primary_email (notmuch_config_t *config)\r
+ {\r
+-    char *email;\r
+-\r
+-    if (config->user_primary_email == NULL) {\r
+-      email = g_key_file_get_string (config->key_file,\r
+-                                     "user", "primary_email", NULL);\r
+-      if (email) {\r
+-          config->user_primary_email = talloc_strdup (config, email);\r
+-          free (email);\r
+-      }\r
+-    }\r
+-\r
+-    return config->user_primary_email;\r
++    return _config_get (config, &config->user_primary_email, "user", "primary_email");\r
+ }\r
\r
+ void\r
+ notmuch_config_set_user_primary_email (notmuch_config_t *config,\r
+                                      const char *primary_email)\r
+ {\r
+-    g_key_file_set_string (config->key_file,\r
+-                         "user", "primary_email", primary_email);\r
+-\r
+-    talloc_free (config->user_primary_email);\r
+-    config->user_primary_email = NULL;\r
++    _config_set (config, &config->user_primary_email, "user", "primary_email", primary_email);\r
+ }\r
\r
+ const char **\r
+-- \r
+1.7.10.4\r
+\r