/*
Copyright (C) 2006 Evan Teran
                   eteran@alum.rit.edu

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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include "RecentFileManager.h"
#include <QSettings>
#include <QAction>
#include <QMenu>

//------------------------------------------------------------------------------
// Name: RecentFileManager()
// Desc: constructor
//------------------------------------------------------------------------------
RecentFileManager::RecentFileManager(QWidget * parent, Qt::WindowFlags f) : QWidget(parent, f), m_Menu(0) {
	loadFileList();
}

//------------------------------------------------------------------------------
// Name: ~RecentFileManager()
// Desc: destructor
//------------------------------------------------------------------------------
RecentFileManager::~RecentFileManager() {
	saveFileList();
}

//------------------------------------------------------------------------------
// Name: saveFileList()
// Desc: 
//------------------------------------------------------------------------------
void RecentFileManager::saveFileList() {
	QSettings settings;
	settings.beginGroup("Recent Files");
	
	for(int i = 0; i < MaxRecentFiles; ++i) {
		QString s;
		if(i < m_FileList.size()) {
			s = m_FileList[i];
		}
		settings.setValue(QString("File%1").arg(i), s);
	}
	settings.endGroup();
}

//------------------------------------------------------------------------------
// Name: loadFileList()
// Desc: 
//------------------------------------------------------------------------------
void RecentFileManager::loadFileList() {
	QSettings settings;
	settings.beginGroup("Recent Files");
	
	m_FileList.clear();
	for(int i = 0; i < MaxRecentFiles; ++i) {
		const QString s = settings.value(QString("File%1").arg(i)).value<QString>();
		if(!s.isEmpty()) {
			m_FileList.push_back(s);
		}
	}
	settings.endGroup();
}

//------------------------------------------------------------------------------
// Name: clearFileList()
// Desc: 
//------------------------------------------------------------------------------
void RecentFileManager::clearFileList() {
	m_FileList.clear();
	if(m_Menu != 0) {
		m_Menu->clear();
		m_Menu->addSeparator();
		m_Menu->addAction(tr("Clear &Menu"), this, SLOT(clearFileList()));
	}
}

//------------------------------------------------------------------------------
// Name: createMenu()
// Desc: 
//------------------------------------------------------------------------------
QMenu *RecentFileManager::createMenu() {

	if(m_Menu == 0) {
		m_Menu = new QMenu(this);
		update();
	}

	return m_Menu;
}

//------------------------------------------------------------------------------
// Name: createMenu()
// Desc: 
//------------------------------------------------------------------------------
void RecentFileManager::update() {
	if(m_Menu != 0) {
		m_Menu->clear();

		foreach(const QString &s, m_FileList) {
			QAction *const action = m_Menu->addAction(s, this, SLOT(itemSelected()));
			if(action != 0) {
				action->setData(s);
			}
		}

		m_Menu->addSeparator();
		m_Menu->addAction(tr("Clear &Menu"), this, SLOT(clearFileList()));
	}
}

//------------------------------------------------------------------------------
// Name: itemSelected()
// Desc: 
//------------------------------------------------------------------------------
void RecentFileManager::itemSelected() {
	QAction *const action = qobject_cast<QAction *>(sender());
	if(action != 0) {
		const QString s = action->data().toString();
		emit fileSelected(s);
	}
}

//------------------------------------------------------------------------------
// Name: addFile(const QString &file)
// Desc: 
//------------------------------------------------------------------------------
void RecentFileManager::addFile(const QString &file) {
	// update recent file list
	if(!m_FileList.contains(file)) {
		m_FileList.push_front(file);

		// make sure we don't add more than the max
		while(m_FileList.size() > MaxRecentFiles) {
			m_FileList.pop_back();
		}
		update();
	}
}
