Pogger/src/Preferences.cpp

213 lines
4.7 KiB
C++
Raw Normal View History

2020-12-30 22:07:54 -06:00
/*
* Copyright 2020, Jaidyn Levesque <jadedctrl@teknik.io>
* All rights reserved. Distributed under the terms of the MIT license.
*/
2021-01-24 23:20:11 -06:00
#include "Preferences.h"
2020-07-03 19:13:41 -05:00
2021-02-27 14:28:53 -06:00
#include <String.h>
2021-03-03 20:45:46 -06:00
#include "Util.h"
2020-12-30 22:07:54 -06:00
2021-01-24 23:20:11 -06:00
Preferences::Preferences() {
2020-07-03 19:13:41 -05:00
}
2020-07-07 20:17:52 -05:00
2020-12-30 22:07:54 -06:00
2021-01-24 23:20:11 -06:00
Preferences::Preferences(Preferences* pref) {
}
2020-12-30 22:07:54 -06:00
2020-07-07 20:17:52 -05:00
// !! handle file status
void
2021-01-24 23:20:11 -06:00
Preferences::Load()
2020-07-07 20:17:52 -05:00
{
BPath cfgPath;
find_directory(B_USER_SETTINGS_DIRECTORY, &cfgPath);
cfgPath.Append("Pogger");
2021-03-20 11:44:39 -05:00
if (BDirectory(cfgPath.Path()).InitCheck() == B_ENTRY_NOT_FOUND) {
BDirectory(cfgPath.Path()).CreateDirectory(cfgPath.Path(), NULL);
}
BString filename = BString(cfgPath.Path()).Append("/Settings");
2020-07-07 20:17:52 -05:00
2021-03-03 20:45:46 -06:00
BFile file = BFile(filename.String(), B_READ_ONLY);
2021-01-12 19:08:42 -06:00
status_t result = file.InitCheck();
2020-07-07 20:17:52 -05:00
2021-03-03 20:45:46 -06:00
if (result == B_PERMISSION_DENIED || result == B_NO_MEMORY)
_FileError(result);
2020-07-07 20:17:52 -05:00
BMessage storage;
2021-01-12 19:08:42 -06:00
storage.Unflatten(&file);
2021-01-25 19:39:31 -06:00
fEntryDir = BString(storage.GetString("entryDir", "/boot/home/feeds/"));
2021-01-28 00:19:36 -06:00
fOpenAs = storage.GetInt8("openAs", kOpenAsUrl);
fOpenWith = BString(storage.GetString("openWith",
"application/x-vnd.Haiku-WebPositive"));
2021-01-25 19:39:31 -06:00
fNewNotify = storage.GetBool("notifyNew", true);
fFailureNotify = storage.GetBool("notifyFailure", true);
fUpdateInterval = storage.GetInt8("updateInterval", 1);
2021-03-02 19:41:26 -06:00
fMainWindowRect = storage.GetRect("mainWindow",
BRect(BPoint(-1000.0, -1000.0), BSize(520, 380)));
fFeedEditRect = storage.GetRect("feedEditWindow",
BRect(BPoint(-1000.0, -1000.0), BSize(500, 150)));
fTabSelection = storage.GetInt32("tabSelection", 0);
2020-07-07 20:17:52 -05:00
}
2020-12-30 22:07:54 -06:00
2020-07-07 20:17:52 -05:00
// !! handle file status
void
2021-01-25 19:39:31 -06:00
Preferences::Save()
2020-07-07 20:17:52 -05:00
{
BPath cfgPath;
find_directory(B_USER_SETTINGS_DIRECTORY, &cfgPath);
cfgPath.Append("Pogger");
BEntry cfgEntry = BEntry(cfgPath.Path());
BDirectory cfgDir;
2020-07-07 20:17:52 -05:00
cfgDir.CreateDirectory(cfgPath.Path(), NULL);
2020-07-07 20:17:52 -05:00
if (cfgEntry.Exists() == false)
cfgDir.CreateDirectory(cfgPath.Path(), NULL);
2020-07-07 20:17:52 -05:00
BMessage storage;
BString filename = BString(cfgPath.Path()).Append("/Settings");
2020-07-07 20:17:52 -05:00
2021-03-03 20:45:46 -06:00
BFile file = BFile(filename.String(), B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
status_t result = file.InitCheck();
2021-03-03 20:45:46 -06:00
if (result != B_OK) {
_FileError(result);
return;
}
2020-07-07 20:17:52 -05:00
2021-01-25 19:39:31 -06:00
storage.AddString("entryDir", fEntryDir.String());
storage.AddString("openWith", fOpenWith.String());
2021-01-28 00:19:36 -06:00
storage.AddInt8("openAs", fOpenAs);
2021-01-25 19:39:31 -06:00
storage.AddBool("notifyNew", fNewNotify);
storage.AddBool("notifyFailure", fFailureNotify);
storage.AddInt8("updateInterval", fUpdateInterval);
2020-07-07 20:17:52 -05:00
2021-03-02 19:41:26 -06:00
storage.AddRect("mainWindow", fMainWindowRect);
storage.AddRect("feedEditWindow", fFeedEditRect);
storage.AddInt32("tabSelection", fTabSelection);
storage.Flatten(&file);
2020-07-07 20:17:52 -05:00
}
2021-01-25 19:39:31 -06:00
int64
Preferences::UpdateInterval()
{
int hours = fUpdateInterval - 1;
switch (hours)
{
case -1:
return -1;
case 0:
return HOUR_IN_MICROSECONDS / 2;
default:
return HOUR_IN_MICROSECONDS * hours;
}
}
int
Preferences::UpdateIntervalIndex()
{
return fUpdateInterval;
}
void
Preferences::SetUpdateIntervalIndex(int8 index)
{
fUpdateInterval = index;
}
2021-01-28 00:19:36 -06:00
BString
Preferences::EntryDir()
2021-01-25 19:39:31 -06:00
{
2021-01-28 00:19:36 -06:00
return fEntryDir;
2021-01-25 19:39:31 -06:00
}
2021-01-28 00:19:36 -06:00
status_t
Preferences::SetEntryDir(const char* path)
2021-01-25 19:39:31 -06:00
{
2021-03-03 20:45:46 -06:00
status_t testStatus = BDirectory(path).InitCheck();
if (BDirectory(path).IsDirectory() == false)
return B_NOT_A_DIRECTORY;
2021-01-28 00:19:36 -06:00
if (testStatus == B_OK)
fEntryDir = BString(path);
return testStatus;
2021-01-25 19:39:31 -06:00
}
2021-01-28 00:19:36 -06:00
BString
Preferences::EntryOpenWith()
2021-01-25 19:39:31 -06:00
{
2021-01-28 00:19:36 -06:00
return fOpenWith;
2021-01-25 19:39:31 -06:00
}
2021-01-28 00:19:36 -06:00
status_t
Preferences::SetEntryOpenWith(const char* binPath)
2021-01-25 19:39:31 -06:00
{
2021-01-28 00:19:36 -06:00
// status_t testStatus = BEntry(binPath).InitCheck();
// if (testStatus == B_OK)
fOpenWith = BString(binPath);
return B_OK;
// return testStatus;
2021-01-25 19:39:31 -06:00
}
bool
Preferences::EntryOpenAsHtml()
{
return fOpenAs == kOpenAsHtml;
}
bool
Preferences::SetEntryOpenAsHtml(bool asHtml)
{
if (asHtml == true)
fOpenAs = kOpenAsHtml;
else
fOpenAs = kOpenAsUrl;
return asHtml;
}
2021-03-03 20:45:46 -06:00
void
Preferences::_FileError(status_t result)
{
BPath cfgPath;
find_directory(B_USER_SETTINGS_DIRECTORY, &cfgPath);
BString permLabel("Couldn't open the preferences file because permission "
"was denied.\nThis usually means that you don't have read permissions to "
"your settings directory.\nPlease make sure that your user has "
"read-access to your settings directory― likely %path%.\nCheck your OS "
"documentation for more information.");
permLabel.ReplaceAll("%path%", cfgPath.Path());
userFileError(result, "Preferences file",
"Couldn't open the preferences file because the path is not "
"specified.\nThis usually means that the programmer made a mistake.\n"
"Please submit a bug report to the Pogger repository on GitHub.\n"
"Your personal settings will not be loaded.",
permLabel.String(),
"There is not enough memory available on your system to load the "
"preferences file.\nPlease try closing a few applications and restarting "
"Pogger.");
}