summaryrefslogtreecommitdiffstats
path: root/tdeioslave/mbox/urlinfo.cc
blob: 4b359e40357e337b8082a9cbd5a576a08ccb8aba (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
/*
 * This is a simple tdeioslave 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 "urlinfo.h"

#include <kdebug.h>
#include <kurl.h>

#include <tqfileinfo.h>
#include <tqstring.h>

UrlInfo::UrlInfo( const KURL& url, const UrlType type )
	: m_type( invalid ),
	m_filename( new TQString ),
	m_id( new TQString )
{
	calculateInfo( url, type );
}

UrlInfo::~UrlInfo()
{
	delete m_filename;
	delete m_id;
}

TQString UrlInfo::mimetype() const
{
	switch( m_type )
	{
	case message:
		return "message/rfc822";
	case directory:
		return "inode/directory";
	case invalid:
	default:
		return "invalid";
	}
}

TQString UrlInfo::filename() const
{
	return *m_filename;
}

TQString UrlInfo::id() const
{
	return *m_id;
}

TQString UrlInfo::url() const
{
	return *m_filename + "/" + *m_id;
}


void UrlInfo::calculateInfo( const KURL& url, const UrlType type )
{
	bool found = false;

	if( !found && type & UrlInfo::message )
		found = isMessage( url );
	if( !found && type & UrlInfo::directory )
		found = isDirectory( url );
	if( !found )
	{
		m_type = invalid;
		*m_filename = "";
		*m_id = "";
	}
}

bool UrlInfo::isDirectory( const KURL& url )
{
	//Check is url is in the form mbox://{filename}
	TQString filename = url.path();
	TQFileInfo info;

	//Remove ending /
	while( filename.length() > 1 && filename.right( 1 ) == "/" )
		filename.remove( filename.length()-2, 1 );

	//Is this a directory?
	info.setFile( filename );
	if( !info.isFile() )
		return false;

	//Setting paramaters
	*m_filename = filename;
	*m_id = TQString();
	m_type = directory;
	kdDebug() << "urlInfo::isDirectory( " << url << " )" << endl;
	return true;
}

bool UrlInfo::isMessage( const KURL& url )
{
	TQString path = url.path();
	TQFileInfo info;
	int cutindex = path.findRev( '/' );
	
	//Does it contain at least one /?
	if( cutindex < 0 )
		return false;

	//Does the mbox-file exists?
	info.setFile( path.left( cutindex ) );
	if( !info.isFile() )
		return false;
	
	//Settings parameters
	kdDebug() << "urlInfo::isMessage( " << url << " )" << endl;
	m_type = message;
	*m_id = path.right( path.length() - cutindex - 1 );
	*m_filename = path.left( cutindex );
	
	return true;
}