KBookmarks

kbookmarkimporter_ie.cpp
1 // -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2 /* This file is part of the KDE libraries
3  Copyright (C) 2002-2003 Alexander Kellett <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "kbookmarkimporter_ie.h"
21 
22 #include <QFileDialog>
23 
24 #include "kbookmarks_debug.h"
25 #include <QTextCodec>
26 #include <QApplication>
27 #include <QRegularExpression>
28 
29 #include <qplatformdefs.h>
30 
31 #include "kbookmarkimporter.h"
32 
37 class KIEBookmarkImporter : public QObject
38 {
39  Q_OBJECT
40 public:
41  KIEBookmarkImporter(const QString &fileName) : m_fileName(fileName) {}
42  ~KIEBookmarkImporter() {}
43 
44  void parseIEBookmarks();
45 
46  // Usual place for IE bookmarks
47  static QString IEBookmarksDir();
48 
49 Q_SIGNALS:
50  void newBookmark(const QString &text, const QString &url, const QString &additionalInfo);
51  void newFolder(const QString &text, bool open, const QString &additionalInfo);
52  void newSeparator();
53  void endFolder();
54 
55 protected:
56  void parseIEBookmarks_dir(const QString &dirname, const QString &name = QString());
57  void parseIEBookmarks_url_file(const QString &filename, const QString &name);
58 
59  QString m_fileName;
60 };
61 
62 void KIEBookmarkImporter::parseIEBookmarks_url_file(const QString &filename, const QString &name)
63 {
64  static const int g_lineLimit = 16 * 1024;
65 
66  QFile f(filename);
67 
68  if (f.open(QIODevice::ReadOnly)) {
69 
70  QByteArray s(g_lineLimit, 0);
71 
72  while (f.readLine(s.data(), g_lineLimit) >= 0) {
73  if (s[s.length() - 1] != '\n') { // Gosh, this line is longer than g_lineLimit. Skipping.
74  qCWarning(KBOOKMARKS_LOG) << "IE bookmarks contain a line longer than " << g_lineLimit << ". Skipping.";
75  continue;
76  }
77  const QString t = QString::fromUtf8(s.trimmed());
78  QRegularExpression rx(QStringLiteral("URL=(.*)"));
79  auto match = rx.match(t);
80  if (match.hasMatch()) {
81  emit newBookmark(name, match.captured(1), QLatin1String(""));
82  }
83  }
84 
85  f.close();
86  }
87 }
88 
89 void KIEBookmarkImporter::parseIEBookmarks_dir(const QString &dirname, const QString &foldername)
90 {
91 
92  QDir dir(dirname);
93  dir.setFilter(QDir::Files | QDir::Dirs | QDir::AllDirs);
95  dir.setNameFilters(QStringList(QStringLiteral("*.url"))); // AK - possibly add ";index.ini" ?
96 
97  const QFileInfoList list = dir.entryInfoList();
98  if (list.isEmpty()) {
99  return;
100  }
101 
102  if (dirname != m_fileName) {
103  emit newFolder(foldername, false, QLatin1String(""));
104  }
105 
106  for (const QFileInfo &fi : list) {
107  if (fi.fileName() == QLatin1String(".") || fi.fileName() == QLatin1String("..")) {
108  continue;
109  }
110 
111  if (fi.isDir()) {
112  parseIEBookmarks_dir(fi.absoluteFilePath(), fi.fileName());
113 
114  } else if (fi.isFile()) {
115  if (fi.fileName().endsWith(QLatin1String(".url"))) {
116  QString name = fi.fileName();
117  name.truncate(name.length() - 4); // .url
118  parseIEBookmarks_url_file(fi.absoluteFilePath(), name);
119  }
120  // AK - add index.ini
121  }
122  }
123 
124  if (dirname != m_fileName) {
125  emit endFolder();
126  }
127 }
128 
129 void KIEBookmarkImporter::parseIEBookmarks()
130 {
131  parseIEBookmarks_dir(m_fileName);
132 }
133 
134 QString KIEBookmarkImporter::IEBookmarksDir()
135 {
136  static KIEBookmarkImporterImpl *p = nullptr;
137  if (!p) {
138  p = new KIEBookmarkImporterImpl;
139  }
140  return p->findDefaultLocation();
141 }
142 
143 void KIEBookmarkImporterImpl::parse()
144 {
145  KIEBookmarkImporter importer(m_fileName);
146  setupSignalForwards(&importer, this);
147  importer.parseIEBookmarks();
148 }
149 
150 QString KIEBookmarkImporterImpl::findDefaultLocation(bool) const
151 {
152  // notify user that they must give a new dir such
153  // as "Favourites" as otherwise it'll just place
154  // lots of .url files in the given dir and gui
155  // stuff in the exporter is ugly so that exclues
156  // the possibility of just writing to Favourites
157  // and checking if overwriting...
159 }
160 
162 
163 class IEExporter : private KBookmarkGroupTraverser
164 {
165 public:
166  IEExporter(const QString &);
167  void write(const KBookmarkGroup &grp)
168  {
169  traverse(grp);
170  }
171 private:
172  void visit(const KBookmark &) override;
173  void visitEnter(const KBookmarkGroup &) override;
174  void visitLeave(const KBookmarkGroup &) override;
175 private:
176  QDir m_currentDir;
177 };
178 
179 static QString ieStyleQuote(const QString &str)
180 {
181  QString s(str);
182  s.replace(QRegularExpression(QStringLiteral("[/\\:*?\"<>|]")), QStringLiteral("_"));
183  return s;
184 }
185 
186 IEExporter::IEExporter(const QString &dname)
187 {
188  m_currentDir.setPath(dname);
189 }
190 
191 void IEExporter::visit(const KBookmark &bk)
192 {
193  const QString fname = m_currentDir.path() + QLatin1Char('/') + ieStyleQuote(bk.fullText()) + QLatin1String(".url");
194  // qCDebug(KBOOKMARKS_LOG) << "visit(" << bk.text() << "), fname == " << fname;
195  QFile file(fname);
196  if (file.open(QIODevice::WriteOnly)) {
197  QTextStream ts(&file);
198  ts << "[InternetShortcut]\r\n";
199  ts << "URL=" << bk.url().toString().toUtf8() << "\r\n";
200  }
201 }
202 
203 void IEExporter::visitEnter(const KBookmarkGroup &grp)
204 {
205  const QString dname = m_currentDir.path() + QLatin1Char('/') + ieStyleQuote(grp.fullText());
206  // qCDebug(KBOOKMARKS_LOG) << "visitEnter(" << grp.text() << "), dname == " << dname;
207  m_currentDir.mkdir(dname);
208  m_currentDir.cd(dname);
209 }
210 
211 void IEExporter::visitLeave(const KBookmarkGroup &)
212 {
213  // qCDebug(KBOOKMARKS_LOG) << "visitLeave()";
214  m_currentDir.cdUp();
215 }
216 
217 void KIEBookmarkExporterImpl::write(const KBookmarkGroup &parent)
218 {
219  IEExporter exporter(m_fileName);
220  exporter.write(parent);
221 }
222 
223 #include "kbookmarkimporter_ie.moc"
QString getExistingDirectory(QWidget *parent, const QString &caption, const QString &dir, QFileDialog::Options options)
void truncate(int position)
A class for importing IE bookmarks.
Q_SIGNALSQ_SIGNALS
QWidget * activeWindow()
QString fromUtf8(const char *str, int size)
Q_OBJECTQ_OBJECT
A group of bookmarks.
Definition: kbookmark.h:327
KIOFILEWIDGETS_EXPORT QString dir(const QString &fileClass)
int length() const const
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 14 2020 22:40:02 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.