Removed all global preferences path constants.

PrefHandler now exports a static method GetDefaultPath() which gets the
path to the default settings file (using find_directory(), of course).
Disabled locale support; should be easy to reenable when needed again, though.
Made all the PrefHandler getters a lot more robust (could easily crash when asking
for an unknown item).
Some cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13853 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2005-07-29 13:12:48 +00:00
parent b11eac8c18
commit 662f52848a
5 changed files with 303 additions and 297 deletions

View File

@ -42,37 +42,35 @@
extern PrefHandler *gTermPref;
#define LOCALE_FILE_DIR PREF_FOLDER"menu/"
//#define LOCALE_FILE_DIR PREF_FOLDER"menu/"
BPopUpMenu *
MakeMenu(ulong msg, const char **items, const char *defaultItemName)
{
BPopUpMenu *menu = new BPopUpMenu("");
int32 i = 0;
while(*items)
{
while (*items) {
menu->AddItem(new BMenuItem(*items, new BMessage(msg)));
if(!strcmp(*items, defaultItemName))
{
(menu->ItemAt(i))->SetMarked(true);
}
if (!strcmp(*items, defaultItemName))
menu->ItemAt(i)->SetMarked(true);
items++;
i++;
}
return menu;
}
int
longname2op(const char *longname)
{
int op = M_UTF8;
const etable *s = encoding_table;
for (int i = 0; s->name; s++, i++)
{
if(!strcmp(s->name, longname))
{
for (int i = 0; s->name; s++, i++) {
if (!strcmp(s->name, longname)) {
op = s->op;
break;
}
@ -80,47 +78,47 @@ longname2op(const char *longname)
return op;
}
const char *
op2longname(int op)
{
return encoding_table[op].name;
}
void
MakeEncodingMenu(BMenu *eMenu, int coding, bool flag)
{
const etable *e = encoding_table;
int i = 0;
while(e->name)
{
while (e->name) {
BMessage *msg = new BMessage(MENU_ENCODING);
msg->AddInt32("op", (int32)e->op);
if (flag)
eMenu->AddItem(new BMenuItem(e->name, msg, e->shortcut));
else
eMenu->AddItem(new BMenuItem(e->name, msg));
if ( i == coding)
{
(eMenu->ItemAt(i))->SetMarked(true);
}
if (i == coding)
eMenu->ItemAt(i)->SetMarked(true);
e++;
i++;
}
}
void
LoadLocaleFile(PrefHandler *pref)
{
char name[B_PATH_NAME_LENGTH];
const char *locale;
char buf[B_PATH_NAME_LENGTH];
status_t sts;
locale = gTermPref->getString (PREF_GUI_LANGUAGE);
sprintf (buf, "%s%s", LOCALE_FILE_DIR, locale);
sts = pref->OpenText (buf);
if (sts == B_ERROR)
pref->OpenText (LOCALE_FILE_DEFAULT);
locale = gTermPref->getString(PREF_GUI_LANGUAGE);
// TODO: this effectively disables any locale support - which is okay for now
sprintf(name, "%s%s", /*LOCALE_FILE_DIR*/"", locale);
//if (pref->OpenText(name) < B_OK)
// pref->OpenText(LOCALE_FILE_DEFAULT);
}

View File

@ -158,40 +158,44 @@ PrefDlg::SaveRequested(BMessage *msg)
{
entry_ref dirref;
const char *filename;
msg->FindRef("directory", &dirref);
msg->FindString("name", &filename);
BDirectory dir(&dirref);
BPath path(&dir, filename);
gTermPref->SaveAsText (path.Path(), PREFFILE_MIMETYPE, TERM_SIGNATURE);
gTermPref->SaveAsText(path.Path(), PREFFILE_MIMETYPE, TERM_SIGNATURE);
}
void
PrefDlg::doSave (void)
PrefDlg::doSave()
{
delete fPrefTemp;
fPrefTemp = new PrefHandler (gTermPref);
gTermPref->SaveAsText (TERM_PREF, PREFFILE_MIMETYPE);
fDirty = false;
fPrefTemp = new PrefHandler(gTermPref);
BPath path;
if (PrefHandler::GetDefaultPath(path) == B_OK) {
gTermPref->SaveAsText(path.Path(), PREFFILE_MIMETYPE);
fDirty = false;
}
}
void
PrefDlg::doRevert (void)
PrefDlg::doRevert()
{
BMessenger messenger (fTermWindow);
delete gTermPref;
gTermPref = new PrefHandler (fPrefTemp);
messenger.SendMessage (MSG_HALF_FONT_CHANGED);
messenger.SendMessage (MSG_COLOR_CHANGED);
messenger.SendMessage (MSG_ROWS_CHANGED);
messenger.SendMessage (MSG_INPUT_METHOD_CHANGED);
gTermPref = new PrefHandler(fPrefTemp);
messenger.SendMessage(MSG_HALF_FONT_CHANGED);
messenger.SendMessage(MSG_COLOR_CHANGED);
messenger.SendMessage(MSG_ROWS_CHANGED);
messenger.SendMessage(MSG_INPUT_METHOD_CHANGED);
fDirty = false;
}

View File

@ -26,286 +26,296 @@
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "Coding.h"
#include "PrefHandler.h"
#include "TermConst.h"
#include <GraphicsDefs.h>
#include <Message.h>
#include <File.h>
#include <Entry.h>
#include <NodeInfo.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <Path.h>
#include "Coding.h"
#include "PrefHandler.h"
#include "TermConst.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
/*
* Startup preference settings.
*/
const prefDefaults termDefaults[] ={
{ PREF_COLS, "80" },
{ PREF_ROWS, "25" },
{ PREF_COLS, "80" },
{ PREF_ROWS, "25" },
{ PREF_HALF_FONT_FAMILY, "Courier10 BT" },
{ PREF_HALF_FONT_SIZE, "12" },
{ PREF_FULL_FONT_FAMILY, "Haru Tohaba" },
{ PREF_FULL_FONT_SIZE, "12" },
{ PREF_HALF_FONT_FAMILY, "Courier10 BT" },
{ PREF_HALF_FONT_SIZE, "12" },
{ PREF_FULL_FONT_FAMILY, "Haru Tohaba" },
{ PREF_FULL_FONT_SIZE, "12" },
{ PREF_TEXT_FORE_COLOR, " 0, 0, 0" },
{ PREF_TEXT_BACK_COLOR, "255, 255, 255" },
{ PREF_SELECT_FORE_COLOR, "255, 255, 255" },
{ PREF_SELECT_BACK_COLOR, " 0, 0, 0" },
{ PREF_CURSOR_FORE_COLOR, "255, 255, 255" },
{ PREF_CURSOR_BACK_COLOR, " 0, 0, 0" },
{ PREF_TEXT_FORE_COLOR, " 0, 0, 0" },
{ PREF_TEXT_BACK_COLOR, "255, 255, 255" },
{ PREF_SELECT_FORE_COLOR, "255, 255, 255" },
{ PREF_SELECT_BACK_COLOR, " 0, 0, 0" },
{ PREF_CURSOR_FORE_COLOR, "255, 255, 255" },
{ PREF_CURSOR_BACK_COLOR, " 0, 0, 0" },
{ PREF_IM_FORE_COLOR, " 0, 0, 0" },
{ PREF_IM_BACK_COLOR, "152, 203, 255" },
{ PREF_IM_SELECT_COLOR, "255, 152, 152" },
{ PREF_IM_FORE_COLOR, " 0, 0, 0" },
{ PREF_IM_BACK_COLOR, "152, 203, 255" },
{ PREF_IM_SELECT_COLOR, "255, 152, 152" },
{ PREF_SHELL, "/bin/sh -login" },
{ PREF_HISTORY_SIZE, "500" },
{ PREF_SHELL, "/bin/sh -login" },
{ PREF_HISTORY_SIZE, "500" },
{ PREF_TEXT_ENCODING, "UTF-8" },
{ PREF_TEXT_ENCODING, "UTF-8" },
{ PREF_SELECT_MBUTTON, "Button 1"},
{ PREF_PASTE_MBUTTON, "Button 2"},
{ PREF_SUBMENU_MBUTTON, "Button 3"},
{ PREF_MOUSE_IMAGE, "Hand cursor"},
{ PREF_DRAGN_COPY, "0"},
{ PREF_SELECT_MBUTTON, "Button 1"},
{ PREF_PASTE_MBUTTON, "Button 2"},
{ PREF_SUBMENU_MBUTTON, "Button 3"},
{ PREF_MOUSE_IMAGE, "Hand cursor"},
{ PREF_DRAGN_COPY, "0"},
{ PREF_GUI_LANGUAGE, "English"},
{ PREF_IM_AWARE, "0"},
{ NULL, NULL},
{ PREF_GUI_LANGUAGE, "English"},
{ PREF_IM_AWARE, "0"},
{ NULL, NULL},
};
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
PrefHandler::PrefHandler()
{
mPrefContainer.what = 'Pref';
OpenText(TERM_PREF, termDefaults);
fContainer.what = 'Pref';
BPath path;
GetDefaultPath(path);
OpenText(path.Path(), termDefaults);
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
PrefHandler::PrefHandler(const PrefHandler* p)
{
mPrefContainer = p->mPrefContainer;
fContainer = p->fContainer;
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
PrefHandler::~PrefHandler()
{
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
/* static */
status_t
PrefHandler::GetDefaultPath(BPath& path)
{
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path, true) != B_OK)
return B_ERROR;
// TODO: maybe just "Terminal"? (but this collides with the R5 Terminal settings file)
path.Append("HaikuTerminal");
path.Append("settings");
return B_OK;
}
status_t
PrefHandler::Open(const char *path, const prefDefaults *defaults)
{
BEntry ent(path);
if(ent.Exists()){
return loadFromFile(&ent);
}else{
return loadFromDefault(defaults);
}
BEntry entry(path);
if (entry.Exists())
return loadFromFile(&entry);
return loadFromDefault(defaults);
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
status_t
PrefHandler::OpenText(const char *path, const prefDefaults *defaults)
PrefHandler::OpenText(const char *path, const prefDefaults *defaults)
{
BEntry ent(path);
if(ent.Exists()){
return loadFromTextFile(path);
}else{
return loadFromDefault(defaults);
}
BEntry entry(path);
if (entry.Exists())
return loadFromTextFile(path);
return loadFromDefault(defaults);
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
status_t
PrefHandler::Save(const char *path)
{
status_t sts;
BFile file(path, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
sts = mPrefContainer.Flatten(&file);
return sts;
// make sure the target path exists
BPath directoryPath(path);
if (directoryPath.GetParent(&directoryPath) == B_OK)
create_directory(directoryPath.Path(), 0755);
BFile file(path, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
return fContainer.Flatten(&file);
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
void
PrefHandler::SaveAsText(const char *path, const char *mimetype,
const char *signature)
const char *signature)
{
BFile file(path, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
type_code type;
char *key;
char buf[256];
for (int32 i = 0; mPrefContainer.GetInfo(B_STRING_TYPE, i, &key, &type) == B_OK; i++){
int len = sprintf(buf, "\"%s\" , \"%s\"\n", key, this->getString(key));
file.Write(buf, len);
}
if(mimetype != NULL){
BNodeInfo info(&file);
info.SetType(mimetype);
info.SetPreferredApp (signature);
}
// make sure the target path exists
BPath directoryPath(path);
if (directoryPath.GetParent(&directoryPath) == B_OK)
create_directory(directoryPath.Path(), 0755);
BFile file(path, B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
char buffer[512];
type_code type;
char *key;
for (int32 i = 0; fContainer.GetInfo(B_STRING_TYPE, i, &key, &type) == B_OK; i++) {
int len = snprintf(buffer, sizeof(buffer), "\"%s\" , \"%s\"\n", key, getString(key));
file.Write(buffer, len);
}
if (mimetype != NULL){
BNodeInfo info(&file);
info.SetType(mimetype);
info.SetPreferredApp(signature);
}
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
int32
PrefHandler::getInt32(const char *key)
{
return atoi(mPrefContainer.FindString(key));
const char *value = fContainer.FindString(key);
if (value == NULL)
return 0;
return atoi(value);
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
float
PrefHandler::getFloat(const char *key)
{
return atof(mPrefContainer.FindString(key));
const char *value = fContainer.FindString(key);
if (value == NULL)
return 0;
return atof(value);
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
const char*
PrefHandler::getString(const char *key)
{
const char *buf;
const char *buffer;
if (fContainer.FindString(key, &buffer) != B_OK)
buffer = "Error!";
if (mPrefContainer.FindString(key, &buf) != B_OK)
buf = "Error!";
//printf("%x GET %s: %s\n", this, key, buf);
return buf;
//printf("%x GET %s: %s\n", this, key, buf);
return buffer;
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
bool
PrefHandler::getBool(const char *key)
{
const char *s = mPrefContainer.FindString(key);
if (!strcmp(s, PREF_TRUE)) return true;
return false;
const char *value = fContainer.FindString(key);
if (value == NULL)
return false;
return !strcmp(value, PREF_TRUE);
}
/////////////////////////////////////////////////////////////////////////////
// getRGB
// Returns RGB data from given key.
/////////////////////////////////////////////////////////////////////////////
/** Returns RGB data from given key. */
rgb_color
PrefHandler::getRGB(const char *key)
{
int r, g, b;
rgb_color col;
if (const char *s = mPrefContainer.FindString(key)) {
sscanf(s, "%d, %d, %d", &r, &g, &b);
} else {
fprintf(stderr, "PrefHandler::getRGB(%s) - key not found\n", key);
r = g = b = 0;
}
col.red = r;
col.green = g;
col.blue = b;
col.alpha = 255;
return col;
rgb_color col;
int r, g, b;
if (const char *s = fContainer.FindString(key)) {
sscanf(s, "%d, %d, %d", &r, &g, &b);
} else {
fprintf(stderr, "PrefHandler::getRGB(%s) - key not found\n", key);
r = g = b = 0;
}
col.red = r;
col.green = g;
col.blue = b;
col.alpha = 255;
return col;
}
/////////////////////////////////////////////////////////////////////////////
// setInt32
// Setting Int32 data with key.
/////////////////////////////////////////////////////////////////////////////
/** Setting Int32 data with key. */
void
PrefHandler::setInt32(const char *key, int32 data)
{
char buf[20];
sprintf(buf, "%d", (int)data);
this->setString(key, buf);
char buffer[32];
snprintf(buffer, sizeof(buffer), "%d", (int)data);
setString(key, buffer);
}
/////////////////////////////////////////////////////////////////////////////
// setFloat
// Setting Float data with key
/////////////////////////////////////////////////////////////////////////////
/** Setting Float data with key */
void
PrefHandler::setFloat(const char *key, float data)
{
char buf[20];
sprintf(buf, "%g", data);
this->setString(key, buf);
char buffer[32];
snprintf(buffer, sizeof(buffer), "%g", data);
setString(key, buffer);
}
/////////////////////////////////////////////////////////////////////////////
// setBool
// Setting Bool data with key
/////////////////////////////////////////////////////////////////////////////
/** Setting Bool data with key */
void
PrefHandler::setBool(const char *key, bool data)
{
if(data){
this->setString(key, PREF_TRUE);
}else{
this->setString(key, PREF_FALSE);
}
if (data)
setString(key, PREF_TRUE);
else
setString(key, PREF_FALSE);
}
/////////////////////////////////////////////////////////////////////////////
// this->setString
// Setting CString data with key
/////////////////////////////////////////////////////////////////////////////
/** Setting CString data with key */
void
PrefHandler::setString(const char *key, const char *data)
{
//printf("%x SET %s: %s\n", this, key, data);
mPrefContainer.RemoveName(key);
mPrefContainer.AddString(key, data);
//printf("%x SET %s: %s\n", this, key, data);
fContainer.RemoveName(key);
fContainer.AddString(key, data);
}
/////////////////////////////////////////////////////////////////////////////
// setRGB
// Setting RGB data with key
/////////////////////////////////////////////////////////////////////////////
/** Setting RGB data with key */
void
PrefHandler::setRGB(const char *key, const rgb_color data)
{
char buf[20];
sprintf(buf, "%d, %d, %d", data.red, data.green, data.blue);
this->setString(key, buf);
char buffer[32];
snprintf(buffer, sizeof(buffer), "%d, %d, %d", data.red, data.green, data.blue);
setString(key, buffer);
}
/////////////////////////////////////////////////////////////////////////////
// IsEmpty
// Check any peference stored or not.
/////////////////////////////////////////////////////////////////////////////
/** Check any peference stored or not. */
bool
PrefHandler::IsEmpty() const
{
return mPrefContainer.IsEmpty();
return fContainer.IsEmpty();
}
/////////////////////////////////////////////////////////////////////////////
//
//
/////////////////////////////////////////////////////////////////////////////
status_t
PrefHandler::loadFromFile(BEntry *ent)
@ -314,8 +324,8 @@ PrefHandler::loadFromFile(BEntry *ent)
// save the settings. (Who cares about compatibility in this case anyway?)
BFile file (ent, B_READ_ONLY);
//mPrefContainer.MakeEmpty();
//mPrefContainer.Unflatten(&file);
//fContainer.MakeEmpty();
//fContainer.Unflatten(&file);
off_t size;
if (file.GetSize(&size) != B_OK || size != sizeof(struct termprefs))
return B_ERROR;

View File

@ -1,7 +1,7 @@
/*
* Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
* Copyright (c) 2004 Daniel Furrer <assimil8or@users.sourceforge.net>
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
* Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files or portions
@ -41,31 +41,30 @@
#define TP_FONT_NAME_SZ 128
struct termprefs {
uint32 magic;
uint32 version;
float x;
float y;
uint32 cols;
uint32 rows;
uint32 tab_width;
uint32 font_size;
char font[TP_FONT_NAME_SZ]; // "Family/Style"
uint32 cursor_blink_rate; // blinktime in µs = 1000000
uint32 refresh_rate; // ??? = 0
rgb_color bg;
rgb_color fg;
rgb_color curbg;
rgb_color curfg;
rgb_color selbg;
rgb_color selfg;
char encoding; // index in the menu (0 = UTF-8)
char unknown[3];
uint32 magic;
uint32 version;
float x;
float y;
uint32 cols;
uint32 rows;
uint32 tab_width;
uint32 font_size;
char font[TP_FONT_NAME_SZ]; // "Family/Style"
uint32 cursor_blink_rate; // blinktime in µs = 1000000
uint32 refresh_rate; // ??? = 0
rgb_color bg;
rgb_color fg;
rgb_color curbg;
rgb_color curfg;
rgb_color selbg;
rgb_color selfg;
char encoding; // index in the menu (0 = UTF-8)
char unknown[3];
};
struct prefDefaults
{
const char *key;
char *item;
struct prefDefaults {
const char *key;
char *item;
};
#define PREF_TRUE "true"
@ -74,42 +73,40 @@ struct prefDefaults
class BMessage;
class BEntry;
class PrefHandler{
public:
PrefHandler();
PrefHandler(const PrefHandler* p);
class PrefHandler {
public:
PrefHandler(const PrefHandler* p);
PrefHandler();
~PrefHandler();
~PrefHandler();
status_t Open(const char *name, const prefDefaults *defaults = NULL);
status_t OpenText(const char *path, const prefDefaults *defaults = NULL);
status_t Save(const char *name);
void SaveAsText(const char *path, const char *minmtype = NULL,
const char *signature = NULL);
status_t Open(const char *name, const prefDefaults *defaults = NULL);
status_t OpenText(const char *path, const prefDefaults *defaults = NULL);
status_t Save(const char *name);
void SaveAsText(const char *path, const char *minmtype = NULL,
const char *signature = NULL);
int32 getInt32(const char *key);
float getFloat(const char *key);
const char* getString(const char *key);
bool getBool(const char *key);
rgb_color getRGB(const char *key);
int32 getInt32(const char *key);
float getFloat(const char *key);
const char* getString(const char *key);
bool getBool(const char *key);
rgb_color getRGB(const char *key);
void setInt32(const char *key, int32 data);
void setFloat(const char *key, float data);
void setString(const char *key, const char *data);
void setBool(const char *key, bool data);
void setRGB(const char *key, const rgb_color data);
void setInt32(const char *key, int32 data);
void setFloat(const char *key, float data);
void setString(const char *key, const char *data);
void setBool(const char *key, bool data);
void setRGB(const char *key, const rgb_color data);
bool IsEmpty() const;
bool IsEmpty() const;
private:
status_t loadFromFile(BEntry *ent);
status_t loadFromDefault(const prefDefaults* defaluts = NULL);
status_t loadFromTextFile(const char * path);
BMessage mPrefContainer;
static status_t GetDefaultPath(BPath& path);
private:
status_t loadFromFile(BEntry *ent);
status_t loadFromDefault(const prefDefaults* defaluts = NULL);
status_t loadFromTextFile(const char * path);
BMessage fContainer;
};
#endif //PREFHANDLER_H_INCLUDED
#endif // PREFHANDLER_H_INCLUDED

View File

@ -1,6 +1,6 @@
/*
* Copyright (c) 2003-4 Kian Duffy <myob@users.sourceforge.net>
* Parts Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
* Copyright (C) 1998,99 Kazuho Okui and Takashi Murai.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files or portions
@ -147,9 +147,6 @@ enum {
////////////////////////////////////////////////////////////////////////////
// Preference Folder and setting path
////////////////////////////////////////////////////////////////////////////
#define PREF_FOLDER "/boot/home/config/settings/MuTerminal/"
const char * const TERM_PREF = PREF_FOLDER "setting.mp";
const char * const LOCALE_FILE_DEFAULT = PREF_FOLDER "menu/en";
const int32 DEFAULT = -1;