add more spacing
[personal-kdebase.git] / runtime / kioslave / info / info.cc
blobc23ba2b06f4f4bb16c7421155af3bfd0e97ef9a5
1 #include "info.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <sys/stat.h>
6 #include <unistd.h> // getpid()
8 #include <QDir>
9 #include <QFile>
10 #include <QTextStream>
11 #include <QRegExp>
13 #include <kdebug.h>
14 #include <kshell.h>
15 #include <kstandarddirs.h>
16 #include <kiconloader.h>
17 #include <kcomponentdata.h>
18 #include <klocale.h>
20 using namespace KIO;
22 InfoProtocol::InfoProtocol( const QByteArray &pool, const QByteArray &app )
23 : SlaveBase( "info", pool, app )
24 , m_page( "" )
25 , m_node( "" )
27 kDebug( 7108 ) << "InfoProtocol::InfoProtocol";
28 m_iconLoader = new KIconLoader(KGlobal::mainComponent().componentName(), KGlobal::mainComponent().dirs());
29 m_perl = KGlobal::dirs()->findExe( "perl" );
30 m_infoScript = KStandardDirs::locate( "data", "kio_info/kde-info2html" );
31 m_infoConf = KStandardDirs::locate("data", "kio_info/kde-info2html.conf");
33 if( m_perl.isNull() || m_infoScript.isNull() || m_infoConf.isNull() ) {
34 kError( 7108 ) << "Critical error: Cannot locate files for HTML-conversion" << endl;
35 QString errorStr;
36 if ( m_perl.isNull() ) {
37 errorStr = "perl.";
38 } else {
39 QString missing =m_infoScript.isNull() ? "kio_info/kde-info2html" : "kio_info/kde-info2html.conf";
40 errorStr = "kde-info2html" + i18n( "\nUnable to locate file %1 which is necessary to run this service. "
41 "Please check your software installation" , missing );
43 error( KIO::ERR_CANNOT_LAUNCH_PROCESS, errorStr );
44 exit();
47 kDebug( 7108 ) << "InfoProtocol::InfoProtocol - done";
50 InfoProtocol::~InfoProtocol()
52 kDebug( 7108 ) << "InfoProtocol::~InfoProtocol";
53 delete m_iconLoader;
54 kDebug( 7108 ) << "InfoProtocol::~InfoProtocol - done";
57 void InfoProtocol::get( const KUrl& url )
59 kDebug( 7108 ) << "InfoProtocol::get";
60 kDebug( 7108 ) << "URL: " << url.prettyUrl() << " , Path :" << url.path();
62 if (url.path()=="/")
64 KUrl newUrl("info:/dir");
65 redirection(newUrl);
66 finished();
67 return;
70 // some people write info://autoconf instead of info:/autoconf
71 if (!url.host().isEmpty()) {
72 KUrl newURl(url);
73 newURl.setPath(url.host()+url.path());
74 newURl.setHost(QString());
75 redirection(newURl);
76 finished();
77 return;
80 if ( url.path().right(1) == "/" )
82 // Trailing / are not supported, so we need to remove them.
83 KUrl newUrl( url );
84 QString newPath( url.path() );
85 newPath.chop( 1 );
86 newUrl.setPath( newPath );
87 redirection( newUrl );
88 finished();
89 return;
92 mimeType("text/html");
93 // extract the path and node from url
94 decodeURL( url );
96 QString path = m_iconLoader->iconPath("go-up", KIconLoader::Toolbar, true);
97 int revindex = path.lastIndexOf('/');
98 path = path.left(revindex);
100 QString cmd = KShell::quoteArg(m_perl);
101 cmd += ' ';
102 cmd += KShell::quoteArg(m_infoScript);
103 cmd += ' ';
104 cmd += KShell::quoteArg(m_infoConf);
105 cmd += ' ';
106 cmd += KShell::quoteArg(path);
107 cmd += ' ';
108 cmd += KShell::quoteArg(m_page);
109 cmd += ' ';
110 cmd += KShell::quoteArg(m_node);
112 kDebug( 7108 ) << "cmd: " << cmd;
114 FILE *file = popen( QFile::encodeName(cmd), "r" );
115 if ( !file ) {
116 kDebug( 7108 ) << "InfoProtocol::get popen failed";
117 error( ERR_CANNOT_LAUNCH_PROCESS, cmd );
118 return;
121 char buffer[ 4096 ];
123 bool empty = true;
124 while ( !feof( file ) )
126 int n = fread( buffer, 1, sizeof( buffer ), file );
127 if ( !n && feof( file ) && empty ) {
128 error( ERR_CANNOT_LAUNCH_PROCESS, cmd );
129 return;
131 if ( n < 0 )
133 // ERROR
134 kDebug( 7108 ) << "InfoProtocol::get ERROR!";
135 pclose( file );
136 return;
139 empty = false;
140 data( QByteArray::fromRawData( buffer, n ) );
143 pclose( file );
145 finished();
147 kDebug( 7108 ) << "InfoProtocol::get - done";
150 void InfoProtocol::mimetype( const KUrl& /* url */ )
152 kDebug( 7108 ) << "InfoProtocol::mimetype";
154 // to get rid of those "Open with" dialogs...
155 mimeType( "text/html" );
157 // finish action
158 finished();
160 kDebug( 7108 ) << "InfoProtocol::mimetype - done";
163 void InfoProtocol::decodeURL( const KUrl &url )
165 kDebug( 7108 ) << "InfoProtocol::decodeURL";
167 /* Notes:
169 * I cleaned up the URL decoding and chose not to support URLs in the
170 * form "info:/usr/local/share/info/libc.info.gz" or similar which the
171 * older code attempted (and failed, maybe it had worked once) to do.
173 * The reason is that an obvious use such as viewing a info file off your
174 * infopath would work for the first page, but then all the links would be
175 * wrong. Of course, one could change kde-info2html to make it work, but I don't
176 * think it worthy, others are free to disagree and write the necessary code ;)
178 * luis pedro
181 if ( url == KUrl( "info:/browse_by_file?special=yes" ) ) {
182 m_page = "#special#";
183 m_node = "browse_by_file";
184 kDebug( 7108 ) << "InfoProtocol::decodeURL - special - browse by file";
185 return;
188 decodePath( url.path() );
190 kDebug( 7108 ) << "InfoProtocol::decodeURL - done";
193 void InfoProtocol::decodePath( QString path )
195 kDebug( 7108 ) << "InfoProtocol::decodePath(-" <<path<<"-)";
197 m_page = "dir"; //default
198 m_node = "";
200 // remove leading slash
201 if ('/' == path[0]) {
202 path = path.mid( 1 );
204 //kDebug( 7108 ) << "Path: " << path;
206 int slashPos = path.indexOf( "/" );
208 if( slashPos < 0 )
210 m_page = path;
211 m_node = "Top";
212 return;
215 m_page = path.left( slashPos );
217 // remove leading+trailing whitespace
218 m_node = path.right( path.length() - slashPos - 1).trimmed ();
220 kDebug( 7108 ) << "InfoProtocol::decodePath - done";
223 // A minimalistic stat with only the file type
224 // This seems to be enough for konqueror
225 void InfoProtocol::stat( const KUrl & )
227 UDSEntry uds_entry;
229 // Regular file with rwx permission for all
230 uds_entry.insert( KIO::UDSEntry::UDS_FILE_TYPE, S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO );
232 statEntry( uds_entry );
234 finished();
237 extern "C" { int KDE_EXPORT kdemain( int argc, char **argv ); }
239 int kdemain( int argc, char **argv )
241 KComponentData componentData( "kio_info" );
243 kDebug() << "kio_info starting " << getpid();
245 if (argc != 4)
247 fprintf(stderr, "Usage: kio_info protocol domain-socket1 domain-socket2\n");
248 exit(-1);
251 InfoProtocol slave( argv[2], argv[3] );
252 slave.dispatchLoop();
254 return 0;