xref: /haiku/src/preferences/filetypes/NewFileTypeWindow.cpp (revision fce97ba360ff70fb19f9cd2a57a16152f0925c06)
1 /*
2  * Copyright 2006-2010, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include "FileTypes.h"
8 #include "FileTypesWindow.h"
9 #include "NewFileTypeWindow.h"
10 
11 #include <Button.h>
12 #include <Catalog.h>
13 #include <ControlLook.h>
14 #include <GroupLayout.h>
15 #include <GridLayoutBuilder.h>
16 #include <Locale.h>
17 #include <MenuBar.h>
18 #include <MenuField.h>
19 #include <MenuItem.h>
20 #include <Mime.h>
21 #include <PopUpMenu.h>
22 #include <SpaceLayoutItem.h>
23 #include <String.h>
24 #include <StringView.h>
25 #include <TextControl.h>
26 
27 #include <string.h>
28 
29 
30 #undef B_TRANSLATION_CONTEXT
31 #define B_TRANSLATION_CONTEXT "New File Type Window"
32 
33 
34 const uint32 kMsgSupertypeChosen = 'sptc';
35 const uint32 kMsgNewSupertypeChosen = 'nstc';
36 
37 const uint32 kMsgNameUpdated = 'nmup';
38 
39 const uint32 kMsgAddType = 'atyp';
40 
41 
42 NewFileTypeWindow::NewFileTypeWindow(FileTypesWindow* target,
43 	const char* currentType)
44 	:
45 	BWindow(BRect(100, 100, 350, 200), B_TRANSLATE("New file type"),
46 		B_MODAL_WINDOW, B_NOT_ZOOMABLE | B_NOT_V_RESIZABLE
47 			| B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS ),
48 	fTarget(target)
49 {
50 	fSupertypesMenu = new BPopUpMenu("supertypes");
51 	BMenuItem* item;
52 	BMessage types;
53 	if (BMimeType::GetInstalledSupertypes(&types) == B_OK) {
54 		const char* type;
55 		int32 i = 0;
56 		while (types.FindString("super_types", i++, &type) == B_OK) {
57 			fSupertypesMenu->AddItem(item = new BMenuItem(type,
58 				new BMessage(kMsgSupertypeChosen)));
59 
60 			// select super type close to the current type
61 			if (currentType != NULL) {
62 				if (!strncmp(type, currentType, strlen(type)))
63 					item->SetMarked(true);
64 			} else if (i == 1)
65 				item->SetMarked(true);
66 		}
67 
68 		if (i > 1)
69 			fSupertypesMenu->AddSeparatorItem();
70 	}
71 
72 	fSupertypesMenu->AddItem(new BMenuItem(B_TRANSLATE("Add new group"),
73 		new BMessage(kMsgNewSupertypeChosen)));
74 	BMenuField* typesMenuField = new BMenuField(NULL, fSupertypesMenu);
75 
76 	BStringView* typesMenuLabel = new BStringView(NULL, B_TRANSLATE("Group:"));
77 		// Create a separate label view, otherwise things don't line up right
78 	typesMenuLabel->SetAlignment(B_ALIGN_LEFT);
79 	typesMenuLabel->SetExplicitAlignment(
80 		BAlignment(B_ALIGN_LEFT, B_ALIGN_USE_FULL_HEIGHT));
81 
82 	fNameControl = new BTextControl(B_TRANSLATE("Internal name:"), "", NULL);
83 	fNameControl->SetModificationMessage(new BMessage(kMsgNameUpdated));
84 
85 	// filter out invalid characters that can't be part of a MIME type name
86 	BTextView* nameControlTextView = fNameControl->TextView();
87 	const char* disallowedCharacters = "/<>@,;:\"()[]?= ";
88 	for (int32 i = 0; disallowedCharacters[i]; i++) {
89 		nameControlTextView->DisallowChar(disallowedCharacters[i]);
90 	}
91 
92 	fAddButton = new BButton(B_TRANSLATE("Add type"),
93 		new BMessage(kMsgAddType));
94 
95 	float padding = be_control_look->DefaultItemSpacing();
96 
97 	SetLayout(new BGroupLayout(B_VERTICAL));
98 	AddChild(BGridLayoutBuilder(padding, padding)
99 		.SetInsets(padding, padding, padding, padding)
100 		.Add(typesMenuLabel, 0, 0)
101 		.Add(typesMenuField, 1, 0, 2)
102 		.Add(fNameControl->CreateLabelLayoutItem(), 0, 1)
103 		.Add(fNameControl->CreateTextViewLayoutItem(), 1, 1, 2)
104 		.Add(BSpaceLayoutItem::CreateGlue(), 0, 2)
105 		.Add(new BButton(B_TRANSLATE("Cancel"),
106 			new BMessage(B_QUIT_REQUESTED)), 1, 2)
107 		.Add(fAddButton, 2, 2)
108 		.SetColumnWeight(0, 3));
109 
110 	BAlignment fullSize = BAlignment(B_ALIGN_USE_FULL_WIDTH,
111 		B_ALIGN_USE_FULL_HEIGHT);
112 	typesMenuField->MenuBar()->SetExplicitAlignment(fullSize);
113 	fNameControl->TextView()->SetExplicitAlignment(fullSize);
114 
115 	BLayoutItem* nameControlLabelItem = fNameControl->CreateLabelLayoutItem();
116 	nameControlLabelItem->SetExplicitMinSize(nameControlLabelItem->MinSize());
117 		// stops fNameControl's label from truncating under certain conditions
118 
119 	fAddButton->MakeDefault(true);
120 	fNameControl->MakeFocus(true);
121 
122 	target->PlaceSubWindow(this);
123 }
124 
125 
126 NewFileTypeWindow::~NewFileTypeWindow()
127 {
128 }
129 
130 
131 void
132 NewFileTypeWindow::MessageReceived(BMessage* message)
133 {
134 	switch (message->what) {
135 		case kMsgSupertypeChosen:
136 			fAddButton->SetLabel(B_TRANSLATE("Add type"));
137 			fNameControl->SetLabel(B_TRANSLATE("Internal name:"));
138 			fNameControl->MakeFocus(true);
139 			InvalidateLayout(true);
140 			break;
141 
142 		case kMsgNewSupertypeChosen:
143 			fAddButton->SetLabel(B_TRANSLATE("Add group"));
144 			fNameControl->SetLabel(B_TRANSLATE("Group name:"));
145 			fNameControl->MakeFocus(true);
146 			InvalidateLayout(true);
147 			break;
148 
149 		case kMsgNameUpdated:
150 		{
151 			bool empty = fNameControl->Text() == NULL
152 				|| fNameControl->Text()[0] == '\0';
153 
154 			if (fAddButton->IsEnabled() == empty)
155 				fAddButton->SetEnabled(!empty);
156 			break;
157 		}
158 
159 		case kMsgAddType:
160 		{
161 			BMenuItem* item = fSupertypesMenu->FindMarked();
162 			if (item != NULL) {
163 				BString type;
164 				if (fSupertypesMenu->IndexOf(item)
165 						!= fSupertypesMenu->CountItems() - 1) {
166 					// add normal type
167 					type = item->Label();
168 					type.Append("/");
169 				}
170 
171 				type.Append(fNameControl->Text());
172 
173 				BMimeType mimeType(type.String());
174 				if (mimeType.IsInstalled()) {
175 					error_alert(B_TRANSLATE("This file type already exists"));
176 					break;
177 				}
178 
179 				status_t status = mimeType.Install();
180 				if (status != B_OK)
181 					error_alert(B_TRANSLATE("Could not install file type"),
182 						status);
183 				else {
184 					BMessage update(kMsgSelectNewType);
185 					update.AddString("type", type.String());
186 
187 					fTarget.SendMessage(&update);
188 				}
189 			}
190 			PostMessage(B_QUIT_REQUESTED);
191 			break;
192 		}
193 
194 		default:
195 			BWindow::MessageReceived(message);
196 			break;
197 	}
198 }
199 
200 
201 bool
202 NewFileTypeWindow::QuitRequested()
203 {
204 	fTarget.SendMessage(kMsgNewTypeWindowClosed);
205 	return true;
206 }
207 
208 
209