2 * Copyright (c) 1998-2007 Matthijs Hollemans
3 * Distributed under the terms of the MIT License.
14 #include <Directory.h>
17 #include <FindDirectory.h>
23 #include "GlobalDefs.h"
32 fSelectedFiles((uint32
)0),
37 fCaseSensitive(false),
38 fRegularExpression(false),
42 fFrame(100, 100, 500, 400),
52 if (find_directory(B_USER_DIRECTORY
, &path
) == B_OK
)
53 fFilePanelPath
= path
.Path();
55 fFilePanelPath
= "/boot/home";
58 if (be_roster
->FindApp(PE_SIGNATURE
, &dummy
) == B_OK
) {
59 // Pe is installed, change the default settings
69 status_t status
= _OpenFile(&file
, PREFS_FILE
);
79 if (file
.ReadAttr("RecurseDirs", B_INT32_TYPE
, 0, &value
,
81 fRecurseDirs
= (value
!= 0);
83 if (file
.ReadAttr("RecurseLinks", B_INT32_TYPE
, 0, &value
,
85 fRecurseLinks
= (value
!= 0);
87 if (file
.ReadAttr("SkipDotDirs", B_INT32_TYPE
, 0, &value
,
89 fSkipDotDirs
= (value
!= 0);
91 if (file
.ReadAttr("CaseSensitive", B_INT32_TYPE
, 0, &value
,
93 fCaseSensitive
= (value
!= 0);
95 if (file
.ReadAttr("RegularExpression", B_INT32_TYPE
, 0, &value
, sizeof(int32
)) > 0)
96 fRegularExpression
= (value
!= 0);
98 if (file
.ReadAttr("TextOnly", B_INT32_TYPE
, 0, &value
, sizeof(int32
)) > 0)
99 fTextOnly
= (value
!= 0);
101 if (file
.ReadAttr("InvokePe", B_INT32_TYPE
, 0, &value
, sizeof(int32
)) > 0)
102 fInvokePe
= (value
!= 0);
104 char buffer
[B_PATH_NAME_LENGTH
+1];
105 int32 length
= file
.ReadAttr("FilePanelPath", B_STRING_TYPE
, 0, &buffer
,
108 buffer
[length
] = '\0';
109 fFilePanelPath
= buffer
;
112 file
.ReadAttr("WindowFrame", B_RECT_TYPE
, 0, &fFrame
, sizeof(BRect
));
114 if (file
.ReadAttr("ShowLines", B_INT32_TYPE
, 0, &value
,
116 fShowLines
= (value
!= 0);
118 if (file
.ReadAttr("Encoding", B_INT32_TYPE
, 0, &value
, sizeof(int32
)) > 0)
131 status_t status
= _OpenFile(&file
, PREFS_FILE
,
132 B_CREATE_FILE
| B_WRITE_ONLY
);
136 status
= file
.Lock();
141 file
.WriteAttr("Version", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
143 value
= fRecurseDirs
? 1 : 0;
144 file
.WriteAttr("RecurseDirs", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
146 value
= fRecurseLinks
? 1 : 0;
147 file
.WriteAttr("RecurseLinks", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
149 value
= fSkipDotDirs
? 1 : 0;
150 file
.WriteAttr("SkipDotDirs", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
152 value
= fCaseSensitive
? 1 : 0;
153 file
.WriteAttr("CaseSensitive", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
155 value
= fRegularExpression
? 1 : 0;
156 file
.WriteAttr("RegularExpression", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
158 value
= fTextOnly
? 1 : 0;
159 file
.WriteAttr("TextOnly", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
161 value
= fInvokePe
? 1 : 0;
162 file
.WriteAttr("InvokePe", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
164 file
.WriteAttr("WindowFrame", B_RECT_TYPE
, 0, &fFrame
, sizeof(BRect
));
166 file
.WriteAttr("FilePanelPath", B_STRING_TYPE
, 0, fFilePanelPath
.String(),
167 fFilePanelPath
.Length() + 1);
169 value
= fShowLines
? 1 : 0;
170 file
.WriteAttr("ShowLines", B_INT32_TYPE
, 0, &value
, sizeof(int32
));
172 file
.WriteAttr("Encoding", B_INT32_TYPE
, 0, &fEncoding
, sizeof(int32
));
182 Model::AddToHistory(const char* text
)
187 BString
* string
= new (nothrow
) BString(text
);
188 if (string
== NULL
|| !items
.AddItem(string
)) {
194 int32 count
= items
.CountItems() - 1;
195 // don't check last item, since that's the one we just added
196 for (int32 t
= 0; t
< count
; ++t
) {
197 // If the same text is already in the list,
198 // then remove it first. Case-sensitive.
199 BString
* string
= static_cast<BString
*>(items
.ItemAt(t
));
200 if (*string
== text
) {
201 delete static_cast<BString
*>(items
.RemoveItem(t
));
206 while (items
.CountItems() >= HISTORY_LIMIT
)
207 delete static_cast<BString
*>(items
.RemoveItem((int32
)0));
215 Model::FillHistoryMenu(BMenu
* menu
) const
218 if (!_LoadHistory(items
))
221 for (int32 t
= items
.CountItems() - 1; t
>= 0; --t
) {
222 BString
* item
= static_cast<BString
*>(items
.ItemAtFast(t
));
223 BMessage
* message
= new BMessage(MSG_SELECT_HISTORY
);
224 message
->AddString("text", item
->String());
225 menu
->AddItem(new BMenuItem(item
->String(), message
));
232 // #pragma mark - private
236 Model::_LoadHistory(BList
& items
) const
239 status_t status
= _OpenFile(&file
, PREFS_FILE
);
243 status
= file
.Lock();
248 status
= message
.Unflatten(&file
);
255 for (int32 x
= 0; message
.FindString("string", x
, &string
) == B_OK
; x
++) {
256 BString
* copy
= new (nothrow
) BString(string
);
257 if (copy
== NULL
|| !items
.AddItem(copy
)) {
268 Model::_SaveHistory(const BList
& items
) const
271 status_t status
= _OpenFile(&file
, PREFS_FILE
,
272 B_CREATE_FILE
| B_WRITE_ONLY
| B_ERASE_FILE
,
273 B_USER_SETTINGS_DIRECTORY
, NULL
);
277 status
= file
.Lock();
282 int32 count
= items
.CountItems();
283 for (int32 i
= 0; i
< count
; i
++) {
284 BString
* string
= static_cast<BString
*>(items
.ItemAtFast(i
));
286 if (message
.AddString("string", string
->String()) != B_OK
)
290 status
= message
.Flatten(&file
);
291 file
.SetSize(message
.FlattenedSize());
300 Model::_FreeHistory(const BList
& items
) const
302 for (int32 t
= items
.CountItems() - 1; t
>= 0; --t
)
303 delete static_cast<BString
*>((items
.ItemAtFast(t
)));
308 Model::_OpenFile(BFile
* file
, const char* name
, uint32 openMode
,
309 directory_which which
, BVolume
* volume
) const
315 status_t status
= find_directory(which
, &path
, true, volume
);
319 status
= path
.Append(PREFS_FILE
);
323 status
= file
->SetTo(path
.Path(), openMode
);
327 status
= file
->InitCheck();