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