diff --git a/src/apps/webpositive/BrowsingHistory.cpp b/src/apps/webpositive/BrowsingHistory.cpp index e70d3c0e03..d58985d9e1 100644 --- a/src/apps/webpositive/BrowsingHistory.cpp +++ b/src/apps/webpositive/BrowsingHistory.cpp @@ -32,10 +32,115 @@ #include #include #include +#include #include #include +BrowsingHistoryItem::BrowsingHistoryItem(const BString& url) + : m_url(url) + , m_dateTime(BDateTime::CurrentDateTime(B_LOCAL_TIME)) + , m_invokationCount(0) +{ +} + +BrowsingHistoryItem::BrowsingHistoryItem(const BrowsingHistoryItem& other) +{ + *this = other; +} + +BrowsingHistoryItem::BrowsingHistoryItem(const BMessage* archive) +{ + if (!archive) + return; + BMessage dateTimeArchive; + if (archive->FindMessage("date time", &dateTimeArchive) == B_OK) + m_dateTime = BDateTime(&dateTimeArchive); + archive->FindString("url", &m_url); + archive->FindUInt32("invokations", &m_invokationCount); +} + +BrowsingHistoryItem::~BrowsingHistoryItem() +{ +} + +status_t BrowsingHistoryItem::archive(BMessage* archive) const +{ + if (!archive) + return B_BAD_VALUE; + BMessage dateTimeArchive; + status_t status = m_dateTime.Archive(&dateTimeArchive); + if (status == B_OK) + status = archive->AddMessage("date time", &dateTimeArchive); + if (status == B_OK) + status = archive->AddString("url", m_url.String()); + if (status == B_OK) + status = archive->AddUInt32("invokations", m_invokationCount); + return status; +} + +BrowsingHistoryItem& BrowsingHistoryItem::operator=(const BrowsingHistoryItem& other) +{ + if (this == &other) + return *this; + + m_url = other.m_url; + m_dateTime = other.m_dateTime; + m_invokationCount = other.m_invokationCount; + + return *this; +} + +bool BrowsingHistoryItem::operator==(const BrowsingHistoryItem& other) const +{ + if (this == &other) + return true; + + return m_url == other.m_url && m_dateTime == other.m_dateTime + && m_invokationCount == other.m_invokationCount; +} + +bool BrowsingHistoryItem::operator!=(const BrowsingHistoryItem& other) const +{ + return !(*this == other); +} + +bool BrowsingHistoryItem::operator<(const BrowsingHistoryItem& other) const +{ + if (this == &other) + return false; + + return m_dateTime < other.m_dateTime || m_url < other.m_url; +} + +bool BrowsingHistoryItem::operator<=(const BrowsingHistoryItem& other) const +{ + return (*this == other) || (*this < other); +} + +bool BrowsingHistoryItem::operator>(const BrowsingHistoryItem& other) const +{ + if (this == &other) + return false; + + return m_dateTime > other.m_dateTime || m_url > other.m_url; +} + +bool BrowsingHistoryItem::operator>=(const BrowsingHistoryItem& other) const +{ + return (*this == other) || (*this > other); +} + +void BrowsingHistoryItem::increaseInvokationCount() +{ + // Eventually, we may overflow... + uint32 count = m_invokationCount + 1; + if (count > m_invokationCount) + m_invokationCount = count; +} + +// #pragma mark - BrowsingHistory + BrowsingHistory BrowsingHistory::s_defaultInstance; BrowsingHistory::BrowsingHistory() diff --git a/src/apps/webpositive/BrowsingHistory.h b/src/apps/webpositive/BrowsingHistory.h index 82ef0ba04f..b660cb30fb 100644 --- a/src/apps/webpositive/BrowsingHistory.h +++ b/src/apps/webpositive/BrowsingHistory.h @@ -28,9 +28,40 @@ #ifndef BrowsingHistory_h #define BrowsingHistory_h +#include "DateTime.h" #include class BFile; +class BString; + +class BrowsingHistoryItem { +public: + BrowsingHistoryItem(const BString& url); + BrowsingHistoryItem(const BrowsingHistoryItem& other); + BrowsingHistoryItem(const BMessage* archive); + ~BrowsingHistoryItem(); + + status_t archive(BMessage* archive) const; + + BrowsingHistoryItem& operator=(const BrowsingHistoryItem& other); + + bool operator==(const BrowsingHistoryItem& other) const; + bool operator!=(const BrowsingHistoryItem& other) const; + bool operator<(const BrowsingHistoryItem& other) const; + bool operator<=(const BrowsingHistoryItem& other) const; + bool operator>(const BrowsingHistoryItem& other) const; + bool operator>=(const BrowsingHistoryItem& other) const; + + const BString& url() const { return m_url; } + const BDateTime& dateTime() const { return m_dateTime; } + uint32 invokationCount() const { return m_invokationCount; } + void increaseInvokationCount(); + +private: + BString m_url; + BDateTime m_dateTime; + uint32 m_invokationCount; +}; class BrowsingHistory : public BLocker { public: