Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / color-profile.cpp
index ee4118e4f0b625b09ad6b00914096cd1e6837cba..e08a416d3b829a9ac84379956186d5c5166ffb6a 100644 (file)
@@ -2,11 +2,12 @@
 # include "config.h"
 #endif
 
-#define DEBUG_LCMS
+#define noDEBUG_LCMS
 
 #include <glib/gstdio.h>
 #include <sys/fcntl.h>
 #include <gdkmm/color.h>
+#include <glib/gi18n.h>
 
 #ifdef DEBUG_LCMS
 #include <gtk/gtkmessagedialog.h>
@@ -14,6 +15,7 @@
 
 #include <cstring>
 #include <string>
+#include <io/sys.h>
 
 #ifdef WIN32
 #ifndef _WIN32_WINDOWS         // Allow use of features specific to Windows 98 or later. Required for correctly including icm.h
@@ -178,9 +180,8 @@ void ColorProfile::init( ColorProfile *cprof )
 void ColorProfile::release( SPObject *object )
 {
     // Unregister ourselves
-    SPDocument* document = SP_OBJECT_DOCUMENT(object);
-    if ( document ) {
-        SP_OBJECT_DOCUMENT (object)->remove_resource ("iccprofile", SP_OBJECT (object));
+    if ( object->document ) {
+        object->document->removeResource("iccprofile", object);
     }
 
     ColorProfile *cprof = COLORPROFILE(object);
@@ -254,7 +255,7 @@ void ColorProfile::build( SPObject *object, SPDocument *document, Inkscape::XML:
 
     // Register
     if ( document ) {
-        document->add_resource( "iccprofile", object );
+        document->addResource( "iccprofile", object );
     }
 }
 
@@ -288,7 +289,7 @@ void ColorProfile::set( SPObject *object, unsigned key, gchar const *value )
                         g_warning("object has no document.  using active");
                     }
                     //# 1.  Get complete URI of document
-                    gchar const *docbase = SP_DOCUMENT_URI( doc );
+                    gchar const *docbase = doc->getURI();
                     if (!docbase)
                     {
                         // Normal for files that have not yet been saved.
@@ -475,7 +476,7 @@ static int getLcmsIntent( guint svgIntent )
 static SPObject* bruteFind( SPDocument* document, gchar const* name )
 {
     SPObject* result = 0;
-    const GSList * current = document->get_resource_list("iccprofile");
+    const GSList * current = document->getResourceList("iccprofile");
     while ( current && !result ) {
         if ( IS_COLORPROFILE(current->data) ) {
             ColorProfile* prof = COLORPROFILE(current->data);
@@ -556,9 +557,6 @@ bool ColorProfile::GamutCheck(SPColor color){
     return (outofgamut == 255);
 }
 
-
-#include <io/sys.h>
-
 class ProfileInfo
 {
 public:
@@ -630,7 +628,14 @@ Glib::ustring Inkscape::get_path_for_profile(Glib::ustring const& name)
 }
 #endif // ENABLE_LCMS
 
-std::list<Glib::ustring> ColorProfile::getProfileDirs() {
+std::list<Glib::ustring> ColorProfile::getBaseProfileDirs() {
+#if ENABLE_LCMS
+    static bool warnSet = false;
+    if (!warnSet) {
+        cmsErrorAction( LCMS_ERROR_SHOW );
+        warnSet = true;
+    }
+#endif // ENABLE_LCMS
     std::list<Glib::ustring> sources;
 
     gchar* base = profile_path("XXX");
@@ -655,17 +660,26 @@ std::list<Glib::ustring> ColorProfile::getProfileDirs() {
     }
 
     // On OS X:
-    if ( g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_EXISTS)  && g_file_test("/Library/ColorSync/Profiles", G_FILE_TEST_IS_DIR) ) {
-        sources.push_back("/Library/ColorSync/Profiles");
-
-        gchar* path = g_build_filename(g_get_home_dir(), "Library", "ColorSync", "Profiles", NULL);
-        if ( g_file_test(path, G_FILE_TEST_EXISTS)  && g_file_test(path, G_FILE_TEST_IS_DIR) ) {
-            sources.push_back(path);
+    {
+        bool onOSX = false;
+        std::list<Glib::ustring> possible;
+        possible.push_back("/System/Library/ColorSync/Profiles");
+        possible.push_back("/Library/ColorSync/Profiles");
+        for ( std::list<Glib::ustring>::const_iterator it = possible.begin(); it != possible.end(); ++it ) {
+            if ( g_file_test(it->c_str(), G_FILE_TEST_EXISTS)  && g_file_test(it->c_str(), G_FILE_TEST_IS_DIR) ) {
+                sources.push_back(it->c_str());
+                onOSX = true;
+            }
+        }
+        if ( onOSX ) {
+            gchar* path = g_build_filename(g_get_home_dir(), "Library", "ColorSync", "Profiles", NULL);
+            if ( g_file_test(path, G_FILE_TEST_EXISTS)  && g_file_test(path, G_FILE_TEST_IS_DIR) ) {
+                sources.push_back(path);
+            }
+            g_free(path);
         }
-        g_free(path);
     }
 
-
 #ifdef WIN32
     wchar_t pathBuf[MAX_PATH + 1];
     pathBuf[0] = 0;
@@ -685,10 +699,50 @@ std::list<Glib::ustring> ColorProfile::getProfileDirs() {
     return sources;
 }
 
+static bool isIccFile( gchar const *filepath )
+{
+    bool isIccFile = false;
+    struct stat st;
+    if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
+        //0-3 == size
+        //36-39 == 'acsp' 0x61637370
+        int fd = g_open( filepath, O_RDONLY, S_IRWXU);
+        if ( fd != -1 ) {
+            guchar scratch[40] = {0};
+            size_t len = sizeof(scratch);
+
+            //size_t left = 40;
+            ssize_t got = read(fd, scratch, len);
+            if ( got != -1 ) {
+                size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
+                if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
+                    isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
+                }
+            }
+
+            close(fd);
 #if ENABLE_LCMS
-static void findThings() {
-    std::list<Glib::ustring> sources = ColorProfile::getProfileDirs();
+            if (isIccFile) {
+                cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
+                if ( prof ) {
+                    icProfileClassSignature profClass = cmsGetDeviceClass(prof);
+                    if ( profClass == icSigNamedColorClass ) {
+                        isIccFile = false; // Ignore named color profiles for now.
+                    }
+                    cmsCloseProfile( prof );
+                }
+            }
+#endif // ENABLE_LCMS
+        }
+    }
+    return isIccFile;
+}
+
+std::list<Glib::ustring> ColorProfile::getProfileFiles()
+{
+    std::list<Glib::ustring> files;
 
+    std::list<Glib::ustring> sources = ColorProfile::getBaseProfileDirs();
     for ( std::list<Glib::ustring>::const_iterator it = sources.begin(); it != sources.end(); ++it ) {
         if ( g_file_test( it->c_str(), G_FILE_TEST_EXISTS ) && g_file_test( it->c_str(), G_FILE_TEST_IS_DIR ) ) {
             GError *err = 0;
@@ -697,57 +751,49 @@ static void findThings() {
             if (dir) {
                 for (gchar const *file = g_dir_read_name(dir); file != NULL; file = g_dir_read_name(dir)) {
                     gchar *filepath = g_build_filename(it->c_str(), file, NULL);
-
-
                     if ( g_file_test( filepath, G_FILE_TEST_IS_DIR ) ) {
                         sources.push_back(g_strdup(filepath));
                     } else {
-                        bool isIccFile = false;
-                        struct stat st;
-                        if ( g_stat(filepath, &st) == 0 && (st.st_size > 128) ) {
-                            //0-3 == size
-                            //36-39 == 'acsp' 0x61637370
-                            int fd = g_open( filepath, O_RDONLY, S_IRWXU);
-                            if ( fd != -1 ) {
-                                guchar scratch[40] = {0};
-                                size_t len = sizeof(scratch);
-
-                                //size_t left = 40;
-                                ssize_t got = read(fd, scratch, len);
-                                if ( got != -1 ) {
-                                    size_t calcSize = (scratch[0] << 24) | (scratch[1] << 16) | (scratch[2] << 8) | scratch[3];
-                                    if ( calcSize > 128 && calcSize <= static_cast<size_t>(st.st_size) ) {
-                                        isIccFile = (scratch[36] == 'a') && (scratch[37] == 'c') && (scratch[38] == 's') && (scratch[39] == 'p');
-                                    }
-                                }
-
-                                close(fd);
-                            }
-                        }
-
-                        if ( isIccFile ) {
-                            cmsHPROFILE prof = cmsOpenProfileFromFile( filepath, "r" );
-                            if ( prof ) {
-                                ProfileInfo info( prof, Glib::filename_to_utf8( filepath ) );
-                                cmsCloseProfile( prof );
-
-                                bool sameName = false;
-                                for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
-                                    if ( it->getName() == info.getName() ) {
-                                        sameName = true;
-                                        break;
-                                    }
-                                }
-
-                                if ( !sameName ) {
-                                    knownProfiles.push_back(info);
-                                }
-                            }
+                        if ( isIccFile( filepath ) ) {
+                            files.push_back( filepath );
                         }
                     }
 
                     g_free(filepath);
                 }
+                g_dir_close(dir);
+                dir = 0;
+            } else {
+                gchar *safeDir = Inkscape::IO::sanitizeString(it->c_str());
+                g_warning(_("Color profiles directory (%s) is unavailable."), safeDir);
+                g_free(safeDir);
+            }
+        }
+    }
+
+    return files;
+}
+
+#if ENABLE_LCMS
+static void findThings() {
+    std::list<Glib::ustring> files = ColorProfile::getProfileFiles();
+
+    for ( std::list<Glib::ustring>::const_iterator it = files.begin(); it != files.end(); ++it ) {
+        cmsHPROFILE prof = cmsOpenProfileFromFile( it->c_str(), "r" );
+        if ( prof ) {
+            ProfileInfo info( prof, Glib::filename_to_utf8( it->c_str() ) );
+            cmsCloseProfile( prof );
+
+            bool sameName = false;
+            for ( std::vector<ProfileInfo>::iterator it = knownProfiles.begin(); it != knownProfiles.end(); ++it ) {
+                if ( it->getName() == info.getName() ) {
+                    sameName = true;
+                    break;
+                }
+            }
+
+            if ( !sameName ) {
+                knownProfiles.push_back(info);
             }
         }
     }
@@ -1145,4 +1191,4 @@ cmsHTRANSFORM Inkscape::colorprofile_get_display_per( Glib::ustring const& id )
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :