OpenASIP  2.0
UserManualCmd.cc
Go to the documentation of this file.
1 /*
2  Copyright (c) 2002-2009 Tampere University.
3 
4  This file is part of TTA-Based Codesign Environment (TCE).
5 
6  Permission is hereby granted, free of charge, to any person obtaining a
7  copy of this software and associated documentation files (the "Software"),
8  to deal in the Software without restriction, including without limitation
9  the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  and/or sell copies of the Software, and to permit persons to whom the
11  Software is furnished to do so, subject to the following conditions:
12 
13  The above copyright notice and this permission notice shall be included in
14  all copies or substantial portions of the Software.
15 
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  DEALINGS IN THE SOFTWARE.
23  */
24 /**
25  * @file UserManualCmd.cc
26  *
27  * Definition of UserManualCmd class.
28  *
29  * @author Tommi Rantanen 2004 (tommi.rantanen-no.spam-tut.fi)
30  */
31 
32 #include <wx/mimetype.h>
33 #include <wx/arrstr.h>
34 #include <string>
35 
36 #include "UserManualCmd.hh"
37 #include "WxConversion.hh"
38 #include "FileSystem.hh"
39 #include "ErrorDialog.hh"
40 #include "WarningDialog.hh"
41 #include "WidgetTools.hh"
42 #include "StringTools.hh"
43 
44 #if wxCHECK_VERSION(3, 0, 0)
45  #define wxOPEN wxFD_OPEN
46 #endif
47 
48 using std::string;
49 
51 
52 const std::string UserManualCmd::COMMAND_NAME = "User Manual";
53 const std::string UserManualCmd::COMMAND_ICON = "help.png";
54 const std::string UserManualCmd::COMMAND_SHORT_NAME = "Help";
55 const int UserManualCmd::COMMAND_ID = 30000;
56 
57 /**
58  * The Constructor.
59  */
61  GUICommand(COMMAND_NAME, NULL) {
62 }
63 
64 
65 /**
66  * The Destructor.
67  */
69 
70 
71 /**
72  * Executes the command.
73  *
74  * @return True, if the command was succesfully executed, false otherwise.
75  */
76 bool
78 
79 
81  wxTheMimeTypesManager->Initialize();
83  }
84 
86  wxString extension = manual.AfterLast(_T('.'));
87  wxFileType* ft =
88  wxTheMimeTypesManager->GetFileTypeFromExtension(extension);
89 
90  std::string cmdStr;
91  wxString cmd;
92  if (ft != NULL) {
93  cmd = ft->GetOpenCommand(manual);
94  delete ft;
95  ft = NULL;
96  cmdStr = std::string(cmd.ToAscii());
97  }
98 
99  // Stupid hack for now. For fedora some reason this cmd would end up
100  // being 'gimp' which is not very nice PDF reader ;)
101  // on ubuntu this could be gv which cannot view PDF's.
102  if (!(cmdStr.find("evince") != std::string::npos ||
103  cmdStr.find("kpdf") != std::string::npos ||
104  cmdStr.find("kghostview") != std::string::npos ||
105  cmdStr.find("acroread") != std::string::npos ||
106  cmdStr.find("okular") != std::string::npos)) {
107  cmdStr = "";
108  }
109  if (cmdStr == "") {
110  // if couldn't get viewer from mime types test if some usual
111  // default viewer is found
112  std::vector<std::string> viewers;
113  viewers.push_back("/usr/bin/evince");
114  viewers.push_back("/usr/bin/kpdf");
115  viewers.push_back("/usr/bin/kghostview");
116  viewers.push_back("/usr/bin/acroread");
117  viewers.push_back("/usr/bin/okular");
118  for (unsigned int i = 0; i < viewers.size(); ++i) {
119  if (FileSystem::fileIsExecutable(viewers.at(i))) {
120  wxExecute(
121  wxString::FromAscii(
122  (viewers.at(i) + " ").c_str()) + manual);
123  return true;
124  }
125  }
126 
127  // if none of above viewers were found try to find from PATH env
128  // variable
129  viewers.clear();
130  std::string DS = FileSystem::DIRECTORY_SEPARATOR;
131  viewers.push_back("evince");
132  viewers.push_back("kpdf");
133  viewers.push_back("kghostview");
134  viewers.push_back("acroread");
135  viewers.push_back("okular");
136 
137  std::vector<std::string> paths;
138  std::string pathsEnv = Environment::environmentVariable("PATH");
139  StringTools::chopString(pathsEnv, ":", paths);
140 
141  for (unsigned int i = 0; i < paths.size(); ++i) {
142  for (unsigned int j = 0; j < viewers.size(); ++j) {
143  std::string viewer = paths.at(i) + DS + viewers.at(j);
144  if (FileSystem::fileIsExecutable(viewer)) {
145  wxExecute(
146  wxString::FromAscii(
147  (viewers.at(i) + " ").c_str()) + manual);
148  return true;
149  }
150  }
151  }
152  // did not find any viewer from path or default locations
153 
154  return askFromUser();
155  } else {
156  wxExecute(cmd);
157  }
158 
159  return true;
160 }
161 
162 
163 /**
164  * Returns id of this command.
165  */
166 int
168  return COMMAND_ID;
169 }
170 
171 
172 /**
173  * Creates and returns a new instance of this command.
174  */
177  return new UserManualCmd();
178 }
179 
180 
181 /**
182  * Returns short version of the command name.
183  */
184 string
186  return COMMAND_SHORT_NAME;
187 }
188 
189 
190 /**
191  * Returns path to the command's icon file.
192  */
193 string
195  return COMMAND_ICON;
196 }
197 
198 
199 /**
200  * This command is always executable.
201  *
202  * @return Always true.
203  */
204 bool
206  return true;
207 }
208 
209 
210 /**
211  * Fall back function which is called if a default pdf viewer is not found.
212  *
213  * @return True, if the user chose a pdf viewer.
214  */
215 bool
217 
219  wxString extension = manual.AfterLast(_T('.'));
220  wxString message = _T("No default PDF viewer found.");
221  message.Append(_T("Please select an executable program to view PDF "));
222  message.Append(_T("files with."));
223  assert(parentWindow() != NULL);
224  ErrorDialog errorDialog(parentWindow(), message);
225  errorDialog.ShowModal();
226 
227  wxFileDialog execDialog(
228  parentWindow(), _T("Choose a pdf viewer executable"), _T(""), _T(""),
229  _T("*"), wxOPEN);
230 
231  if (execDialog.ShowModal() == wxID_OK) {
232  wxString command = execDialog.GetPath();
233  command.Append(_T(" "));
234  command.Append(manual);
235  wxExecute(command);
236  return true;
237  }
238 
239  return false;
240 }
FileSystem.hh
WxConversion::toWxString
static wxString toWxString(const std::string &source)
UserManualCmd::COMMAND_SHORT_NAME
static const std::string COMMAND_SHORT_NAME
Short version of the command name string.
Definition: UserManualCmd.hh:54
UserManualCmd::icon
virtual std::string icon() const
Definition: UserManualCmd.cc:194
UserManualCmd::shortName
virtual std::string shortName() const
Definition: UserManualCmd.cc:185
UserManualCmd::id
virtual int id() const
Definition: UserManualCmd.cc:167
UserManualCmd::~UserManualCmd
virtual ~UserManualCmd()
Definition: UserManualCmd.cc:68
WidgetTools.hh
StringTools.hh
assert
#define assert(condition)
Definition: Application.hh:86
GUICommand
Definition: GUICommand.hh:43
UserManualCmd::COMMAND_ICON
static const std::string COMMAND_ICON
Command icon file name.
Definition: UserManualCmd.hh:56
ErrorDialog
Definition: ErrorDialog.hh:42
UserManualCmd::mimeTypesManagerInitialized_
static bool mimeTypesManagerInitialized_
Tells if the singleton mime types manager has already been initialized.
Definition: UserManualCmd.hh:64
WarningDialog.hh
ErrorDialog.hh
UserManualCmd
Definition: UserManualCmd.hh:40
UserManualCmd::askFromUser
bool askFromUser()
Definition: UserManualCmd.cc:216
UserManualCmd::isEnabled
virtual bool isEnabled()
Definition: UserManualCmd.cc:205
UserManualCmd::UserManualCmd
UserManualCmd()
Definition: UserManualCmd.cc:60
FileSystem::DIRECTORY_SEPARATOR
static const std::string DIRECTORY_SEPARATOR
Definition: FileSystem.hh:189
UserManualCmd::COMMAND_ID
static const int COMMAND_ID
Command ID.
Definition: UserManualCmd.hh:58
StringTools::chopString
static std::vector< TCEString > chopString(const std::string &source, const std::string &delimiters)
Definition: StringTools.cc:181
FileSystem::fileIsExecutable
static bool fileIsExecutable(const std::string fileName)
UserManualCmd::COMMAND_NAME
static const std::string COMMAND_NAME
Command name string.
Definition: UserManualCmd.hh:52
WxConversion.hh
UserManualCmd::create
virtual UserManualCmd * create() const
Definition: UserManualCmd.cc:176
Environment::environmentVariable
static std::string environmentVariable(const std::string &variable)
Definition: Environment.cc:406
DS
#define DS
Definition: LLVMBackend.cc:124
UserManualCmd.hh
GUICommand::parentWindow
wxWindow * parentWindow() const
Definition: GUICommand.cc:75
Environment::pdfManual
static std::string pdfManual()
Definition: Environment.cc:877
UserManualCmd::Do
virtual bool Do()
Definition: UserManualCmd.cc:77