1 | // $Id: TRPrefsWindow.cpp 261 2006-05-29 21:27:31Z titer $ |
---|
2 | |
---|
3 | #include "TRPrefsWindow.h" |
---|
4 | #include "Prefs.h" |
---|
5 | |
---|
6 | #include <Beep.h> |
---|
7 | #include <Box.h> |
---|
8 | #include <Font.h> |
---|
9 | #include <String.h> |
---|
10 | |
---|
11 | #include <stdlib.h> |
---|
12 | #include <stdio.h> |
---|
13 | |
---|
14 | #include "TRApplication.h" |
---|
15 | |
---|
16 | TRPrefsWindow::TRPrefsWindow() : BWindow(BRect(0, 0, 300, 115), "Settings", |
---|
17 | B_TITLED_WINDOW, |
---|
18 | B_ASYNCHRONOUS_CONTROLS | B_NOT_CLOSABLE | B_NOT_ZOOMABLE | B_NOT_RESIZABLE, |
---|
19 | B_CURRENT_WORKSPACE) |
---|
20 | { |
---|
21 | BRect viewRect = Bounds(); |
---|
22 | viewRect.InsetBy(-1, -1); |
---|
23 | BBox *box = new BBox(viewRect, NULL, B_FOLLOW_ALL); |
---|
24 | |
---|
25 | font_height fh; |
---|
26 | be_plain_font->GetHeight(&fh); |
---|
27 | |
---|
28 | // Text Controls. |
---|
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); |
---|
33 | |
---|
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); |
---|
39 | |
---|
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); |
---|
45 | |
---|
46 | // Buttons |
---|
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); |
---|
52 | |
---|
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); |
---|
57 | |
---|
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); |
---|
63 | |
---|
64 | Lock(); |
---|
65 | AddChild(box); |
---|
66 | ResizeTo(Bounds().Width(), viewRect.bottom + btnSave->Bounds().Height()); |
---|
67 | |
---|
68 | Unlock(); |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | TRPrefsWindow::~TRPrefsWindow() { |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | void TRPrefsWindow::MessageReceived(BMessage *msg) { |
---|
77 | if (msg->what == TR_PREF_SAVE) { |
---|
78 | if (WritePrefs()) { |
---|
79 | Hide(); |
---|
80 | } else { |
---|
81 | beep(); |
---|
82 | } |
---|
83 | } else if (msg->what == TR_PREF_CANCEL) { |
---|
84 | Hide(); |
---|
85 | } else if (msg->what == TR_PREF_DEFAULTS) { |
---|
86 | txtFolder->SetText("/boot/home/Downloads"); |
---|
87 | txtPort->SetText("9000"); |
---|
88 | txtUpload->SetText("20"); |
---|
89 | } |
---|
90 | BWindow::MessageReceived(msg); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | void TRPrefsWindow::Show() { |
---|
95 | ReadPrefs(); |
---|
96 | if (Lock()) { |
---|
97 | txtFolder->MakeFocus(true); |
---|
98 | Unlock(); |
---|
99 | } |
---|
100 | BWindow::Show(); |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | void TRPrefsWindow::ReadPrefs() { |
---|
105 | if (Lock()) { |
---|
106 | Prefs prefs(TRANSMISSION_SETTINGS); |
---|
107 | BString str(""); |
---|
108 | if (prefs.FindString("download.folder", &str) != B_OK) { |
---|
109 | prefs.SetString("download.folder", "/boot/home/Downloads"); |
---|
110 | str << "/boot/home/Downloads"; |
---|
111 | } |
---|
112 | txtFolder->SetText(str.String()); |
---|
113 | |
---|
114 | int32 port; |
---|
115 | if (prefs.FindInt32("transmission.bindPort", &port) != B_OK) { |
---|
116 | prefs.SetInt32("transmission.bindPort", 9000); |
---|
117 | port = 9000; |
---|
118 | } |
---|
119 | str = ""; |
---|
120 | str << port; |
---|
121 | txtPort->SetText(str.String()); |
---|
122 | |
---|
123 | int32 upload; |
---|
124 | if (prefs.FindInt32("transmission.uploadLimit", &upload) != B_OK) { |
---|
125 | prefs.SetInt32("transmission.uploadLimit", 20); |
---|
126 | upload = 20; |
---|
127 | } |
---|
128 | str = ""; |
---|
129 | str << upload; |
---|
130 | txtUpload->SetText(str.String()); |
---|
131 | |
---|
132 | Unlock(); |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | bool TRPrefsWindow::WritePrefs() { |
---|
138 | bool valid = true; |
---|
139 | |
---|
140 | int port = atoi(txtPort->Text()); |
---|
141 | if (port <= 0) { |
---|
142 | valid = false; |
---|
143 | txtPort->MakeFocus(true); |
---|
144 | } |
---|
145 | |
---|
146 | const char* uploadStr = txtUpload->Text(); |
---|
147 | int uploadLimit = atoi(txtUpload->Text()); |
---|
148 | |
---|
149 | for (uint i = 0; i < strlen(uploadStr) && valid; i++) { |
---|
150 | if (!(uploadStr[i] >= '0' && uploadStr[i] <= '9')) { |
---|
151 | valid = false; |
---|
152 | txtUpload->MakeFocus(true); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | if (valid) { |
---|
157 | Prefs prefs(TRANSMISSION_SETTINGS); |
---|
158 | |
---|
159 | prefs.SetInt32("transmission.bindPort", (int32)port); |
---|
160 | prefs.SetString("download.folder", txtFolder->Text()); |
---|
161 | prefs.SetInt32("transmission.uploadLimit", (int32)uploadLimit); |
---|
162 | |
---|
163 | be_app_messenger.SendMessage(new BMessage(TR_RELOAD_SETTINGS)); |
---|
164 | } |
---|
165 | |
---|
166 | return valid; |
---|
167 | } |
---|