xref: /haiku/src/kits/tracker/TrackerSettings.cpp (revision da4dbfa47a47beb355289f3dd685797cee69ab77)
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 "TrackerSettings.h"
37 
38 #include <Debug.h>
39 
40 #include "Tracker.h"
41 #include "TrackerDefaults.h"
42 #include "WidgetAttributeText.h"
43 
44 
45 #define RGBTOHEX(c) ((c.alpha << 24) | (c.red << 16) | (c.green << 8) | (c.blue))
46 
47 
48 class TTrackerState : public Settings {
49 public:
50 	static TTrackerState* Get();
51 	void Release();
52 
53 	void LoadSettingsIfNeeded();
54 	void SaveSettings(bool onlyIfNonDefault = true);
55 
56 	TTrackerState();
57 	~TTrackerState();
58 
59 private:
60 	friend class BPrivate::TrackerSettings;
61 
62 	static void InitIfNeeded();
63 	TTrackerState(const TTrackerState&);
64 
65 	BooleanValueSetting* fShowDisksIcon;
66 	BooleanValueSetting* fMountVolumesOntoDesktop;
67 	BooleanValueSetting* fDesktopFilePanelRoot;
68 	BooleanValueSetting* fMountSharedVolumesOntoDesktop;
69 	BooleanValueSetting* fEjectWhenUnmounting;
70 
71 	BooleanValueSetting* fShowFullPathInTitleBar;
72 	BooleanValueSetting* fSingleWindowBrowse;
73 	BooleanValueSetting* fShowNavigator;
74 	BooleanValueSetting* fShowSelectionWhenInactive;
75 	BooleanValueSetting* fTransparentSelection;
76 	BooleanValueSetting* fSortFolderNamesFirst;
77 	BooleanValueSetting* fHideDotFiles;
78 	BooleanValueSetting* fTypeAheadFiltering;
79 	BooleanValueSetting* fGenerateImageThumbnails;
80 
81 	ScalarValueSetting* fRecentApplicationsCount;
82 	ScalarValueSetting* fRecentDocumentsCount;
83 	ScalarValueSetting* fRecentFoldersCount;
84 
85 	BooleanValueSetting* fShowVolumeSpaceBar;
86 	HexScalarValueSetting* fUsedSpaceColor;
87 	HexScalarValueSetting* fFreeSpaceColor;
88 	HexScalarValueSetting* fWarningSpaceColor;
89 
90 	BooleanValueSetting* fDontMoveFilesToTrash;
91 	BooleanValueSetting* fAskBeforeDeleteFile;
92 
93 	Benaphore fInitLock;
94 	bool fInited;
95 	bool fSettingsLoaded;
96 
97 	int32 fUseCounter;
98 
99 	typedef Settings _inherited;
100 };
101 
102 
103 static TTrackerState gTrackerState;
104 
105 
106 rgb_color ValueToColor(int32 value)
107 {
108 	rgb_color color;
109 	color.alpha = static_cast<uchar>((value >> 24L) & 0xff);
110 	color.red = static_cast<uchar>((value >> 16L) & 0xff);
111 	color.green = static_cast<uchar>((value >> 8L) & 0xff);
112 	color.blue = static_cast<uchar>(value & 0xff);
113 
114 	return color;
115 }
116 
117 
118 int32 ColorToValue(rgb_color color)
119 {
120 	return color.alpha << 24L | color.red << 16L | color.green << 8L
121 		| color.blue;
122 }
123 
124 
125 //	#pragma mark - TTrackerState
126 
127 
128 TTrackerState::TTrackerState()
129 	:
130 	Settings("TrackerSettings", "Tracker"),
131 	fShowDisksIcon(NULL),
132 	fMountVolumesOntoDesktop(NULL),
133 	fDesktopFilePanelRoot(NULL),
134 	fMountSharedVolumesOntoDesktop(NULL),
135 	fEjectWhenUnmounting(NULL),
136 	fShowFullPathInTitleBar(NULL),
137 	fSingleWindowBrowse(NULL),
138 	fShowNavigator(NULL),
139 	fShowSelectionWhenInactive(NULL),
140 	fTransparentSelection(NULL),
141 	fSortFolderNamesFirst(NULL),
142 	fHideDotFiles(NULL),
143 	fTypeAheadFiltering(NULL),
144 	fGenerateImageThumbnails(NULL),
145 	fRecentApplicationsCount(NULL),
146 	fRecentDocumentsCount(NULL),
147 	fRecentFoldersCount(NULL),
148 	fShowVolumeSpaceBar(NULL),
149 	fUsedSpaceColor(NULL),
150 	fFreeSpaceColor(NULL),
151 	fWarningSpaceColor(NULL),
152 	fDontMoveFilesToTrash(NULL),
153 	fAskBeforeDeleteFile(NULL),
154 	fInited(false),
155 	fSettingsLoaded(false)
156 {
157 }
158 
159 
160 TTrackerState::TTrackerState(const TTrackerState&)
161 	:
162 	Settings("", ""),
163 	fShowDisksIcon(NULL),
164 	fMountVolumesOntoDesktop(NULL),
165 	fDesktopFilePanelRoot(NULL),
166 	fMountSharedVolumesOntoDesktop(NULL),
167 	fEjectWhenUnmounting(NULL),
168 	fShowFullPathInTitleBar(NULL),
169 	fSingleWindowBrowse(NULL),
170 	fShowNavigator(NULL),
171 	fShowSelectionWhenInactive(NULL),
172 	fTransparentSelection(NULL),
173 	fSortFolderNamesFirst(NULL),
174 	fHideDotFiles(NULL),
175 	fTypeAheadFiltering(NULL),
176 	fGenerateImageThumbnails(NULL),
177 	fRecentApplicationsCount(NULL),
178 	fRecentDocumentsCount(NULL),
179 	fRecentFoldersCount(NULL),
180 	fShowVolumeSpaceBar(NULL),
181 	fUsedSpaceColor(NULL),
182 	fFreeSpaceColor(NULL),
183 	fWarningSpaceColor(NULL),
184 	fDontMoveFilesToTrash(NULL),
185 	fAskBeforeDeleteFile(NULL),
186 	fInited(false),
187 	fSettingsLoaded(false)
188 {
189 	// Placeholder copy constructor to prevent others from accidentally using
190 	// the default copy constructor.  Note, the DEBUGGER call is for the off
191 	// chance that a TTrackerState method (or friend) tries to make a copy.
192 	DEBUGGER("Don't make a copy of this!");
193 }
194 
195 
196 TTrackerState::~TTrackerState()
197 {
198 }
199 
200 
201 void
202 TTrackerState::SaveSettings(bool onlyIfNonDefault)
203 {
204 	if (fSettingsLoaded)
205 		_inherited::SaveSettings(onlyIfNonDefault);
206 }
207 
208 
209 void
210 TTrackerState::LoadSettingsIfNeeded()
211 {
212 	if (fSettingsLoaded)
213 		return;
214 
215 	// Set default settings before reading from disk
216 
217 	Add(fShowDisksIcon = new BooleanValueSetting("ShowDisksIcon", kDefaultShowDisksIcon));
218 	Add(fMountVolumesOntoDesktop
219 		= new BooleanValueSetting("MountVolumesOntoDesktop", kDefaultMountVolumesOntoDesktop));
220 	Add(fMountSharedVolumesOntoDesktop = new BooleanValueSetting(
221 		"MountSharedVolumesOntoDesktop", kDefaultMountSharedVolumesOntoDesktop));
222 	Add(fEjectWhenUnmounting
223 		= new BooleanValueSetting("EjectWhenUnmounting", kDefaultEjectWhenUnmounting));
224 
225 	Add(fDesktopFilePanelRoot // deprecated
226 		= new BooleanValueSetting("DesktopFilePanelRoot", kDefaultDesktopFilePanelRoot));
227 	Add(fShowSelectionWhenInactive // deprecated
228 		= new BooleanValueSetting("ShowSelectionWhenInactive", kDefaultShowSelectionWhenInactive));
229 
230 	Add(fShowFullPathInTitleBar
231 		= new BooleanValueSetting("ShowFullPathInTitleBar", kDefaultShowFullPathInTitleBar));
232 	Add(fSingleWindowBrowse
233 		= new BooleanValueSetting("SingleWindowBrowse", kDefaultSingleWindowBrowse));
234 	Add(fShowNavigator = new BooleanValueSetting("ShowNavigator", kDefaultShowNavigator));
235 	Add(fTransparentSelection
236 		= new BooleanValueSetting("TransparentSelection", kDefaultTransparentSelection));
237 	Add(fSortFolderNamesFirst
238 		= new BooleanValueSetting("SortFolderNamesFirst", kDefaultSortFolderNamesFirst));
239 	Add(fHideDotFiles = new BooleanValueSetting("HideDotFiles", kDefaultHideDotFiles));
240 	Add(fTypeAheadFiltering
241 		= new BooleanValueSetting("TypeAheadFiltering", kDefaultTypeAheadFiltering));
242 	Add(fGenerateImageThumbnails
243 		= new BooleanValueSetting("GenerateImageThumbnails", kDefaultGenerateImageThumbnails));
244 
245 	Add(fRecentApplicationsCount
246 		= new ScalarValueSetting("RecentApplications", kDefaultRecentApplications, "", ""));
247 	Add(fRecentDocumentsCount
248 		= new ScalarValueSetting("RecentDocuments", kDefaultRecentDocuments, "", ""));
249 	Add(fRecentFoldersCount
250 		= new ScalarValueSetting("RecentFolders", kDefaultRecentFolders, "", ""));
251 
252 	Add(fShowVolumeSpaceBar
253 		= new BooleanValueSetting("ShowVolumeSpaceBar", kDefaultShowVolumeSpaceBar));
254 	Add(fUsedSpaceColor
255 		= new HexScalarValueSetting("UsedSpaceColor", RGBTOHEX(kDefaultUsedSpaceColor), "", ""));
256 	Add(fFreeSpaceColor
257 		= new HexScalarValueSetting("FreeSpaceColor", RGBTOHEX(kDefaultFreeSpaceColor), "", ""));
258 	Add(fWarningSpaceColor
259 		= new HexScalarValueSetting("WarningSpaceColor", RGBTOHEX(kDefaultWarningSpaceColor),
260 			"", ""));
261 
262 	Add(fDontMoveFilesToTrash
263 		= new BooleanValueSetting("DontMoveFilesToTrash", kDefaultDontMoveFilesToTrash));
264 	Add(fAskBeforeDeleteFile
265 		= new BooleanValueSetting("AskBeforeDeleteFile", kDefaultAskBeforeDeleteFile));
266 
267 	TryReadingSettings();
268 
269 	NameAttributeText::SetSortFolderNamesFirst(
270 		fSortFolderNamesFirst->Value());
271 	RealNameAttributeText::SetSortFolderNamesFirst(
272 		fSortFolderNamesFirst->Value());
273 
274 	fSettingsLoaded = true;
275 }
276 
277 
278 //	#pragma mark - TrackerSettings
279 
280 
281 TrackerSettings::TrackerSettings()
282 {
283 	gTrackerState.LoadSettingsIfNeeded();
284 }
285 
286 
287 void
288 TrackerSettings::SaveSettings(bool onlyIfNonDefault)
289 {
290 	gTrackerState.SaveSettings(onlyIfNonDefault);
291 }
292 
293 
294 bool
295 TrackerSettings::ShowDisksIcon()
296 {
297 	return gTrackerState.fShowDisksIcon->Value();
298 }
299 
300 
301 void
302 TrackerSettings::SetShowDisksIcon(bool enabled)
303 {
304 	gTrackerState.fShowDisksIcon->SetValue(enabled);
305 }
306 
307 
308 bool
309 TrackerSettings::DesktopFilePanelRoot()
310 {
311 	return gTrackerState.fDesktopFilePanelRoot->Value();
312 }
313 
314 
315 void
316 TrackerSettings::SetDesktopFilePanelRoot(bool enabled)
317 {
318 	gTrackerState.fDesktopFilePanelRoot->SetValue(enabled);
319 }
320 
321 
322 bool
323 TrackerSettings::MountVolumesOntoDesktop()
324 {
325 	return gTrackerState.fMountVolumesOntoDesktop->Value();
326 }
327 
328 
329 void
330 TrackerSettings::SetMountVolumesOntoDesktop(bool enabled)
331 {
332 	gTrackerState.fMountVolumesOntoDesktop->SetValue(enabled);
333 }
334 
335 
336 bool
337 TrackerSettings::MountSharedVolumesOntoDesktop()
338 {
339 	return gTrackerState.fMountSharedVolumesOntoDesktop->Value();
340 }
341 
342 
343 void
344 TrackerSettings::SetMountSharedVolumesOntoDesktop(bool enabled)
345 {
346 	gTrackerState.fMountSharedVolumesOntoDesktop->SetValue(enabled);
347 }
348 
349 
350 bool
351 TrackerSettings::EjectWhenUnmounting()
352 {
353 	return gTrackerState.fEjectWhenUnmounting->Value();
354 }
355 
356 
357 void
358 TrackerSettings::SetEjectWhenUnmounting(bool enabled)
359 {
360 	gTrackerState.fEjectWhenUnmounting->SetValue(enabled);
361 }
362 
363 
364 bool
365 TrackerSettings::ShowVolumeSpaceBar()
366 {
367 	return gTrackerState.fShowVolumeSpaceBar->Value();
368 }
369 
370 
371 void
372 TrackerSettings::SetShowVolumeSpaceBar(bool enabled)
373 {
374 	gTrackerState.fShowVolumeSpaceBar->SetValue(enabled);
375 }
376 
377 
378 rgb_color
379 TrackerSettings::UsedSpaceColor()
380 {
381 	return ValueToColor(gTrackerState.fUsedSpaceColor->Value());
382 }
383 
384 
385 void
386 TrackerSettings::SetUsedSpaceColor(rgb_color color)
387 {
388 	gTrackerState.fUsedSpaceColor->ValueChanged(ColorToValue(color));
389 }
390 
391 
392 rgb_color
393 TrackerSettings::FreeSpaceColor()
394 {
395 	return ValueToColor(gTrackerState.fFreeSpaceColor->Value());
396 }
397 
398 
399 void
400 TrackerSettings::SetFreeSpaceColor(rgb_color color)
401 {
402 	gTrackerState.fFreeSpaceColor->ValueChanged(ColorToValue(color));
403 }
404 
405 
406 rgb_color
407 TrackerSettings::WarningSpaceColor()
408 {
409 	return ValueToColor(gTrackerState.fWarningSpaceColor->Value());
410 }
411 
412 
413 void
414 TrackerSettings::SetWarningSpaceColor(rgb_color color)
415 {
416 	gTrackerState.fWarningSpaceColor->ValueChanged(ColorToValue(color));
417 }
418 
419 
420 bool
421 TrackerSettings::ShowFullPathInTitleBar()
422 {
423 	return gTrackerState.fShowFullPathInTitleBar->Value();
424 }
425 
426 
427 void
428 TrackerSettings::SetShowFullPathInTitleBar(bool enabled)
429 {
430 	gTrackerState.fShowFullPathInTitleBar->SetValue(enabled);
431 }
432 
433 
434 bool
435 TrackerSettings::SortFolderNamesFirst()
436 {
437 	return gTrackerState.fSortFolderNamesFirst->Value();
438 }
439 
440 
441 void
442 TrackerSettings::SetSortFolderNamesFirst(bool enabled)
443 {
444 	gTrackerState.fSortFolderNamesFirst->SetValue(enabled);
445 	NameAttributeText::SetSortFolderNamesFirst(enabled);
446 	RealNameAttributeText::SetSortFolderNamesFirst(enabled);
447 }
448 
449 
450 bool
451 TrackerSettings::HideDotFiles()
452 {
453 	return gTrackerState.fHideDotFiles->Value();
454 }
455 
456 
457 void
458 TrackerSettings::SetHideDotFiles(bool hide)
459 {
460 	gTrackerState.fHideDotFiles->SetValue(hide);
461 }
462 
463 
464 bool
465 TrackerSettings::TypeAheadFiltering()
466 {
467 	return gTrackerState.fTypeAheadFiltering->Value();
468 }
469 
470 
471 void
472 TrackerSettings::SetTypeAheadFiltering(bool enabled)
473 {
474 	gTrackerState.fTypeAheadFiltering->SetValue(enabled);
475 }
476 
477 
478 bool
479 TrackerSettings::GenerateImageThumbnails()
480 {
481 	return gTrackerState.fGenerateImageThumbnails->Value();
482 }
483 
484 
485 void
486 TrackerSettings::SetGenerateImageThumbnails(bool enabled)
487 {
488 	gTrackerState.fGenerateImageThumbnails->SetValue(enabled);
489 }
490 
491 
492 bool
493 TrackerSettings::ShowSelectionWhenInactive()
494 {
495 	return gTrackerState.fShowSelectionWhenInactive->Value();
496 }
497 
498 
499 void
500 TrackerSettings::SetShowSelectionWhenInactive(bool enabled)
501 {
502 	gTrackerState.fShowSelectionWhenInactive->SetValue(enabled);
503 }
504 
505 
506 bool
507 TrackerSettings::TransparentSelection()
508 {
509 	return gTrackerState.fTransparentSelection->Value();
510 }
511 
512 
513 void
514 TrackerSettings::SetTransparentSelection(bool enabled)
515 {
516 	gTrackerState.fTransparentSelection->SetValue(enabled);
517 }
518 
519 
520 bool
521 TrackerSettings::SingleWindowBrowse()
522 {
523 	return gTrackerState.fSingleWindowBrowse->Value();
524 }
525 
526 
527 void
528 TrackerSettings::SetSingleWindowBrowse(bool enabled)
529 {
530 	gTrackerState.fSingleWindowBrowse->SetValue(enabled);
531 }
532 
533 
534 bool
535 TrackerSettings::ShowNavigator()
536 {
537 	return gTrackerState.fShowNavigator->Value();
538 }
539 
540 
541 void
542 TrackerSettings::SetShowNavigator(bool enabled)
543 {
544 	gTrackerState.fShowNavigator->SetValue(enabled);
545 }
546 
547 
548 void
549 TrackerSettings::RecentCounts(int32* applications, int32* documents,
550 	int32* folders)
551 {
552 	if (applications != NULL)
553 		*applications = gTrackerState.fRecentApplicationsCount->Value();
554 
555 	if (documents != NULL)
556 		*documents = gTrackerState.fRecentDocumentsCount->Value();
557 
558 	if (folders != NULL)
559 		*folders = gTrackerState.fRecentFoldersCount->Value();
560 }
561 
562 
563 void
564 TrackerSettings::SetRecentApplicationsCount(int32 count)
565 {
566 	gTrackerState.fRecentApplicationsCount->ValueChanged(count);
567 }
568 
569 
570 void
571 TrackerSettings::SetRecentDocumentsCount(int32 count)
572 {
573 	gTrackerState.fRecentDocumentsCount->ValueChanged(count);
574 }
575 
576 
577 void
578 TrackerSettings::SetRecentFoldersCount(int32 count)
579 {
580 	gTrackerState.fRecentFoldersCount->ValueChanged(count);
581 }
582 
583 
584 bool
585 TrackerSettings::DontMoveFilesToTrash()
586 {
587 	return gTrackerState.fDontMoveFilesToTrash->Value();
588 }
589 
590 
591 void
592 TrackerSettings::SetDontMoveFilesToTrash(bool enabled)
593 {
594 	gTrackerState.fDontMoveFilesToTrash->SetValue(enabled);
595 }
596 
597 
598 bool
599 TrackerSettings::AskBeforeDeleteFile()
600 {
601 	return gTrackerState.fAskBeforeDeleteFile->Value();
602 }
603 
604 
605 void
606 TrackerSettings::SetAskBeforeDeleteFile(bool enabled)
607 {
608 	gTrackerState.fAskBeforeDeleteFile->SetValue(enabled);
609 }
610