Sorry, forgot to check in a file change for last patch :-(

This commit is contained in:
Oliver Duis
2010-12-18 19:21:17 +01:00
parent eb7aa574cf
commit cd2edba2bd
2 changed files with 296 additions and 244 deletions

View File

@@ -1,210 +1,258 @@
/* /*
* This file is part of RawTherapee. * This file is part of RawTherapee.
* *
* Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com> * Copyright (c) 2004-2010 Gabor Horvath <hgabor@rawtherapee.com>
* Copyright (c) 2010 Sasha Vasko <sasha@aftercode.net> * Copyright (c) 2010 Sasha Vasko <sasha@aftercode.net>
* * Copyright (c) 2010 Oliver Duis <www.oliverduis.de>
* RawTherapee is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by * RawTherapee is free software: you can redistribute it and/or modify
* the Free Software Foundation, either version 3 of the License, or * it under the terms of the GNU General Public License as published by
* (at your option) any later version. * the Free Software Foundation, either version 3 of the License, or
* * (at your option) any later version.
* RawTherapee is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of * RawTherapee is distributed in the hope that it will be useful,
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * but WITHOUT ANY WARRANTY; without even the implied warranty of
* GNU General Public License for more details. * 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 RawTherapee. If not, see <http://www.gnu.org/licenses/>. * You should have received a copy of the GNU General Public License
*/ * along with RawTherapee. If not, see <http://www.gnu.org/licenses/>.
*/
#include <safegtk.h>
#include <guiutils.h> #include <safegtk.h>
#include <guiutils.h>
Glib::RefPtr<Gdk::Pixbuf> safe_create_from_file(const std::string& filename) #include <glib/gstdio.h>
{
Glib::RefPtr<Gdk::Pixbuf> res;
#ifdef GLIBMM_EXCEPTIONS_ENABLED Glib::RefPtr<Gdk::Pixbuf> safe_create_from_file(const std::string& filename)
try { {
res = Gdk::Pixbuf::create_from_file (filename); Glib::RefPtr<Gdk::Pixbuf> res;
} #ifdef GLIBMM_EXCEPTIONS_ENABLED
catch (Glib::Exception& ex) { try {
printf ("%s\n", ex.what().c_str()); res = Gdk::Pixbuf::create_from_file (filename);
} }
#else catch (Glib::Exception& ex) {
std::auto_ptr<Glib::Error> error; printf ("%s\n", ex.what().c_str());
res = Gdk::Pixbuf::create_from_file (filename, error); }
if (error.get()) #else
printf ("%s\n", error->what().c_str()); std::auto_ptr<Glib::Error> error;
#endif res = Gdk::Pixbuf::create_from_file (filename, error);
if (error.get())
return res; printf ("%s\n", error->what().c_str());
} #endif
Cairo::RefPtr<Cairo::ImageSurface> safe_create_from_png(const std::string& filename) return res;
{ }
Cairo::RefPtr<Cairo::ImageSurface> res;
Cairo::RefPtr<Cairo::ImageSurface> safe_create_from_png(const std::string& filename)
if (!Glib::file_test (filename, Glib::FILE_TEST_EXISTS)) { {
printf ("ERROR: File \"%s\" not found.\n", filename.c_str()); Cairo::RefPtr<Cairo::ImageSurface> res;
} else {
try { if (!safe_file_test (filename, Glib::FILE_TEST_EXISTS)) {
res = Cairo::ImageSurface::create_from_png (filename); printf ("ERROR: File \"%s\" not found.\n", filename.c_str());
} catch (...) {} } else {
} try {
res = Cairo::ImageSurface::create_from_png (filename);
return res; } catch (...) {}
} }
Glib::RefPtr<Gio::FileInfo> safe_query_file_info (Glib::RefPtr<Gio::File> &file) return res;
{ }
Glib::RefPtr<Gio::FileInfo> info;
#ifdef GLIBMM_EXCEPTIONS_ENABLED Glib::RefPtr<Gio::FileInfo> safe_query_file_info (Glib::RefPtr<Gio::File> &file)
try { info = file->query_info(); }catch (...) { } {
#else Glib::RefPtr<Gio::FileInfo> info;
std::auto_ptr<Glib::Error> error; #ifdef GLIBMM_EXCEPTIONS_ENABLED
info = file->query_info("*", Gio::FILE_QUERY_INFO_NONE, error); try { info = file->query_info(); }catch (...) { }
#endif #else
return info; std::auto_ptr<Glib::Error> error;
} info = file->query_info("*", Gio::FILE_QUERY_INFO_NONE, error);
#endif
#ifdef GLIBMM_EXCEPTIONS_ENABLED return info;
# define SAFE_ENUMERATOR_CODE_START \ }
do{try { if ((dirList = dir->enumerate_children ())) \
for (Glib::RefPtr<Gio::FileInfo> info = dirList->next_file(); info; info = dirList->next_file()) { #ifdef GLIBMM_EXCEPTIONS_ENABLED
# define SAFE_ENUMERATOR_CODE_START \
# define SAFE_ENUMERATOR_CODE_END \ do{try { if ((dirList = dir->enumerate_children ())) \
}} catch (Glib::Exception& ex) { printf ("%s\n", ex.what().c_str()); }}while(0) for (Glib::RefPtr<Gio::FileInfo> info = dirList->next_file(); info; info = dirList->next_file()) {
#else
# define SAFE_ENUMERATOR_CODE_START \ # define SAFE_ENUMERATOR_CODE_END \
do{std::auto_ptr<Glib::Error> error; Glib::RefPtr<Gio::Cancellable> cancellable; \ }} catch (Glib::Exception& ex) { printf ("%s\n", ex.what().c_str()); }}while(0)
if ((dirList = dir->enumerate_children (cancellable, "*", Gio::FILE_QUERY_INFO_NONE, error))) \ #else
for (Glib::RefPtr<Gio::FileInfo> info = dirList->next_file(cancellable, error); !error.get() && info; info = dirList->next_file(cancellable, error)) { # define SAFE_ENUMERATOR_CODE_START \
do{std::auto_ptr<Glib::Error> error; Glib::RefPtr<Gio::Cancellable> cancellable; \
# define SAFE_ENUMERATOR_CODE_END } if (error.get()) printf ("%s\n", error->what().c_str());}while (0) if ((dirList = dir->enumerate_children (cancellable, "*", Gio::FILE_QUERY_INFO_NONE, error))) \
#endif for (Glib::RefPtr<Gio::FileInfo> info = dirList->next_file(cancellable, error); !error.get() && info; info = dirList->next_file(cancellable, error)) {
void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<FileMTimeInfo> &flist) # define SAFE_ENUMERATOR_CODE_END } if (error.get()) printf ("%s\n", error->what().c_str());}while (0)
{ #endif
Glib::RefPtr<Gio::FileEnumerator> dirList;
if (dir) { void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<FileMTimeInfo> &flist)
SAFE_ENUMERATOR_CODE_START {
flist.push_back (FileMTimeInfo (removeExtension(info->get_name()), info->modification_time())); Glib::RefPtr<Gio::FileEnumerator> dirList;
SAFE_ENUMERATOR_CODE_END; if (dir) {
} SAFE_ENUMERATOR_CODE_START
} flist.push_back (FileMTimeInfo (removeExtension(info->get_name()), info->modification_time()));
SAFE_ENUMERATOR_CODE_END;
void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &names, const Glib::ustring &directory) }
{ }
Glib::RefPtr<Gio::FileEnumerator> dirList;
if (dir) { void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &names, const Glib::ustring &directory)
SAFE_ENUMERATOR_CODE_START {
names.push_back (Glib::build_filename (directory, info->get_name())); Glib::RefPtr<Gio::FileEnumerator> dirList;
SAFE_ENUMERATOR_CODE_END; if (dir) {
} SAFE_ENUMERATOR_CODE_START
} names.push_back (Glib::build_filename (directory, info->get_name()));
SAFE_ENUMERATOR_CODE_END;
}
void safe_build_subdir_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &subDirs, bool add_hidden) }
{
Glib::RefPtr<Gio::FileEnumerator> dirList;
if (dir) void safe_build_subdir_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &subDirs, bool add_hidden)
{ {
SAFE_ENUMERATOR_CODE_START Glib::RefPtr<Gio::FileEnumerator> dirList;
if (info->get_file_type() == Gio::FILE_TYPE_DIRECTORY && (!info->is_hidden() || add_hidden)) if (dir)
subDirs.push_back (info->get_name()); {
SAFE_ENUMERATOR_CODE_END; SAFE_ENUMERATOR_CODE_START
} if (info->get_file_type() == Gio::FILE_TYPE_DIRECTORY && (!info->is_hidden() || add_hidden))
} subDirs.push_back (info->get_name());
SAFE_ENUMERATOR_CODE_END;
Glib::ustring safe_locale_to_utf8 (const std::string& src) }
{ }
Glib::ustring utf8_str;
#ifdef GLIBMM_EXCEPTIONS_ENABLED Glib::ustring safe_locale_to_utf8 (const std::string& src)
try { {
utf8_str = Glib::locale_to_utf8(src); Glib::ustring utf8_str;
} #ifdef GLIBMM_EXCEPTIONS_ENABLED
catch (const Glib::ConvertError& e) { try {
utf8_str = Glib::convert_with_fallback(src, "UTF8", "LATIN1","?"); utf8_str = Glib::locale_to_utf8(src);
} }
#else catch (const Glib::ConvertError& e) {
{ utf8_str = Glib::convert_with_fallback(src, "UTF8", "LATIN1","?");
std::auto_ptr<Glib::Error> error; }
utf8_str = locale_to_utf8(src, error); #else
if (error.get()) {
utf8_str = Glib::convert_with_fallback(src, "UTF8", "LATIN1","?", error); std::auto_ptr<Glib::Error> error;
} utf8_str = locale_to_utf8(src, error);
#endif //GLIBMM_EXCEPTIONS_ENABLED if (error.get())
return utf8_str; utf8_str = Glib::convert_with_fallback(src, "UTF8", "LATIN1","?", error);
} }
#endif //GLIBMM_EXCEPTIONS_ENABLED
std::string safe_locale_from_utf8 (const Glib::ustring& utf8_str) return utf8_str;
{ }
std::string str;
#ifdef GLIBMM_EXCEPTIONS_ENABLED std::string safe_locale_from_utf8 (const Glib::ustring& utf8_str)
try { {
str = Glib::locale_from_utf8(utf8_str); std::string str;
} #ifdef GLIBMM_EXCEPTIONS_ENABLED
catch (const Glib::ConvertError& e) { try {
//str = Glib::convert_with_fallback(utf8_str, "LATIN1", "UTF8", "?"); str = Glib::locale_from_utf8(utf8_str);
} }
#else catch (const Glib::ConvertError& e) {
{ //str = Glib::convert_with_fallback(utf8_str, "LATIN1", "UTF8", "?");
std::auto_ptr<Glib::Error> error; }
str = Glib::locale_from_utf8(utf8_str, error); #else
/*if (error.get()) {
{str = Glib::convert_with_fallback(utf8_str, "LATIN1", "UTF8", "?", error);}*/ std::auto_ptr<Glib::Error> error;
} str = Glib::locale_from_utf8(utf8_str, error);
#endif //GLIBMM_EXCEPTIONS_ENABLED /*if (error.get())
return str; {str = Glib::convert_with_fallback(utf8_str, "LATIN1", "UTF8", "?", error);}*/
} }
#endif //GLIBMM_EXCEPTIONS_ENABLED
return str;
bool safe_spawn_command_line_async (const Glib::ustring& cmd_utf8) }
{
std::string cmd; std::string safe_filename_from_utf8 (const Glib::ustring& utf8_str)
bool success = false; {
#ifdef GLIBMM_EXCEPTIONS_ENABLED std::string str;
try { #ifdef GLIBMM_EXCEPTIONS_ENABLED
cmd = Glib::filename_from_utf8(cmd_utf8); try {
printf ("command line: |%s|\n", cmd.c_str()); str = Glib::filename_from_utf8 (utf8_str);
Glib::spawn_command_line_async (cmd); }
success = true; catch (const Glib::ConvertError& e) {
} catch (Glib::Exception& ex) { //str = Glib::convert_with_fallback(utf8_str, "LATIN1", "UTF8", "?");
printf ("%s\n", ex.what().c_str()); }
} #else
#else {
std::auto_ptr<Glib::Error> error; std::auto_ptr<Glib::Error> error;
cmd = Glib::filename_from_utf8(cmd_utf8, error); str = Glib::filename_from_utf8 (utf8_str, error);
if (!error.get()) { /*if (error.get())
printf ("command line: |%s|\n", cmd.c_str()); {str = Glib::convert_with_fallback(utf8_str, "LATIN1", "UTF8", "?", error);}*/
Glib::spawn_command_line_async (cmd, error); }
} #endif //GLIBMM_EXCEPTIONS_ENABLED
if (error.get()) return str;
printf ("%s\n", error->what().c_str()); }
else
success = true; bool safe_spawn_command_line_async (const Glib::ustring& cmd_utf8)
#endif {
return success; std::string cmd;
} bool success = false;
#ifdef GLIBMM_EXCEPTIONS_ENABLED
bool safe_spawn_command_line_sync (const Glib::ustring& cmd_utf8) try {
{ cmd = Glib::filename_from_utf8(cmd_utf8);
std::string cmd; printf ("command line: |%s|\n", cmd.c_str());
std::string stdOut; Glib::spawn_command_line_async (cmd);
std::string stdErr; success = true;
} catch (Glib::Exception& ex) {
bool success = false; printf ("%s\n", ex.what().c_str());
}
int exitStatus=-1; #else
try { std::auto_ptr<Glib::Error> error;
cmd = Glib::filename_from_utf8(cmd_utf8); cmd = Glib::filename_from_utf8(cmd_utf8, error);
printf ("command line: |%s|\n", cmd.c_str()); if (!error.get()) {
printf ("command line: |%s|\n", cmd.c_str());
// if it crashes here on windows, make sure you have the GTK runtime files gspawn-win32-helper*.exe files in RT directory Glib::spawn_command_line_async (cmd, error);
Glib::spawn_command_line_sync (cmd,NULL,NULL, &exitStatus); }
} catch (Glib::Exception& ex) { if (error.get())
printf ("%s\n", ex.what().c_str()); printf ("%s\n", error->what().c_str());
} else
return (exitStatus==0); success = true;
} #endif
return success;
}
bool safe_spawn_command_line_sync (const Glib::ustring& cmd_utf8)
{
std::string cmd;
std::string stdOut;
std::string stdErr;
bool success = false;
int exitStatus=-1;
try {
cmd = Glib::filename_from_utf8(cmd_utf8);
printf ("command line: |%s|\n", cmd.c_str());
// if it crashes here on windows, make sure you have the GTK runtime files gspawn-win32-helper*.exe files in RT directory
Glib::spawn_command_line_sync (cmd,NULL,NULL, &exitStatus);
} catch (Glib::Exception& ex) {
printf ("%s\n", ex.what().c_str());
}
return (exitStatus==0);
}
FILE * safe_g_fopen(const Glib::ustring& src,const gchar *mode)
{
return g_fopen(safe_filename_from_utf8(src).c_str(),mode);
}
bool safe_file_test (const Glib::ustring& filename, Glib::FileTest test)
{
return Glib::file_test (safe_filename_from_utf8(filename), test);
}
int safe_g_remove(const Glib::ustring& filename)
{
return ::g_remove(safe_filename_from_utf8(filename).c_str());
}
int safe_g_rename(const Glib::ustring& oldFilename, const Glib::ustring& newFilename)
{
return ::g_rename(safe_filename_from_utf8(oldFilename).c_str(), safe_filename_from_utf8(newFilename).c_str());
}
int safe_g_mkdir_with_parents(const Glib::ustring& dirName, int mode)
{
return ::g_mkdir_with_parents(safe_filename_from_utf8(dirName).c_str(), mode);
}

