xref: /haiku/src/kits/tracker/TrashWatcher.cpp (revision f8da8f3477d3c18142e59d17d05a545982faa5a8)
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 		} // fall thru
113 		case B_DEVICE_UNMOUNTED: // fall thru
114 		case B_ENTRY_REMOVED:
115 		{
116 			bool full = CheckTrashDirs();
117 			if (fTrashFull != full) {
118 				fTrashFull = full;
119 				UpdateTrashIcons();
120 			}
121 			break;
122 		}
123 		// We should handle DEVICE_UNMOUNTED here too to remove trash
124 
125 		case B_DEVICE_MOUNTED:
126 		{
127 			dev_t device;
128 			BDirectory trashDir;
129 			if (message->FindInt32("new device", &device) == B_OK
130 				&& FSGetTrashDir(&trashDir, device) == B_OK) {
131 				node_ref trashNode;
132 				trashDir.GetNodeRef(&trashNode);
133 				TTracker::WatchNode(&trashNode, B_WATCH_DIRECTORY, this);
134 				fTrashNodeList.AddItem(new node_ref(trashNode));
135 
136 				// Check if the new volume has anything trashed.
137 				if (CheckTrashDirs() && !fTrashFull) {
138 					fTrashFull = true;
139 					UpdateTrashIcons();
140 				}
141 			}
142 			break;
143 		}
144 	}
145 }
146 
147 
148 void
149 BTrashWatcher::UpdateTrashIcons()
150 {
151 	BVolumeRoster roster;
152 	BVolume volume;
153 	roster.Rewind();
154 
155 	BDirectory trashDir;
156 	while (roster.GetNextVolume(&volume) == B_OK) {
157 		if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) {
158 			// pull out the icons for the current trash state from resources
159 			// and apply them onto the trash directory node
160 			size_t largeSize = 0;
161 			size_t smallSize = 0;
162 			const void* largeData
163 				= GetTrackerResources()->LoadResource('ICON',
164 					fTrashFull ? R_TrashFullIcon : R_TrashIcon, &largeSize);
165 
166 			const void* smallData
167 				= GetTrackerResources()->LoadResource('MICN',
168 					fTrashFull ? R_TrashFullIcon : R_TrashIcon,  &smallSize);
169 
170 #ifdef HAIKU_TARGET_PLATFORM_HAIKU
171 			size_t vectorSize = 0;
172 			const void* vectorData = GetTrackerResources()->LoadResource(
173 				B_VECTOR_ICON_TYPE,
174 				fTrashFull ? R_TrashFullIcon : R_TrashIcon, &vectorSize);
175 
176 			if (vectorData) {
177 				trashDir.WriteAttr(kAttrIcon, B_VECTOR_ICON_TYPE, 0,
178 					vectorData, vectorSize);
179 			} else
180 				TRESPASS();
181 #endif
182 
183 			if (largeData) {
184 				trashDir.WriteAttr(kAttrLargeIcon, 'ICON', 0,
185 					largeData, largeSize);
186 			} else
187 				TRESPASS();
188 
189 			if (smallData) {
190 				trashDir.WriteAttr(kAttrMiniIcon, 'MICN', 0,
191 					smallData, smallSize);
192 			} else
193 				TRESPASS();
194 		}
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