xref: /haiku/src/kits/tracker/TrashWatcher.cpp (revision 9ecf9d1c1d4888d341a6eac72112c72d1ae3a4cb)
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 		size_t vectorSize = 0;
166 		const void *largeData = GetTrackerResources()->LoadResource('ICON',
167 			fTrashFull ? kResTrashFullIcon : kResTrashIcon, &largeSize);
168 
169 		const void *smallData = GetTrackerResources()->LoadResource('MICN',
170 			fTrashFull ? kResTrashFullIcon : kResTrashIcon,  &smallSize);
171 
172 		const void *vectorData = GetTrackerResources()->LoadResource(
173 			B_RAW_TYPE, fTrashFull ? kResTrashFullIcon : kResTrashIcon,
174 			&vectorSize);
175 
176 		if (vectorData)
177 			trashDir.WriteAttr(kAttrIcon, B_RAW_TYPE, 0,
178 				vectorData, vectorSize);
179 // TODO: enable me once the vector icon is indeed in the resources
180 //		else
181 //			TRESPASS();
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 void
199 BTrashWatcher::WatchTrashDirs()
200 {
201 	BVolumeRoster volRoster;
202 	volRoster.Rewind();
203 	BVolume	volume;
204 	while (volRoster.GetNextVolume(&volume) == B_OK) {
205 		if (volume.IsReadOnly() || !volume.IsPersistent())
206 			continue;
207 
208 		BDirectory trashDir;
209 		if (FSGetTrashDir(&trashDir, volume.Device()) == B_OK) {
210 			node_ref trash_node;
211 			trashDir.GetNodeRef(&trash_node);
212 			watch_node(&trash_node, B_WATCH_DIRECTORY, this);
213 			fTrashNodeList.AddItem(new node_ref(trash_node));
214 		}
215 	}
216 }
217 
218 
219 bool
220 BTrashWatcher::CheckTrashDirs()
221 {
222 	BVolumeRoster volRoster;
223 	volRoster.Rewind();
224 	BVolume	volume;
225 	while (volRoster.GetNextVolume(&volume) == B_OK) {
226 		if (volume.IsReadOnly() || !volume.IsPersistent())
227 			continue;
228 
229 		BDirectory trashDir;
230 		FSGetTrashDir(&trashDir, volume.Device());
231 		trashDir.Rewind();
232 		BEntry entry;
233 		if (trashDir.GetNextEntry(&entry) == B_OK)
234 			return true;
235 	}
236 
237 	return false;
238 }
239