6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
36 #include "TrashWatcher.h"
41 #include <Directory.h>
42 #include <NodeMonitor.h>
45 #include <VolumeRoster.h>
47 #include "Attributes.h"
53 // #pragma mark - BTrashWatcher
56 BTrashWatcher::BTrashWatcher()
58 BLooper("TrashWatcher", B_LOW_PRIORITY
),
59 fTrashNodeList(20, true)
63 fTrashFull
= CheckTrashDirs();
67 TTracker::WatchNode(0, B_WATCH_MOUNT
, this);
71 BTrashWatcher::~BTrashWatcher()
78 BTrashWatcher::IsTrashNode(const node_ref
* testNode
) const
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
)
92 BTrashWatcher::MessageReceived(BMessage
* message
)
94 if (message
->what
!= B_NODE_MONITOR
) {
95 _inherited::MessageReceived(message
);
99 switch (message
->FindInt32("opcode")) {
100 case B_ENTRY_CREATED
:
109 // allow code to fall through if move is from/to trash
110 // but do nothing for moves in the same directory
113 message
->FindInt64("from directory", &fromDir
);
114 message
->FindInt64("to directory", &toDir
);
115 if (fromDir
== toDir
)
119 case B_DEVICE_UNMOUNTED
:
120 case B_ENTRY_REMOVED
:
122 bool full
= CheckTrashDirs();
123 if (fTrashFull
!= full
) {
129 // We should handle DEVICE_UNMOUNTED here too to remove trash
131 case B_DEVICE_MOUNTED
:
135 if (message
->FindInt32("new device", &device
) == B_OK
136 && FSGetTrashDir(&trashDir
, device
) == B_OK
) {
138 trashDir
.GetNodeRef(&trashNode
);
139 TTracker::WatchNode(&trashNode
, B_WATCH_DIRECTORY
, this);
140 fTrashNodeList
.AddItem(new node_ref(trashNode
));
142 // Check if the new volume has anything trashed.
143 if (CheckTrashDirs() && !fTrashFull
) {
155 BTrashWatcher::UpdateTrashIcons()
157 BVolumeRoster roster
;
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 vectorSize
= 0;
167 const void* vectorData
= GetTrackerResources()->LoadResource(
169 fTrashFull
? R_TrashFullIcon
: R_TrashIcon
, &vectorSize
);
172 trashDir
.WriteAttr(kAttrIcon
, B_VECTOR_ICON_TYPE
, 0,
173 vectorData
, vectorSize
);
182 BTrashWatcher::WatchTrashDirs()
184 BVolumeRoster volRoster
;
187 while (volRoster
.GetNextVolume(&volume
) == B_OK
) {
188 if (volume
.IsReadOnly() || !volume
.IsPersistent())
192 if (FSGetTrashDir(&trashDir
, volume
.Device()) == B_OK
) {
194 trashDir
.GetNodeRef(&trash_node
);
195 watch_node(&trash_node
, B_WATCH_DIRECTORY
, this);
196 fTrashNodeList
.AddItem(new node_ref(trash_node
));
203 BTrashWatcher::CheckTrashDirs()
205 BVolumeRoster volRoster
;
208 while (volRoster
.GetNextVolume(&volume
) == B_OK
) {
209 if (volume
.IsReadOnly() || !volume
.IsPersistent())
213 FSGetTrashDir(&trashDir
, volume
.Device());
216 if (trashDir
.GetNextEntry(&entry
) == B_OK
)