platform.cpp

Go to the documentation of this file.
00001 // -*- c-basic-offset: 4 -*-
00002 
00027 #include "platform.h"
00028 
00029 #include <hugin_utils/utils.h>
00030 
00031 #if defined __WXMAC__ || defined __WXOSX_COCOA__
00032 
00033 #include <CoreFoundation/CFBundle.h>
00034 #if wxCHECK_VERSION(2,9,0)
00035  #include "wx/osx/core/cfstring.h"
00036 #endif
00037 #include <iostream>
00038 #include <stdio.h>
00039 #include "wx/utils.h"
00040 
00041 using namespace std;
00042 
00043 // note this is a "create" function for ownership
00044 CFStringRef MacCreateCFStringWithWxString(const wxString& string)
00045 {
00046     return CFStringCreateWithCString(NULL,
00047                                      (const char*)string.mb_str(wxConvUTF8),
00048                                      kCFStringEncodingUTF8);
00049     
00050 }
00051 
00052 wxString MacGetPathToMainExecutableFileOfBundle(CFStringRef bundlePath)
00053 {
00054     wxString theResult = wxT("");
00055 
00056     CFURLRef bundleURL = CFURLCreateWithFileSystemPath(NULL, bundlePath, kCFURLPOSIXPathStyle, TRUE);
00057 
00058     if(bundleURL == NULL)
00059     {
00060         DEBUG_INFO("Mac: CFURL from string (" << bundlePath << ") failed." );
00061         return theResult;
00062     }
00063     
00064     CFBundleRef bundle = CFBundleCreate(NULL, bundleURL);
00065     CFRelease(bundleURL);
00066     
00067     if(bundle == NULL)
00068     {
00069         DEBUG_INFO("Mac: CFBundleCreate (" << bundlePath << " ) failed" );
00070     }
00071     else
00072     {
00073         CFURLRef PTOurl = CFBundleCopyExecutableURL(bundle);
00074         CFRelease( bundle );
00075         if(PTOurl == NULL)
00076         {
00077             DEBUG_INFO("Mac: Cannot locate the executable in the bundle.");
00078         }
00079         else
00080         {
00081             CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
00082             CFRelease( PTOurl );
00083             if(PTOAbsURL == NULL)
00084             {
00085                 DEBUG_INFO("Mac: Cannot convert the file path to absolute");
00086             }
00087             else
00088             {
00089                 CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
00090                 CFRelease( PTOAbsURL );
00091                 if(pathInCFString == NULL)
00092                 {
00093                     DEBUG_INFO("Mac: Failed to get URL in CFString");
00094                 }
00095                 else
00096                 {
00097                     CFRetain( pathInCFString );
00098                     #if wxCHECK_VERSION(2,9,0)
00099                       theResult =  wxCFStringRef(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00100                     #else
00101                       theResult =  wxMacCFStringHolder(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00102                     #endif
00103                     DEBUG_INFO("Mac: the executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
00104                 }
00105             }
00106         }
00107     }
00108     return theResult;
00109 }
00110 
00111 wxString MacGetPathToMainExecutableFileOfRegisteredBundle(CFStringRef BundleIdentifier)
00112 {
00113     wxString theResult = wxT("");
00114         
00115         FSRef appRef;
00116         CFURLRef bundleURL;
00117         FSRef actuallyLaunched;
00118         OSStatus err;
00119         FSRef documentArray[1]; // Don't really need an array if we only have 1 item
00120         LSLaunchFSRefSpec launchSpec;
00121         //Boolean  isDir;
00122         
00123         err = LSFindApplicationForInfo(kLSUnknownCreator,
00124                                                                    CFSTR("net.sourceforge.hugin.PTBatcherGUI"),
00125                                                                    NULL,
00126                                                                    &appRef,
00127                                                                    &bundleURL);
00128         if (err != noErr) {
00129                 // error, can't find PTBatcherGUI
00130                 cout << "PTBatcherGui check failed \n" << endl;
00131                 wxMessageBox(wxString::Format(_("External program %s not found in the bundle, reverting to system path"), wxT("open")), _("Error"));
00132         }
00133     if(bundleURL == NULL)
00134     {
00135         DEBUG_INFO("Mac: CFURL from string (" << bundleURL << ") failed." );
00136         return theResult;
00137     }
00138     
00139     CFBundleRef bundle = CFBundleCreate(NULL, bundleURL);
00140     CFRelease(bundleURL);
00141     
00142     if(bundle == NULL)
00143     {
00144         DEBUG_INFO("Mac: CFBundleCreate (" << bundleURL << " ) failed" );
00145     }
00146     else
00147     {
00148         CFURLRef PTOurl = CFBundleCopyExecutableURL(bundle);
00149         CFRelease( bundle );
00150         if(PTOurl == NULL)
00151         {
00152             DEBUG_INFO("Mac: Cannot locate the executable in the bundle.");
00153         }
00154         else
00155         {
00156             CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
00157             CFRelease( PTOurl );
00158             if(PTOAbsURL == NULL)
00159             {
00160                 DEBUG_INFO("Mac: Cannot convert the file path to absolute");
00161             }
00162             else
00163             {
00164                 CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
00165                 CFRelease( PTOAbsURL );
00166                 if(pathInCFString == NULL)
00167                 {
00168                     DEBUG_INFO("Mac: Failed to get URL in CFString");
00169                 }
00170                 else
00171                 {
00172                     CFRetain( pathInCFString );
00173                     #if wxCHECK_VERSION(2,9,0)
00174                       theResult =  wxCFStringRef(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00175                     #else
00176                       theResult =  wxMacCFStringHolder(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00177                     #endif
00178                     DEBUG_INFO("Mac: the executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
00179                 }
00180             }
00181         }
00182     }
00183         cout << "PTBatcherGui check returned  value " << theResult << "\n" << endl;
00184     return theResult;
00185 }
00186 
00187 #if defined MAC_SELF_CONTAINED_BUNDLE
00188 
00189 wxString MacGetPathToBundledAppMainExecutableFile(CFStringRef appname)
00190 {
00191     wxString theResult = wxT("");
00192 
00193     CFBundleRef mainbundle = CFBundleGetMainBundle();
00194     if(mainbundle == NULL)
00195     {
00196         DEBUG_INFO("Mac: Not bundled");
00197     }
00198     else
00199     {
00200         CFURLRef XRCurl = CFBundleCopyResourceURL(mainbundle, appname, NULL, NULL);
00201         if(XRCurl == NULL)
00202         {
00203             DEBUG_INFO("Mac: Cannot locate the bundle in bundle.");
00204         }
00205         else
00206         {
00207             CFBundleRef bundledBundle = CFBundleCreate(NULL, XRCurl);
00208             CFRelease( XRCurl );
00209             
00210             if(bundledBundle == NULL)
00211             {
00212                 DEBUG_INFO("Mac: Not bundled");
00213             }
00214             else
00215             {
00216                 CFURLRef PTOurl = CFBundleCopyExecutableURL(bundledBundle);
00217                 CFRelease( bundledBundle );
00218                 
00219                 if(PTOurl == NULL)
00220                 {
00221                     DEBUG_INFO("Mac: Cannot locate the executable in the bundle.");
00222                 }
00223                 else
00224                 {
00225                     CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
00226                     CFRelease( PTOurl );
00227                     if(PTOAbsURL == NULL)
00228                     {
00229                         DEBUG_INFO("Mac: Cannot convert the file path to absolute");
00230                     }
00231                     else
00232                     {
00233                         CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
00234                         CFRelease( PTOAbsURL );
00235                         if(pathInCFString == NULL)
00236                         {
00237                             DEBUG_INFO("Mac: Failed to get URL in CFString");
00238                         }
00239                         else
00240                         {
00241                             CFRetain( pathInCFString );
00242                             #if wxCHECK_VERSION(2,9,0)
00243                               theResult =  wxCFStringRef(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00244                             #else
00245                               theResult =  wxMacCFStringHolder(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00246                             #endif
00247                             DEBUG_INFO("Mac: the executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
00248                         }
00249                     }
00250                 }
00251             }
00252         }
00253     }
00254     return theResult;
00255 }
00256 
00257 wxString MacGetPathToBundledResourceFile(CFStringRef filename)
00258 {
00259     wxString theResult = wxT("");
00260 
00261     CFBundleRef mainbundle = CFBundleGetMainBundle();
00262     if(mainbundle == NULL)
00263     {
00264         DEBUG_INFO("Mac: Not bundled");
00265     }
00266     else
00267     {
00268         CFURLRef XRCurl = CFBundleCopyResourceURL(mainbundle, filename, NULL, NULL);
00269         if(XRCurl == NULL)
00270         {
00271             DEBUG_INFO("Mac: Cannot locate the file in bundle.");
00272         }
00273         else
00274         {
00275             CFStringRef pathInCFString = CFURLCopyFileSystemPath(XRCurl, kCFURLPOSIXPathStyle);
00276             CFRelease( XRCurl );
00277             if(pathInCFString == NULL)
00278             {
00279                 DEBUG_INFO("Mac: Failed to get URL in CFString");
00280             }
00281             else
00282             {
00283                 CFRetain( pathInCFString );
00284                 #if wxCHECK_VERSION(2,9,0)
00285                   theResult = wxCFStringRef(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00286                 #else
00287                   theResult = wxMacCFStringHolder(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00288                 #endif
00289                 DEBUG_INFO("Mac: the resource file's path in the application bundle: " << theResult.mb_str(wxConvLocal));
00290             }
00291         }
00292     }
00293     return theResult;
00294 }
00295 
00296 wxString MacGetPathToBundledFrameworksDirectory()
00297 {
00298     wxString theResult = wxT("");
00299     
00300     CFBundleRef mainbundle = CFBundleGetMainBundle();
00301     if(mainbundle == NULL)
00302     {
00303         DEBUG_INFO("Mac: Not bundled");
00304     }
00305     else
00306     {
00307         CFURLRef XRCurl = CFBundleCopyBundleURL(mainbundle);
00308         if(XRCurl == NULL)
00309         {
00310             DEBUG_INFO("Mac: Cannot locate the file in bundle.");
00311         }
00312         else
00313         {
00314             CFStringRef pathInCFString = CFURLCopyFileSystemPath(XRCurl, kCFURLPOSIXPathStyle);
00315             CFRelease( XRCurl );
00316             if(pathInCFString == NULL)
00317             {
00318                 DEBUG_INFO("Mac: Failed to get URL in CFString");
00319             }
00320             else
00321             {
00322                 CFRetain( pathInCFString );
00323 #if wxCHECK_VERSION(2,9,0)
00324                 theResult = wxCFStringRef(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00325 #else
00326                 theResult = wxMacCFStringHolder(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00327 #endif
00328                 DEBUG_INFO("Mac: the Frameworks file's path in the application bundle: " << theResult.mb_str(wxConvLocal));
00329             }
00330         }
00331     }
00332     return theResult + wxT("/Contents/Frameworks");
00333 }
00334 
00335 wxString MacGetPathToBundledExecutableFile(CFStringRef filename)
00336 {
00337     wxString theResult = wxT("");
00338 
00339     CFBundleRef mainbundle = CFBundleGetMainBundle();
00340     if(mainbundle == NULL)
00341     {
00342         DEBUG_INFO("Mac: Not bundled");
00343     }
00344     else
00345     {
00346         CFURLRef PTOurl = CFBundleCopyAuxiliaryExecutableURL(mainbundle, filename);
00347         if(PTOurl == NULL)
00348         {
00349             DEBUG_INFO("Mac: Cannot locate the file in the bundle.");
00350         }
00351         else
00352         {
00353             CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
00354             if(PTOAbsURL == NULL)
00355             {
00356                 DEBUG_INFO("Mac: Cannot convert the file path to absolute");
00357             }
00358             else
00359             {
00360                 CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
00361                 CFRelease( PTOAbsURL );
00362                 if(pathInCFString == NULL)
00363                 {
00364                     DEBUG_INFO("Mac: Failed to get URL in CFString");
00365                 }
00366                 else
00367                 {
00368                     CFRetain( pathInCFString );
00369                     #if wxCHECK_VERSION(2,9,0)
00370                       theResult =  wxCFStringRef(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00371                     #else
00372                       theResult =  wxMacCFStringHolder(pathInCFString).AsString(wxLocale::GetSystemEncoding());
00373                     #endif
00374                     DEBUG_INFO("Mac: executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
00375                 }
00376             }
00377             CFRelease( PTOurl );
00378         }
00379     }
00380     return theResult;
00381 }
00382 
00383 wxString MacGetPathToUserDomainTempDir()
00384 {
00385     wxString tmpDirPath = wxT("");
00386     
00387     FSRef tempDirRef;
00388     OSErr err = FSFindFolder(kUserDomain, kTemporaryFolderType, kCreateFolder, &tempDirRef);
00389     if (err == noErr)
00390     {
00391         CFURLRef tempDirURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &tempDirRef);
00392         if (tempDirURL != NULL)
00393         {
00394             CFStringRef tmpPath = CFURLCopyFileSystemPath(tempDirURL, kCFURLPOSIXPathStyle);
00395             CFRetain(tmpPath);
00396             #if wxCHECK_VERSION(2,9,0)
00397               tmpDirPath = wxCFStringRef(tmpPath).AsString(wxLocale::GetSystemEncoding());
00398             #else
00399               tmpDirPath = wxMacCFStringHolder(tmpPath).AsString(wxLocale::GetSystemEncoding());
00400             #endif
00401             
00402             CFRelease(tempDirURL);
00403         }
00404     }
00405     
00406     return tmpDirPath;
00407 }
00408 
00409 wxString MacGetPathToUserAppSupportAutoPanoFolder()
00410 {
00411     wxString appSupportAutoPanoFolder = wxT("");
00412 
00413     FSRef appSupportFolder;
00414     OSErr err = FSFindFolder(kUserDomain,kApplicationSupportFolderType,kDontCreateFolder,&appSupportFolder);
00415     if( err == noErr)
00416     {
00417         CFURLRef appSupportFolderURL = CFURLCreateFromFSRef(kCFAllocatorDefault,&appSupportFolder);
00418         CFURLRef appSupportHugin = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,appSupportFolderURL,CFSTR("Hugin"),true);
00419         CFURLRef autopanoURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,appSupportHugin,CFSTR("Autopano"),true);
00420         CFStringRef tmpPath = CFURLCopyFileSystemPath(autopanoURL,  kCFURLPOSIXPathStyle);
00421         CFRetain(tmpPath);
00422         #if wxCHECK_VERSION(2,9,0)
00423           appSupportAutoPanoFolder = wxCFStringRef(tmpPath).AsString(wxLocale::GetSystemEncoding());
00424         #else
00425           appSupportAutoPanoFolder = wxMacCFStringHolder(tmpPath).AsString(wxLocale::GetSystemEncoding());
00426         #endif
00427 
00428         CFRelease(autopanoURL);
00429     }
00430     return appSupportAutoPanoFolder;
00431 }
00432 
00433 
00434 #endif // MAC_SELF_CONTAINED_BUNDLE
00435 
00436 #endif // __WXMAC__
00437 
00438 const wxString getInvalidCharacters()
00439 {
00440 #if defined __WXMSW__
00441     // the characters :"*?<>| are not allowed in filenames, these are handled well by the file dialog
00442     // we need only to check for characters, which does not work with the makefiles
00443     return wxT("=;%");
00444 #else
00445     // the characters =;:% does not work with the makefile
00446     // we are also rejecting the characters <>*?| which are principally allowed in filenames but will probably make problems when used
00447     // the double quote does not work with the panotools file format, so also reject
00448     //@BUG tilde ~ and backslash \ are not working with vigraimpex, if this works again these characters can be removed from the list
00449     return wxT("=;:%*?<>|\"\\~");
00450 #endif
00451 };
00452 
00453 bool containsInvalidCharacters(const wxString stringToTest)
00454 {
00455     if(stringToTest.IsEmpty())
00456         return false;
00457     wxString forbiddenChars=getInvalidCharacters();
00458     for(unsigned int j=0;j<forbiddenChars.size();j++)
00459     {
00460         if(stringToTest.Find(forbiddenChars[j])!=wxNOT_FOUND)
00461             return true;
00462     };
00463     return false;
00464 };
00465 
00466 void ShowFilenameWarning(wxWindow* parent, const wxArrayString filelist)
00467 {
00468     wxDialog dlg;
00469     wxXmlResource::Get()->LoadDialog(&dlg, parent, wxT("dlg_warning_filename"));
00470     XRCCTRL(dlg, "dlg_warning_text", wxStaticText)->SetLabel(wxString::Format(_("The filename(s) contains one of the following invalid characters: %s\nHugin can not work with these filenames. Please rename your file(s) and try again."), getInvalidCharacters().c_str()));
00471     XRCCTRL(dlg, "dlg_warning_list", wxListBox)->Append(filelist);
00472     dlg.Fit();
00473     dlg.CenterOnScreen();
00474     dlg.ShowModal();
00475 };

Generated on Wed Sep 24 01:25:31 2014 for Hugintrunk by  doxygen 1.3.9.1