Пример #1
0
Preferences::Preferences(const char* name, const char* signature, bool doSave)
	: BMessage('Pref'), BLocker("Preferences", true),
	fSavePreferences(doSave)
{
	fNewPreferences = false;
	fSettingsFile = 0;
	BPath prefpath;
	fName = strdup(name);
	if (signature)
		fSignature = strdup(signature);
	else
		fSignature = NULL;

	if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath) == B_OK) {
		BDirectory prefdir(prefpath.Path());
		BEntry entry;
		prefdir.FindEntry(fName, &entry);

		BFile file(&entry, B_READ_ONLY);
		if (file.InitCheck() == B_OK)
			Unflatten(&file);
		else
			fNewPreferences = true;
	}
}
Пример #2
0
Preferences::~Preferences()
{
	if (fSavePreferences) {
		BFile file;
		status_t set = B_ERROR;
		if (fSettingsFile)
			file.SetTo(fSettingsFile, B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE);
		else {
			BPath prefpath;
			if (find_directory(B_USER_SETTINGS_DIRECTORY, &prefpath, true) == B_OK) {
				BDirectory prefdir(prefpath.Path());
				set = prefdir.CreateFile(fName, &file, false);
			}
		}

		if (file.InitCheck () == B_OK) {
			Flatten(&file);
			if (fSignature) {
				file.WriteAttr("BEOS:TYPE", B_MIME_STRING_TYPE, 0, fSignature,
					strlen(fSignature) + 1);
			}
		} else {
			// implement saving somewhere else!
			BString error;
			snprintf(error.LockBuffer(256), 256,
				B_TRANSLATE("Your setting file could not be saved!\n(%s)"),
				strerror(file.InitCheck()));
			error.UnlockBuffer();
			BAlert *alert = new BAlert(B_TRANSLATE("Error saving file"),
				error.String(), B_TRANSLATE("Damned!"), NULL, NULL,
				B_WIDTH_AS_USUAL, B_STOP_ALERT);
			alert->SetShortcut(0, B_ESCAPE);

			alert->Go();
		}
	}
	delete fSettingsFile;
	free(fName);
	free(fSignature);
}