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