/* This file is part of the KDE project
   Copyright (C) 1998, 1999 David Faure <faure@kde.org>
   Copyright (C) 2001 Holger Freyther <freyther@yahoo.com>

   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.
*/

#ifndef __konqpopupmenu_h
#define __konqpopupmenu_h

#include <sys/types.h>

#include <tqpopupmenu.h>
#include <tqmap.h>
#include <kaction.h>

#include <tqstringlist.h>

#include <kfileitem.h>
#include <kmimetype.h> // for KDEDesktopMimeType
#include <libkonq_export.h>

#include <kparts/browserextension.h>

#include "konq_xmlguiclient.h"

typedef TQValueList<KDEDesktopMimeType::Service> ServiceList;

class KPropertiesDialog;
class KNewMenu;
class KService;
class KonqPopupMenuPlugin;
class KBookmarkManager;

// TODO KDE4: change base class to KPopupMenu, see KAction::slotPopupActivated()
/**
 * This class implements the popup menu for URLs in konqueror and kdesktop
 * It's usage is very simple : on right click, create the KonqPopupMenu instance
 * with the correct arguments, then exec() to make it appear, then destroy it.
 *
 */
class LIBKONQ_EXPORT KonqPopupMenu : public TQPopupMenu, public KonqXMLGUIClient
{
  Q_OBJECT
public:

  /**
   * Flags set by the calling application (konqueror/kdesktop), unlike
   * KParts::BrowserExtension::PopupFlags, which are set by the calling part
   */
  typedef uint KonqPopupFlags;
  enum { NoFlags = 0,
         ShowProperties = 1,  ///< whether to show the "Properties" menu item
         IsLink = 2,          ///< HTML link. If set, we won't have cut/copy/paste, and we'll say "bookmark this link"
         ShowNewWindow = 4 };
         // WARNING: bitfield. Next item is 8

  /**
   * @deprecated lacks tqparentWidget pointer, and
   * uses bool instead of KonqPopupFlags enum,
   * might do strange things with the 'new window' action.
   */
  KonqPopupMenu( KBookmarkManager* manager,
                 const KFileItemList &items,
                 KURL viewURL,
                 KActionCollection & actions,
                 KNewMenu * newMenu,
                 bool showPropertiesAndFileType = true ) KDE_DEPRECATED;

  /**
   * @deprecated uses bool instead of KonqPopupFlags enum,
   * might do strange things with the 'new window' action.
   */
  KonqPopupMenu( KBookmarkManager* manager,
                 const KFileItemList &items,
                 KURL viewURL,
                 KActionCollection & actions,
                 KNewMenu * newMenu,
		 TQWidget * tqparentWidget,
		 bool showPropertiesAndFileType = true ) KDE_DEPRECATED;

  /**
   * Constructor
   * @param manager the bookmark manager for this bookmark
   * @param items the list of file items the popupmenu should be shown for
   * @param viewURL the URL shown in the view, to test for RMB click on view background
   * @param actions list of actions the caller wants to see in the menu
   * @param newMenu "New" menu, shared with the File menu, in konqueror
   * @param tqparentWidget the widget we're showing this popup for. Helps destroying
   * the popup if the widget is destroyed before the popup.
   * @param kpf flags from the KonqPopupFlags enum, set by the calling application
   * @param f flags from the BrowserExtension enum, set by the calling part
   *
   * The actions to pass in include :
   * showmenubar, back, forward, up, cut, copy, paste, pasteto, trash, rename, del
   * The others items are automatically inserted.
   *
   * @since 3.2
   *
   * @todo that list is probably not be up-to-date
   */
  KonqPopupMenu( KBookmarkManager* manager,
                 const KFileItemList &items,
                 const KURL& viewURL,
                 KActionCollection & actions,
                 KNewMenu * newMenu,
                 TQWidget * tqparentWidget,
                 KonqPopupFlags kpf,
                 KParts::BrowserExtension::PopupFlags f /*= KParts::BrowserExtension::DefaultPopupItems*/);

  /**
   * Don't forget to destroy the object
   */
  ~KonqPopupMenu();

  /**
   * Set the title of the URL, when the popupmenu is opened for a single URL.
   * This is used if the user chooses to add a bookmark for this URL.
   */
  void setURLTitle( const TQString& urlTitle );

  class LIBKONQ_EXPORT ProtocolInfo {
   public:
    ProtocolInfo();
    bool supportsReading()  const;
    bool supportsWriting()  const;
    bool supportsDeleting() const;
    bool supportsMoving()   const;
    bool trashIncluded()    const;
   private:
    friend class KonqPopupMenu;
    bool m_Reading:1;
    bool m_Writing:1;
    bool m_Deleting:1;
    bool m_Moving:1;
    bool m_TrashIncluded:1;
  };
  /**
   * Reimplemented for internal purpose
   */
  virtual KAction *action( const TQDomElement &element ) const;


  virtual KActionCollection *actionCollection() const;
  TQString mimeType( ) const;
  KURL url( ) const;
  KFileItemList fileItemList() const;
  KURL::List popupURLList( ) const;
  ProtocolInfo protocolInfo() const;

public slots: // KDE4: why public?
  void slotPopupNewDir();
  void slotPopupNewView();
  void slotPopupEmptyTrashBin();
  void slotPopupRestoreTrashedItems();
  void slotPopupOpenWith();
  void slotPopupAddToBookmark();
  void slotRunService();
  void slotPopupMimeType();
  void slotPopupProperties();
  void slotOpenShareFileDialog();
protected:
  KActionCollection &m_actions;
  KActionCollection m_ownActions;

private:
  void init (TQWidget * tqparentWidget, KonqPopupFlags kpf, KParts::BrowserExtension::PopupFlags itemFlags);
  void setup(KonqPopupFlags kpf);
  void addPlugins( );
  int  insertServicesSubmenus(const TQMap<TQString, ServiceList>& list, TQDomElement& menu, bool isBuiltin);
  int  insertServices(const ServiceList& list, TQDomElement& menu, bool isBuiltin);
  bool KIOSKAuthorizedAction(KConfig& cfg);
  KPropertiesDialog* showPropertiesDialog();

  class KonqPopupMenuPrivate;
  KonqPopupMenuPrivate *d;
  KNewMenu *m_pMenuNew;
  KURL m_sViewURL;
  TQString m_sMimeType;
  KFileItemList m_lstItems;
  KURL::List m_lstPopupURLs;
  TQMap<int,KService::Ptr> m_mapPopup;
  TQMap<int,KDEDesktopMimeType::Service> m_mapPopupServices;
  bool m_bHandleEditOperations;
  KXMLGUIFactory *m_factory;
  KXMLGUIBuilder *m_builder;
  TQString attrName;
  ProtocolInfo m_info;
  TQPtrList<KonqPopupMenuPlugin> m_pluginList;
  KBookmarkManager* m_pManager;
};

class LIBKONQ_EXPORT KonqPopupMenuPlugin : public TQObject, public KonqXMLGUIClient {
	Q_OBJECT
public:
  /**
  * Constructor
  * If you want to insert a dynamic item or menu to konqpopupmenu
  * this class is the right choice.
  * Create a KAction and use _popup->tqaddAction(new KAction );
  * If you want to create a submenu use _popup->addGroup( );
  */
  KonqPopupMenuPlugin( KonqPopupMenu *_popup, const char *name ); // this should also be the parent
  virtual ~KonqPopupMenuPlugin ( );
};

#endif