• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KIO

  • kio
  • bookmarks
kbookmarkimporter_ie.cc
Go to the documentation of this file.
1// -*- c-basic-offset:4; indent-tabs-mode:nil -*-
2// vim: set ts=4 sts=4 sw=4 et:
3/* This file is part of the KDE libraries
4 Copyright (C) 2002-2003 Alexander Kellett <lypanov@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20
21#include "kbookmarkimporter_ie.h"
22
23#include <kfiledialog.h>
24#include <kstringhandler.h>
25#include <klocale.h>
26#include <kdebug.h>
27#include <qtextcodec.h>
28#include <QtGui/QApplication>
29
30#include <sys/types.h>
31#include <stddef.h>
32#include <dirent.h>
33#include <sys/stat.h>
34
35#include "kbookmarkimporter.h"
36
41class KIEBookmarkImporter : public QObject
42{
43 Q_OBJECT
44public:
45 KIEBookmarkImporter( const QString & fileName ) : m_fileName(fileName) {}
46 ~KIEBookmarkImporter() {}
47
48 void parseIEBookmarks();
49
50 // Usual place for IE bookmarks
51 static QString IEBookmarksDir();
52
53Q_SIGNALS:
54 void newBookmark( const QString & text, const QString & url, const QString & additionalInfo );
55 void newFolder( const QString & text, bool open, const QString & additionalInfo );
56 void newSeparator();
57 void endFolder();
58
59protected:
60 void parseIEBookmarks_dir( const QString &dirname, const QString &name = QString() );
61 void parseIEBookmarks_url_file( const QString &filename, const QString &name );
62
63 QString m_fileName;
64};
65
66void KIEBookmarkImporter::parseIEBookmarks_url_file( const QString &filename, const QString &name ) {
67 static const int g_lineLimit = 16*1024;
68
69 QFile f(filename);
70
71 if(f.open(QIODevice::ReadOnly)) {
72
73 QByteArray s(g_lineLimit,0);
74
75 while(f.readLine(s.data(), g_lineLimit)>=0) {
76 if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than g_lineLimit. Skipping.
77 {
78 kWarning() << "IE bookmarks contain a line longer than " << g_lineLimit << ". Skipping.";
79 continue;
80 }
81 QByteArray t = s.trimmed();
82 QRegExp rx( "URL=(.*)" );
83 if (rx.exactMatch(t)) {
84 emit newBookmark( name, rx.cap(1), QString("") );
85 }
86 }
87
88 f.close();
89 }
90}
91
92void KIEBookmarkImporter::parseIEBookmarks_dir( const QString &dirname, const QString &foldername )
93{
94
95 QDir dir(dirname);
96 dir.setFilter( QDir::Files | QDir::Dirs | QDir::AllDirs );
97 dir.setSorting( QFlags<QDir::SortFlag>(QDir::Name | QDir::DirsFirst) );
98 dir.setNameFilters(QStringList("*.url")); // AK - possibly add ";index.ini" ?
99
100 const QFileInfoList list = dir.entryInfoList();
101 if (list.isEmpty()) return;
102
103 if (dirname != m_fileName)
104 emit newFolder( foldername, false, "" );
105
106 foreach (const QFileInfo &fi, list) {
107 if (fi.fileName() == "." || fi.fileName() == "..") continue;
108
109 if (fi.isDir()) {
110 parseIEBookmarks_dir(fi.absoluteFilePath(), fi.fileName());
111
112 } else if (fi.isFile()) {
113 if (fi.fileName().endsWith(QLatin1String(".url"))) {
114 QString name = fi.fileName();
115 name.truncate(name.length() - 4); // .url
116 parseIEBookmarks_url_file(fi.absoluteFilePath(), name);
117 }
118 // AK - add index.ini
119 }
120 }
121
122 if (dirname != m_fileName)
123 emit endFolder();
124}
125
126
127void KIEBookmarkImporter::parseIEBookmarks( )
128{
129 parseIEBookmarks_dir( m_fileName );
130}
131
132QString KIEBookmarkImporter::IEBookmarksDir()
133{
134 static KIEBookmarkImporterImpl* p = 0;
135 if (!p)
136 p = new KIEBookmarkImporterImpl;
137 return p->findDefaultLocation();
138}
139
140void KIEBookmarkImporterImpl::parse() {
141 KIEBookmarkImporter importer(m_fileName);
142 setupSignalForwards(&importer, this);
143 importer.parseIEBookmarks();
144}
145
146QString KIEBookmarkImporterImpl::findDefaultLocation(bool) const
147{
148 // notify user that they must give a new dir such
149 // as "Favourites" as otherwise it'll just place
150 // lots of .url files in the given dir and gui
151 // stuff in the exporter is ugly so that exclues
152 // the possibility of just writing to Favourites
153 // and checking if overwriting...
154 return KFileDialog::getExistingDirectory(KUrl(), QApplication::activeWindow());
155}
156
158
159class IEExporter : private KBookmarkGroupTraverser {
160public:
161 IEExporter( const QString & );
162 void write( const KBookmarkGroup &grp ) { traverse(grp); }
163private:
164 virtual void visit( const KBookmark & );
165 virtual void visitEnter( const KBookmarkGroup & );
166 virtual void visitLeave( const KBookmarkGroup & );
167private:
168 QDir m_currentDir;
169};
170
171static QString ieStyleQuote( const QString &str ) {
172 QString s(str);
173 s.replace(QRegExp("[/\\:*?\"<>|]"), "_");
174 return s;
175}
176
177IEExporter::IEExporter( const QString & dname ) {
178 m_currentDir.setPath( dname );
179}
180
181void IEExporter::visit( const KBookmark &bk ) {
182 QString fname = m_currentDir.path() + '/' + ieStyleQuote( bk.fullText() ) + ".url";
183 // kDebug() << "visit(" << bk.text() << "), fname == " << fname;
184 QFile file( fname );
185 if (file.open(QIODevice::WriteOnly)) {
186 QTextStream ts( &file );
187 ts << "[InternetShortcut]\r\n";
188 ts << "URL=" << bk.url().url().toUtf8() << "\r\n";
189 }
190}
191
192void IEExporter::visitEnter( const KBookmarkGroup &grp ) {
193 QString dname = m_currentDir.path() + '/' + ieStyleQuote( grp.fullText() );
194 // kDebug() << "visitEnter(" << grp.text() << "), dname == " << dname;
195 m_currentDir.mkdir( dname );
196 m_currentDir.cd( dname );
197}
198
199void IEExporter::visitLeave( const KBookmarkGroup & ) {
200 // kDebug() << "visitLeave()";
201 m_currentDir.cdUp();
202}
203
204void KIEBookmarkExporterImpl::write(const KBookmarkGroup& parent) {
205 IEExporter exporter( m_fileName );
206 exporter.write( parent );
207}
208
209#include "kbookmarkimporter_ie.moc"
KBookmarkExporterBase::m_fileName
QString m_fileName
Definition: kbookmarkexporter.h:38
KBookmarkGroupTraverser
Definition: kbookmark.h:459
KBookmarkGroupTraverser::traverse
void traverse(const KBookmarkGroup &)
Definition: kbookmark.cc:621
KBookmarkGroup
A group of bookmarks.
Definition: kbookmark.h:348
KBookmarkImporterBase::m_fileName
QString m_fileName
Definition: kbookmarkimporter.h:75
KBookmarkImporterBase::setupSignalForwards
void setupSignalForwards(QObject *src, QObject *dst)
Definition: kbookmarkimporter.cc:73
KBookmark
Definition: kbookmark.h:35
KBookmark::fullText
QString fullText() const
Text shown for the bookmark, not truncated.
Definition: kbookmark.cc:311
KBookmark::url
KUrl url() const
URL contained by the bookmark.
Definition: kbookmark.cc:338
KFileDialog::getExistingDirectory
static QString getExistingDirectory(const KUrl &startDir=KUrl(), QWidget *parent=0, const QString &caption=QString())
Creates a modal directory-selection dialog and returns the selected directory (local only) or an empt...
Definition: kfiledialog.cpp:630
KIEBookmarkExporterImpl::write
virtual void write(const KBookmarkGroup &)
Definition: kbookmarkimporter_ie.cc:204
KIEBookmarkImporterImpl
A class for importing IE bookmarks.
Definition: kbookmarkimporter_ie.h:35
KIEBookmarkImporterImpl::findDefaultLocation
virtual QString findDefaultLocation(bool forSaving=false) const
Definition: kbookmarkimporter_ie.cc:146
KIEBookmarkImporterImpl::parse
virtual void parse()
Definition: kbookmarkimporter_ie.cc:140
KUrl
KUrl::url
QString url(AdjustPathOption trailing=LeaveTrailingSlash) const
QObject
f
static quint32 f(DES_KEY *key, quint32 r, char *subkey)
Definition: des.cpp:378
kWarning
#define kWarning
kbookmarkimporter.h
ieStyleQuote
static QString ieStyleQuote(const QString &str)
Definition: kbookmarkimporter_ie.cc:171
kbookmarkimporter_ie.h
kdebug.h
kfiledialog.h
klocale.h
kstringhandler.h
KRecentDirs::dir
QString dir(const QString &fileClass)
Returns the most recently used directory accociated with this file-class.
Definition: krecentdirs.cpp:68
KRecentDirs::list
QStringList list(const QString &fileClass)
Returns a list of directories associated with this file-class.
Definition: krecentdirs.cpp:60
name
const char * name(StandardAction id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KIO

Skip menu "KIO"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal