1
// $Id: TRPrefsWindow.cpp 261 2006-05-29 21:27:31Z titer $
3
#include "TRPrefsWindow.h"
14
#include "TRApplication.h"
16
TRPrefsWindow::TRPrefsWindow() : BWindow(BRect(0, 0, 300, 115), "Settings",
18
B_ASYNCHRONOUS_CONTROLS | B_NOT_CLOSABLE | B_NOT_ZOOMABLE | B_NOT_RESIZABLE,
21
BRect viewRect = Bounds();
22
viewRect.InsetBy(-1, -1);
23
BBox *box = new BBox(viewRect, NULL, B_FOLLOW_ALL);
26
be_plain_font->GetHeight(&fh);
29
viewRect.Set(5, 5, viewRect.Width() - 10, fh.leading + fh.ascent + 10);
30
txtFolder = new BTextControl(viewRect, "txtFolder", "Download directory:", "",
31
NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP | B_FOLLOW_RIGHT);
32
box->AddChild(txtFolder);
34
viewRect.Set(viewRect.left, viewRect.bottom + 10,
35
viewRect.right, viewRect.bottom + fh.leading + fh.ascent + 15);
36
txtPort = new BTextControl(viewRect, "txtPort", "Listen On Port:", "",
37
NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP | B_FOLLOW_RIGHT);
38
box->AddChild(txtPort);
40
viewRect.Set(viewRect.left, viewRect.bottom + 10,
41
viewRect.right, viewRect.bottom + fh.leading + fh.ascent + 15);
42
txtUpload = new BTextControl(viewRect, "txtUpload", "Upload Limit (KB/sec):", "",
43
NULL, B_FOLLOW_LEFT | B_FOLLOW_TOP | B_FOLLOW_RIGHT);
44
box->AddChild(txtUpload);
47
viewRect.Set(viewRect.left, viewRect.bottom + 20,
48
viewRect.left + be_plain_font->StringWidth("Defaults") + 20,
49
viewRect.bottom + fh.leading + fh.ascent + 10);
50
btnDefaults = new BButton(viewRect, "btnDefault", "Defaults", new BMessage(TR_PREF_DEFAULTS));
51
box->AddChild(btnDefaults);
53
viewRect.OffsetBy(viewRect.Width() + 10, 0);
54
viewRect.right = viewRect.left + be_plain_font->StringWidth("Cancel") + 20;
55
btnCancel = new BButton(viewRect, "btnCancel", "Cancel", new BMessage(TR_PREF_CANCEL));
56
box->AddChild(btnCancel);
58
viewRect.OffsetBy(viewRect.Width() + 15, 0);
59
viewRect.right = viewRect.left + be_plain_font->StringWidth("Apply") + 20;
60
btnSave = new BButton(viewRect, "btnSave", "Apply", new BMessage(TR_PREF_SAVE));
61
btnSave->MakeDefault(true);
62
box->AddChild(btnSave);
66
ResizeTo(Bounds().Width(), viewRect.bottom + btnSave->Bounds().Height());
72
TRPrefsWindow::~TRPrefsWindow() {
76
void TRPrefsWindow::MessageReceived(BMessage *msg) {
77
if (msg->what == TR_PREF_SAVE) {
83
} else if (msg->what == TR_PREF_CANCEL) {
85
} else if (msg->what == TR_PREF_DEFAULTS) {
86
txtFolder->SetText("/boot/home/Downloads");
87
txtPort->SetText("9000");
88
txtUpload->SetText("20");
90
BWindow::MessageReceived(msg);
94
void TRPrefsWindow::Show() {
97
txtFolder->MakeFocus(true);
104
void TRPrefsWindow::ReadPrefs() {
106
Prefs prefs(TRANSMISSION_SETTINGS);
108
if (prefs.FindString("download.folder", &str) != B_OK) {
109
prefs.SetString("download.folder", "/boot/home/Downloads");
110
str << "/boot/home/Downloads";
112
txtFolder->SetText(str.String());
115
if (prefs.FindInt32("transmission.bindPort", &port) != B_OK) {
116
prefs.SetInt32("transmission.bindPort", 9000);
121
txtPort->SetText(str.String());
124
if (prefs.FindInt32("transmission.uploadLimit", &upload) != B_OK) {
125
prefs.SetInt32("transmission.uploadLimit", 20);
130
txtUpload->SetText(str.String());
137
bool TRPrefsWindow::WritePrefs() {
140
int port = atoi(txtPort->Text());
143
txtPort->MakeFocus(true);
146
const char* uploadStr = txtUpload->Text();
147
int uploadLimit = atoi(txtUpload->Text());
149
for (uint i = 0; i < strlen(uploadStr) && valid; i++) {
150
if (!(uploadStr[i] >= '0' && uploadStr[i] <= '9')) {
152
txtUpload->MakeFocus(true);
157
Prefs prefs(TRANSMISSION_SETTINGS);
159
prefs.SetInt32("transmission.bindPort", (int32)port);
160
prefs.SetString("download.folder", txtFolder->Text());
161
prefs.SetInt32("transmission.uploadLimit", (int32)uploadLimit);
163
be_app_messenger.SendMessage(new BMessage(TR_RELOAD_SETTINGS));