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