kio Library: kbookmarkimporter_kde1.cc Source File (kio Library)

| # kio Library API Documentation
Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Namespaces | Source Files

# 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., 59 Temple Place - Suite 330,
    00018    Boston, MA 02111-1307, 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 <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     // Substitute all symbolic links in the path
    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     // 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( QString( _path ) + '/' + QFile::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                 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                 // Is it really a bookmark file ?
    00095                 if ( type == "Link" )
    00096                     parseBookmark( parentElem, ep->d_name, cfg, 0 /* desktop group */ );
    00097                 else
    00098                     kdWarning(7043) << "  Not a link ? Type=" << type << endl;
    00099             }
    00100             else if ( res->name() == "text/plain")
    00101             {
    00102                 // maybe its an IE Favourite..
    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" ) // prevent warnings
    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     //if ( icon != "www" ) // No need to save the default
    00143     // Hmm, after all, it makes KBookmark::pixmapFile faster,
    00144     // and it shows a nice feature to those reading the file
    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 }

This file is part of the documentation for kio Library Version 3.4.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat Jun 11 20:23:55 2005 by doxygen 1.4.1 written by Dimitri van Heesch, © 1997-2003