kio Library API Documentation

kbookmarkexporter.cc

00001 /* This file is part of the KDE libraries
00002    Copyright (C) 1996-1998 Martin R. Jones <mjones@kde.org>
00003    Copyright (C) 2000 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License version 2 as published by the Free Software Foundation.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 #include <qfile.h>
00020 #include <kdebug.h>
00021 #include <stdio.h>
00022 #include <klocale.h>
00023 #include "kbookmarkmanager.h"
00024 #include "kbookmarkexporter.h"
00025 #include <qtextcodec.h>
00026 #include <qregexp.h>
00027 
00028 // write bookmarks file
00029 //
00030 void KNSBookmarkExporter::write( bool utf8 )
00031 {
00032     if ( QFile::exists( m_fileName ) )
00033     {
00034         ::rename( QFile::encodeName( m_fileName ), QFile::encodeName( m_fileName + ".beforekde" ) );
00035     }
00036 
00037     QFile file( m_fileName );
00038 
00039     if ( !file.open( IO_WriteOnly ) )
00040     {
00041         kdError(1203) << "Can't write to file " << m_fileName << endl;
00042         return;
00043     }
00044 
00045     QTextStream stream( &file );
00046     QString charset;
00047     if ( utf8 ) {
00048         stream.setEncoding( QTextStream::UnicodeUTF8 );
00049         charset = "UTF-8";
00050     } else {
00051         stream.setEncoding( QTextStream::Locale );
00052         charset = QString::fromLatin1(QTextCodec::codecForLocale()->name()).upper();
00053     }
00054 
00055     stream << "<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl;
00056     stream << i18n("<!-- This file was generated by Konqueror -->") << endl;
00057     stream << "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=" << charset << "\">" << endl;
00058 
00059     QString title = i18n("Bookmarks");
00060     stream << "<TITLE>" << title << "</TITLE>" << endl;
00061     stream << "<H1>" << title << "</H1>" << endl;
00062 
00063     stream << "<DL><p>" << endl;
00064 
00065     writeFolder( stream, m_pManager->root() );
00066 
00067     stream << "</DL><P>" << endl;
00068 }
00069 
00070 // write the contents of a folder (recursive)
00071 //
00072 void KNSBookmarkExporter::writeFolder( QTextStream &stream, KBookmarkGroup parent )
00073 {
00074     QRegExp amp("&");
00075     QRegExp lt("<");
00076     QRegExp gt(">");
00077     for ( KBookmark bm = parent.first(); !bm.isNull(); bm = parent.next(bm) )
00078     {
00079         if ( bm.isSeparator() )
00080         {
00081             stream << "<HR>" << endl;
00082         } else
00083         {
00084             QString text = bm.text();
00085             text.replace( amp, "&amp;" ).replace( lt, "&lt;" ).replace( gt, "&gt;" );
00086             if ( bm.isGroup() )
00087             {
00088                 stream << "<DT><H3 ";
00089                 if (!bm.toGroup().isOpen())
00090                     stream << "FOLDED ";
00091                 stream << bm.internalElement().attribute("netscapeinfo");
00092                 stream << ">" << text << "</H3>" << endl;
00093                 stream << "<DL><P>" << endl;
00094                 writeFolder( stream, bm.toGroup() );
00095                 stream << "</DL><P>" << endl;
00096             }
00097             else
00098             {
00099                 stream << "<DT><A HREF=\"" << bm.url().url() /* netscape seems to use local8bit...*/ << "\"";
00100                 stream << bm.internalElement().attribute("netscapeinfo");
00101                 stream << ">" << text << "</A>" << endl;
00102             }
00103         }
00104     }
00105 }
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:13:10 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001