editorfileopener.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package
19  * @author
20  * @date
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "editorfileopener.h"
25 
26 
29 
30 #include <SimoxUtility/algorithm/string/string_tools.h>
31 
32 #include <boost/property_tree/xml_parser.hpp>
33 
34 #define EDITORFILEOPENER_CONFIGFILE "ArmarXGui/EditorFileOpenerConfig.xml"
35 #define FILE_VARIABLE "$file"
36 #define LINE_VARIABLE "$line"
37 #define DEFAULT_EDITOR "qtcreator"
38 
39 namespace armarx
40 {
41  EditorFileOpener::EditorFileOpener(std::string configFile)
42  {
43  setTag("EditorFileOpener");
44  using namespace boost::property_tree;
45 
46 
47  static CMakePackageFinder cmake("ArmarXGui");
48 
49  if (configFile.empty())
50  {
51  configFile = cmake.getDataDir() + "/" + std::string(EDITORFILEOPENER_CONFIGFILE);
52  }
53 
54  if (!ArmarXDataPath::getAbsolutePath(configFile, configFile))
55  {
56  ARMARX_WARNING << "Cannot find file '" << configFile << "'";
57  return;
58  }
59 
60  try
61  {
62  xml_parser::read_xml(configFile, pt);
63  }
64  catch (xml_parser::xml_parser_error& e)
65  {
66 
67  ARMARX_WARNING << "Could not load state-configfile '"
68  << configFile << "'. Required parameters may be unset.\nReason at "
69  << e.filename() << ":" << e.line() << ":\n" << e.message() << flush;
70  return;
71  }
72 
73  for (ptree::value_type const& v : pt.get_child("editors"))
74  {
75  if (v.first == "editor")
76  {
77  std::string editorName = v.second.get<std::string>("name");
78  std::string openCommandLine = v.second.get<std::string>("opencommandline");
79  editors[editorName] = openCommandLine;
80  }
81  }
82  }
83 
84  void EditorFileOpener::openFile(const std::string& editorName, const std::string& filepath, int lineNumber)
85  {
86  std::string openCommandLine = editors[editorName];
87 
88  if (openCommandLine.empty())
89  {
90  ARMARX_WARNING << "Cannot find editor named '" << editorName << "'";
91  return;
92  }
93 
94  std::string::size_type lenFile = std::string(FILE_VARIABLE).size();
95  std::string::size_type posFile = openCommandLine.find(FILE_VARIABLE);
96 
97  if (posFile != std::string::npos)
98  {
99  openCommandLine.replace(posFile, lenFile, filepath);
100  }
101 
102  std::string::size_type lenLine = std::string(LINE_VARIABLE).size();
103  std::string::size_type posLine = openCommandLine.find(LINE_VARIABLE);
104  std::stringstream lineStr;
105  lineStr << lineNumber;
106 
107  if (posLine != std::string::npos)
108  {
109  openCommandLine.replace(posLine, lenLine, lineStr.str());
110  }
111 
112  simox::alg::trim(openCommandLine);
113 
114  if (*openCommandLine.rbegin() != '&')
115  {
116  openCommandLine += " &";
117  }
118 
119  ARMARX_VERBOSE << "OpenCommand: " << openCommandLine;
120 
121  if (std::system(openCommandLine.c_str())) {}
122  }
123 
124  void EditorFileOpener::openFileWithDefaultEditor(const std::string& filepath, int lineNumber)
125  {
126  const char* envEditor = std::getenv("ARMARX_EDITOR");
127 
128  const std::string editorName = [&]() -> std::string
129  {
130  if (envEditor == nullptr)
131  {
132  return DEFAULT_EDITOR;
133  }
134 
135  const std::string editor = std::string(envEditor);
136  if (editors.count(editor) > 0)
137  {
138  return editor;
139  }
140 
141  ARMARX_WARNING << "The editor '" << editor << "' is not registered. "
142  << "Check the config '" << EDITORFILEOPENER_CONFIGFILE << "'";
143 
144  return DEFAULT_EDITOR;
145  }();
146 
147  openFile(editorName, filepath, lineNumber);
148  }
149 
150 } // namespace armarx
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
DEFAULT_EDITOR
#define DEFAULT_EDITOR
Definition: editorfileopener.cpp:37
armarx::CMakePackageFinder
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
Definition: CMakePackageFinder.h:53
armarx::EditorFileOpener::openFile
void openFile(const std::string &editorName, const std::string &filepath, int lineNumber=0)
Definition: editorfileopener.cpp:84
armarx::EditorFileOpener::EditorFileOpener
EditorFileOpener(std::string configFile="")
Definition: editorfileopener.cpp:41
armarx::flush
const LogSender::manipulator flush
Definition: LogSender.h:251
armarx::CMakePackageFinder::getDataDir
std::string getDataDir() const
Definition: CMakePackageFinder.h:176
editorfileopener.h
CMakePackageFinder.h
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
FILE_VARIABLE
#define FILE_VARIABLE
Definition: editorfileopener.cpp:35
armarx::EditorFileOpener::openFileWithDefaultEditor
void openFileWithDefaultEditor(const std::string &filepath, int lineNumber=0)
The default editor can be specified by the environment variable $ARMARX_EDITOR.
Definition: editorfileopener.cpp:124
armarx::ArmarXDataPath::getAbsolutePath
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
Definition: ArmarXDataPath.cpp:111
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
EDITORFILEOPENER_CONFIGFILE
#define EDITORFILEOPENER_CONFIGFILE
Definition: editorfileopener.cpp:34
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
LINE_VARIABLE
#define LINE_VARIABLE
Definition: editorfileopener.cpp:36
ArmarXDataPath.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28