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 <tdefiledialog.h>
00023 #include <kstringhandler.h>
00024 #include <tdelocale.h>
00025 #include <kdebug.h>
00026 #include <kcharsets.h>
00027 #include <tqtextcodec.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 TQString & path )
00038 {
00039 TQDomElement elem = m_pDoc->documentElement();
00040 Q_ASSERT(!elem.isNull());
00041 scanIntern( elem, path );
00042 }
00043
00044 void KBookmarkImporter::scanIntern( TQDomElement & parentElem, const TQString & _path )
00045 {
00046 kdDebug(7043) << "KBookmarkImporter::scanIntern " << _path << endl;
00047
00048 TQDir dir( _path );
00049 TQString 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( TQFile::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( TQString( _path ) + '/' + TQFile::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 TQDomElement groupElem = m_pDoc->createElement( "folder" );
00081 parentElem.appendChild( groupElem );
00082 TQDomElement textElem = m_pDoc->createElement( "title" );
00083 groupElem.appendChild( textElem );
00084 textElem.appendChild( m_pDoc->createTextNode( TDEIO::decodeFileName( ep->d_name ) ) );
00085 if ( TDEIO::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 || (res->name() == "media/builtin-mydocuments")
00091 || (res->name() == "media/builtin-mycomputer")
00092 || (res->name() == "media/builtin-mynetworkplaces")
00093 || (res->name() == "media/builtin-printers")
00094 || (res->name() == "media/builtin-trash")
00095 || (res->name() == "media/builtin-webbrowser") )
00096 {
00097 KSimpleConfig cfg( file.path(), true );
00098 cfg.setDesktopGroup();
00099 TQString type = cfg.readEntry( "Type" );
00100
00101 if ( type == "Link" )
00102 parseBookmark( parentElem, ep->d_name, cfg, 0 );
00103 else
00104 kdWarning(7043) << " Not a link ? Type=" << type << endl;
00105 }
00106 else if ( res->name() == "text/plain")
00107 {
00108
00109 KSimpleConfig cfg( file.path(), true );
00110 TQStringList grp = cfg.groupList().grep( "internetshortcut", false );
00111 if ( grp.count() == 0 )
00112 continue;
00113 cfg.setGroup( *grp.begin() );
00114
00115 TQString url = cfg.readPathEntry("URL");
00116 if (!url.isEmpty() )
00117 parseBookmark( parentElem, ep->d_name, cfg, *grp.begin() );
00118 } else
00119 kdWarning(7043) << "Invalid bookmark : found mimetype='" << res->name() << "' for file='" << file.path() << "'!" << endl;
00120 }
00121 }
00122
00123 closedir( dp );
00124 }
00125
00126 void KBookmarkImporter::parseBookmark( TQDomElement & parentElem, TQCString _text,
00127 KSimpleConfig& _cfg, const TQString &_group )
00128 {
00129 if ( !_group.isEmpty() )
00130 _cfg.setGroup( _group );
00131 else
00132 _cfg.setDesktopGroup();
00133
00134 TQString url = _cfg.readPathEntry( "URL" );
00135 TQString icon = _cfg.readEntry( "Icon" );
00136 if (icon.right( 4 ) == ".xpm" )
00137 icon.truncate( icon.length() - 4 );
00138
00139 TQString text = TDEIO::decodeFileName( TQString::fromLocal8Bit(_text) );
00140 if ( text.length() > 8 && text.right( 8 ) == ".desktop" )
00141 text.truncate( text.length() - 8 );
00142 if ( text.length() > 7 && text.right( 7 ) == ".kdelnk" )
00143 text.truncate( text.length() - 7 );
00144
00145 TQDomElement elem = m_pDoc->createElement( "bookmark" );
00146 parentElem.appendChild( elem );
00147 elem.setAttribute( "href", url );
00148
00149
00150
00151 elem.setAttribute( "icon", icon );
00152 TQDomElement textElem = m_pDoc->createElement( "title" );
00153 elem.appendChild( textElem );
00154 textElem.appendChild( m_pDoc->createTextNode( text ) );
00155 kdDebug(7043) << "KBookmarkImporter::parseBookmark text=" << text << endl;
00156 }