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

Generated on 27 Jul 2016 for Hugintrunk by  doxygen 1.4.7