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., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <tdefiledialog.h> 00022 #include <kstringhandler.h> 00023 #include <tdelocale.h> 00024 #include <kdebug.h> 00025 #include <tqtextcodec.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 TQFile file(m_fileName); 00038 if(!file.open(IO_ReadOnly)) { 00039 return; 00040 } 00041 00042 TQTextCodec * codec = TQTextCodec::codecForName("UTF-8"); 00043 Q_ASSERT(codec); 00044 if (!codec) 00045 return; 00046 00047 int lineno = 0; 00048 TQString url, name, type; 00049 static const int g_lineLimit = 16*1024; 00050 TQCString 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 TQString 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 = TQString::null; 00071 name = TQString::null; 00072 url = TQString::null; 00073 00074 } else if (currentLine == "-") { 00075 // end of folder 00076 emit endFolder(); 00077 00078 } else { 00079 // data block line 00080 TQString 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 TQString 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 TQString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const 00107 { 00108 return saving ? KFileDialog::getSaveFileName( 00109 TQDir::homeDirPath() + "/.opera", 00110 i18n("*.adr|Opera Bookmark Files (*.adr)") ) 00111 : KFileDialog::getOpenFileName( 00112 TQDir::homeDirPath() + "/.opera", 00113 i18n("*.adr|Opera Bookmark Files (*.adr)") ); 00114 } 00115 00117 00118 class OperaExporter : private KBookmarkGroupTraverser { 00119 public: 00120 OperaExporter(); 00121 TQString 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 TQString m_string; 00128 TQTextStream 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 TQString content = exporter.generate( parent ); 00160 TQFile file(m_fileName); 00161 if (!file.open(IO_WriteOnly)) { 00162 kdError(7043) << "Can't write to file " << m_fileName << endl; 00163 return; 00164 } 00165 TQTextStream fstream(&file); 00166 fstream.setEncoding(TQTextStream::UnicodeUTF8); 00167 fstream << content; 00168 } 00169 00170 #include "kbookmarkimporter_opera.moc"