USGS

Isis 3.0 Object Programmers' Reference

Home

Environment.cpp
1 #include "Environment.h"
2 
3 #include <stdlib.h>
4 
5 #include <QCoreApplication>
6 
7 #ifndef __APPLE__
8 #include <QtDBus>
9 #endif
10 
11 #include "IException.h"
12 #include "IString.h"
13 #include "TextFile.h"
14 
15 
16 namespace Isis {
18 
19  Environment::Environment() {
20  // Set the Qt plugin directory
21  QStringList pluginPaths;
22 
23  IString root = getEnvironmentValue("ISISROOT", "");
24 
25  if (root != "") {
26  IString thirdPartyPluginPath = root + "/3rdParty/plugins";
27  pluginPaths << thirdPartyPluginPath.ToQt();
28  QCoreApplication::setLibraryPaths(pluginPaths);
29  }
30 
31 #ifndef __APPLE__
32  // We need to force the correct QDBus library to be loaded... to do that, just
33  // use a symbol in it's library. This only applies to linux and fixes #1228.
34 
35  // Long explanation:
36  // When we run GUI apps, the system (and Qt) work together to figure out
37  // which runtime libraries are necessary on-the-fly. When QApplication is
38  // instantiated, it goes into QtGui's style code. The styles ignore our plugin
39  // path setting (above) on all OS's. So Qt GUI grabs a style from the OS's styles,
40  // which is a shared library in the kde area. These styles require a version (any version)
41  // of QtDBus loaded. If QtDBus is not yet loaded, then the style library will grab it.
42  // However, on Ubuntu 12.04, the style library grabs the system (OS) QDBus library. QDBus
43  // detects that you've already loaded Isis' QtCore, so the library versions mismatch, and
44  // it crashes. The problem becomes more interesting because sometimes it picks up the system
45  // QDBus, and sometimes it picks up Isis' QDBus, and I have no good reason why we pick up
46  // one versus another; currently, installed apps pick up the system and locally built apps
47  // pick up Isis' (even when the executables are made to be identical). The end result is no
48  // installed GUI applications will run and our automated tests fail to catch it. This solution
49  // bypasses the entire issue by forcing QDBus to be loaded long before any styles are loaded,
50  // so the style plugins do not need to go and get their own QDBus library.
51  //
52  // The root cause is that Ubuntu's run time loader is failing to respect
53  // our executable's rpaths when loading a style library. However, when we link against the
54  // QBus library directly, we get the right one.
55  QDBusArgument();
56 #endif
57  }
58 
59 
64  return getEnvironmentValue("USER", "Unknown");
65  }
66 
67 
72  return getEnvironmentValue("HOST", "Unknown");
73  }
74 
75 
83  QString Environment::getEnvironmentValue(QString variable,
84  QString defaultValue) {
85 
86  QString value = defaultValue;
87 
88  char *envValue = getenv(variable.toAscii().data());
89  if (envValue)
90  value = envValue;
91 
92  return value;
93  }
94 
95 
100  TextFile versionFile("$ISISROOT/version");
101  QString line1, line2, line3, line4;
102  versionFile.GetLine(line1);
103  versionFile.GetLine(line2);
104  versionFile.GetLine(line3);
105  versionFile.GetLine(line4);
106 
107  QRegExp validPartOfLine("[^ #]*");
108  if (validPartOfLine.indexIn(line1) != -1) {
109  line1 = validPartOfLine.cap();
110  }
111  else {
112  IString msg = "$ISISROOT/version line 1, no valid text found";
114  }
115 
116  if (validPartOfLine.indexIn(line2) != -1) {
117  line2 = validPartOfLine.cap();
118  }
119  else {
120  IString msg = "$ISISROOT/version line 2, no valid text found";
122  }
123 
124  if (validPartOfLine.indexIn(line4) != -1) {
125  line4 = validPartOfLine.cap();
126  }
127  else {
128  IString msg = "$ISISROOT/version line 4, no valid text found";
130  }
131 
132  return line1 + " " + line4 + " | " + line2;
133  }
134 }