vfs: check userland buffers before reading them.
[haiku.git] / src / apps / mail / Status.cpp
blob6911efddccb1598a5ad647c5387dcce64f959437
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 BeMail(TM), Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
34 #include "Status.h"
36 #include <Button.h>
37 #include <Directory.h>
38 #include <FindDirectory.h>
39 #include <fs_index.h>
40 #include <Node.h>
41 #include <NodeInfo.h>
42 #include <Path.h>
43 #include <Query.h>
44 #include <TextControl.h>
45 #include <Volume.h>
46 #include <VolumeRoster.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
52 #include "MailApp.h"
53 #include "MailWindow.h"
54 #include "Messages.h"
57 #define STATUS_TEXT "Status:"
58 #define STATUS_FIELD_H 10
59 #define STATUS_FIELD_V 8
60 #define STATUS_FIELD_WIDTH (STATUS_WIDTH - STATUS_FIELD_H)
61 #define STATUS_FIELD_HEIGHT 16
63 #define BUTTON_WIDTH 70
64 #define BUTTON_HEIGHT 20
66 #define S_OK_BUTTON_X1 (STATUS_WIDTH - BUTTON_WIDTH - 6)
67 #define S_OK_BUTTON_Y1 (STATUS_HEIGHT - (BUTTON_HEIGHT + 10))
68 #define S_OK_BUTTON_X2 (S_OK_BUTTON_X1 + BUTTON_WIDTH)
69 #define S_OK_BUTTON_Y2 (S_OK_BUTTON_Y1 + BUTTON_HEIGHT)
70 #define S_OK_BUTTON_TEXT "OK"
72 #define S_CANCEL_BUTTON_X1 (S_OK_BUTTON_X1 - (BUTTON_WIDTH + 10))
73 #define S_CANCEL_BUTTON_Y1 S_OK_BUTTON_Y1
74 #define S_CANCEL_BUTTON_X2 (S_CANCEL_BUTTON_X1 + BUTTON_WIDTH)
75 #define S_CANCEL_BUTTON_Y2 S_OK_BUTTON_Y2
76 #define S_CANCEL_BUTTON_TEXT "Cancel"
78 enum status_messages {
79 STATUS = 128,
80 OK,
81 CANCEL
85 TStatusWindow::TStatusWindow(BRect rect, BMessenger target, const char* status)
86 : BWindow(rect, "", B_MODAL_WINDOW, B_NOT_RESIZABLE),
87 fTarget(target)
89 BView* view = new BView(Bounds(), "", B_FOLLOW_ALL, B_WILL_DRAW);
90 view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
91 AddChild(view);
93 BRect r(STATUS_FIELD_H, STATUS_FIELD_V,
94 STATUS_FIELD_WIDTH, STATUS_FIELD_V + STATUS_FIELD_HEIGHT);
96 fStatus = new BTextControl(r, "", STATUS_TEXT, status, new BMessage(STATUS));
97 view->AddChild(fStatus);
99 fStatus->SetDivider(fStatus->StringWidth(STATUS_TEXT) + 6);
100 fStatus->BTextControl::MakeFocus(true);
102 r.Set(S_OK_BUTTON_X1, S_OK_BUTTON_Y1, S_OK_BUTTON_X2, S_OK_BUTTON_Y2);
103 BButton *button = new BButton(r, "", S_OK_BUTTON_TEXT, new BMessage(OK));
104 view->AddChild(button);
105 button->MakeDefault(true);
107 r.Set(S_CANCEL_BUTTON_X1, S_CANCEL_BUTTON_Y1, S_CANCEL_BUTTON_X2, S_CANCEL_BUTTON_Y2);
108 button = new BButton(r, "", S_CANCEL_BUTTON_TEXT, new BMessage(CANCEL));
109 view->AddChild(button);
111 Show();
115 TStatusWindow::~TStatusWindow()
120 void
121 TStatusWindow::MessageReceived(BMessage* msg)
123 switch (msg->what) {
124 case STATUS:
125 break;
127 case OK:
129 if (!_Exists(fStatus->Text())) {
130 int32 index = 0;
131 uint32 loop;
132 status_t result;
133 BDirectory dir;
134 BEntry entry;
135 BFile file;
136 BNodeInfo* node;
137 BPath path;
139 find_directory(B_USER_SETTINGS_DIRECTORY, &path, true);
140 dir.SetTo(path.Path());
141 if (dir.FindEntry("Mail", &entry) == B_NO_ERROR)
142 dir.SetTo(&entry);
143 else
144 dir.CreateDirectory("Mail", &dir);
145 if (dir.InitCheck() != B_NO_ERROR)
146 goto err_exit;
147 if (dir.FindEntry("status", &entry) == B_NO_ERROR)
148 dir.SetTo(&entry);
149 else
150 dir.CreateDirectory("status", &dir);
151 if (dir.InitCheck() == B_NO_ERROR) {
152 char name[B_FILE_NAME_LENGTH];
153 char newName[B_FILE_NAME_LENGTH];
155 sprintf(name, "%s", fStatus->Text());
156 if (strlen(name) > B_FILE_NAME_LENGTH - 10)
157 name[B_FILE_NAME_LENGTH - 10] = 0;
158 for (loop = 0; loop < strlen(name); loop++) {
159 if (name[loop] == '/')
160 name[loop] = '\\';
162 strcpy(newName, name);
163 while (1) {
164 if ((result = dir.CreateFile(newName, &file, true)) == B_NO_ERROR)
165 break;
166 if (result != EEXIST)
167 goto err_exit;
168 sprintf(newName, "%s_%" B_PRId32, name, index++);
170 dir.FindEntry(newName, &entry);
171 node = new BNodeInfo(&file);
172 node->SetType("text/plain");
173 delete node;
174 file.Write(fStatus->Text(), strlen(fStatus->Text()) + 1);
175 file.SetSize(file.Position());
176 file.WriteAttr(INDEX_STATUS, B_STRING_TYPE, 0, fStatus->Text(),
177 strlen(fStatus->Text()) + 1);
180 err_exit:
182 BMessage close(M_CLOSE_CUSTOM);
183 close.AddString("status", fStatus->Text());
184 fTarget.SendMessage(&close);
185 // will fall through
188 case CANCEL:
189 Quit();
190 break;
195 bool
196 TStatusWindow::_Exists(const char* status)
198 BVolume volume;
199 BVolumeRoster().GetBootVolume(&volume);
201 BQuery query;
202 query.SetVolume(&volume);
203 query.PushAttr(INDEX_STATUS);
204 query.PushString(status);
205 query.PushOp(B_EQ);
206 query.Fetch();
208 BEntry entry;
209 if (query.GetNextEntry(&entry) == B_NO_ERROR)
210 return true;
212 return false;