• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeio/bookmarks
 

tdeio/bookmarks

  • tdeio
  • bookmarks
kbookmarkimporter_opera.cpp
1/* This file is part of the KDE libraries
2 Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include <tdefiledialog.h>
20#include <kstringhandler.h>
21#include <tdelocale.h>
22#include <kdebug.h>
23#include <tqtextcodec.h>
24
25#include <sys/types.h>
26#include <stddef.h>
27#include <dirent.h>
28#include <sys/stat.h>
29
30#include "kbookmarkimporter.h"
31#include "kbookmarkimporter_opera.h"
32
33void KOperaBookmarkImporter::parseOperaBookmarks( )
34{
35 TQFile file(m_fileName);
36 if(!file.open(IO_ReadOnly)) {
37 return;
38 }
39
40 TQTextCodec * codec = TQTextCodec::codecForName("UTF-8");
41 Q_ASSERT(codec);
42 if (!codec)
43 return;
44
45 int lineno = 0;
46 TQString url, name, type;
47 static const int g_lineLimit = 16*1024;
48 TQCString line(g_lineLimit);
49
50 while ( file.readLine(line.data(), g_lineLimit) >=0 ) {
51 lineno++;
52
53 // skip lines that didn't fit in buffer and first two headers lines
54 if ( line[line.length()-1] != '\n' || lineno <= 2 )
55 continue;
56
57 TQString currentLine = codec->toUnicode(line).stripWhiteSpace();
58
59 if (currentLine.isEmpty()) {
60 // end of data block
61 if (type.isNull())
62 continue;
63 else if ( type == "URL")
64 emit newBookmark( name, url.latin1(), "" );
65 else if (type == "FOLDER" )
66 emit newFolder( name, false, "" );
67
68 type = TQString::null;
69 name = TQString::null;
70 url = TQString::null;
71
72 } else if (currentLine == "-") {
73 // end of folder
74 emit endFolder();
75
76 } else {
77 // data block line
78 TQString tag;
79 if ( tag = "#", currentLine.startsWith( tag ) )
80 type = currentLine.remove( 0, tag.length() );
81 else if ( tag = "NAME=", currentLine.startsWith( tag ) )
82 name = currentLine.remove(0, tag.length());
83 else if ( tag = "URL=", currentLine.startsWith( tag ) )
84 url = currentLine.remove(0, tag.length());
85 }
86 }
87
88}
89
90TQString KOperaBookmarkImporter::operaBookmarksFile()
91{
92 static KOperaBookmarkImporterImpl *p = 0;
93 if (!p)
94 p = new KOperaBookmarkImporterImpl;
95 return p->findDefaultLocation();
96}
97
98void KOperaBookmarkImporterImpl::parse() {
99 KOperaBookmarkImporter importer(m_fileName);
100 setupSignalForwards(&importer, this);
101 importer.parseOperaBookmarks();
102}
103
104TQString KOperaBookmarkImporterImpl::findDefaultLocation(bool saving) const
105{
106 return saving ? KFileDialog::getSaveFileName(
107 TQDir::homeDirPath() + "/.opera",
108 i18n("*.adr|Opera Bookmark Files (*.adr)") )
109 : KFileDialog::getOpenFileName(
110 TQDir::homeDirPath() + "/.opera",
111 i18n("*.adr|Opera Bookmark Files (*.adr)") );
112}
113
115
116class OperaExporter : private KBookmarkGroupTraverser {
117public:
118 OperaExporter();
119 TQString generate( const KBookmarkGroup &grp ) { traverse(grp); return m_string; };
120private:
121 virtual void visit( const KBookmark & );
122 virtual void visitEnter( const KBookmarkGroup & );
123 virtual void visitLeave( const KBookmarkGroup & );
124private:
125 TQString m_string;
126 TQTextStream m_out;
127};
128
129OperaExporter::OperaExporter() : m_out(&m_string, IO_WriteOnly) {
130 m_out << "Opera Hotlist version 2.0" << endl;
131 m_out << "Options: encoding = utf8, version=3" << endl;
132}
133
134void OperaExporter::visit( const KBookmark &bk ) {
135 // kdDebug() << "visit(" << bk.text() << ")" << endl;
136 m_out << "#URL" << endl;
137 m_out << "\tNAME=" << bk.fullText() << endl;
138 m_out << "\tURL=" << bk.url().url().utf8() << endl;
139 m_out << endl;
140}
141
142void OperaExporter::visitEnter( const KBookmarkGroup &grp ) {
143 // kdDebug() << "visitEnter(" << grp.text() << ")" << endl;
144 m_out << "#FOLDER" << endl;
145 m_out << "\tNAME="<< grp.fullText() << endl;
146 m_out << endl;
147}
148
149void OperaExporter::visitLeave( const KBookmarkGroup & ) {
150 // kdDebug() << "visitLeave()" << endl;
151 m_out << "-" << endl;
152 m_out << endl;
153}
154
155void KOperaBookmarkExporterImpl::write(KBookmarkGroup parent) {
156 OperaExporter exporter;
157 TQString content = exporter.generate( parent );
158 TQFile file(m_fileName);
159 if (!file.open(IO_WriteOnly)) {
160 kdError(7043) << "Can't write to file " << m_fileName << endl;
161 return;
162 }
163 TQTextStream fstream(&file);
164 fstream.setEncoding(TQTextStream::UnicodeUTF8);
165 fstream << content;
166}
167
168#include "kbookmarkimporter_opera.moc"
KBookmarkGroupTraverser
Definition: kbookmark.h:316
KBookmarkGroup
A group of bookmarks.
Definition: kbookmark.h:198
KOperaBookmarkImporterImpl
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera.h:60
KOperaBookmarkImporter
A class for importing Opera bookmarks.
Definition: kbookmarkimporter_opera.h:34

tdeio/bookmarks

Skip menu "tdeio/bookmarks"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

tdeio/bookmarks

Skip menu "tdeio/bookmarks"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/bookmarks by doxygen 1.9.4
This website is maintained by Timothy Pearson.