KIO
kbookmarkimporter_opera.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 int lineno = 0;
00051 QString url, name, type;
00052 static const int g_lineLimit = 16*1024;
00053 QByteArray line(g_lineLimit,0);
00054
00055 while ( file.readLine(line.data(), g_lineLimit) >=0 ) {
00056 lineno++;
00057
00058
00059 if ( line[line.length()-1] != '\n' || lineno <= 2 )
00060 continue;
00061
00062 QString currentLine = codec->toUnicode(line).trimmed();
00063
00064 if (currentLine.isEmpty()) {
00065
00066 if (type.isNull())
00067 continue;
00068 else if ( type == "URL")
00069 emit newBookmark( name, url, "" );
00070 else if (type == "FOLDER" )
00071 emit newFolder( name, false, "" );
00072
00073 type.clear();
00074 name.clear();
00075 url.clear();
00076
00077 } else if (currentLine == "-") {
00078
00079 emit endFolder();
00080
00081 } else {
00082
00083 QString tag;
00084 if ( tag = "#", currentLine.startsWith( tag ) )
00085 type = currentLine.remove( 0, tag.length() );
00086 else if ( tag = "NAME=", currentLine.startsWith( tag ) )
00087 name = currentLine.remove(0, tag.length());
00088 else if ( tag = "URL=", currentLine.startsWith( tag ) )
00089 url = currentLine.remove(0, tag.length());
00090 }
00091 }
00092
00093 }
00094
00095 QString KOperaBookmarkImporter::operaBookmarksFile()
00096 {
00097 static KOperaBookmarkImporterImpl *p = 0;
00098 if (!p)
00099 p = new KOperaBookmarkImporterImpl;
00100 return p->findDefaultLocation();
00101 }
00102
00103 void KOperaBookmarkImporterImpl::parse() {
00104 KOperaBookmarkImporter importer(m_fileName);
00105 setupSignalForwards(&importer, this);
00106 importer.parseOperaBookmarks();
00107 }
00108
00109 QString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
00110 {
00111 return saving ? KFileDialog::getSaveFileName(
00112 QDir::homePath() + "/.opera",
00113 i18n("*.adr|Opera Bookmark Files (*.adr)"),
00114 QApplication::activeWindow() )
00115 : KFileDialog::getOpenFileName(
00116 QDir::homePath() + "/.opera",
00117 i18n("*.adr|Opera Bookmark Files (*.adr)"),
00118 QApplication::activeWindow() );
00119 }
00120
00122
00123 class OperaExporter : private KBookmarkGroupTraverser {
00124 public:
00125 OperaExporter();
00126 QString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; }
00127 private:
00128 virtual void visit( const KBookmark & );
00129 virtual void visitEnter( const KBookmarkGroup & );
00130 virtual void visitLeave( const KBookmarkGroup & );
00131 private:
00132 QString m_string;
00133 QTextStream m_out;
00134 };
00135
00136 OperaExporter::OperaExporter() : m_out(&m_string, QIODevice::WriteOnly) {
00137 m_out << "Opera Hotlist version 2.0" << endl;
00138 m_out << "Options: encoding = utf8, version=3" << endl;
00139 }
00140
00141 void OperaExporter::visit( const KBookmark &bk ) {
00142
00143 m_out << "#URL" << endl;
00144 m_out << "\tNAME=" << bk.fullText() << endl;
00145 m_out << "\tURL=" << bk.url().url().toUtf8() << endl;
00146 m_out << endl;
00147 }
00148
00149 void OperaExporter::visitEnter( const KBookmarkGroup &grp ) {
00150
00151 m_out << "#FOLDER" << endl;
00152 m_out << "\tNAME="<< grp.fullText() << endl;
00153 m_out << endl;
00154 }
00155
00156 void OperaExporter::visitLeave( const KBookmarkGroup & ) {
00157
00158 m_out << "-" << endl;
00159 m_out << endl;
00160 }
00161
00162 void KOperaBookmarkExporterImpl::write(const KBookmarkGroup &parent) {
00163 OperaExporter exporter;
00164 QString content = exporter.generate( parent );
00165 QFile file(m_fileName);
00166 if (!file.open(QIODevice::WriteOnly)) {
00167 kError(7043) << "Can't write to file " << m_fileName << endl;
00168 return;
00169 }
00170 QTextStream fstream(&file);
00171 fstream.setCodec(QTextCodec::codecForName("UTF-8"));
00172 fstream << content;
00173 }
00174
00175 #include "kbookmarkimporter_opera_p.moc"