6 #include <unistd.h> // getpid()
10 #include <QTextStream>
15 #include <kstandarddirs.h>
16 #include <kiconloader.h>
17 #include <kcomponentdata.h>
22 InfoProtocol::InfoProtocol( const QByteArray
&pool
, const QByteArray
&app
)
23 : SlaveBase( "info", pool
, app
)
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
;
36 if ( m_perl
.isNull() ) {
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
);
47 kDebug( 7108 ) << "InfoProtocol::InfoProtocol - done";
50 InfoProtocol::~InfoProtocol()
52 kDebug( 7108 ) << "InfoProtocol::~InfoProtocol";
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();
64 KUrl
newUrl("info:/dir");
70 // some people write info://autoconf instead of info:/autoconf
71 if (!url
.host().isEmpty()) {
73 newURl
.setPath(url
.host()+url
.path());
74 newURl
.setHost(QString());
80 if ( url
.path().right(1) == "/" )
82 // Trailing / are not supported, so we need to remove them.
84 QString
newPath( url
.path() );
86 newUrl
.setPath( newPath
);
87 redirection( newUrl
);
92 mimeType("text/html");
93 // extract the path and node from 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
);
102 cmd
+= KShell::quoteArg(m_infoScript
);
104 cmd
+= KShell::quoteArg(m_infoConf
);
106 cmd
+= KShell::quoteArg(path
);
108 cmd
+= KShell::quoteArg(m_page
);
110 cmd
+= KShell::quoteArg(m_node
);
112 kDebug( 7108 ) << "cmd: " << cmd
;
114 FILE *file
= popen( QFile::encodeName(cmd
), "r" );
116 kDebug( 7108 ) << "InfoProtocol::get popen failed";
117 error( ERR_CANNOT_LAUNCH_PROCESS
, cmd
);
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
);
134 kDebug( 7108 ) << "InfoProtocol::get ERROR!";
140 data( QByteArray::fromRawData( buffer
, n
) );
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" );
160 kDebug( 7108 ) << "InfoProtocol::mimetype - done";
163 void InfoProtocol::decodeURL( const KUrl
&url
)
165 kDebug( 7108 ) << "InfoProtocol::decodeURL";
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 ;)
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";
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
200 // remove leading slash
201 if ('/' == path
[0]) {
202 path
= path
.mid( 1 );
204 //kDebug( 7108 ) << "Path: " << path;
206 int slashPos
= path
.indexOf( "/" );
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
& )
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
);
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();
247 fprintf(stderr
, "Usage: kio_info protocol domain-socket1 domain-socket2\n");
251 InfoProtocol
slave( argv
[2], argv
[3] );
252 slave
.dispatchLoop();