1/* 2Open Tracker License 3 4Terms and Conditions 5 6Copyright (c) 1991-2000, Be Incorporated. All rights reserved. 7 8Permission is hereby granted, free of charge, to any person obtaining a copy of 9this software and associated documentation files (the "Software"), to deal in 10the Software without restriction, including without limitation the rights to 11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 12of the Software, and to permit persons to whom the Software is furnished to do 13so, subject to the following conditions: 14 15The above copyright notice and this permission notice applies to all licensees 16and shall be included in all copies or substantial portions of the Software. 17 18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY, 20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION 23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 25Except as contained in this notice, the name of Be Incorporated shall not be 26used in advertising or otherwise to promote the sale, use or other dealings in 27this Software without prior written authorization from Be Incorporated. 28 29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks 30of Be Incorporated in the United States and other countries. Other brand product 31names are registered trademarks or trademarks of their respective holders. 32All rights reserved. 33*/ 34 35 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> 49 50#include "Commands.h" 51 52#include "TemplatesMenu.h" 53#include "IconMenuItem.h" 54#include "MimeTypes.h" 55 56 57#undef B_TRANSLATION_CONTEXT 58#define B_TRANSLATION_CONTEXT "TemplatesMenu" 59 60 61namespace BPrivate { 62 63const char* kTemplatesDirectory = "Tracker/Tracker New Templates"; 64 65} // namespace BPrivate 66 67 68// #pragma mark - TemplatesMenu 69 70 71TemplatesMenu::TemplatesMenu(const BMessenger &target, const char* label) 72 : 73 BMenu(label), 74 fTarget(target), 75 fOpenItem(NULL) 76{ 77} 78 79 80TemplatesMenu::~TemplatesMenu() 81{ 82} 83 84 85void 86TemplatesMenu::AttachedToWindow() 87{ 88 BuildMenu(); 89 BMenu::AttachedToWindow(); 90 SetTargetForItems(fTarget); 91} 92 93 94status_t 95TemplatesMenu::SetTargetForItems(BHandler* target) 96{ 97 status_t result = BMenu::SetTargetForItems(target); 98 if (fOpenItem) 99 fOpenItem->SetTarget(be_app_messenger); 100 101 return result; 102} 103 104 105status_t 106TemplatesMenu::SetTargetForItems(BMessenger messenger) 107{ 108 status_t result = BMenu::SetTargetForItems(messenger); 109 if (fOpenItem) 110 fOpenItem->SetTarget(be_app_messenger); 111 112 return result; 113} 114 115 116void 117TemplatesMenu::UpdateMenuState() 118{ 119 BuildMenu(false); 120} 121 122 123bool 124TemplatesMenu::BuildMenu(bool addItems) 125{ 126 // clear everything... 127 fOpenItem = NULL; 128 int32 count = CountItems(); 129 while (count--) 130 delete RemoveItem((int32)0); 131 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); 137 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); 143 144 count = 0; 145 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); 156 157 BMimeType mime(mimeType); 158 if (mime.IsValid()) { 159 if (count == 0) 160 AddSeparatorItem(); 161 162 count++; 163 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; 169 170 entry_ref ref; 171 entry.GetRef(&ref); 172 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)); 178 } 179 } 180 } 181 182 AddSeparatorItem(); 183 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); 189 190 message->AddRef("refs", &dirRef); 191 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); 198 199 return count > 0; 200} 201