View File

@@ -1,34 +1,38 @@
#ifndef SAFE_GTK_H_INCLUDED #ifndef SAFE_GTK_H_INCLUDED
#define SAFE_GTK_H_INCLUDED #define SAFE_GTK_H_INCLUDED
#include <gtkmm.h> #include <gtkmm.h>
#include <glibmm.h> #include <glibmm.h>
#include <giomm.h> #include <giomm.h>
Glib::RefPtr<Gdk::Pixbuf> safe_create_from_file(const std::string& filename); Glib::RefPtr<Gdk::Pixbuf> safe_create_from_file(const std::string& filename);
Cairo::RefPtr<Cairo::ImageSurface> safe_create_from_png(const std::string& filename); Cairo::RefPtr<Cairo::ImageSurface> safe_create_from_png(const std::string& filename);
class FileMTimeInfo { class FileMTimeInfo {
public: public:
Glib::ustring fname; Glib::ustring fname;
Glib::TimeVal mtime; Glib::TimeVal mtime;
FileMTimeInfo (Glib::ustring name, Glib::TimeVal mtime) : fname(name), mtime(mtime) {} FileMTimeInfo (Glib::ustring name, Glib::TimeVal mtime) : fname(name), mtime(mtime) {}
bool operator<(const FileMTimeInfo& other) const { return mtime<other.mtime; } bool operator<(const FileMTimeInfo& other) const { return mtime<other.mtime; }
}; };
Glib::RefPtr<Gio::FileInfo> safe_query_file_info (Glib::RefPtr<Gio::File> &file); Glib::RefPtr<Gio::FileInfo> safe_query_file_info (Glib::RefPtr<Gio::File> &file);
void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<FileMTimeInfo> &flist); void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<FileMTimeInfo> &flist);
void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &names, const Glib::ustring &directory = ""); void safe_build_file_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &names, const Glib::ustring &directory = "");
void safe_build_subdir_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &subDirs, bool add_hidden); void safe_build_subdir_list (Glib::RefPtr<Gio::File> &dir, std::vector<Glib::ustring> &subDirs, bool add_hidden);
bool safe_spawn_command_line_async (const Glib::ustring& cmd_utf8); bool safe_spawn_command_line_async (const Glib::ustring& cmd_utf8);
bool safe_spawn_command_line_sync (const Glib::ustring& cmd_utf8); bool safe_spawn_command_line_sync (const Glib::ustring& cmd_utf8);
Glib::ustring safe_locale_to_utf8 (const std::string& src); // from rtengine Glib::ustring safe_locale_to_utf8 (const std::string& src); // from rtengine
std::string safe_locale_from_utf8 (const Glib::ustring& utf8_str); std::string safe_locale_from_utf8 (const Glib::ustring& utf8_str);
std::string safe_filename_from_utf8 (const Glib::ustring& utf8_str);
FILE * safe_g_fopen(const Glib::ustring& src,const gchar *mode);
#endif bool safe_file_test (const Glib::ustring& filename, Glib::FileTest test);
int safe_g_remove(const Glib::ustring& filename);
int safe_g_rename(const Glib::ustring& oldFilename, const Glib::ustring& newFilename);
int safe_g_mkdir_with_parents(const Glib::ustring& dirName, int mode);
#endif