xref: /haiku/src/apps/mail/Status.cpp (revision 6dcd0ccf238263a3e5eb2e2a44e2ed0da1617a42)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2001, Be Incorporated. All rights reserved.
7 
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:
14 
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.
17 
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.
24 
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.
28 
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.
33 */
34 
35 
36 #include "Mail.h"
37 #include "Status.h"
38 
39 #include <Button.h>
40 #include <Directory.h>
41 #include <FindDirectory.h>
42 #include <fs_index.h>
43 #include <Node.h>
44 #include <NodeInfo.h>
45 #include <Path.h>
46 #include <Query.h>
47 #include <TextControl.h>
48 #include <Volume.h>
49 #include <VolumeRoster.h>
50 
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 
55 
56 #define STATUS_TEXT			"Status:"
57 #define STATUS_FIELD_H		 10
58 #define STATUS_FIELD_V		  8
59 #define STATUS_FIELD_WIDTH	(STATUS_WIDTH - STATUS_FIELD_H)
60 #define STATUS_FIELD_HEIGHT	 16
61 
62 #define BUTTON_WIDTH		 70
63 #define BUTTON_HEIGHT		 20
64 
65 #define S_OK_BUTTON_X1		(STATUS_WIDTH - BUTTON_WIDTH - 6)
66 #define S_OK_BUTTON_Y1		(STATUS_HEIGHT - (BUTTON_HEIGHT + 10))
67 #define S_OK_BUTTON_X2		(S_OK_BUTTON_X1 + BUTTON_WIDTH)
68 #define S_OK_BUTTON_Y2		(S_OK_BUTTON_Y1 + BUTTON_HEIGHT)
69 #define S_OK_BUTTON_TEXT	"OK"
70 
71 #define S_CANCEL_BUTTON_X1	(S_OK_BUTTON_X1 - (BUTTON_WIDTH + 10))
72 #define S_CANCEL_BUTTON_Y1	S_OK_BUTTON_Y1
73 #define S_CANCEL_BUTTON_X2	(S_CANCEL_BUTTON_X1 + BUTTON_WIDTH)
74 #define S_CANCEL_BUTTON_Y2	S_OK_BUTTON_Y2
75 #define S_CANCEL_BUTTON_TEXT	"Cancel"
76 
77 enum status_messages {
78 	STATUS = 128,
79 	OK,
80 	CANCEL
81 };
82 
83 
84 TStatusWindow::TStatusWindow(BRect rect, BMessenger target, const char* status)
85 	: BWindow(rect, "", B_MODAL_WINDOW, B_NOT_RESIZABLE),
86 	fTarget(target)
87 {
88 	BView* view = new BView(Bounds(), "", B_FOLLOW_ALL, B_WILL_DRAW);
89 	view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
90 	AddChild(view);
91 
92 	BRect r(STATUS_FIELD_H, STATUS_FIELD_V,
93 		STATUS_FIELD_WIDTH, STATUS_FIELD_V + STATUS_FIELD_HEIGHT);
94 
95 	fStatus = new BTextControl(r, "", STATUS_TEXT, status, new BMessage(STATUS));
96 	view->AddChild(fStatus);
97 
98 	fStatus->SetDivider(fStatus->StringWidth(STATUS_TEXT) + 6);
99 	fStatus->BTextControl::MakeFocus(true);
100 
101 	r.Set(S_OK_BUTTON_X1, S_OK_BUTTON_Y1, S_OK_BUTTON_X2, S_OK_BUTTON_Y2);
102 	BButton *button = new BButton(r, "", S_OK_BUTTON_TEXT, new BMessage(OK));
103 	view->AddChild(button);
104 	button->MakeDefault(true);
105 
106 	r.Set(S_CANCEL_BUTTON_X1, S_CANCEL_BUTTON_Y1, S_CANCEL_BUTTON_X2, S_CANCEL_BUTTON_Y2);
107 	button = new BButton(r, "", S_CANCEL_BUTTON_TEXT, new BMessage(CANCEL));
108 	view->AddChild(button);
109 
110 	Show();
111 }
112 
113 
114 TStatusWindow::~TStatusWindow()
115 {
116 }
117 
118 
119 void
120 TStatusWindow::MessageReceived(BMessage* msg)
121 {
122 	switch (msg->what) {
123 		case STATUS:
124 			break;
125 
126 		case OK:
127 		{
128 			if (!_Exists(fStatus->Text())) {
129 				int32 index = 0;
130 				uint32 loop;
131 				status_t result;
132 				BDirectory dir;
133 				BEntry entry;
134 				BFile file;
135 				BNodeInfo* node;
136 				BPath path;
137 
138 				find_directory(B_USER_SETTINGS_DIRECTORY, &path, true);
139 				dir.SetTo(path.Path());
140 				if (dir.FindEntry("bemail", &entry) == B_NO_ERROR)
141 					dir.SetTo(&entry);
142 				else
143 					dir.CreateDirectory("bemail", &dir);
144 				if (dir.InitCheck() != B_NO_ERROR)
145 					goto err_exit;
146 				if (dir.FindEntry("status", &entry) == B_NO_ERROR)
147 					dir.SetTo(&entry);
148 				else
149 					dir.CreateDirectory("status", &dir);
150 				if (dir.InitCheck() == B_NO_ERROR) {
151 					char name[B_FILE_NAME_LENGTH];
152 					char newName[B_FILE_NAME_LENGTH];
153 
154 					sprintf(name, "%s", fStatus->Text());
155 					if (strlen(name) > B_FILE_NAME_LENGTH - 10)
156 						name[B_FILE_NAME_LENGTH - 10] = 0;
157 					for (loop = 0; loop < strlen(name); loop++) {
158 						if (name[loop] == '/')
159 							name[loop] = '\\';
160 					}
161 					strcpy(newName, name);
162 					while (1) {
163 						if ((result = dir.CreateFile(newName, &file, true)) == B_NO_ERROR)
164 							break;
165 						if (result != EEXIST)
166 							goto err_exit;
167 						sprintf(newName, "%s_%ld", name, index++);
168 					}
169 					dir.FindEntry(newName, &entry);
170 					node = new BNodeInfo(&file);
171 					node->SetType("text/plain");
172 					delete node;
173 					file.Write(fStatus->Text(), strlen(fStatus->Text()) + 1);
174 					file.SetSize(file.Position());
175 					file.WriteAttr(INDEX_STATUS, B_STRING_TYPE, 0, fStatus->Text(),
176 						strlen(fStatus->Text()) + 1);
177 				}
178 			}
179 		err_exit:
180 			{
181 				BMessage close(M_CLOSE_CUSTOM);
182 				close.AddString("status", fStatus->Text());
183 				fTarget.SendMessage(&close);
184 				// will fall through
185 			}
186 		}
187 		case CANCEL:
188 			Quit();
189 			break;
190 	}
191 }
192 
193 
194 bool
195 TStatusWindow::_Exists(const char* status)
196 {
197 	BVolume volume;
198 	BVolumeRoster().GetBootVolume(&volume);
199 
200 	BQuery query;
201 	query.SetVolume(&volume);
202 	query.PushAttr(INDEX_STATUS);
203 	query.PushString(status);
204 	query.PushOp(B_EQ);
205 	query.Fetch();
206 
207 	BEntry entry;
208 	if (query.GetNextEntry(&entry) == B_NO_ERROR)
209 		return true;
210 
211 	return false;
212 }
213 
214