# kio Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kbookmarkimporter.cc

    00001 /* This file is part of the KDE libraries
    00002    Copyright (C) 2000 David Faure <faure@kde.org>
    00003
    00004    This library is free software; you can redistribute it and/or
    00005    modify it under the terms of the GNU Library General Public
    00006    License version 2 as published by the Free Software Foundation.
    00007
    00008    This library is distributed in the hope that it will be useful,
    00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
    00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00011    Library General Public License for more details.
    00012
    00013    You should have received a copy of the GNU Library General Public License
    00014    along with this library; see the file COPYING.LIB.  If not, write to
    00015    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00016    Boston, MA 02111-1307, USA.
    00017 */
    00018
    00019 #include "kbookmarkimporter.h"
    00020 #include <kfiledialog.h>
    00021 #include <kstringhandler.h>
    00022 #include <klocale.h>
    00023 #include <kdebug.h>
    00024 #include <kcharsets.h>
    00025 #include <qtextcodec.h>
    00026
    00027 #include <sys/types.h>
    00028 #include <stddef.h>
    00029 #include <dirent.h>
    00030 #include <sys/stat.h>
    00031 #include <assert.h>
    00032
    00034
    00035 void KBookmarkImporter::import( const QString & path )
    00036 {
    00037     QDomElement elem = m_pDoc->documentElement();
    00038     Q_ASSERT(!elem.isNull());
    00039     scanIntern( elem, path );
    00040 }
    00041
    00042 void KBookmarkImporter::scanIntern( QDomElement & parentElem, const QString & _path )
    00043 {
    00044     kdDebug(1203) << "KBookmarkImporter::scanIntern " << _path << endl;
    00045     // Substitute all symbolic links in the path
    00046     QDir dir( _path );
    00047     QString canonical = dir.canonicalPath();
    00048
    00049     if ( m_lstParsedDirs.contains(canonical) )
    00050     {
    00051         kdWarning() << "Directory " << canonical << " already parsed" << endl;
    00052         return;
    00053     }
    00054
    00055     m_lstParsedDirs.append( canonical );
    00056
    00057     DIR *dp;
    00058     struct dirent *ep;
    00059     dp = opendir( QFile::encodeName(_path) );
    00060     if ( dp == 0L )
    00061         return;
    00062
    00063     // Loop thru all directory entries
    00064     while ( ( ep = readdir( dp ) ) != 0L )
    00065     {
    00066         if ( strcmp( ep->d_name, "." ) != 0 && strcmp( ep->d_name, ".." ) != 0 )
    00067         {
    00068             KURL file;
    00069             file.setPath( QString( _path ) + '/' + QFile::decodeName(ep->d_name) );
    00070
    00071             KMimeType::Ptr res = KMimeType::findByURL( file, 0, true );
    00072             //kdDebug(1203) << " - " << file.url() << "  ->  " << res->name() << endl;
    00073
    00074             if ( res->name() == "inode/directory" )
    00075             {
    00076                 // We could use KBookmarkGroup::createNewFolder, but then it
    00077                 // would notify about the change, so we'd need a flag, etc.
    00078                 QDomElement groupElem = m_pDoc->createElement( "folder" );
    00079                 parentElem.appendChild( groupElem );
    00080                 QDomElement textElem = m_pDoc->createElement( "title" );
    00081                 groupElem.appendChild( textElem );
    00082                 textElem.appendChild( m_pDoc->createTextNode( KIO::decodeFileName( ep->d_name ) ) );
    00083                 if ( KIO::decodeFileName( ep->d_name ) == "Toolbar" )
    00084                     groupElem.setAttribute("toolbar","yes");
    00085                 scanIntern( groupElem, file.path() );
    00086             }
    00087             else if ( res->name() == "application/x-desktop" )
    00088             {
    00089                 KSimpleConfig cfg( file.path(), true );
    00090                 cfg.setDesktopGroup();
    00091                 QString type = cfg.readEntry( "Type" );
    00092                 // Is it really a bookmark file ?
    00093                 if ( type == "Link" )
    00094                     parseBookmark( parentElem, ep->d_name, cfg, 0 /* desktop group */ );
    00095                 else
    00096                     kdWarning(1203) << "  Not a link ? Type=" << type << endl;
    00097             }
    00098             else if ( res->name() == "text/plain")
    00099             {
    00100                 // maybe its an IE Favourite..
    00101                 KSimpleConfig cfg( file.path(), true );
    00102                 QStringList grp = cfg.groupList().grep( "internetshortcut", false );
    00103                 if ( grp.count() == 0 )
    00104                     continue;
    00105                 cfg.setGroup( *grp.begin() );
    00106
    00107                 QString url = cfg.readPathEntry("URL");
    00108                 if (!url.isEmpty() )
    00109                     parseBookmark( parentElem, ep->d_name, cfg, *grp.begin() );
    00110             } else
    00111                 kdWarning(1203) << "Invalid bookmark : found mimetype='" << res->name() << "' for file='" << file.path() << "'!" << endl;
    00112         }
    00113     }
    00114
    00115     closedir( dp );
    00116 }
    00117
    00118 void KBookmarkImporter::parseBookmark( QDomElement & parentElem, QCString _text,
    00119                                        KSimpleConfig& _cfg, const QString &_group )
    00120 {
    00121     if ( !_group.isEmpty() )
    00122         _cfg.setGroup( _group );
    00123     else
    00124         _cfg.setDesktopGroup();
    00125
    00126     QString url = _cfg.readPathEntry( "URL" );
    00127     QString icon = _cfg.readEntry( "Icon" );
    00128     if (icon.right( 4 ) == ".xpm" ) // prevent warnings
    00129         icon.truncate( icon.length() - 4 );
    00130
    00131     QString text = KIO::decodeFileName( QString::fromLocal8Bit(_text) );
    00132     if ( text.length() > 8 && text.right( 8 ) == ".desktop" )
    00133         text.truncate( text.length() - 8 );
    00134     if ( text.length() > 7 && text.right( 7 ) == ".kdelnk" )
    00135         text.truncate( text.length() - 7 );
    00136
    00137     QDomElement elem = m_pDoc->createElement( "bookmark" );
    00138     parentElem.appendChild( elem );
    00139     elem.setAttribute( "href", url );
    00140     //if ( icon != "www" ) // No need to save the default
    00141     // Hmm, after all, it makes KBookmark::pixmapFile faster,
    00142     // and it shows a nice feature to those reading the file
    00143     elem.setAttribute( "icon", icon );
    00144     QDomElement textElem = m_pDoc->createElement( "title" );
    00145     elem.appendChild( textElem );
    00146     textElem.appendChild( m_pDoc->createTextNode( text ) );
    00147     kdDebug(1203) << "KBookmarkImporter::parseBookmark text=" << text << endl;
    00148 }
    00149
    00151
    00152 void KNSBookmarkImporter::parseNSBookmarks( bool utf8 )
    00153 {
    00154     QFile f(m_fileName);
    00155     QTextCodec * codec = utf8 ? QTextCodec::codecForName("UTF-8") : QTextCodec::codecForLocale();
    00156     Q_ASSERT(codec);
    00157     if (!codec)
    00158         return;
    00159
    00160     if(f.open(IO_ReadOnly)) {
    00161
    00162 #define NSBKLINELIMIT 2048
    00163         QCString s(NSBKLINELIMIT);
    00164         // skip header
    00165         while(f.readLine(s.data(), NSBKLINELIMIT) >= 0 && !s.contains("<DL>"));
    00166
    00167         while(f.readLine(s.data(), NSBKLINELIMIT)>=0) {
    00168             if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than NSBKLINELIMIT. Skipping.
    00169             {
    00170                kdWarning() << "Netscape bookmarks contain a line longer than " << NSBKLINELIMIT << ". Skipping." << endl;
    00171                continue;
    00172             }
    00173             QCString t = s.stripWhiteSpace();
    00174             if(t.left(12).upper() == "<DT><A HREF=" ||
    00175                t.left(16).upper() == "<DT><H3><A HREF=") {
    00176               int firstQuotes = t.find('"')+1;
    00177               int secondQuotes = t.find('"', firstQuotes);
    00178               if (firstQuotes != -1 && secondQuotes != -1)
    00179               {
    00180                 QCString link = t.mid(firstQuotes, secondQuotes-firstQuotes);
    00181                 int endTag = t.find('>', secondQuotes+1);
    00182                 QCString name = t.mid(endTag+1);
    00183                 name = name.left(name.findRev('<'));
    00184                 if ( name.right(4) == "</A>" )
    00185                     name = name.left( name.length() - 4 );
    00186                 QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
    00187                 QCString additionnalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
    00188
    00189                 emit newBookmark( KStringHandler::csqueeze(qname),
    00190                                   link, codec->toUnicode(additionnalInfo) );
    00191               }
    00192             }
    00193             else if(t.left(7).upper() == "<DT><H3") {
    00194                 int endTag = t.find('>', 7);
    00195                 QCString name = t.mid(endTag+1);
    00196                 name = name.left(name.findRev('<'));
    00197                 QString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
    00198                 QCString additionnalInfo = t.mid( 8, endTag-8 );
    00199                 bool folded = (additionnalInfo.left(6) == "FOLDED");
    00200                 if (folded) additionnalInfo.remove(0,7);
    00201
    00202                 emit newFolder( KStringHandler::csqueeze(qname),
    00203                                 !folded,
    00204                                 codec->toUnicode(additionnalInfo) );
    00205             }
    00206             else if(t.left(4).upper() == "<HR>")
    00207                 emit newSeparator();
    00208             else if(t.left(8).upper() == "</DL><P>")
    00209                 emit endFolder();
    00210         }
    00211
    00212         f.close();
    00213     }
    00214 }
    00215
    00216 QString KNSBookmarkImporter::netscapeBookmarksFile( bool )
    00217 {
    00218     return QDir::homeDirPath() + "/.netscape/bookmarks.html";
    00219 }
    00220
    00221 QString KNSBookmarkImporter::mozillaBookmarksFile( bool forSaving )
    00222 {
    00223     //return QDir::homeDirPath() + "/.mozilla/default/bookmarks.html";
    00224     if ( forSaving )
    00225         return KFileDialog::getSaveFileName( QDir::homeDirPath() + "/.mozilla",
    00226                                              i18n("*.html|HTML files (*.html)") );
    00227     else
    00228         return KFileDialog::getOpenFileName( QDir::homeDirPath() + "/.mozilla",
    00229                                              i18n("*.html|HTML files (*.html)") );
    00230 }
    00231
    00232 #include "kbookmarkimporter.moc"

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:10 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001