tdeioslave/mbox

readmbox.cpp
1/*
2 * This is a simple tdeioslave to handle mbox-files.
3 * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl)
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include <config.h>
20
21#include "readmbox.h"
22
23#include "mbox.h"
24#include "urlinfo.h"
25
26#include <kdebug.h>
27#include <tdeio/global.h>
28
29#include <tqfile.h>
30#include <tqfileinfo.h>
31#include <tqstring.h>
32#include <tqtextstream.h>
33
34#ifdef HAVE_SYS_TYPES_H
35#include <sys/types.h>
36#endif
37
38#include <utime.h>
39
40ReadMBox::ReadMBox( const UrlInfo* info, MBoxProtocol* parent, bool onlynew, bool savetime )
41 : MBoxFile( info, parent ),
42 m_file( 0 ),
43 m_stream( 0 ),
44 m_current_line( new TQString( TQString() ) ),
45 m_current_id( new TQString( TQString() ) ),
46 m_atend( true ),
47 m_prev_time( 0 ),
48 m_only_new( onlynew ),
49 m_savetime( savetime ),
50 m_status( false ),
51 m_prev_status( false ),
52 m_header( true )
53
54{
55 if( m_info->type() == UrlInfo::invalid )
56 m_mbox->emitError( TDEIO::ERR_DOES_NOT_EXIST, info->url() );
57
58 if( !open( savetime ) )
59 m_mbox->emitError( TDEIO::ERR_CANNOT_OPEN_FOR_READING, info->url() );
60
61 if( m_info->type() == UrlInfo::message )
62 if( !searchMessage( m_info->id() ) )
63 m_mbox->emitError( TDEIO::ERR_DOES_NOT_EXIST, info->url() );
64}
65
67{
68 delete m_current_line;
69 close();
70}
71
72TQString ReadMBox::currentLine() const
73{
74 return *m_current_line;
75}
76
77TQString ReadMBox::currentID() const
78{
79 return *m_current_id;
80}
81
83{
84 if( !m_stream )
85 return true;
86
87 *m_current_line = m_stream->readLine();
88 m_atend = m_current_line->isNull();
89 if( m_atend ) // Cursor was at EOF
90 {
91 *m_current_id = TQString();
92 m_prev_status = m_status;
93 return true;
94 }
95
96 //New message
97 if( m_current_line->left( 5 ) == "From " )
98 {
99 *m_current_id = *m_current_line;
100 m_prev_status = m_status;
101 m_status = true;
102 m_header = true;
103 return true;
104 } else if( m_only_new )
105 {
106 if( m_header && m_current_line->left( 7 ) == "Status:" &&
107 ! m_current_line->contains( "U" ) && ! m_current_line->contains( "N" ) )
108 {
109 m_status = false;
110 }
111 }
112
113 if( m_current_line->stripWhiteSpace().isEmpty() )
114 m_header = false;
115
116 return false;
117}
118
119bool ReadMBox::searchMessage( const TQString& id )
120{
121 if( !m_stream )
122 return false;
123
124 while( !m_atend && *m_current_id != id )
125 nextLine();
126
127 return *m_current_id == id;
128}
129
131{
132 unsigned int result = m_current_line->length();
133
134 if( !m_stream )
135 return 0;
136
137 while( !nextLine() )
138 result += m_current_line->length();
139
140 return result;
141}
142
144{
145 if( !m_stream )
146 return; //Rewinding not possible
147
148 m_stream->device()->reset();
149 m_atend = m_stream->atEnd();
150}
151
152bool ReadMBox::atEnd() const
153{
154 if( !m_stream )
155 return true;
156
157 return m_atend || ( m_info->type() == UrlInfo::message && *m_current_id != m_info->id() );
158}
159
161{
162 return !m_only_new || m_prev_status;
163}
164
165bool ReadMBox::open( bool savetime )
166{
167 if( savetime )
168 {
169 TQFileInfo info( m_info->filename() );
170
171 m_prev_time = new utimbuf;
172 m_prev_time->actime = info.lastRead().toTime_t();
173 m_prev_time->modtime = info.lastModified().toTime_t();
174 }
175
176 if( m_file )
177 return false; //File already open
178
179 m_file = new TQFile( m_info->filename() );
180 if( !m_file->open( IO_ReadOnly ) )
181 {
182 delete m_file;
183 m_file = 0;
184 return false;
185 }
186 m_stream = new TQTextStream( m_file );
187 skipMessage();
188
189 return true;
190}
191
192void ReadMBox::close()
193{
194 if( !m_stream )
195 return;
196
197 delete m_stream; m_stream = 0;
198 m_file->close();
199 delete m_file; m_file = 0;
200
201 if( m_prev_time )
202 utime( TQFile::encodeName( m_info->filename() ), m_prev_time );
203}
204
This class can be used to lock files when implemented.
Definition: mboxfile.h:30
const UrlInfo *const m_info
This can be used to get information about the file.
Definition: mboxfile.h:61
MBoxProtocol *const m_mbox
A instance of the parent protocol, meant to throw errors if neccesairy.
Definition: mboxfile.h:66
This class is the main class and implements all function which can be called through the user.
Definition: mbox.h:32
void emitError(int errno, const TQString &arg)
Through this functions, other class which have an instance to this class (classes which are part of t...
Definition: mbox.cpp:163
~ReadMBox()
Destructor.
Definition: readmbox.cpp:66
bool nextLine()
This function reads the next line.
Definition: readmbox.cpp:82
ReadMBox(const UrlInfo *info, MBoxProtocol *parent, bool onlynew=false, bool savetime=false)
Constructor.
Definition: readmbox.cpp:40
TQString currentID() const
This function returns the current id.
Definition: readmbox.cpp:77
void rewind()
Sets the cursor back to the beginning of the file.
Definition: readmbox.cpp:143
bool inListing() const
Return true if the message is a new message, or all messages are listed.
Definition: readmbox.cpp:160
TQString currentLine() const
This functions return the current line.
Definition: readmbox.cpp:72
bool atEnd() const
Returns true if the cursor is at EOF.
Definition: readmbox.cpp:152
unsigned int skipMessage()
Skips all lines which belongs to the current message.
Definition: readmbox.cpp:130
bool searchMessage(const TQString &id)
This function search the file for a certain id.
Definition: readmbox.cpp:119