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

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

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