kbookmarkimporter_kde1.cc
00001 // -*- c-basic-offset:4; indent-tabs-mode:nil -*- 00002 // vim: set ts=4 sts=4 sw=4 et: 00003 /* This file is part of the KDE libraries 00004 Copyright (C) 2000 David Faure <faure@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 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 <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 // Substitute all symbolic links in the path 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 // Loop thru all directory entries 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 //kdDebug(7043) << " - " << file.url() << " -> " << res->name() << endl; 00075 00076 if ( res->name() == "inode/directory" ) 00077 { 00078 // We could use KBookmarkGroup::createNewFolder, but then it 00079 // would notify about the change, so we'd need a flag, etc. 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( 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 || (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 // Is it really a bookmark file ? 00101 if ( type == "Link" ) 00102 parseBookmark( parentElem, ep->d_name, cfg, 0 /* desktop group */ ); 00103 else 00104 kdWarning(7043) << " Not a link ? Type=" << type << endl; 00105 } 00106 else if ( res->name() == "text/plain") 00107 { 00108 // maybe its an IE Favourite.. 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" ) // prevent warnings 00137 icon.truncate( icon.length() - 4 ); 00138 00139 TQString text = KIO::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 //if ( icon != "www" ) // No need to save the default 00149 // Hmm, after all, it makes KBookmark::pixmapFile faster, 00150 // and it shows a nice feature to those reading the file 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 }