GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
resource-manager.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2011-2018 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
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License 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 <https://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if defined (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 #if defined (HAVE_QSTANDARDPATHS)
35 # include <QStandardPaths>
36 #endif
37 #include <QTextCodec>
38 
39 #include "error.h"
40 #include "file-ops.h"
41 #include "help.h"
42 #include "oct-env.h"
43 
44 #include "defaults.h"
45 
46 #include "QTerminal.h"
47 #include "workspace-model.h"
48 #include "variable-editor.h"
49 #include "resource-manager.h"
50 
51 namespace octave
52 {
54 
55  static QString
57  {
58  std::string dsf = octave::sys::env::getenv ("OCTAVE_DEFAULT_QT_SETTINGS");
59 
60  if (dsf.empty ())
63  + "default-qt-settings");
64 
65  return QString::fromStdString (dsf);
66  }
67 
69  : m_settings_directory (), m_settings_file (), m_settings (nullptr),
70  m_default_settings (nullptr)
71  {
72 #if defined (HAVE_QSTANDARDPATHS)
73  QString home_path
74  = QStandardPaths::writableLocation (QStandardPaths::HomeLocation);
75 #else
76  QString home_path
77  = QDesktopServices::storageLocation (QDesktopServices::HomeLocation);
78 #endif
79 
80  m_settings_directory = home_path + "/.config/octave";
81 
82  m_settings_file = m_settings_directory + "/qt-settings";
83 
84  m_default_settings = new QSettings (default_qt_settings_file (),
85  QSettings::IniFormat);
86  }
87 
89  {
90  delete m_settings;
91  delete m_default_settings;
92  }
93 
95  {
96  // get environment variable for the locale dir (e.g. from run-octave)
97  std::string dldir = octave::sys::env::getenv ("OCTAVE_LOCALE_DIR");
98  if (dldir.empty ())
99  dldir = octave::config::oct_locale_dir (); // env-var empty, load the default location
100  return QString::fromStdString (dldir);
101  }
102 
103  void resource_manager::config_translators (QTranslator *qt_tr,
104  QTranslator *qsci_tr,
105  QTranslator *gui_tr)
106  {
107  bool loaded;
108 
109  QString qt_trans_dir
110  = QLibraryInfo::location (QLibraryInfo::TranslationsPath);
111 
112  QString language = "SYSTEM"; // take system language per default
113 
114  QSettings *settings = resource_manager::get_settings ();
115 
116  if (settings)
117  {
118  // get the locale from the settings if already available
119  language = settings->value ("language", "SYSTEM").toString ();
120  }
121 
122  if (language == "SYSTEM")
123  language = QLocale::system ().name (); // get system wide locale
124 
125  // load the translator file for qt strings
126  loaded = qt_tr->load ("qt_" + language, qt_trans_dir);
127 
128  if (! loaded) // try lower case
129  qt_tr->load ("qt_" + language.toLower (), qt_trans_dir);
130 
131  // load the translator file for qscintilla settings
132  loaded = qsci_tr->load ("qscintilla_" + language, qt_trans_dir);
133 
134  if (! loaded) // try lower case
135  qsci_tr->load ("qscintilla_" + language.toLower (), qt_trans_dir);
136 
137  // load the translator file for gui strings
138  gui_tr->load (language, get_gui_translation_dir ());
139  }
140 
142  {
144  }
145 
147  {
149  }
150 
152  {
153  return QTerminal::color_names ();
154  }
155 
157  {
158  return QTerminal::default_colors ();
159  }
160 
162  {
164  }
165 
167  {
169  }
170 
172  {
173  bool retval = true;
174 
175  if (! instance)
176  instance = new resource_manager ();
177 
178  if (! instance)
179  {
180  error ("unable to create resource_manager object!");
181 
182  retval = false;
183  }
184 
185  return retval;
186  }
187 
188  QSettings * resource_manager::do_get_settings (void) const
189  {
190  return m_settings;
191  }
192 
194  {
195  return m_default_settings;
196  }
197 
199  {
200  return m_settings_directory;
201  }
202 
204  {
205  return m_settings_file;
206  }
207 
209  {
210  if (! QFile::exists (m_settings_file))
211  {
212  QDir ("/").mkpath (m_settings_directory);
213  QFile qt_settings (default_qt_settings_file ());
214 
215  if (! qt_settings.open (QFile::ReadOnly))
216  return;
217 
218  QTextStream in (&qt_settings);
219  QString settings_text = in.readAll ();
220  qt_settings.close ();
221 
222  // Get the default monospaced font
223 #if defined (HAVE_QFONT_MONOSPACE)
224  QFont fixed_font;
225  fixed_font.setStyleHint (QFont::Monospace);
226  QString default_family = fixed_font.defaultFamily ();
227 #elif defined (Q_WS_X11) || defined (Q_WS_WIN)
228  QString default_family = "Courier New";
229 #elif defined (Q_WS_MAC)
230  QString default_family = "Courier";
231 #else
232  QString default_family = "courier";
233 #endif
234 
235  // Get the default custom editor
236 #if defined (Q_OS_WIN32)
237  QString custom_editor = "notepad++ -n%l %f";
238 #else
239  QString custom_editor = "emacs +%l %f";
240 #endif
241 
242  // Replace placeholders
243  settings_text.replace ("__default_custom_editor__", custom_editor);
244  settings_text.replace ("__default_font__", default_family);
245  settings_text.replace ("__default_font_size__", "10");
246 
247  QFile user_settings (m_settings_file);
248 
249  if (! user_settings.open (QIODevice::WriteOnly))
250  return;
251 
252  QTextStream out (&user_settings);
253 
254  out << settings_text;
255 
256  user_settings.close ();
257  }
258 
260  }
261 
263  {
264  delete m_settings;
265  m_settings = new QSettings (file, QSettings::IniFormat);
266 
267  if (! (m_settings
268  && QFile::exists (m_settings->fileName ())
269  && m_settings->isWritable ()
270  && m_settings->status () == QSettings::NoError))
271  {
272  QString msg = QString (QT_TR_NOOP (
273  "The settings file\n%1\n"
274  "does not exist and can not be created.\n"
275  "Make sure you have read and write permissions to\n%2\n\n"
276  "Octave GUI must be closed now."));
277  QMessageBox::critical (nullptr, QString (QT_TR_NOOP ("Octave Critical Error")),
278  msg.arg (do_get_settings_file ()).arg (do_get_settings_directory ()));
279  exit (1);
280  }
281  }
282 
283  bool resource_manager::do_update_settings_key (const QString& old_key,
284  const QString& new_key)
285  {
286  if (m_settings->contains (old_key))
287  {
288  QVariant preference = m_settings->value (old_key);
289  m_settings->setValue (new_key, preference);
290  m_settings->remove (old_key);
291  return true;
292  }
293 
294  return false;
295  }
296 
298  {
299  return ! QFile::exists (m_settings_file);
300  }
301 
303  {
304  if (m_settings)
305  {
306  QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
307 
308  if (m_settings->value ("useProxyServer",false).toBool ())
309  {
310  QString proxyTypeString = m_settings->value ("proxyType").toString ();
311 
312  if (proxyTypeString == "Socks5Proxy")
313  proxyType = QNetworkProxy::Socks5Proxy;
314  else if (proxyTypeString == "HttpProxy")
315  proxyType = QNetworkProxy::HttpProxy;
316  }
317 
318  QNetworkProxy proxy;
319 
320  proxy.setType (proxyType);
321  proxy.setHostName (m_settings->value ("proxyHostName").toString ());
322  proxy.setPort (m_settings->value ("proxyPort",80).toInt ());
323  proxy.setUser (m_settings->value ("proxyUserName").toString ());
324  proxy.setPassword (m_settings->value ("proxyPassword").toString ());
325 
326  QNetworkProxy::setApplicationProxy (proxy);
327  }
328  else
329  {
330  // FIXME: Is this an error? If so, what should we do?
331  }
332  }
333 
334  QIcon resource_manager::do_icon (const QString& icon_name, bool fallback)
335  {
336  if (fallback)
337  return QIcon::fromTheme (icon_name,
338  QIcon (":/actions/icons/" + icon_name + ".png"));
339  else
340  return QIcon::fromTheme (icon_name);
341  }
342 
343  // initialize a given combo box with available text encodings
344  void resource_manager::do_combo_encoding (QComboBox *combo, QString current)
345  {
346  // get the codec name for each mib
347  QList<int> all_mibs = QTextCodec::availableMibs ();
348  QStringList all_codecs;
349  foreach (int mib, all_mibs)
350  {
351  QTextCodec *c = QTextCodec::codecForMib (mib);
352  all_codecs << c->name ().toUpper ();
353  }
354  all_codecs.removeDuplicates ();
355  qSort (all_codecs);
356 
357  // the default encoding
358 #if defined (Q_OS_WIN32)
359  QString def_enc = "SYSTEM";
360 #else
361  QString def_enc = "UTF-8";
362 #endif
363 
364  // get the value from the settings file if no current encoding is given
365  QString enc = current;
366  if (enc.isEmpty ())
367  {
368  enc = m_settings->value ("editor/default_encoding",def_enc).toString ();
369  if (enc.isEmpty ()) // still empty?
370  enc = def_enc; // take default
371  }
372 
373  // fill the combo box
374  foreach (QString c, all_codecs)
375  combo->addItem (c);
376 
377  // prepend the default item
378  combo->insertSeparator (0);
379  combo->insertItem (0, def_enc);
380 
381  // select the current/default item
382  int idx = combo->findText (enc);
383  if (idx >= 0)
384  combo->setCurrentIndex (idx);
385  else
386  combo->setCurrentIndex (0);
387 
388  combo->setMaxVisibleItems (12);
389  }
390 }
static QStringList color_names(void)
static QString default_qt_settings_file(void)
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
static void config_translators(QTranslator *, QTranslator *, QTranslator *)
static QList< QColor > terminal_default_colors(void)
std::string oct_etc_dir(void)
Definition: defaults.cc:298
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the system(carriage-return linefeed on Windows). The default when no mode is specified is binary. Additionally
void error(const char *fmt,...)
Definition: error.cc:578
QString fromStdString(const std::string &s)
bool do_is_first_run(void) const
QSettings * do_get_settings(void) const
nd example oindent opens the file binary numeric values will be read assuming they are stored in IEEE format with the least significant bit and then converted to the native representation Opening a file that is already open simply opens it again and returns a separate file id It is not an error to open a file several though writing to the same file through several different file ids may produce unexpected results The possible values of text mode reading and writing automatically converts linefeeds to the appropriate line end character for the you may append a you must also open the file in binary mode The parameter conversions are currently only supported for and permissions will be set to and then everything is written in a single operation This is very efficient and improves performance c
Definition: file-io.cc:587
QString do_get_settings_directory(void)
std::string dir_sep_str(void)
Definition: file-ops.cc:233
static std::string getenv(const std::string &name)
Definition: oct-env.cc:235
static QList< QColor > varedit_default_colors(void)
std::string oct_locale_dir(void)
Definition: defaults.cc:302
QString do_get_settings_file(void)
static QList< QColor > default_colors(void)
octave_value retval
Definition: data.cc:6246
static QList< QColor > storage_class_default_colors(void)
static QStringList storage_class_names(void)
static resource_manager * instance
static QStringList color_names(void)
Definition: QTerminal.cc:59
static QStringList varedit_color_names(void)
void do_combo_encoding(QComboBox *combo, QString current)
static QList< QColor > storage_class_default_colors(void)
static QStringList storage_class_names(void)
static QList< QColor > default_colors(void)
Definition: QTerminal.cc:43
static QSettings * get_settings(void)
static bool instance_ok(void)
QIcon do_icon(const QString &icon, bool fallback)
QSettings * do_get_default_settings(void) const
static QStringList terminal_color_names(void)
static QString get_gui_translation_dir(void)
bool do_update_settings_key(const QString &new_key, const QString &old_key)
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
void do_set_settings(const QString &file)