• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdepimlibs-4.10.5 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 
34 ReadMBox::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 {
47  if( m_info->type() == UrlInfo::invalid )
48  m_mbox->emitError( KIO::ERR_DOES_NOT_EXIST, info->url() );
49 
50  if( !open( savetime ) )
51  m_mbox->emitError( KIO::ERR_CANNOT_OPEN_FOR_READING, info->url() );
52 
53  if( m_info->type() == UrlInfo::message )
54  if( !searchMessage( m_info->id() ) )
55  m_mbox->emitError( KIO::ERR_DOES_NOT_EXIST, info->url() );
56 }
57 
58 ReadMBox::~ReadMBox()
59 {
60  close();
61 }
62 
63 QString ReadMBox::currentLine() const
64 {
65  return m_current_line;
66 }
67 
68 QString ReadMBox::currentID() const
69 {
70  return m_current_id;
71 }
72 
73 bool ReadMBox::nextLine()
74 {
75  if( !m_stream )
76  return true;
77 
78  m_current_line = m_stream->readLine();
79  m_atend = m_current_line.isNull();
80  if( m_atend ) // Cursor was at EOF
81  {
82  m_current_id.clear();
83  m_prev_status = m_status;
84  return true;
85  }
86 
87  //New message
88  if( m_current_line.left( 5 ) == "From " )
89  {
90  m_current_id = m_current_line;
91  m_prev_status = m_status;
92  m_status = true;
93  m_header = true;
94  return true;
95  } else if( m_only_new )
96  {
97  if( m_header && m_current_line.left( 7 ) == "Status:" &&
98  ! m_current_line.contains( "U" ) && ! m_current_line.contains( "N" ) )
99  {
100  m_status = false;
101  }
102  }
103 
104  if( m_current_line.trimmed().isEmpty() )
105  m_header = false;
106 
107  return false;
108 }
109 
110 bool ReadMBox::searchMessage( const QString& id )
111 {
112  if( !m_stream )
113  return false;
114 
115  while( !m_atend && m_current_id != id )
116  nextLine();
117 
118  return m_current_id == id;
119 }
120 
121 unsigned int ReadMBox::skipMessage()
122 {
123  unsigned int result = m_current_line.length();
124 
125  if( !m_stream )
126  return 0;
127 
128  while( !nextLine() )
129  result += m_current_line.length();
130 
131  return result;
132 }
133 
134 void ReadMBox::rewind()
135 {
136  if( !m_stream )
137  return;
138 
139  m_stream->device()->reset();
140  m_atend = m_stream->atEnd();
141 }
142 
143 bool ReadMBox::atEnd() const
144 {
145  if( !m_stream )
146  return true;
147 
148  return m_atend || ( m_info->type() == UrlInfo::message && m_current_id != m_info->id() );
149 }
150 
151 bool ReadMBox::inListing() const
152 {
153  return !m_only_new || m_prev_status;
154 }
155 
156 bool ReadMBox::open( bool savetime )
157 {
158  if( savetime )
159  {
160  QFileInfo info( m_info->filename() );
161 
162  m_prev_time = new utimbuf;
163  m_prev_time->actime = info.lastRead().toTime_t();
164  m_prev_time->modtime = info.lastModified().toTime_t();
165  }
166 
167  if( m_file )
168  return false; //File already open
169 
170  m_file = new QFile( m_info->filename() );
171  if( !m_file->open( QIODevice::ReadOnly ) )
172  {
173  delete m_file;
174  m_file = 0;
175  return false;
176  }
177  m_stream = new QTextStream( m_file );
178  skipMessage();
179 
180  return true;
181 }
182 
183 void ReadMBox::close()
184 {
185  if( !m_stream )
186  return;
187 
188  delete m_stream; m_stream = 0;
189  m_file->close();
190  delete m_file; m_file = 0;
191 
192  if( m_prev_time )
193  {
194  utime( QFile::encodeName( m_info->filename() ), m_prev_time );
195  delete m_prev_time; m_prev_time = 0;
196  }
197 }
198 
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Sat Jul 13 2013 01:25:32 by doxygen 1.8.3.1 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.10.5 API Reference

Skip menu "kdepimlibs-4.10.5 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