GNU Octave  4.0.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-2015 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 {
182  if (! QFile::exists (settings_file))
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
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 
207  // Get the default custom editor
208 #if defined (Q_OS_WIN32)
209  QString custom_editor = "notepad++ -n%l %f";
210 #else
211  QString custom_editor = "emacs +%l %f";
212 #endif
213 
214  // Replace placeholders
215  settings_text.replace ("__default_custom_editor__", custom_editor);
216  settings_text.replace ("__default_font__", default_family);
217  settings_text.replace ("__default_font_size__", "10");
218 
219  QFile user_settings (settings_file);
220 
221  if (! user_settings.open (QIODevice::WriteOnly))
222  return;
223 
224  QTextStream out (&user_settings);
225 
226  out << settings_text;
227 
228  user_settings.close ();
229  }
230 
232 }
233 
234 void
236 {
237  delete settings;
238  settings = new QSettings (file, QSettings::IniFormat);
239 
240  if (! (settings
241  && QFile::exists (settings->fileName ())
242  && settings->isWritable ()
243  && settings->status () == QSettings::NoError))
244  {
245  QString msg = QString (QT_TR_NOOP (
246  "The settings file\n%1\n"
247  "does not exist and can not be created.\n"
248  "Make sure you have read and write permissions to\n%2\n\n"
249  "Octave GUI must be closed now."));
250  QMessageBox::critical (0, QString (QT_TR_NOOP ("Octave Critical Error")),
251  msg.arg (do_get_settings_file ()).arg (do_get_settings_directory ()));
252  exit (1);
253  }
254 }
255 
256 bool
258 {
259  return ! QFile::exists (settings_file);
260 }
261 
262 void
264 {
265  if (settings)
266  {
267  QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
268 
269  if (settings->value ("useProxyServer",false).toBool ())
270  {
271  QString proxyTypeString = settings->value ("proxyType").toString ();
272 
273  if (proxyTypeString == "Socks5Proxy")
274  proxyType = QNetworkProxy::Socks5Proxy;
275  else if (proxyTypeString == "HttpProxy")
276  proxyType = QNetworkProxy::HttpProxy;
277  }
278 
279  QNetworkProxy proxy;
280 
281  proxy.setType (proxyType);
282  proxy.setHostName (settings->value ("proxyHostName").toString ());
283  proxy.setPort (settings->value ("proxyPort",80).toInt ());
284  proxy.setUser (settings->value ("proxyUserName").toString ());
285  proxy.setPassword (settings->value ("proxyPassword").toString ());
286 
287  QNetworkProxy::setApplicationProxy (proxy);
288  }
289  else
290  {
291  // FIXME: Is this an error? If so, what should we do?
292  }
293 }
294 
295 QStringList
297 {
299 }
300 
303 {
305 }
306 
307 QStringList
309 {
310  return QTerminal::color_names ();
311 }
312 
315 {
316  return QTerminal::default_colors ();
317 }
318 
319 QIcon
320 resource_manager::do_icon (const QString& icon_name, bool fallback)
321 {
322  if (fallback)
323  return QIcon::fromTheme (icon_name,
324  QIcon (":/actions/icons/" + icon_name + ".png"));
325  else
326  return QIcon::fromTheme (icon_name);
327 }
static QStringList storage_class_names(void)
static QString default_qt_settings_file(void)
bool do_is_first_run(void) const
static void cleanup_instance(void)
void error(const char *fmt,...)
Definition: error.cc:476
QString fromStdString(const std::string &s)
void do_set_settings(const QString &file)
QString do_get_settings_file(void)
static QString get_gui_translation_dir(void)
static void config_translators(QTranslator *, QTranslator *, QTranslator *)
static QStringList terminal_color_names(void)
QSettings * do_get_settings(void) const
static QList< QColor > storage_class_default_colors(void)
static QList< QColor > storage_class_default_colors(void)
QSettings * do_get_default_settings(void) const
static void add(fptr f)
QSettings * default_settings
static std::string getenv(const std::string &name)
Definition: oct-env.cc:238
QString do_get_settings_directory(void)
static QSettings * get_settings(void)
static QStringList color_names(void)
Definition: QTerminal.cc:59
static QList< QColor > terminal_default_colors(void)
void do_reload_settings(void)
QSettings * settings
static resource_manager * instance
static bool instance_ok(void)
QIcon do_icon(const QString &icon, bool fallback)
static QList< QColor > default_colors(void)
Definition: QTerminal.cc:43
std::string Voct_etc_dir
Definition: defaults.cc:78
static QStringList storage_class_names(void)
static std::string dir_sep_str(void)
Definition: file-ops.h:63
std::string Voct_locale_dir
Definition: defaults.cc:79
void do_update_network_settings(void)