Assorted whitespace cleanup and typo fixes.
[haiku.git] / src / kits / tracker / TemplatesMenu.cpp
blob45011959630d89efd82c43b4e55190960a3b6628
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
36 #include <Application.h>
37 #include <Catalog.h>
38 #include <FindDirectory.h>
39 #include <Directory.h>
40 #include <NodeInfo.h>
41 #include <Locale.h>
42 #include <Mime.h>
43 #include <Message.h>
44 #include <Path.h>
45 #include <Query.h>
46 #include <Roster.h>
47 #include <MenuItem.h>
48 #include <stdio.h>
50 #include "Commands.h"
52 #include "TemplatesMenu.h"
53 #include "IconMenuItem.h"
54 #include "MimeTypes.h"
57 #undef B_TRANSLATION_CONTEXT
58 #define B_TRANSLATION_CONTEXT "TemplatesMenu"
61 namespace BPrivate {
63 const char* kTemplatesDirectory = "Tracker/Tracker New Templates";
65 } // namespace BPrivate
68 // #pragma mark - TemplatesMenu
71 TemplatesMenu::TemplatesMenu(const BMessenger &target, const char* label)
73 BMenu(label),
74 fTarget(target),
75 fOpenItem(NULL)
80 TemplatesMenu::~TemplatesMenu()
85 void
86 TemplatesMenu::AttachedToWindow()
88 BuildMenu();
89 BMenu::AttachedToWindow();
90 SetTargetForItems(fTarget);
94 status_t
95 TemplatesMenu::SetTargetForItems(BHandler* target)
97 status_t result = BMenu::SetTargetForItems(target);
98 if (fOpenItem)
99 fOpenItem->SetTarget(be_app_messenger);
101 return result;
105 status_t
106 TemplatesMenu::SetTargetForItems(BMessenger messenger)
108 status_t result = BMenu::SetTargetForItems(messenger);
109 if (fOpenItem)
110 fOpenItem->SetTarget(be_app_messenger);
112 return result;
116 void
117 TemplatesMenu::UpdateMenuState()
119 BuildMenu(false);
123 bool
124 TemplatesMenu::BuildMenu(bool addItems)
126 // clear everything...
127 fOpenItem = NULL;
128 int32 count = CountItems();
129 while (count--)
130 delete RemoveItem((int32)0);
132 // add the folder
133 IconMenuItem* menuItem = new IconMenuItem(B_TRANSLATE("New folder"),
134 new BMessage(kNewFolder), B_DIR_MIMETYPE, B_MINI_ICON);
135 AddItem(menuItem);
136 menuItem->SetShortcut('N', 0);
138 // the templates folder
139 BPath path;
140 find_directory (B_USER_SETTINGS_DIRECTORY, &path, true);
141 path.Append(kTemplatesDirectory);
142 mkdir(path.Path(), 0777);
144 count = 0;
146 BEntry entry;
147 BDirectory templatesDir(path.Path());
148 while (templatesDir.GetNextEntry(&entry) == B_OK) {
149 BNode node(&entry);
150 BNodeInfo nodeInfo(&node);
151 char fileName[B_FILE_NAME_LENGTH];
152 entry.GetName(fileName);
153 if (nodeInfo.InitCheck() == B_OK) {
154 char mimeType[B_MIME_TYPE_LENGTH];
155 nodeInfo.GetType(mimeType);
157 BMimeType mime(mimeType);
158 if (mime.IsValid()) {
159 if (count == 0)
160 AddSeparatorItem();
162 count++;
164 // If not adding items, we are just seeing if there
165 // are any to list. So if we find one, immediately
166 // bail and return the result.
167 if (!addItems)
168 break;
170 entry_ref ref;
171 entry.GetRef(&ref);
173 BMessage* message = new BMessage(kNewEntryFromTemplate);
174 message->AddRef("refs_template", &ref);
175 message->AddString("name", fileName);
176 AddItem(new IconMenuItem(fileName, message, &nodeInfo,
177 B_MINI_ICON));
182 AddSeparatorItem();
184 // this is the message sent to open the templates folder
185 BMessage* message = new BMessage(B_REFS_RECEIVED);
186 entry_ref dirRef;
187 if (templatesDir.GetEntry(&entry) == B_OK)
188 entry.GetRef(&dirRef);
190 message->AddRef("refs", &dirRef);
192 // add item to show templates folder
193 fOpenItem = new BMenuItem(B_TRANSLATE("Edit templates" B_UTF8_ELLIPSIS),
194 message);
195 AddItem(fOpenItem);
196 if (dirRef == entry_ref())
197 fOpenItem->SetEnabled(false);
199 return count > 0;