• Skip to content
  • Skip to link menu
KDE 4.3 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

KIO

kbookmarkimporter_ie.cc

Go to the documentation of this file.
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., 51 Franklin Street, Fifth Floor,
00018    Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "kbookmarkimporter_ie.h"
00022 
00023 #include <kfiledialog.h>
00024 #include <kstringhandler.h>
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 #include <qtextcodec.h>
00028 #include <QtGui/QApplication>
00029 
00030 #include <sys/types.h>
00031 #include <stddef.h>
00032 #include <dirent.h>
00033 #include <sys/stat.h>
00034 
00035 #include "kbookmarkimporter.h"
00036 
00041 class  KIEBookmarkImporter : public QObject
00042 {
00043     Q_OBJECT
00044 public:
00045     KIEBookmarkImporter( const QString & fileName ) : m_fileName(fileName) {}
00046     ~KIEBookmarkImporter() {}
00047 
00048     void parseIEBookmarks();
00049 
00050     // Usual place for IE bookmarks
00051     static QString IEBookmarksDir();
00052 
00053 Q_SIGNALS:
00054     void newBookmark( const QString & text, const QString & url, const QString & additionalInfo );
00055     void newFolder( const QString & text, bool open, const QString & additionalInfo );
00056     void newSeparator();
00057     void endFolder();
00058 
00059 protected:
00060     void parseIEBookmarks_dir( const QString &dirname, const QString &name = QString() );
00061     void parseIEBookmarks_url_file( const QString &filename, const QString &name );
00062 
00063     QString m_fileName;
00064 };
00065 
00066 void KIEBookmarkImporter::parseIEBookmarks_url_file( const QString &filename, const QString &name ) {
00067     static const int g_lineLimit = 16*1024;
00068 
00069     QFile f(filename);
00070 
00071     if(f.open(QIODevice::ReadOnly)) {
00072 
00073         QByteArray s(g_lineLimit,0);
00074 
00075         while(f.readLine(s.data(), g_lineLimit)>=0) {
00076             if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than g_lineLimit. Skipping.
00077             {
00078                kWarning() << "IE bookmarks contain a line longer than " << g_lineLimit << ". Skipping.";
00079                continue;
00080             }
00081             QByteArray t = s.trimmed();
00082             QRegExp rx( "URL=(.*)" );
00083             if (rx.exactMatch(t)) {
00084                emit newBookmark( name, rx.cap(1), QString("") );
00085             }
00086         }
00087 
00088         f.close();
00089     }
00090 }
00091 
00092 void KIEBookmarkImporter::parseIEBookmarks_dir( const QString &dirname, const QString &foldername )
00093 {
00094 
00095    QDir dir(dirname);
00096    dir.setFilter( QDir::Files | QDir::Dirs | QDir::AllDirs );
00097    dir.setSorting( QFlags<QDir::SortFlag>(QDir::Name | QDir::DirsFirst) );
00098    dir.setNameFilters(QStringList("*.url")); // AK - possibly add ";index.ini" ?
00099 
00100    QFileInfoList list = dir.entryInfoList();
00101    if (list.isEmpty()) return;
00102 
00103    if (dirname != m_fileName)
00104       emit newFolder( foldername, false, "" );
00105 
00106    foreach (const QFileInfo &fi, list) {
00107       if (fi.fileName() == "." || fi.fileName() == "..") continue;
00108 
00109       if (fi.isDir()) {
00110          parseIEBookmarks_dir(fi.absoluteFilePath(), fi.fileName());
00111 
00112       } else if (fi.isFile()) {
00113          if (fi.fileName().endsWith(".url")) {
00114             QString name = fi.fileName();
00115             name.truncate(name.length() - 4); // .url
00116             parseIEBookmarks_url_file(fi.absoluteFilePath(), name);
00117          }
00118          // AK - add index.ini
00119       }
00120    }
00121 
00122    if (dirname != m_fileName)
00123       emit endFolder();
00124 }
00125 
00126 
00127 void KIEBookmarkImporter::parseIEBookmarks( )
00128 {
00129     parseIEBookmarks_dir( m_fileName );
00130 }
00131 
00132 QString KIEBookmarkImporter::IEBookmarksDir()
00133 {
00134    static KIEBookmarkImporterImpl* p = 0;
00135    if (!p)
00136        p = new KIEBookmarkImporterImpl;
00137    return p->findDefaultLocation();
00138 }
00139 
00140 void KIEBookmarkImporterImpl::parse() {
00141    KIEBookmarkImporter importer(m_fileName);
00142    setupSignalForwards(&importer, this);
00143    importer.parseIEBookmarks();
00144 }
00145 
00146 QString KIEBookmarkImporterImpl::findDefaultLocation(bool) const
00147 {
00148     // notify user that they must give a new dir such
00149     // as "Favourites" as otherwise it'll just place
00150     // lots of .url files in the given dir and gui
00151     // stuff in the exporter is ugly so that exclues
00152     // the possibility of just writing to Favourites
00153     // and checking if overwriting...
00154     return KFileDialog::getExistingDirectory(KUrl(), QApplication::activeWindow());
00155 }
00156 
00158 
00159 class IEExporter : private KBookmarkGroupTraverser {
00160 public:
00161     IEExporter( const QString & );
00162     void write( const KBookmarkGroup &grp ) { traverse(grp); }
00163 private:
00164     virtual void visit( const KBookmark & );
00165     virtual void visitEnter( const KBookmarkGroup & );
00166     virtual void visitLeave( const KBookmarkGroup & );
00167 private:
00168     QDir m_currentDir;
00169 };
00170 
00171 static QString ieStyleQuote( const QString &str ) {
00172     QString s(str);
00173     s.replace(QRegExp("[/\\:*?\"<>|]"), "_");
00174     return s;
00175 }
00176 
00177 IEExporter::IEExporter( const QString & dname ) {
00178     m_currentDir.setPath( dname );
00179 }
00180 
00181 void IEExporter::visit( const KBookmark &bk ) {
00182     QString fname = m_currentDir.path() + '/' + ieStyleQuote( bk.fullText() ) + ".url";
00183     // kDebug() << "visit(" << bk.text() << "), fname == " << fname;
00184     QFile file( fname );
00185     file.open( QIODevice::WriteOnly );
00186     QTextStream ts( &file );
00187     ts << "[InternetShortcut]\r\n";
00188     ts << "URL=" << bk.url().url().toUtf8() << "\r\n";
00189 }
00190 
00191 void IEExporter::visitEnter( const KBookmarkGroup &grp ) {
00192     QString dname = m_currentDir.path() + '/' + ieStyleQuote( grp.fullText() );
00193     // kDebug() << "visitEnter(" << grp.text() << "), dname == " << dname;
00194     m_currentDir.mkdir( dname );
00195     m_currentDir.cd( dname );
00196 }
00197 
00198 void IEExporter::visitLeave( const KBookmarkGroup & ) {
00199     // kDebug() << "visitLeave()";
00200     m_currentDir.cdUp();
00201 }
00202 
00203 void KIEBookmarkExporterImpl::write(const KBookmarkGroup& parent) {
00204     IEExporter exporter( m_fileName );
00205     exporter.write( parent );
00206 }
00207 
00208 #include "kbookmarkimporter_ie.moc"

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.6.1
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal