21 #include "kbookmarkimporter.h"
22 #include "kbookmarkexporter.h"
23 #include "kbookmarkmanager.h"
24 #include <tdefiledialog.h>
25 #include <kstringhandler.h>
26 #include <tdelocale.h>
28 #include <kcharsets.h>
29 #include <tqtextcodec.h>
30 #include <tqstylesheet.h>
32 #include <sys/types.h>
38 void KNSBookmarkImporterImpl::parse()
41 TQTextCodec * codec = m_utf8 ? TQTextCodec::codecForName(
"UTF-8") : TQTextCodec::codecForLocale();
46 if(f.open(IO_ReadOnly)) {
48 static const int g_lineLimit = 16*1024;
49 TQCString s(g_lineLimit);
51 while(f.readLine(s.data(), g_lineLimit) >= 0 && !s.contains(
"<DL>"));
53 while(f.readLine(s.data(), g_lineLimit)>=0) {
54 if ( s[s.length()-1] !=
'\n' )
56 kdWarning() <<
"Netscape bookmarks contain a line longer than " << g_lineLimit <<
". Skipping." << endl;
59 TQCString t = s.stripWhiteSpace();
60 if(t.left(12).upper() ==
"<DT><A HREF=" ||
61 t.left(16).upper() ==
"<DT><H3><A HREF=") {
62 int firstQuotes = t.find(
'"')+1;
63 int secondQuotes = t.find(
'"', firstQuotes);
64 if (firstQuotes != -1 && secondQuotes != -1)
66 TQCString link = t.mid(firstQuotes, secondQuotes-firstQuotes);
67 int endTag = t.find(
'>', secondQuotes+1);
68 TQCString name = t.mid(endTag+1);
69 name = name.left(name.findRev(
'<'));
70 if ( name.right(4) ==
"</A>" )
71 name = name.left( name.length() - 4 );
72 TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
73 TQCString additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
76 link, codec->toUnicode(additionalInfo) );
79 else if(t.left(7).upper() ==
"<DT><H3") {
80 int endTag = t.find(
'>', 7);
81 TQCString name = t.mid(endTag+1);
82 name = name.left(name.findRev(
'<'));
83 TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
84 TQCString additionalInfo = t.mid( 8, endTag-8 );
85 bool folded = (additionalInfo.left(6) ==
"FOLDED");
86 if (folded) additionalInfo.remove(0,7);
90 codec->toUnicode(additionalInfo) );
92 else if(t.left(4).upper() ==
"<HR>")
94 else if(t.left(8).upper() ==
"</DL><P>")
102 TQString KNSBookmarkImporterImpl::findDefaultLocation(
bool forSaving)
const
107 return KFileDialog::getSaveFileName( TQDir::homeDirPath() +
"/.mozilla",
108 i18n(
"*.html|HTML Files (*.html)") );
110 return KFileDialog::getOpenFileName( TQDir::homeDirPath() +
"/.mozilla",
111 i18n(
"*.html|HTML Files (*.html)") );
115 return TQDir::homeDirPath() +
"/.netscape/bookmarks.html";
122 void KNSBookmarkImporter::parseNSBookmarks(
bool utf8 )
125 importer.setFilename(m_fileName);
126 importer.setUtf8(utf8);
127 importer.setupSignalForwards(&importer,
this);
131 TQString KNSBookmarkImporter::netscapeBookmarksFile(
bool forSaving )
139 return p->findDefaultLocation(forSaving);
142 TQString KNSBookmarkImporter::mozillaBookmarksFile(
bool forSaving )
150 return p->findDefaultLocation(forSaving);
158 void KNSBookmarkExporter::write(
bool utf8) {
160 exporter.setUtf8(utf8);
161 exporter.write(m_pManager->
root());
164 void KNSBookmarkExporter::writeFolder(TQTextStream &,
KBookmarkGroup ) {
170 void KNSBookmarkExporterImpl::setUtf8(
bool utf8) {
175 if (TQFile::exists(m_fileName)) {
177 TQFile::encodeName(m_fileName),
178 TQFile::encodeName(m_fileName +
".beforekde"));
181 TQFile file(m_fileName);
183 if (!file.open(IO_WriteOnly)) {
184 kdError(7043) <<
"Can't write to file " << m_fileName << endl;
188 TQTextStream fstream(&file);
189 fstream.setEncoding(m_utf8 ? TQTextStream::UnicodeUTF8 : TQTextStream::Locale);
192 = m_utf8 ?
"UTF-8" : TQString::fromLatin1(TQTextCodec::codecForLocale()->name()).upper();
194 fstream <<
"<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
195 << i18n(
"<!-- This file was generated by Konqueror -->") << endl
196 <<
"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset="
197 << charset <<
"\">" << endl
198 <<
"<TITLE>" << i18n(
"Bookmarks") <<
"</TITLE>" << endl
199 <<
"<H1>" << i18n(
"Bookmarks") <<
"</H1>" << endl
201 << folderAsString(parent)
202 <<
"</DL><P>" << endl;
205 TQString KNSBookmarkExporterImpl::folderAsString(
KBookmarkGroup parent)
const {
207 TQTextStream fstream(&str, IO_WriteOnly);
209 for (KBookmark bk = parent.
first(); !bk.isNull(); bk = parent.
next(bk)) {
210 if (bk.isSeparator()) {
211 fstream <<
"<HR>" << endl;
215 TQString text = TQStyleSheet::escape(bk.fullText());
218 fstream <<
"<DT><H3 "
219 << (!bk.toGroup().isOpen() ?
"FOLDED " :
"")
220 << bk.internalElement().attribute(
"netscapeinfo") <<
">"
221 << text <<
"</H3>" << endl
223 << folderAsString(bk.toGroup())
224 <<
"</DL><P>" << endl;
229 fstream <<
"<DT><A HREF=\"" << bk.url().url() <<
"\""
230 << bk.internalElement().attribute(
"netscapeinfo") <<
">"
231 << text <<
"</A>" << endl;
241 #include "kbookmarkimporter_ns.moc"
KBookmark next(const KBookmark ¤t) const
Return the next sibling of a child bookmark of this group.
KBookmark first() const
Return the first child bookmark of this group.
void newFolder(const TQString &text, bool open, const TQString &additionalInfo)
Notify about a new folder Use "bookmark_folder" for the icon.
void newSeparator()
Notify about a new separator.
void endFolder()
Tell the outside world that we're going down one menu.
void newBookmark(const TQString &text, const TQCString &url, const TQString &additionalInfo)
Notify about a new bookmark Use "html" for the icon.
KBookmarkGroup root() const
This will return the root bookmark.
A class for importing NS bookmarks utf8 defaults to off.