xref: /haiku/src/preferences/repositories/RepositoriesSettings.cpp (revision 77de2e81a19ca1dfa7b94837df36959c8ed87038)
1*5bf2b6ebSBrian Hill /*
2*5bf2b6ebSBrian Hill  * Copyright 2017 Haiku Inc. All rights reserved.
3*5bf2b6ebSBrian Hill  * Distributed under the terms of the MIT License.
4*5bf2b6ebSBrian Hill  *
5*5bf2b6ebSBrian Hill  * Authors:
6*5bf2b6ebSBrian Hill  *		Brian Hill
7*5bf2b6ebSBrian Hill  */
8*5bf2b6ebSBrian Hill 
9*5bf2b6ebSBrian Hill 
10*5bf2b6ebSBrian Hill #include "RepositoriesSettings.h"
11*5bf2b6ebSBrian Hill 
12*5bf2b6ebSBrian Hill #include <FindDirectory.h>
13*5bf2b6ebSBrian Hill #include <StringList.h>
14*5bf2b6ebSBrian Hill 
15*5bf2b6ebSBrian Hill #include "constants.h"
16*5bf2b6ebSBrian Hill 
17*5bf2b6ebSBrian Hill const char* settingsFilename = "Repositories_settings";
18*5bf2b6ebSBrian Hill 
19*5bf2b6ebSBrian Hill 
RepositoriesSettings()20*5bf2b6ebSBrian Hill RepositoriesSettings::RepositoriesSettings()
21*5bf2b6ebSBrian Hill {
22*5bf2b6ebSBrian Hill 	status_t status = find_directory(B_USER_SETTINGS_DIRECTORY, &fFilePath);
23*5bf2b6ebSBrian Hill 	if (status == B_OK)
24*5bf2b6ebSBrian Hill 		status = fFilePath.Append(settingsFilename);
25*5bf2b6ebSBrian Hill 	fInitStatus = status;
26*5bf2b6ebSBrian Hill }
27*5bf2b6ebSBrian Hill 
28*5bf2b6ebSBrian Hill 
29*5bf2b6ebSBrian Hill BRect
GetFrame()30*5bf2b6ebSBrian Hill RepositoriesSettings::GetFrame()
31*5bf2b6ebSBrian Hill {
32*5bf2b6ebSBrian Hill 	BMessage settings(_ReadFromFile());
33*5bf2b6ebSBrian Hill 	BRect frame;
34*5bf2b6ebSBrian Hill 	status_t status = settings.FindRect(key_frame, &frame);
35*5bf2b6ebSBrian Hill 	// Set default off screen so it will center itself
36*5bf2b6ebSBrian Hill 	if (status != B_OK)
37*5bf2b6ebSBrian Hill 		frame.Set(-10, -10, 750, 300);
38*5bf2b6ebSBrian Hill 	return frame;
39*5bf2b6ebSBrian Hill }
40*5bf2b6ebSBrian Hill 
41*5bf2b6ebSBrian Hill 
42*5bf2b6ebSBrian Hill void
SetFrame(BRect frame)43*5bf2b6ebSBrian Hill RepositoriesSettings::SetFrame(BRect frame)
44*5bf2b6ebSBrian Hill {
45*5bf2b6ebSBrian Hill 	BMessage settings(_ReadFromFile());
46*5bf2b6ebSBrian Hill 	settings.RemoveData(key_frame);
47*5bf2b6ebSBrian Hill 	settings.AddRect(key_frame, frame);
48*5bf2b6ebSBrian Hill 	_SaveToFile(settings);
49*5bf2b6ebSBrian Hill }
50*5bf2b6ebSBrian Hill 
51*5bf2b6ebSBrian Hill 
52*5bf2b6ebSBrian Hill status_t
GetRepositories(int32 & repoCount,BStringList & nameList,BStringList & urlList)53*5bf2b6ebSBrian Hill RepositoriesSettings::GetRepositories(int32& repoCount, BStringList& nameList,
54*5bf2b6ebSBrian Hill 	BStringList& urlList)
55*5bf2b6ebSBrian Hill {
56*5bf2b6ebSBrian Hill 	BMessage settings(_ReadFromFile());
57*5bf2b6ebSBrian Hill 	type_code type;
58*5bf2b6ebSBrian Hill 	int32 count;
59*5bf2b6ebSBrian Hill 	settings.GetInfo(key_name, &type, &count);
60*5bf2b6ebSBrian Hill 
61*5bf2b6ebSBrian Hill 	status_t result = B_OK;
62*5bf2b6ebSBrian Hill 	int32 index, total = 0;
63*5bf2b6ebSBrian Hill 	BString foundName, foundUrl;
64*5bf2b6ebSBrian Hill 	// get each repository and add to lists
65*5bf2b6ebSBrian Hill 	for (index = 0; index < count; index++) {
66*5bf2b6ebSBrian Hill 		status_t result1 = settings.FindString(key_name, index, &foundName);
67*5bf2b6ebSBrian Hill 		status_t result2 = settings.FindString(key_url, index, &foundUrl);
68*5bf2b6ebSBrian Hill 		if (result1 == B_OK && result2 == B_OK) {
69*5bf2b6ebSBrian Hill 			nameList.Add(foundName);
70*5bf2b6ebSBrian Hill 			urlList.Add(foundUrl);
71*5bf2b6ebSBrian Hill 			total++;
72*5bf2b6ebSBrian Hill 		} else
73*5bf2b6ebSBrian Hill 			result = B_ERROR;
74*5bf2b6ebSBrian Hill 	}
75*5bf2b6ebSBrian Hill 	repoCount = total;
76*5bf2b6ebSBrian Hill 	return result;
77*5bf2b6ebSBrian Hill }
78*5bf2b6ebSBrian Hill 
79*5bf2b6ebSBrian Hill 
80*5bf2b6ebSBrian Hill void
SetRepositories(BStringList & nameList,BStringList & urlList)81*5bf2b6ebSBrian Hill RepositoriesSettings::SetRepositories(BStringList& nameList, BStringList& urlList)
82*5bf2b6ebSBrian Hill {
83*5bf2b6ebSBrian Hill 	BMessage settings(_ReadFromFile());
84*5bf2b6ebSBrian Hill 	settings.RemoveName(key_name);
85*5bf2b6ebSBrian Hill 	settings.RemoveName(key_url);
86*5bf2b6ebSBrian Hill 
87*5bf2b6ebSBrian Hill 	int32 index, count = nameList.CountStrings();
88*5bf2b6ebSBrian Hill 	for (index = 0; index < count; index++) {
89*5bf2b6ebSBrian Hill 		settings.AddString(key_name, nameList.StringAt(index));
90*5bf2b6ebSBrian Hill 		settings.AddString(key_url, urlList.StringAt(index));
91*5bf2b6ebSBrian Hill 	}
92*5bf2b6ebSBrian Hill 	_SaveToFile(settings);
93*5bf2b6ebSBrian Hill }
94*5bf2b6ebSBrian Hill 
95*5bf2b6ebSBrian Hill 
96*5bf2b6ebSBrian Hill BMessage
_ReadFromFile()97*5bf2b6ebSBrian Hill RepositoriesSettings::_ReadFromFile()
98*5bf2b6ebSBrian Hill {
99*5bf2b6ebSBrian Hill 	BMessage settings;
100*5bf2b6ebSBrian Hill 	status_t status = fFile.SetTo(fFilePath.Path(), B_READ_ONLY);
101*5bf2b6ebSBrian Hill 	if (status == B_OK)
102*5bf2b6ebSBrian Hill 		status = settings.Unflatten(&fFile);
103*5bf2b6ebSBrian Hill 	fFile.Unset();
104*5bf2b6ebSBrian Hill 	return settings;
105*5bf2b6ebSBrian Hill }
106*5bf2b6ebSBrian Hill 
107*5bf2b6ebSBrian Hill 
108*5bf2b6ebSBrian Hill status_t
_SaveToFile(BMessage settings)109*5bf2b6ebSBrian Hill RepositoriesSettings::_SaveToFile(BMessage settings)
110*5bf2b6ebSBrian Hill {
111*5bf2b6ebSBrian Hill 	status_t status = fFile.SetTo(fFilePath.Path(),
112*5bf2b6ebSBrian Hill 		B_WRITE_ONLY | B_CREATE_FILE | B_ERASE_FILE);
113*5bf2b6ebSBrian Hill 	if (status == B_OK)
114*5bf2b6ebSBrian Hill 		status = settings.Flatten(&fFile);
115*5bf2b6ebSBrian Hill 	fFile.Unset();
116*5bf2b6ebSBrian Hill 	return status;
117*5bf2b6ebSBrian Hill }
118