GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
resource-manager.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2013 Jacob Dawid
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <string>
28 
29 #include <QFile>
30 #include <QDir>
31 #include <QNetworkProxy>
32 #include <QLibraryInfo>
33 #include <QMessageBox>
34 
35 #include "error.h"
36 #include "file-ops.h"
37 #include "help.h"
38 #include "oct-env.h"
39 #include "singleton-cleanup.h"
40 
41 #include "defaults.h"
42 
43 #include "QTerminal.h"
44 #include "workspace-model.h"
45 #include "resource-manager.h"
46 
48 
49 static QString
51 {
52  std::string dsf = octave_env::getenv ("OCTAVE_DEFAULT_QT_SETTINGS");
53 
54  if (dsf.empty ())
55  dsf = Voct_etc_dir + file_ops::dir_sep_str () + "default-qt-settings";
56 
57  return QString::fromStdString (dsf);
58 }
59 
61  : settings_directory (), settings_file (), settings (0),
62  default_settings (0)
63 {
64  QDesktopServices desktopServices;
65 
66  QString home_path
67  = desktopServices.storageLocation (QDesktopServices::HomeLocation);
68 
69  settings_directory = home_path + "/.config/octave";
70 
71  settings_file = settings_directory + "/qt-settings";
72 
73  default_settings = new QSettings (default_qt_settings_file (),
74  QSettings::IniFormat);
75 }
76 
78 {
79  delete settings;
80  delete default_settings;
81 }
82 
83 QString
85 {
86  // get environment variable for the locale dir (e.g. from run-octave)
87  std::string dldir = octave_env::getenv ("OCTAVE_LOCALE_DIR");
88  if (dldir.empty ())
89  dldir = Voct_locale_dir; // env-var empty, load the default location
90  return QString::fromStdString (dldir);
91 }
92 
93 void
95  QTranslator *qsci_tr,
96  QTranslator *gui_tr)
97 {
98  bool loaded;
99 
100  QString qt_trans_dir
101  = QLibraryInfo::location (QLibraryInfo::TranslationsPath);
102 
103  QString language = "SYSTEM"; // take system language per default
104 
106 
107  if (settings)
108  {
109  // get the locale from the settings if already available
110  language = settings->value ("language","SYSTEM").toString ();
111  }
112 
113  if (language == "SYSTEM")
114  language = QLocale::system ().name (); // get system wide locale
115 
116  // load the translator file for qt strings
117  loaded = qt_tr->load ("qt_" + language, qt_trans_dir);
118 
119  if (!loaded) // try lower case
120  qt_tr->load ("qt_" + language.toLower (), qt_trans_dir);
121 
122  // load the translator file for qscintilla settings
123  loaded = qsci_tr->load ("qscintilla_" + language, qt_trans_dir);
124 
125  if (!loaded) // try lower case
126  qsci_tr->load ("qscintilla_" + language.toLower (), qt_trans_dir);
127 
128  // load the translator file for gui strings
129  gui_tr->load (language, get_gui_translation_dir ());
130 }
131 
132 bool
134 {
135  bool retval = true;
136 
137  if (! instance)
138  {
139  instance = new resource_manager ();
140 
141  if (instance)
143  }
144 
145  if (! instance)
146  {
147  ::error ("unable to create resource_manager object!");
148 
149  retval = false;
150  }
151 
152  return retval;
153 }
154 
155 QSettings *
157 {
158  return settings;
159 }
160 
161 QSettings *
163 {
164  return default_settings;
165 }
166 
167 QString
169 {
170  return settings_directory;
171 }
172 
173 QString
175 {
176  return settings_file;
177 }
178 
179 void
181 {
183  {
184  QDir ("/").mkpath (settings_directory);
185  QFile qt_settings (default_qt_settings_file ());
186 
187  if (!qt_settings.open (QFile::ReadOnly))
188  return;
189 
190  QTextStream in (&qt_settings);
191  QString settings_text = in.readAll ();
192  qt_settings.close ();
193 
194  // Get the default monospaced font and replace placeholder
195 #if defined (HAVE_QFONT_MONOSPACE)
196  QFont fixed_font;
197  fixed_font.setStyleHint (QFont::Monospace);
198  QString default_family = fixed_font.defaultFamily ();
199 #elif defined (Q_WS_X11) || defined (Q_WS_WIN)
200  QString default_family = "Courier New";
201 #elif defined (Q_WS_MAC)
202  QString default_family = "Courier";
203 #else
204  QString default_family = "courier";
205 #endif
206  settings_text.replace ("__default_font__", default_family);
207  settings_text.replace ("__default_font_size__", "10");
208 
209  QFile user_settings (settings_file);
210 
211  if (! user_settings.open (QIODevice::WriteOnly))
212  return;
213 
214  QTextStream out (&user_settings);
215 
216  out << settings_text;
217 
218  user_settings.close ();
219  }
220 
222 }
223 
224 void
226 {
227  delete settings;
228  settings = new QSettings (file, QSettings::IniFormat);
229 
230  if (! (settings
231  && QFile::exists (settings->fileName ())
232  && settings->isWritable ()
233  && settings->status () == QSettings::NoError))
234  {
235  QString msg = QString (QT_TR_NOOP ("The settings file\n%1\n"
236  "does not exist and can not be created.\n"
237  "Make sure you have read and write permissions to\n%2\n\n"
238  "Octave GUI must be closed now."));
239  QMessageBox::critical (0, QString (QT_TR_NOOP ("Octave Critical Error")),
240  msg.arg (do_get_settings_file ()).arg (do_get_settings_directory ()));
241  exit (1);
242  }
243 }
244 
245 bool
247 {
248  return ! QFile::exists (settings_file);
249 }
250 
251 void
253 {
254  if (settings)
255  {
256  QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
257 
258  if (settings->value ("useProxyServer",false).toBool ())
259  {
260  QString proxyTypeString = settings->value ("proxyType").toString ();
261 
262  if (proxyTypeString == "Socks5Proxy")
263  proxyType = QNetworkProxy::Socks5Proxy;
264  else if (proxyTypeString == "HttpProxy")
265  proxyType = QNetworkProxy::HttpProxy;
266  }
267 
268  QNetworkProxy proxy;
269 
270  proxy.setType (proxyType);
271  proxy.setHostName (settings->value ("proxyHostName").toString ());
272  proxy.setPort (settings->value ("proxyPort",80).toInt ());
273  proxy.setUser (settings->value ("proxyUserName").toString ());
274  proxy.setPassword (settings->value ("proxyPassword").toString ());
275 
276  QNetworkProxy::setApplicationProxy (proxy);
277  }
278  else
279  {
280  // FIXME: Is this an error? If so, what should we do?
281  }
282 }
283 
284 QStringList
286 {
288 }
289 
292 {
294 }
295 
296 QStringList
298 {
299  return QTerminal::color_names ();
300 }
301 
304 {
305  return QTerminal::default_colors ();
306 }