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 "TrashWatcher.h" 37 38 #include <string.h> 39 40 #include <Debug.h> 41 #include <Directory.h> 42 #include <NodeMonitor.h> 43 #include <Path.h> 44 #include <Volume.h> 45 #include <VolumeRoster.h> 46 47 #include "Attributes.h" 48 #include "Bitmaps.h" 49 #include "FSUtils.h" 50 #include "Tracker.h" 51 52 53 // #pragma mark - BTrashWatcher 54 55 56 BTrashWatcher::BTrashWatcher() 57 : 58 BLooper("TrashWatcher", B_LOW_PRIORITY), 59 fTrashNodeList(20, true) 60 { 61 FSCreateTrashDirs(); 62 WatchTrashDirs(); 63 fTrashFull = CheckTrashDirs(); 64 UpdateTrashIcons(); 65 66 // watch volumes 67 TTracker::WatchNode(0, B_WATCH_MOUNT, this); 68 } 69 70 71 BTrashWatcher::~BTrashWatcher() 72 { 73 stop_watching(this); 74 } 75 76 77 bool 78 BTrashWatcher::IsTrashNode(const node_ref* testNode) const 79 { 80 int32 count = fTrashNodeList.CountItems(); 81 for (int32 index = 0; index < count; index++) { 82 node_ref* nref = fTrashNodeList.ItemAt(index); 83 if (nref->node == testNode->node && nref->device == testNode->device) 84 return true; 85 } 86 87 return false; 88 } 89 90 91 void 92 BTrashWatcher::MessageReceived(BMessage* message) 93 { 94 if (message->what != B_NODE_MONITOR) { 95 _inherited::MessageReceived(message); 96 return; 97 } 98 99 switch (message->FindInt32("opcode")) { 100 case B_ENTRY_CREATED: 101 if (!fTrashFull) { 102 fTrashFull = true; 103 UpdateTrashIcons(); 104 } 105 break; 106 107 case B_ENTRY_MOVED: 108 { 109 // allow code to fall through if move is from/to trash 110 // but do nothing for moves in the same directory 111 ino_t toDir; 112 ino_t fromDir; 113 message->FindInt64("from directory", &fromDir); 114 message->FindInt64("to directory", &toDir); 115 if (fromDir == toDir) 116 break; 117 } 118 // fall-through 119 case B_DEVICE_UNMOUNTED: 120 case B_ENTRY_REMOVED: 121 { 122 bool full = CheckTrashDirs(); 123 if (fTrashFull != full) { 124 fTrashFull = full; 125 UpdateTrashIcons(); 126 } 127 break; 128 } 129 // We should handle DEVICE_UNMOUNTED here too to remove trash 130 131 case B_DEVICE_MOUNTED: 132 { 133 dev_t device; 134 BDirectory trashDir; 135 if (message->FindInt32("new device", &device) == B_OK 136 && FSGetTrashDir(&trashDir, device) == B_OK) { 137 node_ref trashNode; 138 trashDir.GetNodeRef(&trashNode); 139 TTracker::WatchNode(&trashNode, B_WATCH_DIRECTORY, this); 140 fTrashNodeList.AddItem(new node_ref(trashNode)); 141 142 // Check if the new volume has anything trashed. 143 if (CheckTrashDirs() && !fTrashFull) { 144 fTrashFull = true; 145 UpdateTrashIcons(); 146 } 147 } 148 break; 149 } 150 } 151 } 152 153 154 void 155 BTrashWatcher::UpdateTrashIcons() 156 { 157 BVolumeRoster roster; 158 BVolume volume; 159 roster.Rewind(); 160 161 BDirectory trashDir; 162 while (roster.GetNextVolume(&volume) == B_OK) { 163 if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) { 164 // pull out the icons for the current trash state from resources 165 // and apply them onto the trash directory node 166 size_t largeSize = 0; 167 size_t smallSize = 0; 168 const void* largeData 169 = GetTrackerResources()->LoadResource('ICON', 170 fTrashFull ? R_TrashFullIcon : R_TrashIcon, &largeSize); 171 172 const void* smallData 173 = GetTrackerResources()->LoadResource('MICN', 174 fTrashFull ? R_TrashFullIcon : R_TrashIcon, &smallSize); 175 176 #ifdef HAIKU_TARGET_PLATFORM_HAIKU 177 size_t vectorSize = 0; 178 const void* vectorData = GetTrackerResources()->LoadResource( 179 B_VECTOR_ICON_TYPE, 180 fTrashFull ? R_TrashFullIcon : R_TrashIcon, &vectorSize); 181 182 if (vectorData) { 183 trashDir.WriteAttr(kAttrIcon, B_VECTOR_ICON_TYPE, 0, 184 vectorData, vectorSize); 185 } else 186 TRESPASS(); 187 #endif 188 189 if (largeData) { 190 trashDir.WriteAttr(kAttrLargeIcon, 'ICON', 0, 191 largeData, largeSize); 192 } else 193 TRESPASS(); 194 195 if (smallData) { 196 trashDir.WriteAttr(kAttrMiniIcon, 'MICN', 0, 197 smallData, smallSize); 198 } else 199 TRESPASS(); 200 } 201 } 202 } 203 204 205 void 206 BTrashWatcher::WatchTrashDirs() 207 { 208 BVolumeRoster volRoster; 209 volRoster.Rewind(); 210 BVolume volume; 211 while (volRoster.GetNextVolume(&volume) == B_OK) { 212 if (volume.IsReadOnly() || !volume.IsPersistent()) 213 continue; 214 215 BDirectory trashDir; 216 if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) { 217 node_ref trash_node; 218 trashDir.GetNodeRef(&trash_node); 219 watch_node(&trash_node, B_WATCH_DIRECTORY, this); 220 fTrashNodeList.AddItem(new node_ref(trash_node)); 221 } 222 } 223 } 224 225 226 bool 227 BTrashWatcher::CheckTrashDirs() 228 { 229 BVolumeRoster volRoster; 230 volRoster.Rewind(); 231 BVolume volume; 232 while (volRoster.GetNextVolume(&volume) == B_OK) { 233 if (volume.IsReadOnly() || !volume.IsPersistent()) 234 continue; 235 236 BDirectory trashDir; 237 FSGetTrashDir(&trashDir, volume.Device()); 238 trashDir.Rewind(); 239 BEntry entry; 240 if (trashDir.GetNextEntry(&entry) == B_OK) 241 return true; 242 } 243 244 return false; 245 } 246