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