summaryrefslogtreecommitdiffstats
path: root/tdeio/bookmarks/kbookmarkimporter_ie.cc
blob: 092ebe5968575ce344fd008b1defdb7ceda52b57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//  -*- c-basic-offset:4; indent-tabs-mode:nil -*-
// vim: set ts=4 sts=4 sw=4 et:
/* This file is part of the KDE libraries
   Copyright (C) 2002-2003  Alexander Kellett <lypanov@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tdefiledialog.h>
#include <kstringhandler.h>
#include <klocale.h>
#include <kdebug.h>
#include <tqtextcodec.h>

#include <sys/types.h>
#include <stddef.h>
#include <dirent.h>
#include <sys/stat.h>

#include "kbookmarkimporter.h"
#include "kbookmarkimporter_ie.h"

/* antlarr: KDE 4: Make them const TQString & */
void KIEBookmarkImporter::parseIEBookmarks_url_file( TQString filename, TQString name ) {
    static const int g_lineLimit = 16*1024;

    TQFile f(filename);

    if(f.open(IO_ReadOnly)) {

        TQCString s(g_lineLimit);

        while(f.readLine(s.data(), g_lineLimit)>=0) {
            if ( s[s.length()-1] != '\n' ) // Gosh, this line is longer than g_lineLimit. Skipping.
            {
               kdWarning() << "IE bookmarks contain a line longer than " << g_lineLimit << ". Skipping." << endl;
               continue;
            }
            TQCString t = s.stripWhiteSpace();
            TQRegExp rx( "URL=(.*)" );
            if (rx.exactMatch(t)) {
               emit newBookmark( name, TQString(rx.cap(1)).latin1(), TQString("") );
            }
        }

        f.close();
    }
}

/* antlarr: KDE 4: Make them const TQString & */
void KIEBookmarkImporter::parseIEBookmarks_dir( TQString dirname, TQString foldername )
{

   TQDir dir(dirname);
   dir.setFilter( TQDir::Files | TQDir::Dirs );
   dir.setSorting( TQDir::Name | TQDir::DirsFirst );
   dir.setNameFilter("*.url"); // AK - possibly add ";index.ini" ?
   dir.setMatchAllDirs(true);

   const TQFileInfoList *list = dir.entryInfoList();
   if (!list) return;

   if (dirname != m_fileName) 
      emit newFolder( foldername, false, "" );

   TQFileInfoListIterator it( *list );
   TQFileInfo *fi;

   while ( (fi = it.current()) != 0 ) {
      ++it;

      if (fi->fileName() == "." || fi->fileName() == "..") continue;

      if (fi->isDir()) {
         parseIEBookmarks_dir(fi->absFilePath(), fi->fileName());

      } else if (fi->isFile()) {
         if (fi->fileName().endsWith(".url")) {
            TQString name = fi->fileName();
            name.truncate(name.length() - 4); // .url
            parseIEBookmarks_url_file(fi->absFilePath(), name);
         }
         // AK - add index.ini
      }
   }

   if (dirname != m_fileName) 
      emit endFolder();
}


void KIEBookmarkImporter::parseIEBookmarks( )
{
    parseIEBookmarks_dir( m_fileName );
}

TQString KIEBookmarkImporter::IEBookmarksDir()
{
   static KIEBookmarkImporterImpl* p = 0;
   if (!p) 
       p = new KIEBookmarkImporterImpl;
   return p->findDefaultLocation();
}

void KIEBookmarkImporterImpl::parse() {
   KIEBookmarkImporter importer(m_fileName);
   setupSignalForwards(&importer, this);
   importer.parseIEBookmarks();
}

TQString KIEBookmarkImporterImpl::findDefaultLocation(bool) const
{
    // notify user that they must give a new dir such 
    // as "Favourites" as otherwise it'll just place
    // lots of .url files in the given dir and gui
    // stuff in the exporter is ugly so that exclues
    // the possibility of just writing to Favourites
    // and checking if overwriting...
    return KFileDialog::getExistingDirectory();
}

/////////////////////////////////////////////////

class IEExporter : private KBookmarkGroupTraverser {
public:
    IEExporter( const TQString & );
    void write( const KBookmarkGroup &grp ) { traverse(grp); };
private:
    virtual void visit( const KBookmark & );
    virtual void visitEnter( const KBookmarkGroup & );
    virtual void visitLeave( const KBookmarkGroup & );
private:
    TQDir m_currentDir;
};

static TQString ieStyleQuote( const TQString &str ) {
    TQString s(str);
    s.replace(TQRegExp("[/\\:*?\"<>|]"), "_");
    return s;
}

IEExporter::IEExporter( const TQString & dname ) {
    m_currentDir.setPath( dname );
}

void IEExporter::visit( const KBookmark &bk ) {
    TQString fname = m_currentDir.path() + "/" + ieStyleQuote( bk.fullText() ) + ".url";
    // kdDebug() << "visit(" << bk.text() << "), fname == " << fname << endl;
    TQFile file( fname );
    file.open( IO_WriteOnly );
    TQTextStream ts( &file );
    ts << "[InternetShortcut]\r\n";
    ts << "URL=" << bk.url().url().utf8() << "\r\n";
}

void IEExporter::visitEnter( const KBookmarkGroup &grp ) {
    TQString dname = m_currentDir.path() + "/" + ieStyleQuote( grp.fullText() );
    // kdDebug() << "visitEnter(" << grp.text() << "), dname == " << dname << endl;
    m_currentDir.mkdir( dname );
    m_currentDir.cd( dname );
}

void IEExporter::visitLeave( const KBookmarkGroup & ) {
    // kdDebug() << "visitLeave()" << endl;
    m_currentDir.cdUp();
}

void KIEBookmarkExporterImpl::write(KBookmarkGroup parent) {
    IEExporter exporter( m_fileName );
    exporter.write( parent );
}

#include "kbookmarkimporter_ie.moc"