xref: /haiku/src/kits/tracker/TemplatesMenu.cpp (revision fce97ba360ff70fb19f9cd2a57a16152f0925c06)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, 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 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 #include <Application.h>
36 #include <Catalog.h>
37 #include <FindDirectory.h>
38 #include <Directory.h>
39 #include <NodeInfo.h>
40 #include <Locale.h>
41 #include <Mime.h>
42 #include <Message.h>
43 #include <Path.h>
44 #include <Query.h>
45 #include <Roster.h>
46 #include <MenuItem.h>
47 #include <stdio.h>
48 
49 #include "Commands.h"
50 
51 #include "TemplatesMenu.h"
52 #include "IconMenuItem.h"
53 #include "MimeTypes.h"
54 
55 
56 #undef B_TRANSLATION_CONTEXT
57 #define B_TRANSLATION_CONTEXT "TemplatesMenu"
58 
59 
60 namespace BPrivate {
61 
62 const char *kTemplatesDirectory = "Tracker/Tracker New Templates";
63 
64 
65 }
66 
67 TemplatesMenu::TemplatesMenu(const BMessenger &target, const char *label)
68 	:	BMenu(label),
69 		fTarget(target),
70 		fOpenItem(NULL)
71 {
72 }
73 
74 TemplatesMenu::~TemplatesMenu()
75 {
76 }
77 
78 void
79 TemplatesMenu::AttachedToWindow()
80 {
81 	BuildMenu();
82 	BMenu::AttachedToWindow();
83 	SetTargetForItems(fTarget);
84 }
85 
86 status_t
87 TemplatesMenu::SetTargetForItems(BHandler *target)
88 {
89 	status_t result = BMenu::SetTargetForItems(target);
90 	if (fOpenItem)
91 		fOpenItem->SetTarget(be_app_messenger);
92 	return result;
93 }
94 
95 status_t
96 TemplatesMenu::SetTargetForItems(BMessenger messenger)
97 {
98 	status_t result = BMenu::SetTargetForItems(messenger);
99 	if (fOpenItem)
100 		fOpenItem->SetTarget(be_app_messenger);
101 	return result;
102 }
103 
104 void
105 TemplatesMenu::UpdateMenuState()
106 {
107 	BuildMenu(false);
108 }
109 
110 bool
111 TemplatesMenu::BuildMenu(bool addItems)
112 {
113 	// Clear everything...
114 	fOpenItem = NULL;
115 	int32 count = CountItems();
116 	while (count--)
117 		delete RemoveItem(0L);
118 
119 	// Add the Folder
120 	IconMenuItem* menuItem = new IconMenuItem(B_TRANSLATE("New folder"),
121 		new BMessage(kNewFolder), B_DIR_MIMETYPE, B_MINI_ICON);
122 	AddItem(menuItem);
123 	menuItem->SetShortcut('N', 0);
124 
125 	// The Templates folder
126 	BPath path;
127 	find_directory (B_USER_SETTINGS_DIRECTORY, &path, true);
128 	path.Append(kTemplatesDirectory);
129 	mkdir(path.Path(), 0777);
130 
131 	count = 0;
132 
133 	BEntry entry;
134 	BDirectory templatesDir(path.Path());
135 	while (templatesDir.GetNextEntry(&entry) == B_OK) {
136 		BNode node(&entry);
137 		BNodeInfo nodeInfo(&node);
138 		char fileName[B_FILE_NAME_LENGTH];
139 		entry.GetName(fileName);
140 		if (nodeInfo.InitCheck() == B_OK) {
141 			char mimeType[B_MIME_TYPE_LENGTH];
142 			nodeInfo.GetType(mimeType);
143 
144 			BMimeType mime(mimeType);
145 			if (mime.IsValid()) {
146 
147 				if (count == 0)
148 					AddSeparatorItem();
149 
150 				count++;
151 
152 				// If not adding items, we are just seeing if there
153 				// are any to list.  So if we find one, immediately
154 				// bail and return the result.
155 				if (!addItems)
156 					break;
157 
158 				entry_ref ref;
159 				entry.GetRef(&ref);
160 
161 				BMessage *message = new BMessage(kNewEntryFromTemplate);
162 				message->AddRef("refs_template", &ref);
163 				message->AddString("name", fileName);
164 				AddItem(new IconMenuItem(fileName, message, &nodeInfo, B_MINI_ICON));
165 			}
166 
167 		}
168 	}
169 
170 	AddSeparatorItem();
171 
172 	// This is the message sent to open the templates folder.
173 	BMessage *message = new BMessage(B_REFS_RECEIVED);
174 	entry_ref dirRef;
175 	if (templatesDir.GetEntry(&entry) == B_OK)
176 		entry.GetRef(&dirRef);
177 	message->AddRef("refs", &dirRef);
178 
179 	// Add item to show templates folder.
180 	fOpenItem =	new BMenuItem(B_TRANSLATE("Edit templates" B_UTF8_ELLIPSIS),
181 			message);
182 	AddItem(fOpenItem);
183 	if (dirRef == entry_ref())
184 		fOpenItem->SetEnabled(false);
185 
186 	return count > 0;
187 }
188