2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
|
|
|
// Copyright (C) 2001-2015 David Capello
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License version 2 as
|
|
|
|
// published by the Free Software Foundation.
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
#include "app/console.h"
|
|
|
|
#include "app/document.h"
|
|
|
|
#include "app/file/file.h"
|
2012-06-16 10:37:59 +08:00
|
|
|
#include "app/file_selector.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/job.h"
|
|
|
|
#include "app/modules/editors.h"
|
|
|
|
#include "app/modules/gui.h"
|
|
|
|
#include "app/recent_files.h"
|
|
|
|
#include "app/ui/status_bar.h"
|
|
|
|
#include "app/ui_context.h"
|
2012-07-06 12:06:00 +08:00
|
|
|
#include "base/bind.h"
|
2015-03-02 22:18:33 +08:00
|
|
|
#include "base/path.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
#include "base/thread.h"
|
2012-07-06 12:06:00 +08:00
|
|
|
#include "base/unique_ptr.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "ui/ui.h"
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-10-15 06:58:11 +08:00
|
|
|
#include <cstdio>
|
2012-07-06 12:06:00 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
class OpenFileCommand : public Command {
|
2012-01-06 06:45:03 +08:00
|
|
|
public:
|
|
|
|
OpenFileCommand();
|
2014-08-15 10:07:47 +08:00
|
|
|
Command* clone() const override { return new OpenFileCommand(*this); }
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
protected:
|
2014-08-15 10:07:47 +08:00
|
|
|
void onLoadParams(Params* params) override;
|
|
|
|
void onExecute(Context* context) override;
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-07-06 12:06:00 +08:00
|
|
|
private:
|
|
|
|
std::string m_filename;
|
2015-03-02 22:18:33 +08:00
|
|
|
std::string m_folder;
|
2012-01-06 06:45:03 +08:00
|
|
|
};
|
|
|
|
|
2012-07-06 12:06:00 +08:00
|
|
|
class OpenFileJob : public Job, public IFileOpProgress
|
2012-01-06 06:45:03 +08:00
|
|
|
{
|
2012-07-06 12:06:00 +08:00
|
|
|
public:
|
2013-10-15 06:58:11 +08:00
|
|
|
OpenFileJob(FileOp* fop)
|
2012-07-06 12:06:00 +08:00
|
|
|
: Job("Loading file")
|
|
|
|
, m_fop(fop)
|
|
|
|
{
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 12:06:00 +08:00
|
|
|
void showProgressWindow() {
|
|
|
|
startJob();
|
2014-05-03 04:04:55 +08:00
|
|
|
|
|
|
|
if (isCanceled())
|
|
|
|
fop_stop(m_fop);
|
|
|
|
|
|
|
|
waitJob();
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
2012-07-06 12:06:00 +08:00
|
|
|
private:
|
|
|
|
// Thread to do the hard work: load the file from the disk.
|
2014-08-15 10:07:47 +08:00
|
|
|
virtual void onJob() override {
|
2012-07-06 12:06:00 +08:00
|
|
|
try {
|
|
|
|
fop_operate(m_fop, this);
|
|
|
|
}
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
fop_error(m_fop, "Error loading file:\n%s", e.what());
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-07-06 12:06:00 +08:00
|
|
|
if (fop_is_stop(m_fop) && m_fop->document) {
|
|
|
|
delete m_fop->document;
|
|
|
|
m_fop->document = NULL;
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2012-07-06 12:06:00 +08:00
|
|
|
fop_done(m_fop);
|
|
|
|
}
|
2012-01-06 06:45:03 +08:00
|
|
|
|
2014-08-15 10:07:47 +08:00
|
|
|
virtual void ackFileOpProgress(double progress) override {
|
2012-07-06 12:06:00 +08:00
|
|
|
jobProgress(progress);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
2012-07-06 12:06:00 +08:00
|
|
|
|
|
|
|
FileOp* m_fop;
|
|
|
|
};
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
OpenFileCommand::OpenFileCommand()
|
|
|
|
: Command("OpenFile",
|
|
|
|
"Open Sprite",
|
|
|
|
CmdRecordableFlag)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFileCommand::onLoadParams(Params* params)
|
|
|
|
{
|
|
|
|
m_filename = params->get("filename");
|
2015-03-02 22:18:33 +08:00
|
|
|
m_folder = params->get("folder"); // Initial folder
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenFileCommand::onExecute(Context* context)
|
|
|
|
{
|
|
|
|
Console console;
|
|
|
|
|
|
|
|
// interactive
|
|
|
|
if (context->isUiAvailable() && m_filename.empty()) {
|
|
|
|
char exts[4096];
|
|
|
|
get_readable_extensions(exts, sizeof(exts));
|
2015-03-02 22:18:33 +08:00
|
|
|
|
|
|
|
// Add backslash as show_file_selector() expected a filename as
|
|
|
|
// initial path (and the file part is removed from the path).
|
|
|
|
if (!m_folder.empty() && !base::is_path_separator(m_folder[m_folder.size()-1]))
|
|
|
|
m_folder.push_back(base::path_separator);
|
|
|
|
|
2015-03-05 09:41:34 +08:00
|
|
|
m_filename = app::show_file_selector("Open", m_folder, exts,
|
2015-03-03 03:07:35 +08:00
|
|
|
FileSelectorType::Open);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_filename.empty()) {
|
2014-07-20 09:01:39 +08:00
|
|
|
base::UniquePtr<FileOp> fop(fop_to_load_document(context, m_filename.c_str(), FILE_LOAD_SEQUENCE_ASK));
|
2012-01-06 06:45:03 +08:00
|
|
|
bool unrecent = false;
|
|
|
|
|
|
|
|
if (fop) {
|
|
|
|
if (fop->has_error()) {
|
|
|
|
console.printf(fop->error.c_str());
|
|
|
|
unrecent = true;
|
|
|
|
}
|
|
|
|
else {
|
2013-10-15 06:58:11 +08:00
|
|
|
OpenFileJob task(fop);
|
2012-07-06 12:06:00 +08:00
|
|
|
task.showProgressWindow();
|
2012-01-06 06:45:03 +08:00
|
|
|
|
|
|
|
// Post-load processing, it is called from the GUI because may require user intervention.
|
|
|
|
fop_post_load(fop);
|
|
|
|
|
|
|
|
// Show any error
|
|
|
|
if (fop->has_error())
|
|
|
|
console.printf(fop->error.c_str());
|
|
|
|
|
|
|
|
Document* document = fop->document;
|
|
|
|
if (document) {
|
|
|
|
App::instance()->getRecentFiles()->addRecentFile(fop->filename.c_str());
|
2014-07-29 11:53:24 +08:00
|
|
|
document->setContext(context);
|
2012-01-06 06:45:03 +08:00
|
|
|
}
|
|
|
|
else if (!fop_is_stop(fop))
|
|
|
|
unrecent = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The file was not found or was loaded loaded with errors,
|
|
|
|
// so we can remove it from the recent-file list
|
|
|
|
if (unrecent) {
|
|
|
|
App::instance()->getRecentFiles()->removeRecentFile(m_filename.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Do nothing (the user cancelled or something like that)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Command* CommandFactory::createOpenFileCommand()
|
|
|
|
{
|
|
|
|
return new OpenFileCommand;
|
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|