You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ovdCScriptSettingView.cpp 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "ovdCScriptSettingView.h"
  2. #include "../ovd_base.h"
  3. #include <cstring>
  4. #include <cstdlib>
  5. namespace OpenViBE {
  6. namespace Designer {
  7. namespace Setting {
  8. static void OnButtonSettingFilenameBrowsePressed(GtkButton* /*button*/, gpointer data) { static_cast<CScriptSettingView*>(data)->browse(); }
  9. static void OnButtonSettingScriptEditPressed(GtkButton* /*button*/, gpointer data) { static_cast<CScriptSettingView*>(data)->edit(); }
  10. static void OnChange(GtkEntry* /*entry*/, gpointer data) { static_cast<CScriptSettingView*>(data)->onChange(); }
  11. #if defined TARGET_OS_Windows
  12. static gboolean OnFocusOutEvent(GtkEntry* /*entry*/, GdkEvent* /*event*/, gpointer data)
  13. {
  14. static_cast<CScriptSettingView*>(data)->onFocusLost();
  15. return FALSE;
  16. }
  17. #endif
  18. CScriptSettingView::CScriptSettingView(Kernel::IBox& box, const size_t index, CString& builderName, const Kernel::IKernelContext& ctx)
  19. : CAbstractSettingView(box, index, builderName, "settings_collection-hbox_setting_script"), m_kernelCtx(ctx)
  20. {
  21. GtkWidget* settingWidget = CAbstractSettingView::getEntryFieldWidget();
  22. std::vector<GtkWidget*> widgets;
  23. CAbstractSettingView::extractWidget(settingWidget, widgets);
  24. m_entry = GTK_ENTRY(widgets[0]);
  25. g_signal_connect(G_OBJECT(m_entry), "changed", G_CALLBACK(OnChange), this);
  26. #if defined TARGET_OS_Windows
  27. // Only called for Windows path
  28. g_signal_connect(G_OBJECT(m_entry), "focus_out_event", G_CALLBACK(OnFocusOutEvent), this);
  29. #endif
  30. g_signal_connect(G_OBJECT(widgets[1]), "clicked", G_CALLBACK(OnButtonSettingScriptEditPressed), this);
  31. g_signal_connect(G_OBJECT(widgets[2]), "clicked", G_CALLBACK(OnButtonSettingFilenameBrowsePressed), this);
  32. CAbstractSettingView::initializeValue();
  33. }
  34. void CScriptSettingView::getValue(CString& value) const { value = CString(gtk_entry_get_text(m_entry)); }
  35. void CScriptSettingView::setValue(const CString& value)
  36. {
  37. m_onValueSetting = true;
  38. gtk_entry_set_text(m_entry, value);
  39. m_onValueSetting = false;
  40. }
  41. void CScriptSettingView::browse() const
  42. {
  43. GtkWidget* widgetDialogOpen = gtk_file_chooser_dialog_new("Select file to open...", nullptr, GTK_FILE_CHOOSER_ACTION_SAVE,
  44. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, nullptr);
  45. const CString initialFileName = m_kernelCtx.getConfigurationManager().expand(gtk_entry_get_text(m_entry));
  46. if (g_path_is_absolute(initialFileName.toASCIIString()))
  47. {
  48. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), initialFileName.toASCIIString());
  49. }
  50. else
  51. {
  52. char* fullPath = g_build_filename(g_get_current_dir(), initialFileName.toASCIIString(), nullptr);
  53. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), fullPath);
  54. g_free(fullPath);
  55. }
  56. gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(widgetDialogOpen), false);
  57. if (gtk_dialog_run(GTK_DIALOG(widgetDialogOpen)) == GTK_RESPONSE_ACCEPT)
  58. {
  59. char* fileName = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widgetDialogOpen));
  60. char* backslash;
  61. while ((backslash = strchr(fileName, '\\')) != nullptr) { *backslash = '/'; }
  62. gtk_entry_set_text(m_entry, fileName);
  63. g_free(fileName);
  64. }
  65. gtk_widget_destroy(widgetDialogOpen);
  66. }
  67. void CScriptSettingView::edit() const
  68. {
  69. const CString fileName = m_kernelCtx.getConfigurationManager().expand(gtk_entry_get_text(m_entry));
  70. const CString editorCmd = m_kernelCtx.getConfigurationManager().expand("${Designer_ScriptEditorCommand}");
  71. if (editorCmd != CString(""))
  72. {
  73. CString fullCmd = editorCmd + CString(" \"") + fileName + CString("\"");
  74. #if defined TARGET_OS_Windows
  75. fullCmd = "START " + fullCmd;
  76. #elif defined TARGET_OS_Linux
  77. fullCmd = fullCmd + " &";
  78. #else
  79. #endif
  80. if (system(fullCmd.toASCIIString()) < 0) { m_kernelCtx.getLogManager() << Kernel::LogLevel_Warning << "Could not run command " << fullCmd << "\n"; }
  81. }
  82. }
  83. void CScriptSettingView::onChange()
  84. {
  85. if (!m_onValueSetting)
  86. {
  87. const gchar* value = gtk_entry_get_text(m_entry);
  88. getBox().setSettingValue(getSettingIndex(), value);
  89. }
  90. }
  91. #if defined TARGET_OS_Windows
  92. void CScriptSettingView::onFocusLost()
  93. {
  94. // We replace antislash, interpreted as escape, by slash in Windows path
  95. if (!m_onValueSetting)
  96. {
  97. std::string fileName = gtk_entry_get_text(m_entry);
  98. auto it = fileName.begin();
  99. while ((it = std::find(it, fileName.end(), '\\')) != fileName.end())
  100. {
  101. if (it == std::prev(fileName.end()))
  102. {
  103. *it = '/';
  104. break;
  105. }
  106. if (*std::next(it) != '{' && *std::next(it) != '$' && *std::next(it) != '}') { *it = '/'; }
  107. std::advance(it, 1);
  108. }
  109. gtk_entry_set_text(m_entry, fileName.c_str());
  110. getBox().setSettingValue(this->getSettingIndex(), fileName.c_str());
  111. }
  112. }
  113. #endif
  114. } // namespace Setting
  115. } // namespace Designer
  116. } // namespace OpenViBE