-
Notifications
You must be signed in to change notification settings - Fork 6
/
Preferences.cpp
137 lines (106 loc) · 2.62 KB
/
Preferences.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
* Copyright 2009-2012 Scott McCreary
* Based on BeVexed by DarkWyrm Copyright 2007-2009
*
* Distributed under terms of the MIT License.
*
*/
#include "Preferences.h"
#include <Screen.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <PathFinder.h>
#include <string>
#include <stdio.h>
#include <Roster.h>
#include <Application.h>
#include <Path.h>
BLocker Preferences::fPrefsLock;
BPath Preferences::fPrefsPath;
BMessage Preferences::fPreferences;
void ConstrainWindowFrameToScreen(BRect *rect)
{
if(!rect)
return;
// Even though we have the proper settings, we're going to make sure that
// the whole window can be seen on screen.
BRect screenframe = BScreen().Frame();
// make sure that the top left corner is actually on the screen
int32 xoff = 0, yoff = 0;
if(rect->left < 0)
xoff = (int32) (0-rect->left) + 5;
if(rect->top < 0)
yoff = (int32) (0-rect->top) + 15;
rect->OffsetBy(xoff,yoff);
// If either dimension is too large for the screen, we move and resize the rect so
// that it occupies the screen in that dimension
if(rect->Width() > screenframe.Width())
{
rect->left = 5;
rect->right = screenframe.right - 5;
}
if(rect->Height() > screenframe.Height())
{
// We set top to 15 to account for the window tab
rect->top = 15;
rect->bottom = screenframe.bottom - 5;
}
if(rect->right > screenframe.right)
rect->right = screenframe.right - 5;
if(rect->bottom > screenframe.bottom)
rect->bottom = screenframe.bottom - 5;
}
void
Preferences::Init()
{
if (status_t status = find_directory(B_USER_SETTINGS_DIRECTORY,
&fPrefsPath) == B_OK) {
status = fPrefsPath.Append("HexVexed");
if (status == B_OK)
status = create_directory(fPrefsPath.Path(), 0777);
if (status == B_OK)
fPrefsPath.Append("HexVexed_settings");
}
}
status_t
Preferences::Save()
{
if (!fPrefsLock.IsLocked())
return B_ERROR;
BFile prefsFile(fPrefsPath.Path(), B_READ_WRITE | B_ERASE_FILE | B_CREATE_FILE);
status_t status = prefsFile.InitCheck();
if (status != B_OK)
return status;
return fPreferences.Flatten(&prefsFile);
}
status_t
Preferences::Load()
{
if(!fPrefsLock.IsLocked())
return B_ERROR;
BFile prefsFile(fPrefsPath.Path(), B_READ_ONLY);
status_t status = prefsFile.InitCheck();
if (status != B_OK)
return status;
return fPreferences.Unflatten(&prefsFile);
}
status_t
Preferences::LockPreferences()
{
return fPrefsLock.Lock();
}
void
Preferences::UnlockPreferences()
{
fPrefsLock.Unlock();
}
BMessage &
Preferences::Message()
{
if(!fPrefsLock.IsLocked())
return *(BMessage *)NULL;
return fPreferences;
}