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

KIO

kbookmarkimporter_opera.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_opera.h"
00022 #include "kbookmarkimporter_opera_p.h"
00023 
00024 #include <kfiledialog.h>
00025 #include <kstringhandler.h>
00026 #include <klocale.h>
00027 #include <kdebug.h>
00028 #include <qtextcodec.h>
00029 #include <QtGui/QApplication>
00030 
00031 #include <sys/types.h>
00032 #include <stddef.h>
00033 #include <dirent.h>
00034 #include <sys/stat.h>
00035 
00036 #include "kbookmarkimporter.h"
00037 
00038 void KOperaBookmarkImporter::parseOperaBookmarks( )
00039 {
00040    QFile file(m_fileName);
00041    if(!file.open(QIODevice::ReadOnly)) {
00042       return;
00043    }
00044 
00045    QTextCodec * codec = QTextCodec::codecForName("UTF-8");
00046    Q_ASSERT(codec);
00047    if (!codec)
00048       return;
00049 
00050    int lineno = 0;
00051    QString url, name, type;
00052    static const int g_lineLimit = 16*1024;
00053    QByteArray line(g_lineLimit,0);
00054 
00055    while ( file.readLine(line.data(), g_lineLimit) >=0 ) {
00056       lineno++;
00057 
00058       // skip lines that didn't fit in buffer and first two headers lines
00059       if ( line[line.length()-1] != '\n' || lineno <= 2 )
00060           continue;
00061 
00062       QString currentLine = codec->toUnicode(line).trimmed();
00063 
00064       if (currentLine.isEmpty()) {
00065          // end of data block
00066          if (type.isNull())
00067             continue;
00068          else if ( type == "URL")
00069             emit newBookmark( name, url, "" );
00070          else if (type == "FOLDER" )
00071             emit newFolder( name, false, "" );
00072 
00073          type.clear();
00074          name.clear();
00075          url.clear();
00076 
00077       } else if (currentLine == "-") {
00078          // end of folder
00079          emit endFolder();
00080 
00081       } else {
00082          // data block line
00083          QString tag;
00084          if ( tag = "#", currentLine.startsWith( tag ) )
00085             type = currentLine.remove( 0, tag.length() );
00086          else if ( tag = "NAME=", currentLine.startsWith( tag ) )
00087             name = currentLine.remove(0, tag.length());
00088          else if ( tag = "URL=", currentLine.startsWith( tag ) )
00089             url = currentLine.remove(0, tag.length());
00090       }
00091    }
00092 
00093 }
00094 
00095 QString KOperaBookmarkImporter::operaBookmarksFile()
00096 {
00097    static KOperaBookmarkImporterImpl *p = 0;
00098    if (!p)
00099        p = new KOperaBookmarkImporterImpl;
00100    return p->findDefaultLocation();
00101 }
00102 
00103 void KOperaBookmarkImporterImpl::parse() {
00104    KOperaBookmarkImporter importer(m_fileName);
00105    setupSignalForwards(&importer, this);
00106    importer.parseOperaBookmarks();
00107 }
00108 
00109 QString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
00110 {
00111    return saving ? KFileDialog::getSaveFileName(
00112                        QDir::homePath() + "/.opera",
00113                        i18n("*.adr|Opera Bookmark Files (*.adr)"),
00114                        QApplication::activeWindow() )
00115                  : KFileDialog::getOpenFileName(
00116                        QDir::homePath() + "/.opera",
00117                        i18n("*.adr|Opera Bookmark Files (*.adr)"),
00118                        QApplication::activeWindow() );
00119 }
00120 
00122 
00123 class OperaExporter : private KBookmarkGroupTraverser {
00124 public:
00125     OperaExporter();
00126     QString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; }
00127 private:
00128     virtual void visit( const KBookmark & );
00129     virtual void visitEnter( const KBookmarkGroup & );
00130     virtual void visitLeave( const KBookmarkGroup & );
00131 private:
00132     QString m_string;
00133     QTextStream m_out;
00134 };
00135 
00136 OperaExporter::OperaExporter() : m_out(&m_string, QIODevice::WriteOnly) {
00137     m_out << "Opera Hotlist version 2.0" << endl;
00138     m_out << "Options: encoding = utf8, version=3" << endl;
00139 }
00140 
00141 void OperaExporter::visit( const KBookmark &bk ) {
00142     // kDebug() << "visit(" << bk.text() << ")";
00143     m_out << "#URL" << endl;
00144     m_out << "\tNAME=" << bk.fullText() << endl;
00145     m_out << "\tURL=" << bk.url().url().toUtf8() << endl;
00146     m_out << endl;
00147 }
00148 
00149 void OperaExporter::visitEnter( const KBookmarkGroup &grp ) {
00150     // kDebug() << "visitEnter(" << grp.text() << ")";
00151     m_out << "#FOLDER" << endl;
00152     m_out << "\tNAME="<< grp.fullText() << endl;
00153     m_out << endl;
00154 }
00155 
00156 void OperaExporter::visitLeave( const KBookmarkGroup & ) {
00157     // kDebug() << "visitLeave()";
00158     m_out << "-" << endl;
00159     m_out << endl;
00160 }
00161 
00162 void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent) {
00163     OperaExporter exporter;
00164     QString content = exporter.generate( parent );
00165     QFile file(m_fileName);
00166     if (!file.open(QIODevice::WriteOnly)) {
00167        kError(7043) << "Can't write to file " << m_fileName << endl;
00168        return;
00169     }
00170     QTextStream fstream(&file);
00171     fstream.setCodec(QTextCodec::codecForName("UTF-8"));
00172     fstream << content;
00173 }
00174 
00175 #include "kbookmarkimporter_opera_p.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
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
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