23 #include "kbookmarkimporter.h" 24 #include "kbookmarkexporter.h" 25 #include "kbookmarkmanager.h" 26 #include <kfiledialog.h> 27 #include <kstringhandler.h> 30 #include <kcharsets.h> 31 #include <tqtextcodec.h> 32 #include <tqstylesheet.h> 34 #include <sys/types.h> 40 void KNSBookmarkImporterImpl::parse()
43 TQTextCodec * codec = m_utf8 ? TQTextCodec::codecForName(
"UTF-8") : TQTextCodec::codecForLocale();
48 if(f.open(IO_ReadOnly)) {
50 static const int g_lineLimit = 16*1024;
51 TQCString s(g_lineLimit);
53 while(f.readLine(s.data(), g_lineLimit) >= 0 && !s.contains(
"<DL>"));
55 while(f.readLine(s.data(), g_lineLimit)>=0) {
56 if ( s[s.length()-1] !=
'\n' )
58 kdWarning() <<
"Netscape bookmarks contain a line longer than " << g_lineLimit <<
". Skipping." << endl;
61 TQCString t = s.stripWhiteSpace();
62 if(t.left(12).upper() ==
"<DT><A HREF=" ||
63 t.left(16).upper() ==
"<DT><H3><A HREF=") {
64 int firstQuotes = t.find(
'"')+1;
65 int secondQuotes = t.find(
'"', firstQuotes);
66 if (firstQuotes != -1 && secondQuotes != -1)
68 TQCString link = t.mid(firstQuotes, secondQuotes-firstQuotes);
69 int endTag = t.find(
'>', secondQuotes+1);
70 TQCString name = t.mid(endTag+1);
71 name = name.left(name.findRev(
'<'));
72 if ( name.right(4) ==
"</A>" )
73 name = name.left( name.length() - 4 );
74 TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
75 TQCString additionalInfo = t.mid( secondQuotes+1, endTag-secondQuotes-1 );
78 link, codec->toUnicode(additionalInfo) );
81 else if(t.left(7).upper() ==
"<DT><H3") {
82 int endTag = t.find(
'>', 7);
83 TQCString name = t.mid(endTag+1);
84 name = name.left(name.findRev(
'<'));
85 TQString qname = KCharsets::resolveEntities( codec->toUnicode( name ) );
86 TQCString additionalInfo = t.mid( 8, endTag-8 );
87 bool folded = (additionalInfo.left(6) ==
"FOLDED");
88 if (folded) additionalInfo.remove(0,7);
92 codec->toUnicode(additionalInfo) );
94 else if(t.left(4).upper() ==
"<HR>")
96 else if(t.left(8).upper() ==
"</DL><P>")
104 TQString KNSBookmarkImporterImpl::findDefaultLocation(
bool forSaving)
const 109 return KFileDialog::getSaveFileName( TQDir::homeDirPath() +
"/.mozilla",
110 i18n(
"*.html|HTML Files (*.html)") );
112 return KFileDialog::getOpenFileName( TQDir::homeDirPath() +
"/.mozilla",
113 i18n(
"*.html|HTML Files (*.html)") );
117 return TQDir::homeDirPath() +
"/.netscape/bookmarks.html";
124 void KNSBookmarkImporter::parseNSBookmarks(
bool utf8 )
127 importer.setFilename(m_fileName);
128 importer.setUtf8(utf8);
129 importer.setupSignalForwards(&importer,
this);
133 TQString KNSBookmarkImporter::netscapeBookmarksFile(
bool forSaving )
141 return p->findDefaultLocation(forSaving);
144 TQString KNSBookmarkImporter::mozillaBookmarksFile(
bool forSaving )
152 return p->findDefaultLocation(forSaving);
160 void KNSBookmarkExporter::write(
bool utf8) {
162 exporter.setUtf8(utf8);
163 exporter.write(m_pManager->root());
166 void KNSBookmarkExporter::writeFolder(TQTextStream &,
KBookmarkGroup ) {
172 void KNSBookmarkExporterImpl::setUtf8(
bool utf8) {
177 if (TQFile::exists(m_fileName)) {
179 TQFile::encodeName(m_fileName),
180 TQFile::encodeName(m_fileName +
".beforekde"));
183 TQFile file(m_fileName);
185 if (!file.open(IO_WriteOnly)) {
186 kdError(7043) <<
"Can't write to file " << m_fileName << endl;
190 TQTextStream fstream(&file);
191 fstream.setEncoding(m_utf8 ? TQTextStream::UnicodeUTF8 : TQTextStream::Locale);
194 = m_utf8 ?
"UTF-8" : TQString::fromLatin1(TQTextCodec::codecForLocale()->name()).upper();
196 fstream <<
"<!DOCTYPE NETSCAPE-Bookmark-file-1>" << endl
197 << i18n(
"<!-- This file was generated by Konqueror -->") << endl
198 <<
"<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=" 199 << charset <<
"\">" << endl
200 <<
"<TITLE>" << i18n(
"Bookmarks") <<
"</TITLE>" << endl
201 <<
"<H1>" << i18n(
"Bookmarks") <<
"</H1>" << endl
203 << folderAsString(parent)
204 <<
"</DL><P>" << endl;
207 TQString KNSBookmarkExporterImpl::folderAsString(
KBookmarkGroup parent)
const {
209 TQTextStream fstream(&str, IO_WriteOnly);
211 for (KBookmark bk = parent.
first(); !bk.isNull(); bk = parent.
next(bk)) {
212 if (bk.isSeparator()) {
213 fstream <<
"<HR>" << endl;
217 TQString text = TQStyleSheet::escape(bk.fullText());
220 fstream <<
"<DT><H3 " 221 << (!bk.toGroup().isOpen() ?
"FOLDED " :
"")
222 << bk.internalElement().attribute(
"netscapeinfo") <<
">" 223 << text <<
"</H3>" << endl
225 << folderAsString(bk.toGroup())
226 <<
"</DL><P>" << endl;
231 fstream <<
"<DT><A HREF=\"" << bk.url().url() <<
"\"" 232 << bk.internalElement().attribute(
"netscapeinfo") <<
">" 233 << text <<
"</A>" << endl;
243 #include "kbookmarkimporter_ns.moc" A class for importing NS bookmarks utf8 defaults to off.
void endFolder()
Tell the outside world that we're going down one menu.
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 newBookmark(const TQString &text, const TQCString &url, const TQString &additionalInfo)
Notify about a new bookmark Use "html" for the icon.
void newSeparator()
Notify about a new separator.
KBookmark next(const KBookmark ¤t) const
Return the next sibling of a child bookmark of this group.