1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-11-15 17:47:09 +00:00

Make mysql_* lua settings

This commit is contained in:
Pavel Labath 2010-06-22 01:34:55 +02:00
parent c33d617179
commit b5a4d899ab
4 changed files with 12 additions and 104 deletions

View File

@ -2639,13 +2639,6 @@ static void set_default_configurations(void)
mpd_set_port("6600");
}
#endif /* BUILD_MPD */
#ifdef BUILD_MYSQL
mysql_settings.host = NULL;
mysql_settings.port = 0;
mysql_settings.user = NULL;
mysql_settings.password = NULL;
mysql_settings.db = NULL;
#endif /* BUILD_MYSQL */
#ifdef BUILD_XMMS2
info.xmms2.artist = NULL;
info.xmms2.album = NULL;
@ -2923,43 +2916,6 @@ char load_config_file(const char *f)
}
}
#endif /* BUILD_MPD */
#ifdef BUILD_MYSQL
CONF("mysql_host") {
if (value) {
mysql_set_host(value);
} else {
CONF_ERR;
}
}
CONF("mysql_port") {
if (value) {
mysql_set_port(value);
} else {
CONF_ERR;
}
}
CONF("mysql_user") {
if (value) {
mysql_set_user(value);
} else {
CONF_ERR;
}
}
CONF("mysql_password") {
if (value) {
mysql_set_password(value);
} else {
CONF_ERR;
}
}
CONF("mysql_db") {
if (value) {
mysql_set_db(value);
} else {
CONF_ERR;
}
}
#endif /* BUILD_MYSQL */
CONF("music_player_interval") {
if (value) {
info.music_player_interval = strtod(value, 0);

View File

@ -704,7 +704,6 @@ struct text_object *construct_text_object(char *s, const char *arg, long
END OBJ_ARG(mysql, 0, "mysql needs a query")
obj->data.s = strdup(arg);
obj->callbacks.print = &print_mysql;
obj->callbacks.free = &free_mysql;
#endif /* BUILD_MYSQL */
END OBJ_ARG(no_update, 0, "no_update needs arguments")
scan_no_update(obj, arg);

View File

@ -31,19 +31,27 @@
#include "logging.h"
#include <mysql.h>
struct mysql_conn mysql_settings;
#include "setting.hh"
namespace {
conky::simple_config_setting<std::string> host("mysql_host", "localhost", false);
conky::range_config_setting<int> port("mysql_port", 0, 0xffff, 0, false);
conky::simple_config_setting<std::string> user("mysql_user", "root", false);
conky::simple_config_setting<std::string> password("mysql_password", std::string(), false);
conky::simple_config_setting<std::string> db("mysql_db", "mysql", false);
}
void print_mysql(struct text_object *obj, char *p, int p_max_size) {
MYSQL *conn = mysql_init(NULL);
if(mysql_settings.db == NULL)
mysql_settings.db = strdup("mysql");
if(conn == NULL) {
NORM_ERR("Can't initialize MySQL");
mysql_library_end();
return;
}
if (!mysql_real_connect(conn, mysql_settings.host, mysql_settings.user, mysql_settings.password, mysql_settings.db, mysql_settings.port, NULL, 0)) {
if (!mysql_real_connect(conn, host.get(*state).c_str(), user.get(*state).c_str(),
password.get(*state).c_str(), db.get(*state).c_str(),
port.get(*state), NULL, 0)) {
NORM_ERR("MySQL: %s", mysql_error(conn));
mysql_close(conn);
mysql_library_end();
@ -72,41 +80,3 @@ void print_mysql(struct text_object *obj, char *p, int p_max_size) {
mysql_close(conn);
mysql_library_end();
}
void free_mysql(struct text_object *obj) {
free(mysql_settings.host);
free(mysql_settings.user);
if(mysql_settings.password) free(mysql_settings.password);
free(mysql_settings.db);
free(obj->data.s);
}
void mysql_set_host(const char *host) {
free(mysql_settings.host);
mysql_settings.host = strdup(host);
}
void mysql_set_port(const char *port) {
mysql_settings.port = strtol(port, 0, 0);
if(mysql_settings.port < 1 || mysql_settings.port > 0xffff)
mysql_settings.port = 0;
}
void mysql_set_user(const char *user) {
free(mysql_settings.user);
mysql_settings.user = strdup(user);
}
void mysql_set_password(const char *password) {
free_and_zero(mysql_settings.password);
if(password && strlen(password) > 2 && password[0] == '"' && password[strlen(password)-1] == '"') {
mysql_settings.password = strdup(password+1);
mysql_settings.password[strlen(password)-2] = 0;
} else
mysql_settings.password = NULL;
}
void mysql_set_db(const char *db) {
free(mysql_settings.db);
mysql_settings.db = strdup(db);
}

View File

@ -26,23 +26,6 @@
#ifndef MYSQL_H_
#define MYSQL_H_
struct mysql_conn {
char *host;
int port;
char *user;
char *password;
char *db;
};
extern struct mysql_conn mysql_settings;
void mysql_set_host(const char *);
void mysql_set_port(const char *);
void mysql_set_user(const char *);
void mysql_set_password(const char *);
void mysql_set_db(const char *);
void print_mysql(struct text_object *, char *, int);
void free_mysql(struct text_object *);
#endif /*MYSQL_H_*/