xref: /haiku/src/apps/expander/ExpanderSettings.cpp (revision 67bce78b48ed6d01b5a8eef89f5694c372b7e0a1)
1 /*****************************************************************************/
2 // Expander
3 // Written by Jérôme Duval
4 //
5 // ExpanderSettings.cpp
6 //
7 // Code from Diskprobe by Axel Dörfler
8 //
9 // Copyright (c) 2004 OpenBeOS Project
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining a
12 // copy of this software and associated documentation files (the "Software"),
13 // to deal in the Software without restriction, including without limitation
14 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 // and/or sell copies of the Software, and to permit persons to whom the
16 // Software is furnished to do so, subject to the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be included
19 // in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 // DEALINGS IN THE SOFTWARE.
28 /*****************************************************************************/
29 #include "ExpanderSettings.h"
30 
31 #include <ByteOrder.h>
32 #include <Screen.h>
33 #include <FindDirectory.h>
34 #include <Entry.h>
35 #include <stdlib.h>
36 #include <Path.h>
37 
38 //	Format of Expander_Settings
39 //	1st byte :	unknown (0x1)
40 //	2nd byte :	Automatically expand files (default : 0x0)
41 //	3rd byte :	Close when done (default : 0x0)
42 //	4th byte :	0x66
43 //				0x63 (default)
44 //				0x65
45 //	5th byte : 	unknown (0x0)
46 //	4 bytes : 	dev_t	(default : 0xffff)
47 //	8 bytes : 	ino_t	(default : 0xfffffffff)
48 //	4 bytes : 	name length (big endian) (default : 0x0)
49 //	n bytes : 	name	(default : "")
50 //	1 byte : 	Open destination folder (default : 0x1)
51 //	1 byte : 	Show content listing (default : 0x0)
52 //	4 bytes : 	window position topleft x (default : 0x4842)
53 //	4 bytes : 	window position topleft y (default : 0x4842)
54 
55 ExpanderSettings::ExpanderSettings()
56 	:
57 	fMessage(kMsgExpanderSettings),
58 	fUpdated(false)
59 {
60 	fMessage.AddBool("automatically_expand_files", false);
61 	fMessage.AddBool("close_when_done", false);
62 	fMessage.AddInt8("destination_folder", 0x63);
63 	entry_ref ref;
64 	fMessage.AddRef("destination_folder_use", &ref);
65 	fMessage.AddBool("open_destination_folder", true);
66 	fMessage.AddBool("show_contents_listing", false);
67 	fMessage.AddPoint("window_position", BPoint(50, 50));
68 
69 	BFile file;
70 	if (Open(&file, B_READ_ONLY) != B_OK)
71 		return;
72 
73 	// ToDo: load/save settings as flattened BMessage - but not yet,
74 	//		since that will break compatibility with R5's Expander
75 
76 	bool unknown;
77 	bool automatically_expand_files;
78 	bool close_when_done;
79 	int8 destination_folder;
80 	bool open_destination_folder;
81 	bool show_contents_listing;
82 	BPoint position;
83 	int32 name_size;
84 	if (
85 		(file.Read(&unknown, sizeof(unknown)) == sizeof(unknown))
86 		&& (file.Read(&automatically_expand_files, sizeof(automatically_expand_files)) == sizeof(automatically_expand_files))
87 		&& (file.Read(&close_when_done, sizeof(close_when_done)) == sizeof(close_when_done))
88 		&& (file.Read(&destination_folder, sizeof(destination_folder)) == sizeof(destination_folder))
89 		&& (file.Read(&unknown, sizeof(unknown)) == sizeof(unknown))
90 		&& (file.Read(&ref.device, sizeof(ref.device)) == sizeof(ref.device))
91 		&& (file.Read(&ref.directory, sizeof(ref.directory)) == sizeof(ref.directory))
92 		&& (file.Read(&name_size, sizeof(name_size)) == sizeof(name_size))
93 		&& ((name_size <= 0) || (ref.name = (char*)malloc(name_size)))
94 		&& (file.Read(ref.name, name_size) == name_size)
95 		&& (file.Read(&open_destination_folder, sizeof(open_destination_folder)) == sizeof(open_destination_folder))
96 		&& (file.Read(&show_contents_listing, sizeof(show_contents_listing)) == sizeof(show_contents_listing))
97 		&& (file.Read(&position, sizeof(position)) == sizeof(position))
98 		) {
99 
100 		// check if the window position is on screen at all
101 		BScreen screen;
102 		if (screen.Frame().Contains(position))
103 			fMessage.ReplacePoint("window_position", position);
104 
105 		fMessage.ReplaceBool("automatically_expand_files", automatically_expand_files);
106 		fMessage.ReplaceBool("close_when_done", close_when_done);
107 		if(destination_folder == 0x66 || destination_folder == 0x63 || destination_folder == 0x65)
108 			fMessage.ReplaceInt8("destination_folder", destination_folder);
109 		BEntry entry(&ref);
110 		if(entry.Exists())
111 			fMessage.ReplaceRef("destination_folder_use", &ref);
112 		fMessage.ReplaceBool("open_destination_folder", open_destination_folder);
113 		fMessage.ReplaceBool("show_contents_listing", show_contents_listing);
114 	}
115 }
116 
117 
118 ExpanderSettings::~ExpanderSettings()
119 {
120 	// only save the settings if something has changed
121 	if (!fUpdated)
122 		return;
123 
124 	BFile file;
125 	if (Open(&file, B_CREATE_FILE | B_WRITE_ONLY) != B_OK)
126 		return;
127 
128 	bool automatically_expand_files;
129 	bool close_when_done;
130 	int8 destination_folder;
131 	entry_ref ref;
132 	bool open_destination_folder;
133 	bool show_contents_listing;
134 	BPoint position;
135 	bool unknown = 1;
136 
137 	if ((fMessage.FindPoint("window_position", &position) == B_OK)
138 		&& (fMessage.FindBool("automatically_expand_files", &automatically_expand_files) == B_OK)
139 		&& (fMessage.FindBool("close_when_done", &close_when_done) == B_OK)
140 		&& (fMessage.FindInt8("destination_folder", &destination_folder) == B_OK)
141 		&& (fMessage.FindRef("destination_folder_use", &ref) == B_OK)
142 		&& (fMessage.FindBool("open_destination_folder", &open_destination_folder) == B_OK)
143 		&& (fMessage.FindBool("show_contents_listing", &show_contents_listing) == B_OK)	) {
144 
145 		file.Write(&unknown, sizeof(unknown));
146 		file.Write(&automatically_expand_files, sizeof(automatically_expand_files));
147 		file.Write(&close_when_done, sizeof(close_when_done));
148 		file.Write(&destination_folder, sizeof(destination_folder));
149 		unknown = 0;
150 		file.Write(&unknown, sizeof(unknown));
151 		file.Write(&ref.device, sizeof(ref.device));
152 		file.Write(&ref.directory, sizeof(ref.directory));
153 		int32 name_size = 0;
154 		if(ref.name)
155 			name_size = strlen(ref.name);
156 		file.Write(&name_size, sizeof(name_size));
157 		file.Write(ref.name, name_size);
158 		file.Write(&open_destination_folder, sizeof(open_destination_folder));
159 		file.Write(&show_contents_listing, sizeof(show_contents_listing));
160 		file.Write(&position, sizeof(position));
161 	}
162 
163 }
164 
165 
166 status_t
167 ExpanderSettings::Open(BFile *file, int32 mode)
168 {
169 	BPath path;
170 	if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK)
171 		return B_ERROR;
172 
173 	path.Append("Expander_Settings");
174 
175 	return file->SetTo(path.Path(), mode);
176 }
177 
178 
179 void
180 ExpanderSettings::UpdateFrom(BMessage *message)
181 {
182 	bool automatically_expand_files;
183 	bool close_when_done;
184 	int8 destination_folder;
185 	entry_ref ref;
186 	bool open_destination_folder;
187 	bool show_contents_listing;
188 	BPoint position;
189 
190 	if (message->FindPoint("window_position", &position) == B_OK)
191 		fMessage.ReplacePoint("window_position", position);
192 
193 	if (message->FindBool("automatically_expand_files", &automatically_expand_files) == B_OK)
194 		fMessage.ReplaceBool("automatically_expand_files", automatically_expand_files);
195 
196 	if (message->FindBool("close_when_done", &close_when_done) == B_OK)
197 		fMessage.ReplaceBool("close_when_done", close_when_done);
198 
199 	if (message->FindInt8("destination_folder", &destination_folder) == B_OK)
200 		fMessage.ReplaceInt8("destination_folder", destination_folder);
201 
202 	if (message->FindRef("destination_folder_use", &ref) == B_OK)
203 		fMessage.ReplaceRef("destination_folder_use", &ref);
204 
205 	if (message->FindBool("open_destination_folder", &open_destination_folder) == B_OK)
206 		fMessage.ReplaceBool("open_destination_folder", open_destination_folder);
207 
208 	if (message->FindBool("show_contents_listing", &show_contents_listing) == B_OK)
209 		fMessage.ReplaceBool("show_contents_listing", show_contents_listing);
210 
211 	fUpdated = true;
212 }
213