/************************************************************************
 *									*
 *  This file is part of Kooka, a scanning/OCR application using	*
 *  Qt <http://www.qt.io> and KDE Frameworks <http://www.kde.org>.	*
 *									*
 *  Copyright (C) 2000-2016 Klaas Freitag <freitag@suse.de>		*
 *                          Jonathan Marten <jjm@keelhaul.me.uk>	*
 *									*
 *  Kooka is free software; you can redistribute it and/or modify it	*
 *  under the terms of the GNU Library General Public License as	*
 *  published by the Free Software Foundation and appearing in the	*
 *  file COPYING included in the packaging of this file;  either	*
 *  version 2 of the License, or (at your option) any later version.	*
 *									*
 *  As a special exception, permission is given to link this program	*
 *  with any version of the KADMOS OCR/ICR engine (a product of		*
 *  reRecognition GmbH, Kreuzlingen), and distribute the resulting	*
 *  executable without including the source code for KADMOS in the	*
 *  source distribution.						*
 *									*
 *  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;  see the file COPYING.  If		*
 *  not, see <http://www.gnu.org/licenses/>.				*
 *									*
 ************************************************************************/

#include "destinationgallery.h"

#include <kpluginfactory.h>
#include <klocalizedstring.h>

#include "scangallery.h"
#include "kookasettings.h"
#include "imgsaver.h"
#include "destination_logging.h"


K_PLUGIN_FACTORY_WITH_JSON(DestinationGalleryFactory, "kookadestination-gallery.json", registerPlugin<DestinationGallery>();)
#include "destinationgallery.moc"


DestinationGallery::DestinationGallery(QObject *pnt, const QVariantList &args)
: AbstractDestination(pnt, "DestinationGallery"),
    mBatchFormat("")
{
}


void DestinationGallery::batchStart(const MultiScanOptions *opts)
{
    mBatchFormat = ImageFormat("");			// reset for start of batch
    AbstractDestination::batchStart(opts);
}


bool DestinationGallery::scanStarting(ScanImage::ImageType type)
{
    qCDebug(DESTINATION_LOG) << "type" << type;
    if (type==ScanImage::None) return (true);		// can't prompt for anything yet

    // Create the ImgSaver which will be used to save the image.
    // However, do not prompt for a file name or format until the
    // multiple scan options have been set below.
    if (!gallery()->prepareToSave(type)) return (false);
    ImgSaver *saver = gallery()->imageSaver();

    // Now set the saver options, if multiple scans are being done.
    const MultiScanOptions::Flags f = multiScanOptions()->flags();
    if (f & MultiScanOptions::MultiScan)
    {
        saver->setSaveAskFilename(!(f & MultiScanOptions::AutoGenerate));

        const bool batchMultiple = (f & MultiScanOptions::BatchMultiple);
        saver->setSaveAskFormat(!batchMultiple);
        if (batchMultiple && mBatchFormat.isValid()) saver->setSaveFormat(mBatchFormat);
    }

    // Now ask for the file name and format, if requested and required
    ImgSaver::ImageSaveStatus stat = saver->setImageInfo(type);
    if (stat==ImgSaver::SaveStatusCanceled) return (false);

    // Save the format that was used, for the next file in the batch
    ImageFormat savedFmt = saver->saveFormat();
    if (!mBatchFormat.isValid() && savedFmt.isValid()) mBatchFormat = savedFmt;

    return (true);
}


bool DestinationGallery::imageScanned(ScanImage::Ptr img)
{
    qCDebug(DESTINATION_LOG) << "received image size" << img->size();
    gallery()->addImage(img);
    return (true);
}


KLocalizedString DestinationGallery::scanDestinationString()
{
    return (kxi18n("<filename>%1</filename>").subs(gallery()->saveURL().url(QUrl::PreferLocalFile)));
}


MultiScanOptions::Capabilities DestinationGallery::capabilities() const
{
    return (MultiScanOptions::AcceptBatch|MultiScanOptions::DefaultBatch|MultiScanOptions::FileNames|MultiScanOptions::CannotCancel);
}
