diff --git a/core/contact.cpp b/core/contact.cpp index d1a490d..441218b 100644 --- a/core/contact.cpp +++ b/core/contact.cpp @@ -148,12 +148,8 @@ void Core::Contact::performRequest(int count, const QString& before) emit needEarlierHistory(before, "", QDateTime(), QDateTime()); break; case beginning: { - QString lBefore; bool found = false; if (appendCache.size() != 0) { - if (before.size() == 0) { - lBefore = appendCache.back().getId(); - } for (std::list::const_reverse_iterator itr = appendCache.rbegin(), end = appendCache.rend(); itr != end; ++itr) { const Shared::Message& msg = *itr; if (found) { @@ -177,58 +173,35 @@ void Core::Contact::performRequest(int count, const QString& before) } } if (found) { - lBefore = responseCache.front().getId(); - emit needEarlierHistory(lBefore, "", QDateTime(), QDateTime()); + emit needEarlierHistory(responseCache.front().getId(), "", QDateTime(), QDateTime()); } else { - std::list arc; - if (responseCache.size() > 0) { - lBefore = responseCache.front().getId(); - } else { - lBefore = before; - } - if (count != -1) { - try { - arc = archive->getBefore(requestedCount - receivedCount, lBefore); - responseCache.insert(responseCache.begin(), arc.begin(), arc.end()); - emit historyResponse(responseCache); - nextRequest(); - } catch (Archive::NotFound e) { - requestCache.emplace_back(count, before); - requestedCount = -1; - emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime()); - } - } else { - try { - arc = archive->getBefore(1, lBefore); - //just do nothing since response is not required - nextRequest(); //may be even it's a signal that the history is now complete? - } catch (Archive::NotFound e) { - emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime()); - } + if (requiestFromArchive(before)) { + nextRequest(); } } } break; - case end: - std::list arc; - if (count != -1) { - try { - arc = archive->getBefore(requestedCount - receivedCount, before); - responseCache.insert(responseCache.begin(), arc.begin(), arc.end()); - emit historyResponse(responseCache); - nextRequest(); - } catch (Archive::NotFound e) { - requestCache.emplace_back(count, before); - requestedCount = -1; - emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime()); - } - } else { - try { - arc = archive->getBefore(1, before); - //just do nothing since response is not required - nextRequest(); //may be even it's a signal that the history is now complete? - } catch (Archive::NotFound e) { - emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime()); + case end: { + std::list arc; + if (count != -1) { + try { + arc = archive->getBefore(requestedCount - receivedCount, before); + responseCache.insert(responseCache.begin(), arc.begin(), arc.end()); + emit historyResponse(responseCache); + nextRequest(); + } catch (Archive::NotFound e) { + requestCache.emplace_back(count, before); + requestedCount = -1; + emit needEarlierHistory(archive->oldestId(), "", QDateTime(), QDateTime()); + } + } else { + try { + arc = archive->getBefore(1, before); + //just do nothing since response is not required + nextRequest(); //may be even it's a signal that the history is now complete? + } catch (Archive::NotFound e) { + emit needEarlierHistory(archive->oldestId(), "", QDateTime(), QDateTime()); + } } } break; @@ -241,6 +214,47 @@ void Core::Contact::performRequest(int count, const QString& before) } } +bool Core::Contact::requiestFromArchive(const QString& before) +{ + std::list arc; + QString lBefore; + if (responseCache.size() > 0) { + lBefore = responseCache.front().getId(); + } else { + lBefore = before; + } + if (requestedCount != -1) { + try { + arc = archive->getBefore(requestedCount - receivedCount, lBefore); + responseCache.insert(responseCache.begin(), arc.begin(), arc.end()); + emit historyResponse(responseCache); + return true; + } catch (Archive::NotFound e) { + requestCache.emplace_back(requestedCount, before); + requestedCount = -1; + switch (archiveState) { + case empty: + case beginning: + case end: + case chunk: + case complete: + } + emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime()); + return false; + } + } else { + try { + arc = archive->getBefore(1, lBefore); + //just do nothing since response is not required + //may be even it's a signal that the history is now complete? + return true; + } catch (Archive::NotFound e) { + emit needEarlierHistory("", archive->newestId(), QDateTime(), QDateTime()); + return false; + } + } +} + void Core::Contact::appendMessageToArchive(const Shared::Message& msg) { diff --git a/core/contact.h b/core/contact.h index 49346c6..12b85cd 100644 --- a/core/contact.h +++ b/core/contact.h @@ -83,6 +83,7 @@ private: private: void nextRequest(); void performRequest(int count, const QString& before); + bool requiestFromArchive(const QString& before); }; }