added get_psensor_user_dir fct
[psensor.git] / src / cfg.c
index 6fd812a..5db601c 100644 (file)
--- a/src/cfg.c
+++ b/src/cfg.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2013 jeanfi@gmail.com
+ * Copyright (C) 2010-2014 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301 USA
  */
+
+#include <errno.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
 #include <ctype.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+#include <locale.h>
+#include <libintl.h>
+#define _(str) gettext(str)
 
-#include "cfg.h"
-#include "log.h"
+#include <cfg.h>
+#include <pio.h>
+#include <plog.h>
 
 static const char *KEY_SENSORS = "/apps/psensor/sensors";
 
@@ -33,6 +42,10 @@ static const char *ATT_SENSOR_COLOR = "color";
 static const char *ATT_SENSOR_ENABLED = "enabled";
 static const char *ATT_SENSOR_NAME = "name";
 static const char *ATT_SENSOR_APPINDICATOR_DISABLED = "appindicator/disabled";
+static const char *ATT_SENSOR_APPINDICATOR_LABEL_ENABLED
+= "appindicator/menu/enabled";
+
+static const char *ATT_SENSOR_POSITION = "position";
 
 static const char *KEY_SENSOR_UPDATE_INTERVAL
 = "/apps/psensor/sensor/update_interval";
@@ -93,8 +106,12 @@ static const char *KEY_INTERFACE_TEMPERATURE_UNIT
 static const char *KEY_SLOG_ENABLED = "/apps/psensor/slog/enabled";
 static const char *KEY_SLOG_INTERVAL = "/apps/psensor/slog/interval";
 
+static const char *KEY_NOTIFICATION_SCRIPT = "/apps/psensor/notif_script";
+
 static GConfClient *client;
 
+static char *user_dir;
+
 static char *get_string(const char *key, const char *default_value)
 {
        char *value;
@@ -109,6 +126,29 @@ static char *get_string(const char *key, const char *default_value)
        return value;
 }
 
+char *config_get_notif_script()
+{
+       char *str;
+
+       str =  gconf_client_get_string(client, KEY_NOTIFICATION_SCRIPT, NULL);
+       if (str && !strlen(str)) {
+               free(str);
+               str = NULL;
+       }
+
+       return str;
+}
+
+void config_set_notif_script(const char *str)
+{
+       if (str && strlen(str) > 0)
+               gconf_client_set_string(client,
+                                       KEY_NOTIFICATION_SCRIPT, str, NULL);
+       else
+               gconf_client_set_string(client,
+                                       KEY_NOTIFICATION_SCRIPT, "", NULL);
+}
+
 static struct color *get_background_color()
 {
        char *scolor;
@@ -366,6 +406,27 @@ void config_set_sensor_name(const char *sid, const char *name)
        free(key);
 }
 
+int config_get_sensor_position(const char *sid)
+{
+       char *key;
+       int pos;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
+       pos = gconf_client_get_int(client, key, NULL);
+       free(key);
+
+       return pos;
+}
+
+void config_set_sensor_position(const char *sid, int pos)
+{
+       char *key;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_POSITION);
+       gconf_client_set_int(client, key, pos, NULL);
+       free(key);
+}
+
 bool config_is_appindicator_enabled(const char *sid)
 {
        char *key;
@@ -387,6 +448,27 @@ void config_set_appindicator_enabled(const char *sid, bool enabled)
        free(key);
 }
 
+bool config_is_appindicator_label_enabled(const char *sid)
+{
+       char *key;
+       gboolean b;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
+       b = gconf_client_get_bool(client, key, NULL);
+       free(key);
+
+       return b;
+}
+
+void config_set_appindicator_label_enabled(const char *sid, bool enabled)
+{
+       char *key;
+
+       key = get_sensor_att_key(sid, ATT_SENSOR_APPINDICATOR_LABEL_ENABLED);
+       gconf_client_set_bool(client, key, enabled, NULL);
+       free(key);
+}
+
 bool is_slog_enabled()
 {
        return gconf_client_get_bool(client, KEY_SLOG_ENABLED, NULL);
@@ -397,7 +479,6 @@ static void set_slog_enabled(bool enabled)
        gconf_client_set_bool(client, KEY_SLOG_ENABLED, enabled, NULL);
 }
 
-
 void config_slog_enabled_notify_add(GConfClientNotifyFunc cbk, void *data)
 {
        log_debug("config_slog_enabled_notify_add");
@@ -476,6 +557,11 @@ void config_cleanup()
                g_object_unref(client);
                client = NULL;
        }
+
+       if (user_dir) {
+               free(user_dir);
+               user_dir = NULL;
+       }
 }
 
 struct config *config_load()
@@ -622,3 +708,32 @@ void config_save(const struct config *c)
                             c->temperature_unit,
                             NULL);
 }
+
+const char *get_psensor_user_dir()
+{
+       const char *home;
+
+       log_fct_enter();
+
+       if (!user_dir) {
+               home = getenv("HOME");
+
+               if (!home)
+                       return NULL;
+
+               user_dir = path_append(home, ".psensor");
+
+               if (mkdir(user_dir, 0700) == -1 && errno != EEXIST) {
+                       log_err(_("Failed to create the directory %s: %s"),
+                               user_dir,
+                               strerror(errno));
+
+                       free(user_dir);
+                       user_dir = NULL;
+               }
+       }
+
+       log_fct_exit();
+
+       return user_dir;
+}