/***************************************************************************
 *   Copyright (C) 2004-2010 by Giovanni Venturi                           *
 *   giovanni@ksniffer.org                                                 *
 *                                                                         *
 *   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 WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  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.          *
 ***************************************************************************/

#ifndef KINFOIP_H
#define KINFOIP_H

#include <KPopupMenu>

#define WHOIS_CMD        1
#define TRACEROUTE_CMD   2
#define PING_CMD         3
#define DIG_CMD          4
#define HOST_CMD         5
#define NSLOOKUP_CMD     6

class QString;

class KProcess;
class DisplayData;

class KInfoIP : public QObject
{
  Q_OBJECT

  public:
    /**
     * Constructor
     */
    KInfoIP();

    /**
     * Destructor
     */
    ~KInfoIP();

    /**
     * Create a popup mene according to the available application
     * installed (whois, traceroute, ping, dig, host, nslookup)
     *
     * @return the KPopupMenu reference
     */
    KPopupMenu *createPopupMenu();

    /**
     * Check if ip is a regular IP address
     *
     * @param ip is the possible IP address to check
     *
     * @returns true if ip is a regular IP address
     */
    bool manageIP( const QString& ip );

  private:
    /**
     * The standard output/error channel produced by the execution of the application on an IP
     * (whois, traceroute, ping, dig, host, nslookup)
     */
    QString m_stdoutString;

    /**
     * IP address assigned by manageIP() member
     */
    QString m_IP;

    /**
     * Fullpathname for 'whois' application
     */
    QString m_whoisExec;

    /**
     * Fullpathname for 'tracerout' application
     */
    QString m_tracerouteExec;

    /**
     * Fullpathname for 'ping' application
     */
    QString m_pingExec;

    /**
     * Fullpathname for 'dig' application
     */
    QString m_digExec;

    /**
     * Fullpathname for 'host' application
     */
    QString m_hostExec;

    /**
     * Fullpathname for 'nslookup' application
     */
    QString m_nslookupExec;

    /**
     * KPopupMenu reference
     */
    KPopupMenu *m_popupMenu;

    /**
     * Reference to the DisplayData window box
     */
    DisplayData *m_displayDataBox;

    /**
     * The Cancel button can be 'Cancel' (true) or 'Ok' (false)
     */
    bool m_buttonCancel;

    /**
     * KProcess reference to run the application investigating IP
     * (whois, traceroute, ping, dig, host, nslookup)
     */
    KProcess *m_cmd;

    /**
     * The id for the whois, traceroute, ping, dig, host, nslookup application
     */
    int m_idApplication;

  private slots:
    /**
     * Data available on the standard output/error channel for the KProcess
     *
     * @param proc is the reference to the KProcess
     * @param buffer is the set of characters of the got text
     * @param buflen is the length of the got text
     */
    void gotMessage( KProcess *proc, char *buffer, int buflen );

    /**
     * The application has terminated, so update the button on the window box
     */
    void commandExited();

    /**
     * Start the related whois, traceroute, ping, dig, host, nslookup application
     * opening in a window box to see the output and manage it
     */
    void showData( int id );

    /**
     * Clicked the Cancel/Ok button
     */
    void okCancelClicked();
};

#endif  // KINFOIP_H
