39feb2ab66a65b66d09f964c358867c2efc09038
[quassel.git] / src / core / corebacklogmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include "corebacklogmanager.h"
22 #include "core.h"
23 #include "coresession.h"
24
25 #include <QDebug>
26
27 CoreBacklogManager::CoreBacklogManager(CoreSession *coreSession)
28   : BacklogManager(coreSession),
29     _coreSession(coreSession)
30 {
31 }
32
33 QVariantList CoreBacklogManager::requestBacklog(BufferId bufferId, MsgId first, MsgId last, int limit, int additional) {
34   QVariantList backlog;
35   QList<Message> msgList;
36   msgList = Core::requestMsgs(coreSession()->user(), bufferId, first, last, limit);
37
38   QList<Message>::const_iterator msgIter = msgList.constBegin();
39   QList<Message>::const_iterator msgListEnd = msgList.constEnd();
40   while(msgIter != msgListEnd) {
41     backlog << qVariantFromValue(*msgIter);
42     msgIter++;
43   }
44
45   if(additional) {
46     MsgId oldestMessage;
47     if(!msgList.isEmpty()) {
48       if(msgList.first().msgId() < msgList.last().msgId())
49         oldestMessage = msgList.first().msgId();
50       else
51         oldestMessage = msgList.last().msgId();
52     }
53
54     if(first != -1) {
55       last = first;
56     } else {
57       last = oldestMessage;
58     }
59
60     // only fetch additional messages if they they continue seemlessly
61     // that is, if the list of messages is not truncated by the limit
62     if(last.isValid() && last == oldestMessage) {
63       msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
64       msgIter = msgList.constBegin();
65       msgListEnd = msgList.constEnd();
66       while(msgIter != msgListEnd) {
67         backlog << qVariantFromValue(*msgIter);
68         msgIter++;
69       }
70     }
71   }
72
73   return backlog;
74 }
75
76 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional) {
77   qDebug() << "CoreBacklogManager::requestBacklogAll" << first << last << limit << additional;
78   QVariantList backlog;
79   QList<Message> msgList;
80   msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
81
82   QList<Message>::const_iterator msgIter = msgList.constBegin();
83   QList<Message>::const_iterator msgListEnd = msgList.constEnd();
84   while(msgIter != msgListEnd) {
85     backlog << qVariantFromValue(*msgIter);
86     msgIter++;
87   }
88
89   if(additional) {
90     if(first != -1) {
91       last = first;
92     } else {
93       last = -1;
94       if(!msgList.isEmpty()) {
95         if(msgList.first().msgId() < msgList.last().msgId())
96           last = msgList.first().msgId();
97         else
98           last = msgList.last().msgId();
99       }
100     }
101     msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
102     msgIter = msgList.constBegin();
103     msgListEnd = msgList.constEnd();
104     while(msgIter != msgListEnd) {
105       backlog << qVariantFromValue(*msgIter);
106       msgIter++;
107     }
108   }
109
110   return backlog;
111 }