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
static const int g_lineLimit = 16*1024;
00050
QCString line(g_lineLimit);
00051
00052
while ( file.
readLine(line.data(), g_lineLimit) >=0 ) {
00053 lineno++;
00054
00055
00056
if ( line[line.
length()-1] !=
'\n' || lineno <= 2 )
00057
continue;
00058
00059
QString currentLine = codec->
toUnicode(line).stripWhiteSpace();
00060
00061
if (currentLine.
isEmpty()) {
00062
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 = QString::null;
00071
name = QString::null;
00072 url = QString::null;
00073
00074 }
else if (currentLine ==
"-") {
00075
00076 emit endFolder();
00077
00078 }
else {
00079
00080
QString 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
QString 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
QString KOperaBookmarkImporterImpl::findDefaultLocation(
bool saving)
const
00107
{
00108
return saving ?
KFileDialog::getSaveFileName(
00109 QDir::homeDirPath() +
"/.opera",
00110 i18n(
"*.adr|Opera Bookmark Files (*.adr)") )
00111 :
KFileDialog::getOpenFileName(
00112
QDir::homeDirPath() + "/.opera",
00113 i18n("*.adr|Opera Bookmark Files (*.adr)") );
00114 }
00115
00117
00118
class OperaExporter :
private KBookmarkGroupTraverser {
00119
public:
00120 OperaExporter();
00121
QString 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
QString m_string;
00128
QTextStream 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
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
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
00153 m_out <<
"-" <<
endl;
00154 m_out <<
endl;
00155 }
00156
00157
void KOperaBookmarkExporterImpl::write(
KBookmarkGroup parent) {
00158 OperaExporter exporter;
00159
QString content = exporter.generate( parent );
00160
QFile file(m_fileName);
00161
if (!file.
open(IO_WriteOnly)) {
00162
kdError(7043) <<
"Can't write to file " << m_fileName <<
endl;
00163
return;
00164 }
00165
QTextStream fstream(&file);
00166 fstream.
setEncoding(QTextStream::UnicodeUTF8);
00167 fstream << content;
00168 }
00169
00170
#include "kbookmarkimporter_opera.moc"