~ubuntu-branches/ubuntu/dapper/ksystemlog/dapper

« back to all changes in this revision

Viewing changes to ksystemlog/src/logLineList.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2005-07-07 16:00:00 UTC
  • Revision ID: james.westby@ubuntu.com-20050707160000-a104d769ph3yfkg4
Tags: upstream-0.3.2
ImportĀ upstreamĀ versionĀ 0.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright (C) 2005 by Nicolas Ternisien                               *
 
3
 *   nicolas.ternisien@gmail.com                                           *
 
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) any later version.                                   *
 
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 "logLineList.h"
 
22
#include "logLine.h"
 
23
 
 
24
#include "view.h"
 
25
 
 
26
 
 
27
LogLineList::LogLineList() :
 
28
        oldestLogLine(NULL),
 
29
        firstAdd(true)
 
30
        {
 
31
 
 
32
}
 
33
 
 
34
LogLineList::~LogLineList() {
 
35
 
 
36
}
 
37
 
 
38
LogLine* LogLineList::getOldestLine() {
 
39
        return(oldestLogLine);
 
40
}
 
41
 
 
42
void LogLineList::removeOldestLine() {
 
43
        LogLine* line=this->getOldestLine();
 
44
        
 
45
        if (line!=NULL) {
 
46
                this->remove(line);
 
47
        }
 
48
 
 
49
}
 
50
 
 
51
bool LogLineList::isNewer(LogLine* line) {
 
52
        LogLine* olderLine=this->getOldestLine();
 
53
        
 
54
        if (olderLine==NULL)
 
55
                return(true);
 
56
                
 
57
        return(line->isNewerThan(*olderLine));
 
58
 
 
59
}
 
60
 
 
61
bool LogLineList::isEmpty() {
 
62
        return(list.isEmpty());
 
63
}
 
64
 
 
65
 
 
66
LogLine* LogLineList::lastLineInserted() {      
 
67
        //Can also returns NULL
 
68
        return(list.last());
 
69
}
 
70
 
 
71
int LogLineList::getNewLineCount() {
 
72
        return(addedList.count());
 
73
}
 
74
 
 
75
void LogLineList::insert(LogLine* line) {
 
76
        if (!isNewer(line) || list.count()==0) {
 
77
                oldestLogLine=line;
 
78
        }
 
79
 
 
80
        addedList.append(line);
 
81
        list.append(line);
 
82
}
 
83
 
 
84
bool LogLineList::lineAlreadyExists(LogLine* line) {
 
85
        QPtrListIterator<LogLine> it(list);
 
86
 
 
87
        LogLine* other=it.current();
 
88
        while (other!=NULL) {
 
89
                if (other->equals(*line))
 
90
                        return(true);
 
91
                
 
92
                ++it;
 
93
                other=it.current();
 
94
        }
 
95
 
 
96
        return(false);
 
97
}
 
98
 
 
99
 
 
100
bool LogLineList::remove(LogLine* removedLine) {
 
101
        QPtrListIterator<LogLine> it(list);
 
102
 
 
103
        LogLine* line=it.current();
 
104
        while (line!=NULL) {
 
105
                
 
106
                if (this->equals(removedLine, line)) {
 
107
                        removedList.append(line);
 
108
 
 
109
                        addedList.remove(line);
 
110
                        list.remove(line);
 
111
 
 
112
                        //Now find the new oldest line
 
113
                        updateOldestLine();
 
114
 
 
115
                        return(true);
 
116
                }
 
117
                
 
118
                ++it;
 
119
                line=it.current();
 
120
 
 
121
        }
 
122
 
 
123
        return(false);
 
124
}
 
125
 
 
126
bool LogLineList::equals(LogLine* l1, LogLine* l2) {
 
127
        return(l1->equals(*l2));
 
128
}
 
129
 
 
130
 
 
131
int LogLineList::getItemCount() {
 
132
        return(list.count());
 
133
}
 
134
 
 
135
void LogLineList::clear() {
 
136
        QPtrListIterator<LogLine> it(list);
 
137
 
 
138
        LogLine* line=it.current();
 
139
        while (line!=NULL) {
 
140
                removedList.append(line);
 
141
                
 
142
                ++it;
 
143
                line=it.current();
 
144
        }
 
145
        
 
146
        list.clear();
 
147
 
 
148
        oldestLogLine=NULL;
 
149
}
 
