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.

ovdCBoxConfigurationDialog.cpp 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. #include "ovdCBoxConfigurationDialog.h"
  2. #include "ovdCSettingCollectionHelper.h"
  3. #include <vector>
  4. #include <string>
  5. #include <xml/IXMLHandler.h>
  6. #include <xml/IXMLNode.h>
  7. #include <fs/Files.h>
  8. namespace OpenViBE {
  9. namespace Designer {
  10. static const char* const ROOT_NAME = "OpenViBE-SettingsOverride";
  11. static const char* const SETTING_NAME = "SettingValue";
  12. static void onFileOverrideCheckToggled(GtkToggleButton* button, gpointer data)
  13. {
  14. gtk_widget_set_sensitive(static_cast<GtkWidget*>(data), !gtk_toggle_button_get_active(button));
  15. }
  16. static void OnButtonLoadClicked(GtkButton* /*button*/, gpointer data) { static_cast<CBoxConfigurationDialog*>(data)->loadConfig(); }
  17. static void OnButtonSaveClicked(GtkButton* /*button*/, gpointer data) { static_cast<CBoxConfigurationDialog*>(data)->saveConfig(); }
  18. static void OnOverrideBrowseClicked(GtkButton* /*button*/, gpointer data) { static_cast<CBoxConfigurationDialog*>(data)->onOverrideBrowse(); }
  19. static void CollectWidgetCB(GtkWidget* widget, gpointer data) { static_cast<std::vector<GtkWidget*>*>(data)->push_back(widget); }
  20. CBoxConfigurationDialog::CBoxConfigurationDialog(const Kernel::IKernelContext& ctx, Kernel::IBox& box, const char* guiFilename,
  21. const char* guiSettingsFilename, const bool isScenarioRunning)
  22. : m_kernelCtx(ctx), m_box(box), m_guiFilename(guiFilename), m_guiSettingsFilename(guiSettingsFilename),
  23. m_settingFactory(m_guiSettingsFilename.toASCIIString(), ctx), m_isScenarioRunning(isScenarioRunning)
  24. {
  25. m_box.addObserver(this);
  26. if (m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting))
  27. {
  28. GtkBuilder* builder = gtk_builder_new(); // glade_xml_new(m_guiFilename.toASCIIString(), "box_configuration", nullptr);
  29. gtk_builder_add_from_file(builder, m_guiFilename.toASCIIString(), nullptr);
  30. gtk_builder_connect_signals(builder, nullptr);
  31. if (!m_isScenarioRunning)
  32. {
  33. // TODO : This is not a modal dialog. It would be better if it was.
  34. m_settingDialog = GTK_WIDGET(gtk_builder_get_object(builder, "box_configuration"));
  35. const std::string title = std::string("Configure ") + m_box.getName().toASCIIString() + " settings";
  36. gtk_window_set_title(GTK_WINDOW(m_settingDialog), title.c_str());
  37. }
  38. else
  39. {
  40. // This is actually *not* a dialog
  41. m_settingDialog = GTK_WIDGET(gtk_builder_get_object(builder, "box_configuration-scrolledwindow"));
  42. }
  43. m_settingsTable = GTK_TABLE(gtk_builder_get_object(builder, "box_configuration-table"));
  44. m_scrolledWindow = GTK_SCROLLED_WINDOW(gtk_builder_get_object(builder, "box_configuration-scrolledwindow"));
  45. m_viewPort = GTK_VIEWPORT(gtk_builder_get_object(builder, "box_configuration-viewport"));
  46. gtk_table_resize(m_settingsTable, guint(m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting)), 4);
  47. generateSettingsTable();
  48. const CSettingCollectionHelper helper(m_kernelCtx, m_guiSettingsFilename.toASCIIString());
  49. if (!m_isScenarioRunning)
  50. {
  51. GtkContainer* fileOverrideContainer = GTK_CONTAINER(gtk_builder_get_object(builder, "box_configuration-hbox_filename_override"));
  52. m_fileOverrideCheck = GTK_CHECK_BUTTON(gtk_builder_get_object(builder, "box_configuration-checkbutton_filename_override"));
  53. GtkButton* buttonLoad = GTK_BUTTON(gtk_builder_get_object(builder, "box_configuration-button_load_current_from_file"));
  54. GtkButton* buttonSave = GTK_BUTTON(gtk_builder_get_object(builder, "box_configuration-button_save_current_to_file"));
  55. const std::string settingOverrideWidgetName = helper.getSettingWidgetName(OV_TypeId_Filename).toASCIIString();
  56. GtkBuilder* builderInterfaceSettingCollection = gtk_builder_new();
  57. gtk_builder_add_from_file(builderInterfaceSettingCollection, m_guiSettingsFilename.toASCIIString(), nullptr);
  58. m_overrideEntryContainer = GTK_WIDGET(gtk_builder_get_object(builderInterfaceSettingCollection, settingOverrideWidgetName.c_str()));
  59. std::vector<GtkWidget*> widgets;
  60. gtk_container_foreach(GTK_CONTAINER(m_overrideEntryContainer), CollectWidgetCB, &widgets);
  61. gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(m_overrideEntryContainer)), m_overrideEntryContainer);
  62. gtk_container_add(fileOverrideContainer, m_overrideEntryContainer);
  63. m_overrideEntry = GTK_ENTRY(widgets[0]);
  64. g_signal_connect(G_OBJECT(m_fileOverrideCheck), "toggled", G_CALLBACK(onFileOverrideCheckToggled), GTK_WIDGET(m_settingsTable));
  65. g_signal_connect(G_OBJECT(widgets[1]), "clicked", G_CALLBACK(OnOverrideBrowseClicked), this);
  66. g_signal_connect(G_OBJECT(buttonLoad), "clicked", G_CALLBACK(OnButtonLoadClicked), this);
  67. g_signal_connect(G_OBJECT(buttonSave), "clicked", G_CALLBACK(OnButtonSaveClicked), this);
  68. if (m_box.hasAttribute(OV_AttributeId_Box_SettingOverrideFilename))
  69. {
  70. GtkExpander* expander = GTK_EXPANDER(gtk_builder_get_object(builder, "box_configuration-expander"));
  71. gtk_expander_set_expanded(expander, true);
  72. gtk_entry_set_text(m_overrideEntry, m_box.getAttributeValue(OV_AttributeId_Box_SettingOverrideFilename).toASCIIString());
  73. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_fileOverrideCheck), true);
  74. gtk_widget_set_sensitive(GTK_WIDGET(m_settingsTable), false);
  75. }
  76. else { gtk_entry_set_text(m_overrideEntry, ""); }
  77. g_object_unref(builder);
  78. g_object_unref(builderInterfaceSettingCollection);
  79. }
  80. }
  81. }
  82. CBoxConfigurationDialog::~CBoxConfigurationDialog()
  83. {
  84. m_box.deleteObserver(this);
  85. if (m_settingDialog) { gtk_widget_destroy(m_settingDialog); }
  86. }
  87. bool CBoxConfigurationDialog::run()
  88. {
  89. bool modified = false;
  90. if (m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting))
  91. {
  92. //CSettingCollectionHelper helper(m_kernelCtx, m_guiSettingsFilename.toASCIIString());
  93. storeState();
  94. bool finished = false;
  95. while (!finished)
  96. {
  97. const gint result = gtk_dialog_run(GTK_DIALOG(m_settingDialog));
  98. if (result == GTK_RESPONSE_APPLY)
  99. {
  100. if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(m_fileOverrideCheck)))
  101. {
  102. const gchar* fileName = gtk_entry_get_text(m_overrideEntry);
  103. if (m_box.hasAttribute(OV_AttributeId_Box_SettingOverrideFilename))
  104. {
  105. m_box.setAttributeValue(OV_AttributeId_Box_SettingOverrideFilename, fileName);
  106. }
  107. else { m_box.addAttribute(OV_AttributeId_Box_SettingOverrideFilename, fileName); }
  108. }
  109. else
  110. {
  111. if (m_box.hasAttribute(OV_AttributeId_Box_SettingOverrideFilename)) { m_box.removeAttribute(OV_AttributeId_Box_SettingOverrideFilename); }
  112. }
  113. finished = true;
  114. modified = true;
  115. }
  116. else if (result == GTK_RESPONSE_CANCEL)
  117. {
  118. restoreState();
  119. finished = true;
  120. }
  121. else if (result == 1) // default
  122. {
  123. // Some settings will add/remove other settings;
  124. // by evaluating m_box.getSettingCount() each time we ensure not ending somewhere in the oblivion
  125. for (size_t i = 0; i < m_settingViews.size(); ++i)
  126. {
  127. CString value;
  128. m_box.getSettingDefaultValue(i, value);
  129. m_box.setSettingValue(i, value);
  130. //m_settingViews[i]->setValue(value);
  131. //helper.setValue(settingType, i < m_settingViews.size()? m_settingViews[i]->getEntryWidget() : nullptr, value);
  132. }
  133. gtk_entry_set_text(GTK_ENTRY(m_overrideEntryContainer), "");
  134. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_fileOverrideCheck), false);
  135. //gtk_widget_set_sensitive(GTK_WIDGET(m_settingsTable), true);
  136. modified = false;
  137. }
  138. else if (result == 2) // revert
  139. {
  140. restoreState();
  141. if (m_box.hasAttribute(OV_AttributeId_Box_SettingOverrideFilename))
  142. {
  143. gtk_entry_set_text(
  144. GTK_ENTRY(m_overrideEntryContainer), m_box.getAttributeValue(OV_AttributeId_Box_SettingOverrideFilename).toASCIIString());
  145. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_fileOverrideCheck), true);
  146. }
  147. else
  148. {
  149. gtk_entry_set_text(GTK_ENTRY(m_overrideEntryContainer), "");
  150. gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(m_fileOverrideCheck), false);
  151. }
  152. }
  153. else if (result == 3) { modified = true; } // load
  154. else if (result == 4) { } // save
  155. else { finished = true; }
  156. }
  157. }
  158. return modified;
  159. }
  160. void CBoxConfigurationDialog::update(CObservable& /*o*/, void* data)
  161. {
  162. const Kernel::BoxEventMessage* event = static_cast<Kernel::BoxEventMessage*>(data);
  163. switch (event->m_Type)
  164. {
  165. case Kernel::SettingsAllChange:
  166. generateSettingsTable();
  167. break;
  168. case Kernel::SettingValueUpdate:
  169. {
  170. CString value;
  171. m_box.getSettingValue(event->m_FirstIdx, value);
  172. m_settingViews[event->m_FirstIdx]->setValue(value);
  173. break;
  174. }
  175. case Kernel::SettingDelete:
  176. removeSetting(event->m_FirstIdx);
  177. break;
  178. case Kernel::SettingAdd:
  179. addSetting(event->m_FirstIdx);
  180. break;
  181. case Kernel::SettingChange:
  182. settingChange(event->m_FirstIdx);
  183. break;
  184. case Kernel::SettingsReorder: break;
  185. default: break; //OV_ERROR_KRF("wtf", Kernel::ErrorType::BadSetting);
  186. }
  187. }
  188. void CBoxConfigurationDialog::generateSettingsTable()
  189. {
  190. std::for_each(m_settingViews.begin(), m_settingViews.end(), [](Setting::CAbstractSettingView* elem) { delete elem; });
  191. m_settingViews.clear();
  192. //Remove rows
  193. gtk_container_foreach(GTK_CONTAINER(GTK_WIDGET(m_settingsTable)),
  194. [](GtkWidget* widget, gpointer data) { gtk_container_remove(GTK_CONTAINER(data), widget); },
  195. GTK_WIDGET(m_settingsTable));
  196. size_t size = 0;
  197. if (m_isScenarioRunning)
  198. {
  199. for (size_t i = 0; i < m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting); ++i)
  200. {
  201. bool mod = false;
  202. m_box.getSettingMod(i, mod);
  203. if (mod) { size++; }
  204. }
  205. }
  206. else { size = m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting); }
  207. gtk_table_resize(m_settingsTable, guint(size + 2), 4);
  208. // Iterate over box settings, generate corresponding gtk widgets. If the scenario is running, we are making a
  209. // 'modifiable settings' dialog and use a subset of widgets with a slightly different layout and buttons.
  210. for (size_t settingIdx = 0, tableIdx = 0; settingIdx < m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting); ++settingIdx)
  211. {
  212. if (addSettingsToView(settingIdx, tableIdx)) { ++tableIdx; }
  213. }
  214. updateSize();
  215. }
  216. bool CBoxConfigurationDialog::addSettingsToView(const size_t settingIdx, const size_t tableIdx)
  217. {
  218. bool modifiable;
  219. m_box.getSettingMod(settingIdx, modifiable);
  220. if ((!m_isScenarioRunning) || (m_isScenarioRunning && modifiable))
  221. {
  222. CString name;
  223. m_box.getSettingName(settingIdx, name);
  224. Setting::CAbstractSettingView* view = m_settingFactory.getSettingView(m_box, settingIdx);
  225. bool isDeprecated = false;
  226. m_box.getInterfacorDeprecatedStatus(Kernel::EBoxInterfacorType::Setting, settingIdx, isDeprecated);
  227. if (isDeprecated)
  228. {
  229. gtk_widget_set_sensitive(GTK_WIDGET(view->getNameWidget()), false);
  230. gtk_widget_set_sensitive(GTK_WIDGET(view->getEntryWidget()), false);
  231. }
  232. gtk_table_attach(m_settingsTable, view->getNameWidget(), 0, 1, guint(tableIdx), guint(tableIdx + 1), GtkAttachOptions(GTK_FILL),
  233. GtkAttachOptions(GTK_FILL), 0, 0);
  234. gtk_table_attach(m_settingsTable, view->getEntryWidget(), 1, 4, guint(tableIdx), guint(tableIdx + 1),
  235. GtkAttachOptions(GTK_SHRINK | GTK_FILL | GTK_EXPAND), GtkAttachOptions(GTK_SHRINK), 0, 0);
  236. m_settingViews.insert(m_settingViews.begin() + tableIdx, view);
  237. return true;
  238. }
  239. return false;
  240. }
  241. void CBoxConfigurationDialog::settingChange(const size_t index)
  242. {
  243. //We remeber the place to add the new setting at the same place
  244. const size_t indexTable = getTableIndex(index);
  245. removeSetting(index, false);
  246. addSettingsToView(index, indexTable);
  247. }
  248. void CBoxConfigurationDialog::addSetting(const size_t index)
  249. {
  250. bool modifiable;
  251. m_box.getSettingMod(index, modifiable);
  252. if ((!m_isScenarioRunning) || (m_isScenarioRunning && modifiable))
  253. {
  254. const size_t size = m_settingViews.size();
  255. /*There is two case.
  256. 1) we just add at the end of the setting box
  257. 2) we add it in the middle end we need to shift
  258. */
  259. const size_t tableIdx = (index > m_settingViews[size - 1]->getSettingIndex()) ? size : getTableIndex(index);
  260. gtk_table_resize(m_settingsTable, guint(size + 2), 4);
  261. if (index <= m_settingViews[size - 1]->getSettingIndex())
  262. {
  263. for (size_t i = size - 1; i >= tableIdx; --i)
  264. {
  265. Setting::CAbstractSettingView* view = m_settingViews[i];
  266. //We need to update the index
  267. view->setSettingIndex(view->getSettingIndex() + 1);
  268. gtk_container_remove(GTK_CONTAINER(m_settingsTable), view->getNameWidget());
  269. gtk_table_attach(m_settingsTable, view->getNameWidget(), 0, 1, guint(i + 1), guint(i + 2), GtkAttachOptions(GTK_FILL),
  270. GtkAttachOptions(GTK_FILL), 0, 0);
  271. gtk_container_remove(GTK_CONTAINER(m_settingsTable), view->getEntryWidget());
  272. gtk_table_attach(m_settingsTable, view->getEntryWidget(), 1, 4, guint(i + 1), guint(i + 2),
  273. GtkAttachOptions(GTK_SHRINK | GTK_FILL | GTK_EXPAND), GtkAttachOptions(GTK_SHRINK), 0, 0);
  274. }
  275. }
  276. addSettingsToView(size_t(tableIdx), index);
  277. updateSize();
  278. }
  279. //Even if nothing is add to the interface, we still need to update index
  280. else
  281. {
  282. for (Setting::CAbstractSettingView* view : m_settingViews)
  283. {
  284. if (view->getSettingIndex() >= index) { view->setSettingIndex(view->getSettingIndex() + 1); }
  285. }
  286. }
  287. }
  288. void CBoxConfigurationDialog::removeSetting(const size_t index, const bool shift)
  289. {
  290. const int tableIdx = getTableIndex(index);
  291. if (tableIdx != -1)
  292. {
  293. Setting::CAbstractSettingView* view = m_settingViews[tableIdx];
  294. GtkWidget* name = view->getNameWidget();
  295. GtkWidget* entry = view->getEntryWidget();
  296. gtk_container_remove(GTK_CONTAINER(m_settingsTable), name);
  297. gtk_container_remove(GTK_CONTAINER(m_settingsTable), entry);
  298. delete view;
  299. m_settingViews.erase(m_settingViews.begin() + tableIdx);
  300. //Now if we need to do it we shift everything to avoid an empty row in the table
  301. if (shift)
  302. {
  303. for (size_t i = tableIdx; i < m_settingViews.size(); ++i)
  304. {
  305. view = m_settingViews[i];
  306. view->setSettingIndex(view->getSettingIndex() - 1);
  307. gtk_container_remove(GTK_CONTAINER(m_settingsTable), view->getNameWidget());
  308. gtk_table_attach(m_settingsTable, view->getNameWidget(), 0, 1, guint(i), guint(i + 1), GtkAttachOptions(GTK_FILL), GtkAttachOptions(GTK_FILL),
  309. 0, 0);
  310. gtk_container_remove(GTK_CONTAINER(m_settingsTable), view->getEntryWidget());
  311. gtk_table_attach(m_settingsTable, view->getEntryWidget(), 1, 4, guint(i), guint(i + 1), GtkAttachOptions(GTK_SHRINK | GTK_FILL | GTK_EXPAND),
  312. GtkAttachOptions(GTK_SHRINK), 0, 0);
  313. }
  314. //Now let's resize everything
  315. gtk_table_resize(m_settingsTable, guint(m_settingViews.size() + 2), 4);
  316. updateSize();
  317. }
  318. }
  319. //Even if we delete an "invisible" setting we need to update every index.
  320. else
  321. {
  322. for (Setting::CAbstractSettingView* view : m_settingViews)
  323. {
  324. if (view->getSettingIndex() >= index) { view->setSettingIndex(view->getSettingIndex() - 1); }
  325. }
  326. }
  327. }
  328. int CBoxConfigurationDialog::getTableIndex(const size_t index)
  329. {
  330. size_t tableIdx = 0;
  331. for (auto it = m_settingViews.begin(); it != m_settingViews.end(); ++it, ++tableIdx)
  332. {
  333. Setting::CAbstractSettingView* view = *it;
  334. if (view->getSettingIndex() == index) { return int(index); }
  335. }
  336. return -1;
  337. }
  338. void CBoxConfigurationDialog::updateSize() const
  339. {
  340. // Resize the window to fit as much of the table as possible, but keep the max size
  341. // limited so it doesn't get outside the screen. For safety, we cap to 800x600
  342. // anyway to hopefully prevent the window from going under things such as the gnome toolbar.
  343. // The ui file at the moment does not allow resize of this window because the result
  344. // looked ugly if the window was made overly large, and no satisfying solution at the time was
  345. // found by the limited intellectual resources available.
  346. const gint maxWidth = std::min(800, gdk_screen_get_width(gdk_screen_get_default()));
  347. const gint maxHeight = std::min(600, gdk_screen_get_height(gdk_screen_get_default()));
  348. GtkRequisition size;
  349. gtk_widget_size_request(GTK_WIDGET(m_viewPort), &size);
  350. gtk_widget_set_size_request(GTK_WIDGET(m_scrolledWindow), gint(std::min(maxWidth, size.width)), gint(std::min(maxHeight, size.height)));
  351. }
  352. void CBoxConfigurationDialog::saveConfig() const
  353. {
  354. GtkWidget* widgetDialogOpen = gtk_file_chooser_dialog_new("Select file to save settings to...", nullptr, GTK_FILE_CHOOSER_ACTION_SAVE,
  355. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, nullptr);
  356. const gchar* initialFileNameToExpand = gtk_entry_get_text(GTK_ENTRY(m_overrideEntryContainer));
  357. const CString initialFileName = m_kernelCtx.getConfigurationManager().expand(initialFileNameToExpand);
  358. if (g_path_is_absolute(initialFileName.toASCIIString()))
  359. {
  360. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), initialFileName.toASCIIString());
  361. }
  362. else
  363. {
  364. char* fullPath = g_build_filename(g_get_current_dir(), initialFileName.toASCIIString(), nullptr);
  365. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), fullPath);
  366. g_free(fullPath);
  367. }
  368. if (gtk_dialog_run(GTK_DIALOG(widgetDialogOpen)) == GTK_RESPONSE_ACCEPT)
  369. {
  370. char* fileName = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widgetDialogOpen));
  371. XML::IXMLHandler* handler = XML::createXMLHandler();
  372. XML::IXMLNode* rootNode = XML::createNode(ROOT_NAME);
  373. for (size_t i = 0; i < m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting); ++i)
  374. {
  375. XML::IXMLNode* tmpNode = XML::createNode(SETTING_NAME);
  376. CString value;
  377. m_box.getSettingValue(size_t(i), value);
  378. tmpNode->setPCData(value.toASCIIString());
  379. rootNode->addChild(tmpNode);
  380. }
  381. handler->writeXMLInFile(*rootNode, fileName);
  382. handler->release();
  383. rootNode->release();
  384. g_free(fileName);
  385. }
  386. gtk_widget_destroy(widgetDialogOpen);
  387. }
  388. void CBoxConfigurationDialog::loadConfig() const
  389. {
  390. GtkWidget* widgetDialogOpen = gtk_file_chooser_dialog_new("Select file to load settings from...", nullptr, GTK_FILE_CHOOSER_ACTION_SAVE,
  391. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, nullptr);
  392. const gchar* filenameToExpand = gtk_entry_get_text(GTK_ENTRY(m_overrideEntryContainer));
  393. const CString filename = m_kernelCtx.getConfigurationManager().expand(filenameToExpand);
  394. if (g_path_is_absolute(filename.toASCIIString())) { gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), filename.toASCIIString()); }
  395. else
  396. {
  397. char* fullPath = g_build_filename(g_get_current_dir(), filename.toASCIIString(), nullptr);
  398. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), fullPath);
  399. g_free(fullPath);
  400. }
  401. if (gtk_dialog_run(GTK_DIALOG(widgetDialogOpen)) == GTK_RESPONSE_ACCEPT)
  402. {
  403. char* path = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widgetDialogOpen));
  404. XML::IXMLHandler* handler = XML::createXMLHandler();
  405. XML::IXMLNode* rootNode = handler->parseFile(path);
  406. for (size_t i = 0; i < rootNode->getChildCount(); ++i)
  407. {
  408. //Hope everything will fit in the right place
  409. m_box.setSettingValue(size_t(i), rootNode->getChild(i)->getPCData());
  410. }
  411. rootNode->release();
  412. handler->release();
  413. g_free(path);
  414. }
  415. gtk_widget_destroy(widgetDialogOpen);
  416. }
  417. void CBoxConfigurationDialog::onOverrideBrowse() const
  418. {
  419. GtkWidget* widgetDialogOpen = gtk_file_chooser_dialog_new("Select file to open...", nullptr, GTK_FILE_CHOOSER_ACTION_SAVE,
  420. GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, nullptr);
  421. const CString initialFilename = m_kernelCtx.getConfigurationManager().expand(gtk_entry_get_text(GTK_ENTRY(m_overrideEntry)));
  422. if (g_path_is_absolute(initialFilename.toASCIIString()))
  423. {
  424. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), initialFilename.toASCIIString());
  425. }
  426. else
  427. {
  428. char* fullPath = g_build_filename(g_get_current_dir(), initialFilename.toASCIIString(), nullptr);
  429. gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(widgetDialogOpen), fullPath);
  430. g_free(fullPath);
  431. }
  432. gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(widgetDialogOpen), false);
  433. if (gtk_dialog_run(GTK_DIALOG(widgetDialogOpen)) == GTK_RESPONSE_ACCEPT)
  434. {
  435. gchar* name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widgetDialogOpen));
  436. std::string filename(name);
  437. g_free(name);
  438. std::replace(filename.begin(), filename.end(), '\\', '/');
  439. gtk_entry_set_text(GTK_ENTRY(m_overrideEntry), filename.c_str());
  440. }
  441. gtk_widget_destroy(widgetDialogOpen);
  442. }
  443. void CBoxConfigurationDialog::storeState()
  444. {
  445. m_settingsMemory.clear();
  446. for (size_t i = 0; i < m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting); ++i)
  447. {
  448. CString temp;
  449. m_box.getSettingValue(i, temp);
  450. m_settingsMemory.push_back(temp);
  451. }
  452. }
  453. void CBoxConfigurationDialog::restoreState()
  454. {
  455. for (size_t i = 0; i < m_settingsMemory.size(); ++i)
  456. {
  457. if (i >= m_box.getInterfacorCountIncludingDeprecated(Kernel::EBoxInterfacorType::Setting)) { return; } // This is not supposed to happen
  458. m_box.setSettingValue(i, m_settingsMemory[i]);
  459. }
  460. }
  461. } // namespace Designer
  462. } // namespace OpenViBE