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#include <boost/property_tree/xml_parser.hpp>
27
28#include <SimoxUtility/algorithm/string/string_tools.h>
29
32
33#define EDITORFILEOPENER_CONFIGFILE "ArmarXGui/EditorFileOpenerConfig.xml"
34#define FILE_VARIABLE "$file"
35#define LINE_VARIABLE "$line"
36#define DEFAULT_EDITOR "qtcreator"
37
38namespace armarx
39{
40 EditorFileOpener::EditorFileOpener(std::string configFile)
41 {
42 setTag("EditorFileOpener");
43 using namespace boost::property_tree;
44
45
46 static CMakePackageFinder cmake("ArmarXGui");
47
48 if (configFile.empty())
49 {
50 configFile = cmake.getDataDir() + "/" + std::string(EDITORFILEOPENER_CONFIGFILE);
51 }
52
53 if (!ArmarXDataPath::getAbsolutePath(configFile, configFile))
54 {
55 ARMARX_WARNING << "Cannot find file '" << configFile << "'";
56 return;
57 }
58
59 try
60 {
61 xml_parser::read_xml(configFile, pt);
62 }
63 catch (xml_parser::xml_parser_error& e)
64 {
65
66 ARMARX_WARNING << "Could not load state-configfile '" << configFile
67 << "'. Required parameters may be unset.\nReason at " << e.filename()
68 << ":" << e.line() << ":\n"
69 << 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
85 EditorFileOpener::openFile(const std::string& editorName,
86 const std::string& filepath,
87 int lineNumber)
88 {
89 std::string openCommandLine = editors[editorName];
90
91 if (openCommandLine.empty())
92 {
93 ARMARX_WARNING << "Cannot find editor named '" << editorName << "'";
94 return;
95 }
96
97 std::string::size_type lenFile = std::string(FILE_VARIABLE).size();
98 std::string::size_type posFile = openCommandLine.find(FILE_VARIABLE);
99
100 if (posFile != std::string::npos)
101 {
102 openCommandLine.replace(posFile, lenFile, filepath);
103 }
104
105 std::string::size_type lenLine = std::string(LINE_VARIABLE).size();
106 std::string::size_type posLine = openCommandLine.find(LINE_VARIABLE);
107 std::stringstream lineStr;
108 lineStr << lineNumber;
109
110 if (posLine != std::string::npos)
111 {
112 openCommandLine.replace(posLine, lenLine, lineStr.str());
113 }
114
115 simox::alg::trim(openCommandLine);
116
117 if (*openCommandLine.rbegin() != '&')
118 {
119 openCommandLine += " &";
120 }
121
122 ARMARX_VERBOSE << "OpenCommand: " << openCommandLine;
123
124 if (std::system(openCommandLine.c_str()))
125 {
126 }
127 }
128
129 void
130 EditorFileOpener::openFileWithDefaultEditor(const std::string& filepath, int lineNumber)
131 {
132 const char* envEditor = std::getenv("ARMARX_EDITOR");
133
134 const std::string editorName = [&]() -> std::string
135 {
136 if (envEditor == nullptr)
137 {
138 return DEFAULT_EDITOR;
139 }
140
141 const std::string editor = std::string(envEditor);
142 if (editors.count(editor) > 0)
143 {
144 return editor;
145 }
146
147 ARMARX_WARNING << "The editor '" << editor << "' is not registered. "
148 << "Check the config '" << EDITORFILEOPENER_CONFIGFILE << "'";
149
150 return DEFAULT_EDITOR;
151 }();
152
153 openFile(editorName, filepath, lineNumber);
154 }
155
156} // namespace armarx
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
void openFileWithDefaultEditor(const std::string &filepath, int lineNumber=0)
The default editor can be specified by the environment variable $ARMARX_EDITOR.
void openFile(const std::string &editorName, const std::string &filepath, int lineNumber=0)
EditorFileOpener(std::string configFile="")
void setTag(const LogTag &tag)
Definition Logging.cpp:54
#define LINE_VARIABLE
#define EDITORFILEOPENER_CONFIGFILE
#define FILE_VARIABLE
#define DEFAULT_EDITOR
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
#define ARMARX_VERBOSE
The logging level for verbose information.
Definition Logging.h:187
This file offers overloads of toIce() and fromIce() functions for STL container types.
const LogSender::manipulator flush
Definition LogSender.h:251