tdeioslave/mbox

urlinfo.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 "urlinfo.h"
20 
21 #include <kdebug.h>
22 #include <kurl.h>
23 
24 #include <tqfileinfo.h>
25 #include <tqstring.h>
26 
27 UrlInfo::UrlInfo( const KURL& url, const UrlType type )
28  : m_type( invalid ),
29  m_filename( new TQString ),
30  m_id( new TQString )
31 {
32  calculateInfo( url, type );
33 }
34 
35 UrlInfo::~UrlInfo()
36 {
37  delete m_filename;
38  delete m_id;
39 }
40 
41 TQString UrlInfo::mimetype() const
42 {
43  switch( m_type )
44  {
45  case message:
46  return "message/rfc822";
47  case directory:
48  return "inode/directory";
49  case invalid:
50  default:
51  return "invalid";
52  }
53 }
54 
55 TQString UrlInfo::filename() const
56 {
57  return *m_filename;
58 }
59 
60 TQString UrlInfo::id() const
61 {
62  return *m_id;
63 }
64 
65 TQString UrlInfo::url() const
66 {
67  return *m_filename + "/" + *m_id;
68 }
69 
70 
71 void UrlInfo::calculateInfo( const KURL& url, const UrlType type )
72 {
73  bool found = false;
74 
75  if( !found && type & UrlInfo::message )
76  found = isMessage( url );
77  if( !found && type & UrlInfo::directory )
78  found = isDirectory( url );
79  if( !found )
80  {
81  m_type = invalid;
82  *m_filename = "";
83  *m_id = "";
84  }
85 }
86 
87 bool UrlInfo::isDirectory( const KURL& url )
88 {
89  //Check is url is in the form mbox://{filename}
90  TQString filename = url.path();
91  TQFileInfo info;
92 
93  //Remove ending /
94  while( filename.length() > 1 && filename.right( 1 ) == "/" )
95  filename.remove( filename.length()-2, 1 );
96 
97  //Is this a directory?
98  info.setFile( filename );
99  if( !info.isFile() )
100  return false;
101 
102  //Setting paramaters
103  *m_filename = filename;
104  *m_id = TQString();
105  m_type = directory;
106  kdDebug() << "urlInfo::isDirectory( " << url << " )" << endl;
107  return true;
108 }
109 
110 bool UrlInfo::isMessage( const KURL& url )
111 {
112  TQString path = url.path();
113  TQFileInfo info;
114  int cutindex = path.findRev( '/' );
115 
116  //Does it contain at least one /?
117  if( cutindex < 0 )
118  return false;
119 
120  //Does the mbox-file exists?
121  info.setFile( path.left( cutindex ) );
122  if( !info.isFile() )
123  return false;
124 
125  //Settings parameters
126  kdDebug() << "urlInfo::isMessage( " << url << " )" << endl;
127  m_type = message;
128  *m_id = path.right( path.length() - cutindex - 1 );
129  *m_filename = path.left( cutindex );
130 
131  return true;
132 }
133