Refactor preference saving/loading

This commit is contained in:
Jaidyn Ann 2021-07-28 19:10:09 -05:00
parent d696ef55b3
commit f038c492f7
13 changed files with 148 additions and 365 deletions

View File

@ -96,7 +96,7 @@ Conversation::ImMessage(BMessage* msg)
|| (text.IFindFirst(contact->GetName()) != B_ERROR));
// Send a notification, if appropriate
if (winFocused == false && AppPreferences::Item()->NotifyNewMessage
if (winFocused == false && AppPreferences::Get()->NotifyNewMessage
&& (fUsers.CountItems() <= 2 || mentioned == true))
{
BString notifyTitle = B_TRANSLATE("New mention");

View File

@ -18,6 +18,7 @@
#include <Application.h>
#include <Catalog.h>
#include <Debug.h>
#include <Directory.h>
#include <Entry.h>
#include <Notification.h>
#include <Path.h>
@ -136,7 +137,7 @@ Server::Filter(BMessage* message, BHandler **target)
return result;
// "Whoops" notification
if (AppPreferences::Item()->NotifyProtocolStatus == true
if (AppPreferences::Get()->NotifyProtocolStatus == true
&& message->FindString("name", &name) == B_OK) {
BBitmap* icon = new BBitmap(message);
BString content(B_TRANSLATE("%user% has been disabled!"));
@ -552,7 +553,7 @@ Server::ImMessage(BMessage* msg)
if (msg->FindFloat("progress", &progress) != B_OK)
return result;
if (!AppPreferences::Item()->NotifyProtocolStatus)
if (!AppPreferences::Get()->NotifyProtocolStatus)
break;
BNotification notification(B_PROGRESS_NOTIFICATION);
@ -586,7 +587,7 @@ Server::ImMessage(BMessage* msg)
fAccountEnabled.AddItem(looper->Protocol()->GetName(), true);
// Ready notification
if (AppPreferences::Item()->NotifyProtocolStatus == true)
if (AppPreferences::Get()->NotifyProtocolStatus == true)
_ProtocolNotification(looper, BString(B_TRANSLATE("Connected")),
BString(B_TRANSLATE("%user% has connected!")));

View File

@ -113,16 +113,29 @@ ChatResources()
const char*
AccountsPath()
SettingsPath()
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
return NULL;
path.Append(APP_NAME "/Accounts");
path.Append(APP_NAME);
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
}
const char*
AccountsPath()
{
BPath path(SettingsPath());
if (path.InitCheck() != B_OK)
return NULL;
path.Append("Accounts");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
}
@ -148,10 +161,11 @@ AccountPath(const char* signature, const char* subsignature)
const char*
CachePath()
{
BPath path;
if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
BPath path(SettingsPath());
if (path.InitCheck() != B_OK)
return NULL;
path.Append(APP_NAME "/Cache");
path.Append("Cache");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
return path.Path();
@ -165,6 +179,7 @@ AccountCachePath(const char* accountName)
path.Append("Accounts");
if (path.InitCheck() != B_OK)
return NULL;
path.Append(accountName);
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
@ -178,6 +193,7 @@ RoomsCachePath(const char* accountName)
BPath path(AccountCachePath(accountName));
if (path.InitCheck() != B_OK)
return NULL;
path.Append("Rooms");
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
@ -189,7 +205,9 @@ const char*
RoomCachePath(const char* accountName, const char* roomIdentifier)
{
BPath path(RoomsCachePath(accountName));
if (path.InitCheck() != B_OK) return NULL;
if (path.InitCheck() != B_OK)
return NULL;
path.Append(roomIdentifier);
return path.Path();
}
@ -199,9 +217,12 @@ const char*
UserCachePath(const char* accountName, const char* userIdentifier)
{
BPath path(AccountCachePath(accountName));
if (path.InitCheck() != B_OK) return NULL;
if (path.InitCheck() != B_OK)
return NULL;
path.Append("Users");
if (create_directory(path.Path(), 0755) != B_OK) return NULL;
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
path.Append(userIdentifier);
return path.Path();
}
@ -211,9 +232,12 @@ const char*
ContactCachePath(const char* accountName, const char* userIdentifier)
{
BPath path(AccountCachePath(accountName));
if (path.InitCheck() != B_OK) return NULL;
if (path.InitCheck() != B_OK)
return NULL;
path.Append("Contacts");
if (create_directory(path.Path(), 0755) != B_OK) return NULL;
if (create_directory(path.Path(), 0755) != B_OK)
return NULL;
path.Append(userIdentifier);
return path.Path();
}

View File

@ -31,6 +31,8 @@ BString CommandArgs(BString line);
BResources* ChatResources();
const char* SettingsPath();
const char* AccountsPath();
const char* AccountPath(const char* signature, const char* subsignature);

View File

@ -1,230 +1,82 @@
/*
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License.
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#include "AppPreferences.h"
#include "Cardie.h"
#include <string.h>
#include <stdlib.h>
#include "Utils.h"
template<> const char* AppPreferences::fFolder = APP_NAME;
template<> const char* AppPreferences::fFilename = "preferences";
/* TODO update _Add* methods to
don't take the BPositionIO argument
and to automatically increase
length counters. For example
the _AddBool() method, should
take the BPositionIO argument from
a private class value, and increase
the the size of another class value
respectively. This way the api looks better
and the possibility of bugs related to
size become very minimal : ).
*/
AppPreferences* AppPreferences::fInstance = NULL;
AppPreferencesData::AppPreferencesData()
:
MoveToCurrentWorkspace(true),
RaiseOnMessageReceived(false),
RaiseUserIsTyping(false),
MarkUnreadWindow(true),
HideDeskbar(false),
DisableReplicant(true),
IgnoreEmoticons(true),
NotifyProtocolStatus(true),
NotifyContactStatus(false),
NotifyNewMessage(true),
HideOffline(true),
DisableQuitConfirm(true)
AppPreferences*
AppPreferences::Get()
{
}
AppPreferencesData::~AppPreferencesData()
{
}
bool
AppPreferencesData::IsFixedSize() const
{
return false;
}
type_code
AppPreferencesData::TypeCode() const
{
return APP_PREFERENCES_TYPE;
}
bool
AppPreferencesData::AllowsTypeCode(type_code code) const
{
if (code == APP_PREFERENCES_TYPE)
return true;
return false;
}
ssize_t
AppPreferencesData::FlattenedSize() const
{
// NOTE add the size of every settings
// you added.
ssize_t size = sizeof(bool) * 11;
return size;
}
status_t
AppPreferencesData::Flatten(BPositionIO* flatData) const
{
if (flatData == NULL)
return B_BAD_VALUE;
// Write our type code
type_code code = APP_PREFERENCES_TYPE;
flatData->Write(&code, sizeof(type_code));
// Behaviour
_AddBool(flatData, MoveToCurrentWorkspace);
_AddBool(flatData, RaiseOnMessageReceived);
_AddBool(flatData, RaiseUserIsTyping);
_AddBool(flatData, MarkUnreadWindow);
_AddBool(flatData, NotifyProtocolStatus);
_AddBool(flatData, NotifyContactStatus);
_AddBool(flatData, NotifyNewMessage);
_AddBool(flatData, DisableQuitConfirm);
// Replicant
_AddBool(flatData, HideDeskbar);
_AddBool(flatData, DisableReplicant);
// Chat window
_AddBool(flatData, IgnoreEmoticons);
// Contact list
_AddBool(flatData, HideOffline);
// Usage example for strings :
// _AddString(flatData, yourBString.String());
// NOTE : The order is very important, Unflatten and Flatten
// classes should read/write the values in the same order.
return B_OK;
}
status_t
AppPreferencesData::Flatten(void* buffer, ssize_t size) const
{
if (buffer == NULL)
return B_BAD_VALUE;
BMemoryIO flatData(buffer, size);
return Flatten(&flatData, size);
}
status_t
AppPreferencesData::Unflatten(type_code code, const void* buffer, ssize_t size)
{
if (buffer == NULL)
return B_BAD_VALUE;
BMemoryIO flatData(buffer, size);
return Unflatten(code, &flatData);
}
status_t
AppPreferencesData::Unflatten(type_code code, BPositionIO* flatData)
{
if (code != APP_PREFERENCES_TYPE || flatData == NULL)
return B_BAD_VALUE;
// Reading our type code
type_code typeCode;
flatData->Read(&typeCode, sizeof(type_code));
// checking if the typecode is correct
if (code != typeCode)
return B_BAD_VALUE;
// Behaviour
MoveToCurrentWorkspace = _ReadBool(flatData);
RaiseOnMessageReceived = _ReadBool(flatData);
RaiseUserIsTyping = _ReadBool(flatData);
MarkUnreadWindow = _ReadBool(flatData);
NotifyProtocolStatus = _ReadBool(flatData);
NotifyContactStatus = _ReadBool(flatData);
NotifyNewMessage = _ReadBool(flatData);
DisableQuitConfirm = _ReadBool(flatData);
// Replicant
HideDeskbar = _ReadBool(flatData);
DisableReplicant = _ReadBool(flatData);
// Chat window
IgnoreEmoticons = _ReadBool(flatData);
// Contact list
HideOffline = _ReadBool(flatData);
// Usage example for strings :
// const char* str = _ReadString(flatData);
// yourBString.SetTo(str);
// NOTE : The order is very important, Unflatten and Flatten
// classes should read/write the values in the same order.
return B_OK;
if (fInstance == NULL) {
fInstance = new AppPreferences();
fInstance->Load();
}
return fInstance;
}
void
AppPreferencesData::_AddBool(BPositionIO* data, bool value) const
AppPreferences::Load()
{
data->Write(&value, sizeof(value));
const char* path = _PreferencesPath();
BFile file(_PreferencesPath(), B_READ_ONLY);
BMessage settings;
if (file.InitCheck() == B_OK)
settings.Unflatten(&file);
MoveToCurrentWorkspace = settings.GetBool("MoveToCurrentWorkpace", false);
RaiseOnMessageReceived = settings.GetBool("RaiseOnMessageReceived", false);
RaiseUserIsTyping = settings.GetBool("RaiseUserIsTyping", false);
MarkUnreadWindow = settings.GetBool("MarkUnreadWindow", true);
NotifyProtocolStatus = settings.GetBool("NotifyProtocolStatus", true);
NotifyNewMessage = settings.GetBool("NotifyNewMessage", true);
NotifyContactStatus = settings.GetBool("NotifyContactStatus", false);
HideDeskbar = settings.GetBool("HideDeskbar", false);
DisableReplicant = settings.GetBool("DisableReplicant", true);
DisableQuitConfirm = settings.GetBool("DisableQuitConfirm", false);
IgnoreEmoticons = settings.GetBool("IgnoreEmoticons", true);
HideOffline = settings.GetBool("HideOffline", false);
}
void
AppPreferencesData::_AddString(BPositionIO* data, const char* value) const
AppPreferences::Save()
{
size_t len = strlen(value);
data->Write(&len, sizeof(size_t));
data->Write(value, len);
}
const char* path = _PreferencesPath();
BFile file(_PreferencesPath(), B_WRITE_ONLY);
BMessage settings;
settings.AddBool("MoveToCurrentWorkpace", MoveToCurrentWorkspace);
settings.AddBool("RaiseOnMessageReceived", RaiseOnMessageReceived);
settings.AddBool("RaiseUserIsTyping", RaiseUserIsTyping);
settings.AddBool("MarkUnreadWindow", MarkUnreadWindow);
settings.AddBool("NotifyProtocolStatus", NotifyProtocolStatus);
settings.AddBool("NotifyNewMessage", NotifyNewMessage);
settings.AddBool("NotifyContactStatus", NotifyContactStatus);
settings.AddBool("HideDeskbar", HideDeskbar);
settings.AddBool("DisableReplicant", DisableReplicant);
settings.AddBool("DisableQuitConfirm", DisableQuitConfirm);
settings.AddBool("IgnoreEmoticons", IgnoreEmoticons);
settings.AddBool("HideOffline", HideOffline);
bool
AppPreferencesData::_ReadBool(BPositionIO* data)
{
bool ret;
data->Read(&ret, sizeof(bool));
return ret;
if (file.InitCheck() == B_OK)
settings.Flatten(&file);
}
const char*
AppPreferencesData::_ReadString(BPositionIO* data)
AppPreferences::_PreferencesPath()
{
size_t len;
data->Read(&len, sizeof(size_t));
char* ret = new char[len];
data->Read(ret, len);
return ret;
BPath path(SettingsPath());
path.Append("preferences");
return path.Path();
}

View File

@ -1,54 +1,42 @@
/*
* Copyright 2010, Oliver Ruiz Dorantes. All rights reserved.
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License.
* Copyright 2021, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
#ifndef _APP_PREFERENCES_H
#define _APP_PREFERENCES_H
#include "PreferencesContainer.h"
#include <SupportDefs.h>
class AppPreferencesData : public BFlattenable {
class AppPreferences {
public:
AppPreferencesData();
virtual ~AppPreferencesData();
static AppPreferences* Get();
virtual bool IsFixedSize() const;
virtual type_code TypeCode() const;
virtual bool AllowsTypeCode(type_code code) const;
virtual ssize_t FlattenedSize() const;
void Load();
void Save();
status_t Flatten(BPositionIO* flatData) const;
virtual status_t Flatten(void* buffer, ssize_t size) const;
virtual status_t Unflatten(type_code code, const void* buffer,
ssize_t size);
status_t Unflatten(type_code code, BPositionIO* flatData);
bool MoveToCurrentWorkspace;
bool RaiseOnMessageReceived;
bool RaiseUserIsTyping;
bool MarkUnreadWindow;
bool NotifyProtocolStatus;
bool NotifyContactStatus;
bool NotifyNewMessage;
bool MoveToCurrentWorkspace;
bool RaiseOnMessageReceived;
bool RaiseUserIsTyping;
bool MarkUnreadWindow;
bool NotifyProtocolStatus;
bool NotifyContactStatus;
bool NotifyNewMessage;
bool HideDeskbar;
bool DisableReplicant;
bool DisableQuitConfirm;
bool HideDeskbar;
bool DisableReplicant;
bool DisableQuitConfirm;
bool IgnoreEmoticons;
bool IgnoreEmoticons;
bool HideOffline;
private:
void _AddBool(BPositionIO* data, bool value) const;
void _AddString(BPositionIO* data,
const char* value) const;
bool HideOffline;
bool _ReadBool(BPositionIO* data);
const char* _ReadString(BPositionIO* data);
private:
const char* _PreferencesPath();
static AppPreferences* fInstance;
};
typedef PreferencesContainer<AppPreferencesData> AppPreferences;
#endif // _APP_PREFERENCES_H
#endif // _APP_PREFERENCES_H

View File

@ -138,23 +138,23 @@ PreferencesBehavior::AttachedToWindow()
fDisableQuitConfirm->SetTarget(this);
fHideOffline->SetValue(
AppPreferences::Item()->HideOffline);
AppPreferences::Get()->HideOffline);
fToCurrentWorkspace->SetValue(
AppPreferences::Item()->MoveToCurrentWorkspace);
AppPreferences::Get()->MoveToCurrentWorkspace);
fRaiseUserIsTyping->SetValue(
AppPreferences::Item()->RaiseUserIsTyping);
AppPreferences::Get()->RaiseUserIsTyping);
fRaiseOnMessageReceived->SetValue(
AppPreferences::Item()->RaiseOnMessageReceived);
AppPreferences::Get()->RaiseOnMessageReceived);
fMarkUnreadWindow->SetValue(
AppPreferences::Item()->MarkUnreadWindow);
AppPreferences::Get()->MarkUnreadWindow);
fNotifyProtocols->SetValue(
AppPreferences::Item()->NotifyProtocolStatus);
AppPreferences::Get()->NotifyProtocolStatus);
fNotifyContactStatus->SetValue(
AppPreferences::Item()->NotifyContactStatus);
AppPreferences::Get()->NotifyContactStatus);
fNotifyNewMessage->SetValue(
AppPreferences::Item()->NotifyNewMessage);
AppPreferences::Get()->NotifyNewMessage);
fDisableQuitConfirm->SetValue(
AppPreferences::Item()->DisableQuitConfirm);
AppPreferences::Get()->DisableQuitConfirm);
}
@ -163,39 +163,39 @@ PreferencesBehavior::MessageReceived(BMessage* message)
{
switch (message->what) {
case kHideOffline:
AppPreferences::Item()->HideOffline
AppPreferences::Get()->HideOffline
= fHideOffline->Value();
break;
case kToCurrentWorkspace:
AppPreferences::Item()->MoveToCurrentWorkspace
AppPreferences::Get()->MoveToCurrentWorkspace
= fToCurrentWorkspace->Value();
break;
case kRaiseOnMessageReceived:
AppPreferences::Item()->RaiseOnMessageReceived
AppPreferences::Get()->RaiseOnMessageReceived
= fRaiseOnMessageReceived->Value();
break;
case kRaiseUserIsTyping:
AppPreferences::Item()->RaiseUserIsTyping
AppPreferences::Get()->RaiseUserIsTyping
= fRaiseUserIsTyping->Value();
break;
case kNotifyProtocolsLogin:
AppPreferences::Item()->NotifyProtocolStatus
AppPreferences::Get()->NotifyProtocolStatus
= fNotifyProtocols->Value();
break;
case kNotifyContactStatus:
AppPreferences::Item()->NotifyContactStatus
AppPreferences::Get()->NotifyContactStatus
= fNotifyContactStatus->Value();
break;
case kNotifyNewMessage:
AppPreferences::Item()->NotifyNewMessage
AppPreferences::Get()->NotifyNewMessage
= fNotifyNewMessage->Value();
break;
case kMarkUnreadWindow:
AppPreferences::Item()->MarkUnreadWindow
AppPreferences::Get()->MarkUnreadWindow
= fMarkUnreadWindow->Value();
break;
case kDisablePrompt:
AppPreferences::Item()->DisableQuitConfirm
AppPreferences::Get()->DisableQuitConfirm
= fDisableQuitConfirm->Value();
break;
default:

View File

@ -53,7 +53,7 @@ void
PreferencesChatWindow::AttachedToWindow()
{
fIgnoreEmoticons->SetTarget(this);
fIgnoreEmoticons->SetValue(AppPreferences::Item()->IgnoreEmoticons);
fIgnoreEmoticons->SetValue(AppPreferences::Get()->IgnoreEmoticons);
}
@ -62,7 +62,7 @@ PreferencesChatWindow::MessageReceived(BMessage* message)
{
switch (message->what) {
case kIgnoreEmoticons:
AppPreferences::Item()->IgnoreEmoticons
AppPreferences::Get()->IgnoreEmoticons
= fIgnoreEmoticons->Value();
break;
default:

View File

@ -1,84 +0,0 @@
/*
* Copyright 2010, Oliver Ruiz Dorantes. All rights reserved.
* Copyright 2012, Casalinuovo Dario. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _PREFERENCES_CONTAINER_H
#define _PREFERENCES_CONTAINER_H
#include <libsupport/Singleton.h>
#include <Directory.h>
#include <File.h>
#include <FindDirectory.h>
#include <Path.h>
enum {
APP_PREFERENCES_TYPE = 'CPTY'
};
// TODO: added to main singleton class?
template<typename T> T* Singleton<T>::fInstance = 0;
template<class AppPreferencesData>
class PreferencesContainer
: public Singleton<PreferencesContainer<AppPreferencesData> > {
public:
static AppPreferencesData*
Item()
{
return &(Singleton<PreferencesContainer<AppPreferencesData> >
::Get()->fSettings);
}
status_t Load()
{
if (fPreferencesFile.SetTo(&fDirectory, fFilename,
B_READ_WRITE | B_FAIL_IF_EXISTS) == B_OK) {
return fSettings.Unflatten(APP_PREFERENCES_TYPE,
&fPreferencesFile);
}
return B_ERROR;
}
status_t Save()
{
if (fPreferencesFile.SetTo(&fDirectory, fFilename,
B_READ_WRITE | B_CREATE_FILE | B_ERASE_FILE) == B_OK) {
return fSettings.Flatten(&fPreferencesFile);
}
return B_ERROR;
}
private:
PreferencesContainer<AppPreferencesData>()
: Singleton<PreferencesContainer<AppPreferencesData> >()
{
BPath path;
find_directory(B_USER_SETTINGS_DIRECTORY, &path);
path.Append(fFolder);
fDirectory.SetTo(path.Path());
Load();
}
AppPreferencesData fSettings;
BFile fPreferencesFile;
BDirectory fDirectory;
static const char* fFilename;
static const char* fFolder;
friend class Singleton<PreferencesContainer<AppPreferencesData> >;
};
#endif // _PREFERENCES_CONTAINER_H

View File

@ -36,7 +36,7 @@ PreferencesReplicant::PreferencesReplicant()
B_TRANSLATE("Disable deskbar replicant"),
new BMessage(kDisableReplicant));
if (!AppPreferences::Item()->HideDeskbar)
if (!AppPreferences::Get()->HideDeskbar)
Looper()->PostMessage(new BMessage(kDisableReplicant));
fPermanentReplicant = new BCheckBox("PermanentReplicant",
@ -72,9 +72,9 @@ PreferencesReplicant::AttachedToWindow()
fDisableReplicant->SetTarget(this);
fHideDeskbar->SetValue(
AppPreferences::Item()->HideDeskbar);
AppPreferences::Get()->HideDeskbar);
fDisableReplicant->SetValue(
AppPreferences::Item()->DisableReplicant);
AppPreferences::Get()->DisableReplicant);
}
@ -83,11 +83,11 @@ PreferencesReplicant::MessageReceived(BMessage* message)
{
switch (message->what) {
case kHideDeskbar:
AppPreferences::Item()->HideDeskbar
AppPreferences::Get()->HideDeskbar
= fHideDeskbar->Value();
break;
case kDisableReplicant:
AppPreferences::Item()->DisableReplicant
AppPreferences::Get()->DisableReplicant
= fDisableReplicant->Value();
if (fDisableReplicant->Value() == true)

View File

@ -361,7 +361,7 @@ instantiate_deskbar_item(void)
status_t
ReplicantStatusView::InstallReplicant()
{
if (AppPreferences::Item()->DisableReplicant == true)
if (AppPreferences::Get()->DisableReplicant == true)
return B_OK;
BDeskbar deskbar;

View File

@ -149,7 +149,7 @@ RosterView::ImMessage(BMessage* msg)
UpdateListItem(rosterItem);
// Check if the user want the notification
if (!AppPreferences::Item()->NotifyContactStatus)
if (!AppPreferences::Get()->NotifyContactStatus)
break;
switch (status) {

View File

@ -80,7 +80,7 @@ bool
MainWindow::QuitRequested()
{
int32 button_index = 0;
if(!AppPreferences::Item()->DisableQuitConfirm)
if(!AppPreferences::Get()->DisableQuitConfirm)
{
BAlert* alert = new BAlert(B_TRANSLATE("Closing"),
B_TRANSLATE("Are you sure you want to quit?"),