1////////////////////////////////////////////////////////////////////////////////
2//
3//	File: Prefs.cpp
4//
5//	Date: December 1999
6//
7//	Author: Daniel Switkin
8//
9//	Copyright 2003 (c) by Daniel Switkin. This file is made publically available
10//	under the BSD license, with the stipulations that this complete header must
11//	remain at the top of the file indefinitely, and credit must be given to the
12//	original author in any about box using this software.
13//
14////////////////////////////////////////////////////////////////////////////////
15
16// Additional authors:	Stephan Aßmus, <superstippi@gmx.de>
17
18#include "Prefs.h"
19#include <File.h>
20#include <Path.h>
21#include <FindDirectory.h>
22#include <stdio.h>
23
24extern bool debug;
25
26// constructor
27Prefs::Prefs()
28	: interlaced(false),
29	  usetransparent(false),
30	  usetransparentauto(false),
31	  usedithering(false),
32	  transparentred(0),
33	  transparentgreen(0),
34	  transparentblue(0),
35	  palettemode(0),
36	  palette_size_in_bits(8),
37	  file(NULL)
38{
39	BPath path;
40	find_directory(B_USER_SETTINGS_DIRECTORY, &path);
41	path.Append("GIFTranslator_settings");
42	file = new BFile(path.Path(), B_READ_WRITE | B_CREATE_FILE);
43
44	if (file->InitCheck() != B_OK) {
45		if (debug) printf("Bad filename for attributes\n");
46		return;
47	}
48
49	bool db = false;
50	if (!GetBool("interlaced", &interlaced, &db)) return;
51	db = false;
52	if (!GetBool("usetransparent", &usetransparent, &db)) return;
53	db = false;
54	if (!GetBool("usetransparentauto", &usetransparentauto, &db)) return;
55	db = true;
56	if (!GetBool("usedithering", &usedithering, &db)) return;
57	int di = 0;
58	if (!GetInt("palettemode", &palettemode, &di)) return;
59	di = 8;
60	if (!GetInt("palettesize", &palette_size_in_bits, &di)) return;
61	di = 0;
62	if (!GetInt("transparentred", &transparentred, &di)) return;
63	di = 0;
64	if (!GetInt("transparentgreen", &transparentgreen, &di)) return;
65	di = 0;
66	if (!GetInt("transparentblue", &transparentblue, &di)) return;
67}
68
69// destructor
70Prefs::~Prefs()
71{
72	delete file;
73}
74
75// GetInt
76bool
77Prefs::GetInt(const char *name, int *value, int *defaultvalue)
78{
79	status_t err = file->ReadAttr(name, B_INT32_TYPE, 0, value, 4);
80	if (err == B_ENTRY_NOT_FOUND) {
81		*value = *defaultvalue;
82		if (file->WriteAttr(name, B_INT32_TYPE, 0, defaultvalue, 4) < 0) {
83			if (debug) printf("WriteAttr on %s died\n", name);
84			return false;
85		}
86	} else if  (err < 0) {
87		if (debug) printf("Unknown error reading %s\n", name);
88		return false;
89	}
90	return true;
91}
92
93// GetBool
94bool
95Prefs::GetBool(const char *name, bool *value, bool *defaultvalue)
96{
97	status_t err = file->ReadAttr(name, B_BOOL_TYPE, 0, value, 1);
98	if (err == B_ENTRY_NOT_FOUND) {
99		*value = *defaultvalue;
100		if (file->WriteAttr(name, B_BOOL_TYPE, 0, defaultvalue, 1) < 0) {
101			if (debug) printf("WriteAttr on %s died\n", name);
102			return false;
103		}
104	} else if (err < 0) {
105		if (debug) printf("Unknown error reading %s\n", name);
106		return false;
107	}
108	return true;
109}
110
111// PutInt
112bool
113Prefs::PutInt(const char *name, int *value)
114{
115	status_t err = file->WriteAttr(name, B_INT32_TYPE, 0, value, 4);
116	if (err < 0) {
117		if (debug) printf("WriteAttr on %s died\n", name);
118		return false;
119	}
120	return true;
121}
122
123// PutBool
124bool
125Prefs::PutBool(const char *name, bool *value)
126{
127	status_t err = file->WriteAttr(name, B_BOOL_TYPE, 0, value, 1);
128	if (err < 0) {
129		if (debug) printf("WriteAttr on %s died\n", name);
130		return false;
131	}
132	return true;
133}
134
135// Save
136void
137Prefs::Save()
138{
139	if (!PutBool("interlaced", &interlaced)) return;
140	if (!PutBool("usetransparent", &usetransparent)) return;
141	if (!PutBool("usetransparentauto", &usetransparentauto)) return;
142	if (!PutBool("usedithering", &usedithering)) return;
143	if (!PutInt("palettemode", &palettemode)) return;
144	if (!PutInt("palettesize", &palette_size_in_bits)) return;
145	if (!PutInt("transparentred", &transparentred)) return;
146	if (!PutInt("transparentgreen", &transparentgreen)) return;
147	if (!PutInt("transparentblue", &transparentblue)) return;
148}
149
150