#!/usr/bin/env python

# Copyright (c) 2010 Riverbank Computing Limited.
#
# This file is part of dip.
#
# This file may be used under the terms of the GNU General Public License
# v2 or v3 as published by the Free Software Foundation which can be found in
# the files LICENSE-GPL2.txt and LICENSE-GPL3.txt included in this package.
# In addition, as a special exception, Riverbank gives you certain additional
# rights.  These rights are described in the Riverbank GPL Exception, which
# can be found in the file GPL-Exception.txt in this package.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.


import sys

from PyQt4.QtGui import QApplication


# Every PyQt GUI application needs a QApplication.
app = QApplication(sys.argv, applicationName="DIP Builder")


# Create a plugin manager.
from dip.plugins import PluginManager

plugin_manager = PluginManager()


# Add the infrastructure plugins we need.
from dip.io.plugins import FilesystemPlugin, IOManagerPlugin
from dip.shell.plugins import QMainWindowShellPlugin, ShellManagerPlugin
from dip.ui.plugins import QtToolkitPlugin

plugin_manager.contribute('dip.plugins.plugins',
        QtToolkitPlugin(plugin_manager=plugin_manager))

plugin_manager.contribute('dip.plugins.plugins',
        IOManagerPlugin(plugin_manager=plugin_manager))

plugin_manager.contribute('dip.plugins.plugins',
        FilesystemPlugin(plugin_manager=plugin_manager))

plugin_manager.contribute('dip.plugins.plugins',
        ShellManagerPlugin(plugin_manager=plugin_manager))

plugin_manager.contribute('dip.plugins.plugins',
        QMainWindowShellPlugin(plugin_manager=plugin_manager))


# Enable What's This help.
from dip.shell.actions import WhatsThisAction

plugin_manager.contribute('dip.shell.actions', WhatsThisAction())


# Add the plugins that implement this application.
from dip.builder.plugins import BuilderPlugin, DistutilsPlugin

plugin_manager.contribute('dip.plugins.plugins',
        BuilderPlugin(plugin_manager=plugin_manager))

plugin_manager.contribute('dip.plugins.plugins',
        DistutilsPlugin(plugin_manager=plugin_manager))


# Enable the plugins.
plugin_manager.enable_plugins()


# Get the shell.
from dip.shell import IShell

shell = plugin_manager.service(IShell)


# Handle the command line.
if len(sys.argv) == 2:
    tool = shell.shell_manager.open(sys.argv[1])
    if tool is not None:
        shell.add_tool(tool)

elif len(sys.argv) > 2:
    shell.toolkit.warning("Usage Error",
            "At most one command line argument is expected.")


# Display the shell and start the application.
shell.widget.show()
sys.exit(app.exec_())
