hbexternaleditor.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @file hbexternaleditor.h
  3. * @brief Utility class to launch an external program for editing a file and
  4. * tracking changes on the latter.
  5. *
  6. * $LicenseInfo:firstyear=2019&license=viewergpl$
  7. *
  8. * Copyright (c) 2019, Henri Beauchamp.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_HBEXTERNALEDITOR_H
  34. #define LL_HBEXTERNALEDITOR_H
  35. #include "llstring.h"
  36. class LLLiveFile;
  37. class LLProcessLauncher;
  38. class HBExternalEditor
  39. {
  40. friend class HBEditorLiveFile;
  41. protected:
  42. LOG_CLASS(HBExternalEditor);
  43. public:
  44. typedef void (*HBExternalEditorFileChangedCB)(const std::string& filename,
  45. void* userdata);
  46. HBExternalEditor(HBExternalEditorFileChangedCB callback,
  47. void* userdata = NULL,
  48. bool orphanize_on_destroy = false);
  49. ~HBExternalEditor();
  50. // Call with the name of the file to edit and watch, as well as an optional
  51. // command line (with "%s" as the string argument symbol that will be
  52. // replaced with the filename). E.g.: /usr/bin/nedit %s
  53. // Returns true on success, false on error (error message set accordingly).
  54. bool open(const std::string& filename,
  55. std::string command_line = LLStringUtil::null);
  56. // Call to attempt to kill the external editor (also closes the live file)
  57. void kill();
  58. // Returns true when the external editor is still running, or when we know
  59. // for sure that the editor is detached from the original (and now gone)
  60. // launched process, which happens when we launch a MIME wrapper launcher
  61. // instead of the actual editor.
  62. bool running();
  63. // Returns the last error message.
  64. LL_INLINE const std::string& getErrorMessage() { return mErrorMessage; }
  65. // Call this when planning to update the file yourself and not wanting
  66. // to get notified uselessly about it via the changed callback.
  67. LL_INLINE void ignoreNextUpdate() { mIgnoreNextUpdate = true; }
  68. std::string getFilename();
  69. private:
  70. // For use by HBEditorLiveFile only
  71. void callChangedCallback(const std::string& filename);
  72. private:
  73. void (*mFiledChangedCallback)(const std::string& filename,
  74. void* userdata);
  75. void* mUserData;
  76. LLProcessLauncher* mProcess;
  77. LLLiveFile* mEditedFile;
  78. std::string mErrorMessage;
  79. bool mOrphanizeOnDestroy;
  80. bool mIgnoreNextUpdate;
  81. bool mEditorIsDetached;
  82. };
  83. #endif // LL_HBEXTERNALEDITOR_H