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