• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.14.10 API Reference
  • KDE Home
  • Contact Us
 

kioslave/mbox

  • kioslave
  • mbox
readmbox.cpp
1/*
2 * This is a simple kioslave 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 "readmbox.h"
20#include "mbox.h"
21#include "urlinfo.h"
22
23#include <kdebug.h>
24#include <kio/global.h>
25
26#include <QDateTime>
27#include <QFile>
28#include <QFileInfo>
29#include <QString>
30#include <QTextStream>
31
32#include <utime.h>
33
34ReadMBox::ReadMBox( const UrlInfo* info, MBoxProtocol* parent, bool onlynew, bool savetime )
35 : MBoxFile( info, parent ),
36 m_file( 0 ),
37 m_stream( 0 ),
38 m_atend( true ),
39 m_prev_time( 0 ),
40 m_only_new( onlynew ),
41 m_savetime( savetime ),
42 m_status( false ),
43 m_prev_status( false ),
44 m_header( true )
45{
46 if( m_info->type() == UrlInfo::invalid ) {
47 m_mbox->emitError( KIO::ERR_DOES_NOT_EXIST, info->url() );
48 }
49
50 if( !open( savetime ) ) {
51 m_mbox->emitError( KIO::ERR_CANNOT_OPEN_FOR_READING, info->url() );
52 }
53
54 if( m_info->type() == UrlInfo::message ) {
55 if( !searchMessage( m_info->id() ) ) {
56 m_mbox->emitError( KIO::ERR_DOES_NOT_EXIST, info->url() );
57 }
58 }
59}
60
61ReadMBox::~ReadMBox()
62{
63 close();
64}
65
66QString ReadMBox::currentLine() const
67{
68 return m_current_line;
69}
70
71QString ReadMBox::currentID() const
72{
73 return m_current_id;
74}
75
76bool ReadMBox::nextLine()
77{
78 if( !m_stream ) {
79 return true;
80 }
81
82 m_current_line = m_stream->readLine();
83 m_atend = m_current_line.isNull();
84 if( m_atend ) { // Cursor was at EOF
85 m_current_id.clear();
86 m_prev_status = m_status;
87 return true;
88 }
89
90 //New message
91 if( m_current_line.left( 5 ) == "From " ) {
92 m_current_id = m_current_line;
93 m_prev_status = m_status;
94 m_status = true;
95 m_header = true;
96 return true;
97 } else if( m_only_new ) {
98 if( m_header && m_current_line.left( 7 ) == "Status:" &&
99 ! m_current_line.contains( "U" ) && ! m_current_line.contains( "N" ) ) {
100 m_status = false;
101 }
102 }
103
104 if( m_current_line.trimmed().isEmpty() ) {
105 m_header = false;
106 }
107
108 return false;
109}
110
111bool ReadMBox::searchMessage( const QString& id )
112{
113 if( !m_stream ) {
114 return false;
115 }
116
117 while( !m_atend && m_current_id != id ) {
118 nextLine();
119 }
120
121 return m_current_id == id;
122}
123
124unsigned int ReadMBox::skipMessage()
125{
126 unsigned int result = m_current_line.length();
127
128 if( !m_stream ) {
129 return 0;
130 }
131
132 while( !nextLine() ) {
133 result += m_current_line.length();
134 }
135
136 return result;
137}
138
139void ReadMBox::rewind()
140{
141 if( !m_stream ) {
142 return;
143 }
144
145 m_stream->device()->reset();
146 m_atend = m_stream->atEnd();
147}
148
149bool ReadMBox::atEnd() const
150{
151 if( !m_stream )
152 return true;
153
154 return m_atend || ( m_info->type() == UrlInfo::message && m_current_id != m_info->id() );
155}
156
157bool ReadMBox::inListing() const
158{
159 return !m_only_new || m_prev_status;
160}
161
162bool ReadMBox::open( bool savetime )
163{
164 if( savetime ) {
165 QFileInfo info( m_info->filename() );
166
167 m_prev_time = new utimbuf;
168 m_prev_time->actime = info.lastRead().toTime_t();
169 m_prev_time->modtime = info.lastModified().toTime_t();
170 }
171
172 if( m_file ) {
173 return false; //File already open
174 }
175
176 m_file = new QFile( m_info->filename() );
177 if( !m_file->open( QIODevice::ReadOnly ) ) {
178 delete m_file;
179 m_file = 0;
180 return false;
181 }
182 m_stream = new QTextStream( m_file );
183 skipMessage();
184
185 return true;
186}
187
188void ReadMBox::close()
189{
190 if( !m_stream ) {
191 return;
192 }
193
194 delete m_stream; m_stream = 0;
195 m_file->close();
196 delete m_file; m_file = 0;
197
198 if( m_prev_time ) {
199 utime( QFile::encodeName( m_info->filename() ), m_prev_time );
200 delete m_prev_time; m_prev_time = 0;
201 }
202}
MBoxFile
This class can be used to lock files when implemented.
Definition: mboxfile.h:30
MBoxFile::m_info
const UrlInfo *const m_info
This can be used to get information about the file.
Definition: mboxfile.h:61
MBoxFile::m_mbox
MBoxProtocol *const m_mbox
A instance of the parent protocol, meant to throw errors if neccesairy.
Definition: mboxfile.h:66
MBoxProtocol
This class is the main class and implements all function which can be called through the user.
Definition: mbox.h:34
MBoxProtocol::emitError
void emitError(int _errno, const QString &arg)
Through this functions, other class which have an instance to this class (classes which are part of k...
Definition: mbox.cpp:155
ReadMBox::~ReadMBox
~ReadMBox()
Destructor.
Definition: readmbox.cpp:61
ReadMBox::nextLine
bool nextLine()
This function reads the next line.
Definition: readmbox.cpp:76
ReadMBox::ReadMBox
ReadMBox(const UrlInfo *info, MBoxProtocol *parent, bool onlynew=false, bool savetime=false)
Constructor.
Definition: readmbox.cpp:34
ReadMBox::currentLine
QString currentLine() const
This functions return the current line.
Definition: readmbox.cpp:66
ReadMBox::rewind
void rewind()
Sets the cursor back to the beginning of the file.
Definition: readmbox.cpp:139
ReadMBox::inListing
bool inListing() const
Return true if the message is a new message, or all messages are listed.
Definition: readmbox.cpp:157
ReadMBox::searchMessage
bool searchMessage(const QString &id)
This function search the file for a certain id.
Definition: readmbox.cpp:111
ReadMBox::atEnd
bool atEnd() const
Returns true if the cursor is at EOF.
Definition: readmbox.cpp:149
ReadMBox::skipMessage
unsigned int skipMessage()
Skips all lines which belongs to the current message.
Definition: readmbox.cpp:124
ReadMBox::currentID
QString currentID() const
This function returns the current id.
Definition: readmbox.cpp:71
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Thu Jul 21 2022 00:00:00 by doxygen 1.9.5 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kioslave/mbox

Skip menu "kioslave/mbox"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List

kdepimlibs-4.14.10 API Reference

Skip menu "kdepimlibs-4.14.10 API Reference"
  • akonadi
  •   contact
  •   kmime
  •   socialutils
  • kabc
  • kalarmcal
  • kblog
  • kcal
  • kcalcore
  • kcalutils
  • kholidays
  • kimap
  • kioslave
  •   imap4
  •   mbox
  •   nntp
  • kldap
  • kmbox
  • kmime
  • kontactinterface
  • kpimidentities
  • kpimtextedit
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • microblog
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal