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

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

# kbookmarkimporter_ie.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) 2002-2003  Alexander Kellett <lypanov@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 <kfiledialog.h>
    00022 #include <kstringhandler.h>
    00023 #include <klocale.h>
    00024 #include <kdebug.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
    00032 #include "kbookmarkimporter.h"
    00033 #include "kbookmarkimporter_ie.h"
    00034
    00035 /* antlarr: KDE 4: Make them const QString & */
    00036 void KIEBookmarkImporter::parseIEBookmarks_url_file( QString filename, QString name ) {
    00037     static const int g_lineLimit = 16*1024;
    00038
    00039     QFile f(filename);
    00040
    00041     if(f.open(IO_ReadOnly)) {
    00042
    00043         QCString s(g_lineLimit);
    00044
    00045         while(f.readLine(s.data(), g_lineLimit)>=0) {
    00046             if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than g_lineLimit. Skipping.
    00047             {
    00048                kdWarning() << "IE bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl;
    00049                continue;
    00050             }
    00051             QCString t = s.stripWhiteSpace();
    00052             QRegExp rx( "URL=(.*)" );
    00053             if (rx.exactMatch(t)) {
    00054                emit newBookmark( name, rx.cap(1).latin1(), QString("") );
    00055             }
    00056         }
    00057
    00058         f.close();
    00059     }
    00060 }
    00061
    00062 /* antlarr: KDE 4: Make them const QString & */
    00063 void KIEBookmarkImporter::parseIEBookmarks_dir( QString dirname, QString foldername )
    00064 {
    00065
    00066    QDir dir(dirname);
    00067    dir.setFilter( QDir::Files | QDir::Dirs );
    00068    dir.setSorting( QDir::Name | QDir::DirsFirst );
    00069    dir.setNameFilter("*.url"); // AK - possibly add ";index.ini" ?
    00070    dir.setMatchAllDirs(true);
    00071
    00072    const QFileInfoList *list = dir.entryInfoList();
    00073    if (!list) return;
    00074
    00075    if (dirname != m_fileName)
    00076       emit newFolder( foldername, false, "" );
    00077
    00078    QFileInfoListIterator it( *list );
    00079    QFileInfo *fi;
    00080
    00081    while ( (fi = it.current()) != 0 ) {
    00082       ++it;
    00083
    00084       if (fi->fileName() == "." || fi->fileName() == "..") continue;
    00085
    00086       if (fi->isDir()) {
    00087          parseIEBookmarks_dir(fi->absFilePath(), fi->fileName());
    00088
    00089       } else if (fi->isFile()) {
    00090          if (fi->fileName().endsWith(".url")) {
    00091             QString name = fi->fileName();
    00092             name.truncate(name.length() - 4); // .url
    00093             parseIEBookmarks_url_file(fi->absFilePath(), name);
    00094          }
    00095          // AK - add index.ini
    00096       }
    00097    }
    00098
    00099    if (dirname != m_fileName)
    00100       emit endFolder();
    00101 }
    00102
    00103
    00104 void KIEBookmarkImporter::parseIEBookmarks( )
    00105 {
    00106     parseIEBookmarks_dir( m_fileName );
    00107 }
    00108
    00109 QString KIEBookmarkImporter::IEBookmarksDir()
    00110 {
    00111    static KIEBookmarkImporterImpl* p = 0;
    00112    if (!p)
    00113        p = new KIEBookmarkImporterImpl;
    00114    return p->findDefaultLocation();
    00115 }
    00116
    00117 void KIEBookmarkImporterImpl::parse() {
    00118    KIEBookmarkImporter importer(m_fileName);
    00119    setupSignalForwards(&importer, this);
    00120    importer.parseIEBookmarks();
    00121 }
    00122
    00123 QString KIEBookmarkImporterImpl::findDefaultLocation(bool) const
    00124 {
    00125     // notify user that they must give a new dir such
    00126     // as "Favourites" as otherwise it'll just place
    00127     // lots of .url files in the given dir and gui
    00128     // stuff in the exporter is ugly so that exclues
    00129     // the possibility of just writing to Favourites
    00130     // and checking if overwriting...
    00131     return KFileDialog::getExistingDirectory();
    00132 }
    00133
    00135
    00136 class IEExporter : private KBookmarkGroupTraverser {
    00137 public:
    00138     IEExporter( const QString & );
    00139     void write( const KBookmarkGroup &grp ) { traverse(grp); };
    00140 private:
    00141     virtual void visit( const KBookmark & );
    00142     virtual void visitEnter( const KBookmarkGroup & );
    00143     virtual void visitLeave( const KBookmarkGroup & );
    00144 private:
    00145     QDir m_currentDir;
    00146 };
    00147
    00148 static QString ieStyleQuote( const QString &str ) {
    00149     QString s(str);
    00150     s.replace(QRegExp("[/\\:*?\"<>|]"), "_");
    00151     return s;
    00152 }
    00153
    00154 IEExporter::IEExporter( const QString & dname ) {
    00155     m_currentDir.setPath( dname );
    00156 }
    00157
    00158 void IEExporter::visit( const KBookmark &bk ) {
    00159     QString fname = m_currentDir.path() + "/" + ieStyleQuote( bk.fullText() ) + ".url";
    00160     // kdDebug() << "visit(" << bk.text() << "), fname == " << fname << endl;
    00161     QFile file( fname );
    00162     file.open( IO_WriteOnly );
    00163     QTextStream ts( &file );
    00164     ts << "[InternetShortcut]\r\n";
    00165     ts << "URL=" << bk.url().url().utf8() << "\r\n";
    00166 }
    00167
    00168 void IEExporter::visitEnter( const KBookmarkGroup &grp ) {
    00169     QString dname = m_currentDir.path() + "/" + ieStyleQuote( grp.fullText() );
    00170     // kdDebug() << "visitEnter(" << grp.text() << "), dname == " << dname << endl;
    00171     m_currentDir.mkdir( dname );
    00172     m_currentDir.cd( dname );
    00173 }
    00174
    00175 void IEExporter::visitLeave( const KBookmarkGroup & ) {
    00176     // kdDebug() << "visitLeave()" << endl;
    00177     m_currentDir.cdUp();
    00178 }
    00179
    00180 void KIEBookmarkExporterImpl::write(KBookmarkGroup parent) {
    00181     IEExporter exporter( m_fileName );
    00182     exporter.write( parent );
    00183 }
    00184
    00185 #include "kbookmarkimporter_ie.moc"

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