Implemented loading configuration files

This commit is contained in:
Pierre Pronchery 2007-08-23 02:21:08 +00:00
parent c5382e3b65
commit dbc8445777
7 changed files with 75 additions and 9 deletions

View File

@ -23,7 +23,7 @@ mailer_OBJS = account.o callbacks.o common.o compose.o mailer.o main.o
mailer_CFLAGS = $(CPPFLAGS) $(CFLAGSF) $(CFLAGS)
mailer: $(mailer_OBJS)
$(CC) -o mailer $(mailer_OBJS) $(LDFLAGSF) $(LDFLAGS)
$(CC) -o mailer $(mailer_OBJS) $(LDFLAGSF) $(LDFLAGS) -L $(PREFIX)/lib -Wl,-rpath,$(PREFIX)/lib -l System
account.o: account.c account/account.h
$(CC) $(mailer_CFLAGS) -c account.c

View File

@ -32,6 +32,7 @@
#ifndef PLUGINDIR
# define PLUGINDIR LIBDIR "Mailer"
#endif
#define ACCOUNT "account"
/* Account */
@ -42,14 +43,15 @@ Account * account_new(char const * type, char const * name)
if((account = malloc(sizeof(*account))) == NULL)
return NULL;
memset(account, 0, sizeof(*account));
if((account->name = strdup(name)) == NULL
|| (filename = malloc(strlen(PLUGINDIR) + strlen(type)
|| (filename = malloc(strlen(PLUGINDIR ACCOUNT)
+ strlen(name) + 6)) == NULL)
{
account_delete(account);
return NULL;
}
sprintf(filename, "%s/%s/%s.so", PLUGINDIR, type, name);
sprintf(filename, "%s/%s/%s.so", PLUGINDIR, ACCOUNT, type);
if((account->handle = dlopen(filename, RTLD_LAZY)) == NULL
|| (account->plugin = dlsym(account->handle,
"account_plugin")) == NULL)
@ -60,7 +62,7 @@ Account * account_new(char const * type, char const * name)
return NULL;
}
free(filename);
account->title = NULL;
account->title = strdup(name);
account->identity = NULL;
return account;
}

View File

@ -76,7 +76,6 @@ typedef struct _Account
/* functions */
/* FIXME type should be automatically "account" and wrap plug-in engine */
Account * account_new(char const * type, char const * name);
void account_delete(Account * account);

View File

@ -700,8 +700,7 @@ static void _on_assistant_prepare(GtkWidget * widget, GtkWidget * page,
if(ad->account != NULL)
account_delete(ad->account);
ac = &ad->mailer->available[ad->available];
ad->account = account_new("account", ac->name);
account_set_title(ad->account, ad->title);
ad->account = account_new(ac->name, ad->title);
}
if(ad->account == NULL)
ad->settings = gtk_label_new("Could not load plug-in");

View File

@ -88,6 +88,8 @@ static struct _toolbar _mailer_toolbar[] =
/* functions */
static int _mailer_error(char const * message, int ret);
static int _mailer_dlerror(char const * message, int ret);
static char * _mailer_config_filename(void);
static int _mailer_config_load_account(Mailer * mailer, char const * name);
static int _mailer_error(char const * message, int ret)
{
@ -102,6 +104,33 @@ static int _mailer_dlerror(char const * message, int ret)
return ret;
}
static char * _mailer_config_filename(void)
{
char * homedir;
char * filename;
if((homedir = getenv("HOME")) == NULL)
return NULL;
if((filename = malloc(strlen(homedir) + strlen(MAILER_CONFIG_FILE) + 2))
== NULL)
return NULL;
sprintf(filename, "%s/%s", homedir, MAILER_CONFIG_FILE);
return filename;
}
static int _mailer_config_load_account(Mailer * mailer, char const * name)
{
Account * account;
char * type;
if((type = config_get(mailer->config, name, "type")) == NULL)
return 1;
if((account = account_new(type, name)) == NULL)
return 1;
mailer_account_add(mailer, account);
return 0;
}
/* public */
/* functions */
@ -109,7 +138,7 @@ static int _mailer_dlerror(char const * message, int ret)
static int _new_plugins(Mailer * mailer);
static GtkWidget * _new_folders_view(void);
static GtkWidget * _new_headers(Mailer * mailer);
/* static gboolean _new_accounts(gpointer data); */
static void _new_config_load(Mailer * mailer);
Mailer * mailer_new(void)
{
@ -129,7 +158,6 @@ Mailer * mailer_new(void)
mailer->account = NULL;
mailer->account_cnt = 0;
mailer->account_cur = NULL;
/* g_idle_add(_new_accounts, mailer); */
/* widgets */
mailer->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(mailer->window), 640, 480);
@ -177,6 +205,9 @@ Mailer * mailer_new(void)
mailer->statusbar_id = 0;
gtk_box_pack_start(GTK_BOX(vbox), mailer->statusbar, FALSE, FALSE, 0);
gtk_container_add(GTK_CONTAINER(mailer->window), vbox);
/* load configuration */
_new_config_load(mailer);
/* show window */
gtk_widget_show_all(mailer->window);
gtk_widget_hide(mailer->hdr_vbox);
mailer->pr_window = NULL;
@ -322,6 +353,33 @@ static GtkWidget * _new_headers(Mailer * mailer)
return vbox;
}
static void _new_config_load(Mailer * mailer)
{
char * filename;
char * accounts;
char * p;
if((mailer->config = config_new()) == NULL)
return;
if((filename = _mailer_config_filename()) == NULL)
return;
config_load(mailer->config, filename);
free(filename);
if((accounts = config_get(mailer->config, "", "accounts")) == NULL
|| accounts[0] == '\0')
return;
for(p = accounts; *p != '\0'; p++)
{
if(*p != ',')
continue;
*p = '\0';
_mailer_config_load_account(mailer, accounts);
*p = ',';
accounts = p + 1;
}
if(accounts[0] != '\0')
_mailer_config_load_account(mailer, accounts);
}
void mailer_delete(Mailer * mailer)
{

View File

@ -18,11 +18,15 @@
#ifndef MAILER_MAILER_H
# define MAILER_MAILER_H
# include <System.h>
# include <gtk/gtk.h>
# include "account/account.h"
/* Mailer */
/* defaults */
# define MAILER_CONFIG_FILE ".mailer"
/* types */
typedef struct _Mailer
{
@ -33,6 +37,9 @@ typedef struct _Mailer
unsigned int account_cnt;
Account * account_cur;
/* configuration */
Config * config;
/* widgets */
GtkWidget * window;
GtkWidget * view_folders;

View File

@ -8,6 +8,7 @@ dist=Makefile,callbacks.h,common.h,compose.h,mailer.h
[mailer]
type=binary
ldflags=-L $(PREFIX)/lib -Wl,-rpath,$(PREFIX)/lib -l System
sources=account.c,callbacks.c,common.c,compose.c,mailer.c,main.c
[account.c]