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 size_t vectorSize = 0; 177 const void* vectorData = GetTrackerResources()->LoadResource( 178 B_VECTOR_ICON_TYPE, 179 fTrashFull ? R_TrashFullIcon : R_TrashIcon, &vectorSize); 180 181 if (vectorData) { 182 trashDir.WriteAttr(kAttrIcon, B_VECTOR_ICON_TYPE, 0, 183 vectorData, vectorSize); 184 } else 185 TRESPASS(); 186 187 if (largeData) { 188 trashDir.WriteAttr(kAttrLargeIcon, 'ICON', 0, 189 largeData, largeSize); 190 } else 191 TRESPASS(); 192 193 if (smallData) { 194 trashDir.WriteAttr(kAttrMiniIcon, 'MICN', 0, 195 smallData, smallSize); 196 } else 197 TRESPASS(); 198 } 199 } 200 } 201 202 203 void 204 BTrashWatcher::WatchTrashDirs() 205 { 206 BVolumeRoster volRoster; 207 volRoster.Rewind(); 208 BVolume volume; 209 while (volRoster.GetNextVolume(&volume) == B_OK) { 210 if (volume.IsReadOnly() || !volume.IsPersistent()) 211 continue; 212 213 BDirectory trashDir; 214 if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) { 215 node_ref trash_node; 216 trashDir.GetNodeRef(&trash_node); 217 watch_node(&trash_node, B_WATCH_DIRECTORY, this); 218 fTrashNodeList.AddItem(new node_ref(trash_node)); 219 } 220 } 221 } 222 223 224 bool 225 BTrashWatcher::CheckTrashDirs() 226 { 227 BVolumeRoster volRoster; 228 volRoster.Rewind(); 229 BVolume volume; 230 while (volRoster.GetNextVolume(&volume) == B_OK) { 231 if (volume.IsReadOnly() || !volume.IsPersistent()) 232 continue; 233 234 BDirectory trashDir; 235 FSGetTrashDir(&trashDir, volume.Device()); 236 trashDir.Rewind(); 237 BEntry entry; 238 if (trashDir.GetNextEntry(&entry) == B_OK) 239 return true; 240 } 241 242 return false; 243 } 244