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