summaryrefslogtreecommitdiffstats
path: root/kftpgrabber/src/misc/customcommands
diff options
context:
space:
mode:
Diffstat (limited to 'kftpgrabber/src/misc/customcommands')
-rw-r--r--kftpgrabber/src/misc/customcommands/Makefile.am16
-rw-r--r--kftpgrabber/src/misc/customcommands/commands.xml113
-rw-r--r--kftpgrabber/src/misc/customcommands/entry.cpp155
-rw-r--r--kftpgrabber/src/misc/customcommands/entry.h236
-rw-r--r--kftpgrabber/src/misc/customcommands/handlers.cpp97
-rw-r--r--kftpgrabber/src/misc/customcommands/handlers.h147
-rw-r--r--kftpgrabber/src/misc/customcommands/manager.cpp211
-rw-r--r--kftpgrabber/src/misc/customcommands/manager.h118
-rw-r--r--kftpgrabber/src/misc/customcommands/parameterentrydialog.cpp131
-rw-r--r--kftpgrabber/src/misc/customcommands/parameterentrydialog.h79
-rw-r--r--kftpgrabber/src/misc/customcommands/responsedialog.cpp60
-rw-r--r--kftpgrabber/src/misc/customcommands/responsedialog.h65
12 files changed, 1428 insertions, 0 deletions
diff --git a/kftpgrabber/src/misc/customcommands/Makefile.am b/kftpgrabber/src/misc/customcommands/Makefile.am
new file mode 100644
index 0000000..8436cf4
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/Makefile.am
@@ -0,0 +1,16 @@
+INCLUDES = -I$(srcdir)/../.. \
+ -I$(srcdir)/.. \
+ -I$(srcdir)/../../engine \
+ -I$(srcdir)/../../misc/interfaces \
+ -I$(srcdir)/../../widgets \
+ $(all_includes)
+METASOURCES = AUTO
+noinst_LIBRARIES = libcustomcommands.a
+noinst_HEADERS = handlers.h entry.h manager.h parameterentrydialog.h \
+ responsedialog.h
+libcustomcommands_a_SOURCES = handlers.cpp entry.cpp manager.cpp \
+ parameterentrydialog.cpp responsedialog.cpp
+
+shellrcdir = $(kde_datadir)/kftpgrabber
+shellrc_DATA = commands.xml
+
diff --git a/kftpgrabber/src/misc/customcommands/commands.xml b/kftpgrabber/src/misc/customcommands/commands.xml
new file mode 100644
index 0000000..82bde8e
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/commands.xml
@@ -0,0 +1,113 @@
+<commands>
+ <category name="GlFTPd">
+ <category name="Information">
+ <entry name="Show current aliases">
+ <description>Shows current user aliases.</description>
+ <command>SITE ALIAS</command>
+ <response handler="Raw" display="Window">
+ <expected code="2" />
+ </response>
+ </entry>
+
+ <entry name="Show group info" icon="info">
+ <description>Shows group information.</description>
+ <command>SITE GRP %1</command>
+ <params>
+ <param type="String">Group name</param>
+ </params>
+ <response handler="Raw" display="Window">
+ <expected code="2" />
+ </response>
+ </entry>
+
+ <entry name="Show your idle time">
+ <description>Shows your current idle time.</description>
+ <command>SITE IDLE</command>
+ <response handler="Raw" display="Window">
+ <expected code="2" />
+ </response>
+ </entry>
+
+ <entry name="Show online users">
+ <description>Shows the users that are currently online.</description>
+ <command>SITE WHO</command>
+ <response handler="Raw" display="Window">
+ <expected code="2" />
+ </response>
+ </entry>
+
+ <entry name="Show welcome screen">
+ <description>Shows the welcome screen.</description>
+ <command>SITE WELCOME</command>
+ <response handler="Raw" display="Window">
+ <expected code="2" />
+ </response>
+ </entry>
+
+ <separator />
+
+ <entry name="Set your idle time">
+ <description>Sets your maximum idle time (in seconds).</description>
+ <command>SITE IDLE %1</command>
+ <params>
+ <param type="Integer">Idle time</param>
+ </params>
+ <response handler="Substitute" display="MessageBox">
+ <expected code="2" />
+ <text>Idle time successfully changed.</text>
+ </response>
+ </entry>
+
+ <entry name="Change your password" icon="password">
+ <description>Changes your account password.</description>
+ <command>SITE PASSWD %1</command>
+ <params>
+ <param type="Password">Password</param>
+ </params>
+ <response handler="Substitute" display="MessageBox">
+ <expected code="2" />
+ <text>Password successfully changed.</text>
+ </response>
+ </entry>
+ </category>
+
+ <category name="User Management">
+ <entry name="Delete a user" icon="editdelete">
+ <description>Removes a user from the server's user database.</description>
+ <command>SITE DELUSER %1</command>
+ <params>
+ <param type="String">Username</param>
+ </params>
+ <response handler="Substitute" display="MessageBox">
+ <expected code="2" />
+ <text>User successfully removed.</text>
+ </response>
+ </entry>
+
+ <entry name="Change user's password" icon="password">
+ <description>Changes a user's password.</description>
+ <command>SITE CHPASS %1 %2</command>
+ <params>
+ <param type="String">Username</param>
+ <param type="Password">New password</param>
+ </params>
+ <response handler="Substitute" display="MessageBox">
+ <expected code="2" />
+ <text>Password successfully changed.</text>
+ </response>
+ </entry>
+
+ <entry name="Kick a user from this server">
+ <description>Terminates all connections for the specified user.</description>
+ <command>SITE KICK %1</command>
+ <params>
+ <param type="String">Username</param>
+ </params>
+ <response handler="Substitute" display="MessageBox">
+ <expected code="2" />
+ <text>User has been kicked.</text>
+ </response>
+ </entry>
+ </category>
+ </category>
+</commands> \ No newline at end of file
diff --git a/kftpgrabber/src/misc/customcommands/entry.cpp b/kftpgrabber/src/misc/customcommands/entry.cpp
new file mode 100644
index 0000000..346f3cf
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/entry.cpp
@@ -0,0 +1,155 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#include "entry.h"
+#include "kftpsession.h"
+#include "manager.h"
+
+#include "parameterentrydialog.h"
+#include "responsedialog.h"
+
+#include <klocale.h>
+#include <kmessagebox.h>
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+Entry::Entry(QObject *parent, const QString &name)
+ : QObject(parent),
+ m_name(name)
+{
+}
+
+void Entry::appendParameter(ParameterType type, const QString &name)
+{
+ m_params.append(Parameter(type, name));
+}
+
+void Entry::setResponseHandler(const QString &handler, QDomNode args)
+{
+ m_handler = handler;
+ m_handlerArguments = args;
+}
+
+void Entry::execute(KFTPSession::Session *session)
+{
+ // Create a dialog for parameter input
+ QString command = m_command;
+
+ if (m_params.count() > 0) {
+ ParameterEntryDialog *dialog = new ParameterEntryDialog(this, m_params);
+ if (dialog->exec() != QDialog::Accepted) {
+ delete dialog;
+ return;
+ }
+
+ command = dialog->formatCommand(command);
+ delete dialog;
+ }
+
+ // Execute the command with proper parameters
+ m_lastSession = session;
+
+ connect(session->getClient()->eventHandler(), SIGNAL(gotRawResponse(const QString&)), this, SLOT(handleResponse(const QString&)));
+ session->getClient()->raw(command);
+}
+
+void Entry::handleResponse(const QString &response)
+{
+ if (!m_lastSession)
+ return;
+
+ m_lastSession->getClient()->eventHandler()->QObject::disconnect(this);
+ m_lastSession = 0;
+
+ // Invoke the proper handler
+ QString expectedReturn = m_handlerArguments.namedItem("expected").toElement().attribute("code");
+
+ if (!response.startsWith(expectedReturn)) {
+ KMessageBox::error(0, i18n("<qt>Requested operation has failed! Response from server is:<br/><br /><b>%1</b></qt>").arg(response));
+ return;
+ }
+
+ Handlers::Handler *handler = Manager::self()->handler(m_handler);
+
+ if (!handler) {
+ KMessageBox::error(0, i18n("<qt>Handler named <b>%1</b> can't be found for response parsing!</qt>").arg(m_handler));
+ return;
+ }
+
+ QString parsed = handler->handleResponse(response, m_handlerArguments);
+
+ // Find the proper way to display the parsed response
+ switch (m_displayType) {
+ case None: return;
+ case Window: {
+ ResponseDialog *dialog = new ResponseDialog(m_name, parsed);
+ dialog->exec();
+ delete dialog;
+ break;
+ }
+ case MessageBox: {
+ KMessageBox::information(0, parsed);
+ break;
+ }
+ }
+}
+
+Entry::Parameter::Parameter()
+ : m_type(String),
+ m_name("<invalid>")
+{
+}
+
+Entry::Parameter::Parameter(ParameterType type, const QString &name)
+ : m_type(type),
+ m_name(name)
+{
+}
+
+EntryAction::EntryAction(Entry *entry, KFTPSession::Session *session)
+ : KAction(entry->name(), entry->icon()),
+ m_entryInfo(entry),
+ m_session(session)
+{
+}
+
+}
+
+}
+
+#include "entry.moc"
+
diff --git a/kftpgrabber/src/misc/customcommands/entry.h b/kftpgrabber/src/misc/customcommands/entry.h
new file mode 100644
index 0000000..e7c82c4
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/entry.h
@@ -0,0 +1,236 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#ifndef KFTPCORE_CUSTOMCOMMANDSENTRY_H
+#define KFTPCORE_CUSTOMCOMMANDSENTRY_H
+
+#include <qdom.h>
+#include <qvaluelist.h>
+
+#include <kaction.h>
+
+namespace KFTPSession {
+ class Session;
+}
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+/**
+ * This class represents a single custom command entry. A tree of
+ * such objects is constructed from an XML file.
+ *
+ * @author Jernej Kos
+ */
+class Entry : public QObject {
+Q_OBJECT
+public:
+ /**
+ * Possible parameter types.
+ */
+ enum ParameterType {
+ String,
+ Password,
+ Integer
+ };
+
+ /**
+ * Possible display types.
+ */
+ enum DisplayType {
+ None,
+ Window,
+ MessageBox
+ };
+
+ /**
+ * A single command parameter.
+ */
+ class Parameter {
+ public:
+ /**
+ * Class constructor.
+ */
+ Parameter();
+
+ /**
+ * Class constructor.
+ *
+ * @param type Parameter type
+ * @param name Parameter name
+ */
+ Parameter(ParameterType type, const QString &name);
+
+ /**
+ * Returns the parameter type.
+ */
+ ParameterType type() const { return m_type; }
+
+ /**
+ * Returns the parameter name.
+ */
+ QString name() const { return m_name; }
+ private:
+ ParameterType m_type;
+ QString m_name;
+ };
+
+ /**
+ * Class constructor.
+ *
+ * @param name Short entry name
+ */
+ Entry(QObject *parent, const QString &name);
+
+ /**
+ * Returns entry's name.
+ */
+ QString name() const { return m_name; }
+
+ /**
+ * Returns entry's description.
+ */
+ QString description() const { return m_description; }
+
+ /**
+ * Returns entry's icon name.
+ */
+ QString icon() const { return m_icon; }
+
+ /**
+ * Sets entry's description.
+ *
+ * @param description A longer entry description; can be rich text
+ */
+ void setDescription(const QString &description) { m_description = description; }
+
+ /**
+ * Set entry's icon.
+ *
+ * @param icon An icon name
+ */
+ void setIcon(const QString &icon) { m_icon = icon; }
+
+ /**
+ * Sets the raw command to be sent.
+ *
+ * @param command A valid FTP command with optional parameter placeholders
+ */
+ void setCommand(const QString &command) { m_command = command; }
+
+ /**
+ * Appends a command parameter.
+ *
+ * @param type Parameter type
+ * @param name Human readable parameter name
+ */
+ void appendParameter(ParameterType type, const QString &name);
+
+ /**
+ * Sets response display type.
+ *
+ * @param type Display type
+ */
+ void setDisplayType(DisplayType type) { m_displayType = type; }
+
+ /**
+ * Sets the response handler to use.
+ *
+ * @param handler Handler name
+ * @param args Optional argument node
+ */
+ void setResponseHandler(const QString &handler, QDomNode args);
+
+ /**
+ * Executes this entry. This will actually generate and show a proper
+ * user input dialog, execute the command with the provided parameters,
+ * pass the raw response to a selected handler and properly display
+ * the result.
+ *
+ * @param session A remote session where command should be executed
+ */
+ void execute(KFTPSession::Session *session);
+private slots:
+ void handleResponse(const QString &response);
+private:
+ QString m_name;
+ QString m_description;
+ QString m_icon;
+ QString m_command;
+ QString m_handler;
+ DisplayType m_displayType;
+
+ QValueList<Parameter> m_params;
+ QDomNode m_handlerArguments;
+
+ KFTPSession::Session *m_lastSession;
+};
+
+/**
+ * This class is a wrapper action, so a proper entry gets pulled and
+ * executed.
+ *
+ * @author Jernej Kos
+ */
+class EntryAction : public KAction {
+public:
+ /**
+ * Class constructor.
+ *
+ * @param entry Associated entry
+ * @param session Associated session
+ */
+ EntryAction(Entry *entry, KFTPSession::Session *session);
+
+ /**
+ * Returns the associated entry instance.
+ */
+ Entry *entryInfo() const { return m_entryInfo; }
+
+ /**
+ * Returns the associated session instance.
+ */
+ KFTPSession::Session *session() const { return m_session; }
+private:
+ Entry *m_entryInfo;
+ KFTPSession::Session *m_session;
+};
+
+}
+
+}
+
+#endif
diff --git a/kftpgrabber/src/misc/customcommands/handlers.cpp b/kftpgrabber/src/misc/customcommands/handlers.cpp
new file mode 100644
index 0000000..8cf5349
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/handlers.cpp
@@ -0,0 +1,97 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#include "handlers.h"
+
+#include <qdom.h>
+#include <qregexp.h>
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+namespace Handlers {
+
+Handler::Handler(const QString &name)
+ : m_name(name)
+{
+}
+
+RawHandler::RawHandler()
+ : Handler("Raw")
+{
+}
+
+SubstituteHandler::SubstituteHandler()
+ : Handler("Substitue")
+{
+}
+
+QString SubstituteHandler::handleResponse(const QString &raw, QDomNode arguments) const
+{
+ QString text = arguments.namedItem("text").toElement().text();
+
+ if (text.contains("%1"))
+ return text.arg(raw);
+
+ return text;
+}
+
+RegexpHandler::RegexpHandler()
+ : Handler("Regexp")
+{
+}
+
+QString RegexpHandler::handleResponse(const QString &raw, QDomNode arguments) const
+{
+ QString result;
+ QRegExp e(arguments.namedItem("match").toElement().text());
+
+ if (e.exactMatch(raw.stripWhiteSpace())) {
+ result = arguments.namedItem("display").toElement().text();
+
+ for (int i = 1; i <= e.numCaptures(); i++) {
+ result.replace(QString("\\%1").arg(i), e.cap(i));
+ }
+ }
+
+ return result;
+}
+
+}
+
+}
+
+}
diff --git a/kftpgrabber/src/misc/customcommands/handlers.h b/kftpgrabber/src/misc/customcommands/handlers.h
new file mode 100644
index 0000000..f41a3de
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/handlers.h
@@ -0,0 +1,147 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#ifndef KFTPCORE_CUSTOMCOMMANDS_HANDLERSHANDLERS_H
+#define KFTPCORE_CUSTOMCOMMANDS_HANDLERSHANDLERS_H
+
+#include <qstring.h>
+#include <qdom.h>
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+namespace Handlers {
+
+/**
+ * The handler class is an abstract class which every actual handler
+ * must implement.
+ *
+ * @author Jernej Kos
+ */
+class Handler {
+public:
+ /**
+ * Class constructor.
+ *
+ * @param name Handler name
+ */
+ Handler(const QString &name);
+
+ /**
+ * Returns the handler's name.
+ */
+ QString name() const { return m_name; }
+
+ /**
+ * This method should be implemented by actual handlers to handler the
+ * server response.
+ *
+ * @param raw Raw FTP response
+ * @param arguments Any argument nodes supplied in the XML file
+ * @return This method should return a formatted string
+ */
+ virtual QString handleResponse(const QString &raw, QDomNode arguments) const = 0;
+private:
+ QString m_name;
+};
+
+/**
+ * The Raw handler accepts no arguments and simply passes on raw data.
+ *
+ * @author Jernej Kos
+ */
+class RawHandler : public Handler {
+public:
+ /**
+ * Class constructor.
+ */
+ RawHandler();
+
+ /**
+ * @overload
+ * Reimplemented from Handler.
+ */
+ QString handleResponse(const QString &raw, QDomNode) const { return raw; }
+};
+
+/**
+ * The Substitue handler always returns a predefined value when the
+ * operation is completed successfully. %1 can be used in place of the
+ * raw data received from the server.
+ *
+ * @author Jernej Kos
+ */
+class SubstituteHandler : public Handler {
+public:
+ /**
+ * Class constructor.
+ */
+ SubstituteHandler();
+
+ /**
+ * @overload
+ * Reimplemented from Handler.
+ */
+ QString handleResponse(const QString &raw, QDomNode arguments) const;
+};
+
+/**
+ * The Regexp handler enables custom response parsing using regular
+ * expressions.
+ *
+ * @author Jernej Kos
+ */
+class RegexpHandler : public Handler {
+public:
+ /**
+ * Class constructor.
+ */
+ RegexpHandler();
+
+ /**
+ * @overload
+ * Reimplemented from Handler.
+ */
+ QString handleResponse(const QString &raw, QDomNode arguments) const;
+};
+
+}
+
+}
+
+}
+
+#endif
diff --git a/kftpgrabber/src/misc/customcommands/manager.cpp b/kftpgrabber/src/misc/customcommands/manager.cpp
new file mode 100644
index 0000000..1cda06a
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/manager.cpp
@@ -0,0 +1,211 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#include "manager.h"
+#include "entry.h"
+
+#include <qfile.h>
+
+#include <kstandarddirs.h>
+#include <kstaticdeleter.h>
+#include <kpopupmenu.h>
+#include <kmessagebox.h>
+#include <klocale.h>
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+Manager *Manager::m_self = 0;
+static KStaticDeleter<Manager> staticManagerDeleter;
+
+Manager *Manager::self()
+{
+ if (!m_self) {
+ staticManagerDeleter.setObject(m_self, new Manager());
+ }
+
+ return m_self;
+}
+
+Manager::Manager()
+ : QObject()
+{
+ // Populate the handlers list
+ m_handlers["Raw"] = new Handlers::RawHandler();
+ m_handlers["Substitute"] = new Handlers::SubstituteHandler();
+ m_handlers["Regexp"] = new Handlers::RegexpHandler();
+}
+
+Manager::~Manager()
+{
+ if (m_self == this)
+ staticManagerDeleter.setObject(m_self, 0, false);
+
+ // Destroy the handlers
+ delete static_cast<Handlers::RawHandler*>(m_handlers["Raw"]);
+ delete static_cast<Handlers::SubstituteHandler*>(m_handlers["Substitute"]);
+ delete static_cast<Handlers::RegexpHandler*>(m_handlers["Regexp"]);
+
+ m_handlers.clear();
+}
+
+void Manager::load()
+{
+ QString filename = locateLocal("appdata", "commands.xml");
+
+ if (!QFile::exists(filename)) {
+ // Copy the default command set over
+ QFile source(locate("appdata", "commands.xml"));
+ QFile destination(filename);
+
+ source.open(IO_ReadOnly);
+ destination.open(IO_WriteOnly | IO_Truncate);
+
+ destination.writeBlock(source.readAll());
+ source.close();
+ destination.close();
+ }
+
+ QFile file(filename);
+ if (!file.open(IO_ReadOnly))
+ return;
+
+ m_document.setContent(&file);
+ file.close();
+}
+
+void Manager::parseEntries(KActionMenu *parentMenu, const QDomNode &parentNode, KFTPSession::Session *session) const
+{
+ QDomNode n = parentNode.firstChild();
+
+ while (!n.isNull()) {
+ if (n.isElement()) {
+ QDomElement e = n.toElement();
+ QString tagName = e.tagName();
+ QString name = e.attribute("name");
+
+ if (tagName == "category") {
+ KActionMenu *menu = new KActionMenu(name, "folder", parentMenu);
+ parentMenu->insert(menu);
+
+ // Recurse into this category
+ parseEntries(menu, n, session);
+ } else if (tagName == "entry") {
+ Entry *entry = new Entry((Manager*) this, name);
+ entry->setDescription(n.namedItem("description").toElement().text());
+ entry->setIcon(e.attribute("icon"));
+ entry->setCommand(n.namedItem("command").toElement().text());
+
+ QDomNode p = n.namedItem("params").firstChild();
+ while (!p.isNull()) {
+ QDomElement pElement = p.toElement();
+
+ if (pElement.tagName() == "param") {
+ QString typeString = pElement.attribute("type");
+ Entry::ParameterType type = Entry::String;
+
+ if (typeString == "String")
+ type = Entry::String;
+ else if (typeString == "Password")
+ type = Entry::Password;
+ else if (typeString == "Integer")
+ type = Entry::Integer;
+
+ entry->appendParameter(type, pElement.text());
+ }
+
+ p = p.nextSibling();
+ }
+
+ QDomElement rElement = n.namedItem("response").toElement();
+ entry->setResponseHandler(rElement.attribute("handler"), rElement);
+
+ QString displayString = rElement.attribute("display");
+ Entry::DisplayType displayType = Entry::None;
+
+ if (displayString == "None")
+ displayType = Entry::None;
+ else if (displayString == "Window")
+ displayType = Entry::Window;
+ else if (displayString == "MessageBox")
+ displayType = Entry::MessageBox;
+
+ entry->setDisplayType(displayType);
+
+ // Create a new action
+ EntryAction *action = new EntryAction(entry, session);
+ connect(action, SIGNAL(activated()), this, SLOT(slotActionActivated()));
+
+ parentMenu->insert(action);
+ } else if (tagName == "separator") {
+ parentMenu->popupMenu()->insertSeparator();
+ } else {
+ KMessageBox::error(0, i18n("Unknown tag while parsing custom site commands!"));
+ }
+ }
+
+ n = n.nextSibling();
+ }
+}
+
+Handlers::Handler *Manager::handler(const QString &name) const
+{
+ if (m_handlers.contains(name))
+ return m_handlers[name];
+
+ return 0;
+}
+
+KActionMenu *Manager::categories(const QString &name, KFTPSession::Session *session) const
+{
+ KActionMenu *actionMenu = new KActionMenu(name);
+ parseEntries(actionMenu, m_document.documentElement(), session);
+
+ return actionMenu;
+}
+
+void Manager::slotActionActivated()
+{
+ EntryAction *action = (EntryAction*) QObject::sender();
+ action->entryInfo()->execute(action->session());
+}
+
+}
+
+}
+
+#include "manager.moc"
+
diff --git a/kftpgrabber/src/misc/customcommands/manager.h b/kftpgrabber/src/misc/customcommands/manager.h
new file mode 100644
index 0000000..db5dd6d
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/manager.h
@@ -0,0 +1,118 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#ifndef KFTPCORE_CUSTOMCOMMANDSMANAGER_H
+#define KFTPCORE_CUSTOMCOMMANDSMANAGER_H
+
+#include <qobject.h>
+#include <qdom.h>
+#include <qmap.h>
+
+#include <kaction.h>
+
+#include "handlers.h"
+
+namespace KFTPSession {
+ class Session;
+}
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+/**
+ * This class represents the entry manager. It parses the XML file and
+ * creates the necessary entries. This class is a singleton.
+ *
+ * @author Jernej Kos
+ */
+class Manager : public QObject {
+Q_OBJECT
+public:
+ /**
+ * Class destructor.
+ */
+ ~Manager();
+
+ /**
+ * Returns a Manager instance.
+ */
+ static Manager *self();
+
+ /**
+ * Returns an appropriate handler.
+ *
+ * @param name Handler string identifier
+ */
+ Handlers::Handler *handler(const QString &name) const;
+
+ /**
+ * Returns a KActionMenu that can be used for displaying categories of
+ * entries to the user.
+ */
+ KActionMenu *categories(const QString &name, KFTPSession::Session *session) const;
+public slots:
+ /**
+ * Loads a valid commands XML file.
+ */
+ void load();
+protected:
+ static Manager *m_self;
+
+ /**
+ * Class constructor.
+ */
+ Manager();
+
+ /**
+ * Recursive entry parser.
+ */
+ void parseEntries(KActionMenu *parentMenu, const QDomNode &parentNode, KFTPSession::Session *session) const;
+protected slots:
+ /**
+ * This slot gets called when an EntryAction is activated.
+ */
+ void slotActionActivated();
+private:
+ KActionMenu *m_categories;
+ QDomDocument m_document;
+ QMap<QString, Handlers::Handler*> m_handlers;
+};
+
+}
+
+}
+
+#endif
diff --git a/kftpgrabber/src/misc/customcommands/parameterentrydialog.cpp b/kftpgrabber/src/misc/customcommands/parameterentrydialog.cpp
new file mode 100644
index 0000000..808b400
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/parameterentrydialog.cpp
@@ -0,0 +1,131 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#include "parameterentrydialog.h"
+#include "entry.h"
+
+#include <qvbox.h>
+#include <qlayout.h>
+#include <qlabel.h>
+
+#include <kiconloader.h>
+#include <klineedit.h>
+#include <kpassdlg.h>
+#include <knuminput.h>
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+ParameterEntryDialog::ParameterEntryDialog(Entry *entry, QValueList<Entry::Parameter> params)
+ : KDialogBase(0, "", true, entry->name(), Ok|Cancel, Ok),
+ m_params(params)
+{
+ QFrame *mainWidget = makeMainWidget();
+ QVBoxLayout *mainLayout = new QVBoxLayout(mainWidget);
+
+ QHBoxLayout *headerLayout = new QHBoxLayout(mainWidget);
+ QLabel *icon = new QLabel(mainWidget);
+ icon->setPixmap(DesktopIcon(entry->icon(), 32));
+ headerLayout->addWidget(icon);
+
+ QVBoxLayout *headerTextLayout = new QVBoxLayout(mainWidget);
+ headerTextLayout->addWidget(new QLabel(QString("<b>%1</b>").arg(entry->name()), mainWidget));
+ headerTextLayout->addWidget(new QLabel(entry->description(), mainWidget));
+ headerLayout->addLayout(headerTextLayout, 1);
+
+ mainLayout->addLayout(headerLayout);
+ mainLayout->addSpacing(5);
+
+ QFrame *frame = new QFrame(mainWidget);
+ frame->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
+
+ QVBoxLayout *frameLayout = new QVBoxLayout(frame);
+ frameLayout->setMargin(10);
+ mainLayout->addWidget(frame);
+
+ int num = 0;
+ QValueList<Entry::Parameter>::ConstIterator lend = params.end();
+ for (QValueList<Entry::Parameter>::ConstIterator i = params.begin(); i != lend; ++i) {
+ QHBoxLayout *layout = new QHBoxLayout(frame);
+ QWidget *entryWidget = 0;
+ QString name = QString("param_%1").arg(num++);
+
+ switch ((*i).type()) {
+ case Entry::String: entryWidget = new KLineEdit(frame, name.ascii()); break;
+ case Entry::Password: entryWidget = new KPasswordEdit(frame, name.ascii()); break;
+ case Entry::Integer: entryWidget = new KIntNumInput(frame, name.ascii()); break;
+ }
+
+ // The first widget should have focus
+ if (num == 1)
+ entryWidget->setFocus();
+
+ layout->addWidget(new QLabel((*i).name() + ":", frame));
+ layout->addStretch(1);
+ layout->addWidget(entryWidget);
+ frameLayout->addLayout(layout);
+ frameLayout->addSpacing(5);
+ }
+
+ setMaximumWidth(350);
+ resize(350, minimumHeight());
+}
+
+QString ParameterEntryDialog::formatCommand(const QString &command)
+{
+ QString tmp = command;
+
+ int num = 0;
+ QValueList<Entry::Parameter>::ConstIterator lend = m_params.end();
+ for (QValueList<Entry::Parameter>::ConstIterator i = m_params.begin(); i != lend; ++i) {
+ QObject *entryWidget = child(QString("param_%1").arg(num++).ascii());
+
+ switch ((*i).type()) {
+ case Entry::String: tmp = tmp.arg(static_cast<KLineEdit*>(entryWidget)->text()); break;
+ case Entry::Password: tmp = tmp.arg(static_cast<KPasswordEdit*>(entryWidget)->password()); break;
+ case Entry::Integer: tmp = tmp.arg(static_cast<KIntNumInput*>(entryWidget)->value()); break;
+ }
+ }
+
+ return tmp;
+}
+
+}
+
+}
+
+#include "parameterentrydialog.moc"
+
diff --git a/kftpgrabber/src/misc/customcommands/parameterentrydialog.h b/kftpgrabber/src/misc/customcommands/parameterentrydialog.h
new file mode 100644
index 0000000..3a7d242
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/parameterentrydialog.h
@@ -0,0 +1,79 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#ifndef KFTPCORE_CUSTOMCOMMANDSPARAMETERENTRYDIALOG_H
+#define KFTPCORE_CUSTOMCOMMANDSPARAMETERENTRYDIALOG_H
+
+#include <kdialogbase.h>
+#include <qvaluelist.h>
+
+#include "entry.h"
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+/**
+ * A dialog for parameter entry.
+ *
+ * @author Jernej Kos <kostko@jweb-network.net>
+ */
+class ParameterEntryDialog : public KDialogBase {
+Q_OBJECT
+public:
+ /**
+ * Class constructor.
+ *
+ * @param entry An entry instance
+ * @param params Parameter list
+ */
+ ParameterEntryDialog(Entry *entry, QValueList<Entry::Parameter> params);
+
+ /**
+ * Properly replaces the parameter placeholders with actual user
+ * entered values.
+ *
+ * @param command A command string with placeholders
+ */
+ QString formatCommand(const QString &command);
+private:
+ QValueList<Entry::Parameter> m_params;
+};
+
+}
+
+}
+
+#endif
diff --git a/kftpgrabber/src/misc/customcommands/responsedialog.cpp b/kftpgrabber/src/misc/customcommands/responsedialog.cpp
new file mode 100644
index 0000000..11970f9
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/responsedialog.cpp
@@ -0,0 +1,60 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#include "responsedialog.h"
+
+#include <ktextedit.h>
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+ResponseDialog::ResponseDialog(const QString &caption, const QString &response)
+ : KDialogBase(0, "", true, caption, Ok)
+{
+ KTextEdit *contents = new KTextEdit(this);
+ setMainWidget(contents);
+
+ contents->setTextFormat(Qt::LogText);
+ contents->unsetPalette();
+ contents->append(response);
+
+ setMinimumSize(500, 300);
+}
+
+}
+
+}
+
diff --git a/kftpgrabber/src/misc/customcommands/responsedialog.h b/kftpgrabber/src/misc/customcommands/responsedialog.h
new file mode 100644
index 0000000..ab370ee
--- /dev/null
+++ b/kftpgrabber/src/misc/customcommands/responsedialog.h
@@ -0,0 +1,65 @@
+/*
+ * This file is part of the KFTPGrabber project
+ *
+ * Copyright (C) 2003-2006 by the KFTPGrabber developers
+ * Copyright (C) 2003-2006 Jernej Kos <kostko@jweb-network.net>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
+ * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
+ * NON-INFRINGEMENT. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Steet, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ *
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL. If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so. If you
+ * do not wish to do so, delete this exception statement from your
+ * version. If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+#ifndef KFTPCORE_CUSTOMCOMMANDSRESPONSEDIALOG_H
+#define KFTPCORE_CUSTOMCOMMANDSRESPONSEDIALOG_H
+
+#include <kdialogbase.h>
+
+namespace KFTPCore {
+
+namespace CustomCommands {
+
+/**
+ * A dialog for displaying operation results.
+ *
+ * @author Jernej Kos <kostko@jweb-network.net>
+ */
+class ResponseDialog : public KDialogBase {
+public:
+ /**
+ * Class constructor.
+ *
+ * @param caption Dialog caption
+ * @param response Response text (can be rich-text)
+ */
+ ResponseDialog(const QString &caption, const QString &response);
+};
+
+}
+
+}
+
+#endif