Make --norestore work again
[quassel.git] / src / core / corebacklogmanager.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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     if(first != -1) {
47       last = first;
48     } else {
49       last = -1;
50       if(!msgList.isEmpty()) {
51         if(msgList.first().msgId() < msgList.last().msgId())
52           last = msgList.first().msgId();
53         else
54           last = msgList.last().msgId();
55       }
56     }
57     msgList = Core::requestMsgs(coreSession()->user(), bufferId, -1, last, additional);
58     msgIter = msgList.constBegin();
59     msgListEnd = msgList.constEnd();
60     while(msgIter != msgListEnd) {
61       backlog << qVariantFromValue(*msgIter);
62       msgIter++;
63     }
64   }
65
66   return backlog;
67 }
68
69 QVariantList CoreBacklogManager::requestBacklogAll(MsgId first, MsgId last, int limit, int additional) {
70   qDebug() << "CoreBacklogManager::requestBacklogAll" << first << last << limit << additional;
71   QVariantList backlog;
72   QList<Message> msgList;
73   msgList = Core::requestAllMsgs(coreSession()->user(), first, last, limit);
74
75   QList<Message>::const_iterator msgIter = msgList.constBegin();
76   QList<Message>::const_iterator msgListEnd = msgList.constEnd();
77   while(msgIter != msgListEnd) {
78     backlog << qVariantFromValue(*msgIter);
79     msgIter++;
80   }
81
82   if(additional) {
83     if(first != -1) {
84       last = first;
85     } else {
86       last = -1;
87       if(!msgList.isEmpty()) {
88         if(msgList.first().msgId() < msgList.last().msgId())
89           last = msgList.first().msgId();
90         else
91           last = msgList.last().msgId();
92       }
93     }
94     msgList = Core::requestAllMsgs(coreSession()->user(), -1, last, additional);
95     msgIter = msgList.constBegin();
96     msgListEnd = msgList.constEnd();
97     while(msgIter != msgListEnd) {
98       backlog << qVariantFromValue(*msgIter);
99       msgIter++;
100     }
101   }
102
103   return backlog;
104 }