00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include "kbookmarkimporter_kde1.h"
00022
#include <kfiledialog.h>
00023
#include <kstringhandler.h>
00024
#include <klocale.h>
00025
#include <kdebug.h>
00026
#include <kcharsets.h>
00027
#include <qtextcodec.h>
00028
00029
#include <sys/types.h>
00030
#include <stddef.h>
00031
#include <dirent.h>
00032
#include <sys/stat.h>
00033
#include <assert.h>
00034
00036
00037
void KBookmarkImporter::import(
const QString & path )
00038 {
00039
QDomElement elem = m_pDoc->documentElement();
00040 Q_ASSERT(!elem.isNull());
00041 scanIntern( elem, path );
00042 }
00043
00044
void KBookmarkImporter::scanIntern(
QDomElement & parentElem,
const QString & _path )
00045 {
00046
kdDebug(7043) <<
"KBookmarkImporter::scanIntern " << _path <<
endl;
00047
00048
QDir dir( _path );
00049
QString canonical = dir.canonicalPath();
00050
00051
if ( m_lstParsedDirs.contains(canonical) )
00052 {
00053
kdWarning() <<
"Directory " << canonical <<
" already parsed" <<
endl;
00054
return;
00055 }
00056
00057 m_lstParsedDirs.append( canonical );
00058
00059 DIR *dp;
00060
struct dirent *ep;
00061 dp = opendir( QFile::encodeName(_path) );
00062
if ( dp == 0L )
00063
return;
00064
00065
00066
while ( ( ep = readdir( dp ) ) != 0L )
00067 {
00068
if ( strcmp( ep->d_name,
"." ) != 0 && strcmp( ep->d_name,
".." ) != 0 )
00069 {
00070
KURL file;
00071 file.
setPath(
QString( _path ) +
'/' + QFile::decodeName(ep->d_name) );
00072
00073 KMimeType::Ptr res =
KMimeType::findByURL( file, 0,
true );
00074
00075
00076
if ( res->name() ==
"inode/directory" )
00077 {
00078
00079
00080
QDomElement groupElem = m_pDoc->createElement(
"folder" );
00081 parentElem.appendChild( groupElem );
00082
QDomElement textElem = m_pDoc->createElement(
"title" );
00083 groupElem.appendChild( textElem );
00084 textElem.appendChild( m_pDoc->createTextNode( KIO::decodeFileName( ep->d_name ) ) );
00085
if (
KIO::decodeFileName( ep->d_name ) ==
"Toolbar" )
00086 groupElem.setAttribute(
"toolbar",
"yes");
00087 scanIntern( groupElem, file.
path() );
00088 }
00089
else if ( res->name() ==
"application/x-desktop" )
00090 {
00091
KSimpleConfig cfg( file.
path(),
true );
00092 cfg.setDesktopGroup();
00093
QString type = cfg.readEntry(
"Type" );
00094
00095
if ( type ==
"Link" )
00096 parseBookmark( parentElem, ep->d_name, cfg, 0 );
00097
else
00098
kdWarning(7043) <<
" Not a link ? Type=" << type <<
endl;
00099 }
00100
else if ( res->name() ==
"text/plain")
00101 {
00102
00103
KSimpleConfig cfg( file.
path(),
true );
00104
QStringList grp = cfg.groupList().grep(
"internetshortcut",
false );
00105
if ( grp.count() == 0 )
00106
continue;
00107 cfg.setGroup( *grp.begin() );
00108
00109
QString url = cfg.readPathEntry(
"URL");
00110
if (!url.isEmpty() )
00111 parseBookmark( parentElem, ep->d_name, cfg, *grp.begin() );
00112 }
else
00113
kdWarning(7043) <<
"Invalid bookmark : found mimetype='" << res->name() <<
"' for file='" << file.
path() <<
"'!" <<
endl;
00114 }
00115 }
00116
00117 closedir( dp );
00118 }
00119
00120
void KBookmarkImporter::parseBookmark(
QDomElement & parentElem,
QCString _text,
00121
KSimpleConfig& _cfg,
const QString &_group )
00122 {
00123
if ( !_group.isEmpty() )
00124 _cfg.
setGroup( _group );
00125
else
00126 _cfg.
setDesktopGroup();
00127
00128
QString url = _cfg.
readPathEntry(
"URL" );
00129
QString icon = _cfg.
readEntry(
"Icon" );
00130
if (icon.right( 4 ) ==
".xpm" )
00131 icon.truncate( icon.length() - 4 );
00132
00133
QString text =
KIO::decodeFileName( QString::fromLocal8Bit(_text) );
00134
if ( text.length() > 8 && text.right( 8 ) ==
".desktop" )
00135 text.truncate( text.length() - 8 );
00136
if ( text.length() > 7 && text.right( 7 ) ==
".kdelnk" )
00137 text.truncate( text.length() - 7 );
00138
00139
QDomElement elem = m_pDoc->createElement(
"bookmark" );
00140 parentElem.appendChild( elem );
00141 elem.setAttribute(
"href", url );
00142
00143
00144
00145 elem.setAttribute(
"icon", icon );
00146
QDomElement textElem = m_pDoc->createElement(
"title" );
00147 elem.appendChild( textElem );
00148 textElem.appendChild( m_pDoc->createTextNode( text ) );
00149
kdDebug(7043) <<
"KBookmarkImporter::parseBookmark text=" << text <<
endl;
00150 }