You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
171 lines
5.2 KiB
171 lines
5.2 KiB
15 years ago
|
// -*- c-basic-offset:4; indent-tabs-mode:nil -*-
|
||
|
// vim: set ts=4 sts=4 sw=4 et:
|
||
|
/* This file is part of the KDE libraries
|
||
|
Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License version 2 as published by the Free Software Foundation.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
12 years ago
|
#include <tdefiledialog.h>
|
||
15 years ago
|
#include <kstringhandler.h>
|
||
|
#include <klocale.h>
|
||
|
#include <kdebug.h>
|
||
13 years ago
|
#include <tqtextcodec.h>
|
||
15 years ago
|
|
||
|
#include <sys/types.h>
|
||
|
#include <stddef.h>
|
||
|
#include <dirent.h>
|
||
|
#include <sys/stat.h>
|
||
|
|
||
|
#include "kbookmarkimporter.h"
|
||
|
#include "kbookmarkimporter_opera.h"
|
||
|
|
||
|
void KOperaBookmarkImporter::parseOperaBookmarks( )
|
||
|
{
|
||
15 years ago
|
TQFile file(m_fileName);
|
||
15 years ago
|
if(!file.open(IO_ReadOnly)) {
|
||
|
return;
|
||
|
}
|
||
|
|
||
15 years ago
|
TQTextCodec * codec = TQTextCodec::codecForName("UTF-8");
|
||
15 years ago
|
Q_ASSERT(codec);
|
||
|
if (!codec)
|
||
|
return;
|
||
|
|
||
|
int lineno = 0;
|
||
15 years ago
|
TQString url, name, type;
|
||
15 years ago
|
static const int g_lineLimit = 16*1024;
|
||
15 years ago
|
TQCString line(g_lineLimit);
|
||
15 years ago
|
|
||
|
while ( file.readLine(line.data(), g_lineLimit) >=0 ) {
|
||
|
lineno++;
|
||
|
|
||
|
// skip lines that didn't fit in buffer and first two headers lines
|
||
|
if ( line[line.length()-1] != '\n' || lineno <= 2 )
|
||
|
continue;
|
||
|
|
||
15 years ago
|
TQString currentLine = codec->toUnicode(line).stripWhiteSpace();
|
||
15 years ago
|
|
||
|
if (currentLine.isEmpty()) {
|
||
|
// end of data block
|
||
|
if (type.isNull())
|
||
|
continue;
|
||
|
else if ( type == "URL")
|
||
|
emit newBookmark( name, url.latin1(), "" );
|
||
|
else if (type == "FOLDER" )
|
||
|
emit newFolder( name, false, "" );
|
||
|
|
||
15 years ago
|
type = TQString::null;
|
||
|
name = TQString::null;
|
||
|
url = TQString::null;
|
||
15 years ago
|
|
||
|
} else if (currentLine == "-") {
|
||
|
// end of folder
|
||
|
emit endFolder();
|
||
|
|
||
|
} else {
|
||
|
// data block line
|
||
15 years ago
|
TQString tag;
|
||
15 years ago
|
if ( tag = "#", currentLine.startsWith( tag ) )
|
||
|
type = currentLine.remove( 0, tag.length() );
|
||
|
else if ( tag = "NAME=", currentLine.startsWith( tag ) )
|
||
|
name = currentLine.remove(0, tag.length());
|
||
|
else if ( tag = "URL=", currentLine.startsWith( tag ) )
|
||
|
url = currentLine.remove(0, tag.length());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
15 years ago
|
TQString KOperaBookmarkImporter::operaBookmarksFile()
|
||
15 years ago
|
{
|
||
|
static KOperaBookmarkImporterImpl *p = 0;
|
||
|
if (!p)
|
||
|
p = new KOperaBookmarkImporterImpl;
|
||
14 years ago
|
return p->findDefaultLocation();
|
||
15 years ago
|
}
|
||
|
|
||
|
void KOperaBookmarkImporterImpl::parse() {
|
||
|
KOperaBookmarkImporter importer(m_fileName);
|
||
|
setupSignalForwards(&importer, this);
|
||
|
importer.parseOperaBookmarks();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
|
||
15 years ago
|
{
|
||
|
return saving ? KFileDialog::getSaveFileName(
|
||
15 years ago
|
TQDir::homeDirPath() + "/.opera",
|
||
15 years ago
|
i18n("*.adr|Opera Bookmark Files (*.adr)") )
|
||
|
: KFileDialog::getOpenFileName(
|
||
15 years ago
|
TQDir::homeDirPath() + "/.opera",
|
||
15 years ago
|
i18n("*.adr|Opera Bookmark Files (*.adr)") );
|
||
|
}
|
||
|
|
||
|
/////////////////////////////////////////////////
|
||
|
|
||
|
class OperaExporter : private KBookmarkGroupTraverser {
|
||
|
public:
|
||
|
OperaExporter();
|
||
15 years ago
|
TQString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; };
|
||
15 years ago
|
private:
|
||
|
virtual void visit( const KBookmark & );
|
||
|
virtual void visitEnter( const KBookmarkGroup & );
|
||
|
virtual void visitLeave( const KBookmarkGroup & );
|
||
|
private:
|
||
15 years ago
|
TQString m_string;
|
||
|
TQTextStream m_out;
|
||
15 years ago
|
};
|
||
|
|
||
|
OperaExporter::OperaExporter() : m_out(&m_string, IO_WriteOnly) {
|
||
|
m_out << "Opera Hotlist version 2.0" << endl;
|
||
|
m_out << "Options: encoding = utf8, version=3" << endl;
|
||
|
}
|
||
|
|
||
|
void OperaExporter::visit( const KBookmark &bk ) {
|
||
|
// kdDebug() << "visit(" << bk.text() << ")" << endl;
|
||
|
m_out << "#URL" << endl;
|
||
|
m_out << "\tNAME=" << bk.fullText() << endl;
|
||
|
m_out << "\tURL=" << bk.url().url().utf8() << endl;
|
||
|
m_out << endl;
|
||
|
}
|
||
|
|
||
|
void OperaExporter::visitEnter( const KBookmarkGroup &grp ) {
|
||
|
// kdDebug() << "visitEnter(" << grp.text() << ")" << endl;
|
||
|
m_out << "#FOLDER" << endl;
|
||
|
m_out << "\tNAME="<< grp.fullText() << endl;
|
||
|
m_out << endl;
|
||
|
}
|
||
|
|
||
|
void OperaExporter::visitLeave( const KBookmarkGroup & ) {
|
||
|
// kdDebug() << "visitLeave()" << endl;
|
||
|
m_out << "-" << endl;
|
||
|
m_out << endl;
|
||
|
}
|
||
|
|
||
|
void KOperaBookmarkExporterImpl::write(KBookmarkGroup parent) {
|
||
|
OperaExporter exporter;
|
||
15 years ago
|
TQString content = exporter.generate( parent );
|
||
|
TQFile file(m_fileName);
|
||
15 years ago
|
if (!file.open(IO_WriteOnly)) {
|
||
|
kdError(7043) << "Can't write to file " << m_fileName << endl;
|
||
|
return;
|
||
|
}
|
||
15 years ago
|
TQTextStream fstream(&file);
|
||
|
fstream.setEncoding(TQTextStream::UnicodeUTF8);
|
||
15 years ago
|
fstream << content;
|
||
|
}
|
||
|
|
||
|
#include "kbookmarkimporter_opera.moc"
|