150
 
 
151
 
 
152
LogLine* LogLineList::synchronize(View* view) {
 
153
        synchronizeRemovedLines(view);
 
154
        
 
155
        if (firstAdd==false && !addedList.isEmpty()) {
 
156
                synchronizeRecentLines();
 
157
        }
 
158
 
 
159
        LogLine* line=NULL;
 
160
        
 
161
        line=synchronizeAddedLines(view);
 
162
        
 
163
        kdDebug() << "Returning the added line..." << endl;
 
164
        
 
165
        return(line);
 
166
        
 
167
}
 
168
 
 
169
void LogLineList::synchronizeRemovedLines(View* view) {
 
170
        QPtrListIterator<LogLine> it(removedList);
 
171
        
 
172
        LogLine* line=it.current();
 
173
        
 
174
        kdDebug() << "Removing old items from list..." << endl;
 
175
        
 
176
        //We removed the old items
 
177
        while(line!=NULL) {
 
178
                if (line->itemExists())
 
179
                        line->removeItem(view->getLogList());
 
180
        
 
181
                //TODO Delete the line object
 
182
                
 
183
                ++it;
 
184
                line=it.current();
 
185
        }
 
186
        
 
187
        removedList.clear();
 
188
        
 
189
}
 
190
 
 
191
LogLine* LogLineList::synchronizeAddedLines(View* view) {
 
192
 
 
193
        kdDebug() << "Adding new items to the list..." << endl;
 
194
        
 
195
        //We add the new items
 
196
        QPtrListIterator<LogLine> it(addedList);
 
197
        
 
198
        LogLine* line=it.current();
 
199
        
 
200
        while (line!=NULL) {
 
201
                line->insertItem(view->getLogList());
 
202
                
 
203
                ++it;
 
204
                line=it.current();
 
205
        }
 
206
        
 
207
        kdDebug() << "Returning latest item..." << endl;
 
208
        
 
209
        //Get the last line of addedList list (can be NULL)
 
210
        line=addedList.last();
 
211
        
 
212
        addedList.clear();
 
213
 
 
214
        //Returns line, even if line is NULL (must be tested by the caller class)
 
215
        return(line);
 
216
}
 
217
 
 
218
void LogLineList::synchronizeRecentLines() {
 
219
        kdDebug() << "Recent items become normal..." << endl;
 
220
        
 
221
        //The older lines are no longer recent
 
222
        QPtrListIterator<LogLine> it(recentList);
 
223
        LogLine* line=it.current();
 
224
        
 
225
        while (line!=NULL) {
 
226
                //kdDebug() << "before setRecent... " << endl;
 
227
                //kdDebug() << "before setRecent original... : " << line->getOriginalFile() << endl;
 
228
                line->setRecent(false);
 
229
                //kdDebug() << "after setRecent..." << endl;
 
230
                
 
231
                ++it;
 
232
                line=it.current();
 
233
        }
 
234
        recentList.clear();
 
235
        
 
236
        kdDebug() << "New items become recent..." << endl;
 
237
        
 
238
        //The new lines added becomes recent
 
239
        it=addedList;
 
240
        line=it.current();
 
241
        while (line!=NULL) {
 
242
                line->setRecent(true);
 
243
                recentList.append(line);
 
244
                
 
245
                ++it;
 
246
                line=it.current();
 
247
        }
 
248
}
 
249
 
 
250
void LogLineList::setFirstReadPerformed(bool add) {
 
251
        firstAdd=!add;
 
252
}
 
253
 
 
254
void LogLineList::updateOldestLine() {
 
255
        
 
256
        QPtrListIterator<LogLine> it(list);
 
257
        if (it.current()==NULL)
 
258
                oldestLogLine=NULL;
 
259
        
 
260
        oldestLogLine=it.current();
 
261
        
 
262
        LogLine* line=it.current();
 
263
        while (line!=NULL) {
 
264
                if (line->isOlderThan(*oldestLogLine))
 
265
                        oldestLogLine=line;
 
266
                        
 
267
                ++it;
 
268
                line=it.current();
 
269
        }
 
270
 
 
271
}