/*************************************************************************
	removes a WindowFactory from the registry (by pointer)
*************************************************************************/
void WindowFactoryManager::removeFactory(WindowFactory* factory)
{
    if (factory != NULL)
    {
        removeFactory(factory->getTypeName());
    }

}
//----------------------------------------------------------------------------//
void WindowFactoryManager::removeAllFactories(void)
{
    while (!d_factoryRegistry.empty())
        removeFactory((*d_factoryRegistry.begin()).second);
}
OpenWithManagerDialog::OpenWithManagerDialog(QWidget *parentObject) :
  QDialog(parentObject),
  ui(new Ui::OpenWithManagerDialog),
  m_factoryModel(new OpenWithExecutableModel(this)),
  m_patternModel(new OpenWithPatternModel(this)),
  m_patternMapper(new QDataWidgetMapper(this)),
  m_handlerMapper(new QDataWidgetMapper(this)),
  m_patternTypeDelegate(new PatternTypeDelegate(this)),
  m_dirty(false)
{
  // Setup ui:
  ui->setupUi(this);

  // Setup MVC:
  ui->tableFactories->setModel(m_factoryModel);

  ui->tablePattern->setModel(m_patternModel);
  ui->tablePattern->setItemDelegate(m_patternTypeDelegate);

  ui->comboMatch->setModel(m_patternTypeDelegate->patternTypeModel());

  m_handlerMapper->setModel(m_factoryModel);
  m_handlerMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
  m_handlerMapper->addMapping(ui->editName, 0);
  m_handlerMapper->addMapping(ui->comboType, 1, "currentIndex");
  m_handlerMapper->addMapping(ui->editExec, 2);

  m_patternMapper->setModel(m_patternModel);
  m_patternMapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);
  m_patternMapper->setItemDelegate(m_patternTypeDelegate);
  m_patternMapper->addMapping(ui->editPattern,
                              OpenWithPatternModel::PatternCol);
  m_patternMapper->addMapping(ui->comboMatch,
                              OpenWithPatternModel::PatternTypeCol);
  m_patternMapper->addMapping(ui->checkCaseSensitive,
                              OpenWithPatternModel::CaseSensitivityCol);

  // Setup executable completion
  QFileSystemModel *fsModel = new QFileSystemModel(this);
  fsModel->setFilter(QDir::Files | QDir::Dirs | QDir::NoDot);
  fsModel->setRootPath(QDir::rootPath());
  QCompleter *fsCompleter = new QCompleter(fsModel, this);
  ui->editExec->setCompleter(fsCompleter);

  // Factory GUI:
  connect(ui->pushAddFactory, SIGNAL(clicked()),
          this, SLOT(addFactory()));
  connect(ui->pushRemoveFactory, SIGNAL(clicked()),
          this, SLOT(removeFactory()));
  connect(ui->comboType, SIGNAL(currentIndexChanged(int)),
          this, SLOT(factoryTypeChanged(int)));
  connect(ui->tableFactories->selectionModel(),
          SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
          this, SLOT(factorySelectionChanged()));

  // Executable GUI:
  connect(ui->pushExec, SIGNAL(clicked()), SLOT(browseExecutable()));
  connect(ui->editExec, SIGNAL(textChanged(QString)), SLOT(testExecutable()));

  // Pattern GUI:
  connect(ui->pushAddPattern, SIGNAL(clicked()),
          this, SLOT(addPattern()));
  connect(ui->pushRemovePattern, SIGNAL(clicked()),
          this, SLOT(removePattern()));
  connect(ui->tablePattern->selectionModel(),
          SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
          this, SLOT(patternSelectionChanged()));
  connect(ui->pushApplyPattern, SIGNAL(clicked()),
          m_patternMapper, SLOT(submit()));
  connect(ui->pushRevertPattern, SIGNAL(clicked()),
          m_patternMapper, SLOT(revert()));
  connect(m_patternModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
          this, SLOT(patternDimensionsChanged()));
  connect(m_patternModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
          this, SLOT(patternDimensionsChanged()));
  connect(m_patternModel, SIGNAL(modelReset()),
          this, SLOT(patternDimensionsChanged()));

  // Test updates:
  connect(ui->editTest, SIGNAL(textChanged(QString)),
          this, SLOT(checkTestText()));
  connect(m_patternModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
          this, SLOT(checkTestText()));
  connect(m_patternModel, SIGNAL(layoutChanged()),
          this, SLOT(checkTestText()));
  connect(ui->tableFactories->selectionModel(),
          SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
          this, SLOT(checkTestText()));

  // handle apply button:
  connect(ui->buttonBox, SIGNAL(clicked(QAbstractButton*)),
          SLOT(buttonBoxClicked(QAbstractButton*)));

  // Mark dirty when the data changes.
  connect(m_factoryModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
          SLOT(markDirty()));
  connect(m_factoryModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
          SLOT(markDirty()));
  connect(m_factoryModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
          SLOT(markDirty()));
  connect(m_patternModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
          SLOT(markDirty()));
  connect(m_patternModel, SIGNAL(rowsInserted(QModelIndex, int, int)),
          SLOT(markDirty()));
  connect(m_patternModel, SIGNAL(rowsRemoved(QModelIndex, int, int)),
          SLOT(markDirty()));
}