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