Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
platform.cpp
Go to the documentation of this file.
1 // -*- c-basic-offset: 4 -*-
2 
27 #include "platform.h"
28 
29 #include <hugin_utils/utils.h>
30 #include "wxutils.h"
31 #include <vigra/imageinfo.hxx>
32 
34 wxString GetFilterExtensions(const wxString& ext)
35 {
36  wxString extensionString("*.");
37  extensionString.Append(ext);
38  if (wxFileName::IsCaseSensitive())
39  {
40  extensionString.Append(";*.").Append(ext.Upper());
41  };
42  return extensionString;
43 };
44 
46 {
47  wxString filterString;
48  const std::string extensions = vigra::impexListExtensions();
49  std::vector<std::string> exts = hugin_utils::SplitString(extensions, " ");
50  if (exts.empty())
51  {
52  // if something goes wrong, add some defaults
53  exts.push_back("tif");
54  exts.push_back("tiff");
55  exts.push_back("jpg");
56  exts.push_back("jpeg");
57  exts.push_back("png");
58  }
59  filterString.Append(_("All Image files")).Append("|");
60  for (auto& ext : exts)
61  {
62  filterString.Append(GetFilterExtensions(ext)).Append(";");
63  };
64  // remove last character == ";"
65  filterString.RemoveLast();
66  filterString.Append("|");
67  return filterString;
68 };
69 
71 {
72  wxString filterString=GetVigraImageFilter();
73  // append JPG/TIFF/PNG
74  filterString.Append(GetMainImageFilters());
75  filterString.Append("|").Append(_("HDR files (*.hdr)")).Append("|").Append(GetFilterExtensions("hdr"));
76  filterString.Append("|").Append(_("EXR files (*.exr)")).Append("|").Append(GetFilterExtensions("exr"));
77  filterString.Append("|").Append(_("All files (*)")).Append("|*");
78  return filterString;
79 }
80 
82 {
83  wxString filterString;
84  filterString.Append(_("JPEG files (*.jpg,*.jpeg)")).Append("|").Append(GetFilterExtensions("jpg")).Append(";").Append(GetFilterExtensions("jpeg"));
85  filterString.Append("|").Append(_("TIFF files (*.tif,*.tiff)")).Append("|").Append(GetFilterExtensions("tif")).Append(";").Append(GetFilterExtensions("tiff"));
86  filterString.Append("|").Append(_("PNG files (*.png)")).Append("|").Append(GetFilterExtensions("png"));
87  return filterString;
88 }
89 
91 {
92  wxString filterString = GetVigraImageFilter();
93  // now the raw formats
94  const std::vector<std::string> exts = hugin_utils::GetRawExtensions();
95  filterString.Append(_("Raw files")).Append("|");
96  for (auto& ext : exts)
97  {
98  filterString.Append(GetFilterExtensions(ext)).Append(";");
99  };
100  // remove last character == ";"
101  filterString.RemoveLast();
102  filterString.Append("|");
103  // now add some image format at its own
104  filterString.Append(_("JPEG files (*.jpg,*.jpeg)")).Append("|").Append(GetFilterExtensions("jpg")).Append(";").Append(GetFilterExtensions("jpeg"));
105  filterString.Append("|").Append(_("TIFF files (*.tif,*.tiff)")).Append("|").Append(GetFilterExtensions("tif")).Append(";").Append(GetFilterExtensions("tiff"));
106  filterString.Append("|").Append(_("PNG files (*.png)")).Append("|").Append(GetFilterExtensions("png"));
107  filterString.Append("|").Append(_("HDR files (*.hdr)")).Append("|").Append(GetFilterExtensions("hdr"));
108  filterString.Append("|").Append(_("EXR files (*.exr)")).Append("|").Append(GetFilterExtensions("exr"));
109  return filterString;
110 };
111 
112 bool IsRawExtension(const wxString& testExt)
113 {
114  const std::vector<std::string> rawExts = hugin_utils::GetRawExtensions();
115  for (auto& ext : rawExts)
116  {
117  if (testExt.CmpNoCase(wxString(ext.c_str(), wxConvLocal)) == 0)
118  {
119  return true;
120  };
121  };
122  return false;
123 };
124 
125 #if defined __WXMAC__ || defined __WXOSX_COCOA__
126 
127 #include <CoreFoundation/CFBundle.h>
128  #include "wx/osx/core/cfstring.h"
129 #include <iostream>
130 #include <stdio.h>
131 #include "wx/utils.h"
132 
133 using namespace std;
134 
135 // note this is a "create" function for ownership
136 CFStringRef MacCreateCFStringWithWxString(const wxString& string)
137 {
138  return CFStringCreateWithCString(NULL,
139  (const char*)string.mb_str(wxConvUTF8),
140  kCFStringEncodingUTF8);
141 
142 }
143 
144 wxString MacGetPathToMainExecutableFileOfBundle(CFStringRef bundlePath)
145 {
146  wxString theResult = wxEmptyString;
147 
148  CFURLRef bundleURL = CFURLCreateWithFileSystemPath(NULL, bundlePath, kCFURLPOSIXPathStyle, TRUE);
149 
150  if(bundleURL == NULL)
151  {
152  DEBUG_INFO("Mac: CFURL from string (" << bundlePath << ") failed." );
153  return theResult;
154  }
155 
156  CFBundleRef bundle = CFBundleCreate(NULL, bundleURL);
157  CFRelease(bundleURL);
158 
159  if(bundle == NULL)
160  {
161  DEBUG_INFO("Mac: CFBundleCreate (" << bundlePath << " ) failed" );
162  }
163  else
164  {
165  CFURLRef PTOurl = CFBundleCopyExecutableURL(bundle);
166  CFRelease( bundle );
167  if(PTOurl == NULL)
168  {
169  DEBUG_INFO("Mac: Cannot locate the executable in the bundle.");
170  }
171  else
172  {
173  CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
174  CFRelease( PTOurl );
175  if(PTOAbsURL == NULL)
176  {
177  DEBUG_INFO("Mac: Cannot convert the file path to absolute");
178  }
179  else
180  {
181  CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
182  CFRelease( PTOAbsURL );
183  if(pathInCFString == NULL)
184  {
185  DEBUG_INFO("Mac: Failed to get URL in CFString");
186  }
187  else
188  {
189  CFRetain( pathInCFString );
190  theResult = wxCFStringRef(pathInCFString).AsString();
191  DEBUG_INFO("Mac: the executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
192  }
193  }
194  }
195  }
196  return theResult;
197 }
198 
199 wxString MacGetPathToMainExecutableFileOfRegisteredBundle(CFStringRef BundleIdentifier)
200 {
201  wxString theResult = wxEmptyString;
202 
203  FSRef appRef;
204  CFURLRef bundleURL;
205  FSRef actuallyLaunched;
206  OSStatus err;
207  FSRef documentArray[1]; // Don't really need an array if we only have 1 item
208  LSLaunchFSRefSpec launchSpec;
209  //Boolean isDir;
210 
211  err = LSFindApplicationForInfo(kLSUnknownCreator,
212  CFSTR("net.sourceforge.hugin.PTBatcherGUI"),
213  NULL,
214  &appRef,
215  &bundleURL);
216  if (err != noErr) {
217  // error, can't find PTBatcherGUI
218  cout << "PTBatcherGui check failed \n" << endl;
219  hugin_utils::HuginMessageBox(wxString::Format(_("External program %s not found in the bundle, reverting to system path"), "open"), _("Hugin"), wxOK, wxGetActiveWindow());
220  }
221  if(bundleURL == NULL)
222  {
223  DEBUG_INFO("Mac: CFURL from string (" << bundleURL << ") failed." );
224  return theResult;
225  }
226 
227  CFBundleRef bundle = CFBundleCreate(NULL, bundleURL);
228  CFRelease(bundleURL);
229 
230  if(bundle == NULL)
231  {
232  DEBUG_INFO("Mac: CFBundleCreate (" << bundleURL << " ) failed" );
233  }
234  else
235  {
236  CFURLRef PTOurl = CFBundleCopyExecutableURL(bundle);
237  CFRelease( bundle );
238  if(PTOurl == NULL)
239  {
240  DEBUG_INFO("Mac: Cannot locate the executable in the bundle.");
241  }
242  else
243  {
244  CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
245  CFRelease( PTOurl );
246  if(PTOAbsURL == NULL)
247  {
248  DEBUG_INFO("Mac: Cannot convert the file path to absolute");
249  }
250  else
251  {
252  CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
253  CFRelease( PTOAbsURL );
254  if(pathInCFString == NULL)
255  {
256  DEBUG_INFO("Mac: Failed to get URL in CFString");
257  }
258  else
259  {
260  CFRetain( pathInCFString );
261  theResult = wxCFStringRef(pathInCFString).AsString();
262  DEBUG_INFO("Mac: the executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
263  }
264  }
265  }
266  }
267  cout << "PTBatcherGui check returned value " << theResult << "\n" << endl;
268  return theResult;
269 }
270 
271 #if defined MAC_SELF_CONTAINED_BUNDLE
272 
273 wxString MacGetPathToBundledAppMainExecutableFile(CFStringRef appname)
274 {
275  wxString theResult = wxEmptyString;
276 
277  CFBundleRef mainbundle = CFBundleGetMainBundle();
278  if(mainbundle == NULL)
279  {
280  DEBUG_INFO("Mac: Not bundled");
281  }
282  else
283  {
284  CFURLRef XRCurl = CFBundleCopyResourceURL(mainbundle, appname, NULL, NULL);
285  if(XRCurl == NULL)
286  {
287  DEBUG_INFO("Mac: Cannot locate the bundle in bundle.");
288  }
289  else
290  {
291  CFBundleRef bundledBundle = CFBundleCreate(NULL, XRCurl);
292  CFRelease( XRCurl );
293 
294  if(bundledBundle == NULL)
295  {
296  DEBUG_INFO("Mac: Not bundled");
297  }
298  else
299  {
300  CFURLRef PTOurl = CFBundleCopyExecutableURL(bundledBundle);
301  CFRelease( bundledBundle );
302 
303  if(PTOurl == NULL)
304  {
305  DEBUG_INFO("Mac: Cannot locate the executable in the bundle.");
306  }
307  else
308  {
309  CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
310  CFRelease( PTOurl );
311  if(PTOAbsURL == NULL)
312  {
313  DEBUG_INFO("Mac: Cannot convert the file path to absolute");
314  }
315  else
316  {
317  CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
318  CFRelease( PTOAbsURL );
319  if(pathInCFString == NULL)
320  {
321  DEBUG_INFO("Mac: Failed to get URL in CFString");
322  }
323  else
324  {
325  CFRetain( pathInCFString );
326  theResult = wxCFStringRef(pathInCFString).AsString();
327  DEBUG_INFO("Mac: the executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
328  }
329  }
330  }
331  }
332  }
333  }
334  return theResult;
335 }
336 
337 wxString MacGetPathToBundledResourceFile(CFStringRef filename)
338 {
339  wxString theResult = wxEmptyString;
340 
341  CFBundleRef mainbundle = CFBundleGetMainBundle();
342  if(mainbundle == NULL)
343  {
344  DEBUG_INFO("Mac: Not bundled");
345  }
346  else
347  {
348  CFURLRef XRCurl = CFBundleCopyResourceURL(mainbundle, filename, NULL, NULL);
349  if(XRCurl == NULL)
350  {
351  DEBUG_INFO("Mac: Cannot locate the file in bundle.");
352  }
353  else
354  {
355  CFStringRef pathInCFString = CFURLCopyFileSystemPath(XRCurl, kCFURLPOSIXPathStyle);
356  CFRelease( XRCurl );
357  if(pathInCFString == NULL)
358  {
359  DEBUG_INFO("Mac: Failed to get URL in CFString");
360  }
361  else
362  {
363  CFRetain( pathInCFString );
364  theResult = wxCFStringRef(pathInCFString).AsString();
365  DEBUG_INFO("Mac: the resource file's path in the application bundle: " << theResult.mb_str(wxConvLocal));
366  }
367  }
368  }
369  return theResult;
370 }
371 
372 wxString MacGetPathToBundledFrameworksDirectory()
373 {
374  wxString theResult = wxEmptyString;
375 
376  CFBundleRef mainbundle = CFBundleGetMainBundle();
377  if(mainbundle == NULL)
378  {
379  DEBUG_INFO("Mac: Not bundled");
380  }
381  else
382  {
383  CFURLRef XRCurl = CFBundleCopyBundleURL(mainbundle);
384  if(XRCurl == NULL)
385  {
386  DEBUG_INFO("Mac: Cannot locate the file in bundle.");
387  }
388  else
389  {
390  CFStringRef pathInCFString = CFURLCopyFileSystemPath(XRCurl, kCFURLPOSIXPathStyle);
391  CFRelease( XRCurl );
392  if(pathInCFString == NULL)
393  {
394  DEBUG_INFO("Mac: Failed to get URL in CFString");
395  }
396  else
397  {
398  CFRetain( pathInCFString );
399  theResult = wxCFStringRef(pathInCFString).AsString();
400  DEBUG_INFO("Mac: the Frameworks file's path in the application bundle: " << theResult.mb_str(wxConvLocal));
401  }
402  }
403  }
404  return theResult + "/Contents/Frameworks";
405 }
406 
407 wxString MacGetPathToBundledExecutableFile(CFStringRef filename)
408 {
409  wxString theResult = wxEmptyString;
410 
411  CFBundleRef mainbundle = CFBundleGetMainBundle();
412  if(mainbundle == NULL)
413  {
414  DEBUG_INFO("Mac: Not bundled");
415  }
416  else
417  {
418  CFURLRef PTOurl = CFBundleCopyAuxiliaryExecutableURL(mainbundle, filename);
419  if(PTOurl == NULL)
420  {
421  DEBUG_INFO("Mac: Cannot locate the file in the bundle.");
422  }
423  else
424  {
425  CFURLRef PTOAbsURL = CFURLCopyAbsoluteURL( PTOurl );
426  if(PTOAbsURL == NULL)
427  {
428  DEBUG_INFO("Mac: Cannot convert the file path to absolute");
429  }
430  else
431  {
432  CFStringRef pathInCFString = CFURLCopyFileSystemPath(PTOAbsURL, kCFURLPOSIXPathStyle);
433  CFRelease( PTOAbsURL );
434  if(pathInCFString == NULL)
435  {
436  DEBUG_INFO("Mac: Failed to get URL in CFString");
437  }
438  else
439  {
440  CFRetain( pathInCFString );
441  theResult = wxCFStringRef(pathInCFString).AsString();
442  DEBUG_INFO("Mac: executable's full path in the application bundle: " << theResult.mb_str(wxConvLocal));
443  }
444  }
445  CFRelease( PTOurl );
446  }
447  }
448  return theResult;
449 }
450 
451 wxString MacGetPathToUserDomainTempDir()
452 {
453  wxString tmpDirPath = wxEmptyString;
454 
455  FSRef tempDirRef;
456  OSErr err = FSFindFolder(kUserDomain, kTemporaryFolderType, kCreateFolder, &tempDirRef);
457  if (err == noErr)
458  {
459  CFURLRef tempDirURL = CFURLCreateFromFSRef(kCFAllocatorSystemDefault, &tempDirRef);
460  if (tempDirURL != NULL)
461  {
462  CFStringRef tmpPath = CFURLCopyFileSystemPath(tempDirURL, kCFURLPOSIXPathStyle);
463  CFRetain(tmpPath);
464  tmpDirPath = wxCFStringRef(tmpPath).AsString();
465  CFRelease(tempDirURL);
466  }
467  }
468 
469  return tmpDirPath;
470 }
471 
472 wxString MacGetPathToUserAppSupportAutoPanoFolder()
473 {
474  wxString appSupportAutoPanoFolder = wxEmptyString;
475 
476  FSRef appSupportFolder;
477  OSErr err = FSFindFolder(kUserDomain,kApplicationSupportFolderType,kDontCreateFolder,&appSupportFolder);
478  if( err == noErr)
479  {
480  CFURLRef appSupportFolderURL = CFURLCreateFromFSRef(kCFAllocatorDefault,&appSupportFolder);
481  CFURLRef appSupportHugin = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,appSupportFolderURL,CFSTR("Hugin"),true);
482  CFURLRef autopanoURL = CFURLCreateCopyAppendingPathComponent(kCFAllocatorDefault,appSupportHugin,CFSTR("Autopano"),true);
483  CFStringRef tmpPath = CFURLCopyFileSystemPath(autopanoURL, kCFURLPOSIXPathStyle);
484  CFRetain(tmpPath);
485  appSupportAutoPanoFolder = wxCFStringRef(tmpPath).AsString();
486  CFRelease(autopanoURL);
487  }
488  return appSupportAutoPanoFolder;
489 }
490 
491 
492 #endif // MAC_SELF_CONTAINED_BUNDLE
493 
494 #endif // __WXMAC__
495 
496 const wxString getInvalidCharacters()
497 {
498 #if defined __WXMSW__
499  // the characters :"*?<>| are not allowed in filenames, these are handled well by the file dialog
500  // all other characters should work
501  return wxEmptyString;
502 #else
503  // the characters =;:% does not work with the makefile
504  // we are also rejecting the characters <>*?| which are principally allowed in filenames but will probably make problems when used
505  // the double quote does not work with the panotools file format, so also reject
506  //@BUG tilde ~ and backslash \ are not working with vigraimpex, if this works again these characters can be removed from the list
507  return "*?<>|\"\\~";
508 #endif
509 };
510 
511 bool containsInvalidCharacters(const wxString stringToTest)
512 {
513  if(stringToTest.IsEmpty())
514  return false;
515  wxString forbiddenChars=getInvalidCharacters();
516  for(unsigned int j=0;j<forbiddenChars.size();j++)
517  {
518  if(stringToTest.Find(forbiddenChars[j])!=wxNOT_FOUND)
519  return true;
520  };
521  return false;
522 };
523 
524 void ShowFilenameWarning(wxWindow* parent, const wxArrayString filelist)
525 {
526  wxDialog dlg;
527  wxXmlResource::Get()->LoadDialog(&dlg, parent, "dlg_warning_filename");
528  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()));
529  XRCCTRL(dlg, "dlg_warning_list", wxListBox)->Append(filelist);
530  dlg.Fit();
531  dlg.CenterOnScreen();
532  dlg.ShowModal();
533 };
534 
535 #if wxUSE_ON_FATAL_EXCEPTION
536 void GenerateReport(wxDebugReport::Context ctx)
537 {
538  //from the debugrpt sample inspired
539  wxDebugReportCompress report;
540  report.AddAll(ctx);
541  if (wxDebugReportPreviewStd().Show(report))
542  {
543  if (report.Process())
544  {
545  wxLogMessage(_("Debug report generated in \"%s\"."), report.GetCompressedFileName().c_str());
546  report.Reset();
547  };
548  };
549 };
550 #endif
#define DEBUG_INFO(msg)
Definition: utils.h:69
implementation of huginApp Class
wxString GetFileDialogImageAndRawFilters()
return filter for image and raw files, needed by file open dialog
Definition: platform.cpp:90
void ShowFilenameWarning(wxWindow *parent, const wxArrayString filelist)
shows a dialog about filename with invalid characters, all names in filelist will be show in list ...
Definition: platform.cpp:524
wxString GetMainImageFilters()
return a filter for the main image files (JPG/TIFF/PNG) only
Definition: platform.cpp:81
std::vector< std::string > GetRawExtensions()
return vector of known extensions of raw files, all lower case
Definition: utils.cpp:964
bool containsInvalidCharacters(const wxString stringToTest)
returns true, if the given strings contains invalid characters
Definition: platform.cpp:511
wxString GetVigraImageFilter()
Definition: platform.cpp:45
wxString GetFilterExtensions(const wxString &ext)
build filter string &quot;*.ext&quot;, adds also upper case version for UNIX paths when needed ...
Definition: platform.cpp:34
bool IsRawExtension(const wxString &testExt)
return true, if given extension is in list of known raw extension (comparision is case insensitive ...
Definition: platform.cpp:112
std::vector< std::string > SplitString(const std::string &s, const std::string &sep)
split string s at given sep, returns vector of strings
Definition: utils.cpp:294
const wxString getInvalidCharacters()
returns all invalid characters for the filename (mainly characters, which does not work with gnu make...
Definition: platform.cpp:496
wxString GetFileDialogImageFilters()
return filter for image files, needed by file open dialog it contains all image format vigra can read...
Definition: platform.cpp:70
int HuginMessageBox(const wxString &message, const wxString &caption, int style, wxWindow *parent)
Definition: wxutils.cpp:176