kio
kbookmarkimporter_ie.cc
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <kfiledialog.h>
00022 #include <kstringhandler.h>
00023 #include <klocale.h>
00024 #include <kdebug.h>
00025 #include <qtextcodec.h>
00026
00027 #include <sys/types.h>
00028 #include <stddef.h>
00029 #include <dirent.h>
00030 #include <sys/stat.h>
00031
00032 #include "kbookmarkimporter.h"
00033 #include "kbookmarkimporter_ie.h"
00034
00035
00036 void KIEBookmarkImporter::parseIEBookmarks_url_file( QString filename, QString name ) {
00037 static const int g_lineLimit = 16*1024;
00038
00039 QFile f(filename);
00040
00041 if(f.open(IO_ReadOnly)) {
00042
00043 QCString s(g_lineLimit);
00044
00045 while(f.readLine(s.data(), g_lineLimit)>=0) {
00046 if ( s[s.length()-1] != '\n' )
00047 {
00048 kdWarning() << "IE bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl;
00049 continue;
00050 }
00051 QCString t = s.stripWhiteSpace();
00052 QRegExp rx( "URL=(.*)" );
00053 if (rx.exactMatch(t)) {
00054 emit newBookmark( name, rx.cap(1).latin1(), QString("") );
00055 }
00056 }
00057
00058 f.close();
00059 }
00060 }
00061
00062
00063 void KIEBookmarkImporter::parseIEBookmarks_dir( QString dirname, QString foldername )
00064 {
00065
00066 QDir dir(dirname);
00067 dir.setFilter( QDir::Files | QDir::Dirs );
00068 dir.setSorting( QDir::Name | QDir::DirsFirst );
00069 dir.setNameFilter("*.url");
00070 dir.setMatchAllDirs(true);
00071
00072 const QFileInfoList *list = dir.entryInfoList();
00073 if (!list) return;
00074
00075 if (dirname != m_fileName)
00076 emit newFolder( foldername, false, "" );
00077
00078 QFileInfoListIterator it( *list );
00079 QFileInfo *fi;
00080
00081 while ( (fi = it.current()) != 0 ) {
00082 ++it;
00083
00084 if (fi->fileName() == "." || fi->fileName() == "..") continue;
00085
00086 if (fi->isDir()) {
00087 parseIEBookmarks_dir(fi->absFilePath(), fi->fileName());
00088
00089 } else if (fi->isFile()) {
00090 if (fi->fileName().endsWith(".url")) {
00091 QString name = fi->fileName();
00092 name.truncate(name.length() - 4);
00093 parseIEBookmarks_url_file(fi->absFilePath(), name);
00094 }
00095
00096 }
00097 }
00098
00099 if (dirname != m_fileName)
00100 emit endFolder();
00101 }
00102
00103
00104 void KIEBookmarkImporter::parseIEBookmarks( )
00105 {
00106 parseIEBookmarks_dir( m_fileName );
00107 }
00108
00109 QString KIEBookmarkImporter::IEBookmarksDir()
00110 {
00111 static KIEBookmarkImporterImpl* p = 0;
00112 if (!p)
00113 p = new KIEBookmarkImporterImpl;
00114 return p->findDefaultLocation();
00115 }
00116
00117 void KIEBookmarkImporterImpl::parse() {
00118 KIEBookmarkImporter importer(m_fileName);
00119 setupSignalForwards(&importer, this);
00120 importer.parseIEBookmarks();
00121 }
00122
00123 QString KIEBookmarkImporterImpl::findDefaultLocation(bool) const
00124 {
00125
00126
00127
00128
00129
00130
00131 return KFileDialog::getExistingDirectory();
00132 }
00133
00135
00136 class IEExporter : private KBookmarkGroupTraverser {
00137 public:
00138 IEExporter( const QString & );
00139 void write( const KBookmarkGroup &grp ) { traverse(grp); };
00140 private:
00141 virtual void visit( const KBookmark & );
00142 virtual void visitEnter( const KBookmarkGroup & );
00143 virtual void visitLeave( const KBookmarkGroup & );
00144 private:
00145 QDir m_currentDir;
00146 };
00147
00148 static QString ieStyleQuote( const QString &str ) {
00149 QString s(str);
00150 s.replace(QRegExp("[/\\:*?\"<>|]"), "_");
00151 return s;
00152 }
00153
00154 IEExporter::IEExporter( const QString & dname ) {
00155 m_currentDir.setPath( dname );
00156 }
00157
00158 void IEExporter::visit( const KBookmark &bk ) {
00159 QString fname = m_currentDir.path() + "/" + ieStyleQuote( bk.fullText() ) + ".url";
00160
00161 QFile file( fname );
00162 file.open( IO_WriteOnly );
00163 QTextStream ts( &file );
00164 ts << "[InternetShortcut]\r\n";
00165 ts << "URL=" << bk.url().url().utf8() << "\r\n";
00166 }
00167
00168 void IEExporter::visitEnter( const KBookmarkGroup &grp ) {
00169 QString dname = m_currentDir.path() + "/" + ieStyleQuote( grp.fullText() );
00170
00171 m_currentDir.mkdir( dname );
00172 m_currentDir.cd( dname );
00173 }
00174
00175 void IEExporter::visitLeave( const KBookmarkGroup & ) {
00176
00177 m_currentDir.cdUp();
00178 }
00179
00180 void KIEBookmarkExporterImpl::write(KBookmarkGroup parent) {
00181 IEExporter exporter( m_fileName );
00182 exporter.write( parent );
00183 }
00184
00185 #include "kbookmarkimporter_ie.moc"