xref: /haiku/src/apps/mail/Status.cpp (revision e1c4049fed1047bdb957b0529e1921e97ef94770)
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 <LayoutBuilder.h>
41 #include <Node.h>
42 #include <NodeInfo.h>
43 #include <Path.h>
44 #include <Query.h>
45 #include <TextControl.h>
46 #include <Volume.h>
47 #include <VolumeRoster.h>
48 
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "MailApp.h"
54 #include "MailWindow.h"
55 #include "Messages.h"
56 
57 
58 #undef B_TRANSLATION_CONTEXT
59 #define B_TRANSLATION_CONTEXT "Mail"
60 
61 
62 enum status_messages {
63 	STATUS = 128,
64 	OK,
65 	CANCEL
66 };
67 
68 
69 TStatusWindow::TStatusWindow(BRect frame, BMessenger target, const char* status)
70 	: BWindow(BRect(), "", B_MODAL_WINDOW, B_NOT_RESIZABLE | B_AUTO_UPDATE_SIZE_LIMITS),
71 	fTarget(target)
72 {
73 	fStatus = new BTextControl("status",  B_TRANSLATE("Status:"), status,
74 		new BMessage(STATUS));
75 
76 	BButton *ok = new BButton("ok", B_TRANSLATE("OK"), new BMessage(OK));
77 	ok->MakeDefault(true);
78 
79 	BButton *cancel = new BButton("cancel", B_TRANSLATE("Cancel"), new BMessage(CANCEL));
80 
81 	BLayoutBuilder::Group<>(this, B_VERTICAL, 0)
82 		.SetInsets(B_USE_DEFAULT_SPACING)
83 		.AddGroup(B_HORIZONTAL, 0)
84 			.Add(fStatus)
85 		.End()
86 		.AddStrut(B_USE_SMALL_SPACING)
87 		.AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING, 0)
88 			.AddStrut(B_USE_SMALL_SPACING)
89 			.Add(cancel)
90 			.Add(ok);
91 
92 	fStatus->BTextControl::MakeFocus(true);
93 	CenterIn(frame);
94 	Show();
95 }
96 
97 
98 TStatusWindow::~TStatusWindow()
99 {
100 }
101 
102 
103 void
104 TStatusWindow::MessageReceived(BMessage* msg)
105 {
106 	switch (msg->what) {
107 		case STATUS:
108 			break;
109 
110 		case OK:
111 		{
112 			if (!_Exists(fStatus->Text())) {
113 				int32 index = 0;
114 				uint32 loop;
115 				status_t result;
116 				BDirectory dir;
117 				BEntry entry;
118 				BFile file;
119 				BNodeInfo* node;
120 				BPath path;
121 
122 				find_directory(B_USER_SETTINGS_DIRECTORY, &path, true);
123 				dir.SetTo(path.Path());
124 				if (dir.FindEntry("Mail", &entry) == B_NO_ERROR)
125 					dir.SetTo(&entry);
126 				else
127 					dir.CreateDirectory("Mail", &dir);
128 				if (dir.InitCheck() != B_NO_ERROR)
129 					goto err_exit;
130 				if (dir.FindEntry("status", &entry) == B_NO_ERROR)
131 					dir.SetTo(&entry);
132 				else
133 					dir.CreateDirectory("status", &dir);
134 				if (dir.InitCheck() == B_NO_ERROR) {
135 					char name[B_FILE_NAME_LENGTH];
136 					char newName[B_FILE_NAME_LENGTH];
137 
138 					sprintf(name, "%s", fStatus->Text());
139 					if (strlen(name) > B_FILE_NAME_LENGTH - 10)
140 						name[B_FILE_NAME_LENGTH - 10] = 0;
141 					for (loop = 0; loop < strlen(name); loop++) {
142 						if (name[loop] == '/')
143 							name[loop] = '\\';
144 					}
145 					strcpy(newName, name);
146 					while (1) {
147 						if ((result = dir.CreateFile(newName, &file, true)) == B_NO_ERROR)
148 							break;
149 						if (result != EEXIST)
150 							goto err_exit;
151 						snprintf(newName, B_FILE_NAME_LENGTH, "%s_%" B_PRId32,
152 							name, index++);
153 					}
154 					dir.FindEntry(newName, &entry);
155 					node = new BNodeInfo(&file);
156 					node->SetType("text/plain");
157 					delete node;
158 					file.Write(fStatus->Text(), strlen(fStatus->Text()) + 1);
159 					file.SetSize(file.Position());
160 					file.WriteAttr(INDEX_STATUS, B_STRING_TYPE, 0, fStatus->Text(),
161 						strlen(fStatus->Text()) + 1);
162 				}
163 			}
164 		err_exit:
165 			{
166 				BMessage close(M_CLOSE_CUSTOM);
167 				close.AddString("status", fStatus->Text());
168 				fTarget.SendMessage(&close);
169 				// will fall through
170 			}
171 		}
172 		case CANCEL:
173 			Quit();
174 			break;
175 	}
176 }
177 
178 
179 bool
180 TStatusWindow::_Exists(const char* status)
181 {
182 	BVolume volume;
183 	BVolumeRoster().GetBootVolume(&volume);
184 
185 	BQuery query;
186 	query.SetVolume(&volume);
187 	query.PushAttr(INDEX_STATUS);
188 	query.PushString(status);
189 	query.PushOp(B_EQ);
190 	query.Fetch();
191 
192 	BEntry entry;
193 	if (query.GetNextEntry(&entry) == B_NO_ERROR)
194 		return true;
195 
196 	return false;
197 }
198