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