apprunner.cpp
1 //------------------------------------------------------------------------------
2 // apprunner.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but 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, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "apprunner.h"
24 
25 #include "gui/standardserverconsole.h"
26 #include "serverapi/message.h"
27 #include "log.h"
28 #include "strings.h"
29 #include <QProcess>
30 
32 {
33  return !executable.filePath().isEmpty();
34 }
35 
36 void AppRunner::cleanArguments(QStringList& args)
37 {
38  QStringList::iterator it;
39  for (it = args.begin(); it != args.end(); ++it)
40  {
41  if (it->contains(" "))
42  {
43  Strings::trim(*it, "\"");
44  }
45  }
46 }
47 
48 #ifdef Q_WS_MAC
49 QString AppRunner::findBundleBinary(const QFileInfo &file)
50 {
51  // Scan the plist file for where the real binary is in a bundle. We have
52  // to do this because some bundles (ZDaemon) don't like the --args method
53  // and I heard that only works on Mac OS X 10.6 anywyas.
54  QFile pLists(file.canonicalFilePath() + "/Contents/Info.plist");
55  if(!pLists.open(QIODevice::ReadOnly | QIODevice::Text))
56  {
57  gLog << tr("Could not read bundle plist. (%1)").arg(file.canonicalFilePath() + "/Contents/Info.plist");
58  return QString();
59  }
60 
61  char line[128];
62  bool keyFound = false;
63  while(pLists.readLine(line, 128) != -1)
64  {
65  if(!keyFound)
66  {
67  if(QString(line).trimmed() == "<key>CFBundleExecutable</key>")
68  keyFound = true;
69  }
70  else
71  {
72  QString binaryLine(line);
73  binaryLine = binaryLine.trimmed();
74  if(binaryLine.startsWith("<string>") && binaryLine.endsWith("</string>"))
75  return QString("/Contents/MacOS/") + binaryLine.mid(8, binaryLine.indexOf("</string>")-8);
76  keyFound = false;
77  }
78 
79  }
80  return QString();
81 }
82 #endif
83 
84 Message AppRunner::runExecutable(const CommandLineInfo& cmdInfo)
85 {
86  gLog << tr("Starting (working dir %1): %2").arg(cmdInfo.applicationDir.absolutePath()).arg(cmdInfo.executable.absoluteFilePath());
87  QStringList args = cmdInfo.args;
88  cleanArguments(args);
89 
90  int result;
91 
92  #ifdef Q_WS_MAC
93  if( cmdInfo.executable.isBundle() )
94  {
95  result = QProcess::startDetached(cmdInfo.executable.absoluteFilePath() + AppRunner::findBundleBinary(cmdInfo.executable), args, cmdInfo.applicationDir.absolutePath());
96  }
97  else
98  #endif
99  {
100  result = QProcess::startDetached(cmdInfo.executable.absoluteFilePath(), args, cmdInfo.applicationDir.absolutePath());
101  }
102 
103  Message message;
104 
105  if (!result)
106  {
107  QString error = tr("File: %1\ncannot be run").arg(cmdInfo.executable.absoluteFilePath());
108  gLog << error;
109  message = Message::customError(error);
110  return message;
111  }
112 
113  return message;
114 }
115 
117  const QIcon &icon, const CommandLineInfo &cli)
118 {
119  gLog << tr("Starting (working dir %1): %2").arg(cli.applicationDir.absolutePath())
120  .arg(cli.executable.absoluteFilePath());
121  QStringList args = cli.args;
122  cleanArguments(args);
123  new StandardServerConsole(icon, cli.executable.absoluteFilePath(), args);
124 }
Structure holding parameters for application launch.
Definition: apprunner.h:37
Message object used to pass messages throughout the Doomseeker's system.
Definition: message.h:62
QStringList args
launch parameters
Definition: apprunner.h:43
static void runExecutableWrappedInStandardServerConsole(const QIcon &icon, const CommandLineInfo &cli)
Executes predefined command line.
Definition: apprunner.cpp:116
static void cleanArguments(QStringList &args)
On Windows this removes any wrapping " chars.
Definition: apprunner.cpp:36
QDir applicationDir
working directory
Definition: apprunner.h:41
QFileInfo executable
path to the executable
Definition: apprunner.h:45
static Message customError(const QString &content)
Convenience method. Sets type to Type::CUSTOM_ERROR.
Definition: message.h:122
bool isValid() const
It's valid when at least executable is set.
Definition: apprunner.cpp:31