summaryrefslogtreecommitdiffstats
path: root/konq-plugins/validators/plugin_validators.cpp
blob: 3d6514a71ee0f2f956d201dbdec11647714d191b (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* This file is part of Validators
 *
 *  It's a merge of the HTML- and the CSSValidator
 *
 *  Copyright (C) 2001 by  Richard Moore <rich@kde.org>
 *                         Andreas Schlapbach <schlpbch@iam.unibe.ch>
 *
 *  for information how to write your own plugin see:
 *    http://developer.kde.org/documentation/tutorials/dot/writing-plugins.html
 *
 *  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$ */

#include <kinstance.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kaction.h>
#include <tdehtml_part.h>
#include <kgenericfactory.h>
#include <kdebug.h>
#include <kaboutdata.h>

#include "plugin_validators.h"
#include "validatorsdialog.h"


typedef KGenericFactory<PluginValidators> PluginValidatorsFactory;
static const TDEAboutData aboutdata("validatorsplugin", I18N_NOOP("Validate Web Page") , "1.0" );
K_EXPORT_COMPONENT_FACTORY( libvalidatorsplugin,
	                    PluginValidatorsFactory( &aboutdata ) )

PluginValidators::PluginValidators( TQObject* parent, const char* name,
                                    const TQStringList & )
  : Plugin( parent, name ), m_configDialog(0), m_part(0)
{
  setInstance(PluginValidatorsFactory::instance());

  m_menu = new KActionMenu ( i18n( "&Validate Web Page" ), "validators",
			     actionCollection(), "validateWebpage" );
  m_menu->setDelayed( false );

  m_menu->insert( new KAction( i18n( "Validate &HTML" ),
			       "htmlvalidator", 0,
			       this, TQT_SLOT(slotValidateHTML()),
			       actionCollection(), "validateHTML") );

  m_menu->insert( new KAction( i18n( "Validate &CSS" ),
			       "cssvalidator", 0,
			       this, TQT_SLOT(slotValidateCSS()),
			       actionCollection(), "validateCSS") );

  m_menu->insert( new KAction( i18n( "Validate &Links" ),
			       0,
			       this, TQT_SLOT(slotValidateLinks()),
			       actionCollection(), "validateLinks") );

  m_menu->setEnabled( false );

  if ( parent && parent->inherits( "KHTMLPart" ))
    {
      m_menu->insert( new KAction( i18n( "C&onfigure Validator..." ),
				   "configure", 0,
				   this, TQT_SLOT(slotConfigure()),
				   actionCollection(), "configure") );

      m_part = static_cast<KHTMLPart *>( parent );
      m_configDialog = new ValidatorsDialog( m_part->widget() );
      setURLs();

      connect( m_part, TQT_SIGNAL(started(TDEIO::Job*)), this,
             TQT_SLOT(slotStarted(TDEIO::Job*)) );
    }
}

PluginValidators::~PluginValidators()
{
  delete m_configDialog;
// Dont' delete the action. KActionCollection as parent does the job already
// and not deleting it at this point also ensures that in case we are not unplugged
// from the GUI yet and the ~KXMLGUIClient destructor will do so it won't hit a
// dead pointer. The kxmlgui factory keeps references to the actions, but it does not
// connect to their destroyed() signal, yet (need to find an elegant solution for that
// as it can easily increase the memory usage significantly) . That's why actions must
// persist as long as the plugin is plugged into the GUI.
//  delete m_menu;
}

void PluginValidators::setURLs()
{
  m_WWWValidatorUrl = KURL(m_configDialog->getWWWValidatorUrl());
  m_CSSValidatorUrl = KURL(m_configDialog->getCSSValidatorUrl());
  m_WWWValidatorUploadUrl = KURL(m_configDialog->getWWWValidatorUploadUrl());
  m_CSSValidatorUploadUrl = KURL(m_configDialog->getCSSValidatorUploadUrl());
  m_linkValidatorUrl = KURL(m_configDialog->getLinkValidatorUrl());
}

void PluginValidators::slotStarted( TDEIO::Job* )
{
  // The w3c validator can only access http URLs, and upload local files.
  // No https, probably no webdav either.
  m_menu->setEnabled( m_part->url().isLocalFile()
                      || m_part->url().protocol().lower() == "http" );
}

void PluginValidators::slotValidateHTML()
{
  validateURL(m_WWWValidatorUrl, m_WWWValidatorUploadUrl);
}

void PluginValidators::slotValidateCSS()
{
  validateURL(m_CSSValidatorUrl, m_CSSValidatorUploadUrl);
}

void PluginValidators::slotValidateLinks()
{
  validateURL(m_linkValidatorUrl, KURL());
}

void PluginValidators::slotConfigure()
{
  m_configDialog->show();
  setURLs();
}

void PluginValidators::validateURL(const KURL &url, const KURL &uploadUrl)
{
  // The parent is assumed to be a KHTMLPart
  if ( !parent()->inherits("KHTMLPart") )
    {
      TQString title = i18n( "Cannot Validate Source" );
      TQString text = i18n( "You cannot validate anything except web pages with "
			   "this plugin." );

      KMessageBox::sorry( 0, text, title );
      return;
    }

  KURL validatorUrl(url);

  // Get URL
  KURL partUrl = m_part->url();
  if ( !partUrl.isValid() ) // Just in case ;)
    {
      TQString title = i18n( "Malformed URL" );
      TQString text = i18n( "The URL you entered is not valid, please "
			   "correct it and try again." );
      KMessageBox::sorry( 0, text, title );
      return;
    }

  if (partUrl.isLocalFile())
    {
      if ( validatorUrl.isEmpty() ) {
        TQString title = i18n( "Upload Not Possible" );
        TQString text = i18n( "Validating links is not possible for local "
                             "files." );
        KMessageBox::sorry( 0, text, title );
        return;
      }
      validatorUrl = uploadUrl;
    }
  else
    {
      if (partUrl.hasPass())
        {
          KMessageBox::sorry(
          	m_part->widget(),
          	i18n("<qt>The selected URL cannot be verified because it contains "
          	     "a password. Sending this URL to <b>%1</b> would put the security "
          	     "of <b>%2</b> at risk.</qt>")
          	     .arg(validatorUrl.host()).arg(partUrl.host()));
          return;
        }
      // Set entered URL as a parameter
      TQString q = partUrl.url();
      q = KURL::encode_string( q );
      TQString p = "uri=";
      p += q;
      validatorUrl.setQuery( p );
    }
  kdDebug(90120) << "final URL: " << validatorUrl.url() << endl;

  emit m_part->browserExtension()->openURLRequest( validatorUrl );
}

#include <plugin_validators.moc>