summaryrefslogtreecommitdiffstats
path: root/konq-plugins/webarchiver/plugin_webarchiver.cpp
blob: 85d43ee710414518d0f2155d7079e08d10ceaeff (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
/* This file is part of Webarchiver
 *  Copyright (C) 2001 by Andreas Schlapbach <[email protected]>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  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.
 **/

/* $Id$ */

/*
 * There are two recursions within this code:
 * - Recursively create DOM-Tree for referenced links which get recursively
 *   converted to HTML
 *
 * => This code has the potential to download whole sites to a TarGz-Archive
 */

//#define DEBUG_WAR

#include <tqdir.h>
#include <tqfile.h>

#include <kaction.h>
#include <kinstance.h>

#include <kfiledialog.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <khtml_part.h>
#include <kdebug.h>
#include <kgenericfactory.h>
#include <kglobalsettings.h>

#include "plugin_webarchiver.h"
#include "archivedialog.h"

typedef KGenericFactory<PluginWebArchiver> PluginWebArchiverFactory;
K_EXPORT_COMPONENT_FACTORY( libwebarchiverplugin,
	                    PluginWebArchiverFactory( "webarchiver" ) )

PluginWebArchiver::PluginWebArchiver( TQObject* parent, const char* name,
                                      const TQStringList & )
  : Plugin( parent, name )
{
  (void) new KAction( i18n("Archive &Web Page..."),
                      "webarchiver", 0,
                      this, TQT_SLOT(slotSaveToArchive()),
                      actionCollection(), "archivepage" );
}

PluginWebArchiver::~PluginWebArchiver()
{
}

void PluginWebArchiver::slotSaveToArchive()
{
  // ## Unicode ok?
  if( !parent() || !parent()->inherits("KHTMLPart"))
    return;
  KHTMLPart *part = static_cast<KHTMLPart *>( parent() );

  TQString archiveName = TQString::fromUtf8(part->htmlDocument().title().string().utf8());

  if (archiveName.isEmpty())
    archiveName = i18n("Untitled");

  // Replace space with underscore, proposed Frank Pieczynski <[email protected]>

  archiveName.replace( "\\s:", " ");
  archiveName = archiveName.simplifyWhiteSpace();
  archiveName.replace( "?", "");
  archiveName.replace( ":", "");
  archiveName.replace( "/", "");
  archiveName = archiveName.replace( TQRegExp("\\s+"), "_");

  archiveName = KGlobalSettings::documentPath() + "/" + archiveName + ".war" ;

  KURL url = KFileDialog::getSaveURL(archiveName, i18n("*.war *.tgz|Web Archives"), part->widget(),
					  i18n("Save Page as Web-Archive") );

  if (url.isEmpty()) { return; }

  if (!(url.isValid())) {
    const TQString title = i18n( "Invalid URL" );
    const TQString text = i18n( "The URL\n%1\nis not valid." ).tqarg(url.prettyURL());
    KMessageBox::sorry(part->widget(), text, title );
    return;
  }

  const TQFile file(url.path());
  if (file.exists()) {
    const TQString title = i18n( "File Exists" );
    const TQString text = i18n( "Do you really want to overwrite:\n%1?" ).tqarg(url.prettyURL());
    if (KMessageBox::Continue != KMessageBox::warningContinueCancel( part->widget(), text, title, i18n("Overwrite") ) ) {
      return;
    }
  }

  ArchiveDialog *dialog=new ArchiveDialog(0L, url.path(), part);
  dialog->show();
  dialog->archive();
}

#include "plugin_webarchiver.moc"