tdeioslave/mbox

mbox.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 "mbox.h"
20 
21 #include "readmbox.h"
22 #include "stat.h"
23 #include "urlinfo.h"
24 
25 #include <tqstring.h>
26 #include <tqcstring.h>
27 
28 #include <kdebug.h>
29 #include <tdelocale.h>
30 #include <kinstance.h>
31 #include <tdeglobal.h>
32 #include <kurl.h>
33 #include <tdeio/global.h>
34 
35 #include <stdlib.h>
36 
37 #include <tdemacros.h>
38 
39 #include "mbox.h"
40 
41 extern "C" { TDE_EXPORT int kdemain(int argc, char* argv[]); }
42 
43 int kdemain( int argc, char * argv[] )
44 {
45  TDELocale::setMainCatalogue("tdelibs");
46  TDEInstance instance("tdeio_mbox");
47  (void) TDEGlobal::locale();
48 
49  if (argc != 4) {
50  fprintf(stderr, "Usage: tdeio_mbox protocol "
51  "domain-socket1 domain-socket2\n");
52  exit(-1);
53  }
54 
55  MBoxProtocol slave(argv[2], argv[3]);
56  slave.dispatchLoop();
57 
58  return 0;
59 }
60 
61 MBoxProtocol::MBoxProtocol( const TQCString& arg1, const TQCString& arg2 )
62  : TDEIO::SlaveBase( "mbox2", arg1, arg2 ),
63  m_errorState( true )
64 {
65 
66 }
67 
69 {
70 }
71 
72 void MBoxProtocol::get( const KURL& url )
73 {
74  m_errorState = false;
75 
76  UrlInfo info( url, UrlInfo::message );
77  TQString line;
78  TQByteArray ba_line;
79 
80  if( info.type() == UrlInfo::invalid && !m_errorState )
81  {
82  error( TDEIO::ERR_DOES_NOT_EXIST, info.url() );
83  return;
84  }
85 
86  ReadMBox mbox( &info, this );
87 
88  while( !mbox.atEnd() && !m_errorState)
89  {
90  line = mbox.currentLine();
91  line += '\n';
92  ba_line = TQCString( line.utf8() );
93  ba_line.truncate( ba_line.size() - 1 ); //Removing training '\0'
94  data( ba_line );
95  mbox.nextLine();
96  };
97 
98  if( !m_errorState )
99  {
100  data( TQByteArray() );
101  finished();
102  }
103 }
104 
105 void MBoxProtocol::listDir( const KURL& url )
106 {
107  m_errorState = false;
108 
109  TDEIO::UDSEntry entry;
110  UrlInfo info( url, UrlInfo::directory );
111  ReadMBox mbox( &info, this, hasMetaData( "onlynew" ), hasMetaData( "savetime" ) );
112 
113  if( m_errorState )
114  return;
115 
116  if( info.type() != UrlInfo::directory )
117  {
118  error( TDEIO::ERR_DOES_NOT_EXIST, info.url() );
119  return;
120  }
121 
122  while( !mbox.atEnd() && !m_errorState )
123  {
124  entry = Stat::stat( mbox, info );
125  if( mbox.inListing() )
126  listEntry( entry, false );
127  }
128 
129  listEntry( TDEIO::UDSEntry(), true );
130  finished();
131 }
132 
133 void MBoxProtocol::stat( const KURL& url )
134 {
135  UrlInfo info( url );
136  if( info.type() == UrlInfo::invalid )
137  {
138  error( TDEIO::ERR_DOES_NOT_EXIST, url.path() );
139  return;
140  } else
141  {
142  statEntry( Stat::stat( info ) );
143  }
144  finished();
145 }
146 
147 void MBoxProtocol::mimetype( const KURL& url )
148 {
149  m_errorState = false;
150 
151  UrlInfo info( url );
152 
153  if( m_errorState )
154  return;
155 
156  if( info.type() == UrlInfo::invalid )
157  error( TDEIO::ERR_DOES_NOT_EXIST, i18n( "Invalid URL" ) );
158  else
159  mimeType( info.mimetype() );
160  finished();
161 }
162 
163 void MBoxProtocol::emitError( int errno, const TQString& arg )
164 {
165  m_errorState = true;
166  error( errno, arg );
167 }
168 
This class is the main class and implements all function which can be called through the user.
Definition: mbox.h:32
virtual void stat(const KURL &url)
This functions gives general properties about a mbox-file, or mbox-email back.
Definition: mbox.cpp:133
virtual void get(const KURL &url)
This functions is used when an user or a program wants to get a file from a mbox-file.
Definition: mbox.cpp:72
MBoxProtocol(const TQCString &, const TQCString &)
Constructor, for the parameters, See TDEIO::SlaveBase.
Definition: mbox.cpp:61
virtual ~MBoxProtocol()
Empty destructor.
Definition: mbox.cpp:68
virtual void mimetype(const KURL &url)
This functions determinate the mimetype of a given mbox-file or mbox-email.
Definition: mbox.cpp:147
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
virtual void listDir(const KURL &url)
This functions gives a listing back.
Definition: mbox.cpp:105
This class handels reading from a mbox-file.
Definition: readmbox.h:37
bool nextLine()
This function reads the next line.
Definition: readmbox.cpp:82
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
static TDEIO::UDSEntry stat(const UrlInfo &info)
This functions gives information with a given UrlInfo.
Definition: stat.cpp:29