xref: /haiku/src/apps/mail/Status.cpp (revision 1f52c921e27aa442370e1bd4adc021acf2b78b64)
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 #include "Status.h"
35 
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>
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 
52 #include "MailApp.h"
53 #include "MailWindow.h"
54 #include "Messages.h"
55 
56 
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
62 
63 #define BUTTON_WIDTH		 70
64 #define BUTTON_HEIGHT		 20
65 
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"
71 
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"
77 
78 enum status_messages {
79 	STATUS = 128,
80 	OK,
81 	CANCEL
82 };
83 
84 
85 TStatusWindow::TStatusWindow(BRect rect, BMessenger target, const char* status)
86 	: BWindow(rect, "", B_MODAL_WINDOW, B_NOT_RESIZABLE),
87 	fTarget(target)
88 {
89 	BView* view = new BView(Bounds(), "", B_FOLLOW_ALL, B_WILL_DRAW);
90 	view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
91 	AddChild(view);
92 
93 	BRect r(STATUS_FIELD_H, STATUS_FIELD_V,
94 		STATUS_FIELD_WIDTH, STATUS_FIELD_V + STATUS_FIELD_HEIGHT);
95 
96 	fStatus = new BTextControl(r, "", STATUS_TEXT, status, new BMessage(STATUS));
97 	view->AddChild(fStatus);
98 
99 	fStatus->SetDivider(fStatus->StringWidth(STATUS_TEXT) + 6);
100 	fStatus->BTextControl::MakeFocus(true);
101 
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);
106 
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);
110 
111 	Show();
112 }
113 
114 
115 TStatusWindow::~TStatusWindow()
116 {
117 }
118 
119 
120 void
121 TStatusWindow::MessageReceived(BMessage* msg)
122 {
123 	switch (msg->what) {
124 		case STATUS:
125 			break;
126 
127 		case OK:
128 		{
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;
138 
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];
154 
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] = '\\';
161 					}
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 						snprintf(newName, B_FILE_NAME_LENGTH, "%s_%" B_PRId32,
169 							name, index++);
170 					}
171 					dir.FindEntry(newName, &entry);
172 					node = new BNodeInfo(&file);
173 					node->SetType("text/plain");
174 					delete node;
175 					file.Write(fStatus->Text(), strlen(fStatus->Text()) + 1);
176 					file.SetSize(file.Position());
177 					file.WriteAttr(INDEX_STATUS, B_STRING_TYPE, 0, fStatus->Text(),
178 						strlen(fStatus->Text()) + 1);
179 				}
180 			}
181 		err_exit:
182 			{
183 				BMessage close(M_CLOSE_CUSTOM);
184 				close.AddString("status", fStatus->Text());
185 				fTarget.SendMessage(&close);
186 				// will fall through
187 			}
188 		}
189 		case CANCEL:
190 			Quit();
191 			break;
192 	}
193 }
194 
195 
196 bool
197 TStatusWindow::_Exists(const char* status)
198 {
199 	BVolume volume;
200 	BVolumeRoster().GetBootVolume(&volume);
201 
202 	BQuery query;
203 	query.SetVolume(&volume);
204 	query.PushAttr(INDEX_STATUS);
205 	query.PushString(status);
206 	query.PushOp(B_EQ);
207 	query.Fetch();
208 
209 	BEntry entry;
210 	if (query.GetNextEntry(&entry) == B_NO_ERROR)
211 		return true;
212 
213 	return false;
214 }
215 
216