/***************************************************************************
 *   Copyright (C) 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 "imageprovider.h"
#include <QPixmap>
#include <QTimer>
#include <QSettings>
#include <QDir>
#include <QDebug>
#include <QFileSystemWatcher>

ImageProvider::ImageProvider(QObject *parent)
: QObject(parent), m_offset(0)
{
	m_timer = new QTimer(this);
	connect( m_timer, SIGNAL(timeout()), this, SLOT(timeout()));
	
	QSettings settings;
	m_timer->setInterval (settings.value("Photos/Timeout", 5000).value<int>() );
	m_timer->start();
	
	QFileSystemWatcher *watcher = new QFileSystemWatcher( this );
	connect( watcher, SIGNAL(directoryChanged ( const QString &)),
	         this, SLOT(updateImageCache()));
	watcher->addPath(settings.value("Photos/RootDirectory", QDir::currentPath() ).value<QString>());
	QTimer::singleShot( 0, this, SLOT(updateImageCache()));
}


ImageProvider::~ImageProvider()
{
}

void ImageProvider::timeout()
{
	QPixmap image;

	if( m_fileList.size() <= m_offset )
		m_offset = 0;
	
	if( image.load( m_fileList.value(m_offset)) )
		emit newImage( image );
	else
		qDebug() << "Failed to load" << m_fileList.value(m_offset);
	
	m_offset++;
}

void ImageProvider::updateImageCache()
{
	QSettings settings;
	m_fileList.clear();
	scanDirectory( settings.value("Photos/RootDirectory", QDir::currentPath() ).value<QString>() );
	
	qDebug() << m_fileList;
	
	timeout();
}

void ImageProvider::scanDirectory(const QString & fileDir)
{
	QSettings settings;
	
	QDir dir( fileDir );
	QStringList filters = settings.value("Photos/Types", QStringList() << "*.jpg" << "*.jpeg" ).value<QStringList>();	
		
	foreach( QFileInfo info , dir.entryInfoList(filters, QDir::Files) )
		m_fileList << info.absoluteFilePath();
	
	foreach( QFileInfo info , dir.entryInfoList( QDir::NoDotAndDotDot | QDir::Dirs ) )
		scanDirectory( info.absoluteFilePath() );
}



