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