Files
haiku-beta6/src/apps/deskcalc/CalcApplication.cpp
T
Adrien Destugues 7c2ffab858 Patch from Jason Fagnière (Protonux) as part of GCI :
* i18nize devices, charactermap, cdplayer, activitymonitor.

Thanks!

(note : there are some known 80-column problems in this patch)


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39835 a95241bf-73f2-0310-859d-f6bbb57e9c96
2010-12-13 21:00:43 +00:00

156 lines
2.9 KiB
C++

/*
* Copyright 2006 Haiku, Inc. All Rights Reserved.
* Copyright 1997, 1998 R3 Software Ltd. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Timothy Wayper <[email protected]>
* Stephan Aßmus <[email protected]>
*/
#include "CalcApplication.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <Catalog.h>
#include <Directory.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
#include "CalcWindow.h"
#undef B_TRANSLATE_CONTEXT
#define B_TRANSLATE_CONTEXT "CalcApplication"
static const char* kSettingsFileName = B_TRANSLATE("DeskCalc_settings");
const char* kAppSig = "application/x-vnd.Haiku-DeskCalc";
static const float kDefaultWindowWidth = 220.0;
static const float kDefaultWindowHeight = 140.0;
CalcApplication::CalcApplication()
:
BApplication(kAppSig),
fCalcWindow(NULL)
{
}
CalcApplication::~CalcApplication()
{
}
void
CalcApplication::ReadyToRun()
{
BMessage settings;
_LoadSettings(settings);
BRect frame(0, 0, kDefaultWindowWidth - 1, kDefaultWindowHeight - 1);
fCalcWindow = new CalcWindow(frame, &settings);
// reveal window
fCalcWindow->Show();
}
void
CalcApplication::AboutRequested()
{
// TODO: implement me!
return BApplication::AboutRequested();
}
bool
CalcApplication::QuitRequested()
{
// save current user preferences
_SaveSettings();
return true;
}
// #pragma mark -
void
CalcApplication::_LoadSettings(BMessage& archive)
{
// locate preferences file
BFile prefsFile;
if (_InitSettingsFile(&prefsFile, false) < B_OK) {
printf("no preference file found.\n");
return;
}
// unflatten settings data
if (archive.Unflatten(&prefsFile) < B_OK) {
printf("error unflattening settings.\n");
}
}
void
CalcApplication::_SaveSettings()
{
if (!fCalcWindow->Lock())
return;
// archive the current state of the calculator
BMessage archive;
status_t ret = fCalcWindow->SaveSettings(&archive);
fCalcWindow->Unlock();
if (ret < B_OK) {
fprintf(stderr, "CalcApplication::_SaveSettings() - error from window: %s\n", strerror(ret));
return;
}
// flatten entire acrhive and write to settings file
BFile prefsFile;
ret = _InitSettingsFile(&prefsFile, true);
if (ret < B_OK) {
fprintf(stderr, "CalcApplication::_SaveSettings() - error creating file: %s\n", strerror(ret));
return;
}
ret = archive.Flatten(&prefsFile);
if (ret < B_OK) {
fprintf(stderr, "CalcApplication::_SaveSettings() - error flattening to file: %s\n", strerror(ret));
return;
}
}
status_t
CalcApplication::_InitSettingsFile(BFile* file, bool write)
{
// find user settings directory
BPath prefsPath;
status_t ret = find_directory(B_USER_SETTINGS_DIRECTORY, &prefsPath);
if (ret < B_OK)
return ret;
ret = prefsPath.Append(kSettingsFileName);
if (ret < B_OK)
return ret;
if (write) {
ret = file->SetTo(prefsPath.Path(),
B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY);
} else
ret = file->SetTo(prefsPath.Path(), B_READ_ONLY);
return ret;
}