/***************************************************************************
*   Copyright (C) 2006-2008 by Ian Reinhart Geiser                        *
*   geiseri@yahoo.com                                                     *
*                                                                         *
*   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.,                                       *
*   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/
#include "docbookexportdialog.h"
#include "ui_docbookexport.h"
#include "docbookreporter.h"
#include "mindmap.h"

#include <QFile>
#include <QFileDialog>
#include <QTextStream>
#include <QMessageBox>

DocbookExportDialog::DocbookExportDialog(QWidget *parent)
		: QDialog(parent)
{
	m_reporter = new DocbookReporter;
	m_ui = new Ui::DocbookExportDialog;
	m_ui->setupUi(this);
}


DocbookExportDialog::~DocbookExportDialog()
{
	delete m_reporter;
	delete m_ui;
}




void DocbookExportDialog::exportMindmap( MindMap * map )
{
	DocbookExportDialog dlg( QApplication::activeWindow() );
	dlg.m_mindMap = map;
	dlg.m_ui->filePath->setText( map->getProperty("docbookreport.path",
		map->generateFilePath("docbook")).toString() );
	dlg.m_ui->copyrightText->setText(map->getProperty("docbookreport.copyrightholder",
		map->getProperty("document.author")).toString() );
	dlg.m_ui->copyrightDate->setDate(map->getProperty("docbookreport.copyrightdate",
		QDate::currentDate()).toDate() );

	dlg.m_filePath = dlg.m_ui->filePath->text();
	dlg.exec();

	if( dlg.m_filePath.isEmpty() )
		return;

	map->setProperty("docbookreport.path", dlg.m_ui->filePath->text());
	map->setProperty("docbookreport.copyrightholder", dlg.m_ui->copyrightText->text());
	map->setProperty("docbookreport.copyrightdate", dlg.m_ui->copyrightDate->date());

	map->setModified(true);

	QFile exportFile( dlg.m_filePath );
	if( exportFile.open(QIODevice::WriteOnly) )
	{
		QTextStream ts(&exportFile);
		dlg.m_reporter->setResourceBaseDir( QFileInfo(exportFile).absoluteDir().path() );
		ts << dlg.m_reporter->generateReport( map );
	}
	else
	{
		QMessageBox::critical(&dlg, "Error", QString("Could not save file."), QMessageBox::Ok, QMessageBox::NoButton);
	}
	exportFile.close();
}

MindMap* DocbookExportDialog::mindMap() const
{
	return m_mindMap;
}


void DocbookExportDialog::setMindMap(MindMap* theValue)
{
	m_mindMap = theValue;
}

void DocbookExportDialog::on_selectFile_clicked( )
{
	QString fileName = QFileDialog::getSaveFileName(
	                                                 QApplication::activeWindow(), tr("Select an export file"),
	                                                 m_filePath,
	                                                 tr("Dockbook Files (*.docbook)"));
	if( !fileName.isEmpty() )
		m_filePath = fileName;
	m_ui->filePath->setText( m_filePath );
}
