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