/****************************************************************************
**
** Copyright (C) 2005-2006 Trolltech AS. All rights reserved.
**
** This file is part of the example classes of the Qt Toolkit.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.trolltech.com/products/qt/opensource.html
**
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://www.trolltech.com/products/qt/licensing.html or contact the
** sales department at sales@trolltech.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef XBELTREE_H
#define XBELTREE_H

#include <QDomDocument>
#include <eapplicationlauncher.h>
#include <QHash>
#include <QIcon>
#include <QTreeWidget>
#include <QList>
#include <QPair>
#include <QString>
#include <QHash>
#include "finder.h"

#define CONFIG_PATH	QString("/runtime/elettra/etc/")


class BrowserTree : public QTreeWidget
{
    Q_OBJECT

public:
    BrowserTree(QWidget *parent = 0);

    ~BrowserTree();

    bool read(QIODevice *device);
    bool write(QIODevice *device);
    QList <QPair <bool, QString> > GetExecutablesList();
    QMenu* IconMeaningsMenu() { return iconmeanings; }
    void SetEditMode(bool mode) { edit_mode = mode; }
    bool IsEditMode() { return edit_mode; }
    QString WindowTitle() { return wintitle; }
    QString SystrayIconFile() { return systray_icon_file; }

    void SetUniqueEnabled(bool en) { x11_unique_check_enabled = en; }
    bool UniqueEnabled() { return x11_unique_check_enabled; }
    QString Filename() { return filename; }

    EApplicationLauncher* GetAppByExeAndArgs(const QString& exename, const QStringList &args);

    /* Given a complete command, with an arbitrary number of spaces between arguments, extracts
         * the executable and the parameters in a string and a string list respectively.
         */
    void getExeAndArgs(QString &command, QString& exename, QStringList& arguments);

    QTreeWidgetItem *getItemByLauncher( EApplicationLauncher *l);

    void setDBusService(EApplicationLauncher *l, const QString& dbusServiceName);

    QStringList runningApplications() const;

signals:

    void dbusRaise(const QString& commandLine);
    void dbusStop(const QString& commandLine);

    void enableDBusServiceChangedNotification(bool);

public slots:
    int ShowFindDialog();
    void ApplicationRaised(bool started_ok, EApplicationLauncher *launcher = NULL);
    void ApplicationExited(bool brutally = false, EApplicationLauncher *launcher = NULL);

    /* expand item and its parent items to show the item corresponding to the launcher */
    void expand( EApplicationLauncher *);void view_exec_command(bool);
    void view_appli_states(bool);
    void view_description(bool);

    void viewDBusService(bool);
    void unlockHeaders(bool);

private slots:

    void ContextMenu(const QPoint &);
    void executeRaiseOrStartAction();
    //    void ElementClicked(QTreeWidgetItem *item, int column);

    void ApplicationBrutallyKilled();
    void updateDomElement(QTreeWidgetItem *item, int column);
    void SetItemEditable(QTreeWidgetItem* item, int column);
    void collapse_parent();
    void collapse_all();
    void copyCommand();
    //   void ReloadTree();

    //    void FindItem();

    void slotDBusRaise();
    void slotDBusStop();

    void checkIfCumbiaApp();

protected:
    
    void mouseDoubleClickEvent(QMouseEvent* me);

protected slots:
private:

    QString wintitle, filename;
    QString systray_icon_file;
    /* Starts the application invoking the launcher in x11appli
     * library
     */
    void StartApplication(const QString &fullCmdPath, const QStringList& args);
    
    /* Returns the number of elements named `link' children of an element */
    int NumberOfLinks(QDomElement elem);
    
    QString LookForEnvPath(QTreeWidgetItem* item);
    QString LookForTreeColor(QTreeWidgetItem* item);
    bool ReplacePlaceholders(QString &cmd);
    void applyColor(QTreeWidgetItem *, QString);

    void parseFolderElement(const QDomElement &element,
                            QTreeWidgetItem *parentItem = 0);
    QTreeWidgetItem *createItem(const QDomElement &element,
                                QTreeWidgetItem *parentItem = 0);

    void ExecuteElement(QTreeWidgetItem *item, int column);
    
    QMenu *CreateIconMeaningsMenu();
    
    QDomDocument domDocument;
    
    
    QHash<QTreeWidgetItem *, QDomElement> domElementForItem;
    QIcon folderIcon, folderExecIcon;
    QIcon bookmarkIcon;
    
    /* This is the list of all the applications deduced by the browser.xml
     * during the initial parsing.
     * Every element is dynamically allocated at startup and is searched by
     * means of GetAppByArgs().
     */
    QHash<EApplicationLauncher *, QTreeWidgetItem *> m_appItemHash;

    /* This one returns the EApplicationLauncher corresponding to the QTreeWidgetItem
     * passed as arguments.
     */
    EApplicationLauncher* GetAppByTreeWidgetItem(QTreeWidgetItem* item);

    /* The following adds the application to the list, verifying
     * first if an entry with the same command already exists 
     */
    EApplicationLauncher* AddApplicationToList(const QString &fullCommandLine, QTreeWidgetItem *item);
    
    QMenu* iconmeanings;
    
    Finder* finder;
    int LookFor(QString expression);
    int LookForFontSize(QTreeWidgetItem *item);

    int defaultFontSize;
    bool fontAutoDecrease;
    bool edit_mode, adopt, x11_unique_check_enabled;

    QStringList m_cumbia_apps_names;
};

#endif