/* Katoob * Copyright (c) 2002,2003 Arabeyes, Mohammed Sameer. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef HAVE_CONFIG_H #include #endif #include "katoob.h" #include #include /* remove() */ #include /* mkdir() */ #include /* mkdir() */ #include #include /* strcmp() */ #include "conf.h" #include "spell.h" #include "misc.h" #ifdef ENABLE_PRINT #include "print.h" extern PConfig *PConf; #endif /* ENABLE_PRINT */ static gboolean pre_config (gchar * file); static gchar *decode_font (gchar * font); static gchar *encode_font (gchar * font); void config_init (conf * config) { katoob_debug (__FUNCTION__); config->x = 0; config->y = 25; config->w = 793; config->h = 514; config->recentno = 10; config->tabsmenu = TRUE; config->showtabs = TRUE; config->showclose = TRUE; config->toolbar = TRUE; config->extended_toolbar = TRUE; config->statusbar = TRUE; config->savewinpos = TRUE; config->saveonexit = TRUE; config->textwrap = TRUE; config->text_dir = KATOOB_BIDI_AUT; config->tabspos = TABS_POS_TOP; config->toolbartype = g_strdup ("both"); config->recent = TRUE; config->xkb_err_dlg = TRUE; config->xkb = TRUE; config->tabsmenu = TRUE; config->scrolltabs = TRUE; config->undo = TRUE; config->undono = 0; #ifdef HAVE_SPELL config->spell_check = TRUE; config->mispelled_red = 65535; config->mispelled_blue = 0; config->mispelled_green = 0; config->dicts_dir = g_strdup (DICTS_DIR); #endif /* HAVE_SPELL */ config->emulator = TRUE; config->disable_xft = FALSE; config->defenc = -1; config->default_font = TRUE; #ifdef ENABLE_HIGHLIGHT config->highlight = TRUE; #endif /* ENABLE_HIGHLIGHT */ config->backup = FALSE; config->backup_in_same_dir = FALSE; config->backup_ext = g_strdup ("~"); config->backup_dir = g_strdup (g_get_home_dir ()); } static gboolean pre_config (gchar * file) { gint x; FILE *tmp; gchar *_tmp; katoob_debug (__FUNCTION__); /************************************************************************** * If the user had used 0.1 before "the configuration file was ~/.katoob, * * Then we'll sadly erase the file ;-) * **************************************************************************/ _tmp = g_strdup_printf ("%s/.katoob", g_get_home_dir ()); if (g_file_test (_tmp, G_FILE_TEST_IS_REGULAR)) { x = remove (_tmp); if (x != 0) { g_warning ("Can't remove old configuration file"); g_free (_tmp); return FALSE; } } /* The first time to run, No ~/.katoob directory */ if (!g_file_test (_tmp, G_FILE_TEST_IS_DIR)) { /* Try to make it */ x = mkdir (_tmp, 0700); if (x != 0) { g_warning ("Can't create configuration directory"); g_free (_tmp); return FALSE; } g_free (_tmp); /* Try to make an empty configuration file */ _tmp = g_strdup_printf ("%s/.katoob/%s", g_get_home_dir (), file); tmp = fopen (_tmp, "w"); g_free (_tmp); if (!tmp) { g_warning ("Can't create configuration file"); return FALSE; } fclose (tmp); } return TRUE; } void config_load (conf * config) { gint x; gchar *configfile, *key, *value, *buff; FILE *prefs; gchar *_tmp; katoob_debug (__FUNCTION__); if (!pre_config ("config")) { return; } key = g_malloc (128); value = g_malloc (128); buff = g_malloc (128); configfile = g_strdup_printf ("%s/.katoob/config", g_get_home_dir ()); prefs = fopen (configfile, "r"); if (prefs) { while (1) { buff = fgets (buff, 127, prefs); if (!buff) { break; } for (x = 0; x < 127; x++) { if (buff[x] != '\t' && buff[x] != ' ') break; } if (x == 126) continue; buff += x; if (buff[0] == '\n') { continue; } sscanf (buff, "%s = %s\n", key, value); _tmp = g_strdup_printf ("%s: %s = %s", __FUNCTION__, key, value); katoob_debug (_tmp); g_free (_tmp); if ((x = strcmp (key, "x")) == 0) { config->x = atoi (value); } if ((x = strcmp (key, "y")) == 0) { config->y = atoi (value); } if ((x = strcmp (key, "w")) == 0) { config->w = atoi (value); } if ((x = strcmp (key, "h")) == 0) { config->h = atoi (value); } if ((x = strcmp (key, "recentno")) == 0) { config->recentno = atoi (value); } if ((x = strcmp (key, "tabsmenu")) == 0) { config->tabsmenu = atoi (value); } if ((x = strcmp (key, "linenumbers")) == 0) { config->linenumbers = atoi (value); } if ((x = strcmp (key, "toolbar")) == 0) { config->toolbar = atoi (value); } if ((x = strcmp (key, "extended_toolbar")) == 0) { config->extended_toolbar = atoi (value); } if ((x = strcmp (key, "statusbar")) == 0) { config->statusbar = atoi (value); } if ((x = strcmp (key, "savewinpos")) == 0) { config->savewinpos = atoi (value); } if ((x = strcmp (key, "saveonexit")) == 0) { config->saveonexit = atoi (value); } if ((x = strcmp (key, "textwrap")) == 0) { config->textwrap = atoi (value); } if ((x = strcmp (key, "text_dir")) == 0) { config->text_dir = atoi (value); } if ((x = strcmp (key, "recent")) == 0) { config->recent = atoi (value); } if ((x = strcmp (key, "tabsmenu")) == 0) { config->tabsmenu = atoi (value); } if ((x = strcmp (key, "showtabs")) == 0) { config->showtabs = atoi (value); } if ((x = strcmp (key, "showclose")) == 0) { config->showclose = atoi (value); } if ((x = strcmp (key, "scrolltabs")) == 0) { config->scrolltabs = atoi (value); } if ((x = strcmp (key, "tabspos")) == 0) { config->tabspos = atoi (value); } if ((x = strcmp (key, "toolbartype")) == 0) { if (value) { g_free (config->toolbartype); config->toolbartype = g_strdup (value); } } if ((x = strcmp (key, "filesno")) == 0) { config->filesno = atoi (value); } if ((x = strcmp (key, "xkb_err_dlg")) == 0) { config->xkb_err_dlg = atoi (value); } if ((x = strcmp (key, "xkb")) == 0) { config->xkb = atoi (value); } if ((x = strcmp (key, "undono")) == 0) { config->undono = atoi (value); } if ((x = strcmp (key, "undo")) == 0) { config->undo = atoi (value); } #ifdef HAVE_SPELL if ((x = strcmp (key, "spell_check")) == 0) { config->spell_check = atoi (value); } if ((x = strcmp (key, "mispelled_red")) == 0) { config->mispelled_red = atoi (value); } if ((x = strcmp (key, "mispelled_blue")) == 0) { config->mispelled_blue = atoi (value); } if ((x = strcmp (key, "mispelled_green")) == 0) { config->mispelled_green = atoi (value); } if ((x = strcmp (key, "dicts_dir")) == 0) { if (value) { g_free (config->dicts_dir); config->dicts_dir = g_strdup (value); } } if ((x = strcmp (key, "default_dict")) == 0) { if (value) { config->default_dict = g_strdup (value); } } #endif /* HAVE_SPELL */ if ((x = strcmp (key, "emulator")) == 0) { config->emulator = atoi (value); } if ((x = strcmp (key, "disable_xft")) == 0) { config->disable_xft = atoi (value); } if ((x = strcmp (key, "locale_enc")) == 0) { config->locale_enc = atoi (value); } if ((x = strcmp (key, "special_enc")) == 0) { config->special_enc = atoi (value); } if ((x = strcmp (key, "saved_enc")) == 0) { if (value) { config->saved_enc = g_strdup (value); } } if ((x = strcmp (key, "font")) == 0) { if (value) { config->font = g_strdup (decode_font (value)); } } if ((x = strcmp (key, "default_font")) == 0) { config->default_font = atoi (value); } #ifdef ENABLE_HIGHLIGHT if ((x = strcmp (key, "highlight")) == 0) { config->highlight = atoi (value); } #endif /* ENABLE_HIGHLIGHT */ if ((x = strcmp (key, "backup")) == 0) { config->backup = atoi (value); } if ((x = strcmp (key, "backup_in_same_dir")) == 0) { config->backup_in_same_dir = atoi (value); } if ((x = strcmp (key, "backup_ext")) == 0) { if (value) { g_free (config->backup_ext); config->backup_ext = g_strdup (value); } } if ((x = strcmp (key, "backup_dir")) == 0) { if (value) { g_free (config->backup_dir); config->backup_dir = g_strdup (value); } } } fclose (prefs); } else { g_free (configfile); g_free (buff); g_free (key); g_free (value); return; } g_free (configfile); g_free (buff); g_free (key); g_free (value); return; } void config_save () { extern conf *config; extern UI *katoob; gchar *configfile; FILE *prefs; katoob_debug (__FUNCTION__); if (!config->saveonexit) { config_free (); config = g_malloc (sizeof (conf)); config_init (config); config_load (config); config->saveonexit = FALSE; } if (!pre_config ("config")) { return; } configfile = g_strdup_printf ("%s/.katoob/config", g_get_home_dir ()); prefs = fopen (configfile, "w"); if (!prefs) { g_warning ("Can't write configuration file"); g_free (configfile); return; } if (config->savewinpos) { katoob_debug ("Getting the main window position"); gtk_window_get_position (GTK_WINDOW (katoob->win), &config->x, &config->y); gtk_window_get_size (GTK_WINDOW (katoob->win), &config->w, &config->h); } if (config->saved_enc) { g_free (config->saved_enc); } config->saved_enc = katoob_encodings_get_by_number (config->defenc); /* fprintf (prefs, "ver = %s\n", VERSION); */ fprintf (prefs, "x = %i\n", config->x); fprintf (prefs, "y = %i\n", config->y); fprintf (prefs, "w = %i\n", config->w); fprintf (prefs, "h = %i\n", config->h); fprintf (prefs, "recentno = %i\n", config->recentno); fprintf (prefs, "tabsmenu = %i\n", config->tabsmenu); fprintf (prefs, "toolbar = %i\n", config->toolbar); fprintf (prefs, "extended_toolbar = %i\n", config->extended_toolbar); fprintf (prefs, "statusbar = %i\n", config->statusbar); fprintf (prefs, "savewinpos = %i\n", config->savewinpos); fprintf (prefs, "saveonexit = %i\n", config->saveonexit); fprintf (prefs, "textwrap = %i\n", config->textwrap); fprintf (prefs, "text_dir = %i\n", config->text_dir); fprintf (prefs, "recent = %i\n", config->recent); fprintf (prefs, "tabsmenu = %i\n", config->tabsmenu); fprintf (prefs, "showtabs = %i\n", config->showtabs); fprintf (prefs, "showclose = %i\n", config->showclose); fprintf (prefs, "scrolltabs = %i\n", config->scrolltabs); fprintf (prefs, "tabspos = %i\n", config->tabspos); fprintf (prefs, "toolbartype = %s\n", config->toolbartype); fprintf (prefs, "filesno = %i\n", config->filesno); fprintf (prefs, "xkb_err_dlg = %i\n", config->xkb_err_dlg); fprintf (prefs, "xkb = %i\n", config->xkb); fprintf (prefs, "undo = %i\n", config->undo); fprintf (prefs, "undono = %i\n", config->undono); #ifdef HAVE_SPELL fprintf (prefs, "spell_check = %i\n", config->spell_check); fprintf (prefs, "mispelled_red = %u\n", config->mispelled_red); fprintf (prefs, "mispelled_blue = %u\n", config->mispelled_blue); fprintf (prefs, "mispelled_green = %u\n", config->mispelled_green); if (config->dicts_dir) { fprintf (prefs, "dicts_dir = %s\n", config->dicts_dir); } if (config->default_dict) { fprintf (prefs, "default_dict = %s\n", config->default_dict); } #endif /* HAVE_SPELL */ fprintf (prefs, "linenumbers = %i\n", config->linenumbers); fprintf (prefs, "emulator = %i\n", config->emulator); fprintf (prefs, "disable_xft = %i\n", config->disable_xft); fprintf (prefs, "locale_enc = %i\n", config->locale_enc); fprintf (prefs, "special_enc = %i\n", config->special_enc); fprintf (prefs, "saved_enc = %s\n", config->saved_enc); if (config->font) { fprintf (prefs, "font = %s\n", encode_font (config->font)); } fprintf (prefs, "default_font = %i\n", config->default_font); #ifdef ENABLE_HIGHLIGHT fprintf (prefs, "highlight = %i\n", config->highlight); #endif /* ENABLE_HIGHLIGHT */ fprintf (prefs, "backup = %i\n", config->backup); fprintf (prefs, "backup_in_same_dir = %i\n", config->backup_in_same_dir); if (config->backup_ext) { fprintf (prefs, "backup_ext = %s\n", config->backup_ext); } if (config->backup_dir) { fprintf (prefs, "backup_dir = %s\n", config->backup_dir); } fclose (prefs); g_free (configfile); return; } void config_free () { extern conf *config; katoob_debug (__FUNCTION__); if (config->font) { g_free (config->font); } #ifdef HAVE_SPELL if (config->dicts_dir) { g_free (config->dicts_dir); } if (config->default_dict) { g_free (config->default_dict); } #endif /* HAVE_SPELL */ g_free (config->toolbartype); g_free (config); config = NULL; } static gchar * encode_font (gchar * font) { gchar *new_font = font; katoob_debug (__FUNCTION__); if (!font) { return NULL; } katoob_debug (font); while (*new_font) { if (*new_font == ' ') { *new_font = '_'; } new_font++; } katoob_debug (font); return font; } static gchar * decode_font (gchar * font) { gchar *new_font = font; katoob_debug (__FUNCTION__); katoob_debug (font); while (*new_font) { if (*new_font == '_') { *new_font = ' '; } new_font++; } katoob_debug (font); return font; } #ifdef ENABLE_PRINT void print_config_load () { gint x; gchar *key, *value, *buff; FILE *prefs; gchar *_tmp; katoob_debug (__FUNCTION__); if (!pre_config ("print")) { return; } _tmp = g_strdup_printf ("%s/.katoob/print", g_get_home_dir ()); prefs = fopen (_tmp, "r"); g_free (_tmp); if (!prefs) { return; } key = g_malloc (128); value = g_malloc (128); buff = g_malloc (128); while (1) { buff = fgets (buff, 127, prefs); if (!buff) { break; } if (buff == "\n") { continue; } sscanf (buff, "%s = %s\n", key, value); _tmp = g_strdup_printf ("%s: %s = %s", __FUNCTION__, key, value); katoob_debug (_tmp); g_free (_tmp); if ((x = strcmp (key, "font_family")) == 0) { if (value) { g_free (PConf->font_family); PConf->font_family = g_strdup (decode_font (value)); } } if ((x = strcmp (key, "font_style")) == 0) { if (value) { g_free (PConf->font_style); PConf->font_style = g_strdup (decode_font (value)); } } if ((x = strcmp (key, "font_scale")) == 0) { if (value) { g_free (PConf->font_scale); PConf->font_scale = g_strdup (value); } } if ((x = strcmp (key, "custom_entry")) == 0) { if (value) { g_free (PConf->custom_entry); PConf->custom_entry = g_strdup (value); } } if ((x = strcmp (key, "file_entry")) == 0) { if (value) { g_free (PConf->file_entry); PConf->file_entry = g_strdup (value); } } if ((x = strcmp (key, "pdf_entry")) == 0) { if (value) { g_free (PConf->pdf_entry); PConf->pdf_entry = g_strdup (value); } } if ((x = strcmp (key, "printer")) == 0) { PConf->printer = atoi (value); } if ((x = strcmp (key, "location")) == 0) { PConf->location = atoi (value); } if ((x = strcmp (key, "paper_size")) == 0) { PConf->paper_size = atoi (value); } if ((x = strcmp (key, "paper_width")) == 0) { PConf->paper_width = atoi (value); } if ((x = strcmp (key, "paper_height")) == 0) { PConf->paper_height = atoi (value); } if ((x = strcmp (key, "paper_size_unite")) == 0) { PConf->paper_size_unite = atoi (value); } if ((x = strcmp (key, "top_margin")) == 0) { PConf->top_margin = atoi (value); } if ((x = strcmp (key, "left_margin")) == 0) { PConf->left_margin = atoi (value); } if ((x = strcmp (key, "right_margin")) == 0) { PConf->right_margin = atoi (value); } if ((x = strcmp (key, "bottom_margin")) == 0) { PConf->bottom_margin = atoi (value); } if ((x = strcmp (key, "page_orientation")) == 0) { PConf->page_orientation = atoi (value); } if ((x = strcmp (key, "dpi_x")) == 0) { PConf->dpi_x = atoi (value); } if ((x = strcmp (key, "dpi_y")) == 0) { PConf->dpi_y = atoi (value); } if ((x = strcmp (key, "copies")) == 0) { PConf->copies = atoi (value); } if ((x = strcmp (key, "gui_show_advanced")) == 0) { PConf->gui_show_advanced = atoi (value); } } g_free (key); g_free (value); g_free (buff); fclose (prefs); } void print_config_save () { extern conf *config; FILE *fp; gchar *_tmp; katoob_debug (__FUNCTION__); if (!config->saveonexit) { return; } if (!pre_config ("print")) { return; } _tmp = g_strdup_printf ("%s/.katoob/print", g_get_home_dir ()); fp = fopen (_tmp, "w"); g_free (_tmp); if (!fp) { g_warning ("Can't open the printing configuration file."); return; } fprintf (fp, "font_family = %s\n", encode_font (PConf->font_family)); fprintf (fp, "font_style = %s\n", encode_font (PConf->font_style)); fprintf (fp, "font_scale = %s\n", PConf->font_scale); fprintf (fp, "custom_entry = %s\n", PConf->custom_entry); fprintf (fp, "file_entry = %s\n", PConf->file_entry); fprintf (fp, "pdf_entry = %s\n", PConf->pdf_entry); fprintf (fp, "printer = %i\n", PConf->printer); fprintf (fp, "location = %i\n", PConf->location); fprintf (fp, "paper_size = %i\n", PConf->paper_size); fprintf (fp, "paper_width = %i\n", PConf->paper_width); fprintf (fp, "paper_height = %i\n", PConf->paper_height); fprintf (fp, "paper_size_unite = %i\n", PConf->paper_size_unite); fprintf (fp, "top_margin = %i\n", PConf->top_margin); fprintf (fp, "left_margin = %i\n", PConf->left_margin); fprintf (fp, "right_margin = %i\n", PConf->right_margin); fprintf (fp, "bottom_margin = %i\n", PConf->bottom_margin); fprintf (fp, "page_orientation = %i\n", PConf->page_orientation); fprintf (fp, "dpi_x = %i\n", PConf->dpi_x); fprintf (fp, "dpi_y = %i\n", PConf->dpi_y); fprintf (fp, "copies = %i\n", PConf->copies); fprintf (fp, "gui_show_advanced = %i\n", PConf->gui_show_advanced); fclose (fp); } void print_config_init () { katoob_debug (__FUNCTION__); PConf = g_malloc (sizeof (PConfig)); PConf->font_family = g_strdup (DEFAULT_FAMILY); PConf->font_style = g_strdup (DEFAULT_STYLE); PConf->font_scale = g_strdup (DEFAULT_SIZE); PConf->printer = PRINTER_POSTSCRIPT; PConf->location = LOCATION_LPR; PConf->custom_entry = g_strdup ("lpr"); PConf->file_entry = g_strdup_printf ("%s/katoob.ps", g_get_home_dir ()); PConf->pdf_entry = g_strdup_printf ("%s/katoob.pdf", g_get_home_dir ()); PConf->paper_size = 6; /* A4 */ PConf->paper_width = 0; PConf->paper_height = 0; PConf->paper_size_unite = PAPER_SIZE_UNITS_INCHES; PConf->top_margin = 1; PConf->left_margin = 1; PConf->right_margin = 1; PConf->bottom_margin = 1; PConf->page_orientation = PAGE_ORIENTATION_PORTRAIT; PConf->dpi_x = 150; PConf->dpi_y = 150; PConf->copies = 1; PConf->gui_show_advanced = 0; } void print_config_free () { katoob_debug (__FUNCTION__); g_free (PConf->font_family); g_free (PConf->font_style); g_free (PConf->font_scale); g_free (PConf->custom_entry); g_free (PConf->file_entry); g_free (PConf->pdf_entry); g_free (PConf); } #endif /* ENABLE_PRINT */