You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
321 lines
8.3 KiB
321 lines
8.3 KiB
15 years ago
|
/***************************************************************************
|
||
|
* Copyright (C) 2004-2007 by Georgy Yunaev, gyunaev@ulduzsoft.com *
|
||
|
* Please do not use email address above for bug reports; see *
|
||
|
* the README file *
|
||
|
* *
|
||
|
* This program is free software; you can redistribute it and/or modify *
|
||
|
* it under the terms of the GNU General Public License as published by *
|
||
|
* the Free Software Foundation; either version 2 of the License, or *
|
||
|
* (at your option) any later version. *
|
||
|
* *
|
||
|
* This program 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 General Public License for more details. *
|
||
|
* *
|
||
|
* You should have received a copy of the GNU General Public License *
|
||
|
* along with this program; if not, write to the *
|
||
|
* Free Software Foundation, Inc., *
|
||
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
|
||
|
***************************************************************************/
|
||
|
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <sys/stat.h>
|
||
|
|
||
12 years ago
|
#include <tdeapplication.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
#include <kinstance.h>
|
||
|
#include <kurl.h>
|
||
|
#include <kmimemagic.h>
|
||
|
|
||
13 years ago
|
#include <tqfile.h>
|
||
|
#include <tqbitarray.h>
|
||
|
#include <tqvaluevector.h>
|
||
15 years ago
|
|
||
|
#include "msits.h"
|
||
|
#include "libchmurlfactory.h"
|
||
|
|
||
12 years ago
|
using namespace TDEIO;
|
||
15 years ago
|
|
||
|
extern "C"
|
||
|
{
|
||
|
int kdemain( int argc, char **argv )
|
||
|
{
|
||
12 years ago
|
kdDebug() << "*** tdeio_msits Init" << endl;
|
||
15 years ago
|
|
||
12 years ago
|
TDEInstance instance( "tdeio_msits" );
|
||
15 years ago
|
|
||
|
if ( argc != 4 )
|
||
|
{
|
||
12 years ago
|
kdDebug() << "Usage: tdeio_msits protocol domain-socket1 domain-socket2" << endl;
|
||
15 years ago
|
exit (-1);
|
||
|
}
|
||
|
|
||
|
ProtocolMSITS slave ( argv[2], argv[3] );
|
||
|
slave.dispatchLoop();
|
||
|
|
||
12 years ago
|
kdDebug() << "*** tdeio_msits Done" << endl;
|
||
15 years ago
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
ProtocolMSITS::ProtocolMSITS (const TQCString &pool_socket, const TQCString &app_socket)
|
||
12 years ago
|
: SlaveBase ("tdeio_msits", pool_socket, app_socket)
|
||
15 years ago
|
{
|
||
|
m_chmFile = 0;
|
||
|
}
|
||
|
|
||
|
ProtocolMSITS::~ProtocolMSITS()
|
||
|
{
|
||
|
if ( !m_chmFile )
|
||
|
return;
|
||
|
|
||
|
chm_close (m_chmFile);
|
||
|
m_chmFile = 0;
|
||
|
}
|
||
|
|
||
|
// A simple stat() wrapper
|
||
13 years ago
|
static bool isDirectory ( const TQString & filename )
|
||
15 years ago
|
{
|
||
|
return filename[filename.length() - 1] == '/';
|
||
|
}
|
||
|
|
||
|
|
||
|
void ProtocolMSITS::get( const KURL& url )
|
||
|
{
|
||
13 years ago
|
TQString htmdata, fileName;
|
||
15 years ago
|
chmUnitInfo ui;
|
||
13 years ago
|
TQByteArray buf;
|
||
15 years ago
|
|
||
12 years ago
|
kdDebug() << "tdeio_msits::get() " << url.path() << endl;
|
||
15 years ago
|
|
||
|
if ( !parseLoadAndLookup ( url, fileName ) )
|
||
|
return; // error() has been called by parseLoadAndLookup
|
||
|
|
||
12 years ago
|
kdDebug() << "tdeio_msits::get: parseLoadAndLookup returned " << fileName << endl;
|
||
15 years ago
|
|
||
|
if ( LCHMUrlFactory::handleFileType( url.path(), htmdata ) )
|
||
|
{
|
||
|
buf = htmdata.utf8();
|
||
|
kdDebug() << "Using special handling for image pages: " << htmdata << endl;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if ( isDirectory (fileName) )
|
||
|
{
|
||
12 years ago
|
error( TDEIO::ERR_IS_DIRECTORY, url.prettyURL() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
if ( !ResolveObject ( fileName, &ui) )
|
||
|
{
|
||
12 years ago
|
kdDebug() << "tdeio_msits::get: could not resolve filename " << fileName << endl;
|
||
12 years ago
|
error( TDEIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
buf.resize( ui.length );
|
||
|
|
||
|
if ( RetrieveObject (&ui, (unsigned char*) buf.data(), 0, ui.length) == 0 )
|
||
|
{
|
||
12 years ago
|
kdDebug() << "tdeio_msits::get: could not retrieve filename " << fileName << endl;
|
||
12 years ago
|
error( TDEIO::ERR_NO_CONTENT, url.prettyURL() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
totalSize( buf.size() );
|
||
|
KMimeMagicResult * result = KMimeMagic::self()->findBufferFileType( buf, fileName );
|
||
|
kdDebug() << "Emitting mimetype " << result->mimeType() << endl;
|
||
|
|
||
|
mimeType( result->mimeType() );
|
||
|
data( buf );
|
||
|
processedSize( buf.size() );
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
|
|
||
13 years ago
|
bool ProtocolMSITS::parseLoadAndLookup ( const KURL& url, TQString& abspath )
|
||
15 years ago
|
{
|
||
|
kdDebug() << "ProtocolMSITS::parseLoadAndLookup (const KURL&) " << url.path() << endl;
|
||
|
|
||
13 years ago
|
int pos = url.path().find ("::");
|
||
15 years ago
|
|
||
|
if ( pos == -1 )
|
||
|
{
|
||
12 years ago
|
error( TDEIO::ERR_MALFORMED_URL, url.prettyURL() );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
13 years ago
|
TQString filename = url.path().left (pos);
|
||
15 years ago
|
abspath = url.path().mid (pos + 2); // skip ::
|
||
|
|
||
|
// Some buggy apps add ms-its:/ to the path as well
|
||
|
if ( abspath.startsWith( "ms-its:" ) )
|
||
|
abspath = abspath.mid( 7 );
|
||
|
|
||
|
kdDebug() << "ProtocolMSITS::parseLoadAndLookup: filename " << filename << ", path " << abspath << endl;
|
||
|
|
||
|
if ( filename.isEmpty() )
|
||
|
{
|
||
12 years ago
|
error( TDEIO::ERR_MALFORMED_URL, url.prettyURL() );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
// If the file has been already loaded, nothing to do.
|
||
|
if ( m_chmFile && filename == m_openedFile )
|
||
|
return true;
|
||
|
|
||
|
kdDebug() << "Opening a new CHM file " << filename << endl;
|
||
|
|
||
|
// First try to open a temporary file
|
||
|
chmFile * tmpchm;
|
||
|
|
||
13 years ago
|
if ( (tmpchm = chm_open ( TQFile::encodeName (filename))) == 0 )
|
||
15 years ago
|
{
|
||
12 years ago
|
error( TDEIO::ERR_COULD_NOT_READ, url.prettyURL() );
|
||
15 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
// Replace an existing file by a new one
|
||
|
if ( m_chmFile )
|
||
|
chm_close (m_chmFile);
|
||
|
|
||
|
m_chmFile = tmpchm;
|
||
|
m_openedFile = filename;
|
||
|
|
||
|
kdDebug() << "A CHM file " << filename << " has beed opened successfully" << endl;
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/*
|
||
11 years ago
|
* Shamelessly stolen from a KDE TDEIO tutorial
|
||
15 years ago
|
*/
|
||
13 years ago
|
static void app_entry(UDSEntry& e, unsigned int uds, const TQString& str)
|
||
15 years ago
|
{
|
||
|
UDSAtom a;
|
||
|
a.m_uds = uds;
|
||
|
a.m_str = str;
|
||
|
e.append(a);
|
||
|
}
|
||
|
|
||
|
// appends an int with the UDS-ID uds
|
||
|
static void app_entry(UDSEntry& e, unsigned int uds, long l)
|
||
|
{
|
||
|
UDSAtom a;
|
||
|
a.m_uds = uds;
|
||
|
a.m_long = l;
|
||
|
e.append(a);
|
||
|
}
|
||
|
|
||
|
// internal function
|
||
|
// fills a directory item with its name and size
|
||
13 years ago
|
static void app_dir(UDSEntry& e, const TQString & name)
|
||
15 years ago
|
{
|
||
|
e.clear();
|
||
12 years ago
|
app_entry(e, TDEIO::UDS_NAME, name);
|
||
|
app_entry(e, TDEIO::UDS_FILE_TYPE, S_IFDIR);
|
||
|
app_entry(e, TDEIO::UDS_SIZE, 1);
|
||
15 years ago
|
}
|
||
|
|
||
|
// internal function
|
||
|
// fills a file item with its name and size
|
||
13 years ago
|
static void app_file(UDSEntry& e, const TQString & name, size_t size)
|
||
15 years ago
|
{
|
||
|
e.clear();
|
||
12 years ago
|
app_entry(e, TDEIO::UDS_NAME, name);
|
||
|
app_entry(e, TDEIO::UDS_FILE_TYPE, S_IFREG);
|
||
|
app_entry(e, TDEIO::UDS_SIZE, size);
|
||
15 years ago
|
}
|
||
|
|
||
|
void ProtocolMSITS::stat (const KURL & url)
|
||
|
{
|
||
13 years ago
|
TQString fileName;
|
||
15 years ago
|
chmUnitInfo ui;
|
||
|
|
||
12 years ago
|
kdDebug() << "tdeio_msits::stat (const KURL& url) " << url.path() << endl;
|
||
15 years ago
|
|
||
|
if ( !parseLoadAndLookup ( url, fileName ) )
|
||
|
return; // error() has been called by parseLoadAndLookup
|
||
|
|
||
|
if ( !ResolveObject ( fileName, &ui ) )
|
||
|
{
|
||
12 years ago
|
error( TDEIO::ERR_DOES_NOT_EXIST, url.prettyURL() );
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
kdDebug() << "tdeio_msits::stat: adding an entry for " << fileName << endl;
|
||
15 years ago
|
UDSEntry entry;
|
||
|
|
||
|
if ( isDirectory ( fileName ) )
|
||
|
app_dir(entry, fileName);
|
||
|
else
|
||
|
app_file(entry, fileName, ui.length);
|
||
|
|
||
|
statEntry (entry);
|
||
|
|
||
|
finished();
|
||
|
}
|
||
|
|
||
|
|
||
|
// A local CHMLIB enumerator
|
||
|
static int chmlib_enumerator (struct chmFile *, struct chmUnitInfo *ui, void *context)
|
||
|
{
|
||
13 years ago
|
((TQValueVector<TQString> *) context)->push_back (TQString::fromLocal8Bit (ui->path));
|
||
15 years ago
|
return CHM_ENUMERATOR_CONTINUE;
|
||
|
}
|
||
|
|
||
|
|
||
|
void ProtocolMSITS::listDir (const KURL & url)
|
||
|
{
|
||
13 years ago
|
TQString filepath;
|
||
15 years ago
|
|
||
12 years ago
|
kdDebug() << "tdeio_msits::listDir (const KURL& url) " << url.path() << endl;
|
||
15 years ago
|
|
||
|
if ( !parseLoadAndLookup ( url, filepath ) )
|
||
|
return; // error() has been called by parseLoadAndLookup
|
||
|
|
||
|
filepath += "/";
|
||
|
|
||
|
if ( !isDirectory (filepath) )
|
||
|
{
|
||
12 years ago
|
error(TDEIO::ERR_CANNOT_ENTER_DIRECTORY, url.path());
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
kdDebug() << "tdeio_msits::listDir: enumerating directory " << filepath << endl;
|
||
15 years ago
|
|
||
13 years ago
|
TQValueVector<TQString> listing;
|
||
15 years ago
|
|
||
|
if ( chm_enumerate_dir ( m_chmFile,
|
||
|
filepath.local8Bit(),
|
||
|
CHM_ENUMERATE_NORMAL | CHM_ENUMERATE_FILES | CHM_ENUMERATE_DIRS,
|
||
|
chmlib_enumerator,
|
||
|
&listing ) != 1 )
|
||
|
{
|
||
12 years ago
|
error(TDEIO::ERR_CANNOT_ENTER_DIRECTORY, url.path());
|
||
15 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
UDSEntry entry;
|
||
|
unsigned int striplength = filepath.length();
|
||
|
|
||
|
for ( unsigned int i = 0; i < listing.size(); i++ )
|
||
|
{
|
||
|
// Strip the direcroty name
|
||
13 years ago
|
TQString ename = listing[i].mid (striplength);
|
||
15 years ago
|
|
||
|
if ( isDirectory ( ename ) )
|
||
|
app_dir(entry, ename);
|
||
|
else
|
||
|
app_file(entry, ename, 0);
|
||
|
|
||
|
listEntry(entry, false);
|
||
|
}
|
||
|
|
||
|
listEntry(entry, true);
|
||
|
finished();
|
||
|
}
|