btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / webpositive / BrowsingHistory.cpp
blob665656407effd8d702c458ae167be7aad02da8db
1 /*
2 * Copyright (C) 2010 Stephan Aßmus <superstippi@gmx.de>
4 * All rights reserved. Distributed under the terms of the MIT License.
5 */
7 #include "BrowsingHistory.h"
9 #include <new>
10 #include <stdio.h>
12 #include <Autolock.h>
13 #include <Entry.h>
14 #include <File.h>
15 #include <FindDirectory.h>
16 #include <Message.h>
17 #include <Path.h>
19 #include "BrowserApp.h"
22 BrowsingHistoryItem::BrowsingHistoryItem(const BString& url)
24 fURL(url),
25 fDateTime(BDateTime::CurrentDateTime(B_LOCAL_TIME)),
26 fInvokationCount(0)
31 BrowsingHistoryItem::BrowsingHistoryItem(const BrowsingHistoryItem& other)
33 *this = other;
37 BrowsingHistoryItem::BrowsingHistoryItem(const BMessage* archive)
39 if (!archive)
40 return;
41 BMessage dateTimeArchive;
42 if (archive->FindMessage("date time", &dateTimeArchive) == B_OK)
43 fDateTime = BDateTime(&dateTimeArchive);
44 archive->FindString("url", &fURL);
45 archive->FindUInt32("invokations", &fInvokationCount);
49 BrowsingHistoryItem::~BrowsingHistoryItem()
54 status_t
55 BrowsingHistoryItem::Archive(BMessage* archive) const
57 if (!archive)
58 return B_BAD_VALUE;
59 BMessage dateTimeArchive;
60 status_t status = fDateTime.Archive(&dateTimeArchive);
61 if (status == B_OK)
62 status = archive->AddMessage("date time", &dateTimeArchive);
63 if (status == B_OK)
64 status = archive->AddString("url", fURL.String());
65 if (status == B_OK)
66 status = archive->AddUInt32("invokations", fInvokationCount);
67 return status;
71 BrowsingHistoryItem&
72 BrowsingHistoryItem::operator=(const BrowsingHistoryItem& other)
74 if (this == &other)
75 return *this;
77 fURL = other.fURL;
78 fDateTime = other.fDateTime;
79 fInvokationCount = other.fInvokationCount;
81 return *this;
85 bool
86 BrowsingHistoryItem::operator==(const BrowsingHistoryItem& other) const
88 if (this == &other)
89 return true;
91 return fURL == other.fURL && fDateTime == other.fDateTime
92 && fInvokationCount == other.fInvokationCount;
96 bool
97 BrowsingHistoryItem::operator!=(const BrowsingHistoryItem& other) const
99 return !(*this == other);
103 bool
104 BrowsingHistoryItem::operator<(const BrowsingHistoryItem& other) const
106 if (this == &other)
107 return false;
109 return fDateTime < other.fDateTime || fURL < other.fURL;
113 bool
114 BrowsingHistoryItem::operator<=(const BrowsingHistoryItem& other) const
116 return (*this == other) || (*this < other);
120 bool
121 BrowsingHistoryItem::operator>(const BrowsingHistoryItem& other) const
123 if (this == &other)
124 return false;
126 return fDateTime > other.fDateTime || fURL > other.fURL;
130 bool
131 BrowsingHistoryItem::operator>=(const BrowsingHistoryItem& other) const
133 return (*this == other) || (*this > other);
137 void
138 BrowsingHistoryItem::Invoked()
140 // Eventually, we may overflow...
141 uint32 count = fInvokationCount + 1;
142 if (count > fInvokationCount)
143 fInvokationCount = count;
144 fDateTime = BDateTime::CurrentDateTime(B_LOCAL_TIME);
148 // #pragma mark - BrowsingHistory
151 BrowsingHistory
152 BrowsingHistory::sDefaultInstance;
155 BrowsingHistory::BrowsingHistory()
157 BLocker("browsing history"),
158 fHistoryItems(64),
159 fMaxHistoryItemAge(7),
160 fSettingsLoaded(false)
165 BrowsingHistory::~BrowsingHistory()
167 _SaveSettings();
168 _Clear();
172 /*static*/ BrowsingHistory*
173 BrowsingHistory::DefaultInstance()
175 if (sDefaultInstance.Lock()) {
176 sDefaultInstance._LoadSettings();
177 sDefaultInstance.Unlock();
179 return &sDefaultInstance;
183 bool
184 BrowsingHistory::AddItem(const BrowsingHistoryItem& item)
186 BAutolock _(this);
188 return _AddItem(item, false);
192 int32
193 BrowsingHistory::BrowsingHistory::CountItems() const
195 BAutolock _(const_cast<BrowsingHistory*>(this));
197 return fHistoryItems.CountItems();
201 BrowsingHistoryItem
202 BrowsingHistory::HistoryItemAt(int32 index) const
204 BAutolock _(const_cast<BrowsingHistory*>(this));
206 BrowsingHistoryItem* existingItem = reinterpret_cast<BrowsingHistoryItem*>(
207 fHistoryItems.ItemAt(index));
208 if (!existingItem)
209 return BrowsingHistoryItem(BString());
211 return BrowsingHistoryItem(*existingItem);
215 void
216 BrowsingHistory::Clear()
218 BAutolock _(this);
219 _Clear();
220 _SaveSettings();
224 void
225 BrowsingHistory::SetMaxHistoryItemAge(int32 days)
227 BAutolock _(this);
228 if (fMaxHistoryItemAge != days) {
229 fMaxHistoryItemAge = days;
230 _SaveSettings();
235 int32
236 BrowsingHistory::MaxHistoryItemAge() const
238 return fMaxHistoryItemAge;
242 // #pragma mark - private
245 void
246 BrowsingHistory::_Clear()
248 int32 count = CountItems();
249 for (int32 i = 0; i < count; i++) {
250 BrowsingHistoryItem* item = reinterpret_cast<BrowsingHistoryItem*>(
251 fHistoryItems.ItemAtFast(i));
252 delete item;
254 fHistoryItems.MakeEmpty();
258 bool
259 BrowsingHistory::_AddItem(const BrowsingHistoryItem& item, bool internal)
261 int32 count = CountItems();
262 int32 insertionIndex = count;
263 for (int32 i = 0; i < count; i++) {
264 BrowsingHistoryItem* existingItem
265 = reinterpret_cast<BrowsingHistoryItem*>(
266 fHistoryItems.ItemAtFast(i));
267 if (item.URL() == existingItem->URL()) {
268 if (!internal) {
269 existingItem->Invoked();
270 _SaveSettings();
272 return true;
274 if (item < *existingItem)
275 insertionIndex = i;
277 BrowsingHistoryItem* newItem = new(std::nothrow) BrowsingHistoryItem(item);
278 if (!newItem || !fHistoryItems.AddItem(newItem, insertionIndex)) {
279 delete newItem;
280 return false;
283 if (!internal) {
284 newItem->Invoked();
285 _SaveSettings();
288 return true;
292 void
293 BrowsingHistory::_LoadSettings()
295 if (fSettingsLoaded)
296 return;
298 fSettingsLoaded = true;
300 BFile settingsFile;
301 if (_OpenSettingsFile(settingsFile, B_READ_ONLY)) {
302 BMessage settingsArchive;
303 settingsArchive.Unflatten(&settingsFile);
304 if (settingsArchive.FindInt32("max history item age",
305 &fMaxHistoryItemAge) != B_OK) {
306 fMaxHistoryItemAge = 7;
308 BDateTime oldestAllowedDateTime
309 = BDateTime::CurrentDateTime(B_LOCAL_TIME);
310 oldestAllowedDateTime.Date().AddDays(-fMaxHistoryItemAge);
312 BMessage historyItemArchive;
313 for (int32 i = 0; settingsArchive.FindMessage("history item", i,
314 &historyItemArchive) == B_OK; i++) {
315 BrowsingHistoryItem item(&historyItemArchive);
316 if (oldestAllowedDateTime < item.DateTime())
317 _AddItem(item, true);
318 historyItemArchive.MakeEmpty();
324 void
325 BrowsingHistory::_SaveSettings()
327 BFile settingsFile;
328 if (_OpenSettingsFile(settingsFile,
329 B_CREATE_FILE | B_ERASE_FILE | B_WRITE_ONLY)) {
330 BMessage settingsArchive;
331 settingsArchive.AddInt32("max history item age", fMaxHistoryItemAge);
332 BMessage historyItemArchive;
333 int32 count = CountItems();
334 for (int32 i = 0; i < count; i++) {
335 BrowsingHistoryItem item = HistoryItemAt(i);
336 if (item.Archive(&historyItemArchive) != B_OK)
337 break;
338 if (settingsArchive.AddMessage("history item",
339 &historyItemArchive) != B_OK) {
340 break;
342 historyItemArchive.MakeEmpty();
344 settingsArchive.Flatten(&settingsFile);
349 bool
350 BrowsingHistory::_OpenSettingsFile(BFile& file, uint32 mode)
352 BPath path;
353 if (find_directory(B_USER_SETTINGS_DIRECTORY, &path) != B_OK
354 || path.Append(kApplicationName) != B_OK
355 || path.Append("BrowsingHistory") != B_OK) {
356 return false;
358 return file.SetTo(path.Path(), mode) == B_OK;