00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
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
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
00075 emit endFolder();
00076
00077 }
else {
00078
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
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
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
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"