kio Library API Documentation

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