GNU Octave  4.2.1
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-2017 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 #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_QT5)
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 "resource-manager.h"
49 
51 
52 static QString
54 {
55  std::string dsf = octave::sys::env::getenv ("OCTAVE_DEFAULT_QT_SETTINGS");
56 
57  if (dsf.empty ())
59  "default-qt-settings";
60 
61  return QString::fromStdString (dsf);
62 }
63 
65  : settings_directory (), settings_file (), settings (0),
66  default_settings (0)
67 {
68 #if defined (HAVE_QT4)
69  QString home_path
70  = QDesktopServices::storageLocation (QDesktopServices::HomeLocation);
71 #else
72  QString home_path
73  = QStandardPaths::writableLocation (QStandardPaths::HomeLocation);
74 #endif
75 
76  settings_directory = home_path + "/.config/octave";
77 
78  settings_file = settings_directory + "/qt-settings";
79 
80  default_settings = new QSettings (default_qt_settings_file (),
81  QSettings::IniFormat);
82 }
83 
85 {
86  delete settings;
87  delete default_settings;
88 }
89 
90 QString
92 {
93  // get environment variable for the locale dir (e.g. from run-octave)
94  std::string dldir = octave::sys::env::getenv ("OCTAVE_LOCALE_DIR");
95  if (dldir.empty ())
96  dldir = Voct_locale_dir; // env-var empty, load the default location
97  return QString::fromStdString (dldir);
98 }
99 
100 void
102  QTranslator *qsci_tr,
103  QTranslator *gui_tr)
104 {
105  bool loaded;
106 
107  QString qt_trans_dir
108  = QLibraryInfo::location (QLibraryInfo::TranslationsPath);
109 
110  QString language = "SYSTEM"; // take system language per default
111 
113 
114  if (settings)
115  {
116  // get the locale from the settings if already available
117  language = settings->value ("language","SYSTEM").toString ();
118  }
119 
120  if (language == "SYSTEM")
121  language = QLocale::system ().name (); // get system wide locale
122 
123  // load the translator file for qt strings
124  loaded = qt_tr->load ("qt_" + language, qt_trans_dir);
125 
126  if (! loaded) // try lower case
127  qt_tr->load ("qt_" + language.toLower (), qt_trans_dir);
128 
129  // load the translator file for qscintilla settings
130  loaded = qsci_tr->load ("qscintilla_" + language, qt_trans_dir);
131 
132  if (! loaded) // try lower case
133  qsci_tr->load ("qscintilla_" + language.toLower (), qt_trans_dir);
134 
135  // load the translator file for gui strings
136  gui_tr->load (language, get_gui_translation_dir ());
137 }
138 
139 bool
141 {
142  bool retval = true;
143 
144  if (! instance)
145  instance = new resource_manager ();
146 
147  if (! instance)
148  {
149  error ("unable to create resource_manager object!");
150 
151  retval = false;
152  }
153 
154  return retval;
155 }
156 
157 QSettings *
159 {
160  return settings;
161 }
162 
163 QSettings *
165 {
166  return default_settings;
167 }
168 
169 QString
171 {
172  return settings_directory;
173 }
174 
175 QString
177 {
178  return settings_file;
179 }
180 
181 void
183 {
184  if (! QFile::exists (settings_file))
185  {
186  QDir ("/").mkpath (settings_directory);
187  QFile qt_settings (default_qt_settings_file ());
188 
189  if (! qt_settings.open (QFile::ReadOnly))
190  return;
191 
192  QTextStream in (&qt_settings);
193  QString settings_text = in.readAll ();
194  qt_settings.close ();
195 
196  // Get the default monospaced font
197 #if defined (HAVE_QFONT_MONOSPACE)
198  QFont fixed_font;
199  fixed_font.setStyleHint (QFont::Monospace);
200  QString default_family = fixed_font.defaultFamily ();
201 #elif defined (Q_WS_X11) || defined (Q_WS_WIN)
202  QString default_family = "Courier New";
203 #elif defined (Q_WS_MAC)
204  QString default_family = "Courier";
205 #else
206  QString default_family = "courier";
207 #endif
208 
209  // Get the default custom editor
210 #if defined (Q_OS_WIN32)
211  QString custom_editor = "notepad++ -n%l %f";
212 #else
213  QString custom_editor = "emacs +%l %f";
214 #endif
215 
216  // Replace placeholders
217  settings_text.replace ("__default_custom_editor__", custom_editor);
218  settings_text.replace ("__default_font__", default_family);
219  settings_text.replace ("__default_font_size__", "10");
220 
221  QFile user_settings (settings_file);
222 
223  if (! user_settings.open (QIODevice::WriteOnly))
224  return;
225 
226  QTextStream out (&user_settings);
227 
228  out << settings_text;
229 
230  user_settings.close ();
231  }
232 
234 }
235 
236 void
238 {
239  delete settings;
240  settings = new QSettings (file, QSettings::IniFormat);
241 
242  if (! (settings
243  && QFile::exists (settings->fileName ())
244  && settings->isWritable ()
245  && settings->status () == QSettings::NoError))
246  {
247  QString msg = QString (QT_TR_NOOP (
248  "The settings file\n%1\n"
249  "does not exist and can not be created.\n"
250  "Make sure you have read and write permissions to\n%2\n\n"
251  "Octave GUI must be closed now."));
252  QMessageBox::critical (0, QString (QT_TR_NOOP ("Octave Critical Error")),
253  msg.arg (do_get_settings_file ()).arg (do_get_settings_directory ()));
254  exit (1);
255  }
256 }
257 
258 bool
260 {
261  return ! QFile::exists (settings_file);
262 }
263 
264 void
266 {
267  if (settings)
268  {
269  QNetworkProxy::ProxyType proxyType = QNetworkProxy::NoProxy;
270 
271  if (settings->value ("useProxyServer",false).toBool ())
272  {
273  QString proxyTypeString = settings->value ("proxyType").toString ();
274 
275  if (proxyTypeString == "Socks5Proxy")
276  proxyType = QNetworkProxy::Socks5Proxy;
277  else if (proxyTypeString == "HttpProxy")
278  proxyType = QNetworkProxy::HttpProxy;
279  }
280 
281  QNetworkProxy proxy;
282 
283  proxy.setType (proxyType);
284  proxy.setHostName (settings->value ("proxyHostName").toString ());
285  proxy.setPort (settings->value ("proxyPort",80).toInt ());
286  proxy.setUser (settings->value ("proxyUserName").toString ());
287  proxy.setPassword (settings->value ("proxyPassword").toString ());
288 
289  QNetworkProxy::setApplicationProxy (proxy);
290  }
291  else
292  {
293  // FIXME: Is this an error? If so, what should we do?
294  }
295 }
296 
297 QStringList
299 {
301 }
302 
305 {
307 }
308 
309 QStringList
311 {
312  return QTerminal::color_names ();
313 }
314 
317 {
318  return QTerminal::default_colors ();
319 }
320 
321 QIcon
322 resource_manager::do_icon (const QString& icon_name, bool fallback)
323 {
324  if (fallback)
325  return QIcon::fromTheme (icon_name,
326  QIcon (":/actions/icons/" + icon_name + ".png"));
327  else
328  return QIcon::fromTheme (icon_name);
329 }
330 
331 // initialize a given combo box with available text encodings
332 void
333 resource_manager::do_combo_encoding (QComboBox *combo, QString current)
334 {
335  // get the codec name for each mib
336  QList<int> all_mibs = QTextCodec::availableMibs ();
337  QStringList all_codecs;
338  foreach (int mib, all_mibs)
339  {
340  QTextCodec *c = QTextCodec::codecForMib (mib);
341  all_codecs << c->name ().toUpper ();
342  }
343  all_codecs.removeDuplicates ();
344  qSort (all_codecs);
345 
346  // the default encoding
347 #if defined (Q_OS_WIN32)
348  QString def_enc = "SYSTEM";
349 #else
350  QString def_enc = "UTF-8";
351 #endif
352 
353  // get the value from the settings file if no current encoding is given
354  QString enc = current;
355  if (enc.isEmpty ())
356  {
357  enc = settings->value ("editor/default_encoding",def_enc).toString ();
358  if (enc.isEmpty ()) // still empty?
359  enc = def_enc; // take default
360  }
361 
362  // fill the combo box
363  foreach (QString c, all_codecs)
364  combo->addItem (c);
365 
366  // prepend the default item
367  combo->insertSeparator (0);
368  combo->insertItem (0, def_enc);
369 
370  // select the current/default item
371  int idx = combo->findText (enc);
372  if (idx >= 0)
373  combo->setCurrentIndex (idx);
374  else
375  combo->setCurrentIndex (0);
376 
377  combo->setMaxVisibleItems (12);
378 }
void do_combo_encoding(QComboBox *combo, QString current)
static std::string dir_sep_str(void)
Definition: file-ops.h:80
static QStringList storage_class_names(void)
static QString default_qt_settings_file(void)
bool do_is_first_run(void) const
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:120
void error(const char *fmt,...)
Definition: error.cc:570
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 std::string getenv(const std::string &name)
Definition: oct-env.cc:235
static QList< QColor > storage_class_default_colors(void)
static QList< QColor > storage_class_default_colors(void)
QSettings * do_get_default_settings(void) const
QSettings * default_settings
QString do_get_settings_directory(void)
octave_value retval
Definition: data.cc:6294
the sparsity preserving column transformation such that that defines the pivoting threshold can be given in which case it defines the c
Definition: lu.cc:138
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
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 text mode reading and writing automatically converts linefeeds to the appropriate line end character for the system(carriage-return linefeed on Windows, carriage-return on Macintosh).The default when no mode is specified is binary mode.Additionally
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:75
static QStringList storage_class_names(void)
std::string Voct_locale_dir
Definition: defaults.cc:76
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:854
void do_update_network_settings(void)