• Skip to content
  • Skip to link menu
KDE 4.2 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    QString url, name, type;
00051    int lineno = 0, version = 0;
00052    QTextStream stream(&file);
00053    stream.setCodec(codec);
00054    while(! stream.atEnd()) {
00055         lineno++;
00056         QString line = stream.readLine().trimmed();
00057 
00058         // first two headers lines contain details about the format
00059         if (lineno <= 2) {
00060             if (line.toLower().startsWith("options:")) {
00061                 foreach(QString ba, line.mid(8).split(',')) {
00062                     const int pos = ba.indexOf('=');
00063                     if (pos < 1)
00064                         continue;
00065                     const QString key = ba.left(pos).trimmed().toLower();
00066                     const QString value = ba.mid(pos+1).trimmed();
00067                     if (key == "version")
00068                         version = value.toInt();
00069                 }
00070             }
00071             continue;
00072         }
00073 
00074         // at least up till version<=3 the following is valid
00075         if (line.isEmpty()) {
00076             // end of data block
00077             if (type.isNull())
00078                 continue;
00079             else if ( type == "URL")
00080                 emit newBookmark( name, url, "" );
00081             else if (type == "FOLDER" )
00082                 emit newFolder( name, false, "" );
00083 
00084             type.clear();
00085             name.clear();
00086             url.clear();
00087         } else if (line == "-") {
00088             // end of folder
00089             emit endFolder();
00090         } else {
00091             // data block line
00092             QString tag;
00093             if ( tag = "#", line.startsWith( tag ) )
00094                 type = line.remove( 0, tag.length() );
00095             else if ( tag = "NAME=", line.startsWith( tag ) )
00096                 name = line.remove(0, tag.length());
00097             else if ( tag = "URL=", line.startsWith( tag ) )
00098                 url = line.remove(0, tag.length());
00099         }
00100    }
00101 }
00102 
00103 QString KOperaBookmarkImporter::operaBookmarksFile()
00104 {
00105    static KOperaBookmarkImporterImpl *p = 0;
00106    if (!p)
00107        p = new KOperaBookmarkImporterImpl;
00108    return p->findDefaultLocation();
00109 }
00110 
00111 void KOperaBookmarkImporterImpl::parse() {
00112    KOperaBookmarkImporter importer(m_fileName);
00113    setupSignalForwards(&importer, this);
00114    importer.parseOperaBookmarks();
00115 }
00116 
00117 QString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
00118 {
00119    return saving ? KFileDialog::getSaveFileName(
00120                        QDir::homePath() + "/.opera",
00121                        i18n("*.adr|Opera Bookmark Files (*.adr)"),
00122                        QApplication::activeWindow() )
00123                  : KFileDialog::getOpenFileName(
00124                        QDir::homePath() + "/.opera",
00125                        i18n("*.adr|Opera Bookmark Files (*.adr)"),
00126                        QApplication::activeWindow() );
00127 }
00128 
00130 
00131 class OperaExporter : private KBookmarkGroupTraverser {
00132 public:
00133     OperaExporter();
00134     QString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; }
00135 private:
00136     virtual void visit( const KBookmark & );
00137     virtual void visitEnter( const KBookmarkGroup & );
00138     virtual void visitLeave( const KBookmarkGroup & );
00139 private:
00140     QString m_string;
00141     QTextStream m_out;
00142 };
00143 
00144 OperaExporter::OperaExporter() : m_out(&m_string, QIODevice::WriteOnly) {
00145     m_out << "Opera Hotlist version 2.0" << endl;
00146     m_out << "Options: encoding = utf8, version=3" << endl;
00147 }
00148 
00149 void OperaExporter::visit( const KBookmark &bk ) {
00150     // kDebug() << "visit(" << bk.text() << ")";
00151     m_out << "#URL" << endl;
00152     m_out << "\tNAME=" << bk.fullText() << endl;
00153     m_out << "\tURL=" << bk.url().url().toUtf8() << endl;
00154     m_out << endl;
00155 }
00156 
00157 void OperaExporter::visitEnter( const KBookmarkGroup &grp ) {
00158     // kDebug() << "visitEnter(" << grp.text() << ")";
00159     m_out << "#FOLDER" << endl;
00160     m_out << "\tNAME="<< grp.fullText() << endl;
00161     m_out << endl;
00162 }
00163 
00164 void OperaExporter::visitLeave( const KBookmarkGroup & ) {
00165     // kDebug() << "visitLeave()";
00166     m_out << "-" << endl;
00167     m_out << endl;
00168 }
00169 
00170 void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent) {
00171     OperaExporter exporter;
00172     QString content = exporter.generate( parent );
00173     QFile file(m_fileName);
00174     if (!file.open(QIODevice::WriteOnly)) {
00175        kError(7043) << "Can't write to file " << m_fileName << endl;
00176        return;
00177     }
00178     QTextStream fstream(&file);
00179     fstream.setCodec(QTextCodec::codecForName("UTF-8"));
00180     fstream << content;
00181 }
00182 
00183 #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
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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