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 BVolume boot; 156 if (BVolumeRoster().GetBootVolume(&boot) != B_OK) 157 return; 158 159 BDirectory trashDir; 160 if (FSGetTrashDir(&trashDir, boot.Device()) == B_OK) { 161 // pull out the icons for the current trash state from resources and 162 // apply them onto the trash directory node 163 size_t largeSize = 0; 164 size_t smallSize = 0; 165 const void *largeData = GetTrackerResources()->LoadResource('ICON', 166 fTrashFull ? kResTrashFullIcon : kResTrashIcon, &largeSize); 167 168 const void *smallData = GetTrackerResources()->LoadResource('MICN', 169 fTrashFull ? kResTrashFullIcon : kResTrashIcon, &smallSize); 170 171 if (largeData) 172 trashDir.WriteAttr(kAttrLargeIcon, 'ICON', 0, 173 largeData, largeSize); 174 else 175 TRESPASS(); 176 177 if (smallData) 178 trashDir.WriteAttr(kAttrMiniIcon, 'MICN', 0, 179 smallData, smallSize); 180 else 181 TRESPASS(); 182 } 183 } 184 185 186 void 187 BTrashWatcher::WatchTrashDirs() 188 { 189 BVolumeRoster volRoster; 190 volRoster.Rewind(); 191 BVolume volume; 192 while (volRoster.GetNextVolume(&volume) == B_OK) { 193 if (volume.IsReadOnly() || !volume.IsPersistent()) 194 continue; 195 196 BDirectory trashDir; 197 if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) { 198 node_ref trash_node; 199 trashDir.GetNodeRef(&trash_node); 200 watch_node(&trash_node, B_WATCH_DIRECTORY, this); 201 fTrashNodeList.AddItem(new node_ref(trash_node)); 202 } 203 } 204 } 205 206 207 bool 208 BTrashWatcher::CheckTrashDirs() 209 { 210 BVolumeRoster volRoster; 211 volRoster.Rewind(); 212 BVolume volume; 213 while (volRoster.GetNextVolume(&volume) == B_OK) { 214 if (volume.IsReadOnly() || !volume.IsPersistent()) 215 continue; 216 217 BDirectory trashDir; 218 FSGetTrashDir(&trashDir, volume.Device()); 219 trashDir.Rewind(); 220 BEntry entry; 221 if (trashDir.GetNextEntry(&entry) == B_OK) 222 return true; 223 } 224 225 return false; 226 } 227