summaryrefslogtreecommitdiffstats
path: root/kioslaves/mbox/mbox.cc
blob: 1828401b6a0761f0bc2c96b008ad0b7b18e27815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * This is a simple kioslave to handle mbox-files.
 * Copyright (C) 2004 Mart Kelder (mart.kde@hccnet.nl)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
#include "mbox.h"

#include "readmbox.h"
#include "stat.h"
#include "urlinfo.h"

#include <tqstring.h>
#include <tqcstring.h>

#include <kdebug.h>
#include <klocale.h>
#include <kinstance.h>
#include <kglobal.h>
#include <kurl.h>
#include <kio/global.h>

#include <stdlib.h>

#include "kdepimmacros.h"

#include "mbox.h"

extern "C" { KDE_EXPORT int kdemain(int argc, char* argv[]); }

int kdemain( int argc, char * argv[] )
{
	KLocale::setMainCatalogue("kdelibs");
	KInstance instance("kio_mbox");
	(void) KGlobal::locale();

	if (argc != 4) {
		fprintf(stderr, "Usage: kio_mbox protocol "
	        	        "domain-socket1 domain-socket2\n");
		exit(-1);
	}

	MBoxProtocol slave(argv[2], argv[3]);
	slave.dispatchLoop();
	
	return 0;
}

MBoxProtocol::MBoxProtocol( const TQCString& arg1, const TQCString& arg2 )
	: KIO::SlaveBase( "mbox2", arg1, arg2 ),
	m_errorState( true )
{
	
}

MBoxProtocol::~MBoxProtocol()
{
}

void MBoxProtocol::get( const KURL& url )
{
	m_errorState = false;
	
	UrlInfo info( url, UrlInfo::message );
	TQString line;
	TQByteArray ba_line;

	if( info.type() == UrlInfo::invalid && !m_errorState )
	{
		error( KIO::ERR_DOES_NOT_EXIST, info.url() );
		return;
	}
	
	ReadMBox mbox( &info, this );

	while( !mbox.atEnd() && !m_errorState)
	{
		line = mbox.currentLine();
		line += '\n';
		ba_line = TQCString( line.utf8() );
		ba_line.truncate( ba_line.size() - 1 ); //Removing training '\0'
		data( ba_line );
		mbox.nextLine();
	};
	
	if( !m_errorState )
	{
		data( TQByteArray() );
		finished();
	}
}

void MBoxProtocol::listDir( const KURL& url )
{
	m_errorState = false;
	
	KIO::UDSEntry entry;
	UrlInfo info( url, UrlInfo::directory );
	ReadMBox mbox( &info, this, hasMetaData( "onlynew" ), hasMetaData( "savetime" ) );

	if( m_errorState )
		return;
	
	if( info.type() != UrlInfo::directory )
	{
		error( KIO::ERR_DOES_NOT_EXIST, info.url() );
		return;
	}
	
	while( !mbox.atEnd() && !m_errorState )
	{
		entry = Stat::stat( mbox, info );
		if( mbox.inListing() )
			listEntry( entry, false );
	}

	listEntry( KIO::UDSEntry(), true );
	finished();
}

void MBoxProtocol::stat( const KURL& url )
{
	UrlInfo info( url );
	if( info.type() == UrlInfo::invalid )
	{
		error( KIO::ERR_DOES_NOT_EXIST, url.path() );
		return;
	} else
	{
		statEntry( Stat::stat( info ) );
	}
	finished();
}

void MBoxProtocol::mimetype( const KURL& url )
{	
	m_errorState = false;
	
	UrlInfo info( url );

	if( m_errorState )
		return;
	
	if( info.type() == UrlInfo::invalid )
		error( KIO::ERR_DOES_NOT_EXIST, i18n( "Invalid URL" ) );
	else
		mimeType( info.mimetype() );
	finished();
}

void MBoxProtocol::emitError( int errno, const TQString& arg )
{
	m_errorState = true;
	error( errno, arg );
}